peer_userhash.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
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.
7 */
8
9/* DEBUG: section 39 Peer user hash based selection */
10
11#include "squid.h"
12
13#if USE_AUTH
14
15#include "auth/UserRequest.h"
16#include "CachePeer.h"
17#include "CachePeers.h"
18#include "globals.h"
19#include "HttpRequest.h"
20#include "mgr/Registration.h"
21#include "neighbors.h"
22#include "peer_userhash.h"
23#include "PeerSelectState.h"
24#include "SquidConfig.h"
25#include "Store.h"
26
27#include <cmath>
28
29#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
30
32static auto &
34{
35 static const auto hashPeers = new SelectedCachePeers();
36 return *hashPeers;
37}
38
41
42static int
43peerSortWeight(const void *a, const void *b)
44{
45 const CachePeer *const *p1 = (const CachePeer *const *)a;
46 const CachePeer *const *p2 = (const CachePeer *const *)b;
47 return (*p1)->weight - (*p2)->weight;
48}
49
50void
52{
53 int W = 0;
54 double P_last, X_last, Xn;
55 char *t;
56 /* Clean up */
57
58 UserHashPeers().clear();
59 /* find out which peers we have */
60
62
63 RawCachePeers rawUserHashPeers;
64 for (const auto &p: CurrentCachePeers()) {
65 const auto peer = p.get();
66
67 if (!p->options.userhash)
68 continue;
69
70 assert(p->type == PEER_PARENT);
71
72 if (p->weight == 0)
73 continue;
74
75 rawUserHashPeers.push_back(peer);
76
77 W += p->weight;
78 }
79
80 if (rawUserHashPeers.empty())
81 return;
82
83 /* calculate hashes and load factors */
84 for (const auto &p: rawUserHashPeers) {
85 /* calculate this peers hash */
86 p->userhash.hash = 0;
87
88 for (t = p->name; *t != 0; ++t)
89 p->userhash.hash += ROTATE_LEFT(p->userhash.hash, 19) + (unsigned int) *t;
90
91 p->userhash.hash += p->userhash.hash * 0x62531965;
92
93 p->userhash.hash = ROTATE_LEFT(p->userhash.hash, 21);
94
95 /* and load factor */
96 p->userhash.load_factor = ((double) p->weight) / (double) W;
97
98 if (floor(p->userhash.load_factor * 1000.0) == 0.0)
99 p->userhash.load_factor = 0.0;
100 }
101
102 /* Sort our list on weight */
103 qsort(rawUserHashPeers.data(), rawUserHashPeers.size(), sizeof(decltype(rawUserHashPeers)::value_type), peerSortWeight);
104
105 /* Calculate the load factor multipliers X_k
106 *
107 * X_1 = pow ((K*p_1), (1/K))
108 * X_k = ([K-k+1] * [P_k - P_{k-1}])/(X_1 * X_2 * ... * X_{k-1})
109 * X_k += pow ((X_{k-1}, {K-k+1})
110 * X_k = pow (X_k, {1/(K-k+1)})
111 * simplified to have X_1 part of the loop
112 */
113 const auto K = rawUserHashPeers.size();
114
115 P_last = 0.0; /* Empty P_0 */
116
117 Xn = 1.0; /* Empty starting point of X_1 * X_2 * ... * X_{x-1} */
118
119 X_last = 0.0; /* Empty X_0, nullifies the first pow statement */
120
121 for (size_t k = 1; k <= K; ++k) {
122 double Kk1 = (double) (K - k + 1);
123 const auto p = rawUserHashPeers[k - 1];
124 p->userhash.load_multiplier = (Kk1 * (p->userhash.load_factor - P_last)) / Xn;
125 p->userhash.load_multiplier += pow(X_last, Kk1);
126 p->userhash.load_multiplier = pow(p->userhash.load_multiplier, 1.0 / Kk1);
127 Xn *= p->userhash.load_multiplier;
128 X_last = p->userhash.load_multiplier;
129 P_last = p->userhash.load_factor;
130 }
131
132 UserHashPeers().assign(rawUserHashPeers.begin(), rawUserHashPeers.end());
133}
134
135static void
137{
138 Mgr::RegisterAction("userhash", "peer userhash information", peerUserHashCachemgr,
139 0, 1);
140}
141
142CachePeer *
144{
145 const char *c;
146 CachePeer *p = nullptr;
147 unsigned int user_hash = 0;
148 unsigned int combined_hash;
149 double score;
150 double high_score = 0;
151 const char *key = nullptr;
152
153 if (UserHashPeers().empty())
154 return nullptr;
155
156 assert(ps);
157 HttpRequest *request = ps->request;
158
159 if (request->auth_user_request != nullptr)
160 key = request->auth_user_request->username();
161
162 if (!key)
163 return nullptr;
164
165 /* calculate hash key */
166 debugs(39, 2, "peerUserHashSelectParent: Calculating hash for " << key);
167
168 for (c = key; *c != 0; ++c)
169 user_hash += ROTATE_LEFT(user_hash, 19) + *c;
170
171 /* select CachePeer */
172 for (const auto &tp: UserHashPeers()) {
173 if (!tp)
174 continue; // peer gone
175
176 combined_hash = (user_hash ^ tp->userhash.hash);
177 combined_hash += combined_hash * 0x62531965;
178 combined_hash = ROTATE_LEFT(combined_hash, 21);
179 score = combined_hash * tp->userhash.load_multiplier;
180 debugs(39, 3, *tp << " combined_hash " << combined_hash <<
181 " score " << std::setprecision(0) << score);
182
183 if ((score > high_score) && peerHTTPOkay(tp.get(), ps)) {
184 p = tp.get();
185 high_score = score;
186 }
187 }
188
189 if (p)
190 debugs(39, 2, "selected " << *p);
191
192 return p;
193}
194
195static void
197{
198 int sumfetches = 0;
199 storeAppendPrintf(sentry, "%24s %10s %10s %10s %10s\n",
200 "Hostname",
201 "Hash",
202 "Multiplier",
203 "Factor",
204 "Actual");
205
206 for (const auto &p: UserHashPeers()) {
207 if (!p)
208 continue;
209 sumfetches += p->stats.fetches;
210 }
211
212 for (const auto &p: UserHashPeers()) {
213 if (!p)
214 continue;
215 storeAppendPrintf(sentry, "%24s %10x %10f %10f %10f\n",
216 p->name, p->userhash.hash,
217 p->userhash.load_multiplier,
218 p->userhash.load_factor,
219 sumfetches ? (double) p->stats.fetches / sumfetches : -1.0);
220 }
221}
222
223#endif /* USE_AUTH */
224
const CachePeers & CurrentCachePeers()
Definition: CachePeers.cc:41
std::vector< CachePeer *, PoolingAllocator< CachePeer * > > RawCachePeers
Temporary, local storage of raw pointers to zero or more Config.peers.
Definition: CachePeers.h:66
std::vector< CbcPointer< CachePeer >, PoolingAllocator< CbcPointer< CachePeer > > > SelectedCachePeers
Definition: CachePeers.h:63
#define assert(EX)
Definition: assert.h:17
char const * username() const
Definition: UserRequest.cc:32
int weight
Definition: CachePeer.h:151
Auth::UserRequest::Pointer auth_user_request
Definition: HttpRequest.h:127
HttpRequest * request
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:194
@ PEER_PARENT
Definition: enums.h:30
void OBJH(StoreEntry *)
Definition: forward.h:44
void RegisterAction(char const *action, char const *desc, OBJH *handler, int pw_req_flag, int atomic)
Definition: Registration.cc:16
int peerHTTPOkay(const CachePeer *p, PeerSelector *ps)
Definition: neighbors.cc:257
#define ROTATE_LEFT(x, n)
static void peerUserHashRegisterWithCacheManager(void)
static int peerSortWeight(const void *a, const void *b)
static auto & UserHashPeers()
userhash peers ordered by their userhash weight
void peerUserHashInit(void)
static OBJH peerUserHashCachemgr
CachePeer * peerUserHashSelectParent(PeerSelector *ps)
void storeAppendPrintf(StoreEntry *e, const char *fmt,...)
Definition: store.cc:841
void EVH void double
Definition: stub_event.cc:16
int unsigned int
Definition: stub_fd.cc:19

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors