]> git.ipfire.org Git - thirdparty/squid.git/blame - src/peer_userhash.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / peer_userhash.cc
CommitLineData
63104e28
HN
1
2/*
262a0e14 3 * $Id$
63104e28 4 *
eec27647 5 * DEBUG: section 39 Peer user hash based selection
63104e28 6 * AUTHOR: Henrik Nordstrom
eec27647 7 * BASED ON: carp.cc
63104e28
HN
8 *
9 * SQUID Web Proxy Cache http://www.squid-cache.org/
10 * ----------------------------------------------------------
11 *
12 * Squid is the result of efforts by numerous individuals from
13 * the Internet community; see the CONTRIBUTORS file for full
14 * details. Many organizations have provided support for Squid's
15 * development; see the SPONSORS file for full details. Squid is
16 * Copyrighted (C) 2001 by the Regents of the University of
17 * California; see the COPYRIGHT file for full details. Squid
18 * incorporates software developed and/or copyrighted by other
19 * sources; see the CREDITS file for full details.
20 *
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2 of the License, or
24 * (at your option) any later version.
26ac0430 25 *
63104e28
HN
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
26ac0430 30 *
63104e28
HN
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
34 *
35 */
36
582c2af2 37#include "squid.h"
2f1431ea
AJ
38
39#if USE_AUTH
40
582c2af2
FC
41#include "auth/UserRequest.h"
42#include "globals.h"
43#include "HttpRequest.h"
8822ebee 44#include "mgr/Registration.h"
582c2af2 45#include "protos.h"
63104e28 46#include "Store.h"
582c2af2
FC
47
48#if HAVE_MATH_H
49#include <math.h>
50#endif
63104e28
HN
51
52#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
53
f7e1d9ce
HN
54static int n_userhash_peers = 0;
55static peer **userhash_peers = NULL;
56static OBJH peerUserHashCachemgr;
6b661f2e 57static void peerUserHashRegisterWithCacheManager(void);
63104e28
HN
58
59static int
60peerSortWeight(const void *a, const void *b)
61{
62 const peer *const *p1 = (const peer *const *)a;
63 const peer *const *p2 = (const peer *const *)b;
64 return (*p1)->weight - (*p2)->weight;
65}
66
67void
f7e1d9ce 68peerUserHashInit(void)
63104e28
HN
69{
70 int W = 0;
71 int K;
72 int k;
73 double P_last, X_last, Xn;
74 peer *p;
75 peer **P;
76 char *t;
77 /* Clean up */
78
5db6bf73 79 for (k = 0; k < n_userhash_peers; ++k) {
f7e1d9ce 80 cbdataReferenceDone(userhash_peers[k]);
63104e28
HN
81 }
82
f7e1d9ce
HN
83 safe_free(userhash_peers);
84 n_userhash_peers = 0;
63104e28
HN
85 /* find out which peers we have */
86
6b661f2e
FC
87 peerUserHashRegisterWithCacheManager();
88
63104e28 89 for (p = Config.peers; p; p = p->next) {
f7e1d9ce 90 if (!p->options.userhash)
63104e28
HN
91 continue;
92
93 assert(p->type == PEER_PARENT);
94
95 if (p->weight == 0)
96 continue;
97
5db6bf73 98 ++n_userhash_peers;
63104e28
HN
99
100 W += p->weight;
101 }
102
f7e1d9ce 103 if (n_userhash_peers == 0)
63104e28
HN
104 return;
105
f7e1d9ce 106 userhash_peers = (peer **)xcalloc(n_userhash_peers, sizeof(*userhash_peers));
63104e28
HN
107
108 /* Build a list of the found peers and calculate hashes and load factors */
f7e1d9ce
HN
109 for (P = userhash_peers, p = Config.peers; p; p = p->next) {
110 if (!p->options.userhash)
63104e28
HN
111 continue;
112
113 if (p->weight == 0)
114 continue;
115
116 /* calculate this peers hash */
f7e1d9ce 117 p->userhash.hash = 0;
63104e28 118
5db6bf73 119 for (t = p->name; *t != 0; ++t)
f7e1d9ce 120 p->userhash.hash += ROTATE_LEFT(p->userhash.hash, 19) + (unsigned int) *t;
63104e28 121
f7e1d9ce 122 p->userhash.hash += p->userhash.hash * 0x62531965;
63104e28 123
f7e1d9ce 124 p->userhash.hash = ROTATE_LEFT(p->userhash.hash, 21);
63104e28
HN
125
126 /* and load factor */
f7e1d9ce 127 p->userhash.load_factor = ((double) p->weight) / (double) W;
63104e28 128
f7e1d9ce
HN
129 if (floor(p->userhash.load_factor * 1000.0) == 0.0)
130 p->userhash.load_factor = 0.0;
63104e28
HN
131
132 /* add it to our list of peers */
133 *P++ = cbdataReference(p);
134 }
135
136 /* Sort our list on weight */
f7e1d9ce 137 qsort(userhash_peers, n_userhash_peers, sizeof(*userhash_peers), peerSortWeight);
63104e28
HN
138
139 /* Calculate the load factor multipliers X_k
140 *
141 * X_1 = pow ((K*p_1), (1/K))
142 * X_k = ([K-k+1] * [P_k - P_{k-1}])/(X_1 * X_2 * ... * X_{k-1})
143 * X_k += pow ((X_{k-1}, {K-k+1})
144 * X_k = pow (X_k, {1/(K-k+1)})
145 * simplified to have X_1 part of the loop
146 */
f7e1d9ce 147 K = n_userhash_peers;
63104e28
HN
148
149 P_last = 0.0; /* Empty P_0 */
150
151 Xn = 1.0; /* Empty starting point of X_1 * X_2 * ... * X_{x-1} */
152
153 X_last = 0.0; /* Empty X_0, nullifies the first pow statement */
154
5db6bf73 155 for (k = 1; k <= K; ++k) {
63104e28 156 double Kk1 = (double) (K - k + 1);
f7e1d9ce
HN
157 p = userhash_peers[k - 1];
158 p->userhash.load_multiplier = (Kk1 * (p->userhash.load_factor - P_last)) / Xn;
159 p->userhash.load_multiplier += pow(X_last, Kk1);
160 p->userhash.load_multiplier = pow(p->userhash.load_multiplier, 1.0 / Kk1);
161 Xn *= p->userhash.load_multiplier;
162 X_last = p->userhash.load_multiplier;
163 P_last = p->userhash.load_factor;
63104e28
HN
164 }
165}
166
6b661f2e
FC
167static void
168peerUserHashRegisterWithCacheManager(void)
63104e28 169{
8822ebee 170 Mgr::RegisterAction("userhash", "peer userhash information", peerUserHashCachemgr,
d9fc6862 171 0, 1);
63104e28
HN
172}
173
174peer *
f7e1d9ce 175peerUserHashSelectParent(HttpRequest * request)
63104e28
HN
176{
177 int k;
178 const char *c;
179 peer *p = NULL;
180 peer *tp;
181 unsigned int user_hash = 0;
182 unsigned int combined_hash;
183 double score;
184 double high_score = 0;
185 const char *key = NULL;
186
f7e1d9ce 187 if (n_userhash_peers == 0)
63104e28
HN
188 return NULL;
189
a33a428a 190 if (request->auth_user_request != NULL)
26ac0430 191 key = request->auth_user_request->username();
eec27647
HN
192
193 if (!key)
26ac0430 194 return NULL;
63104e28
HN
195
196 /* calculate hash key */
f7e1d9ce 197 debugs(39, 2, "peerUserHashSelectParent: Calculating hash for " << key);
63104e28 198
5db6bf73 199 for (c = key; *c != 0; ++c)
63104e28
HN
200 user_hash += ROTATE_LEFT(user_hash, 19) + *c;
201
202 /* select peer */
5db6bf73 203 for (k = 0; k < n_userhash_peers; ++k) {
f7e1d9ce
HN
204 tp = userhash_peers[k];
205 combined_hash = (user_hash ^ tp->userhash.hash);
63104e28
HN
206 combined_hash += combined_hash * 0x62531965;
207 combined_hash = ROTATE_LEFT(combined_hash, 21);
f7e1d9ce 208 score = combined_hash * tp->userhash.load_multiplier;
26ac0430 209 debugs(39, 3, "peerUserHashSelectParent: " << tp->name << " combined_hash " << combined_hash <<
63104e28
HN
210 " score " << std::setprecision(0) << score);
211
212 if ((score > high_score) && peerHTTPOkay(tp, request)) {
213 p = tp;
214 high_score = score;
215 }
216 }
217
218 if (p)
f7e1d9ce 219 debugs(39, 2, "peerUserHashSelectParent: selected " << p->name);
63104e28
HN
220
221 return p;
222}
223
224static void
f7e1d9ce 225peerUserHashCachemgr(StoreEntry * sentry)
63104e28
HN
226{
227 peer *p;
228 int sumfetches = 0;
229 storeAppendPrintf(sentry, "%24s %10s %10s %10s %10s\n",
230 "Hostname",
231 "Hash",
232 "Multiplier",
233 "Factor",
234 "Actual");
235
236 for (p = Config.peers; p; p = p->next)
237 sumfetches += p->stats.fetches;
238
239 for (p = Config.peers; p; p = p->next) {
240 storeAppendPrintf(sentry, "%24s %10x %10f %10f %10f\n",
f7e1d9ce
HN
241 p->name, p->userhash.hash,
242 p->userhash.load_multiplier,
243 p->userhash.load_factor,
63104e28
HN
244 sumfetches ? (double) p->stats.fetches / sumfetches : -1.0);
245 }
246}
2f1431ea
AJ
247
248#endif /* USE_AUTH */