jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 1 | // Copyright (c) 2007- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
| 7 | #include <algorithm> |
| 8 | #include <iostream> |
| 9 | |
| 10 | #include "TSocketPool.h" |
| 11 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 12 | namespace facebook { namespace thrift { namespace transport { |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 13 | |
| 14 | using namespace std; |
| 15 | |
David Reiss | 8f3bce4 | 2008-03-18 18:21:52 +0000 | [diff] [blame] | 16 | using boost::shared_ptr; |
| 17 | |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 18 | /** |
David Reiss | 6d0cccd | 2008-02-28 21:20:12 +0000 | [diff] [blame] | 19 | * TSocketPoolServer implementation |
| 20 | * |
| 21 | * @author Akhil Wable <akhil@facebook.com> |
| 22 | */ |
| 23 | TSocketPoolServer::TSocketPoolServer() |
| 24 | : host_(""), |
| 25 | port_(0), |
| 26 | lastFailTime_(0), |
| 27 | consecutiveFailures_(0) {} |
| 28 | |
| 29 | /** |
| 30 | * Constructor for TSocketPool server |
| 31 | */ |
David Reiss | 8f3bce4 | 2008-03-18 18:21:52 +0000 | [diff] [blame] | 32 | TSocketPoolServer::TSocketPoolServer(const string &host, int port) |
David Reiss | 6d0cccd | 2008-02-28 21:20:12 +0000 | [diff] [blame] | 33 | : host_(host), |
| 34 | port_(port), |
| 35 | lastFailTime_(0), |
| 36 | consecutiveFailures_(0) {} |
| 37 | |
| 38 | /** |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 39 | * TSocketPool implementation. |
| 40 | * |
| 41 | * @author Jason Sobel <jsobel@facebook.com> |
| 42 | */ |
| 43 | |
David Reiss | 8f3bce4 | 2008-03-18 18:21:52 +0000 | [diff] [blame] | 44 | TSocketPool::TSocketPool() : TSocket(), |
| 45 | numRetries_(1), |
| 46 | retryInterval_(60), |
| 47 | maxConsecutiveFailures_(1), |
| 48 | randomize_(true), |
| 49 | alwaysTryLast_(true) { |
| 50 | } |
| 51 | |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 52 | TSocketPool::TSocketPool(const vector<string> &hosts, |
| 53 | const vector<int> &ports) : TSocket(), |
| 54 | numRetries_(1), |
| 55 | retryInterval_(60), |
| 56 | maxConsecutiveFailures_(1), |
| 57 | randomize_(true), |
| 58 | alwaysTryLast_(true) |
| 59 | { |
| 60 | if (hosts.size() != ports.size()) { |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 61 | GlobalOutput("TSocketPool::TSocketPool: hosts.size != ports.size"); |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 62 | throw TTransportException(TTransportException::BAD_ARGS); |
| 63 | } |
| 64 | |
| 65 | for (unsigned int i = 0; i < hosts.size(); ++i) { |
dweatherford | d137282 | 2007-10-09 22:57:23 +0000 | [diff] [blame] | 66 | addServer(hosts[i], ports[i]); |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
David Reiss | 8f3bce4 | 2008-03-18 18:21:52 +0000 | [diff] [blame] | 70 | TSocketPool::TSocketPool(const vector<pair<string, int> >& servers) : TSocket(), |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 71 | numRetries_(1), |
| 72 | retryInterval_(60), |
| 73 | maxConsecutiveFailures_(1), |
| 74 | randomize_(true), |
| 75 | alwaysTryLast_(true) |
| 76 | { |
David Reiss | 6d0cccd | 2008-02-28 21:20:12 +0000 | [diff] [blame] | 77 | for (unsigned i = 0; i < servers.size(); ++i) { |
| 78 | addServer(servers[i].first, servers[i].second); |
| 79 | } |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 80 | } |
| 81 | |
David Reiss | 8f3bce4 | 2008-03-18 18:21:52 +0000 | [diff] [blame] | 82 | TSocketPool::TSocketPool(const vector< shared_ptr<TSocketPoolServer> >& servers) : TSocket(), |
David Reiss | 907ad76 | 2008-03-02 00:25:58 +0000 | [diff] [blame] | 83 | servers_(servers), |
| 84 | numRetries_(1), |
| 85 | retryInterval_(60), |
| 86 | maxConsecutiveFailures_(1), |
| 87 | randomize_(true), |
| 88 | alwaysTryLast_(true) |
| 89 | { |
| 90 | } |
| 91 | |
dweatherford | d137282 | 2007-10-09 22:57:23 +0000 | [diff] [blame] | 92 | TSocketPool::TSocketPool(const string& host, int port) : TSocket(), |
| 93 | numRetries_(1), |
| 94 | retryInterval_(60), |
| 95 | maxConsecutiveFailures_(1), |
| 96 | randomize_(true), |
| 97 | alwaysTryLast_(true) |
| 98 | { |
| 99 | addServer(host, port); |
| 100 | } |
| 101 | |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 102 | TSocketPool::~TSocketPool() { |
| 103 | close(); |
| 104 | } |
| 105 | |
dweatherford | d137282 | 2007-10-09 22:57:23 +0000 | [diff] [blame] | 106 | void TSocketPool::addServer(const string& host, int port) { |
David Reiss | 8f3bce4 | 2008-03-18 18:21:52 +0000 | [diff] [blame] | 107 | servers_.push_back(shared_ptr<TSocketPoolServer>(new TSocketPoolServer(host, port))); |
dweatherford | d137282 | 2007-10-09 22:57:23 +0000 | [diff] [blame] | 108 | } |
| 109 | |
David Reiss | 8f3bce4 | 2008-03-18 18:21:52 +0000 | [diff] [blame] | 110 | void TSocketPool::setServers(const vector< shared_ptr<TSocketPoolServer> >& servers) { |
| 111 | servers_ = servers; |
| 112 | } |
| 113 | |
| 114 | void TSocketPool::getServers(vector< shared_ptr<TSocketPoolServer> >& servers) { |
| 115 | servers = servers_; |
David Reiss | 907ad76 | 2008-03-02 00:25:58 +0000 | [diff] [blame] | 116 | } |
| 117 | |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 118 | void TSocketPool::setNumRetries(int numRetries) { |
| 119 | numRetries_ = numRetries; |
| 120 | } |
| 121 | |
| 122 | void TSocketPool::setRetryInterval(int retryInterval) { |
| 123 | retryInterval_ = retryInterval; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | void TSocketPool::setMaxConsecutiveFailures(int maxConsecutiveFailures) { |
| 128 | maxConsecutiveFailures_ = maxConsecutiveFailures; |
| 129 | } |
| 130 | |
| 131 | void TSocketPool::setRandomize(bool randomize) { |
| 132 | randomize_ = randomize; |
| 133 | } |
| 134 | |
| 135 | void TSocketPool::setAlwaysTryLast(bool alwaysTryLast) { |
| 136 | alwaysTryLast_ = alwaysTryLast; |
| 137 | } |
| 138 | |
| 139 | /* TODO: without apc we ignore a lot of functionality from the php version */ |
| 140 | void TSocketPool::open() { |
| 141 | if (randomize_) { |
David Reiss | 8f3bce4 | 2008-03-18 18:21:52 +0000 | [diff] [blame] | 142 | random_shuffle(servers_.begin(), servers_.end()); |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 143 | } |
| 144 | |
David Reiss | 6d0cccd | 2008-02-28 21:20:12 +0000 | [diff] [blame] | 145 | unsigned int numServers = servers_.size(); |
| 146 | for (unsigned int i = 0; i < numServers; ++i) { |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 147 | |
David Reiss | 8f3bce4 | 2008-03-18 18:21:52 +0000 | [diff] [blame] | 148 | TSocketPoolServer &server = *(servers_[i]); |
David Reiss | 6d0cccd | 2008-02-28 21:20:12 +0000 | [diff] [blame] | 149 | bool retryIntervalPassed = (server.lastFailTime_ == 0); |
| 150 | bool isLastServer = alwaysTryLast_ ? (i == (numServers - 1)) : false; |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 151 | |
David Reiss | f50021a | 2008-02-29 07:33:47 +0000 | [diff] [blame] | 152 | host_ = server.host_; |
| 153 | port_ = server.port_; |
| 154 | |
David Reiss | 6d0cccd | 2008-02-28 21:20:12 +0000 | [diff] [blame] | 155 | if (server.lastFailTime_ > 0) { |
| 156 | // The server was marked as down, so check if enough time has elapsed to retry |
| 157 | int elapsedTime = time(NULL) - server.lastFailTime_; |
| 158 | if (elapsedTime > retryInterval_) { |
| 159 | retryIntervalPassed = true; |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
David Reiss | 6d0cccd | 2008-02-28 21:20:12 +0000 | [diff] [blame] | 162 | |
| 163 | if (retryIntervalPassed || isLastServer) { |
| 164 | for (int j = 0; j < numRetries_; ++j) { |
| 165 | try { |
| 166 | TSocket::open(); |
| 167 | |
| 168 | // reset lastFailTime_ is required |
| 169 | if (server.lastFailTime_) { |
| 170 | server.lastFailTime_ = 0; |
| 171 | } |
| 172 | |
| 173 | // success |
| 174 | return; |
| 175 | } catch (TException e) { |
| 176 | // connection failed |
| 177 | } |
| 178 | } |
David Reiss | 6d0cccd | 2008-02-28 21:20:12 +0000 | [diff] [blame] | 179 | |
David Reiss | 8f3bce4 | 2008-03-18 18:21:52 +0000 | [diff] [blame] | 180 | ++server.consecutiveFailures_; |
| 181 | if (server.consecutiveFailures_ > maxConsecutiveFailures_) { |
| 182 | // Mark server as down |
| 183 | server.consecutiveFailures_ = 0; |
| 184 | server.lastFailTime_ = time(NULL); |
| 185 | } |
David Reiss | 6d0cccd | 2008-02-28 21:20:12 +0000 | [diff] [blame] | 186 | } |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 187 | } |
| 188 | |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 189 | GlobalOutput("TSocketPool::open: all connections failed"); |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 190 | throw TTransportException(TTransportException::NOT_OPEN); |
| 191 | } |
| 192 | |
| 193 | }}} // facebook::thrift::transport |