--------------------- PatchSet 2969 Date: 2001/09/06 21:50:56 Author: hno Branch: hno-emara Tag: (none) Log: Merged in sourcehash from rproxy Members: src/Makefile.in:1.14.4.2->1.14.4.3 src/cache_cf.c:1.33.4.3->1.33.4.4 src/enums.h:1.22.6.1->1.22.6.2 src/neighbors.c:1.12.12.1->1.12.12.2 src/peer_select.c:1.9.26.1->1.9.26.2 src/peer_sourcehash.c:1.1->1.1.4.1 src/peer_userhash.c:1.1.18.1->1.1.18.2 src/protos.h:1.32.4.2->1.32.4.3 src/structs.h:1.38.6.5->1.38.6.6 Index: squid/src/Makefile.in =================================================================== RCS file: /cvsroot/squid-sf//squid/src/Attic/Makefile.in,v retrieving revision 1.14.4.2 retrieving revision 1.14.4.3 diff -u -r1.14.4.2 -r1.14.4.3 --- squid/src/Makefile.in 26 Aug 2001 20:42:32 -0000 1.14.4.2 +++ squid/src/Makefile.in 6 Sep 2001 21:50:56 -0000 1.14.4.3 @@ -1,7 +1,7 @@ # # Makefile for the Squid Object Cache server # -# $Id: Makefile.in,v 1.14.4.2 2001/08/26 20:42:32 hno Exp $ +# $Id: Makefile.in,v 1.14.4.3 2001/09/06 21:50:56 hno Exp $ # # Uncomment and customize the following to suit your needs: # @@ -158,6 +158,7 @@ peer_digest.o \ peer_monitor.o \ peer_select.o \ + peer_sourcehash.o \ peer_userhash.o \ redirect.o \ referer.o \ Index: squid/src/cache_cf.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/cache_cf.c,v retrieving revision 1.33.4.3 retrieving revision 1.33.4.4 diff -u -r1.33.4.3 -r1.33.4.4 --- squid/src/cache_cf.c 30 Aug 2001 14:06:28 -0000 1.33.4.3 +++ squid/src/cache_cf.c 6 Sep 2001 21:50:56 -0000 1.33.4.4 @@ -1,6 +1,6 @@ /* - * $Id: cache_cf.c,v 1.33.4.3 2001/08/30 14:06:28 hno Exp $ + * $Id: cache_cf.c,v 1.33.4.4 2001/09/06 21:50:56 hno Exp $ * * DEBUG: section 3 Configuration File Parsing * AUTHOR: Harvest Derived @@ -1328,6 +1328,8 @@ p->options.roundrobin = 1; } else if (!strcasecmp(token, "userhash")) { p->options.userhash = 1; + } else if (!strcasecmp(token, "sourcehash")) { + p->options.sourcehash = 1; #if USE_HTCP } else if (!strcasecmp(token, "htcp")) { p->options.htcp = 1; Index: squid/src/enums.h =================================================================== RCS file: /cvsroot/squid-sf//squid/src/enums.h,v retrieving revision 1.22.6.1 retrieving revision 1.22.6.2 diff -u -r1.22.6.1 -r1.22.6.2 --- squid/src/enums.h 26 Aug 2001 20:02:41 -0000 1.22.6.1 +++ squid/src/enums.h 6 Sep 2001 21:50:57 -0000 1.22.6.2 @@ -1,6 +1,6 @@ /* - * $Id: enums.h,v 1.22.6.1 2001/08/26 20:02:41 hno Exp $ + * $Id: enums.h,v 1.22.6.2 2001/09/06 21:50:57 hno Exp $ * * * SQUID Web Proxy Cache http://www.squid-cache.org/ @@ -310,6 +310,7 @@ #endif ANY_OLD_PARENT, USERHASH_PARENT, + SOURCEHASH_PARENT, HIER_MAX } hier_code; Index: squid/src/neighbors.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/neighbors.c,v retrieving revision 1.12.12.1 retrieving revision 1.12.12.2 diff -u -r1.12.12.1 -r1.12.12.2 --- squid/src/neighbors.c 26 Aug 2001 20:02:41 -0000 1.12.12.1 +++ squid/src/neighbors.c 6 Sep 2001 21:50:57 -0000 1.12.12.2 @@ -1,6 +1,6 @@ /* - * $Id: neighbors.c,v 1.12.12.1 2001/08/26 20:02:41 hno Exp $ + * $Id: neighbors.c,v 1.12.12.2 2001/09/06 21:50:57 hno Exp $ * * DEBUG: section 15 Neighbor Routines * AUTHOR: Harvest Derived @@ -1241,6 +1241,8 @@ storeAppendPrintf(sentry, " round-robin"); if (p->options.userhash) storeAppendPrintf(sentry, " userhash"); + if (p->options.sourcehash) + storeAppendPrintf(sentry, " sourcehash"); #if USE_HTCP if (p->options.htcp) storeAppendPrintf(sentry, " htcp"); Index: squid/src/peer_select.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/peer_select.c,v retrieving revision 1.9.26.1 retrieving revision 1.9.26.2 diff -u -r1.9.26.1 -r1.9.26.2 --- squid/src/peer_select.c 26 Aug 2001 20:02:41 -0000 1.9.26.1 +++ squid/src/peer_select.c 6 Sep 2001 21:50:57 -0000 1.9.26.2 @@ -1,6 +1,6 @@ /* - * $Id: peer_select.c,v 1.9.26.1 2001/08/26 20:02:41 hno Exp $ + * $Id: peer_select.c,v 1.9.26.2 2001/09/06 21:50:57 hno Exp $ * * DEBUG: section 44 Peer Selection Algorithm * AUTHOR: Duane Wessels @@ -60,6 +60,7 @@ #endif "ANY_PARENT", "USERHASH_PARENT", + "SOURCEHASH_PARENT", "INVALID CODE" }; @@ -445,6 +446,8 @@ code = DEFAULT_PARENT; } else if ((p = peerUserHashSelectParent(request))) { code = USERHASH_PARENT; + } else if ((p = peerSourceHashSelectParent(request))) { + code = SOURCEHASH_PARENT; } else if ((p = getRoundRobinParent(request))) { code = ROUNDROBIN_PARENT; } else if ((p = getFirstUpParent(request))) { --- /dev/null Wed Feb 14 00:55:00 2007 +++ squid/src/peer_sourcehash.c Wed Feb 14 00:55:02 2007 @@ -0,0 +1,159 @@ + +/* + * $Id: peer_sourcehash.c,v 1.1.4.1 2001/09/06 21:50:57 hno Exp $ + * + * DEBUG: section 44 Peer user hash based selection + * AUTHOR: Henrik Nordstrom + * BASED ON: carp.c by Eric Stern + * + * SQUID Web Proxy Cache http://www.squid-cache.org/ + * ---------------------------------------------------------- + * + * Squid is the result of efforts by numerous individuals from + * the Internet community; see the CONTRIBUTORS file for full + * details. Many organizations have provided support for Squid's + * development; see the SPONSORS file for full details. Squid is + * Copyrighted (C) 2001 by the Regents of the University of + * California; see the COPYRIGHT file for full details. Squid + * incorporates software developed and/or copyrighted by other + * sources; see the CREDITS file for full details. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. + * + */ + +#include "squid.h" + +#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (((sizeof(unsigned long)*8)-(n))))) + +static int n_sourcehash_peers = 0; +static peer **sourcehash_peers = NULL; + +static int +peerSortWeight(const void *a, const void *b) +{ + const peer *p1 = a, *p2 = b; + return p1->weight - p2->weight; +} + +void +peerSourceHashInit(void) +{ + int W = 0; + int K; + int k; + double P_last, X_last, Xn; + peer *p; + peer **P; + char *t; + /* Clean up */ + for (k = 0; k < n_sourcehash_peers; k++) { + cbdataUnlock(sourcehash_peers[k]); + } + safe_free(sourcehash_peers); + n_sourcehash_peers = 0; + /* find out which peers we have */ + for (p = Config.peers; p; p = p->next) { + if (!p->options.sourcehash) + continue; + assert(p->type == PEER_PARENT); + if (p->weight == 0) + continue; + n_sourcehash_peers++; + W += p->weight; + } + if (n_sourcehash_peers == 0) + return; + sourcehash_peers = xcalloc(n_sourcehash_peers, sizeof(*sourcehash_peers)); + /* Build a list of the found peers and calculate hashes and load factors */ + for (P = sourcehash_peers, p = Config.peers; p; p = p->next) { + if (!p->options.sourcehash) + continue; + if (p->weight == 0) + continue; + /* calculate this peers hash */ + p->sourcehash.hash = 0; + for (t = p->host; *t != 0; t++) + p->sourcehash.hash += ROTATE_LEFT(p->sourcehash.hash, 19) + (unsigned int) *t; + p->sourcehash.hash += p->sourcehash.hash * 0x62531965; + p->sourcehash.hash = ROTATE_LEFT(p->sourcehash.hash, 21); + /* and load factor */ + p->sourcehash.load_factor = ((double) p->weight) / (double) W; + if (floor(p->sourcehash.load_factor * 1000.0) == 0.0) + p->sourcehash.load_factor = 0.0; + /* add it to our list of peers */ + *P++ = p; + cbdataLock(p); + } + /* Sort our list on weight */ + qsort(sourcehash_peers, n_sourcehash_peers, sizeof(*sourcehash_peers), peerSortWeight); + /* Calculate the load factor multipliers X_k + * + * X_1 = pow ((K*p_1), (1/K)) + * X_k = ([K-k+1] * [P_k - P_{k-1}])/(X_1 * X_2 * ... * X_{k-1}) + * X_k += pow ((X_{k-1}, {K-k+1}) + * X_k = pow (X_k, {1/(K-k+1)}) + * simplified to have X_1 part of the loop + */ + K = n_sourcehash_peers; + P_last = 0.0; /* Empty P_0 */ + Xn = 1.0; /* Empty starting point of X_1 * X_2 * ... * X_{x-1} */ + X_last = 0.0; /* Empty X_0, nullifies the first pow statement */ + for (k = 1; k <= K; k++) { + double Kk1 = (double) (K - k + 1); + p = sourcehash_peers[k - 1]; + p->sourcehash.load_multiplier = (Kk1 * (p->sourcehash.load_factor - P_last)) / Xn; + p->sourcehash.load_multiplier += pow(X_last, Kk1); + p->sourcehash.load_multiplier = pow(p->sourcehash.load_multiplier, 1.0 / Kk1); + Xn *= p->sourcehash.load_multiplier; + X_last = p->sourcehash.load_multiplier; + P_last = p->sourcehash.load_factor; + } +} + +peer * +peerSourceHashSelectParent(request_t * request) +{ + int k; + const char *c; + peer *p = NULL; + peer *tp; + unsigned long user_hash = 0; + unsigned long combined_hash; + unsigned long high_score = 0; + char *ip = inet_ntoa(request->client_addr); + + /* calculate source hash */ + debug(39, 2) ("peerSourceHashSelectParent: Calculating hash for %s\n", ip); + for (c = ip; *c != 0; c++) + user_hash += ROTATE_LEFT(user_hash, 19) + *c; + /* select peer */ + for (k = 0; k < n_sourcehash_peers; k++) { + tp = sourcehash_peers[k]; + combined_hash = (user_hash ^ tp->sourcehash.hash); + combined_hash += combined_hash * 0x62531965; + combined_hash = ROTATE_LEFT(combined_hash, 21); + combined_hash = combined_hash * tp->sourcehash.load_multiplier; + debug(39, 3) ("peerSourceHashSelectParent: %s combined_hash %d\n", + tp->host, combined_hash); + if ((combined_hash > high_score) && peerHTTPOkay(tp, request)) { + p = tp; + high_score = combined_hash; + } + } + if (p) + debug(39, 2) ("peerSourceHashSelectParent: selected %s\n", p->host); + return p; +} Index: squid/src/peer_userhash.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/peer_userhash.c,v retrieving revision 1.1.18.1 retrieving revision 1.1.18.2 diff -u -r1.1.18.1 -r1.1.18.2 --- squid/src/peer_userhash.c 26 Aug 2001 20:02:41 -0000 1.1.18.1 +++ squid/src/peer_userhash.c 6 Sep 2001 21:50:57 -0000 1.1.18.2 @@ -1,6 +1,6 @@ /* - * $Id: peer_userhash.c,v 1.1.18.1 2001/08/26 20:02:41 hno Exp $ + * $Id: peer_userhash.c,v 1.1.18.2 2001/09/06 21:50:57 hno Exp $ * * DEBUG: section 44 Peer user hash based selection * AUTHOR: Henrik Nordstrom @@ -141,7 +141,7 @@ if (!user) return NULL; /* calculate username hash */ - debug(39, 2) ("carpSelectParent: userhash Calculating hash for %s\n", user); + debug(39, 2) ("peerUserHashSelectParent: Calculating hash for %s\n", user); for (c = user; *c != 0; c++) user_hash += ROTATE_LEFT(user_hash, 19) + *c; /* select peer */ @@ -151,7 +151,7 @@ combined_hash += combined_hash * 0x62531965; combined_hash = ROTATE_LEFT(combined_hash, 21); combined_hash = combined_hash * tp->userhash.load_multiplier; - debug(39, 3) ("carpSelectParent: %s combined_hash %d\n", + debug(39, 3) ("peerUserHashSelectParent: %s combined_hash %d\n", tp->host, combined_hash); if ((combined_hash > high_score) && peerHTTPOkay(tp, request)) { p = tp; @@ -159,6 +159,6 @@ } } if (p) - debug(39, 3) ("carpSelectParent: selected CARP %s\n", p->host); + debug(39, 2) ("peerUserHashSelectParent: selected %s\n", p->host); return p; } Index: squid/src/protos.h =================================================================== RCS file: /cvsroot/squid-sf//squid/src/protos.h,v retrieving revision 1.32.4.2 retrieving revision 1.32.4.3 diff -u -r1.32.4.2 -r1.32.4.3 --- squid/src/protos.h 26 Aug 2001 20:42:32 -0000 1.32.4.2 +++ squid/src/protos.h 6 Sep 2001 21:50:57 -0000 1.32.4.3 @@ -1,6 +1,6 @@ /* - * $Id: protos.h,v 1.32.4.2 2001/08/26 20:42:32 hno Exp $ + * $Id: protos.h,v 1.32.4.3 2001/09/06 21:50:57 hno Exp $ * * * SQUID Web Proxy Cache http://www.squid-cache.org/ @@ -1226,6 +1226,8 @@ extern void peerUserHashInit(void); extern peer *peerUserHashSelectParent(request_t *); +extern void peerSourceHashInit(void); +extern peer *peerSourceHashSelectParent(request_t *); #if DELAY_POOLS extern void delayPoolsInit(void); Index: squid/src/structs.h =================================================================== RCS file: /cvsroot/squid-sf//squid/src/structs.h,v retrieving revision 1.38.6.5 retrieving revision 1.38.6.6 diff -u -r1.38.6.5 -r1.38.6.6 --- squid/src/structs.h 30 Aug 2001 12:03:15 -0000 1.38.6.5 +++ squid/src/structs.h 6 Sep 2001 21:50:57 -0000 1.38.6.6 @@ -1,6 +1,6 @@ /* - * $Id: structs.h,v 1.38.6.5 2001/08/30 12:03:15 hno Exp $ + * $Id: structs.h,v 1.38.6.6 2001/09/06 21:50:57 hno Exp $ * * * SQUID Web Proxy Cache http://www.squid-cache.org/ @@ -1262,6 +1262,7 @@ unsigned int allow_miss:1; unsigned int originserver:1; unsigned int userhash:1; + unsigned int sourcehash:1; } options; int weight; struct { @@ -1298,6 +1299,11 @@ double load_multiplier; float load_factor; } userhash; + struct { + unsigned int hash; + double load_multiplier; + float load_factor; + } sourcehash; char *login; /* Proxy authorization */ time_t connect_timeout; int max_conn;