]>
Commit | Line | Data |
---|---|---|
afd88fbe | 1 | /* |
f70aedc4 | 2 | * Copyright (C) 1996-2021 The Squid Software Foundation and contributors |
e25c139f | 3 | * |
bbc27441 AJ |
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. | |
0cdcddb9 | 7 | */ |
afd88fbe | 8 | |
bbc27441 AJ |
9 | /* DEBUG: section 39 Cache Array Routing Protocol */ |
10 | ||
582c2af2 | 11 | #include "squid.h" |
a011edee | 12 | #include "CachePeer.h" |
de03b596 | 13 | #include "HttpRequest.h" |
8822ebee | 14 | #include "mgr/Registration.h" |
f0ba2534 | 15 | #include "neighbors.h" |
cb365059 | 16 | #include "PeerSelectState.h" |
4d5904f7 | 17 | #include "SquidConfig.h" |
e6ccf245 | 18 | #include "Store.h" |
afd88fbe | 19 | |
074d6a40 | 20 | #include <cmath> |
582c2af2 | 21 | |
b3995439 | 22 | #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) |
23 | ||
24 | static int n_carp_peers = 0; | |
a3c6762c | 25 | static CachePeer **carp_peers = NULL; |
8ee9b49f | 26 | static OBJH carpCachemgr; |
27 | ||
b3995439 | 28 | static int |
29 | peerSortWeight(const void *a, const void *b) | |
30 | { | |
a3c6762c FC |
31 | const CachePeer *const *p1 = (const CachePeer *const *)a; |
32 | const CachePeer *const *p2 = (const CachePeer *const *)b; | |
b3995439 | 33 | return (*p1)->weight - (*p2)->weight; |
34 | } | |
35 | ||
5f5e883f FC |
36 | static void |
37 | carpRegisterWithCacheManager(void) | |
38 | { | |
8822ebee | 39 | Mgr::RegisterAction("carp", "CARP information", carpCachemgr, 0, 1); |
5f5e883f FC |
40 | } |
41 | ||
afd88fbe | 42 | void |
43 | carpInit(void) | |
44 | { | |
b3995439 | 45 | int W = 0; |
46 | int K; | |
afd88fbe | 47 | int k; |
b3995439 | 48 | double P_last, X_last, Xn; |
a3c6762c FC |
49 | CachePeer *p; |
50 | CachePeer **P; | |
b3995439 | 51 | char *t; |
52 | /* Clean up */ | |
62e76326 | 53 | |
d7ae3534 | 54 | for (k = 0; k < n_carp_peers; ++k) { |
62e76326 | 55 | cbdataReferenceDone(carp_peers[k]); |
b3995439 | 56 | } |
62e76326 | 57 | |
b3995439 | 58 | safe_free(carp_peers); |
59 | n_carp_peers = 0; | |
8cb183ec FC |
60 | |
61 | /* initialize cache manager before we have a chance to leave the execution path */ | |
62 | carpRegisterWithCacheManager(); | |
63 | ||
b3995439 | 64 | /* find out which peers we have */ |
62e76326 | 65 | |
afd88fbe | 66 | for (p = Config.peers; p; p = p->next) { |
62e76326 | 67 | if (!p->options.carp) |
68 | continue; | |
69 | ||
70 | assert(p->type == PEER_PARENT); | |
71 | ||
72 | if (p->weight == 0) | |
73 | continue; | |
74 | ||
d7ae3534 | 75 | ++n_carp_peers; |
62e76326 | 76 | |
77 | W += p->weight; | |
afd88fbe | 78 | } |
62e76326 | 79 | |
b3995439 | 80 | if (n_carp_peers == 0) |
62e76326 | 81 | return; |
82 | ||
a3c6762c | 83 | carp_peers = (CachePeer **)xcalloc(n_carp_peers, sizeof(*carp_peers)); |
62e76326 | 84 | |
b3995439 | 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) { | |
62e76326 | 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 | ||
d7ae3534 | 96 | for (t = p->name; *t != 0; ++t) |
62e76326 | 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 */ | |
a38ec4b1 FC |
110 | *P = cbdataReference(p); |
111 | ++P; | |
67766c17 | 112 | } |
62e76326 | 113 | |
b3995439 | 114 | /* Sort our list on weight */ |
115 | qsort(carp_peers, n_carp_peers, sizeof(*carp_peers), peerSortWeight); | |
62e76326 | 116 | |
b3995439 | 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 | |
afd88fbe | 124 | */ |
b3995439 | 125 | K = n_carp_peers; |
62e76326 | 126 | |
f53969cc | 127 | P_last = 0.0; /* Empty P_0 */ |
62e76326 | 128 | |
f53969cc | 129 | Xn = 1.0; /* Empty starting point of X_1 * X_2 * ... * X_{x-1} */ |
62e76326 | 130 | |
f53969cc | 131 | X_last = 0.0; /* Empty X_0, nullifies the first pow statement */ |
62e76326 | 132 | |
d7ae3534 | 133 | for (k = 1; k <= K; ++k) { |
62e76326 | 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; | |
afd88fbe | 142 | } |
62ee09ca | 143 | } |
62e76326 | 144 | |
a3c6762c | 145 | CachePeer * |
cb365059 | 146 | carpSelectParent(PeerSelector *ps) |
afd88fbe | 147 | { |
cb365059 EB |
148 | assert(ps); |
149 | HttpRequest *request = ps->request; | |
150 | ||
b3995439 | 151 | int k; |
a3c6762c FC |
152 | CachePeer *p = NULL; |
153 | CachePeer *tp; | |
b3995439 | 154 | unsigned int user_hash = 0; |
155 | unsigned int combined_hash; | |
156 | double score; | |
157 | double high_score = 0; | |
b3995439 | 158 | |
159 | if (n_carp_peers == 0) | |
62e76326 | 160 | return NULL; |
b3995439 | 161 | |
b3995439 | 162 | /* calculate hash key */ |
851feda6 | 163 | debugs(39, 2, "carpSelectParent: Calculating hash for " << request->effectiveRequestUri()); |
62e76326 | 164 | |
a3c6762c | 165 | /* select CachePeer */ |
d7ae3534 | 166 | for (k = 0; k < n_carp_peers; ++k) { |
2128aa19 | 167 | SBuf key; |
62e76326 | 168 | tp = carp_peers[k]; |
de03b596 | 169 | if (tp->options.carp_key.set) { |
851feda6 AJ |
170 | // this code follows URI syntax pattern. |
171 | // corner cases should use the full effective request URI | |
de03b596 | 172 | if (tp->options.carp_key.scheme) { |
d31d59d8 | 173 | key.append(request->url.getScheme().image()); |
2128aa19 | 174 | if (key.length()) //if the scheme is not empty |
de03b596 FC |
175 | key.append("://"); |
176 | } | |
177 | if (tp->options.carp_key.host) { | |
5c51bffb | 178 | key.append(request->url.host()); |
de03b596 FC |
179 | } |
180 | if (tp->options.carp_key.port) { | |
5c51bffb | 181 | key.appendf(":%u", request->url.port()); |
de03b596 FC |
182 | } |
183 | if (tp->options.carp_key.path) { | |
51b5dcf5 AJ |
184 | // XXX: fix when path and query are separate |
185 | key.append(request->url.path().substr(0,request->url.path().find('?'))); // 0..N | |
de03b596 FC |
186 | } |
187 | if (tp->options.carp_key.params) { | |
51b5dcf5 AJ |
188 | // XXX: fix when path and query are separate |
189 | SBuf::size_type pos; | |
190 | if ((pos=request->url.path().find('?')) != SBuf::npos) | |
191 | key.append(request->url.path().substr(pos)); // N..npos | |
de03b596 | 192 | } |
96f6f33b | 193 | } |
de03b596 FC |
194 | // if the url-based key is empty, e.g. because the user is |
195 | // asking to balance on the path but the request doesn't supply any, | |
851feda6 | 196 | // then fall back to the effective request URI |
de03b596 | 197 | |
2128aa19 | 198 | if (key.isEmpty()) |
851feda6 | 199 | key=request->effectiveRequestUri(); |
de03b596 | 200 | |
2128aa19 | 201 | for (const char *c = key.rawContent(), *e=key.rawContent()+key.length(); c < e; ++c) |
de03b596 | 202 | user_hash += ROTATE_LEFT(user_hash, 19) + *c; |
62e76326 | 203 | combined_hash = (user_hash ^ tp->carp.hash); |
204 | combined_hash += combined_hash * 0x62531965; | |
205 | combined_hash = ROTATE_LEFT(combined_hash, 21); | |
206 | score = combined_hash * tp->carp.load_multiplier; | |
de03b596 FC |
207 | debugs(39, 3, "carpSelectParent: key=" << key << " name=" << tp->name << " combined_hash=" << combined_hash << |
208 | " score=" << std::setprecision(0) << score); | |
62e76326 | 209 | |
cb365059 | 210 | if ((score > high_score) && peerHTTPOkay(tp, ps)) { |
62e76326 | 211 | p = tp; |
212 | high_score = score; | |
213 | } | |
afd88fbe | 214 | } |
62e76326 | 215 | |
afd88fbe | 216 | if (p) |
d4140027 | 217 | debugs(39, 2, "carpSelectParent: selected " << p->name); |
62e76326 | 218 | |
afd88fbe | 219 | return p; |
220 | } | |
8ee9b49f | 221 | |
222 | static void | |
223 | carpCachemgr(StoreEntry * sentry) | |
224 | { | |
a3c6762c | 225 | CachePeer *p; |
8ee9b49f | 226 | int sumfetches = 0; |
227 | storeAppendPrintf(sentry, "%24s %10s %10s %10s %10s\n", | |
62e76326 | 228 | "Hostname", |
229 | "Hash", | |
230 | "Multiplier", | |
231 | "Factor", | |
232 | "Actual"); | |
233 | ||
8ee9b49f | 234 | for (p = Config.peers; p; p = p->next) |
62e76326 | 235 | sumfetches += p->stats.fetches; |
236 | ||
8ee9b49f | 237 | for (p = Config.peers; p; p = p->next) { |
62e76326 | 238 | storeAppendPrintf(sentry, "%24s %10x %10f %10f %10f\n", |
d4140027 | 239 | p->name, p->carp.hash, |
62e76326 | 240 | p->carp.load_multiplier, |
241 | p->carp.load_factor, | |
242 | sumfetches ? (double) p->stats.fetches / sumfetches : -1.0); | |
8ee9b49f | 243 | } |
8ee9b49f | 244 | } |
f53969cc | 245 |