2 * Copyright (C) 1996-2017 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 user hash based selection */
15 #include "auth/UserRequest.h"
16 #include "CachePeer.h"
18 #include "HttpRequest.h"
19 #include "mgr/Registration.h"
20 #include "neighbors.h"
21 #include "SquidConfig.h"
26 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
28 static int n_userhash_peers
= 0;
29 static CachePeer
**userhash_peers
= NULL
;
30 static OBJH peerUserHashCachemgr
;
31 static void peerUserHashRegisterWithCacheManager(void);
34 peerSortWeight(const void *a
, const void *b
)
36 const CachePeer
*const *p1
= (const CachePeer
*const *)a
;
37 const CachePeer
*const *p2
= (const CachePeer
*const *)b
;
38 return (*p1
)->weight
- (*p2
)->weight
;
42 peerUserHashInit(void)
47 double P_last
, X_last
, Xn
;
53 for (k
= 0; k
< n_userhash_peers
; ++k
) {
54 cbdataReferenceDone(userhash_peers
[k
]);
57 safe_free(userhash_peers
);
59 /* find out which peers we have */
61 peerUserHashRegisterWithCacheManager();
63 for (p
= Config
.peers
; p
; p
= p
->next
) {
64 if (!p
->options
.userhash
)
67 assert(p
->type
== PEER_PARENT
);
77 if (n_userhash_peers
== 0)
80 userhash_peers
= (CachePeer
**)xcalloc(n_userhash_peers
, sizeof(*userhash_peers
));
82 /* Build a list of the found peers and calculate hashes and load factors */
83 for (P
= userhash_peers
, p
= Config
.peers
; p
; p
= p
->next
) {
84 if (!p
->options
.userhash
)
90 /* calculate this peers hash */
93 for (t
= p
->name
; *t
!= 0; ++t
)
94 p
->userhash
.hash
+= ROTATE_LEFT(p
->userhash
.hash
, 19) + (unsigned int) *t
;
96 p
->userhash
.hash
+= p
->userhash
.hash
* 0x62531965;
98 p
->userhash
.hash
= ROTATE_LEFT(p
->userhash
.hash
, 21);
100 /* and load factor */
101 p
->userhash
.load_factor
= ((double) p
->weight
) / (double) W
;
103 if (floor(p
->userhash
.load_factor
* 1000.0) == 0.0)
104 p
->userhash
.load_factor
= 0.0;
106 /* add it to our list of peers */
107 *P
++ = cbdataReference(p
);
110 /* Sort our list on weight */
111 qsort(userhash_peers
, n_userhash_peers
, sizeof(*userhash_peers
), peerSortWeight
);
113 /* Calculate the load factor multipliers X_k
115 * X_1 = pow ((K*p_1), (1/K))
116 * X_k = ([K-k+1] * [P_k - P_{k-1}])/(X_1 * X_2 * ... * X_{k-1})
117 * X_k += pow ((X_{k-1}, {K-k+1})
118 * X_k = pow (X_k, {1/(K-k+1)})
119 * simplified to have X_1 part of the loop
121 K
= n_userhash_peers
;
123 P_last
= 0.0; /* Empty P_0 */
125 Xn
= 1.0; /* Empty starting point of X_1 * X_2 * ... * X_{x-1} */
127 X_last
= 0.0; /* Empty X_0, nullifies the first pow statement */
129 for (k
= 1; k
<= K
; ++k
) {
130 double Kk1
= (double) (K
- k
+ 1);
131 p
= userhash_peers
[k
- 1];
132 p
->userhash
.load_multiplier
= (Kk1
* (p
->userhash
.load_factor
- P_last
)) / Xn
;
133 p
->userhash
.load_multiplier
+= pow(X_last
, Kk1
);
134 p
->userhash
.load_multiplier
= pow(p
->userhash
.load_multiplier
, 1.0 / Kk1
);
135 Xn
*= p
->userhash
.load_multiplier
;
136 X_last
= p
->userhash
.load_multiplier
;
137 P_last
= p
->userhash
.load_factor
;
142 peerUserHashRegisterWithCacheManager(void)
144 Mgr::RegisterAction("userhash", "peer userhash information", peerUserHashCachemgr
,
149 peerUserHashSelectParent(HttpRequest
* request
)
155 unsigned int user_hash
= 0;
156 unsigned int combined_hash
;
158 double high_score
= 0;
159 const char *key
= NULL
;
161 if (n_userhash_peers
== 0)
164 if (request
->auth_user_request
!= NULL
)
165 key
= request
->auth_user_request
->username();
170 /* calculate hash key */
171 debugs(39, 2, "peerUserHashSelectParent: Calculating hash for " << key
);
173 for (c
= key
; *c
!= 0; ++c
)
174 user_hash
+= ROTATE_LEFT(user_hash
, 19) + *c
;
176 /* select CachePeer */
177 for (k
= 0; k
< n_userhash_peers
; ++k
) {
178 tp
= userhash_peers
[k
];
179 combined_hash
= (user_hash
^ tp
->userhash
.hash
);
180 combined_hash
+= combined_hash
* 0x62531965;
181 combined_hash
= ROTATE_LEFT(combined_hash
, 21);
182 score
= combined_hash
* tp
->userhash
.load_multiplier
;
183 debugs(39, 3, "peerUserHashSelectParent: " << tp
->name
<< " combined_hash " << combined_hash
<<
184 " score " << std::setprecision(0) << score
);
186 if ((score
> high_score
) && peerHTTPOkay(tp
, request
)) {
193 debugs(39, 2, "peerUserHashSelectParent: selected " << p
->name
);
199 peerUserHashCachemgr(StoreEntry
* sentry
)
203 storeAppendPrintf(sentry
, "%24s %10s %10s %10s %10s\n",
210 for (p
= Config
.peers
; p
; p
= p
->next
)
211 sumfetches
+= p
->stats
.fetches
;
213 for (p
= Config
.peers
; p
; p
= p
->next
) {
214 storeAppendPrintf(sentry
, "%24s %10x %10f %10f %10f\n",
215 p
->name
, p
->userhash
.hash
,
216 p
->userhash
.load_multiplier
,
217 p
->userhash
.load_factor
,
218 sumfetches
? (double) p
->stats
.fetches
/ sumfetches
: -1.0);
222 #endif /* USE_AUTH */