2 * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 39 Cache Array Routing Protocol */
12 #include "base/RunnersRegistry.h"
13 #include "CachePeer.h"
14 #include "CachePeers.h"
16 #include "HttpRequest.h"
17 #include "mgr/Registration.h"
18 #include "neighbors.h"
19 #include "PeerSelectState.h"
20 #include "SquidConfig.h"
25 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
27 /// CARP cache_peers ordered by their CARP weight
31 static const auto carpPeers
= new SelectedCachePeers();
35 static OBJH carpCachemgr
;
38 peerSortWeight(const void *a
, const void *b
)
40 const CachePeer
*const *p1
= (const CachePeer
*const *)a
;
41 const CachePeer
*const *p2
= (const CachePeer
*const *)b
;
42 return (*p1
)->weight
- (*p2
)->weight
;
46 carpRegisterWithCacheManager(void)
48 Mgr::RegisterAction("carp", "CARP information", carpCachemgr
, 0, 1);
55 double P_last
, X_last
, Xn
;
61 /* initialize cache manager before we have a chance to leave the execution path */
62 carpRegisterWithCacheManager();
64 /* find out which peers we have */
66 RawCachePeers rawCarpPeers
;
67 for (const auto &peer
: CurrentCachePeers()) {
68 const auto p
= peer
.get();
73 assert(p
->type
== PEER_PARENT
);
78 rawCarpPeers
.push_back(p
);
83 if (rawCarpPeers
.empty())
86 /* calculate hashes and load factors */
87 for (const auto p
: rawCarpPeers
) {
88 /* calculate this peers hash */
91 for (t
= p
->name
; *t
!= 0; ++t
)
92 p
->carp
.hash
+= ROTATE_LEFT(p
->carp
.hash
, 19) + (unsigned int) *t
;
94 p
->carp
.hash
+= p
->carp
.hash
* 0x62531965;
96 p
->carp
.hash
= ROTATE_LEFT(p
->carp
.hash
, 21);
99 p
->carp
.load_factor
= ((double) p
->weight
) / (double) W
;
101 if (floor(p
->carp
.load_factor
* 1000.0) == 0.0)
102 p
->carp
.load_factor
= 0.0;
105 /* Sort our list on weight */
106 qsort(rawCarpPeers
.data(), rawCarpPeers
.size(), sizeof(decltype(rawCarpPeers
)::value_type
), peerSortWeight
);
108 /* Calculate the load factor multipliers X_k
110 * X_1 = pow ((K*p_1), (1/K))
111 * X_k = ([K-k+1] * [P_k - P_{k-1}])/(X_1 * X_2 * ... * X_{k-1})
112 * X_k += pow ((X_{k-1}, {K-k+1})
113 * X_k = pow (X_k, {1/(K-k+1)})
114 * simplified to have X_1 part of the loop
116 const auto K
= rawCarpPeers
.size();
118 P_last
= 0.0; /* Empty P_0 */
120 Xn
= 1.0; /* Empty starting point of X_1 * X_2 * ... * X_{x-1} */
122 X_last
= 0.0; /* Empty X_0, nullifies the first pow statement */
124 for (size_t k
= 1; k
<= K
; ++k
) {
125 double Kk1
= (double) (K
- k
+ 1);
126 const auto p
= rawCarpPeers
[k
- 1];
127 p
->carp
.load_multiplier
= (Kk1
* (p
->carp
.load_factor
- P_last
)) / Xn
;
128 p
->carp
.load_multiplier
+= pow(X_last
, Kk1
);
129 p
->carp
.load_multiplier
= pow(p
->carp
.load_multiplier
, 1.0 / Kk1
);
130 Xn
*= p
->carp
.load_multiplier
;
131 X_last
= p
->carp
.load_multiplier
;
132 P_last
= p
->carp
.load_factor
;
135 CarpPeers().assign(rawCarpPeers
.begin(), rawCarpPeers
.end());
138 /// reacts to RegisteredRunner events relevant to this module
139 class CarpRr
: public RegisteredRunner
142 /* RegisteredRunner API */
143 void useConfig() override
{ carpInit(); }
144 void syncConfig() override
{ carpInit(); }
147 DefineRunnerRegistrator(CarpRr
);
150 carpSelectParent(PeerSelector
*ps
)
153 HttpRequest
*request
= ps
->request
;
155 CachePeer
*p
= nullptr;
156 unsigned int user_hash
= 0;
157 unsigned int combined_hash
;
159 double high_score
= 0;
161 if (CarpPeers().empty())
164 /* calculate hash key */
165 debugs(39, 2, "carpSelectParent: Calculating hash for " << request
->effectiveRequestUri());
167 /* select CachePeer */
168 for (const auto &tp
: CarpPeers()) {
170 continue; // peer gone
173 if (tp
->options
.carp_key
.set
) {
174 // this code follows URI syntax pattern.
175 // corner cases should use the full effective request URI
176 if (tp
->options
.carp_key
.scheme
) {
177 key
.append(request
->url
.getScheme().image());
178 if (key
.length()) //if the scheme is not empty
181 if (tp
->options
.carp_key
.host
) {
182 key
.append(request
->url
.host());
184 if (tp
->options
.carp_key
.port
) {
185 key
.appendf(":%hu", request
->url
.port().value_or(0));
187 if (tp
->options
.carp_key
.path
) {
188 // XXX: fix when path and query are separate
189 key
.append(request
->url
.path().substr(0,request
->url
.path().find('?'))); // 0..N
191 if (tp
->options
.carp_key
.params
) {
192 // XXX: fix when path and query are separate
194 if ((pos
=request
->url
.path().find('?')) != SBuf::npos
)
195 key
.append(request
->url
.path().substr(pos
)); // N..npos
198 // if the url-based key is empty, e.g. because the user is
199 // asking to balance on the path but the request doesn't supply any,
200 // then fall back to the effective request URI
203 key
=request
->effectiveRequestUri();
205 for (const char *c
= key
.rawContent(), *e
=key
.rawContent()+key
.length(); c
< e
; ++c
)
206 user_hash
+= ROTATE_LEFT(user_hash
, 19) + *c
;
207 combined_hash
= (user_hash
^ tp
->carp
.hash
);
208 combined_hash
+= combined_hash
* 0x62531965;
209 combined_hash
= ROTATE_LEFT(combined_hash
, 21);
210 score
= combined_hash
* tp
->carp
.load_multiplier
;
211 debugs(39, 3, *tp
<< " key=" << key
<< " combined_hash=" << combined_hash
<<
212 " score=" << std::setprecision(0) << score
);
214 if ((score
> high_score
) && peerHTTPOkay(tp
.get(), ps
)) {
221 debugs(39, 2, "selected " << *p
);
227 carpCachemgr(StoreEntry
* sentry
)
230 storeAppendPrintf(sentry
, "%24s %10s %10s %10s %10s\n",
237 for (const auto &p
: CarpPeers()) {
240 sumfetches
+= p
->stats
.fetches
;
243 for (const auto &p
: CarpPeers()) {
246 storeAppendPrintf(sentry
, "%24s %10x %10f %10f %10f\n",
247 p
->name
, p
->carp
.hash
,
248 p
->carp
.load_multiplier
,
250 sumfetches
? (double) p
->stats
.fetches
/ sumfetches
: -1.0);