]> git.ipfire.org Git - thirdparty/squid.git/blob - src/peer_userhash.cc
Merged Postfix-Prefix branch: refactor inc/decrement operators from postfix to prefix...
[thirdparty/squid.git] / src / peer_userhash.cc
1
2 /*
3 * $Id$
4 *
5 * DEBUG: section 39 Peer user hash based selection
6 * AUTHOR: Henrik Nordstrom
7 * BASED ON: carp.cc
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.
25 *
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.
30 *
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
37 #include "squid-old.h"
38
39 #if USE_AUTH
40
41 #include "mgr/Registration.h"
42 #include "Store.h"
43 #include "HttpRequest.h"
44 #include "auth/UserRequest.h"
45
46 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
47
48 static int n_userhash_peers = 0;
49 static peer **userhash_peers = NULL;
50 static OBJH peerUserHashCachemgr;
51 static void peerUserHashRegisterWithCacheManager(void);
52
53 static int
54 peerSortWeight(const void *a, const void *b)
55 {
56 const peer *const *p1 = (const peer *const *)a;
57 const peer *const *p2 = (const peer *const *)b;
58 return (*p1)->weight - (*p2)->weight;
59 }
60
61 void
62 peerUserHashInit(void)
63 {
64 int W = 0;
65 int K;
66 int k;
67 double P_last, X_last, Xn;
68 peer *p;
69 peer **P;
70 char *t;
71 /* Clean up */
72
73 for (k = 0; k < n_userhash_peers; ++k) {
74 cbdataReferenceDone(userhash_peers[k]);
75 }
76
77 safe_free(userhash_peers);
78 n_userhash_peers = 0;
79 /* find out which peers we have */
80
81
82 peerUserHashRegisterWithCacheManager();
83
84 for (p = Config.peers; p; p = p->next) {
85 if (!p->options.userhash)
86 continue;
87
88 assert(p->type == PEER_PARENT);
89
90 if (p->weight == 0)
91 continue;
92
93 ++n_userhash_peers;
94
95 W += p->weight;
96 }
97
98 if (n_userhash_peers == 0)
99 return;
100
101 userhash_peers = (peer **)xcalloc(n_userhash_peers, sizeof(*userhash_peers));
102
103 /* Build a list of the found peers and calculate hashes and load factors */
104 for (P = userhash_peers, p = Config.peers; p; p = p->next) {
105 if (!p->options.userhash)
106 continue;
107
108 if (p->weight == 0)
109 continue;
110
111 /* calculate this peers hash */
112 p->userhash.hash = 0;
113
114 for (t = p->name; *t != 0; ++t)
115 p->userhash.hash += ROTATE_LEFT(p->userhash.hash, 19) + (unsigned int) *t;
116
117 p->userhash.hash += p->userhash.hash * 0x62531965;
118
119 p->userhash.hash = ROTATE_LEFT(p->userhash.hash, 21);
120
121 /* and load factor */
122 p->userhash.load_factor = ((double) p->weight) / (double) W;
123
124 if (floor(p->userhash.load_factor * 1000.0) == 0.0)
125 p->userhash.load_factor = 0.0;
126
127 /* add it to our list of peers */
128 *P++ = cbdataReference(p);
129 }
130
131 /* Sort our list on weight */
132 qsort(userhash_peers, n_userhash_peers, sizeof(*userhash_peers), peerSortWeight);
133
134 /* Calculate the load factor multipliers X_k
135 *
136 * X_1 = pow ((K*p_1), (1/K))
137 * X_k = ([K-k+1] * [P_k - P_{k-1}])/(X_1 * X_2 * ... * X_{k-1})
138 * X_k += pow ((X_{k-1}, {K-k+1})
139 * X_k = pow (X_k, {1/(K-k+1)})
140 * simplified to have X_1 part of the loop
141 */
142 K = n_userhash_peers;
143
144 P_last = 0.0; /* Empty P_0 */
145
146 Xn = 1.0; /* Empty starting point of X_1 * X_2 * ... * X_{x-1} */
147
148 X_last = 0.0; /* Empty X_0, nullifies the first pow statement */
149
150 for (k = 1; k <= K; ++k) {
151 double Kk1 = (double) (K - k + 1);
152 p = userhash_peers[k - 1];
153 p->userhash.load_multiplier = (Kk1 * (p->userhash.load_factor - P_last)) / Xn;
154 p->userhash.load_multiplier += pow(X_last, Kk1);
155 p->userhash.load_multiplier = pow(p->userhash.load_multiplier, 1.0 / Kk1);
156 Xn *= p->userhash.load_multiplier;
157 X_last = p->userhash.load_multiplier;
158 P_last = p->userhash.load_factor;
159 }
160 }
161
162 static void
163 peerUserHashRegisterWithCacheManager(void)
164 {
165 Mgr::RegisterAction("userhash", "peer userhash information", peerUserHashCachemgr,
166 0, 1);
167 }
168
169 peer *
170 peerUserHashSelectParent(HttpRequest * request)
171 {
172 int k;
173 const char *c;
174 peer *p = NULL;
175 peer *tp;
176 unsigned int user_hash = 0;
177 unsigned int combined_hash;
178 double score;
179 double high_score = 0;
180 const char *key = NULL;
181
182 if (n_userhash_peers == 0)
183 return NULL;
184
185 if (request->auth_user_request != NULL)
186 key = request->auth_user_request->username();
187
188 if (!key)
189 return NULL;
190
191 /* calculate hash key */
192 debugs(39, 2, "peerUserHashSelectParent: Calculating hash for " << key);
193
194 for (c = key; *c != 0; ++c)
195 user_hash += ROTATE_LEFT(user_hash, 19) + *c;
196
197 /* select peer */
198 for (k = 0; k < n_userhash_peers; ++k) {
199 tp = userhash_peers[k];
200 combined_hash = (user_hash ^ tp->userhash.hash);
201 combined_hash += combined_hash * 0x62531965;
202 combined_hash = ROTATE_LEFT(combined_hash, 21);
203 score = combined_hash * tp->userhash.load_multiplier;
204 debugs(39, 3, "peerUserHashSelectParent: " << tp->name << " combined_hash " << combined_hash <<
205 " score " << std::setprecision(0) << score);
206
207 if ((score > high_score) && peerHTTPOkay(tp, request)) {
208 p = tp;
209 high_score = score;
210 }
211 }
212
213 if (p)
214 debugs(39, 2, "peerUserHashSelectParent: selected " << p->name);
215
216 return p;
217 }
218
219 static void
220 peerUserHashCachemgr(StoreEntry * sentry)
221 {
222 peer *p;
223 int sumfetches = 0;
224 storeAppendPrintf(sentry, "%24s %10s %10s %10s %10s\n",
225 "Hostname",
226 "Hash",
227 "Multiplier",
228 "Factor",
229 "Actual");
230
231 for (p = Config.peers; p; p = p->next)
232 sumfetches += p->stats.fetches;
233
234 for (p = Config.peers; p; p = p->next) {
235 storeAppendPrintf(sentry, "%24s %10x %10f %10f %10f\n",
236 p->name, p->userhash.hash,
237 p->userhash.load_multiplier,
238 p->userhash.load_factor,
239 sumfetches ? (double) p->stats.fetches / sumfetches : -1.0);
240 }
241 }
242
243 #endif /* USE_AUTH */