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