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 Peer source hash based selection */
12 #include "CachePeer.h"
13 #include "CachePeers.h"
14 #include "HttpRequest.h"
15 #include "mgr/Registration.h"
16 #include "neighbors.h"
17 #include "peer_sourcehash.h"
18 #include "PeerSelectState.h"
19 #include "SquidConfig.h"
24 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
26 /// sourcehash peers ordered by their sourcehash weight
30 static const auto hashPeers
= new SelectedCachePeers();
34 static OBJH peerSourceHashCachemgr
;
35 static void peerSourceHashRegisterWithCacheManager(void);
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 peerSourceHashInit(void)
49 double P_last
, X_last
, Xn
;
53 SourceHashPeers().clear();
54 /* find out which peers we have */
56 RawCachePeers rawSourceHashPeers
;
57 for (const auto &p
: CurrentCachePeers()) {
58 const auto peer
= p
.get();
60 if (!p
->options
.sourcehash
)
63 assert(p
->type
== PEER_PARENT
);
68 rawSourceHashPeers
.push_back(peer
);
73 peerSourceHashRegisterWithCacheManager();
75 if (rawSourceHashPeers
.empty())
78 /* calculate hashes and load factors */
79 for (const auto &p
: rawSourceHashPeers
) {
80 /* calculate this peers hash */
81 p
->sourcehash
.hash
= 0;
83 for (t
= p
->name
; *t
!= 0; ++t
)
84 p
->sourcehash
.hash
+= ROTATE_LEFT(p
->sourcehash
.hash
, 19) + (unsigned int) *t
;
86 p
->sourcehash
.hash
+= p
->sourcehash
.hash
* 0x62531965;
88 p
->sourcehash
.hash
= ROTATE_LEFT(p
->sourcehash
.hash
, 21);
91 p
->sourcehash
.load_factor
= ((double) p
->weight
) / (double) W
;
93 if (floor(p
->sourcehash
.load_factor
* 1000.0) == 0.0)
94 p
->sourcehash
.load_factor
= 0.0;
97 /* Sort our list on weight */
98 qsort(rawSourceHashPeers
.data(), rawSourceHashPeers
.size(), sizeof(decltype(rawSourceHashPeers
)::value_type
), peerSortWeight
);
100 /* Calculate the load factor multipliers X_k
102 * X_1 = pow ((K*p_1), (1/K))
103 * X_k = ([K-k+1] * [P_k - P_{k-1}])/(X_1 * X_2 * ... * X_{k-1})
104 * X_k += pow ((X_{k-1}, {K-k+1})
105 * X_k = pow (X_k, {1/(K-k+1)})
106 * simplified to have X_1 part of the loop
108 const auto K
= rawSourceHashPeers
.size();
110 P_last
= 0.0; /* Empty P_0 */
112 Xn
= 1.0; /* Empty starting point of X_1 * X_2 * ... * X_{x-1} */
114 X_last
= 0.0; /* Empty X_0, nullifies the first pow statement */
116 for (size_t k
= 1; k
<= K
; ++k
) {
117 double Kk1
= (double) (K
- k
+ 1);
118 const auto p
= rawSourceHashPeers
[k
- 1];
119 p
->sourcehash
.load_multiplier
= (Kk1
* (p
->sourcehash
.load_factor
- P_last
)) / Xn
;
120 p
->sourcehash
.load_multiplier
+= pow(X_last
, Kk1
);
121 p
->sourcehash
.load_multiplier
= pow(p
->sourcehash
.load_multiplier
, 1.0 / Kk1
);
122 Xn
*= p
->sourcehash
.load_multiplier
;
123 X_last
= p
->sourcehash
.load_multiplier
;
124 P_last
= p
->sourcehash
.load_factor
;
127 SourceHashPeers().assign(rawSourceHashPeers
.begin(), rawSourceHashPeers
.end());
131 peerSourceHashRegisterWithCacheManager(void)
133 Mgr::RegisterAction("sourcehash", "peer sourcehash information",
134 peerSourceHashCachemgr
, 0, 1);
138 peerSourceHashSelectParent(PeerSelector
*ps
)
141 CachePeer
*p
= nullptr;
142 unsigned int user_hash
= 0;
143 unsigned int combined_hash
;
145 double high_score
= 0;
146 const char *key
= nullptr;
147 char ntoabuf
[MAX_IPSTRLEN
];
149 if (SourceHashPeers().empty())
153 HttpRequest
*request
= ps
->request
;
155 key
= request
->client_addr
.toStr(ntoabuf
, sizeof(ntoabuf
));
157 /* calculate hash key */
158 debugs(39, 2, "peerSourceHashSelectParent: Calculating hash for " << key
);
160 for (c
= key
; *c
!= 0; ++c
)
161 user_hash
+= ROTATE_LEFT(user_hash
, 19) + *c
;
163 /* select CachePeer */
164 for (const auto &tp
: SourceHashPeers()) {
166 continue; // peer gone
168 combined_hash
= (user_hash
^ tp
->sourcehash
.hash
);
169 combined_hash
+= combined_hash
* 0x62531965;
170 combined_hash
= ROTATE_LEFT(combined_hash
, 21);
171 score
= combined_hash
* tp
->sourcehash
.load_multiplier
;
172 debugs(39, 3, *tp
<< " combined_hash " << combined_hash
<<
173 " score " << std::setprecision(0) << score
);
175 if ((score
> high_score
) && peerHTTPOkay(tp
.get(), ps
)) {
182 debugs(39, 2, "selected " << *p
);
188 peerSourceHashCachemgr(StoreEntry
* sentry
)
191 storeAppendPrintf(sentry
, "%24s %10s %10s %10s %10s\n",
198 for (const auto &p
: SourceHashPeers()) {
201 sumfetches
+= p
->stats
.fetches
;
204 for (const auto &p
: SourceHashPeers()) {
207 storeAppendPrintf(sentry
, "%24s %10x %10f %10f %10f\n",
208 p
->name
, p
->sourcehash
.hash
,
209 p
->sourcehash
.load_multiplier
,
210 p
->sourcehash
.load_factor
,
211 sumfetches
? (double) p
->stats
.fetches
/ sumfetches
: -1.0);