3 * $Id: carp.cc,v 1.27 2008/01/14 12:13:49 hno Exp $
5 * DEBUG: section 39 Peer user hash based selection
6 * AUTHOR: Henrik Nordstrom
9 * SQUID Web Proxy Cache http://www.squid-cache.org/
10 * ----------------------------------------------------------
12 * Squid is the result of efforts by numerous individuals from
13 * the Internet community; see the CONTRIBUTORS file for full
14 * details. Many organizations have provided support for Squid's
15 * development; see the SPONSORS file for full details. Squid is
16 * Copyrighted (C) 2001 by the Regents of the University of
17 * California; see the COPYRIGHT file for full details. Squid
18 * incorporates software developed and/or copyrighted by other
19 * sources; see the CREDITS file for full details.
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
38 #include "CacheManager.h"
40 #include "HttpRequest.h"
41 #include "AuthUserRequest.h"
43 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
45 static int n_userhash_peers
= 0;
46 static peer
**userhash_peers
= NULL
;
47 static OBJH peerUserHashCachemgr
;
48 static void peerUserHashRegisterWithCacheManager(void);
51 peerSortWeight(const void *a
, const void *b
)
53 const peer
*const *p1
= (const peer
*const *)a
;
54 const peer
*const *p2
= (const peer
*const *)b
;
55 return (*p1
)->weight
- (*p2
)->weight
;
59 peerUserHashInit(void)
64 double P_last
, X_last
, Xn
;
70 for (k
= 0; k
< n_userhash_peers
; k
++) {
71 cbdataReferenceDone(userhash_peers
[k
]);
74 safe_free(userhash_peers
);
76 /* find out which peers we have */
79 peerUserHashRegisterWithCacheManager();
81 for (p
= Config
.peers
; p
; p
= p
->next
) {
82 if (!p
->options
.userhash
)
85 assert(p
->type
== PEER_PARENT
);
95 if (n_userhash_peers
== 0)
98 userhash_peers
= (peer
**)xcalloc(n_userhash_peers
, sizeof(*userhash_peers
));
100 /* Build a list of the found peers and calculate hashes and load factors */
101 for (P
= userhash_peers
, p
= Config
.peers
; p
; p
= p
->next
) {
102 if (!p
->options
.userhash
)
108 /* calculate this peers hash */
109 p
->userhash
.hash
= 0;
111 for (t
= p
->name
; *t
!= 0; t
++)
112 p
->userhash
.hash
+= ROTATE_LEFT(p
->userhash
.hash
, 19) + (unsigned int) *t
;
114 p
->userhash
.hash
+= p
->userhash
.hash
* 0x62531965;
116 p
->userhash
.hash
= ROTATE_LEFT(p
->userhash
.hash
, 21);
118 /* and load factor */
119 p
->userhash
.load_factor
= ((double) p
->weight
) / (double) W
;
121 if (floor(p
->userhash
.load_factor
* 1000.0) == 0.0)
122 p
->userhash
.load_factor
= 0.0;
124 /* add it to our list of peers */
125 *P
++ = cbdataReference(p
);
128 /* Sort our list on weight */
129 qsort(userhash_peers
, n_userhash_peers
, sizeof(*userhash_peers
), peerSortWeight
);
131 /* Calculate the load factor multipliers X_k
133 * X_1 = pow ((K*p_1), (1/K))
134 * X_k = ([K-k+1] * [P_k - P_{k-1}])/(X_1 * X_2 * ... * X_{k-1})
135 * X_k += pow ((X_{k-1}, {K-k+1})
136 * X_k = pow (X_k, {1/(K-k+1)})
137 * simplified to have X_1 part of the loop
139 K
= n_userhash_peers
;
141 P_last
= 0.0; /* Empty P_0 */
143 Xn
= 1.0; /* Empty starting point of X_1 * X_2 * ... * X_{x-1} */
145 X_last
= 0.0; /* Empty X_0, nullifies the first pow statement */
147 for (k
= 1; k
<= K
; k
++) {
148 double Kk1
= (double) (K
- k
+ 1);
149 p
= userhash_peers
[k
- 1];
150 p
->userhash
.load_multiplier
= (Kk1
* (p
->userhash
.load_factor
- P_last
)) / Xn
;
151 p
->userhash
.load_multiplier
+= pow(X_last
, Kk1
);
152 p
->userhash
.load_multiplier
= pow(p
->userhash
.load_multiplier
, 1.0 / Kk1
);
153 Xn
*= p
->userhash
.load_multiplier
;
154 X_last
= p
->userhash
.load_multiplier
;
155 P_last
= p
->userhash
.load_factor
;
160 peerUserHashRegisterWithCacheManager(void)
162 CacheManager::GetInstance()->
163 registerAction("userhash", "peer userhash information", peerUserHashCachemgr
,
168 peerUserHashSelectParent(HttpRequest
* request
)
174 unsigned int user_hash
= 0;
175 unsigned int combined_hash
;
177 double high_score
= 0;
178 const char *key
= NULL
;
180 if (n_userhash_peers
== 0)
183 if (request
->auth_user_request
)
184 key
= request
->auth_user_request
->username();
189 /* calculate hash key */
190 debugs(39, 2, "peerUserHashSelectParent: Calculating hash for " << key
);
192 for (c
= key
; *c
!= 0; c
++)
193 user_hash
+= ROTATE_LEFT(user_hash
, 19) + *c
;
196 for (k
= 0; k
< n_userhash_peers
; k
++) {
197 tp
= userhash_peers
[k
];
198 combined_hash
= (user_hash
^ tp
->userhash
.hash
);
199 combined_hash
+= combined_hash
* 0x62531965;
200 combined_hash
= ROTATE_LEFT(combined_hash
, 21);
201 score
= combined_hash
* tp
->userhash
.load_multiplier
;
202 debugs(39, 3, "peerUserHashSelectParent: " << tp
->name
<< " combined_hash " << combined_hash
<<
203 " score " << std::setprecision(0) << score
);
205 if ((score
> high_score
) && peerHTTPOkay(tp
, request
)) {
212 debugs(39, 2, "peerUserHashSelectParent: selected " << p
->name
);
218 peerUserHashCachemgr(StoreEntry
* sentry
)
222 storeAppendPrintf(sentry
, "%24s %10s %10s %10s %10s\n",
229 for (p
= Config
.peers
; p
; p
= p
->next
)
230 sumfetches
+= p
->stats
.fetches
;
232 for (p
= Config
.peers
; p
; p
= p
->next
) {
233 storeAppendPrintf(sentry
, "%24s %10x %10f %10f %10f\n",
234 p
->name
, p
->userhash
.hash
,
235 p
->userhash
.load_multiplier
,
236 p
->userhash
.load_factor
,
237 sumfetches
? (double) p
->stats
.fetches
/ sumfetches
: -1.0);