]>
Commit | Line | Data |
---|---|---|
63104e28 HN |
1 | |
2 | /* | |
eec27647 | 3 | * DEBUG: section 39 Peer user hash based selection |
63104e28 | 4 | * AUTHOR: Henrik Nordstrom |
eec27647 | 5 | * BASED ON: carp.cc |
63104e28 HN |
6 | * |
7 | * SQUID Web Proxy Cache http://www.squid-cache.org/ | |
8 | * ---------------------------------------------------------- | |
9 | * | |
10 | * Squid is the result of efforts by numerous individuals from | |
11 | * the Internet community; see the CONTRIBUTORS file for full | |
12 | * details. Many organizations have provided support for Squid's | |
13 | * development; see the SPONSORS file for full details. Squid is | |
14 | * Copyrighted (C) 2001 by the Regents of the University of | |
15 | * California; see the COPYRIGHT file for full details. Squid | |
16 | * incorporates software developed and/or copyrighted by other | |
17 | * sources; see the CREDITS file for full details. | |
18 | * | |
19 | * This program is free software; you can redistribute it and/or modify | |
20 | * it under the terms of the GNU General Public License as published by | |
21 | * the Free Software Foundation; either version 2 of the License, or | |
22 | * (at your option) any later version. | |
26ac0430 | 23 | * |
63104e28 HN |
24 | * This program is distributed in the hope that it will be useful, |
25 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
27 | * GNU General Public License for more details. | |
26ac0430 | 28 | * |
63104e28 HN |
29 | * You should have received a copy of the GNU General Public License |
30 | * along with this program; if not, write to the Free Software | |
31 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |
32 | * | |
33 | */ | |
34 | ||
582c2af2 | 35 | #include "squid.h" |
2f1431ea AJ |
36 | |
37 | #if USE_AUTH | |
38 | ||
582c2af2 | 39 | #include "auth/UserRequest.h" |
a011edee | 40 | #include "CachePeer.h" |
582c2af2 FC |
41 | #include "globals.h" |
42 | #include "HttpRequest.h" | |
8822ebee | 43 | #include "mgr/Registration.h" |
f0ba2534 | 44 | #include "neighbors.h" |
4d5904f7 | 45 | #include "SquidConfig.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 | 54 | static int n_userhash_peers = 0; |
a3c6762c | 55 | static CachePeer **userhash_peers = NULL; |
f7e1d9ce | 56 | static OBJH peerUserHashCachemgr; |
6b661f2e | 57 | static void peerUserHashRegisterWithCacheManager(void); |
63104e28 HN |
58 | |
59 | static int | |
60 | peerSortWeight(const void *a, const void *b) | |
61 | { | |
a3c6762c FC |
62 | const CachePeer *const *p1 = (const CachePeer *const *)a; |
63 | const CachePeer *const *p2 = (const CachePeer *const *)b; | |
63104e28 HN |
64 | return (*p1)->weight - (*p2)->weight; |
65 | } | |
66 | ||
67 | void | |
f7e1d9ce | 68 | peerUserHashInit(void) |
63104e28 HN |
69 | { |
70 | int W = 0; | |
71 | int K; | |
72 | int k; | |
73 | double P_last, X_last, Xn; | |
a3c6762c FC |
74 | CachePeer *p; |
75 | CachePeer **P; | |
63104e28 HN |
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 | ||
a3c6762c | 106 | userhash_peers = (CachePeer **)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 |
167 | static void |
168 | peerUserHashRegisterWithCacheManager(void) | |
63104e28 | 169 | { |
8822ebee | 170 | Mgr::RegisterAction("userhash", "peer userhash information", peerUserHashCachemgr, |
d9fc6862 | 171 | 0, 1); |
63104e28 HN |
172 | } |
173 | ||
a3c6762c | 174 | CachePeer * |
f7e1d9ce | 175 | peerUserHashSelectParent(HttpRequest * request) |
63104e28 HN |
176 | { |
177 | int k; | |
178 | const char *c; | |
a3c6762c FC |
179 | CachePeer *p = NULL; |
180 | CachePeer *tp; | |
63104e28 HN |
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 | ||
a3c6762c | 202 | /* select CachePeer */ |
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 | ||
224 | static void | |
f7e1d9ce | 225 | peerUserHashCachemgr(StoreEntry * sentry) |
63104e28 | 226 | { |
a3c6762c | 227 | CachePeer *p; |
63104e28 HN |
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 */ |