2 * Copyright (C) 1996-2023 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 "CachePeer.h"
13 #include "CachePeers.h"
15 #include "HttpRequest.h"
16 #include "mgr/Registration.h"
17 #include "neighbors.h"
18 #include "PeerSelectState.h"
19 #include "SquidConfig.h"
24 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
26 /// CARP cache_peers ordered by their CARP weight
27 static SelectedCachePeers TheCarpPeers
;
29 static OBJH carpCachemgr
;
32 peerSortWeight(const void *a
, const void *b
)
34 const CachePeer
*const *p1
= (const CachePeer
*const *)a
;
35 const CachePeer
*const *p2
= (const CachePeer
*const *)b
;
36 return (*p1
)->weight
- (*p2
)->weight
;
40 carpRegisterWithCacheManager(void)
42 Mgr::RegisterAction("carp", "CARP information", carpCachemgr
, 0, 1);
49 double P_last
, X_last
, Xn
;
55 /* initialize cache manager before we have a chance to leave the execution path */
56 carpRegisterWithCacheManager();
58 /* find out which peers we have */
60 RawCachePeers rawCarpPeers
;
61 for (const auto &peer
: CurrentCachePeers()) {
62 const auto p
= peer
.get();
67 assert(p
->type
== PEER_PARENT
);
72 rawCarpPeers
.push_back(p
);
77 if (rawCarpPeers
.empty())
80 /* calculate hashes and load factors */
81 for (const auto p
: rawCarpPeers
) {
82 /* calculate this peers hash */
85 for (t
= p
->name
; *t
!= 0; ++t
)
86 p
->carp
.hash
+= ROTATE_LEFT(p
->carp
.hash
, 19) + (unsigned int) *t
;
88 p
->carp
.hash
+= p
->carp
.hash
* 0x62531965;
90 p
->carp
.hash
= ROTATE_LEFT(p
->carp
.hash
, 21);
93 p
->carp
.load_factor
= ((double) p
->weight
) / (double) W
;
95 if (floor(p
->carp
.load_factor
* 1000.0) == 0.0)
96 p
->carp
.load_factor
= 0.0;
99 /* Sort our list on weight */
100 qsort(rawCarpPeers
.data(), rawCarpPeers
.size(), sizeof(decltype(rawCarpPeers
)::value_type
), peerSortWeight
);
102 /* Calculate the load factor multipliers X_k
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
110 const auto K
= rawCarpPeers
.size();
112 P_last
= 0.0; /* Empty P_0 */
114 Xn
= 1.0; /* Empty starting point of X_1 * X_2 * ... * X_{x-1} */
116 X_last
= 0.0; /* Empty X_0, nullifies the first pow statement */
118 for (size_t k
= 1; k
<= K
; ++k
) {
119 double Kk1
= (double) (K
- k
+ 1);
120 const auto p
= rawCarpPeers
[k
- 1];
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
;
129 TheCarpPeers
.assign(rawCarpPeers
.begin(), rawCarpPeers
.end());
133 carpSelectParent(PeerSelector
*ps
)
136 HttpRequest
*request
= ps
->request
;
138 CachePeer
*p
= nullptr;
139 unsigned int user_hash
= 0;
140 unsigned int combined_hash
;
142 double high_score
= 0;
144 if (TheCarpPeers
.empty())
147 /* calculate hash key */
148 debugs(39, 2, "carpSelectParent: Calculating hash for " << request
->effectiveRequestUri());
150 /* select CachePeer */
151 for (const auto &tp
: TheCarpPeers
) {
153 continue; // peer gone
156 if (tp
->options
.carp_key
.set
) {
157 // this code follows URI syntax pattern.
158 // corner cases should use the full effective request URI
159 if (tp
->options
.carp_key
.scheme
) {
160 key
.append(request
->url
.getScheme().image());
161 if (key
.length()) //if the scheme is not empty
164 if (tp
->options
.carp_key
.host
) {
165 key
.append(request
->url
.host());
167 if (tp
->options
.carp_key
.port
) {
168 key
.appendf(":%hu", request
->url
.port().value_or(0));
170 if (tp
->options
.carp_key
.path
) {
171 // XXX: fix when path and query are separate
172 key
.append(request
->url
.path().substr(0,request
->url
.path().find('?'))); // 0..N
174 if (tp
->options
.carp_key
.params
) {
175 // XXX: fix when path and query are separate
177 if ((pos
=request
->url
.path().find('?')) != SBuf::npos
)
178 key
.append(request
->url
.path().substr(pos
)); // N..npos
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,
183 // then fall back to the effective request URI
186 key
=request
->effectiveRequestUri();
188 for (const char *c
= key
.rawContent(), *e
=key
.rawContent()+key
.length(); c
< e
; ++c
)
189 user_hash
+= ROTATE_LEFT(user_hash
, 19) + *c
;
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
;
194 debugs(39, 3, *tp
<< " key=" << key
<< " combined_hash=" << combined_hash
<<
195 " score=" << std::setprecision(0) << score
);
197 if ((score
> high_score
) && peerHTTPOkay(tp
.get(), ps
)) {
204 debugs(39, 2, "selected " << *p
);
210 carpCachemgr(StoreEntry
* sentry
)
213 storeAppendPrintf(sentry
, "%24s %10s %10s %10s %10s\n",
220 for (const auto &p
: TheCarpPeers
) {
223 sumfetches
+= p
->stats
.fetches
;
226 for (const auto &p
: TheCarpPeers
) {
229 storeAppendPrintf(sentry
, "%24s %10x %10f %10f %10f\n",
230 p
->name
, p
->carp
.hash
,
231 p
->carp
.load_multiplier
,
233 sumfetches
? (double) p
->stats
.fetches
/ sumfetches
: -1.0);