]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/peer_sourcehash.cc
3 * $Id: carp.cc,v 1.27 2008/01/14 12:13:49 hno Exp $
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.
38 #include "CacheManager.h"
41 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
43 static int n_carp_peers
= 0;
44 static peer
**carp_peers
= NULL
;
45 static OBJH carpCachemgr
;
48 peerSortWeight(const void *a
, const void *b
)
50 const peer
*const *p1
= (const peer
*const *)a
;
51 const peer
*const *p2
= (const peer
*const *)b
;
52 return (*p1
)->weight
- (*p2
)->weight
;
61 double P_last
, X_last
, Xn
;
67 for (k
= 0; k
< n_carp_peers
; k
++) {
68 cbdataReferenceDone(carp_peers
[k
]);
71 safe_free(carp_peers
);
73 /* find out which peers we have */
75 for (p
= Config
.peers
; p
; p
= p
->next
) {
79 assert(p
->type
== PEER_PARENT
);
89 if (n_carp_peers
== 0)
92 carp_peers
= (peer
**)xcalloc(n_carp_peers
, sizeof(*carp_peers
));
94 /* Build a list of the found peers and calculate hashes and load factors */
95 for (P
= carp_peers
, p
= Config
.peers
; p
; p
= p
->next
) {
102 /* calculate this peers hash */
105 for (t
= p
->name
; *t
!= 0; t
++)
106 p
->carp
.hash
+= ROTATE_LEFT(p
->carp
.hash
, 19) + (unsigned int) *t
;
108 p
->carp
.hash
+= p
->carp
.hash
* 0x62531965;
110 p
->carp
.hash
= ROTATE_LEFT(p
->carp
.hash
, 21);
112 /* and load factor */
113 p
->carp
.load_factor
= ((double) p
->weight
) / (double) W
;
115 if (floor(p
->carp
.load_factor
* 1000.0) == 0.0)
116 p
->carp
.load_factor
= 0.0;
118 /* add it to our list of peers */
119 *P
++ = cbdataReference(p
);
122 /* Sort our list on weight */
123 qsort(carp_peers
, n_carp_peers
, sizeof(*carp_peers
), peerSortWeight
);
125 /* Calculate the load factor multipliers X_k
127 * X_1 = pow ((K*p_1), (1/K))
128 * X_k = ([K-k+1] * [P_k - P_{k-1}])/(X_1 * X_2 * ... * X_{k-1})
129 * X_k += pow ((X_{k-1}, {K-k+1})
130 * X_k = pow (X_k, {1/(K-k+1)})
131 * simplified to have X_1 part of the loop
135 P_last
= 0.0; /* Empty P_0 */
137 Xn
= 1.0; /* Empty starting point of X_1 * X_2 * ... * X_{x-1} */
139 X_last
= 0.0; /* Empty X_0, nullifies the first pow statement */
141 for (k
= 1; k
<= K
; k
++) {
142 double Kk1
= (double) (K
- k
+ 1);
143 p
= carp_peers
[k
- 1];
144 p
->carp
.load_multiplier
= (Kk1
* (p
->carp
.load_factor
- P_last
)) / Xn
;
145 p
->carp
.load_multiplier
+= pow(X_last
, Kk1
);
146 p
->carp
.load_multiplier
= pow(p
->carp
.load_multiplier
, 1.0 / Kk1
);
147 Xn
*= p
->carp
.load_multiplier
;
148 X_last
= p
->carp
.load_multiplier
;
149 P_last
= p
->carp
.load_factor
;
154 carpRegisterWithCacheManager(CacheManager
& manager
)
156 manager
.registerAction("carp", "CARP information", carpCachemgr
, 0, 1);
160 carpSelectParent(HttpRequest
* request
)
166 unsigned int user_hash
= 0;
167 unsigned int combined_hash
;
169 double high_score
= 0;
170 const char *key
= NULL
;
171 char ntoabuf
[MAX_IPSTRLEN
];
173 if (n_carp_peers
== 0)
176 key
= request
->client_addr
.NtoA(ntoabuf
, sizeof(ntoabuf
));
178 /* calculate hash key */
179 debugs(39, 2, "carpSelectParent: Calculating hash for " << key
);
181 for (c
= key
; *c
!= 0; c
++)
182 user_hash
+= ROTATE_LEFT(user_hash
, 19) + *c
;
185 for (k
= 0; k
< n_carp_peers
; k
++) {
187 combined_hash
= (user_hash
^ tp
->carp
.hash
);
188 combined_hash
+= combined_hash
* 0x62531965;
189 combined_hash
= ROTATE_LEFT(combined_hash
, 21);
190 score
= combined_hash
* tp
->carp
.load_multiplier
;
191 debugs(39, 3, "carpSelectParent: " << tp
->name
<< " combined_hash " << combined_hash
<<
192 " score " << std::setprecision(0) << score
);
194 if ((score
> high_score
) && peerHTTPOkay(tp
, request
)) {
201 debugs(39, 2, "carpSelectParent: selected " << p
->name
);
207 carpCachemgr(StoreEntry
* sentry
)
211 storeAppendPrintf(sentry
, "%24s %10s %10s %10s %10s\n",
218 for (p
= Config
.peers
; p
; p
= p
->next
)
219 sumfetches
+= p
->stats
.fetches
;
221 for (p
= Config
.peers
; p
; p
= p
->next
) {
222 storeAppendPrintf(sentry
, "%24s %10x %10f %10f %10f\n",
223 p
->name
, p
->carp
.hash
,
224 p
->carp
.load_multiplier
,
226 sumfetches
? (double) p
->stats
.fetches
/ sumfetches
: -1.0);