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