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

 

Introduction

Documentation

Support

Miscellaneous