]> git.ipfire.org Git - thirdparty/squid.git/blob - src/carp.cc
Cache Manager initialization reshuffling.
[thirdparty/squid.git] / src / carp.cc
1
2 /*
3 * $Id: carp.cc,v 1.27 2008/01/14 12:13:49 hno Exp $
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 "CacheManager.h"
39 #include "Store.h"
40
41 #if USE_CARP
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 void
58 carpInit(void)
59 {
60 int W = 0;
61 int K;
62 int k;
63 double P_last, X_last, Xn;
64 peer *p;
65 peer **P;
66 char *t;
67 /* Clean up */
68
69 for (k = 0; k < n_carp_peers; k++) {
70 cbdataReferenceDone(carp_peers[k]);
71 }
72
73 safe_free(carp_peers);
74 n_carp_peers = 0;
75
76 /* initialize cache manager before we have a chance to leave the execution path */
77 carpRegisterWithCacheManager();
78
79 /* find out which peers we have */
80
81 for (p = Config.peers; p; p = p->next) {
82 if (!p->options.carp)
83 continue;
84
85 assert(p->type == PEER_PARENT);
86
87 if (p->weight == 0)
88 continue;
89
90 n_carp_peers++;
91
92 W += p->weight;
93 }
94
95 if (n_carp_peers == 0)
96 return;
97
98 carp_peers = (peer **)xcalloc(n_carp_peers, sizeof(*carp_peers));
99
100 /* Build a list of the found peers and calculate hashes and load factors */
101 for (P = carp_peers, p = Config.peers; p; p = p->next) {
102 if (!p->options.carp)
103 continue;
104
105 if (p->weight == 0)
106 continue;
107
108 /* calculate this peers hash */
109 p->carp.hash = 0;
110
111 for (t = p->name; *t != 0; t++)
112 p->carp.hash += ROTATE_LEFT(p->carp.hash, 19) + (unsigned int) *t;
113
114 p->carp.hash += p->carp.hash * 0x62531965;
115
116 p->carp.hash = ROTATE_LEFT(p->carp.hash, 21);
117
118 /* and load factor */
119 p->carp.load_factor = ((double) p->weight) / (double) W;
120
121 if (floor(p->carp.load_factor * 1000.0) == 0.0)
122 p->carp.load_factor = 0.0;
123
124 /* add it to our list of peers */
125 *P++ = cbdataReference(p);
126 }
127
128 /* Sort our list on weight */
129 qsort(carp_peers, n_carp_peers, sizeof(*carp_peers), peerSortWeight);
130
131 /* Calculate the load factor multipliers X_k
132 *
133 * X_1 = pow ((K*p_1), (1/K))
134 * X_k = ([K-k+1] * [P_k - P_{k-1}])/(X_1 * X_2 * ... * X_{k-1})
135 * X_k += pow ((X_{k-1}, {K-k+1})
136 * X_k = pow (X_k, {1/(K-k+1)})
137 * simplified to have X_1 part of the loop
138 */
139 K = n_carp_peers;
140
141 P_last = 0.0; /* Empty P_0 */
142
143 Xn = 1.0; /* Empty starting point of X_1 * X_2 * ... * X_{x-1} */
144
145 X_last = 0.0; /* Empty X_0, nullifies the first pow statement */
146
147 for (k = 1; k <= K; k++) {
148 double Kk1 = (double) (K - k + 1);
149 p = carp_peers[k - 1];
150 p->carp.load_multiplier = (Kk1 * (p->carp.load_factor - P_last)) / Xn;
151 p->carp.load_multiplier += pow(X_last, Kk1);
152 p->carp.load_multiplier = pow(p->carp.load_multiplier, 1.0 / Kk1);
153 Xn *= p->carp.load_multiplier;
154 X_last = p->carp.load_multiplier;
155 P_last = p->carp.load_factor;
156 }
157 }
158
159 void
160 carpRegisterWithCacheManager()
161 {
162 CacheManager::GetInstance()->
163 registerAction("carp", "CARP information", carpCachemgr, 0, 1);
164 }
165
166 peer *
167 carpSelectParent(HttpRequest * request)
168 {
169 int k;
170 const char *c;
171 peer *p = NULL;
172 peer *tp;
173 unsigned int user_hash = 0;
174 unsigned int combined_hash;
175 double score;
176 double high_score = 0;
177 const char *key = NULL;
178
179 if (n_carp_peers == 0)
180 return NULL;
181
182 key = urlCanonical(request);
183
184 /* calculate hash key */
185 debugs(39, 2, "carpSelectParent: Calculating hash for " << key);
186
187 for (c = key; *c != 0; c++)
188 user_hash += ROTATE_LEFT(user_hash, 19) + *c;
189
190 /* select peer */
191 for (k = 0; k < n_carp_peers; k++) {
192 tp = carp_peers[k];
193 combined_hash = (user_hash ^ tp->carp.hash);
194 combined_hash += combined_hash * 0x62531965;
195 combined_hash = ROTATE_LEFT(combined_hash, 21);
196 score = combined_hash * tp->carp.load_multiplier;
197 debugs(39, 3, "carpSelectParent: " << tp->name << " combined_hash " << combined_hash <<
198 " score " << std::setprecision(0) << score);
199
200 if ((score > high_score) && peerHTTPOkay(tp, request)) {
201 p = tp;
202 high_score = score;
203 }
204 }
205
206 if (p)
207 debugs(39, 2, "carpSelectParent: selected " << p->name);
208
209 return p;
210 }
211
212 static void
213 carpCachemgr(StoreEntry * sentry)
214 {
215 peer *p;
216 int sumfetches = 0;
217 storeAppendPrintf(sentry, "%24s %10s %10s %10s %10s\n",
218 "Hostname",
219 "Hash",
220 "Multiplier",
221 "Factor",
222 "Actual");
223
224 for (p = Config.peers; p; p = p->next)
225 sumfetches += p->stats.fetches;
226
227 for (p = Config.peers; p; p = p->next) {
228 storeAppendPrintf(sentry, "%24s %10x %10f %10f %10f\n",
229 p->name, p->carp.hash,
230 p->carp.load_multiplier,
231 p->carp.load_factor,
232 sumfetches ? (double) p->stats.fetches / sumfetches : -1.0);
233 }
234 }
235
236 #endif