5 * DEBUG: section 39 Peer source 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.
39 #include "HttpRequest.h"
40 #include "mgr/Registration.h"
42 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
44 static int n_sourcehash_peers
= 0;
45 static peer
**sourcehash_peers
= NULL
;
46 static OBJH peerSourceHashCachemgr
;
47 static void peerSourceHashRegisterWithCacheManager(void);
50 peerSortWeight(const void *a
, const void *b
)
52 const peer
*const *p1
= (const peer
*const *)a
;
53 const peer
*const *p2
= (const peer
*const *)b
;
54 return (*p1
)->weight
- (*p2
)->weight
;
58 peerSourceHashInit(void)
63 double P_last
, X_last
, Xn
;
69 for (k
= 0; k
< n_sourcehash_peers
; k
++) {
70 cbdataReferenceDone(sourcehash_peers
[k
]);
73 safe_free(sourcehash_peers
);
74 n_sourcehash_peers
= 0;
75 /* find out which peers we have */
77 for (p
= Config
.peers
; p
; p
= p
->next
) {
78 if (!p
->options
.sourcehash
)
81 assert(p
->type
== PEER_PARENT
);
91 peerSourceHashRegisterWithCacheManager();
93 if (n_sourcehash_peers
== 0)
96 sourcehash_peers
= (peer
**)xcalloc(n_sourcehash_peers
, sizeof(*sourcehash_peers
));
98 /* Build a list of the found peers and calculate hashes and load factors */
99 for (P
= sourcehash_peers
, p
= Config
.peers
; p
; p
= p
->next
) {
100 if (!p
->options
.sourcehash
)
106 /* calculate this peers hash */
107 p
->sourcehash
.hash
= 0;
109 for (t
= p
->name
; *t
!= 0; t
++)
110 p
->sourcehash
.hash
+= ROTATE_LEFT(p
->sourcehash
.hash
, 19) + (unsigned int) *t
;
112 p
->sourcehash
.hash
+= p
->sourcehash
.hash
* 0x62531965;
114 p
->sourcehash
.hash
= ROTATE_LEFT(p
->sourcehash
.hash
, 21);
116 /* and load factor */
117 p
->sourcehash
.load_factor
= ((double) p
->weight
) / (double) W
;
119 if (floor(p
->sourcehash
.load_factor
* 1000.0) == 0.0)
120 p
->sourcehash
.load_factor
= 0.0;
122 /* add it to our list of peers */
123 *P
++ = cbdataReference(p
);
126 /* Sort our list on weight */
127 qsort(sourcehash_peers
, n_sourcehash_peers
, sizeof(*sourcehash_peers
), peerSortWeight
);
129 /* Calculate the load factor multipliers X_k
131 * X_1 = pow ((K*p_1), (1/K))
132 * X_k = ([K-k+1] * [P_k - P_{k-1}])/(X_1 * X_2 * ... * X_{k-1})
133 * X_k += pow ((X_{k-1}, {K-k+1})
134 * X_k = pow (X_k, {1/(K-k+1)})
135 * simplified to have X_1 part of the loop
137 K
= n_sourcehash_peers
;
139 P_last
= 0.0; /* Empty P_0 */
141 Xn
= 1.0; /* Empty starting point of X_1 * X_2 * ... * X_{x-1} */
143 X_last
= 0.0; /* Empty X_0, nullifies the first pow statement */
145 for (k
= 1; k
<= K
; k
++) {
146 double Kk1
= (double) (K
- k
+ 1);
147 p
= sourcehash_peers
[k
- 1];
148 p
->sourcehash
.load_multiplier
= (Kk1
* (p
->sourcehash
.load_factor
- P_last
)) / Xn
;
149 p
->sourcehash
.load_multiplier
+= pow(X_last
, Kk1
);
150 p
->sourcehash
.load_multiplier
= pow(p
->sourcehash
.load_multiplier
, 1.0 / Kk1
);
151 Xn
*= p
->sourcehash
.load_multiplier
;
152 X_last
= p
->sourcehash
.load_multiplier
;
153 P_last
= p
->sourcehash
.load_factor
;
158 peerSourceHashRegisterWithCacheManager(void)
160 Mgr::RegisterAction("sourcehash", "peer sourcehash information",
161 peerSourceHashCachemgr
, 0, 1);
165 peerSourceHashSelectParent(HttpRequest
* request
)
171 unsigned int user_hash
= 0;
172 unsigned int combined_hash
;
174 double high_score
= 0;
175 const char *key
= NULL
;
176 char ntoabuf
[MAX_IPSTRLEN
];
178 if (n_sourcehash_peers
== 0)
181 key
= request
->client_addr
.NtoA(ntoabuf
, sizeof(ntoabuf
));
183 /* calculate hash key */
184 debugs(39, 2, "peerSourceHashSelectParent: Calculating hash for " << key
);
186 for (c
= key
; *c
!= 0; c
++)
187 user_hash
+= ROTATE_LEFT(user_hash
, 19) + *c
;
190 for (k
= 0; k
< n_sourcehash_peers
; k
++) {
191 tp
= sourcehash_peers
[k
];
192 combined_hash
= (user_hash
^ tp
->sourcehash
.hash
);
193 combined_hash
+= combined_hash
* 0x62531965;
194 combined_hash
= ROTATE_LEFT(combined_hash
, 21);
195 score
= combined_hash
* tp
->sourcehash
.load_multiplier
;
196 debugs(39, 3, "peerSourceHashSelectParent: " << tp
->name
<< " combined_hash " << combined_hash
<<
197 " score " << std::setprecision(0) << score
);
199 if ((score
> high_score
) && peerHTTPOkay(tp
, request
)) {
206 debugs(39, 2, "peerSourceHashSelectParent: selected " << p
->name
);
212 peerSourceHashCachemgr(StoreEntry
* sentry
)
216 storeAppendPrintf(sentry
, "%24s %10s %10s %10s %10s\n",
223 for (p
= Config
.peers
; p
; p
= p
->next
)
224 sumfetches
+= p
->stats
.fetches
;
226 for (p
= Config
.peers
; p
; p
= p
->next
) {
227 storeAppendPrintf(sentry
, "%24s %10x %10f %10f %10f\n",
228 p
->name
, p
->sourcehash
.hash
,
229 p
->sourcehash
.load_multiplier
,
230 p
->sourcehash
.load_factor
,
231 sumfetches
? (double) p
->stats
.fetches
/ sumfetches
: -1.0);