]> git.ipfire.org Git - thirdparty/squid.git/blob - src/carp.cc
Merged from trunk
[thirdparty/squid.git] / src / carp.cc
1
2 /*
3 * $Id$
4 *
5 * DEBUG: section 39 Cache Array Routing Protocol
6 * AUTHOR: Henrik Nordstrom
7 * BASED ON: carp.c by Eric Stern and draft-vinod-carp-v1-03.txt
8 *
9 * SQUID Web Proxy Cache http://www.squid-cache.org/
10 * ----------------------------------------------------------
11 *
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.
20 *
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.
25 *
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.
30 *
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.
34 *
35 */
36
37 #include "squid.h"
38 #include "HttpRequest.h"
39 #include "mgr/Registration.h"
40 #include "neighbors.h"
41 #include "protos.h"
42 #include "Store.h"
43 #include "URL.h"
44 #include "URLScheme.h"
45
46 #if HAVE_MATH_H
47 #include <math.h>
48 #endif
49
50 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
51
52 static int n_carp_peers = 0;
53 static peer **carp_peers = NULL;
54 static OBJH carpCachemgr;
55
56 static int
57 peerSortWeight(const void *a, const void *b)
58 {
59 const peer *const *p1 = (const peer *const *)a;
60 const peer *const *p2 = (const peer *const *)b;
61 return (*p1)->weight - (*p2)->weight;
62 }
63
64 static void
65 carpRegisterWithCacheManager(void)
66 {
67 Mgr::RegisterAction("carp", "CARP information", carpCachemgr, 0, 1);
68 }
69
70 void
71 carpInit(void)
72 {
73 int W = 0;
74 int K;
75 int k;
76 double P_last, X_last, Xn;
77 peer *p;
78 peer **P;
79 char *t;
80 /* Clean up */
81
82 for (k = 0; k < n_carp_peers; ++k) {
83 cbdataReferenceDone(carp_peers[k]);
84 }
85
86 safe_free(carp_peers);
87 n_carp_peers = 0;
88
89 /* initialize cache manager before we have a chance to leave the execution path */
90 carpRegisterWithCacheManager();
91
92 /* find out which peers we have */
93
94 for (p = Config.peers; p; p = p->next) {
95 if (!p->options.carp)
96 continue;
97
98 assert(p->type == PEER_PARENT);
99
100 if (p->weight == 0)
101 continue;
102
103 ++n_carp_peers;
104
105 W += p->weight;
106 }
107
108 if (n_carp_peers == 0)
109 return;
110
111 carp_peers = (peer **)xcalloc(n_carp_peers, sizeof(*carp_peers));
112
113 /* Build a list of the found peers and calculate hashes and load factors */
114 for (P = carp_peers, p = Config.peers; p; p = p->next) {
115 if (!p->options.carp)
116 continue;
117
118 if (p->weight == 0)
119 continue;
120
121 /* calculate this peers hash */
122 p->carp.hash = 0;
123
124 for (t = p->name; *t != 0; ++t)
125 p->carp.hash += ROTATE_LEFT(p->carp.hash, 19) + (unsigned int) *t;
126
127 p->carp.hash += p->carp.hash * 0x62531965;
128
129 p->carp.hash = ROTATE_LEFT(p->carp.hash, 21);
130
131 /* and load factor */
132 p->carp.load_factor = ((double) p->weight) / (double) W;
133
134 if (floor(p->carp.load_factor * 1000.0) == 0.0)
135 p->carp.load_factor = 0.0;
136
137 /* add it to our list of peers */
138 *P = cbdataReference(p);
139 ++P;
140 }
141
142 /* Sort our list on weight */
143 qsort(carp_peers, n_carp_peers, sizeof(*carp_peers), peerSortWeight);
144
145 /* Calculate the load factor multipliers X_k
146 *
147 * X_1 = pow ((K*p_1), (1/K))
148 * X_k = ([K-k+1] * [P_k - P_{k-1}])/(X_1 * X_2 * ... * X_{k-1})
149 * X_k += pow ((X_{k-1}, {K-k+1})
150 * X_k = pow (X_k, {1/(K-k+1)})
151 * simplified to have X_1 part of the loop
152 */
153 K = n_carp_peers;
154
155 P_last = 0.0; /* Empty P_0 */
156
157 Xn = 1.0; /* Empty starting point of X_1 * X_2 * ... * X_{x-1} */
158
159 X_last = 0.0; /* Empty X_0, nullifies the first pow statement */
160
161 for (k = 1; k <= K; ++k) {
162 double Kk1 = (double) (K - k + 1);
163 p = carp_peers[k - 1];
164 p->carp.load_multiplier = (Kk1 * (p->carp.load_factor - P_last)) / Xn;
165 p->carp.load_multiplier += pow(X_last, Kk1);
166 p->carp.load_multiplier = pow(p->carp.load_multiplier, 1.0 / Kk1);
167 Xn *= p->carp.load_multiplier;
168 X_last = p->carp.load_multiplier;
169 P_last = p->carp.load_factor;
170 }
171 }
172
173 peer *
174 carpSelectParent(HttpRequest * request)
175 {
176 int k;
177 peer *p = NULL;
178 peer *tp;
179 unsigned int user_hash = 0;
180 unsigned int combined_hash;
181 double score;
182 double high_score = 0;
183
184 if (n_carp_peers == 0)
185 return NULL;
186
187 /* calculate hash key */
188 debugs(39, 2, "carpSelectParent: Calculating hash for " << urlCanonical(request));
189
190 /* select peer */
191 for (k = 0; k < n_carp_peers; ++k) {
192 String key;
193 tp = carp_peers[k];
194 if (tp->options.carp_key.set) {
195 //this code follows urlCanonical's pattern.
196 // corner cases should use the canonical URL
197 if (tp->options.carp_key.scheme) {
198 // temporary, until bug 1961 URL handling is fixed.
199 const URLScheme sch = request->protocol;
200 key.append(sch.const_str());
201 if (key.size()) //if the scheme is not empty
202 key.append("://");
203 }
204 if (tp->options.carp_key.host) {
205 key.append(request->GetHost());
206 }
207 if (tp->options.carp_key.port) {
208 static char portbuf[7];
209 snprintf(portbuf,7,":%d", request->port);
210 key.append(portbuf);
211 }
212 if (tp->options.carp_key.path) {
213 String::size_type pos;
214 if ((pos=request->urlpath.find('?'))!=String::npos)
215 key.append(request->urlpath.substr(0,pos));
216 else
217 key.append(request->urlpath);
218 }
219 if (tp->options.carp_key.params) {
220 String::size_type pos;
221 if ((pos=request->urlpath.find('?'))!=String::npos)
222 key.append(request->urlpath.substr(pos,request->urlpath.size()));
223 }
224 }
225 // if the url-based key is empty, e.g. because the user is
226 // asking to balance on the path but the request doesn't supply any,
227 // then fall back to canonical URL
228
229 if (key.size()==0)
230 key=urlCanonical(request);
231
232 for (const char *c = key.rawBuf(), *e=key.rawBuf()+key.size(); c < e; ++c)
233 user_hash += ROTATE_LEFT(user_hash, 19) + *c;
234 combined_hash = (user_hash ^ tp->carp.hash);
235 combined_hash += combined_hash * 0x62531965;
236 combined_hash = ROTATE_LEFT(combined_hash, 21);
237 score = combined_hash * tp->carp.load_multiplier;
238 debugs(39, 3, "carpSelectParent: key=" << key << " name=" << tp->name << " combined_hash=" << combined_hash <<
239 " score=" << std::setprecision(0) << score);
240
241 if ((score > high_score) && peerHTTPOkay(tp, request)) {
242 p = tp;
243 high_score = score;
244 }
245 }
246
247 if (p)
248 debugs(39, 2, "carpSelectParent: selected " << p->name);
249
250 return p;
251 }
252
253 static void
254 carpCachemgr(StoreEntry * sentry)
255 {
256 peer *p;
257 int sumfetches = 0;
258 storeAppendPrintf(sentry, "%24s %10s %10s %10s %10s\n",
259 "Hostname",
260 "Hash",
261 "Multiplier",
262 "Factor",
263 "Actual");
264
265 for (p = Config.peers; p; p = p->next)
266 sumfetches += p->stats.fetches;
267
268 for (p = Config.peers; p; p = p->next) {
269 storeAppendPrintf(sentry, "%24s %10x %10f %10f %10f\n",
270 p->name, p->carp.hash,
271 p->carp.load_multiplier,
272 p->carp.load_factor,
273 sumfetches ? (double) p->stats.fetches / sumfetches : -1.0);
274 }
275 }