Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | /** Inherits from Socket */ |
Mark Slee | 1c4a559 | 2006-09-25 21:32:05 +0000 | [diff] [blame] | 4 | include_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php'; |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 5 | |
| 6 | /** |
| 7 | * This library makes use of APC cache to make hosts as down in a web |
| 8 | * environment. If you are running from the CLI or on a system without APC |
| 9 | * installed, then these null functions will step in and act like cache |
| 10 | * misses. |
| 11 | */ |
| 12 | if (!function_exists('apc_fetch')) { |
| 13 | function apc_fetch($key) { return FALSE; } |
| 14 | function apc_store($key, $var, $ttl=0) { return FALSE; } |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Sockets implementation of the TTransport interface that allows connection |
| 19 | * to a pool of servers. |
| 20 | * |
| 21 | * @package thrift.transport |
| 22 | * @author Mark Slee <mcslee@facebook.com> |
| 23 | */ |
| 24 | class TSocketPool extends TSocket { |
| 25 | |
| 26 | /** |
Mark Slee | 3f11b7a | 2006-10-04 19:02:03 +0000 | [diff] [blame] | 27 | * Remote servers. Array of associative arrays with 'host' and 'port' keys |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 28 | */ |
Mark Slee | 3f11b7a | 2006-10-04 19:02:03 +0000 | [diff] [blame] | 29 | private $servers_ = array(); |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * How many times to retry each host in connect |
| 33 | * |
| 34 | * @var int |
| 35 | */ |
| 36 | private $numRetries_ = 1; |
| 37 | |
| 38 | /** |
| 39 | * Retry interval in seconds, how long to not try a host if it has been |
| 40 | * marked as down. |
| 41 | * |
| 42 | * @var int |
| 43 | */ |
| 44 | private $retryInterval_ = 60; |
| 45 | |
| 46 | /** |
| 47 | * Max consecutive failures before marking a host down. |
| 48 | * |
| 49 | * @var int |
| 50 | */ |
| 51 | private $maxConsecutiveFailures_ = 1; |
| 52 | |
| 53 | /** |
| 54 | * Try hosts in order? or Randomized? |
| 55 | * |
| 56 | * @var bool |
| 57 | */ |
| 58 | private $randomize_ = TRUE; |
| 59 | |
| 60 | /** |
| 61 | * Always try last host, even if marked down? |
| 62 | * |
| 63 | * @var bool |
| 64 | */ |
| 65 | private $alwaysTryLast_ = TRUE; |
| 66 | |
| 67 | /** |
| 68 | * Socket pool constructor |
| 69 | * |
Mark Slee | ad58f95 | 2007-01-03 19:23:50 +0000 | [diff] [blame] | 70 | * @param array $hosts List of remote hostnames |
| 71 | * @param mixed $ports Array of remote ports, or a single common port |
| 72 | * @param bool $persist Whether to use a persistent socket |
| 73 | * @param mixed $debugHandler Function for error logging |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 74 | */ |
| 75 | public function __construct($hosts=array('localhost'), |
| 76 | $ports=array(9090), |
Mark Slee | ad58f95 | 2007-01-03 19:23:50 +0000 | [diff] [blame] | 77 | $persist=FALSE, |
| 78 | $debugHandler=null) { |
| 79 | parent::__construct(null, 0, $persist, $debugHandler); |
| 80 | |
Mark Slee | 3f11b7a | 2006-10-04 19:02:03 +0000 | [diff] [blame] | 81 | if (!is_array($ports)) { |
| 82 | $port = $ports; |
| 83 | $ports = array(); |
| 84 | foreach ($hosts as $key => $val) { |
| 85 | $ports[$key] = $port; |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
Mark Slee | 3f11b7a | 2006-10-04 19:02:03 +0000 | [diff] [blame] | 88 | |
| 89 | foreach ($hosts as $key => $host) { |
| 90 | $this->servers_ []= array('host' => $host, |
| 91 | 'port' => $ports[$key]); |
| 92 | } |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Sets how many time to keep retrying a host in the connect function. |
| 97 | * |
| 98 | * @param int $numRetries |
| 99 | */ |
| 100 | public function setNumRetries($numRetries) { |
| 101 | $this->numRetries_ = $numRetries; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Sets how long to wait until retrying a host if it was marked down |
| 106 | * |
| 107 | * @param int $numRetries |
| 108 | */ |
| 109 | public function setRetryInterval($retryInterval) { |
| 110 | $this->retryInterval_ = $retryInterval; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Sets how many time to keep retrying a host before marking it as down. |
| 115 | * |
| 116 | * @param int $numRetries |
| 117 | */ |
| 118 | public function setMaxConsecutiveFailures($maxConsecutiveFailures) { |
| 119 | $this->maxConsecutiveFailures_ = $maxConsecutiveFailures; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Turns randomization in connect order on or off. |
| 124 | * |
| 125 | * @param bool $randomize |
| 126 | */ |
| 127 | public function setRandomize($randomize) { |
| 128 | $this->randomize_ = $randomize; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Whether to always try the last server. |
| 133 | * |
| 134 | * @param bool $alwaysTryLast |
| 135 | */ |
| 136 | public function setAlwaysTryLast($alwaysTryLast) { |
| 137 | $this->alwaysTryLast_ = $alwaysTryLast; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | /** |
| 142 | * Connects the socket by iterating through all the servers in the pool |
| 143 | * and trying to find one that works. |
| 144 | */ |
| 145 | public function open() { |
Mark Slee | 3f11b7a | 2006-10-04 19:02:03 +0000 | [diff] [blame] | 146 | // Check if we want order randomization |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 147 | if ($this->randomize_) { |
Mark Slee | 3f11b7a | 2006-10-04 19:02:03 +0000 | [diff] [blame] | 148 | shuffle($this->servers_); |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 149 | } |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 150 | |
Mark Slee | 3f11b7a | 2006-10-04 19:02:03 +0000 | [diff] [blame] | 151 | // Count servers to identify the "last" one |
| 152 | $numServers = count($this->servers_); |
| 153 | |
| 154 | for ($i = 0; $i < $numServers; ++$i) { |
| 155 | |
| 156 | // This extracts the $host and $port variables |
| 157 | extract($this->servers_[$i]); |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 158 | |
| 159 | // Check APC cache for a record of this server being down |
Mark Slee | 3f11b7a | 2006-10-04 19:02:03 +0000 | [diff] [blame] | 160 | $failtimeKey = 'thrift_failtime:'.$host.':'.$port.'~'; |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 161 | |
| 162 | // Cache miss? Assume it's OK |
| 163 | $lastFailtime = apc_fetch($failtimeKey); |
| 164 | if ($lastFailtime === FALSE) { |
| 165 | $lastFailtime = 0; |
| 166 | } |
| 167 | |
| 168 | $retryIntervalPassed = FALSE; |
| 169 | |
| 170 | // Cache hit...make sure enough the retry interval has elapsed |
| 171 | if ($lastFailtime > 0) { |
| 172 | $elapsed = time() - $lastFailtime; |
robert | b0fac3e | 2007-01-15 23:53:25 +0000 | [diff] [blame] | 173 | if ($elapsed > $this->retryInterval_) { |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 174 | $retryIntervalPassed = TRUE; |
| 175 | if ($this->debug_) { |
Mark Slee | e7714a6 | 2007-01-11 01:26:00 +0000 | [diff] [blame] | 176 | call_user_func($this->debugHandler_, |
| 177 | 'TSocketPool: retryInterval '. |
| 178 | '('.$this->retryInterval_.') '. |
| 179 | 'has passed for host '.$host.':'.$port); |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // Only connect if not in the middle of a fail interval, OR if this |
| 185 | // is the LAST server we are trying, just hammer away on it |
| 186 | $isLastServer = FALSE; |
Mark Slee | a09e34e | 2007-01-03 18:45:04 +0000 | [diff] [blame] | 187 | if ($this->alwaysTryLast_) { |
Mark Slee | 3f11b7a | 2006-10-04 19:02:03 +0000 | [diff] [blame] | 188 | $isLastServer = ($i == ($numServers - 1)); |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | if (($lastFailtime === 0) || |
| 192 | ($isLastServer) || |
| 193 | ($lastFailtime > 0 && $retryIntervalPassed)) { |
| 194 | |
| 195 | // Set underlying TSocket params to this one |
| 196 | $this->host_ = $host; |
| 197 | $this->port_ = $port; |
| 198 | |
Mark Slee | 3f11b7a | 2006-10-04 19:02:03 +0000 | [diff] [blame] | 199 | // Try up to numRetries_ connections per server |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 200 | for ($attempt = 0; $attempt < $this->numRetries_; $attempt++) { |
| 201 | try { |
Mark Slee | 3f11b7a | 2006-10-04 19:02:03 +0000 | [diff] [blame] | 202 | // Use the underlying TSocket open function |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 203 | parent::open(); |
| 204 | |
| 205 | // Only clear the failure counts if required to do so |
| 206 | if ($lastFailtime > 0) { |
| 207 | apc_store($failtimeKey, 0); |
| 208 | } |
Mark Slee | 3f11b7a | 2006-10-04 19:02:03 +0000 | [diff] [blame] | 209 | |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 210 | // Successful connection, return now |
| 211 | return; |
| 212 | |
| 213 | } catch (Exception $x) { |
| 214 | // Connection failed |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // Mark failure of this host in the cache |
| 219 | $consecfailsKey = 'thrift_consecfails:'.$host.':'.$port.'~'; |
| 220 | |
| 221 | // Ignore cache misses |
| 222 | $consecfails = apc_fetch($consecfailsKey); |
| 223 | if ($consecfails === FALSE) { |
| 224 | $consecfails = 0; |
| 225 | } |
| 226 | |
| 227 | // Increment by one |
| 228 | $consecfails++; |
| 229 | |
| 230 | // Log and cache this failure |
| 231 | if ($consecfails >= $this->maxConsecutiveFailures_) { |
| 232 | if ($this->debug_) { |
Mark Slee | e7714a6 | 2007-01-11 01:26:00 +0000 | [diff] [blame] | 233 | call_user_func($this->debugHandler_, |
| 234 | 'TSocketPool: marking '.$host.':'.$port. |
Karl Lehenbauer | 893ef72 | 2007-01-17 18:56:10 +0000 | [diff] [blame] | 235 | ' as down for '.$this->retryInterval_.' secs '. |
Mark Slee | e7714a6 | 2007-01-11 01:26:00 +0000 | [diff] [blame] | 236 | 'after '.$consecfails.' failed attempts.'); |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 237 | } |
| 238 | // Store the failure time |
| 239 | apc_store($failtimeKey, time()); |
| 240 | |
| 241 | // Clear the count of consecutive failures |
| 242 | apc_store($consecfailsKey, 0); |
| 243 | } else { |
| 244 | apc_store($consecfailsKey, $consecfails); |
| 245 | } |
| 246 | } |
Mark Slee | 3f11b7a | 2006-10-04 19:02:03 +0000 | [diff] [blame] | 247 | } |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 248 | |
| 249 | // Holy shit we failed them all. The system is totally ill! |
| 250 | $error = 'TSocketPool: All hosts in pool are down. '; |
Mark Slee | 588e452 | 2006-11-15 22:23:06 +0000 | [diff] [blame] | 251 | $hosts = array(); |
| 252 | foreach ($this->servers_ as $server) { |
| 253 | $hosts []= $server['host'].':'.$server['port']; |
| 254 | } |
| 255 | $hostlist = implode(',', $hosts); |
| 256 | $error .= '('.$hostlist.')'; |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 257 | if ($this->debug_) { |
Mark Slee | e7714a6 | 2007-01-11 01:26:00 +0000 | [diff] [blame] | 258 | call_user_func($this->debugHandler_, $error); |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 259 | } |
| 260 | throw new Exception($error); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | ?> |