Merge pull request from liutongxuan:features/reuse_port_option

PiperOrigin-RevId: 310392434
Change-Id: Ia7304812ec321085dc0a9f2fd408d386a0244021
This commit is contained in:
TensorFlower Gardener 2020-05-07 10:50:13 -07:00
commit 7cfa63117f

View File

@ -70,6 +70,18 @@ class NoReusePortOption : public ::grpc::ServerBuilderOption {
plugins) override {}
};
// Define an option subclass in order to enable SO_REUSEPORT for the
// server socket.
class ReusePortOption : public ::grpc::ServerBuilderOption {
public:
void UpdateArguments(::grpc::ChannelArguments* args) override {
args->SetInt(GRPC_ARG_ALLOW_REUSEPORT, 1);
}
void UpdatePlugins(std::vector<std::unique_ptr<::grpc::ServerBuilderPlugin>>*
plugins) override {}
};
// static utility function
RendezvousMgrInterface* NewRpcRendezvousMgr(const WorkerEnv* env) {
return new RpcRendezvousMgr(env);
@ -220,8 +232,18 @@ Status GrpcServer::Init(const GrpcServerOptions& opts) {
GetServerCredentials(server_def_), &bound_port_);
builder.SetMaxMessageSize(std::numeric_limits<int32>::max());
builder.SetOption(
std::unique_ptr<::grpc::ServerBuilderOption>(new NoReusePortOption));
bool reuse_port = false;
const Status status =
ReadBoolFromEnvVar("TF_GRPC_REUSE_PORT", false, &reuse_port);
if (!status.ok()) {
LOG(ERROR) << status.error_message();
}
auto server_build_option =
reuse_port
? std::unique_ptr<::grpc::ServerBuilderOption>(new ReusePortOption)
: std::unique_ptr<::grpc::ServerBuilderOption>(new NoReusePortOption);
builder.SetOption(std::move(server_build_option));
// Allow subclasses to specify more args to pass to the gRPC server.
MaybeMutateBuilder(&builder);
master_impl_ = CreateMaster(&master_env_);