carp.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 Cache Array Routing Protocol */
10
11#include "squid.h"
12#include "CachePeer.h"
13#include "CachePeers.h"
14#include "carp.h"
15#include "HttpRequest.h"
16#include "mgr/Registration.h"
17#include "neighbors.h"
18#include "PeerSelectState.h"
19#include "SquidConfig.h"
20#include "Store.h"
21
22#include <cmath>
23
24#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
25
27static auto &
29{
30 static const auto carpPeers = new SelectedCachePeers();
31 return *carpPeers;
32}
33
35
36static int
37peerSortWeight(const void *a, const void *b)
38{
39 const CachePeer *const *p1 = (const CachePeer *const *)a;
40 const CachePeer *const *p2 = (const CachePeer *const *)b;
41 return (*p1)->weight - (*p2)->weight;
42}
43
44static void
46{
47 Mgr::RegisterAction("carp", "CARP information", carpCachemgr, 0, 1);
48}
49
50void
52{
53 int W = 0;
54 double P_last, X_last, Xn;
55 char *t;
56 /* Clean up */
57
58 CarpPeers().clear();
59
60 /* initialize cache manager before we have a chance to leave the execution path */
62
63 /* find out which peers we have */
64
65 RawCachePeers rawCarpPeers;
66 for (const auto &peer: CurrentCachePeers()) {
67 const auto p = peer.get();
68
69 if (!p->options.carp)
70 continue;
71
72 assert(p->type == PEER_PARENT);
73
74 if (p->weight == 0)
75 continue;
76
77 rawCarpPeers.push_back(p);
78
79 W += p->weight;
80 }
81
82 if (rawCarpPeers.empty())
83 return;
84
85 /* calculate hashes and load factors */
86 for (const auto p: rawCarpPeers) {
87 /* calculate this peers hash */
88 p->carp.hash = 0;
89
90 for (t = p->name; *t != 0; ++t)
91 p->carp.hash += ROTATE_LEFT(p->carp.hash, 19) + (unsigned int) *t;
92
93 p->carp.hash += p->carp.hash * 0x62531965;
94
95 p->carp.hash = ROTATE_LEFT(p->carp.hash, 21);
96
97 /* and load factor */
98 p->carp.load_factor = ((double) p->weight) / (double) W;
99
100 if (floor(p->carp.load_factor * 1000.0) == 0.0)
101 p->carp.load_factor = 0.0;
102 }
103
104 /* Sort our list on weight */
105 qsort(rawCarpPeers.data(), rawCarpPeers.size(), sizeof(decltype(rawCarpPeers)::value_type), peerSortWeight);
106
107 /* Calculate the load factor multipliers X_k
108 *
109 * X_1 = pow ((K*p_1), (1/K))
110 * X_k = ([K-k+1] * [P_k - P_{k-1}])/(X_1 * X_2 * ... * X_{k-1})
111 * X_k += pow ((X_{k-1}, {K-k+1})
112 * X_k = pow (X_k, {1/(K-k+1)})
113 * simplified to have X_1 part of the loop
114 */
115 const auto K = rawCarpPeers.size();
116
117 P_last = 0.0; /* Empty P_0 */
118
119 Xn = 1.0; /* Empty starting point of X_1 * X_2 * ... * X_{x-1} */
120
121 X_last = 0.0; /* Empty X_0, nullifies the first pow statement */
122
123 for (size_t k = 1; k <= K; ++k) {
124 double Kk1 = (double) (K - k + 1);
125 const auto p = rawCarpPeers[k - 1];
126 p->carp.load_multiplier = (Kk1 * (p->carp.load_factor - P_last)) / Xn;
127 p->carp.load_multiplier += pow(X_last, Kk1);
128 p->carp.load_multiplier = pow(p->carp.load_multiplier, 1.0 / Kk1);
129 Xn *= p->carp.load_multiplier;
130 X_last = p->carp.load_multiplier;
131 P_last = p->carp.load_factor;
132 }
133
134 CarpPeers().assign(rawCarpPeers.begin(), rawCarpPeers.end());
135}
136
137CachePeer *
139{
140 assert(ps);
141 HttpRequest *request = ps->request;
142
143 CachePeer *p = nullptr;
144 unsigned int user_hash = 0;
145 unsigned int combined_hash;
146 double score;
147 double high_score = 0;
148
149 if (CarpPeers().empty())
150 return nullptr;
151
152 /* calculate hash key */
153 debugs(39, 2, "carpSelectParent: Calculating hash for " << request->effectiveRequestUri());
154
155 /* select CachePeer */
156 for (const auto &tp: CarpPeers()) {
157 if (!tp)
158 continue; // peer gone
159
160 SBuf key;
161 if (tp->options.carp_key.set) {
162 // this code follows URI syntax pattern.
163 // corner cases should use the full effective request URI
164 if (tp->options.carp_key.scheme) {
165 key.append(request->url.getScheme().image());
166 if (key.length()) //if the scheme is not empty
167 key.append("://");
168 }
169 if (tp->options.carp_key.host) {
170 key.append(request->url.host());
171 }
172 if (tp->options.carp_key.port) {
173 key.appendf(":%hu", request->url.port().value_or(0));
174 }
175 if (tp->options.carp_key.path) {
176 // XXX: fix when path and query are separate
177 key.append(request->url.path().substr(0,request->url.path().find('?'))); // 0..N
178 }
179 if (tp->options.carp_key.params) {
180 // XXX: fix when path and query are separate
181 SBuf::size_type pos;
182 if ((pos=request->url.path().find('?')) != SBuf::npos)
183 key.append(request->url.path().substr(pos)); // N..npos
184 }
185 }
186 // if the url-based key is empty, e.g. because the user is
187 // asking to balance on the path but the request doesn't supply any,
188 // then fall back to the effective request URI
189
190 if (key.isEmpty())
191 key=request->effectiveRequestUri();
192
193 for (const char *c = key.rawContent(), *e=key.rawContent()+key.length(); c < e; ++c)
194 user_hash += ROTATE_LEFT(user_hash, 19) + *c;
195 combined_hash = (user_hash ^ tp->carp.hash);
196 combined_hash += combined_hash * 0x62531965;
197 combined_hash = ROTATE_LEFT(combined_hash, 21);
198 score = combined_hash * tp->carp.load_multiplier;
199 debugs(39, 3, *tp << " key=" << key << " combined_hash=" << combined_hash <<
200 " score=" << std::setprecision(0) << score);
201
202 if ((score > high_score) && peerHTTPOkay(tp.get(), ps)) {
203 p = tp.get();
204 high_score = score;
205 }
206 }
207
208 if (p)
209 debugs(39, 2, "selected " << *p);
210
211 return p;
212}
213
214static void
216{
217 int sumfetches = 0;
218 storeAppendPrintf(sentry, "%24s %10s %10s %10s %10s\n",
219 "Hostname",
220 "Hash",
221 "Multiplier",
222 "Factor",
223 "Actual");
224
225 for (const auto &p: CarpPeers()) {
226 if (!p)
227 continue;
228 sumfetches += p->stats.fetches;
229 }
230
231 for (const auto &p: CarpPeers()) {
232 if (!p)
233 continue;
234 storeAppendPrintf(sentry, "%24s %10x %10f %10f %10f\n",
235 p->name, p->carp.hash,
236 p->carp.load_multiplier,
237 p->carp.load_factor,
238 sumfetches ? (double) p->stats.fetches / sumfetches : -1.0);
239 }
240}
241
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
static void carpRegisterWithCacheManager(void)
Definition: carp.cc:45
static auto & CarpPeers()
CARP cache_peers ordered by their CARP weight.
Definition: carp.cc:28
CachePeer * carpSelectParent(PeerSelector *ps)
Definition: carp.cc:138
static OBJH carpCachemgr
Definition: carp.cc:34
#define ROTATE_LEFT(x, n)
Definition: carp.cc:24
void carpInit(void)
Definition: carp.cc:51
static int peerSortWeight(const void *a, const void *b)
Definition: carp.cc:37
SBuf image() const
Definition: UriScheme.h:57
AnyP::UriScheme const & getScheme() const
Definition: Uri.h:67
void path(const char *p)
Definition: Uri.h:101
void port(const Port p)
reset authority port subcomponent
Definition: Uri.h:95
void host(const char *src)
Definition: Uri.cc:100
int weight
Definition: CachePeer.h:151
AnyP::Uri url
the request URI
Definition: HttpRequest.h:115
const SBuf & effectiveRequestUri() const
RFC 7230 section 5.5 - Effective Request URI.
Definition: HttpRequest.cc:744
HttpRequest * request
Definition: SBuf.h:94
const char * rawContent() const
Definition: SBuf.cc:509
static const size_type npos
Definition: SBuf.h:99
size_type length() const
Returns the number of bytes stored in SBuf.
Definition: SBuf.h:415
SBuf & appendf(const char *fmt,...) PRINTF_FORMAT_ARG2
Definition: SBuf.cc:229
bool isEmpty() const
Definition: SBuf.h:431
SBuf & append(const SBuf &S)
Definition: SBuf.cc:185
MemBlob::size_type size_type
Definition: SBuf.h:96
#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
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