22
33#include < poll.h>
44#include < unistd.h>
5- #include < system_error>
65#include < algorithm>
6+ #include < system_error>
77
88namespace c10d {
99
1010namespace {
1111
12- enum class QueryType : uint8_t {
13- SET,
14- GET,
15- ADD,
16- CHECK,
17- WAIT
18- };
12+ enum class QueryType : uint8_t { SET, GET, ADD, CHECK, WAIT };
1913
20- enum class CheckResponseType : uint8_t {
21- READY,
22- NOT_READY
23- };
14+ enum class CheckResponseType : uint8_t { READY, NOT_READY };
2415
25- enum class WaitResponseType : uint8_t {
26- STOP_WAITING
27- };
16+ enum class WaitResponseType : uint8_t { STOP_WAITING };
2817
2918} // anonymous namespace
3019
3120// TCPStoreDaemon class methods
3221// Simply start the daemon thread
33- TCPStoreDaemon::TCPStoreDaemon (int storeListenSocket) :
34- storeListenSocket_ (storeListenSocket)
35- {
22+ TCPStoreDaemon::TCPStoreDaemon (int storeListenSocket)
23+ : storeListenSocket_(storeListenSocket) {
3624 daemonThread_ = std::thread (&TCPStoreDaemon::run, this );
3725}
3826
@@ -62,14 +50,15 @@ void TCPStoreDaemon::join() {
6250void TCPStoreDaemon::run () {
6351 // Create the control pipe
6452 if (pipe (controlPipeFd_.data ()) == -1 ) {
65- throw std::runtime_error (" Failed to create the control pipe to start the "
66- " TCPStoreDaemon run" );
53+ throw std::runtime_error (
54+ " Failed to create the control pipe to start the "
55+ " TCPStoreDaemon run" );
6756 }
6857
6958 std::vector<struct pollfd > fds;
70- fds.push_back ({ .fd = storeListenSocket_, .events = POLLIN });
59+ fds.push_back ({.fd = storeListenSocket_, .events = POLLIN});
7160 // Push the read end of the pipe to signal the stopping of the daemon run
72- fds.push_back ({ .fd = controlPipeFd_[0 ], .events = POLLHUP });
61+ fds.push_back ({.fd = controlPipeFd_[0 ], .events = POLLHUP});
7362
7463 // receive the queries
7564 bool finished = false ;
@@ -84,21 +73,25 @@ void TCPStoreDaemon::run() {
8473 // accept new connections.
8574 if (fds[0 ].revents != 0 ) {
8675 if (fds[0 ].revents ^ POLLIN) {
87- throw std::system_error (ECONNABORTED, std::system_category (),
76+ throw std::system_error (
77+ ECONNABORTED,
78+ std::system_category (),
8879 " Unexpected poll revent on the master's listening socket: " +
89- std::to_string (fds[0 ].revents ));
80+ std::to_string (fds[0 ].revents ));
9081 }
9182 int sockFd = std::get<0 >(tcputil::accept (storeListenSocket_));
9283 sockets_.push_back (sockFd);
93- fds.push_back ({ .fd = sockFd, .events = POLLIN });
84+ fds.push_back ({.fd = sockFd, .events = POLLIN});
9485 }
9586 // The pipe receives an event which tells us to shutdown the daemon
9687 if (fds[1 ].revents != 0 ) {
9788 // Will be POLLUP when the pipe is closed
9889 if (fds[1 ].revents ^ POLLHUP) {
99- throw std::system_error (ECONNABORTED, std::system_category (),
90+ throw std::system_error (
91+ ECONNABORTED,
92+ std::system_category (),
10093 " Unexpected poll revent on the control pipe's reading fd: " +
101- std::to_string (fds[1 ].revents ));
94+ std::to_string (fds[1 ].revents ));
10295 }
10396 finished = true ;
10497 break ;
@@ -112,10 +105,11 @@ void TCPStoreDaemon::run() {
112105 }
113106
114107 if (fds[fdIdx].revents ^ POLLIN) {
115- throw std::system_error (ECONNABORTED, std::system_category (),
116- " Unexpected poll revent: " +
117- std::to_string (fds[fdIdx].revents ) + " on socket: " +
118- std::to_string (fds[fdIdx].fd ));
108+ throw std::system_error (
109+ ECONNABORTED,
110+ std::system_category (),
111+ " Unexpected poll revent: " + std::to_string (fds[fdIdx].revents ) +
112+ " on socket: " + std::to_string (fds[fdIdx].fd ));
119113 }
120114 // Now query the socket that has the event
121115 try {
@@ -131,8 +125,8 @@ void TCPStoreDaemon::run() {
131125 ::close (fds[fdIdx].fd);
132126
133127 // Remove all the tracking state of the close FD
134- for (auto it = waitingSockets_.begin (); it != waitingSockets_.end (); ) {
135- for (auto vecIt = it->second .begin (); vecIt != it->second .end (); ) {
128+ for (auto it = waitingSockets_.begin (); it != waitingSockets_.end ();) {
129+ for (auto vecIt = it->second .begin (); vecIt != it->second .end ();) {
136130 if (*vecIt == fds[fdIdx].fd ) {
137131 vecIt = it->second .erase (vecIt);
138132 } else {
@@ -145,7 +139,7 @@ void TCPStoreDaemon::run() {
145139 ++it;
146140 }
147141 }
148- for (auto it = keysAwaited_.begin (); it != keysAwaited_.end (); ) {
142+ for (auto it = keysAwaited_.begin (); it != keysAwaited_.end ();) {
149143 if (it->first == fds[fdIdx].fd ) {
150144 it = keysAwaited_.erase (it);
151145 } else {
@@ -203,8 +197,8 @@ void TCPStoreDaemon::wakeupWaitingClients(const std::string& key) {
203197 if (socketsToWait != waitingSockets_.end ()) {
204198 for (int socket : socketsToWait->second ) {
205199 if (--keysAwaited_[socket] == 0 ) {
206- tcputil::sendValue<WaitResponseType>(socket,
207- WaitResponseType::STOP_WAITING);
200+ tcputil::sendValue<WaitResponseType>(
201+ socket, WaitResponseType::STOP_WAITING);
208202 }
209203 }
210204 waitingSockets_.erase (socketsToWait);
@@ -264,8 +258,8 @@ void TCPStoreDaemon::waitHandler(int socket) {
264258 keys[i] = tcputil::recvString (socket);
265259 }
266260 if (checkKeys (keys)) {
267- tcputil::sendValue<WaitResponseType>(socket,
268- WaitResponseType::STOP_WAITING);
261+ tcputil::sendValue<WaitResponseType>(
262+ socket, WaitResponseType::STOP_WAITING);
269263 } else {
270264 for (auto & key : keys) {
271265 waitingSockets_[key].push_back (socket);
@@ -275,27 +269,25 @@ void TCPStoreDaemon::waitHandler(int socket) {
275269}
276270
277271bool TCPStoreDaemon::checkKeys (const std::vector<std::string>& keys) const {
278- return std::all_of (keys.begin (), keys.end (),
279- [this ](const std::string& s) {
280- return tcpStore_.count (s) > 0 ;
281- });
272+ return std::all_of (keys.begin (), keys.end (), [this ](const std::string& s) {
273+ return tcpStore_.count (s) > 0 ;
274+ });
282275}
283276
284277// TCPStore class methods
285- TCPStore::TCPStore (const std::string& masterAddr,
286- PortType masterPort ,
287- bool isServer)
288- : isServer_( isServer)
289- , tcpStoreAddr_(masterAddr)
290- , tcpStorePort_(masterPort)
291- {
278+ TCPStore::TCPStore (
279+ const std::string& masterAddr ,
280+ PortType masterPort,
281+ bool isServer)
282+ : isServer_(isServer),
283+ tcpStoreAddr_ (masterAddr),
284+ tcpStorePort_(masterPort) {
292285 if (isServer_) {
293286 // Opening up the listening socket
294287 std::tie (masterListenSocket_, std::ignore) = tcputil::listen (masterPort);
295288 // Now start the daemon
296289 tcpStoreDaemon_ = std::unique_ptr<TCPStoreDaemon>(
297- new TCPStoreDaemon (masterListenSocket_)
298- );
290+ new TCPStoreDaemon (masterListenSocket_));
299291 }
300292 // Connect to the daemon
301293 storeSocket_ = tcputil::connect (tcpStoreAddr_, tcpStorePort_);
@@ -348,17 +340,19 @@ bool TCPStore::check(const std::vector<std::string>& keys) {
348340 }
349341}
350342
351- void TCPStore::wait (const std::vector<std::string>& keys,
352- const std::chrono::milliseconds& timeout) {
343+ void TCPStore::wait (
344+ const std::vector<std::string>& keys,
345+ const std::chrono::milliseconds& timeout) {
353346 // Set the socket timeout if there is a wait timeout
354347 if (timeout != kNoTimeout ) {
355348 struct timeval timeoutTV = {.tv_sec = timeout.count () / 1000 ,
356349 .tv_usec = (timeout.count () % 1000 ) * 1000 };
357- SYSCHECK (::setsockopt (storeSocket_,
358- SOL_SOCKET,
359- SO_RCVTIMEO,
360- reinterpret_cast <char *>(&timeoutTV),
361- sizeof (timeoutTV)));
350+ SYSCHECK (::setsockopt (
351+ storeSocket_,
352+ SOL_SOCKET,
353+ SO_RCVTIMEO,
354+ reinterpret_cast <char *>(&timeoutTV),
355+ sizeof (timeoutTV)));
362356 }
363357 tcputil::sendValue<QueryType>(storeSocket_, QueryType::WAIT);
364358 SizeType nkeys = keys.size ();
0 commit comments