]>
Commit | Line | Data |
---|---|---|
afd88fbe | 1 | /* |
b8ae064d | 2 | * Copyright (C) 1996-2023 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" |
e7959b56 | 13 | #include "CachePeers.h" |
8b082ed9 | 14 | #include "carp.h" |
de03b596 | 15 | #include "HttpRequest.h" |
8822ebee | 16 | #include "mgr/Registration.h" |
f0ba2534 | 17 | #include "neighbors.h" |
cb365059 | 18 | #include "PeerSelectState.h" |
4d5904f7 | 19 | #include "SquidConfig.h" |
e6ccf245 | 20 | #include "Store.h" |
afd88fbe | 21 | |
074d6a40 | 22 | #include <cmath> |
582c2af2 | 23 | |
b3995439 | 24 | #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) |
25 | ||
e7959b56 | 26 | /// CARP cache_peers ordered by their CARP weight |
2d5ac435 EB |
27 | static auto & |
28 | CarpPeers() | |
29 | { | |
30 | static const auto carpPeers = new SelectedCachePeers(); | |
31 | return *carpPeers; | |
32 | } | |
e7959b56 | 33 | |
8ee9b49f | 34 | static OBJH carpCachemgr; |
35 | ||
b3995439 | 36 | static int |
37 | peerSortWeight(const void *a, const void *b) | |
38 | { | |
a3c6762c FC |
39 | const CachePeer *const *p1 = (const CachePeer *const *)a; |
40 | const CachePeer *const *p2 = (const CachePeer *const *)b; | |
b3995439 | 41 | return (*p1)->weight - (*p2)->weight; |
42 | } | |
43 | ||
5f5e883f FC |
44 | static void |
45 | carpRegisterWithCacheManager(void) | |
46 | { | |
8822ebee | 47 | Mgr::RegisterAction("carp", "CARP information", carpCachemgr, 0, 1); |
5f5e883f FC |
48 | } |
49 | ||
afd88fbe | 50 | void |
51 | carpInit(void) | |
52 | { | |
b3995439 | 53 | int W = 0; |
b3995439 | 54 | double P_last, X_last, Xn; |
b3995439 | 55 | char *t; |
56 | /* Clean up */ | |
62e76326 | 57 | |
2d5ac435 | 58 | CarpPeers().clear(); |
8cb183ec FC |
59 | |
60 | /* initialize cache manager before we have a chance to leave the execution path */ | |
61 | carpRegisterWithCacheManager(); | |
62 | ||
b3995439 | 63 | /* find out which peers we have */ |
62e76326 | 64 | |
e7959b56 | 65 | RawCachePeers rawCarpPeers; |
2e24d0bf EB |
66 | for (const auto &peer: CurrentCachePeers()) { |
67 | const auto p = peer.get(); | |
68 | ||
62e76326 | 69 | if (!p->options.carp) |
70 | continue; | |
71 | ||
72 | assert(p->type == PEER_PARENT); | |
73 | ||
74 | if (p->weight == 0) | |
75 | continue; | |
76 | ||
e7959b56 | 77 | rawCarpPeers.push_back(p); |
62e76326 | 78 | |
79 | W += p->weight; | |
afd88fbe | 80 | } |
62e76326 | 81 | |
e7959b56 | 82 | if (rawCarpPeers.empty()) |
62e76326 | 83 | return; |
84 | ||
e7959b56 EB |
85 | /* calculate hashes and load factors */ |
86 | for (const auto p: rawCarpPeers) { | |
62e76326 | 87 | /* calculate this peers hash */ |
88 | p->carp.hash = 0; | |
89 | ||
d7ae3534 | 90 | for (t = p->name; *t != 0; ++t) |
62e76326 | 91 | p->carp.hash += ROTATE_LEFT(p->carp.hash, 19) + (unsigned int) *t; |
92 | ||
93 | p->carp.hash += p->carp.hash * 0x62531965; | |
94 | ||
95 | p->carp.hash = ROTATE_LEFT(p->carp.hash, 21); | |
96 | ||
97 | /* and load factor */ | |
98 | p->carp.load_factor = ((double) p->weight) / (double) W; | |
99 | ||
100 | if (floor(p->carp.load_factor * 1000.0) == 0.0) | |
101 | p->carp.load_factor = 0.0; | |
67766c17 | 102 | } |
62e76326 | 103 | |
b3995439 | 104 | /* Sort our list on weight */ |
e7959b56 | 105 | qsort(rawCarpPeers.data(), rawCarpPeers.size(), sizeof(decltype(rawCarpPeers)::value_type), peerSortWeight); |
62e76326 | 106 | |
b3995439 | 107 | /* Calculate the load factor multipliers X_k |
108 | * | |
109 | * X_1 = pow ((K*p_1), (1/K)) | |
110 | * X_k = ([K-k+1] * [P_k - P_{k-1}])/(X_1 * X_2 * ... * X_{k-1}) | |
111 | * X_k += pow ((X_{k-1}, {K-k+1}) | |
112 | * X_k = pow (X_k, {1/(K-k+1)}) | |
113 | * simplified to have X_1 part of the loop | |
afd88fbe | 114 | */ |
e7959b56 | 115 | const auto K = rawCarpPeers.size(); |
62e76326 | 116 | |
f53969cc | 117 | P_last = 0.0; /* Empty P_0 */ |
62e76326 | 118 | |
f53969cc | 119 | Xn = 1.0; /* Empty starting point of X_1 * X_2 * ... * X_{x-1} */ |
62e76326 | 120 | |
f53969cc | 121 | X_last = 0.0; /* Empty X_0, nullifies the first pow statement */ |
62e76326 | 122 | |
e7959b56 | 123 | for (size_t k = 1; k <= K; ++k) { |
62e76326 | 124 | double Kk1 = (double) (K - k + 1); |
e7959b56 | 125 | const auto p = rawCarpPeers[k - 1]; |
62e76326 | 126 | p->carp.load_multiplier = (Kk1 * (p->carp.load_factor - P_last)) / Xn; |
127 | p->carp.load_multiplier += pow(X_last, Kk1); | |
128 | p->carp.load_multiplier = pow(p->carp.load_multiplier, 1.0 / Kk1); | |
129 | Xn *= p->carp.load_multiplier; | |
130 | X_last = p->carp.load_multiplier; | |
131 | P_last = p->carp.load_factor; | |
afd88fbe | 132 | } |
e7959b56 | 133 | |
2d5ac435 | 134 | CarpPeers().assign(rawCarpPeers.begin(), rawCarpPeers.end()); |
62ee09ca | 135 | } |
62e76326 | 136 | |
a3c6762c | 137 | CachePeer * |
cb365059 | 138 | carpSelectParent(PeerSelector *ps) |
afd88fbe | 139 | { |
cb365059 EB |
140 | assert(ps); |
141 | HttpRequest *request = ps->request; | |
142 | ||
aee3523a | 143 | CachePeer *p = nullptr; |
b3995439 | 144 | unsigned int user_hash = 0; |
145 | unsigned int combined_hash; | |
146 | double score; | |
147 | double high_score = 0; | |
b3995439 | 148 | |
2d5ac435 | 149 | if (CarpPeers().empty()) |
aee3523a | 150 | return nullptr; |
b3995439 | 151 | |
b3995439 | 152 | /* calculate hash key */ |
851feda6 | 153 | debugs(39, 2, "carpSelectParent: Calculating hash for " << request->effectiveRequestUri()); |
62e76326 | 154 | |
a3c6762c | 155 | /* select CachePeer */ |
2d5ac435 | 156 | for (const auto &tp: CarpPeers()) { |
e7959b56 EB |
157 | if (!tp) |
158 | continue; // peer gone | |
159 | ||
2128aa19 | 160 | SBuf key; |
de03b596 | 161 | if (tp->options.carp_key.set) { |
851feda6 AJ |
162 | // this code follows URI syntax pattern. |
163 | // corner cases should use the full effective request URI | |
de03b596 | 164 | if (tp->options.carp_key.scheme) { |
d31d59d8 | 165 | key.append(request->url.getScheme().image()); |
2128aa19 | 166 | if (key.length()) //if the scheme is not empty |
de03b596 FC |
167 | key.append("://"); |
168 | } | |
169 | if (tp->options.carp_key.host) { | |
5c51bffb | 170 | key.append(request->url.host()); |
de03b596 FC |
171 | } |
172 | if (tp->options.carp_key.port) { | |
380b09ae | 173 | key.appendf(":%hu", request->url.port().value_or(0)); |
de03b596 FC |
174 | } |
175 | if (tp->options.carp_key.path) { | |
51b5dcf5 AJ |
176 | // XXX: fix when path and query are separate |
177 | key.append(request->url.path().substr(0,request->url.path().find('?'))); // 0..N | |
de03b596 FC |
178 | } |
179 | if (tp->options.carp_key.params) { | |
51b5dcf5 AJ |
180 | // XXX: fix when path and query are separate |
181 | SBuf::size_type pos; | |
182 | if ((pos=request->url.path().find('?')) != SBuf::npos) | |
183 | key.append(request->url.path().substr(pos)); // N..npos | |
de03b596 | 184 | } |
96f6f33b | 185 | } |
de03b596 FC |
186 | // if the url-based key is empty, e.g. because the user is |
187 | // asking to balance on the path but the request doesn't supply any, | |
851feda6 | 188 | // then fall back to the effective request URI |
de03b596 | 189 | |
2128aa19 | 190 | if (key.isEmpty()) |
851feda6 | 191 | key=request->effectiveRequestUri(); |
de03b596 | 192 | |
2128aa19 | 193 | for (const char *c = key.rawContent(), *e=key.rawContent()+key.length(); c < e; ++c) |
de03b596 | 194 | user_hash += ROTATE_LEFT(user_hash, 19) + *c; |
62e76326 | 195 | combined_hash = (user_hash ^ tp->carp.hash); |
196 | combined_hash += combined_hash * 0x62531965; | |
197 | combined_hash = ROTATE_LEFT(combined_hash, 21); | |
198 | score = combined_hash * tp->carp.load_multiplier; | |
a555a85b | 199 | debugs(39, 3, *tp << " key=" << key << " combined_hash=" << combined_hash << |
de03b596 | 200 | " score=" << std::setprecision(0) << score); |
62e76326 | 201 | |
e7959b56 EB |
202 | if ((score > high_score) && peerHTTPOkay(tp.get(), ps)) { |
203 | p = tp.get(); | |
62e76326 | 204 | high_score = score; |
205 | } | |
afd88fbe | 206 | } |
62e76326 | 207 | |
afd88fbe | 208 | if (p) |
a555a85b | 209 | debugs(39, 2, "selected " << *p); |
62e76326 | 210 | |
afd88fbe | 211 | return p; |
212 | } | |
8ee9b49f | 213 | |
214 | static void | |
215 | carpCachemgr(StoreEntry * sentry) | |
216 | { | |
8ee9b49f | 217 | int sumfetches = 0; |
218 | storeAppendPrintf(sentry, "%24s %10s %10s %10s %10s\n", | |
62e76326 | 219 | "Hostname", |
220 | "Hash", | |
221 | "Multiplier", | |
222 | "Factor", | |
223 | "Actual"); | |
224 | ||
2d5ac435 | 225 | for (const auto &p: CarpPeers()) { |
e7959b56 EB |
226 | if (!p) |
227 | continue; | |
62e76326 | 228 | sumfetches += p->stats.fetches; |
e7959b56 | 229 | } |
62e76326 | 230 | |
2d5ac435 | 231 | for (const auto &p: CarpPeers()) { |
e7959b56 EB |
232 | if (!p) |
233 | continue; | |
62e76326 | 234 | storeAppendPrintf(sentry, "%24s %10x %10f %10f %10f\n", |
d4140027 | 235 | p->name, p->carp.hash, |
62e76326 | 236 | p->carp.load_multiplier, |
237 | p->carp.load_factor, | |
238 | sumfetches ? (double) p->stats.fetches / sumfetches : -1.0); | |
8ee9b49f | 239 | } |
8ee9b49f | 240 | } |
f53969cc | 241 |