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