2 * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 39 Peer source hash based selection */
12 #include "CachePeer.h"
13 #include "HttpRequest.h"
14 #include "mgr/Registration.h"
15 #include "neighbors.h"
16 #include "PeerSelectState.h"
17 #include "SquidConfig.h"
22 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
24 static int n_sourcehash_peers
= 0;
25 static CachePeer
**sourcehash_peers
= NULL
;
26 static OBJH peerSourceHashCachemgr
;
27 static void peerSourceHashRegisterWithCacheManager(void);
30 peerSortWeight(const void *a
, const void *b
)
32 const CachePeer
*const *p1
= (const CachePeer
*const *)a
;
33 const CachePeer
*const *p2
= (const CachePeer
*const *)b
;
34 return (*p1
)->weight
- (*p2
)->weight
;
38 peerSourceHashInit(void)
43 double P_last
, X_last
, Xn
;
49 for (k
= 0; k
< n_sourcehash_peers
; ++k
) {
50 cbdataReferenceDone(sourcehash_peers
[k
]);
53 safe_free(sourcehash_peers
);
54 n_sourcehash_peers
= 0;
55 /* find out which peers we have */
57 for (p
= Config
.peers
; p
; p
= p
->next
) {
58 if (!p
->options
.sourcehash
)
61 assert(p
->type
== PEER_PARENT
);
71 peerSourceHashRegisterWithCacheManager();
73 if (n_sourcehash_peers
== 0)
76 sourcehash_peers
= (CachePeer
**)xcalloc(n_sourcehash_peers
, sizeof(*sourcehash_peers
));
78 /* Build a list of the found peers and calculate hashes and load factors */
79 for (P
= sourcehash_peers
, p
= Config
.peers
; p
; p
= p
->next
) {
80 if (!p
->options
.sourcehash
)
86 /* calculate this peers hash */
87 p
->sourcehash
.hash
= 0;
89 for (t
= p
->name
; *t
!= 0; ++t
)
90 p
->sourcehash
.hash
+= ROTATE_LEFT(p
->sourcehash
.hash
, 19) + (unsigned int) *t
;
92 p
->sourcehash
.hash
+= p
->sourcehash
.hash
* 0x62531965;
94 p
->sourcehash
.hash
= ROTATE_LEFT(p
->sourcehash
.hash
, 21);
97 p
->sourcehash
.load_factor
= ((double) p
->weight
) / (double) W
;
99 if (floor(p
->sourcehash
.load_factor
* 1000.0) == 0.0)
100 p
->sourcehash
.load_factor
= 0.0;
102 /* add it to our list of peers */
103 *P
++ = cbdataReference(p
);
106 /* Sort our list on weight */
107 qsort(sourcehash_peers
, n_sourcehash_peers
, sizeof(*sourcehash_peers
), peerSortWeight
);
109 /* Calculate the load factor multipliers X_k
111 * X_1 = pow ((K*p_1), (1/K))
112 * X_k = ([K-k+1] * [P_k - P_{k-1}])/(X_1 * X_2 * ... * X_{k-1})
113 * X_k += pow ((X_{k-1}, {K-k+1})
114 * X_k = pow (X_k, {1/(K-k+1)})
115 * simplified to have X_1 part of the loop
117 K
= n_sourcehash_peers
;
119 P_last
= 0.0; /* Empty P_0 */
121 Xn
= 1.0; /* Empty starting point of X_1 * X_2 * ... * X_{x-1} */
123 X_last
= 0.0; /* Empty X_0, nullifies the first pow statement */
125 for (k
= 1; k
<= K
; ++k
) {
126 double Kk1
= (double) (K
- k
+ 1);
127 p
= sourcehash_peers
[k
- 1];
128 p
->sourcehash
.load_multiplier
= (Kk1
* (p
->sourcehash
.load_factor
- P_last
)) / Xn
;
129 p
->sourcehash
.load_multiplier
+= pow(X_last
, Kk1
);
130 p
->sourcehash
.load_multiplier
= pow(p
->sourcehash
.load_multiplier
, 1.0 / Kk1
);
131 Xn
*= p
->sourcehash
.load_multiplier
;
132 X_last
= p
->sourcehash
.load_multiplier
;
133 P_last
= p
->sourcehash
.load_factor
;
138 peerSourceHashRegisterWithCacheManager(void)
140 Mgr::RegisterAction("sourcehash", "peer sourcehash information",
141 peerSourceHashCachemgr
, 0, 1);
145 peerSourceHashSelectParent(PeerSelector
*ps
)
151 unsigned int user_hash
= 0;
152 unsigned int combined_hash
;
154 double high_score
= 0;
155 const char *key
= NULL
;
156 char ntoabuf
[MAX_IPSTRLEN
];
158 if (n_sourcehash_peers
== 0)
162 HttpRequest
*request
= ps
->request
;
164 key
= request
->client_addr
.toStr(ntoabuf
, sizeof(ntoabuf
));
166 /* calculate hash key */
167 debugs(39, 2, "peerSourceHashSelectParent: Calculating hash for " << key
);
169 for (c
= key
; *c
!= 0; ++c
)
170 user_hash
+= ROTATE_LEFT(user_hash
, 19) + *c
;
172 /* select CachePeer */
173 for (k
= 0; k
< n_sourcehash_peers
; ++k
) {
174 tp
= sourcehash_peers
[k
];
175 combined_hash
= (user_hash
^ tp
->sourcehash
.hash
);
176 combined_hash
+= combined_hash
* 0x62531965;
177 combined_hash
= ROTATE_LEFT(combined_hash
, 21);
178 score
= combined_hash
* tp
->sourcehash
.load_multiplier
;
179 debugs(39, 3, "peerSourceHashSelectParent: " << tp
->name
<< " combined_hash " << combined_hash
<<
180 " score " << std::setprecision(0) << score
);
182 if ((score
> high_score
) && peerHTTPOkay(tp
, ps
)) {
189 debugs(39, 2, "peerSourceHashSelectParent: selected " << p
->name
);
195 peerSourceHashCachemgr(StoreEntry
* sentry
)
199 storeAppendPrintf(sentry
, "%24s %10s %10s %10s %10s\n",
206 for (p
= Config
.peers
; p
; p
= p
->next
)
207 sumfetches
+= p
->stats
.fetches
;
209 for (p
= Config
.peers
; p
; p
= p
->next
) {
210 storeAppendPrintf(sentry
, "%24s %10x %10f %10f %10f\n",
211 p
->name
, p
->sourcehash
.hash
,
212 p
->sourcehash
.load_multiplier
,
213 p
->sourcehash
.load_factor
,
214 sumfetches
? (double) p
->stats
.fetches
/ sumfetches
: -1.0);