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 | |
| 60 | TSocketPool::TSocketPool(const 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 | |
dweatherford | d137282 | 2007-10-09 22:57:23 +0000 | [diff] [blame] | 72 | TSocketPool::TSocketPool(const string& host, int port) : TSocket(), |
| 73 | numRetries_(1), |
| 74 | retryInterval_(60), |
| 75 | maxConsecutiveFailures_(1), |
| 76 | randomize_(true), |
| 77 | alwaysTryLast_(true) |
| 78 | { |
| 79 | addServer(host, port); |
| 80 | } |
| 81 | |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 82 | TSocketPool::~TSocketPool() { |
| 83 | close(); |
| 84 | } |
| 85 | |
dweatherford | d137282 | 2007-10-09 22:57:23 +0000 | [diff] [blame] | 86 | void TSocketPool::addServer(const string& host, int port) { |
David Reiss | 6d0cccd | 2008-02-28 21:20:12 +0000 | [diff] [blame^] | 87 | servers_.push_back(TSocketPoolServer(host, port)); |
dweatherford | d137282 | 2007-10-09 22:57:23 +0000 | [diff] [blame] | 88 | } |
| 89 | |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 90 | void TSocketPool::setNumRetries(int numRetries) { |
| 91 | numRetries_ = numRetries; |
| 92 | } |
| 93 | |
| 94 | void TSocketPool::setRetryInterval(int retryInterval) { |
| 95 | retryInterval_ = retryInterval; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | void TSocketPool::setMaxConsecutiveFailures(int maxConsecutiveFailures) { |
| 100 | maxConsecutiveFailures_ = maxConsecutiveFailures; |
| 101 | } |
| 102 | |
| 103 | void TSocketPool::setRandomize(bool randomize) { |
| 104 | randomize_ = randomize; |
| 105 | } |
| 106 | |
| 107 | void TSocketPool::setAlwaysTryLast(bool alwaysTryLast) { |
| 108 | alwaysTryLast_ = alwaysTryLast; |
| 109 | } |
| 110 | |
| 111 | /* TODO: without apc we ignore a lot of functionality from the php version */ |
| 112 | void TSocketPool::open() { |
| 113 | if (randomize_) { |
| 114 | std::random_shuffle(servers_.begin(), servers_.end()); |
| 115 | } |
| 116 | |
David Reiss | 6d0cccd | 2008-02-28 21:20:12 +0000 | [diff] [blame^] | 117 | unsigned int numServers = servers_.size(); |
| 118 | for (unsigned int i = 0; i < numServers; ++i) { |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 119 | |
David Reiss | 6d0cccd | 2008-02-28 21:20:12 +0000 | [diff] [blame^] | 120 | TSocketPoolServer &server = servers_[i]; |
| 121 | bool retryIntervalPassed = (server.lastFailTime_ == 0); |
| 122 | bool isLastServer = alwaysTryLast_ ? (i == (numServers - 1)) : false; |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 123 | |
David Reiss | 6d0cccd | 2008-02-28 21:20:12 +0000 | [diff] [blame^] | 124 | if (server.lastFailTime_ > 0) { |
| 125 | // The server was marked as down, so check if enough time has elapsed to retry |
| 126 | int elapsedTime = time(NULL) - server.lastFailTime_; |
| 127 | if (elapsedTime > retryInterval_) { |
| 128 | retryIntervalPassed = true; |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 129 | } |
| 130 | } |
David Reiss | 6d0cccd | 2008-02-28 21:20:12 +0000 | [diff] [blame^] | 131 | |
| 132 | if (retryIntervalPassed || isLastServer) { |
| 133 | for (int j = 0; j < numRetries_; ++j) { |
| 134 | try { |
| 135 | TSocket::open(); |
| 136 | |
| 137 | // reset lastFailTime_ is required |
| 138 | if (server.lastFailTime_) { |
| 139 | server.lastFailTime_ = 0; |
| 140 | } |
| 141 | |
| 142 | // success |
| 143 | return; |
| 144 | } catch (TException e) { |
| 145 | // connection failed |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | ++server.consecutiveFailures_; |
| 151 | if (server.consecutiveFailures_ > maxConsecutiveFailures_) { |
| 152 | // Mark server as down |
| 153 | server.consecutiveFailures_ = 0; |
| 154 | server.lastFailTime_ = time(NULL); |
| 155 | } |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 156 | } |
| 157 | |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 158 | GlobalOutput("TSocketPool::open: all connections failed"); |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 159 | throw TTransportException(TTransportException::NOT_OPEN); |
| 160 | } |
| 161 | |
| 162 | }}} // facebook::thrift::transport |