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