]>
Commit | Line | Data |
---|---|---|
63104e28 | 1 | /* |
bf95c10a | 2 | * Copyright (C) 1996-2022 The Squid Software Foundation and contributors |
63104e28 | 3 | * |
bbc27441 AJ |
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. | |
63104e28 HN |
7 | */ |
8 | ||
bbc27441 AJ |
9 | /* DEBUG: section 39 Peer source hash based selection */ |
10 | ||
582c2af2 | 11 | #include "squid.h" |
a011edee | 12 | #include "CachePeer.h" |
f4a21650 | 13 | #include "HttpRequest.h" |
8822ebee | 14 | #include "mgr/Registration.h" |
f0ba2534 | 15 | #include "neighbors.h" |
8b082ed9 | 16 | #include "peer_sourcehash.h" |
cb365059 | 17 | #include "PeerSelectState.h" |
4d5904f7 | 18 | #include "SquidConfig.h" |
582c2af2 FC |
19 | #include "Store.h" |
20 | ||
074d6a40 | 21 | #include <cmath> |
63104e28 HN |
22 | |
23 | #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) | |
24 | ||
f7e1d9ce | 25 | static int n_sourcehash_peers = 0; |
aee3523a | 26 | static CachePeer **sourcehash_peers = nullptr; |
f7e1d9ce | 27 | static OBJH peerSourceHashCachemgr; |
6b661f2e | 28 | static void peerSourceHashRegisterWithCacheManager(void); |
63104e28 HN |
29 | |
30 | static int | |
31 | peerSortWeight(const void *a, const void *b) | |
32 | { | |
a3c6762c FC |
33 | const CachePeer *const *p1 = (const CachePeer *const *)a; |
34 | const CachePeer *const *p2 = (const CachePeer *const *)b; | |
63104e28 HN |
35 | return (*p1)->weight - (*p2)->weight; |
36 | } | |
37 | ||
38 | void | |
f7e1d9ce | 39 | peerSourceHashInit(void) |
63104e28 HN |
40 | { |
41 | int W = 0; | |
42 | int K; | |
43 | int k; | |
44 | double P_last, X_last, Xn; | |
a3c6762c FC |
45 | CachePeer *p; |
46 | CachePeer **P; | |
63104e28 HN |
47 | char *t; |
48 | /* Clean up */ | |
49 | ||
5db6bf73 | 50 | for (k = 0; k < n_sourcehash_peers; ++k) { |
f7e1d9ce | 51 | cbdataReferenceDone(sourcehash_peers[k]); |
63104e28 HN |
52 | } |
53 | ||
f7e1d9ce HN |
54 | safe_free(sourcehash_peers); |
55 | n_sourcehash_peers = 0; | |
63104e28 HN |
56 | /* find out which peers we have */ |
57 | ||
58 | for (p = Config.peers; p; p = p->next) { | |
f7e1d9ce | 59 | if (!p->options.sourcehash) |
63104e28 HN |
60 | continue; |
61 | ||
62 | assert(p->type == PEER_PARENT); | |
63 | ||
64 | if (p->weight == 0) | |
65 | continue; | |
66 | ||
5db6bf73 | 67 | ++n_sourcehash_peers; |
63104e28 HN |
68 | |
69 | W += p->weight; | |
70 | } | |
71 | ||
6b661f2e FC |
72 | peerSourceHashRegisterWithCacheManager(); |
73 | ||
f7e1d9ce | 74 | if (n_sourcehash_peers == 0) |
63104e28 HN |
75 | return; |
76 | ||
a3c6762c | 77 | sourcehash_peers = (CachePeer **)xcalloc(n_sourcehash_peers, sizeof(*sourcehash_peers)); |
63104e28 HN |
78 | |
79 | /* Build a list of the found peers and calculate hashes and load factors */ | |
f7e1d9ce HN |
80 | for (P = sourcehash_peers, p = Config.peers; p; p = p->next) { |
81 | if (!p->options.sourcehash) | |
63104e28 HN |
82 | continue; |
83 | ||
84 | if (p->weight == 0) | |
85 | continue; | |
86 | ||
87 | /* calculate this peers hash */ | |
f7e1d9ce | 88 | p->sourcehash.hash = 0; |
63104e28 | 89 | |
5db6bf73 | 90 | for (t = p->name; *t != 0; ++t) |
f7e1d9ce | 91 | p->sourcehash.hash += ROTATE_LEFT(p->sourcehash.hash, 19) + (unsigned int) *t; |
63104e28 | 92 | |
f7e1d9ce | 93 | p->sourcehash.hash += p->sourcehash.hash * 0x62531965; |
63104e28 | 94 | |
f7e1d9ce | 95 | p->sourcehash.hash = ROTATE_LEFT(p->sourcehash.hash, 21); |
63104e28 HN |
96 | |
97 | /* and load factor */ | |
f7e1d9ce | 98 | p->sourcehash.load_factor = ((double) p->weight) / (double) W; |
63104e28 | 99 | |
f7e1d9ce HN |
100 | if (floor(p->sourcehash.load_factor * 1000.0) == 0.0) |
101 | p->sourcehash.load_factor = 0.0; | |
63104e28 HN |
102 | |
103 | /* add it to our list of peers */ | |
104 | *P++ = cbdataReference(p); | |
105 | } | |
106 | ||
107 | /* Sort our list on weight */ | |
f7e1d9ce | 108 | qsort(sourcehash_peers, n_sourcehash_peers, sizeof(*sourcehash_peers), peerSortWeight); |
63104e28 HN |
109 | |
110 | /* Calculate the load factor multipliers X_k | |
111 | * | |
112 | * X_1 = pow ((K*p_1), (1/K)) | |
113 | * X_k = ([K-k+1] * [P_k - P_{k-1}])/(X_1 * X_2 * ... * X_{k-1}) | |
114 | * X_k += pow ((X_{k-1}, {K-k+1}) | |
115 | * X_k = pow (X_k, {1/(K-k+1)}) | |
116 | * simplified to have X_1 part of the loop | |
117 | */ | |
f7e1d9ce | 118 | K = n_sourcehash_peers; |
63104e28 | 119 | |
f53969cc | 120 | P_last = 0.0; /* Empty P_0 */ |
63104e28 | 121 | |
f53969cc | 122 | Xn = 1.0; /* Empty starting point of X_1 * X_2 * ... * X_{x-1} */ |
63104e28 | 123 | |
f53969cc | 124 | X_last = 0.0; /* Empty X_0, nullifies the first pow statement */ |
63104e28 | 125 | |
5db6bf73 | 126 | for (k = 1; k <= K; ++k) { |
63104e28 | 127 | double Kk1 = (double) (K - k + 1); |
f7e1d9ce HN |
128 | p = sourcehash_peers[k - 1]; |
129 | p->sourcehash.load_multiplier = (Kk1 * (p->sourcehash.load_factor - P_last)) / Xn; | |
130 | p->sourcehash.load_multiplier += pow(X_last, Kk1); | |
131 | p->sourcehash.load_multiplier = pow(p->sourcehash.load_multiplier, 1.0 / Kk1); | |
132 | Xn *= p->sourcehash.load_multiplier; | |
133 | X_last = p->sourcehash.load_multiplier; | |
134 | P_last = p->sourcehash.load_factor; | |
63104e28 HN |
135 | } |
136 | } | |
137 | ||
6b661f2e FC |
138 | static void |
139 | peerSourceHashRegisterWithCacheManager(void) | |
63104e28 | 140 | { |
8822ebee | 141 | Mgr::RegisterAction("sourcehash", "peer sourcehash information", |
d9fc6862 | 142 | peerSourceHashCachemgr, 0, 1); |
63104e28 HN |
143 | } |
144 | ||
a3c6762c | 145 | CachePeer * |
cb365059 | 146 | peerSourceHashSelectParent(PeerSelector *ps) |
63104e28 HN |
147 | { |
148 | int k; | |
149 | const char *c; | |
aee3523a | 150 | CachePeer *p = nullptr; |
a3c6762c | 151 | CachePeer *tp; |
63104e28 HN |
152 | unsigned int user_hash = 0; |
153 | unsigned int combined_hash; | |
154 | double score; | |
155 | double high_score = 0; | |
aee3523a | 156 | const char *key = nullptr; |
eec27647 | 157 | char ntoabuf[MAX_IPSTRLEN]; |
63104e28 | 158 | |
f7e1d9ce | 159 | if (n_sourcehash_peers == 0) |
aee3523a | 160 | return nullptr; |
63104e28 | 161 | |
cb365059 EB |
162 | assert(ps); |
163 | HttpRequest *request = ps->request; | |
164 | ||
4dd643d5 | 165 | key = request->client_addr.toStr(ntoabuf, sizeof(ntoabuf)); |
63104e28 HN |
166 | |
167 | /* calculate hash key */ | |
f7e1d9ce | 168 | debugs(39, 2, "peerSourceHashSelectParent: Calculating hash for " << key); |
63104e28 | 169 | |
5db6bf73 | 170 | for (c = key; *c != 0; ++c) |
63104e28 HN |
171 | user_hash += ROTATE_LEFT(user_hash, 19) + *c; |
172 | ||
a3c6762c | 173 | /* select CachePeer */ |
5db6bf73 | 174 | for (k = 0; k < n_sourcehash_peers; ++k) { |
f7e1d9ce HN |
175 | tp = sourcehash_peers[k]; |
176 | combined_hash = (user_hash ^ tp->sourcehash.hash); | |
63104e28 HN |
177 | combined_hash += combined_hash * 0x62531965; |
178 | combined_hash = ROTATE_LEFT(combined_hash, 21); | |
f7e1d9ce | 179 | score = combined_hash * tp->sourcehash.load_multiplier; |
26ac0430 | 180 | debugs(39, 3, "peerSourceHashSelectParent: " << tp->name << " combined_hash " << combined_hash << |
63104e28 HN |
181 | " score " << std::setprecision(0) << score); |
182 | ||
cb365059 | 183 | if ((score > high_score) && peerHTTPOkay(tp, ps)) { |
63104e28 HN |
184 | p = tp; |
185 | high_score = score; | |
186 | } | |
187 | } | |
188 | ||
189 | if (p) | |
f7e1d9ce | 190 | debugs(39, 2, "peerSourceHashSelectParent: selected " << p->name); |
63104e28 HN |
191 | |
192 | return p; | |
193 | } | |
194 | ||
195 | static void | |
f7e1d9ce | 196 | peerSourceHashCachemgr(StoreEntry * sentry) |
63104e28 | 197 | { |
a3c6762c | 198 | CachePeer *p; |
63104e28 HN |
199 | int sumfetches = 0; |
200 | storeAppendPrintf(sentry, "%24s %10s %10s %10s %10s\n", | |
201 | "Hostname", | |
202 | "Hash", | |
203 | "Multiplier", | |
204 | "Factor", | |
205 | "Actual"); | |
206 | ||
207 | for (p = Config.peers; p; p = p->next) | |
208 | sumfetches += p->stats.fetches; | |
209 | ||
210 | for (p = Config.peers; p; p = p->next) { | |
211 | storeAppendPrintf(sentry, "%24s %10x %10f %10f %10f\n", | |
f7e1d9ce HN |
212 | p->name, p->sourcehash.hash, |
213 | p->sourcehash.load_multiplier, | |
214 | p->sourcehash.load_factor, | |
63104e28 HN |
215 | sumfetches ? (double) p->stats.fetches / sumfetches : -1.0); |
216 | } | |
217 | } | |
f53969cc | 218 |