Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit 54b02a4

Browse files
Nov11pervazea
authored andcommitted
Remove retry on close failure (#1271)
* remove retrying on close failing with EINTR * give more specific debug message when close fails * add close$NOCANCEL declaration * fix compile error * ignore strerror_r's return type which differs from mac to linux * see if it will compile without __DARWIN_NON_CANCELABLE * strerror's return value cannot be ignore by casting to (void). so just use it. * remove redundant semicolon * deal with unused variable warning * refactory: extract close method into library utility * fix include format * fix compile error * erase default parameter & add comments * remove retry on closing listening socket * remove redundant logging when closing server
1 parent 87efb13 commit 54b02a4

File tree

5 files changed

+104
-24
lines changed

5 files changed

+104
-24
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ if(UNIX OR APPLE)
7777
if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
7878
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-tautological-undefined-compare -Wno-unused-private-field -Wno-braced-scalar-init -Wno-constant-conversion -Wno-potentially-evaluated-expression -Wno-infinite-recursion -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS -I/usr/local/opt/openssl/include")
7979
endif()
80+
# ---[ close$NOCANCEL for Mac OS
81+
if(APPLE)
82+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-dollar-in-identifier-extension")
83+
endif()
8084
endif()
8185

8286
# There is a problem with building on g++5.4 on Ubuntu 17.10 where

src/common/utility.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// utility.cpp
6+
//
7+
// Identification: src/include/common/utility.h
8+
//
9+
// Copyright (c) 2015-17, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include <unistd.h>
14+
#include <vector>
15+
#include <string.h>
16+
#include <errno.h>
17+
18+
#include "common/logger.h"
19+
#include "common/utility.h"
20+
#if __APPLE__
21+
extern "C"{
22+
#include <sys/cdefs.h>
23+
int close$NOCANCEL(int);
24+
};
25+
#endif
26+
27+
namespace peloton {
28+
29+
int peloton_close(int fd) {
30+
//On Mac OS, close$NOCANCEL guarantees that no descriptor leak & no need to retry on failure.
31+
//On linux, close will do the same.
32+
//In short, call close/close$NOCANCEL once and consider it done. AND NEVER RETRY ON FAILURE.
33+
//The errno code is just a hint. It's logged but no further processing on it.
34+
//Retry on failure may close another file descriptor that just has been assigned by OS with the same number
35+
//and break assumptions of other threads.
36+
37+
int close_ret = -1;
38+
#if __APPLE__
39+
close_ret = ::close$NOCANCEL(fd);
40+
#else
41+
close_ret = close(fd);
42+
#endif
43+
44+
if (close_ret != 0) {
45+
auto error_message = peloton_error_message();
46+
LOG_DEBUG("Close failed on fd: %d, errno: %d [%s]", fd, errno, error_message.c_str());
47+
}
48+
49+
return close_ret;
50+
}
51+
52+
std::string peloton_error_message() {
53+
std::vector<char> buffer(100, '\0');
54+
int saved_errno = errno;
55+
char *error_message = nullptr;
56+
#if __APPLE__
57+
(void)strerror_r(errno, buffer.data(), buffer.size() - 1);
58+
error_message = buffer.data();
59+
#else
60+
error_message = strerror_r(saved_errno, buffer.data(), buffer.size() - 1);
61+
#endif
62+
63+
errno = saved_errno;
64+
return std::string(error_message);
65+
}
66+
}

src/include/common/utility.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// utility.h
6+
//
7+
// Identification: src/include/common/utility.h
8+
//
9+
// Copyright (c) 2015-16, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#pragma once
14+
#include <string>
15+
16+
namespace peloton{
17+
18+
int peloton_close(int fd);
19+
20+
std::string peloton_error_message();
21+
}

src/network/connection_handle.cpp

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "network/protocol_handler_factory.h"
2121

2222
#include "settings/settings_manager.h"
23+
#include "common/utility.h"
2324

2425
namespace peloton {
2526
namespace network {
@@ -89,7 +90,7 @@ DEF_TRANSITION_GRAPH
8990
ON(WAKEUP) SET_STATE_TO(READ) AND_INVOKE(FillReadBuffer)
9091
ON(PROCEED) SET_STATE_TO(PROCESS) AND_INVOKE(Process)
9192
ON(NEED_DATA) SET_STATE_TO(READ) AND_WAIT
92-
ON(FINISH) SET_STATE_TO(CLOSING) AND_INVOKE(CloseSocket)
93+
ON(FINISH) SET_STATE_TO(CLOSING) AND_INVOKE(CloseSocket)
9394
END_DEF
9495

9596
DEFINE_STATE(PROCESS_WRITE_SSL_HANDSHAKE)
@@ -99,23 +100,23 @@ DEF_TRANSITION_GRAPH
99100
ON(FINISH) SET_STATE_TO(CLOSING) AND_INVOKE(CloseSocket)
100101
ON(PROCEED) SET_STATE_TO(PROCESS) AND_INVOKE(Process)
101102
END_DEF
102-
103-
DEFINE_STATE(PROCESS)
103+
104+
DEFINE_STATE(PROCESS)
104105
ON(PROCEED) SET_STATE_TO(WRITE) AND_INVOKE(ProcessWrite)
105106
ON(NEED_DATA) SET_STATE_TO(READ) AND_INVOKE(FillReadBuffer)
106107
ON(GET_RESULT) SET_STATE_TO(GET_RESULT) AND_WAIT
107108
ON(FINISH) SET_STATE_TO(CLOSING) AND_INVOKE(CloseSocket)
108-
ON(NEED_SSL_HANDSHAKE) SET_STATE_TO(PROCESS_WRITE_SSL_HANDSHAKE)
109+
ON(NEED_SSL_HANDSHAKE) SET_STATE_TO(PROCESS_WRITE_SSL_HANDSHAKE)
109110
AND_INVOKE(ProcessWrite_SSLHandshake)
110111
END_DEF
111112

112-
DEFINE_STATE(WRITE)
113+
DEFINE_STATE(WRITE)
113114
ON(WAKEUP) SET_STATE_TO(WRITE) AND_INVOKE(ProcessWrite)
114115
ON(NEED_DATA) SET_STATE_TO(PROCESS) AND_INVOKE(Process)
115116
ON(PROCEED) SET_STATE_TO(PROCESS) AND_INVOKE(Process)
116117
END_DEF
117118

118-
DEFINE_STATE(GET_RESULT)
119+
DEFINE_STATE(GET_RESULT)
119120
ON(WAKEUP) SET_STATE_TO(GET_RESULT) AND_INVOKE(GetResult)
120121
ON(PROCEED) SET_STATE_TO(WRITE) AND_INVOKE(ProcessWrite)
121122
END_DEF
@@ -557,18 +558,9 @@ Transition ConnectionHandle::CloseSocket() {
557558
conn_SSL_context = nullptr;
558559
}
559560

560-
while (true) {
561-
int status = close(sock_fd_);
562-
if (status < 0) {
563-
// failed close
564-
if (errno == EINTR) {
565-
// interrupted, try closing again
566-
continue;
567-
}
568-
}
569-
LOG_DEBUG("Already Closed the connection %d", sock_fd_);
570-
return Transition::NONE;
571-
}
561+
peloton_close(sock_fd_);
562+
return Transition::NONE;
563+
572564
}
573565

574566
Transition ConnectionHandle::ProcessWrite_SSLHandshake() {

src/network/peloton_server.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include <fstream>
1515
#include <memory>
16+
#include "common/utility.h"
1617
#include "event2/thread.h"
1718

1819
#include "common/dedicated_thread_registry.h"
@@ -276,12 +277,8 @@ void PelotonServer::ServerLoop() {
276277
.RegisterDedicatedThread<PelotonRpcHandlerTask>(this, rpc_task);
277278
}
278279
dispatcher_task_->EventLoop();
279-
LOG_INFO("Closing server");
280-
int status;
281-
do {
282-
status = close(listen_fd_);
283-
} while (status < 0 && errno == EINTR);
284-
LOG_DEBUG("Already Closed the connection %d", listen_fd_);
280+
281+
peloton_close(listen_fd_);
285282

286283
LOG_INFO("Server Closed");
287284
}

0 commit comments

Comments
 (0)