]> git.ipfire.org Git - thirdparty/squid.git/blame_incremental - src/carp.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / carp.cc
... / ...
CommitLineData
1/*
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9/* DEBUG: section 39 Cache Array Routing Protocol */
10
11#include "squid.h"
12#include "CachePeer.h"
13#include "HttpRequest.h"
14#include "mgr/Registration.h"
15#include "neighbors.h"
16#include "SquidConfig.h"
17#include "Store.h"
18#include "URL.h"
19
20#include <cmath>
21
22#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
23
24static int n_carp_peers = 0;
25static CachePeer **carp_peers = NULL;
26static OBJH carpCachemgr;
27
28static int
29peerSortWeight(const void *a, const void *b)
30{
31 const CachePeer *const *p1 = (const CachePeer *const *)a;
32 const CachePeer *const *p2 = (const CachePeer *const *)b;
33 return (*p1)->weight - (*p2)->weight;
34}
35
36static void
37carpRegisterWithCacheManager(void)
38{
39 Mgr::RegisterAction("carp", "CARP information", carpCachemgr, 0, 1);
40}
41
42void
43carpInit(void)
44{
45 int W = 0;
46 int K;
47 int k;
48 double P_last, X_last, Xn;
49 CachePeer *p;
50 CachePeer **P;
51 char *t;
52 /* Clean up */
53
54 for (k = 0; k < n_carp_peers; ++k) {
55 cbdataReferenceDone(carp_peers[k]);
56 }
57
58 safe_free(carp_peers);
59 n_carp_peers = 0;
60
61 /* initialize cache manager before we have a chance to leave the execution path */
62 carpRegisterWithCacheManager();
63
64 /* find out which peers we have */
65
66 for (p = Config.peers; p; p = p->next) {
67 if (!p->options.carp)
68 continue;
69
70 assert(p->type == PEER_PARENT);
71
72 if (p->weight == 0)
73 continue;
74
75 ++n_carp_peers;
76
77 W += p->weight;
78 }
79
80 if (n_carp_peers == 0)
81 return;
82
83 carp_peers = (CachePeer **)xcalloc(n_carp_peers, sizeof(*carp_peers));
84
85 /* Build a list of the found peers and calculate hashes and load factors */
86 for (P = carp_peers, p = Config.peers; p; p = p->next) {
87 if (!p->options.carp)
88 continue;
89
90 if (p->weight == 0)
91 continue;
92
93 /* calculate this peers hash */
94 p->carp.hash = 0;
95
96 for (t = p->name; *t != 0; ++t)
97 p->carp.hash += ROTATE_LEFT(p->carp.hash, 19) + (unsigned int) *t;
98
99 p->carp.hash += p->carp.hash * 0x62531965;
100
101 p->carp.hash = ROTATE_LEFT(p->carp.hash, 21);
102
103 /* and load factor */
104 p->carp.load_factor = ((double) p->weight) / (double) W;
105
106 if (floor(p->carp.load_factor * 1000.0) == 0.0)
107 p->carp.load_factor = 0.0;
108
109 /* add it to our list of peers */
110 *P = cbdataReference(p);
111 ++P;
112 }
113
114 /* Sort our list on weight */
115 qsort(carp_peers, n_carp_peers, sizeof(*carp_peers), peerSortWeight);
116
117 /* Calculate the load factor multipliers X_k
118 *
119 * X_1 = pow ((K*p_1), (1/K))
120 * X_k = ([K-k+1] * [P_k - P_{k-1}])/(X_1 * X_2 * ... * X_{k-1})
121 * X_k += pow ((X_{k-1}, {K-k+1})
122 * X_k = pow (X_k, {1/(K-k+1)})
123 * simplified to have X_1 part of the loop
124 */
125 K = n_carp_peers;
126
127 P_last = 0.0; /* Empty P_0 */
128
129 Xn = 1.0; /* Empty starting point of X_1 * X_2 * ... * X_{x-1} */
130
131 X_last = 0.0; /* Empty X_0, nullifies the first pow statement */
132
133 for (k = 1; k <= K; ++k) {
134 double Kk1 = (double) (K - k + 1);
135 p = carp_peers[k - 1];
136 p->carp.load_multiplier = (Kk1 * (p->carp.load_factor - P_last)) / Xn;
137 p->carp.load_multiplier += pow(X_last, Kk1);
138 p->carp.load_multiplier = pow(p->carp.load_multiplier, 1.0 / Kk1);
139 Xn *= p->carp.load_multiplier;
140 X_last = p->carp.load_multiplier;
141 P_last = p->carp.load_factor;
142 }
143}
144
145CachePeer *
146carpSelectParent(HttpRequest * request)
147{
148 int k;
149 CachePeer *p = NULL;
150 CachePeer *tp;
151 unsigned int user_hash = 0;
152 unsigned int combined_hash;
153 double score;
154 double high_score = 0;
155
156 if (n_carp_peers == 0)
157 return NULL;
158
159 /* calculate hash key */
160 debugs(39, 2, "carpSelectParent: Calculating hash for " << urlCanonical(request));
161
162 /* select CachePeer */
163 for (k = 0; k < n_carp_peers; ++k) {
164 SBuf key;
165 tp = carp_peers[k];
166 if (tp->options.carp_key.set) {
167 //this code follows urlCanonical's pattern.
168 // corner cases should use the canonical URL
169 if (tp->options.carp_key.scheme) {
170 key.append(request->url.getScheme().c_str());
171 if (key.length()) //if the scheme is not empty
172 key.append("://");
173 }
174 if (tp->options.carp_key.host) {
175 key.append(request->GetHost());
176 }
177 if (tp->options.carp_key.port) {
178 key.appendf(":%d", request->port);
179 }
180 if (tp->options.carp_key.path) {
181 String::size_type pos;
182 if ((pos=request->urlpath.find('?'))!=String::npos)
183 key.append(SBuf(request->urlpath.substr(0,pos)));
184 else
185 key.append(SBuf(request->urlpath));
186 }
187 if (tp->options.carp_key.params) {
188 String::size_type pos;
189 if ((pos=request->urlpath.find('?'))!=String::npos)
190 key.append(SBuf(request->urlpath.substr(pos,request->urlpath.size())));
191 }
192 }
193 // if the url-based key is empty, e.g. because the user is
194 // asking to balance on the path but the request doesn't supply any,
195 // then fall back to canonical URL
196
197 if (key.isEmpty())
198 key=SBuf(urlCanonical(request));
199
200 for (const char *c = key.rawContent(), *e=key.rawContent()+key.length(); c < e; ++c)
201 user_hash += ROTATE_LEFT(user_hash, 19) + *c;
202 combined_hash = (user_hash ^ tp->carp.hash);
203 combined_hash += combined_hash * 0x62531965;
204 combined_hash = ROTATE_LEFT(combined_hash, 21);
205 score = combined_hash * tp->carp.load_multiplier;
206 debugs(39, 3, "carpSelectParent: key=" << key << " name=" << tp->name << " combined_hash=" << combined_hash <<
207 " score=" << std::setprecision(0) << score);
208
209 if ((score > high_score) && peerHTTPOkay(tp, request)) {
210 p = tp;
211 high_score = score;
212 }
213 }
214
215 if (p)
216 debugs(39, 2, "carpSelectParent: selected " << p->name);
217
218 return p;
219}
220
221static void
222carpCachemgr(StoreEntry * sentry)
223{
224 CachePeer *p;
225 int sumfetches = 0;
226 storeAppendPrintf(sentry, "%24s %10s %10s %10s %10s\n",
227 "Hostname",
228 "Hash",
229 "Multiplier",
230 "Factor",
231 "Actual");
232
233 for (p = Config.peers; p; p = p->next)
234 sumfetches += p->stats.fetches;
235
236 for (p = Config.peers; p; p = p->next) {
237 storeAppendPrintf(sentry, "%24s %10x %10f %10f %10f\n",
238 p->name, p->carp.hash,
239 p->carp.load_multiplier,
240 p->carp.load_factor,
241 sumfetches ? (double) p->stats.fetches / sumfetches : -1.0);
242 }
243}
244