]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/carp.cc
3 * DEBUG: section 39 Cache Array Routing Protocol
4 * AUTHOR: Henrik Nordstrom
5 * BASED ON: carp.c by Eric Stern and draft-vinod-carp-v1-03.txt
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
36 #include "HttpRequest.h"
37 #include "mgr/Registration.h"
38 #include "neighbors.h"
41 #include "URLScheme.h"
47 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
49 static int n_carp_peers
= 0;
50 static peer
**carp_peers
= NULL
;
51 static OBJH carpCachemgr
;
54 peerSortWeight(const void *a
, const void *b
)
56 const peer
*const *p1
= (const peer
*const *)a
;
57 const peer
*const *p2
= (const peer
*const *)b
;
58 return (*p1
)->weight
- (*p2
)->weight
;
62 carpRegisterWithCacheManager(void)
64 Mgr::RegisterAction("carp", "CARP information", carpCachemgr
, 0, 1);
73 double P_last
, X_last
, Xn
;
79 for (k
= 0; k
< n_carp_peers
; ++k
) {
80 cbdataReferenceDone(carp_peers
[k
]);
83 safe_free(carp_peers
);
86 /* initialize cache manager before we have a chance to leave the execution path */
87 carpRegisterWithCacheManager();
89 /* find out which peers we have */
91 for (p
= Config
.peers
; p
; p
= p
->next
) {
95 assert(p
->type
== PEER_PARENT
);
105 if (n_carp_peers
== 0)
108 carp_peers
= (peer
**)xcalloc(n_carp_peers
, sizeof(*carp_peers
));
110 /* Build a list of the found peers and calculate hashes and load factors */
111 for (P
= carp_peers
, p
= Config
.peers
; p
; p
= p
->next
) {
112 if (!p
->options
.carp
)
118 /* calculate this peers hash */
121 for (t
= p
->name
; *t
!= 0; ++t
)
122 p
->carp
.hash
+= ROTATE_LEFT(p
->carp
.hash
, 19) + (unsigned int) *t
;
124 p
->carp
.hash
+= p
->carp
.hash
* 0x62531965;
126 p
->carp
.hash
= ROTATE_LEFT(p
->carp
.hash
, 21);
128 /* and load factor */
129 p
->carp
.load_factor
= ((double) p
->weight
) / (double) W
;
131 if (floor(p
->carp
.load_factor
* 1000.0) == 0.0)
132 p
->carp
.load_factor
= 0.0;
134 /* add it to our list of peers */
135 *P
= cbdataReference(p
);
139 /* Sort our list on weight */
140 qsort(carp_peers
, n_carp_peers
, sizeof(*carp_peers
), peerSortWeight
);
142 /* Calculate the load factor multipliers X_k
144 * X_1 = pow ((K*p_1), (1/K))
145 * X_k = ([K-k+1] * [P_k - P_{k-1}])/(X_1 * X_2 * ... * X_{k-1})
146 * X_k += pow ((X_{k-1}, {K-k+1})
147 * X_k = pow (X_k, {1/(K-k+1)})
148 * simplified to have X_1 part of the loop
152 P_last
= 0.0; /* Empty P_0 */
154 Xn
= 1.0; /* Empty starting point of X_1 * X_2 * ... * X_{x-1} */
156 X_last
= 0.0; /* Empty X_0, nullifies the first pow statement */
158 for (k
= 1; k
<= K
; ++k
) {
159 double Kk1
= (double) (K
- k
+ 1);
160 p
= carp_peers
[k
- 1];
161 p
->carp
.load_multiplier
= (Kk1
* (p
->carp
.load_factor
- P_last
)) / Xn
;
162 p
->carp
.load_multiplier
+= pow(X_last
, Kk1
);
163 p
->carp
.load_multiplier
= pow(p
->carp
.load_multiplier
, 1.0 / Kk1
);
164 Xn
*= p
->carp
.load_multiplier
;
165 X_last
= p
->carp
.load_multiplier
;
166 P_last
= p
->carp
.load_factor
;
171 carpSelectParent(HttpRequest
* request
)
176 unsigned int user_hash
= 0;
177 unsigned int combined_hash
;
179 double high_score
= 0;
181 if (n_carp_peers
== 0)
184 /* calculate hash key */
185 debugs(39, 2, "carpSelectParent: Calculating hash for " << urlCanonical(request
));
188 for (k
= 0; k
< n_carp_peers
; ++k
) {
191 if (tp
->options
.carp_key
.set
) {
192 //this code follows urlCanonical's pattern.
193 // corner cases should use the canonical URL
194 if (tp
->options
.carp_key
.scheme
) {
195 // temporary, until bug 1961 URL handling is fixed.
196 const URLScheme sch
= request
->protocol
;
197 key
.append(sch
.const_str());
198 if (key
.size()) //if the scheme is not empty
201 if (tp
->options
.carp_key
.host
) {
202 key
.append(request
->GetHost());
204 if (tp
->options
.carp_key
.port
) {
205 static char portbuf
[7];
206 snprintf(portbuf
,7,":%d", request
->port
);
209 if (tp
->options
.carp_key
.path
) {
210 String::size_type pos
;
211 if ((pos
=request
->urlpath
.find('?'))!=String::npos
)
212 key
.append(request
->urlpath
.substr(0,pos
));
214 key
.append(request
->urlpath
);
216 if (tp
->options
.carp_key
.params
) {
217 String::size_type pos
;
218 if ((pos
=request
->urlpath
.find('?'))!=String::npos
)
219 key
.append(request
->urlpath
.substr(pos
,request
->urlpath
.size()));
222 // if the url-based key is empty, e.g. because the user is
223 // asking to balance on the path but the request doesn't supply any,
224 // then fall back to canonical URL
227 key
=urlCanonical(request
);
229 for (const char *c
= key
.rawBuf(), *e
=key
.rawBuf()+key
.size(); c
< e
; ++c
)
230 user_hash
+= ROTATE_LEFT(user_hash
, 19) + *c
;
231 combined_hash
= (user_hash
^ tp
->carp
.hash
);
232 combined_hash
+= combined_hash
* 0x62531965;
233 combined_hash
= ROTATE_LEFT(combined_hash
, 21);
234 score
= combined_hash
* tp
->carp
.load_multiplier
;
235 debugs(39, 3, "carpSelectParent: key=" << key
<< " name=" << tp
->name
<< " combined_hash=" << combined_hash
<<
236 " score=" << std::setprecision(0) << score
);
238 if ((score
> high_score
) && peerHTTPOkay(tp
, request
)) {
245 debugs(39, 2, "carpSelectParent: selected " << p
->name
);
251 carpCachemgr(StoreEntry
* sentry
)
255 storeAppendPrintf(sentry
, "%24s %10s %10s %10s %10s\n",
262 for (p
= Config
.peers
; p
; p
= p
->next
)
263 sumfetches
+= p
->stats
.fetches
;
265 for (p
= Config
.peers
; p
; p
= p
->next
) {
266 storeAppendPrintf(sentry
, "%24s %10x %10f %10f %10f\n",
267 p
->name
, p
->carp
.hash
,
268 p
->carp
.load_multiplier
,
270 sumfetches
? (double) p
->stats
.fetches
/ sumfetches
: -1.0);