]> git.ipfire.org Git - thirdparty/squid.git/blame - src/carp.cc
Changed some structs to classes.
[thirdparty/squid.git] / src / carp.cc
CommitLineData
f740a279 1
afd88fbe 2/*
67f46679 3 * DEBUG: section 39 Cache Array Routing Protocol
b3995439 4 * AUTHOR: Henrik Nordstrom
5 * BASED ON: carp.c by Eric Stern and draft-vinod-carp-v1-03.txt
afd88fbe 6 *
2b6662ba 7 * SQUID Web Proxy Cache http://www.squid-cache.org/
e25c139f 8 * ----------------------------------------------------------
9 *
2b6662ba 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.
e25c139f 18 *
afd88fbe 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.
26ac0430 23 *
afd88fbe 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.
26ac0430 28 *
afd88fbe 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
cbdec147 31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
e25c139f 32 *
0cdcddb9 33 */
afd88fbe 34
582c2af2 35#include "squid.h"
de03b596 36#include "HttpRequest.h"
8822ebee 37#include "mgr/Registration.h"
f0ba2534 38#include "neighbors.h"
e6ccf245 39#include "Store.h"
b1bd952a 40#include "URL.h"
de03b596 41#include "URLScheme.h"
afd88fbe 42
582c2af2
FC
43#if HAVE_MATH_H
44#include <math.h>
45#endif
46
b3995439 47#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
48
49static int n_carp_peers = 0;
50static peer **carp_peers = NULL;
8ee9b49f 51static OBJH carpCachemgr;
52
b3995439 53static int
54peerSortWeight(const void *a, const void *b)
55{
ff83713b 56 const peer *const *p1 = (const peer *const *)a;
57 const peer *const *p2 = (const peer *const *)b;
b3995439 58 return (*p1)->weight - (*p2)->weight;
59}
60
5f5e883f
FC
61static void
62carpRegisterWithCacheManager(void)
63{
8822ebee 64 Mgr::RegisterAction("carp", "CARP information", carpCachemgr, 0, 1);
5f5e883f
FC
65}
66
afd88fbe 67void
68carpInit(void)
69{
b3995439 70 int W = 0;
71 int K;
afd88fbe 72 int k;
b3995439 73 double P_last, X_last, Xn;
afd88fbe 74 peer *p;
b3995439 75 peer **P;
76 char *t;
77 /* Clean up */
62e76326 78
d7ae3534 79 for (k = 0; k < n_carp_peers; ++k) {
62e76326 80 cbdataReferenceDone(carp_peers[k]);
b3995439 81 }
62e76326 82
b3995439 83 safe_free(carp_peers);
84 n_carp_peers = 0;
8cb183ec
FC
85
86 /* initialize cache manager before we have a chance to leave the execution path */
87 carpRegisterWithCacheManager();
88
b3995439 89 /* find out which peers we have */
62e76326 90
afd88fbe 91 for (p = Config.peers; p; p = p->next) {
62e76326 92 if (!p->options.carp)
93 continue;
94
95 assert(p->type == PEER_PARENT);
96
97 if (p->weight == 0)
98 continue;
99
d7ae3534 100 ++n_carp_peers;
62e76326 101
102 W += p->weight;
afd88fbe 103 }
62e76326 104
b3995439 105 if (n_carp_peers == 0)
62e76326 106 return;
107
ff83713b 108 carp_peers = (peer **)xcalloc(n_carp_peers, sizeof(*carp_peers));
62e76326 109
b3995439 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) {
62e76326 112 if (!p->options.carp)
113 continue;
114
115 if (p->weight == 0)
116 continue;
117
118 /* calculate this peers hash */
119 p->carp.hash = 0;
120
d7ae3534 121 for (t = p->name; *t != 0; ++t)
62e76326 122 p->carp.hash += ROTATE_LEFT(p->carp.hash, 19) + (unsigned int) *t;
123
124 p->carp.hash += p->carp.hash * 0x62531965;
125
126 p->carp.hash = ROTATE_LEFT(p->carp.hash, 21);
127
128 /* and load factor */
129 p->carp.load_factor = ((double) p->weight) / (double) W;
130
131 if (floor(p->carp.load_factor * 1000.0) == 0.0)
132 p->carp.load_factor = 0.0;
133
134 /* add it to our list of peers */
a38ec4b1
FC
135 *P = cbdataReference(p);
136 ++P;
67766c17 137 }
62e76326 138
b3995439 139 /* Sort our list on weight */
140 qsort(carp_peers, n_carp_peers, sizeof(*carp_peers), peerSortWeight);
62e76326 141
b3995439 142 /* Calculate the load factor multipliers X_k
143 *
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
afd88fbe 149 */
b3995439 150 K = n_carp_peers;
62e76326 151
b3995439 152 P_last = 0.0; /* Empty P_0 */
62e76326 153
b3995439 154 Xn = 1.0; /* Empty starting point of X_1 * X_2 * ... * X_{x-1} */
62e76326 155
b3995439 156 X_last = 0.0; /* Empty X_0, nullifies the first pow statement */
62e76326 157
d7ae3534 158 for (k = 1; k <= K; ++k) {
62e76326 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;
afd88fbe 167 }
62ee09ca 168}
62e76326 169
afd88fbe 170peer *
190154cf 171carpSelectParent(HttpRequest * request)
afd88fbe 172{
b3995439 173 int k;
afd88fbe 174 peer *p = NULL;
175 peer *tp;
b3995439 176 unsigned int user_hash = 0;
177 unsigned int combined_hash;
178 double score;
179 double high_score = 0;
b3995439 180
181 if (n_carp_peers == 0)
62e76326 182 return NULL;
b3995439 183
b3995439 184 /* calculate hash key */
de03b596 185 debugs(39, 2, "carpSelectParent: Calculating hash for " << urlCanonical(request));
62e76326 186
afd88fbe 187 /* select peer */
d7ae3534 188 for (k = 0; k < n_carp_peers; ++k) {
96f6f33b 189 String key;
62e76326 190 tp = carp_peers[k];
de03b596
FC
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
199 key.append("://");
200 }
201 if (tp->options.carp_key.host) {
202 key.append(request->GetHost());
203 }
204 if (tp->options.carp_key.port) {
205 static char portbuf[7];
206 snprintf(portbuf,7,":%d", request->port);
207 key.append(portbuf);
208 }
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));
213 else
214 key.append(request->urlpath);
215 }
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()));
220 }
96f6f33b 221 }
de03b596
FC
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
225
226 if (key.size()==0)
227 key=urlCanonical(request);
228
d7ae3534 229 for (const char *c = key.rawBuf(), *e=key.rawBuf()+key.size(); c < e; ++c)
de03b596 230 user_hash += ROTATE_LEFT(user_hash, 19) + *c;
62e76326 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;
de03b596
FC
235 debugs(39, 3, "carpSelectParent: key=" << key << " name=" << tp->name << " combined_hash=" << combined_hash <<
236 " score=" << std::setprecision(0) << score);
62e76326 237
238 if ((score > high_score) && peerHTTPOkay(tp, request)) {
239 p = tp;
240 high_score = score;
241 }
afd88fbe 242 }
62e76326 243
afd88fbe 244 if (p)
d4140027 245 debugs(39, 2, "carpSelectParent: selected " << p->name);
62e76326 246
afd88fbe 247 return p;
248}
8ee9b49f 249
250static void
251carpCachemgr(StoreEntry * sentry)
252{
253 peer *p;
254 int sumfetches = 0;
255 storeAppendPrintf(sentry, "%24s %10s %10s %10s %10s\n",
62e76326 256 "Hostname",
257 "Hash",
258 "Multiplier",
259 "Factor",
260 "Actual");
261
8ee9b49f 262 for (p = Config.peers; p; p = p->next)
62e76326 263 sumfetches += p->stats.fetches;
264
8ee9b49f 265 for (p = Config.peers; p; p = p->next) {
62e76326 266 storeAppendPrintf(sentry, "%24s %10x %10f %10f %10f\n",
d4140027 267 p->name, p->carp.hash,
62e76326 268 p->carp.load_multiplier,
269 p->carp.load_factor,
270 sumfetches ? (double) p->stats.fetches / sumfetches : -1.0);
8ee9b49f 271 }
8ee9b49f 272}