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