blob: dca69f988f5fcf4c744803daef7bbccbe356edc2 [file] [log] [blame]
Mark Sleeade2c832006-09-08 03:41:50 +00001<?php
2
3/** Inherits from Socket */
Mark Slee1c4a5592006-09-25 21:32:05 +00004include_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php';
Mark Sleeade2c832006-09-08 03:41:50 +00005
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 */
12if (!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 */
24class TSocketPool extends TSocket {
25
26 /**
Mark Slee3f11b7a2006-10-04 19:02:03 +000027 * Remote servers. Array of associative arrays with 'host' and 'port' keys
Mark Sleeade2c832006-09-08 03:41:50 +000028 */
Mark Slee3f11b7a2006-10-04 19:02:03 +000029 private $servers_ = array();
Mark Sleeade2c832006-09-08 03:41:50 +000030
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 Sleead58f952007-01-03 19:23:50 +000070 * @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 Sleeade2c832006-09-08 03:41:50 +000074 */
75 public function __construct($hosts=array('localhost'),
76 $ports=array(9090),
Mark Sleead58f952007-01-03 19:23:50 +000077 $persist=FALSE,
78 $debugHandler=null) {
79 parent::__construct(null, 0, $persist, $debugHandler);
80
Mark Slee3f11b7a2006-10-04 19:02:03 +000081 if (!is_array($ports)) {
82 $port = $ports;
83 $ports = array();
84 foreach ($hosts as $key => $val) {
85 $ports[$key] = $port;
Mark Sleeade2c832006-09-08 03:41:50 +000086 }
87 }
Mark Slee3f11b7a2006-10-04 19:02:03 +000088
89 foreach ($hosts as $key => $host) {
90 $this->servers_ []= array('host' => $host,
91 'port' => $ports[$key]);
92 }
Mark Sleeade2c832006-09-08 03:41:50 +000093 }
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 Slee3f11b7a2006-10-04 19:02:03 +0000146 // Check if we want order randomization
Mark Sleeade2c832006-09-08 03:41:50 +0000147 if ($this->randomize_) {
Mark Slee3f11b7a2006-10-04 19:02:03 +0000148 shuffle($this->servers_);
Mark Sleeade2c832006-09-08 03:41:50 +0000149 }
Mark Sleeade2c832006-09-08 03:41:50 +0000150
Mark Slee3f11b7a2006-10-04 19:02:03 +0000151 // 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 Sleeade2c832006-09-08 03:41:50 +0000158
159 // Check APC cache for a record of this server being down
Mark Slee3f11b7a2006-10-04 19:02:03 +0000160 $failtimeKey = 'thrift_failtime:'.$host.':'.$port.'~';
Mark Sleeade2c832006-09-08 03:41:50 +0000161
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;
robertb0fac3e2007-01-15 23:53:25 +0000173 if ($elapsed > $this->retryInterval_) {
Mark Sleeade2c832006-09-08 03:41:50 +0000174 $retryIntervalPassed = TRUE;
175 if ($this->debug_) {
Mark Sleee7714a62007-01-11 01:26:00 +0000176 call_user_func($this->debugHandler_,
177 'TSocketPool: retryInterval '.
178 '('.$this->retryInterval_.') '.
179 'has passed for host '.$host.':'.$port);
Mark Sleeade2c832006-09-08 03:41:50 +0000180 }
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 Sleea09e34e2007-01-03 18:45:04 +0000187 if ($this->alwaysTryLast_) {
Mark Slee3f11b7a2006-10-04 19:02:03 +0000188 $isLastServer = ($i == ($numServers - 1));
Mark Sleeade2c832006-09-08 03:41:50 +0000189 }
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 Slee3f11b7a2006-10-04 19:02:03 +0000199 // Try up to numRetries_ connections per server
Mark Sleeade2c832006-09-08 03:41:50 +0000200 for ($attempt = 0; $attempt < $this->numRetries_; $attempt++) {
201 try {
Mark Slee3f11b7a2006-10-04 19:02:03 +0000202 // Use the underlying TSocket open function
Mark Sleeade2c832006-09-08 03:41:50 +0000203 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 Slee3f11b7a2006-10-04 19:02:03 +0000209
Mark Sleeade2c832006-09-08 03:41:50 +0000210 // 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 Sleee7714a62007-01-11 01:26:00 +0000233 call_user_func($this->debugHandler_,
234 'TSocketPool: marking '.$host.':'.$port.
Karl Lehenbauer893ef722007-01-17 18:56:10 +0000235 ' as down for '.$this->retryInterval_.' secs '.
Mark Sleee7714a62007-01-11 01:26:00 +0000236 'after '.$consecfails.' failed attempts.');
Mark Sleeade2c832006-09-08 03:41:50 +0000237 }
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 Slee3f11b7a2006-10-04 19:02:03 +0000247 }
Mark Sleeade2c832006-09-08 03:41:50 +0000248
249 // Holy shit we failed them all. The system is totally ill!
250 $error = 'TSocketPool: All hosts in pool are down. ';
Mark Slee588e4522006-11-15 22:23:06 +0000251 $hosts = array();
252 foreach ($this->servers_ as $server) {
253 $hosts []= $server['host'].':'.$server['port'];
254 }
255 $hostlist = implode(',', $hosts);
256 $error .= '('.$hostlist.')';
Mark Sleeade2c832006-09-08 03:41:50 +0000257 if ($this->debug_) {
Mark Sleee7714a62007-01-11 01:26:00 +0000258 call_user_func($this->debugHandler_, $error);
Mark Sleeade2c832006-09-08 03:41:50 +0000259 }
260 throw new Exception($error);
261 }
262}
263
264?>