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