2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 39 Peer source hash based selection */
12 #include "CachePeer.h"
13 #include "HttpRequest.h"
14 #include "mgr/Registration.h"
15 #include "neighbors.h"
16 #include "SquidConfig.h"
21 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
23 static int n_sourcehash_peers
= 0;
24 static CachePeer
**sourcehash_peers
= NULL
;
25 static OBJH peerSourceHashCachemgr
;
26 static void peerSourceHashRegisterWithCacheManager(void);
29 peerSortWeight(const void *a
, const void *b
)
31 const CachePeer
*const *p1
= (const CachePeer
*const *)a
;
32 const CachePeer
*const *p2
= (const CachePeer
*const *)b
;
33 return (*p1
)->weight
- (*p2
)->weight
;
37 peerSourceHashInit(void)
42 double P_last
, X_last
, Xn
;
48 for (k
= 0; k
< n_sourcehash_peers
; ++k
) {
49 cbdataReferenceDone(sourcehash_peers
[k
]);
52 safe_free(sourcehash_peers
);
53 n_sourcehash_peers
= 0;
54 /* find out which peers we have */
56 for (p
= Config
.peers
; p
; p
= p
->next
) {
57 if (!p
->options
.sourcehash
)
60 assert(p
->type
== PEER_PARENT
);
70 peerSourceHashRegisterWithCacheManager();
72 if (n_sourcehash_peers
== 0)
75 sourcehash_peers
= (CachePeer
**)xcalloc(n_sourcehash_peers
, sizeof(*sourcehash_peers
));
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
)
85 /* calculate this peers hash */
86 p
->sourcehash
.hash
= 0;
88 for (t
= p
->name
; *t
!= 0; ++t
)
89 p
->sourcehash
.hash
+= ROTATE_LEFT(p
->sourcehash
.hash
, 19) + (unsigned int) *t
;
91 p
->sourcehash
.hash
+= p
->sourcehash
.hash
* 0x62531965;
93 p
->sourcehash
.hash
= ROTATE_LEFT(p
->sourcehash
.hash
, 21);
96 p
->sourcehash
.load_factor
= ((double) p
->weight
) / (double) W
;
98 if (floor(p
->sourcehash
.load_factor
* 1000.0) == 0.0)
99 p
->sourcehash
.load_factor
= 0.0;
101 /* add it to our list of peers */
102 *P
++ = cbdataReference(p
);
105 /* Sort our list on weight */
106 qsort(sourcehash_peers
, n_sourcehash_peers
, sizeof(*sourcehash_peers
), peerSortWeight
);
108 /* Calculate the load factor multipliers X_k
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
116 K
= n_sourcehash_peers
;
118 P_last
= 0.0; /* Empty P_0 */
120 Xn
= 1.0; /* Empty starting point of X_1 * X_2 * ... * X_{x-1} */
122 X_last
= 0.0; /* Empty X_0, nullifies the first pow statement */
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
;
137 peerSourceHashRegisterWithCacheManager(void)
139 Mgr::RegisterAction("sourcehash", "peer sourcehash information",
140 peerSourceHashCachemgr
, 0, 1);
144 peerSourceHashSelectParent(HttpRequest
* request
)
150 unsigned int user_hash
= 0;
151 unsigned int combined_hash
;
153 double high_score
= 0;
154 const char *key
= NULL
;
155 char ntoabuf
[MAX_IPSTRLEN
];
157 if (n_sourcehash_peers
== 0)
160 key
= request
->client_addr
.toStr(ntoabuf
, sizeof(ntoabuf
));
162 /* calculate hash key */
163 debugs(39, 2, "peerSourceHashSelectParent: Calculating hash for " << key
);
165 for (c
= key
; *c
!= 0; ++c
)
166 user_hash
+= ROTATE_LEFT(user_hash
, 19) + *c
;
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
);
178 if ((score
> high_score
) && peerHTTPOkay(tp
, request
)) {
185 debugs(39, 2, "peerSourceHashSelectParent: selected " << p
->name
);
191 peerSourceHashCachemgr(StoreEntry
* sentry
)
195 storeAppendPrintf(sentry
, "%24s %10s %10s %10s %10s\n",
202 for (p
= Config
.peers
; p
; p
= p
->next
)
203 sumfetches
+= p
->stats
.fetches
;
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);