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