Edward Yang | 517c7c9 | 2018-12-08 19:32:01 -0800 | [diff] [blame] | 1 | #include <c10d/TCPStore.hpp> |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 2 | |
| 3 | #include <poll.h> |
Pieter Noordhuis | 2ead3b0 | 2018-07-12 17:43:27 -0700 | [diff] [blame] | 4 | |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 5 | #include <unistd.h> |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 6 | #include <algorithm> |
Pieter Noordhuis | 7d0de4f | 2018-05-23 11:26:35 -0700 | [diff] [blame] | 7 | #include <system_error> |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 8 | |
| 9 | namespace c10d { |
| 10 | |
| 11 | namespace { |
| 12 | |
Pieter Noordhuis | 7d0de4f | 2018-05-23 11:26:35 -0700 | [diff] [blame] | 13 | enum class QueryType : uint8_t { SET, GET, ADD, CHECK, WAIT }; |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 14 | |
Pieter Noordhuis | 7d0de4f | 2018-05-23 11:26:35 -0700 | [diff] [blame] | 15 | enum class CheckResponseType : uint8_t { READY, NOT_READY }; |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 16 | |
Pieter Noordhuis | 7d0de4f | 2018-05-23 11:26:35 -0700 | [diff] [blame] | 17 | enum class WaitResponseType : uint8_t { STOP_WAITING }; |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 18 | |
| 19 | } // anonymous namespace |
| 20 | |
| 21 | // TCPStoreDaemon class methods |
| 22 | // Simply start the daemon thread |
Pieter Noordhuis | 7d0de4f | 2018-05-23 11:26:35 -0700 | [diff] [blame] | 23 | TCPStoreDaemon::TCPStoreDaemon(int storeListenSocket) |
| 24 | : storeListenSocket_(storeListenSocket) { |
Pieter Noordhuis | c360330 | 2018-11-10 17:52:31 -0800 | [diff] [blame] | 25 | // Use control pipe to signal instance destruction to the daemon thread. |
| 26 | if (pipe(controlPipeFd_.data()) == -1) { |
| 27 | throw std::runtime_error( |
| 28 | "Failed to create the control pipe to start the " |
| 29 | "TCPStoreDaemon run"); |
| 30 | } |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 31 | daemonThread_ = std::thread(&TCPStoreDaemon::run, this); |
| 32 | } |
| 33 | |
| 34 | TCPStoreDaemon::~TCPStoreDaemon() { |
| 35 | // Stop the run |
| 36 | stop(); |
| 37 | // Join the thread |
| 38 | join(); |
| 39 | // Close unclosed sockets |
| 40 | for (auto socket : sockets_) { |
| 41 | if (socket != -1) { |
| 42 | ::close(socket); |
| 43 | } |
| 44 | } |
| 45 | // Now close the rest control pipe |
| 46 | for (auto fd : controlPipeFd_) { |
| 47 | if (fd != -1) { |
| 48 | ::close(fd); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | void TCPStoreDaemon::join() { |
| 54 | daemonThread_.join(); |
| 55 | } |
| 56 | |
| 57 | void TCPStoreDaemon::run() { |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 58 | std::vector<struct pollfd> fds; |
Pieter Noordhuis | 7d0de4f | 2018-05-23 11:26:35 -0700 | [diff] [blame] | 59 | fds.push_back({.fd = storeListenSocket_, .events = POLLIN}); |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 60 | // Push the read end of the pipe to signal the stopping of the daemon run |
Pieter Noordhuis | 7d0de4f | 2018-05-23 11:26:35 -0700 | [diff] [blame] | 61 | fds.push_back({.fd = controlPipeFd_[0], .events = POLLHUP}); |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 62 | |
| 63 | // receive the queries |
| 64 | bool finished = false; |
| 65 | while (!finished) { |
| 66 | for (size_t i = 0; i < sockets_.size(); i++) { |
| 67 | fds[i].revents = 0; |
| 68 | } |
| 69 | |
SsnL | 774705b | 2019-01-14 15:59:29 -0800 | [diff] [blame] | 70 | SYSCHECK_ERR_RETURN_NEG1(::poll(fds.data(), fds.size(), -1)); |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 71 | |
| 72 | // TCPStore's listening socket has an event and it should now be able to |
| 73 | // accept new connections. |
| 74 | if (fds[0].revents != 0) { |
| 75 | if (fds[0].revents ^ POLLIN) { |
Pieter Noordhuis | 7d0de4f | 2018-05-23 11:26:35 -0700 | [diff] [blame] | 76 | throw std::system_error( |
| 77 | ECONNABORTED, |
| 78 | std::system_category(), |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 79 | "Unexpected poll revent on the master's listening socket: " + |
Pieter Noordhuis | 7d0de4f | 2018-05-23 11:26:35 -0700 | [diff] [blame] | 80 | std::to_string(fds[0].revents)); |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 81 | } |
| 82 | int sockFd = std::get<0>(tcputil::accept(storeListenSocket_)); |
| 83 | sockets_.push_back(sockFd); |
Pieter Noordhuis | 7d0de4f | 2018-05-23 11:26:35 -0700 | [diff] [blame] | 84 | fds.push_back({.fd = sockFd, .events = POLLIN}); |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 85 | } |
| 86 | // The pipe receives an event which tells us to shutdown the daemon |
| 87 | if (fds[1].revents != 0) { |
| 88 | // Will be POLLUP when the pipe is closed |
| 89 | if (fds[1].revents ^ POLLHUP) { |
Pieter Noordhuis | 7d0de4f | 2018-05-23 11:26:35 -0700 | [diff] [blame] | 90 | throw std::system_error( |
| 91 | ECONNABORTED, |
| 92 | std::system_category(), |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 93 | "Unexpected poll revent on the control pipe's reading fd: " + |
Pieter Noordhuis | 7d0de4f | 2018-05-23 11:26:35 -0700 | [diff] [blame] | 94 | std::to_string(fds[1].revents)); |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 95 | } |
| 96 | finished = true; |
| 97 | break; |
| 98 | } |
| 99 | // Skipping the fds[0] and fds[1], |
| 100 | // fds[0] is master's listening socket |
| 101 | // fds[1] is control pipe's reading fd |
| 102 | for (size_t fdIdx = 2; fdIdx < fds.size(); ++fdIdx) { |
| 103 | if (fds[fdIdx].revents == 0) { |
| 104 | continue; |
| 105 | } |
| 106 | |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 107 | // Now query the socket that has the event |
| 108 | try { |
| 109 | query(fds[fdIdx].fd); |
| 110 | } catch (...) { |
| 111 | // There was an error when processing query. Probably an exception |
| 112 | // occurred in recv/send what would indicate that socket on the other |
| 113 | // side has been closed. If the closing was due to normal exit, then |
| 114 | // the store should continue executing. Otherwise, if it was different |
| 115 | // exception, other connections will get an exception once they try to |
| 116 | // use the store. We will go ahead and close this connection whenever |
| 117 | // we hit an exception here. |
| 118 | ::close(fds[fdIdx].fd); |
| 119 | |
| 120 | // Remove all the tracking state of the close FD |
Pieter Noordhuis | 7d0de4f | 2018-05-23 11:26:35 -0700 | [diff] [blame] | 121 | for (auto it = waitingSockets_.begin(); it != waitingSockets_.end();) { |
| 122 | for (auto vecIt = it->second.begin(); vecIt != it->second.end();) { |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 123 | if (*vecIt == fds[fdIdx].fd) { |
| 124 | vecIt = it->second.erase(vecIt); |
| 125 | } else { |
| 126 | ++vecIt; |
| 127 | } |
| 128 | } |
| 129 | if (it->second.size() == 0) { |
| 130 | it = waitingSockets_.erase(it); |
| 131 | } else { |
| 132 | ++it; |
| 133 | } |
| 134 | } |
Pieter Noordhuis | 7d0de4f | 2018-05-23 11:26:35 -0700 | [diff] [blame] | 135 | for (auto it = keysAwaited_.begin(); it != keysAwaited_.end();) { |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 136 | if (it->first == fds[fdIdx].fd) { |
| 137 | it = keysAwaited_.erase(it); |
| 138 | } else { |
| 139 | ++it; |
| 140 | } |
| 141 | } |
| 142 | fds.erase(fds.begin() + fdIdx); |
| 143 | sockets_.erase(sockets_.begin() + fdIdx - 2); |
| 144 | --fdIdx; |
| 145 | continue; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void TCPStoreDaemon::stop() { |
| 152 | if (controlPipeFd_[1] != -1) { |
| 153 | // close the write end of the pipe |
| 154 | ::close(controlPipeFd_[1]); |
| 155 | controlPipeFd_[1] = -1; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // query communicates with the worker. The format |
| 160 | // of the query is as follows: |
| 161 | // type of query | size of arg1 | arg1 | size of arg2 | arg2 | ... |
| 162 | // or, in the case of wait |
| 163 | // type of query | number of args | size of arg1 | arg1 | ... |
| 164 | void TCPStoreDaemon::query(int socket) { |
| 165 | QueryType qt; |
| 166 | tcputil::recvBytes<QueryType>(socket, &qt, 1); |
| 167 | |
| 168 | if (qt == QueryType::SET) { |
| 169 | setHandler(socket); |
| 170 | |
| 171 | } else if (qt == QueryType::ADD) { |
| 172 | addHandler(socket); |
| 173 | |
| 174 | } else if (qt == QueryType::GET) { |
| 175 | getHandler(socket); |
| 176 | |
| 177 | } else if (qt == QueryType::CHECK) { |
| 178 | checkHandler(socket); |
| 179 | |
| 180 | } else if (qt == QueryType::WAIT) { |
| 181 | waitHandler(socket); |
| 182 | |
| 183 | } else { |
| 184 | throw std::runtime_error("Unexpected query type"); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | void TCPStoreDaemon::wakeupWaitingClients(const std::string& key) { |
| 189 | auto socketsToWait = waitingSockets_.find(key); |
| 190 | if (socketsToWait != waitingSockets_.end()) { |
| 191 | for (int socket : socketsToWait->second) { |
| 192 | if (--keysAwaited_[socket] == 0) { |
Pieter Noordhuis | 7d0de4f | 2018-05-23 11:26:35 -0700 | [diff] [blame] | 193 | tcputil::sendValue<WaitResponseType>( |
| 194 | socket, WaitResponseType::STOP_WAITING); |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | waitingSockets_.erase(socketsToWait); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | void TCPStoreDaemon::setHandler(int socket) { |
| 202 | std::string key = tcputil::recvString(socket); |
| 203 | tcpStore_[key] = tcputil::recvVector<uint8_t>(socket); |
| 204 | // On "set", wake up all clients that have been waiting |
| 205 | wakeupWaitingClients(key); |
| 206 | } |
| 207 | |
| 208 | void TCPStoreDaemon::addHandler(int socket) { |
| 209 | std::string key = tcputil::recvString(socket); |
| 210 | int64_t addVal = tcputil::recvValue<int64_t>(socket); |
| 211 | |
| 212 | if (tcpStore_.find(key) != tcpStore_.end()) { |
| 213 | auto buf = reinterpret_cast<const char*>(tcpStore_[key].data()); |
| 214 | auto len = tcpStore_[key].size(); |
| 215 | addVal += std::stoll(std::string(buf, len)); |
| 216 | } |
| 217 | auto addValStr = std::to_string(addVal); |
| 218 | tcpStore_[key] = std::vector<uint8_t>(addValStr.begin(), addValStr.end()); |
| 219 | // Now send the new value |
| 220 | tcputil::sendValue<int64_t>(socket, addVal); |
| 221 | // On "add", wake up all clients that have been waiting |
| 222 | wakeupWaitingClients(key); |
| 223 | } |
| 224 | |
| 225 | void TCPStoreDaemon::getHandler(int socket) const { |
| 226 | std::string key = tcputil::recvString(socket); |
| 227 | auto data = tcpStore_.at(key); |
| 228 | tcputil::sendVector<uint8_t>(socket, data); |
| 229 | } |
| 230 | |
| 231 | void TCPStoreDaemon::checkHandler(int socket) const { |
| 232 | SizeType nargs; |
| 233 | tcputil::recvBytes<SizeType>(socket, &nargs, 1); |
| 234 | std::vector<std::string> keys(nargs); |
| 235 | for (size_t i = 0; i < nargs; i++) { |
| 236 | keys[i] = tcputil::recvString(socket); |
| 237 | } |
| 238 | // Now we have received all the keys |
| 239 | if (checkKeys(keys)) { |
| 240 | tcputil::sendValue<CheckResponseType>(socket, CheckResponseType::READY); |
| 241 | } else { |
| 242 | tcputil::sendValue<CheckResponseType>(socket, CheckResponseType::NOT_READY); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | void TCPStoreDaemon::waitHandler(int socket) { |
| 247 | SizeType nargs; |
| 248 | tcputil::recvBytes<SizeType>(socket, &nargs, 1); |
| 249 | std::vector<std::string> keys(nargs); |
| 250 | for (size_t i = 0; i < nargs; i++) { |
| 251 | keys[i] = tcputil::recvString(socket); |
| 252 | } |
| 253 | if (checkKeys(keys)) { |
Pieter Noordhuis | 7d0de4f | 2018-05-23 11:26:35 -0700 | [diff] [blame] | 254 | tcputil::sendValue<WaitResponseType>( |
| 255 | socket, WaitResponseType::STOP_WAITING); |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 256 | } else { |
| 257 | for (auto& key : keys) { |
| 258 | waitingSockets_[key].push_back(socket); |
| 259 | } |
| 260 | keysAwaited_[socket] = keys.size(); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | bool TCPStoreDaemon::checkKeys(const std::vector<std::string>& keys) const { |
Pieter Noordhuis | 7d0de4f | 2018-05-23 11:26:35 -0700 | [diff] [blame] | 265 | return std::all_of(keys.begin(), keys.end(), [this](const std::string& s) { |
| 266 | return tcpStore_.count(s) > 0; |
| 267 | }); |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | // TCPStore class methods |
Pieter Noordhuis | 7d0de4f | 2018-05-23 11:26:35 -0700 | [diff] [blame] | 271 | TCPStore::TCPStore( |
| 272 | const std::string& masterAddr, |
| 273 | PortType masterPort, |
Teng Li | b4bc55b | 2019-01-18 02:23:51 -0800 | [diff] [blame] | 274 | int numWorkers, |
Rohan Varma | f57ecd5 | 2019-09-24 12:34:20 -0700 | [diff] [blame] | 275 | bool isServer, |
| 276 | const std::chrono::milliseconds& timeout) |
| 277 | : Store(timeout), |
| 278 | isServer_(isServer), |
Pieter Noordhuis | 7d0de4f | 2018-05-23 11:26:35 -0700 | [diff] [blame] | 279 | tcpStoreAddr_(masterAddr), |
Teng Li | b4bc55b | 2019-01-18 02:23:51 -0800 | [diff] [blame] | 280 | tcpStorePort_(masterPort), |
| 281 | numWorkers_(numWorkers), |
| 282 | initKey_("init/"), |
| 283 | regularPrefix_("/") { |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 284 | if (isServer_) { |
| 285 | // Opening up the listening socket |
| 286 | std::tie(masterListenSocket_, std::ignore) = tcputil::listen(masterPort); |
| 287 | // Now start the daemon |
| 288 | tcpStoreDaemon_ = std::unique_ptr<TCPStoreDaemon>( |
Pieter Noordhuis | 7d0de4f | 2018-05-23 11:26:35 -0700 | [diff] [blame] | 289 | new TCPStoreDaemon(masterListenSocket_)); |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 290 | } |
| 291 | // Connect to the daemon |
Rohan Varma | f57ecd5 | 2019-09-24 12:34:20 -0700 | [diff] [blame] | 292 | storeSocket_ = tcputil::connect( |
| 293 | tcpStoreAddr_, tcpStorePort_, /* wait= */ true, timeout_); |
Teng Li | b4bc55b | 2019-01-18 02:23:51 -0800 | [diff] [blame] | 294 | |
| 295 | waitForWorkers_(); |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | TCPStore::~TCPStore() { |
| 299 | ::close(storeSocket_); |
| 300 | if (isServer_) { |
| 301 | // Store daemon should end because of closed connection. |
| 302 | // daemon destructor should join the thread |
| 303 | tcpStoreDaemon_.reset(nullptr); |
| 304 | ::close(masterListenSocket_); |
| 305 | } |
| 306 | } |
| 307 | |
Teng Li | b4bc55b | 2019-01-18 02:23:51 -0800 | [diff] [blame] | 308 | void TCPStore::waitForWorkers_() { |
| 309 | addHelper_(initKey_, 1); |
| 310 | // Let server block until all workers have completed, this ensures that |
| 311 | // the server daemon thread is always running until the very end |
| 312 | if (isServer_) { |
| 313 | const auto start = std::chrono::steady_clock::now(); |
| 314 | while (true) { |
| 315 | std::vector<uint8_t> value = getHelper_(initKey_); |
| 316 | auto buf = reinterpret_cast<const char*>(value.data()); |
| 317 | auto len = value.size(); |
| 318 | int numWorkersCompleted = std::stoi(std::string(buf, len)); |
| 319 | if (numWorkersCompleted >= numWorkers_) { |
| 320 | break; |
| 321 | } |
| 322 | const auto elapsed = std::chrono::duration_cast<std::chrono::seconds>( |
| 323 | std::chrono::steady_clock::now() - start); |
| 324 | if (timeout_ != kNoTimeout && elapsed > timeout_) { |
| 325 | break; |
| 326 | } |
| 327 | /* sleep override */ |
| 328 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 333 | void TCPStore::set(const std::string& key, const std::vector<uint8_t>& data) { |
Teng Li | b4bc55b | 2019-01-18 02:23:51 -0800 | [diff] [blame] | 334 | std::string regKey = regularPrefix_ + key; |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 335 | tcputil::sendValue<QueryType>(storeSocket_, QueryType::SET); |
Teng Li | b4bc55b | 2019-01-18 02:23:51 -0800 | [diff] [blame] | 336 | tcputil::sendString(storeSocket_, regKey, true); |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 337 | tcputil::sendVector<uint8_t>(storeSocket_, data); |
| 338 | } |
| 339 | |
| 340 | std::vector<uint8_t> TCPStore::get(const std::string& key) { |
Teng Li | b4bc55b | 2019-01-18 02:23:51 -0800 | [diff] [blame] | 341 | std::string regKey = regularPrefix_ + key; |
| 342 | return getHelper_(regKey); |
| 343 | } |
| 344 | |
| 345 | std::vector<uint8_t> TCPStore::getHelper_(const std::string& key) { |
| 346 | waitHelper_({key}, timeout_); |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 347 | tcputil::sendValue<QueryType>(storeSocket_, QueryType::GET); |
| 348 | tcputil::sendString(storeSocket_, key); |
| 349 | return tcputil::recvVector<uint8_t>(storeSocket_); |
| 350 | } |
| 351 | |
| 352 | int64_t TCPStore::add(const std::string& key, int64_t value) { |
Teng Li | b4bc55b | 2019-01-18 02:23:51 -0800 | [diff] [blame] | 353 | std::string regKey = regularPrefix_ + key; |
| 354 | return addHelper_(regKey, value); |
| 355 | } |
| 356 | |
| 357 | int64_t TCPStore::addHelper_(const std::string& key, int64_t value) { |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 358 | tcputil::sendValue<QueryType>(storeSocket_, QueryType::ADD); |
| 359 | tcputil::sendString(storeSocket_, key, true); |
| 360 | tcputil::sendValue<int64_t>(storeSocket_, value); |
| 361 | return tcputil::recvValue<int64_t>(storeSocket_); |
| 362 | } |
| 363 | |
| 364 | bool TCPStore::check(const std::vector<std::string>& keys) { |
| 365 | tcputil::sendValue<QueryType>(storeSocket_, QueryType::CHECK); |
| 366 | SizeType nkeys = keys.size(); |
| 367 | tcputil::sendBytes<SizeType>(storeSocket_, &nkeys, 1, (nkeys > 0)); |
| 368 | for (size_t i = 0; i < nkeys; i++) { |
Teng Li | b4bc55b | 2019-01-18 02:23:51 -0800 | [diff] [blame] | 369 | std::string regKey = regularPrefix_ + keys[i]; |
| 370 | tcputil::sendString(storeSocket_, regKey, (i != (nkeys - 1))); |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 371 | } |
| 372 | auto checkResponse = tcputil::recvValue<CheckResponseType>(storeSocket_); |
| 373 | if (checkResponse == CheckResponseType::READY) { |
| 374 | return true; |
| 375 | } else if (checkResponse == CheckResponseType::NOT_READY) { |
| 376 | return false; |
| 377 | } else { |
| 378 | throw std::runtime_error("ready or not_ready response expected"); |
| 379 | } |
| 380 | } |
| 381 | |
Teng Li | ec19512 | 2018-09-06 12:47:20 -0700 | [diff] [blame] | 382 | void TCPStore::wait(const std::vector<std::string>& keys) { |
| 383 | wait(keys, timeout_); |
| 384 | } |
| 385 | |
Pieter Noordhuis | 7d0de4f | 2018-05-23 11:26:35 -0700 | [diff] [blame] | 386 | void TCPStore::wait( |
| 387 | const std::vector<std::string>& keys, |
| 388 | const std::chrono::milliseconds& timeout) { |
Teng Li | b4bc55b | 2019-01-18 02:23:51 -0800 | [diff] [blame] | 389 | std::vector<std::string> regKeys; |
| 390 | regKeys.resize(keys.size()); |
| 391 | for (size_t i = 0; i < keys.size(); ++i) { |
| 392 | regKeys[i] = regularPrefix_ + keys[i]; |
| 393 | } |
| 394 | waitHelper_(regKeys, timeout); |
| 395 | } |
| 396 | |
| 397 | void TCPStore::waitHelper_( |
| 398 | const std::vector<std::string>& keys, |
| 399 | const std::chrono::milliseconds& timeout) { |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 400 | // Set the socket timeout if there is a wait timeout |
| 401 | if (timeout != kNoTimeout) { |
| 402 | struct timeval timeoutTV = {.tv_sec = timeout.count() / 1000, |
| 403 | .tv_usec = (timeout.count() % 1000) * 1000}; |
SsnL | 774705b | 2019-01-14 15:59:29 -0800 | [diff] [blame] | 404 | SYSCHECK_ERR_RETURN_NEG1(::setsockopt( |
Pieter Noordhuis | 7d0de4f | 2018-05-23 11:26:35 -0700 | [diff] [blame] | 405 | storeSocket_, |
| 406 | SOL_SOCKET, |
| 407 | SO_RCVTIMEO, |
| 408 | reinterpret_cast<char*>(&timeoutTV), |
| 409 | sizeof(timeoutTV))); |
Teng Li | 0d27d26 | 2018-05-17 13:38:06 -0700 | [diff] [blame] | 410 | } |
| 411 | tcputil::sendValue<QueryType>(storeSocket_, QueryType::WAIT); |
| 412 | SizeType nkeys = keys.size(); |
| 413 | tcputil::sendBytes<SizeType>(storeSocket_, &nkeys, 1, (nkeys > 0)); |
| 414 | for (size_t i = 0; i < nkeys; i++) { |
| 415 | tcputil::sendString(storeSocket_, keys[i], (i != (nkeys - 1))); |
| 416 | } |
| 417 | auto waitResponse = tcputil::recvValue<WaitResponseType>(storeSocket_); |
| 418 | if (waitResponse != WaitResponseType::STOP_WAITING) { |
| 419 | throw std::runtime_error("Stop_waiting response is expected"); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | } // namespace c10d |