]> git.ipfire.org Git - thirdparty/squid.git/blame - src/carp.cc
Bootstrapped
[thirdparty/squid.git] / src / carp.cc
CommitLineData
f740a279 1
afd88fbe 2/*
b3995439 3 * $Id: carp.cc,v 1.16 2002/04/04 21:03:46 hno Exp $
afd88fbe 4 *
67f46679 5 * DEBUG: section 39 Cache Array Routing Protocol
b3995439 6 * AUTHOR: Henrik Nordstrom
7 * BASED ON: carp.c by Eric Stern and draft-vinod-carp-v1-03.txt
afd88fbe 8 *
2b6662ba 9 * SQUID Web Proxy Cache http://www.squid-cache.org/
e25c139f 10 * ----------------------------------------------------------
11 *
2b6662ba 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.
e25c139f 20 *
afd88fbe 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.
e25c139f 25 *
afd88fbe 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.
e25c139f 30 *
afd88fbe 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
cbdec147 33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
e25c139f 34 *
0cdcddb9 35 */
afd88fbe 36
37#include "squid.h"
38
39#if USE_CARP
40
b3995439 41#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
42
43static int n_carp_peers = 0;
44static peer **carp_peers = NULL;
8ee9b49f 45static OBJH carpCachemgr;
46
b3995439 47static int
48peerSortWeight(const void *a, const void *b)
49{
50 const peer *const *p1 = a, *const *p2 = b;
51 return (*p1)->weight - (*p2)->weight;
52}
53
afd88fbe 54void
55carpInit(void)
56{
b3995439 57 int W = 0;
58 int K;
afd88fbe 59 int k;
b3995439 60 double P_last, X_last, Xn;
afd88fbe 61 peer *p;
b3995439 62 peer **P;
63 char *t;
64 /* Clean up */
65 for (k = 0; k < n_carp_peers; k++) {
66 cbdataUnlock(carp_peers[k]);
67 }
68 safe_free(carp_peers);
69 n_carp_peers = 0;
70 /* find out which peers we have */
afd88fbe 71 for (p = Config.peers; p; p = p->next) {
b3995439 72 if (!p->options.carp)
73 continue;
74 assert(p->type == PEER_PARENT);
75 if (p->weight == 0)
76 continue;
77 n_carp_peers++;
78 W += p->weight;
afd88fbe 79 }
b3995439 80 if (n_carp_peers == 0)
afd88fbe 81 return;
b3995439 82 carp_peers = xcalloc(n_carp_peers, sizeof(*carp_peers));
83 /* Build a list of the found peers and calculate hashes and load factors */
84 for (P = carp_peers, p = Config.peers; p; p = p->next) {
85 if (!p->options.carp)
86 continue;
87 if (p->weight == 0)
88 continue;
89 /* calculate this peers hash */
90 p->carp.hash = 0;
91 for (t = p->host; *t != 0; t++)
92 p->carp.hash += ROTATE_LEFT(p->carp.hash, 19) + (unsigned int) *t;
93 p->carp.hash += p->carp.hash * 0x62531965;
94 p->carp.hash = ROTATE_LEFT(p->carp.hash, 21);
95 /* and load factor */
96 p->carp.load_factor = ((double) p->weight) / (double) W;
97 if (floor(p->carp.load_factor * 1000.0) == 0.0)
98 p->carp.load_factor = 0.0;
99 /* add it to our list of peers */
100 *P++ = p;
101 cbdataLock(p);
67766c17 102 }
b3995439 103 /* Sort our list on weight */
104 qsort(carp_peers, n_carp_peers, sizeof(*carp_peers), peerSortWeight);
105 /* Calculate the load factor multipliers X_k
106 *
107 * X_1 = pow ((K*p_1), (1/K))
108 * X_k = ([K-k+1] * [P_k - P_{k-1}])/(X_1 * X_2 * ... * X_{k-1})
109 * X_k += pow ((X_{k-1}, {K-k+1})
110 * X_k = pow (X_k, {1/(K-k+1)})
111 * simplified to have X_1 part of the loop
afd88fbe 112 */
b3995439 113 K = n_carp_peers;
114 P_last = 0.0; /* Empty P_0 */
115 Xn = 1.0; /* Empty starting point of X_1 * X_2 * ... * X_{x-1} */
116 X_last = 0.0; /* Empty X_0, nullifies the first pow statement */
117 for (k = 1; k <= K; k++) {
118 double Kk1 = (double) (K - k + 1);
119 p = carp_peers[k - 1];
120 p->carp.load_multiplier = (Kk1 * (p->carp.load_factor - P_last)) / Xn;
121 p->carp.load_multiplier += pow(X_last, Kk1);
122 p->carp.load_multiplier = pow(p->carp.load_multiplier, 1.0 / Kk1);
67766c17 123 Xn *= p->carp.load_multiplier;
124 X_last = p->carp.load_multiplier;
67766c17 125 P_last = p->carp.load_factor;
afd88fbe 126 }
8ee9b49f 127 cachemgrRegister("carp", "CARP information", carpCachemgr, 0, 1);
afd88fbe 128}
129
130peer *
131carpSelectParent(request_t * request)
132{
b3995439 133 int k;
afd88fbe 134 const char *c;
135 peer *p = NULL;
136 peer *tp;
b3995439 137 unsigned int user_hash = 0;
138 unsigned int combined_hash;
139 double score;
140 double high_score = 0;
141 char *key = NULL;
142
143 if (n_carp_peers == 0)
144 return NULL;
145
146 key = urlCanonical(url);
147
148 /* calculate hash key */
149 debug(39, 2) ("carpSelectParent: Calculating hash for %s\n", key);
150 for (c = key; *c != 0; c++)
151 user_hash += ROTATE_LEFT(user_hash, 19) + *c;
afd88fbe 152 /* select peer */
b3995439 153 for (k = 0; k < n_carp_peers; k++) {
154 tp = carp_peers[k];
155 combined_hash = (user_hash ^ tp->carp.hash);
afd88fbe 156 combined_hash += combined_hash * 0x62531965;
d20b1cd0 157 combined_hash = ROTATE_LEFT(combined_hash, 21);
b3995439 158 score = combined_hash * tp->carp.load_multiplier;
159 debug(39, 3) ("carpSelectParent: %s combined_hash %u score %.0f\n",
160 tp->host, combined_hash, score);
161 if ((score > high_score) && peerHTTPOkay(tp, request)) {
afd88fbe 162 p = tp;
b3995439 163 high_score = score;
afd88fbe 164 }
165 }
166 if (p)
b3995439 167 debug(39, 2) ("carpSelectParent: selected %s\n", p->host);
afd88fbe 168 return p;
169}
8ee9b49f 170
171static void
172carpCachemgr(StoreEntry * sentry)
173{
174 peer *p;
175 int sumfetches = 0;
176 storeAppendPrintf(sentry, "%24s %10s %10s %10s %10s\n",
177 "Hostname",
178 "Hash",
179 "Multiplier",
180 "Factor",
181 "Actual");
182 for (p = Config.peers; p; p = p->next)
183 sumfetches += p->stats.fetches;
184 for (p = Config.peers; p; p = p->next) {
185 storeAppendPrintf(sentry, "%24s %10x %10f %10f %10f\n",
186 p->host, p->carp.hash,
187 p->carp.load_multiplier,
188 p->carp.load_factor,
189 sumfetches ? (double) p->stats.fetches / sumfetches : -1.0);
190 }
8ee9b49f 191}
192
afd88fbe 193#endif