szetszwo commented on code in PR #1466:
URL: https://github.com/apache/ratis/pull/1466#discussion_r3276156221
##########
ratis-grpc/src/main/java/org/apache/ratis/grpc/GrpcConfigKeys.java:
##########
@@ -155,6 +155,22 @@ static GrpcTlsConfig tlsConf(Parameters parameters) {
static void setTlsConf(Parameters parameters, GrpcTlsConfig conf) {
parameters.put(TLS_CONF_PARAMETER, conf, TLS_CONF_CLASS);
}
+
+ /**
+ * The number of worker threads for the gRPC client-side {@link
+ * org.apache.ratis.thirdparty.io.netty.channel.EventLoopGroup}.
+ * 0 (default) means use the gRPC default (i.e. {@code availableProcessors
* 2});
+ * a positive value caps the worker event-loop thread count.
+ */
+ String WORKER_EVENT_LOOP_THREADS_KEY = PREFIX +
".worker.event-loop.threads";
Review Comment:
In NettyConfigKeys, we have the following conf:
```
key: raft.netty.dataStream.client.use-epoll (boolean, default=true)
key: raft.netty.dataStream.client.worker-group.size (int, default=28)
```
Let's use the same similar conf for gRPC.
##########
ratis-grpc/src/main/java/org/apache/ratis/grpc/server/GrpcServicesImpl.java:
##########
@@ -234,6 +242,24 @@ Server newServer(GrpcClientProtocolService client,
ServerInterceptor interceptor
return buildServer(serverBuilder, types);
}
+ private EventLoopGroup bossEventLoopGroup;
+ private EventLoopGroup workerEventLoopGroup;
+
+ EventLoopGroup getBossEventLoopGroup() {
+ if (workerEventLoopThreads > 0 && bossEventLoopGroup == null) {
+ bossEventLoopGroup = GrpcEventLoops.newEventLoopGroup(1,
server.getId() + "-grpc-boss-ELG");
Review Comment:
Let's make bossEventLoopThreads configurable?
##########
ratis-grpc/src/main/java/org/apache/ratis/grpc/server/GrpcServicesImpl.java:
##########
@@ -274,20 +300,27 @@ public static Builder newBuilder() {
private final GrpcClientProtocolService clientProtocolService;
private final MetricServerInterceptor serverInterceptor;
+ private final EventLoopGroup bossEventLoopGroup;
+ private final EventLoopGroup workerEventLoopGroup;
private GrpcServicesImpl(Builder b) {
- super(b.server::getId, id -> new PeerProxyMap<>(id.toString(),
b::newGrpcServerProtocolClient));
+ super(b.server::getId, id -> new PeerProxyMap<>(id.toString(),
+ peer -> b.newGrpcServerProtocolClient(peer,
b.getWorkerEventLoopGroup())));
Review Comment:
We should create only one group for all clients.
##########
ratis-grpc/src/main/java/org/apache/ratis/grpc/server/GrpcServicesImpl.java:
##########
@@ -375,6 +408,9 @@ public void closeImpl() {
} catch (IOException e) {
LOG.warn("{}: Failed to close proxies", getId(), e);
}
+
+ GrpcEventLoops.shutdownGracefully(workerEventLoopGroup);
+ GrpcEventLoops.shutdownGracefully(bossEventLoopGroup);
Review Comment:
We should call shutdownGracefully() for all groups and then await(..)
##########
ratis-grpc/src/main/java/org/apache/ratis/grpc/server/GrpcServicesImpl.java:
##########
@@ -274,20 +300,27 @@ public static Builder newBuilder() {
private final GrpcClientProtocolService clientProtocolService;
private final MetricServerInterceptor serverInterceptor;
+ private final EventLoopGroup bossEventLoopGroup;
+ private final EventLoopGroup workerEventLoopGroup;
Review Comment:
We also need a group for client. BTW, let's use shorter names.
```java
private EventLoopGroup serverBosses;
private EventLoopGroup serverWorkers;
private EventLoopGroup clientWorkers;
```
##########
ratis-grpc/src/main/java/org/apache/ratis/grpc/GrpcEventLoops.java:
##########
@@ -0,0 +1,122 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ratis.grpc;
+
+import org.apache.ratis.thirdparty.io.grpc.netty.NettyChannelBuilder;
+import org.apache.ratis.thirdparty.io.grpc.netty.NettyServerBuilder;
+import org.apache.ratis.thirdparty.io.netty.channel.Channel;
+import org.apache.ratis.thirdparty.io.netty.channel.EventLoopGroup;
+import org.apache.ratis.thirdparty.io.netty.channel.ServerChannel;
+import org.apache.ratis.thirdparty.io.netty.channel.epoll.Epoll;
+import org.apache.ratis.thirdparty.io.netty.channel.epoll.EpollEventLoopGroup;
+import
org.apache.ratis.thirdparty.io.netty.channel.epoll.EpollServerSocketChannel;
+import org.apache.ratis.thirdparty.io.netty.channel.epoll.EpollSocketChannel;
+import org.apache.ratis.thirdparty.io.netty.channel.nio.NioEventLoopGroup;
+import
org.apache.ratis.thirdparty.io.netty.channel.socket.nio.NioServerSocketChannel;
+import
org.apache.ratis.thirdparty.io.netty.channel.socket.nio.NioSocketChannel;
+import
org.apache.ratis.thirdparty.io.netty.util.concurrent.DefaultThreadFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Helper for creating bounded Netty {@link EventLoopGroup} instances for gRPC,
+ * along with matching {@link Channel} / {@link ServerChannel} types.
+ *
+ * <p>By default, gRPC uses a shared {@link EventLoopGroup} sized to
+ * {@code Runtime.getRuntime().availableProcessors() * 2}, and Netty threads
+ * in that group never exit once started. A traffic burst (e.g. a follower
+ * catch-up after restart) can permanently inflate the active thread count.
+ * This helper lets callers construct a smaller, dedicated group and wire it
+ * into {@link NettyServerBuilder} / {@link NettyChannelBuilder} consistently
+ * with the matching channel type.
+ */
+public final class GrpcEventLoops {
Review Comment:
Let's move NettyUtils to ratis-common and then add the new methods there.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]