Skip to content

Commit c1bf966

Browse files
authored
Add options to disable SO_REUSEPORT of grpc. (#14269)
1 parent 60d25bf commit c1bf966

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

paddle/fluid/operators/distributed/grpc_client.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ limitations under the License. */
2222
#include "paddle/fluid/operators/distributed/request_handler.h"
2323
#include "paddle/fluid/platform/profiler.h"
2424

25+
DECLARE_bool(rpc_disable_reuse_port);
26+
2527
namespace paddle {
2628
namespace operators {
2729
namespace distributed {
@@ -383,6 +385,9 @@ std::shared_ptr<grpc::Channel> GRPCClient::GetChannel(const std::string& ep) {
383385
// Channel configurations:
384386
grpc::ChannelArguments args;
385387
args.SetInt(GRPC_ARG_MAX_RECONNECT_BACKOFF_MS, 2000);
388+
if (FLAGS_rpc_disable_reuse_port) {
389+
args.SetInt(GRPC_ARG_ALLOW_REUSEPORT, 0);
390+
}
386391
args.SetCompressionAlgorithm(GRPC_COMPRESS_NONE);
387392
args.SetMaxSendMessageSize(std::numeric_limits<int>::max());
388393
args.SetMaxReceiveMessageSize(std::numeric_limits<int>::max());

paddle/fluid/operators/distributed/grpc_server.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ limitations under the License. */
2020

2121
using ::grpc::ServerAsyncResponseWriter;
2222

23+
DECLARE_bool(rpc_disable_reuse_port);
24+
2325
namespace paddle {
2426
namespace operators {
2527
namespace distributed {
@@ -252,13 +254,31 @@ void AsyncGRPCServer::WaitServerReady() {
252254
VLOG(40) << "AsyncGRPCServer WaitSeverReady";
253255
}
254256

257+
// Define an option subclass in order to disable SO_REUSEPORT for the
258+
// server socket.
259+
// Come from:
260+
// https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/distributed_runtime/rpc/grpc_server_lib.cc
261+
class NoReusePortOption : public ::grpc::ServerBuilderOption {
262+
public:
263+
void UpdateArguments(::grpc::ChannelArguments* args) override {
264+
args->SetInt(GRPC_ARG_ALLOW_REUSEPORT, 0);
265+
}
266+
267+
void UpdatePlugins(std::vector<std::unique_ptr<::grpc::ServerBuilderPlugin>>*
268+
plugins) override {}
269+
};
270+
255271
void AsyncGRPCServer::StartServer() {
256272
::grpc::ServerBuilder builder;
257273
builder.AddListeningPort(bind_address_, ::grpc::InsecureServerCredentials(),
258274
&selected_port_);
259275

260276
builder.SetMaxSendMessageSize(std::numeric_limits<int>::max());
261277
builder.SetMaxReceiveMessageSize(std::numeric_limits<int>::max());
278+
if (FLAGS_rpc_disable_reuse_port) {
279+
builder.SetOption(
280+
std::unique_ptr<::grpc::ServerBuilderOption>(new NoReusePortOption));
281+
}
262282
builder.RegisterService(&service_);
263283

264284
for (auto t : rpc_call_map_) {

paddle/fluid/operators/distributed/sendrecvop_utils.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ limitations under the License. */
2222
#include "paddle/fluid/operators/distributed/sendrecvop_utils.h"
2323
#include "paddle/fluid/operators/distributed/variable_response.h"
2424

25+
DEFINE_bool(rpc_disable_reuse_port, false, "Disable SO_REUSEPORT or not.");
26+
2527
namespace paddle {
2628
namespace operators {
2729
namespace distributed {

python/paddle/fluid/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ def __bootstrap__():
129129
read_env_flags.append('rpc_send_thread_num')
130130
read_env_flags.append('rpc_get_thread_num')
131131
read_env_flags.append('rpc_prefetch_thread_num')
132+
read_env_flags.append('rpc_disable_reuse_port')
132133

133134
if core.is_compiled_with_cuda():
134135
read_env_flags += [

0 commit comments

Comments
 (0)