amaliujia commented on code in PR #1679:
URL: https://github.com/apache/ratis/pull/1679#discussion_r4462900718


##########
ratis-grpc/src/main/java/org/apache/ratis/grpc/server/GrpcServerProtocolClient.java:
##########
@@ -191,6 +228,7 @@ public void resetConnectBackoff() {
     if (useSeparateHBChannel) {
       hbChannel.resetConnectBackoff();
     }
-    channel.resetConnectBackoff();
+    // the first one is the main channel
+    appendEntriesChannels.forEach(ManagedChannel::resetConnectBackoff);

Review Comment:
   ditto. Can we do in-parallel reset in case there is 1024 channels?



##########
ratis-grpc/src/main/java/org/apache/ratis/grpc/server/GrpcServerProtocolClient.java:
##########
@@ -109,7 +129,8 @@ public void close() {
     if (useSeparateHBChannel) {
       GrpcUtil.shutdownManagedChannel(hbChannel);
     }
-    GrpcUtil.shutdownManagedChannel(channel);
+    // the first one is the main channel
+    appendEntriesChannels.forEach(GrpcUtil::shutdownManagedChannel);

Review Comment:
   Can we have in-parallel shutdown here in case the channel number is large?



##########
ratis-test/src/test/java/org/apache/ratis/grpc/server/TestGrpcLogAppenderConnections.java:
##########
@@ -0,0 +1,65 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.server;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.apache.ratis.BaseTest;
+import org.apache.ratis.client.RaftClient;
+import org.apache.ratis.conf.RaftProperties;
+import org.apache.ratis.grpc.GrpcConfigKeys;
+import org.apache.ratis.grpc.MiniRaftClusterWithGrpc;
+import org.apache.ratis.protocol.RaftGroup;
+import org.apache.ratis.protocol.RaftPeer;
+import org.apache.ratis.server.impl.GroupManagementBaseTest;
+import org.apache.ratis.server.impl.MiniRaftCluster;
+import org.apache.ratis.server.RaftServer;
+import org.apache.ratis.RaftTestUtil.SimpleMessage;
+import org.apache.ratis.util.function.CheckedBiConsumer;
+import org.junit.jupiter.api.Test;
+
+/** Several groups on the same servers replicate over more than one 
appendEntries connection per peer. */
+public class TestGrpcLogAppenderConnections extends BaseTest {
+  private static final int CONNECTIONS = 3;
+
+  @Test
+  public void testMultiGroupWithLogAppenderConnections() throws Exception {
+    final RaftProperties properties = new RaftProperties();
+    GrpcConfigKeys.Server.setLogAppenderConnections(properties, CONNECTIONS);
+    final MiniRaftCluster cluster = 
MiniRaftClusterWithGrpc.FACTORY.newCluster(0, properties);
+
+    final CheckedBiConsumer<MiniRaftCluster, RaftGroup, Exception> checker = 
(c, group) -> {
+      for (RaftPeer peer : group.getPeers()) {
+        final RaftServer server = c.getServer(peer.getId());
+        final GrpcServicesImpl rpc = (GrpcServicesImpl) server.getServerRpc();
+        for (RaftPeer other : group.getPeers()) {
+          if (!other.getId().equals(peer.getId())) {
+            assertEquals(CONNECTIONS, 
rpc.getProxies().getProxy(other.getId()).getAppendEntriesChannels().size());
+          }
+        }
+      }
+      try (RaftClient client = c.createClient(group)) {
+        for (int i = 0; i < 10; i++) {
+          assertTrue(client.io().send(new SimpleMessage(group.getGroupId() + 
"-" + i)).isSuccess());
+        }
+      }
+    };
+    GroupManagementBaseTest.runMultiGroupTest(cluster, new int[] {3, 6, 9}, 
-1, checker);

Review Comment:
   It seems to me that this cluster creation won't create the groups and 
servers we want.
   
   The creation code is
   ```
         final RaftGroup[] groups = new RaftGroup[idIndex.length];
         for (int i = 0; i < idIndex.length; i++) {
           final RaftGroupId gid = RaftGroupId.randomId();
           final int previous = i == 0 ? 0 : idIndex[i - 1];
           final RaftPeer[] peers = allPeers.subList(previous, 
idIndex[i]).toArray(RaftPeer.emptyArray());
           groups[i] = RaftGroup.valueOf(gid, peers);
   ```,
   
   and each group has 3 adjacent peers and all 3 groups has non-overlapping 
peers by the `{3, 6, 9}` setup.
   
   There seems no such helper existing for the purpose of this PR.



##########
ratis-grpc/src/main/java/org/apache/ratis/grpc/server/GrpcServerProtocolClient.java:
##########
@@ -171,15 +192,31 @@ public void onCompleted() {
     }
   }
 
-  CallStreamObserver<AppendEntriesRequestProto> appendEntries(
+  CallStreamObserver<AppendEntriesRequestProto> appendEntries(RaftGroupId 
groupId,
       StreamObserver<AppendEntriesReplyProto> responseHandler, boolean 
isHeartbeat) {
     if (isHeartbeat && useSeparateHBChannel) {
       return (CallStreamObserver<AppendEntriesRequestProto>) 
hbAsyncStub.appendEntries(responseHandler);
     } else {
-      return (CallStreamObserver<AppendEntriesRequestProto>) 
asyncStub.appendEntries(responseHandler);
+      final RaftServerProtocolServiceStub stub = 
appendEntriesStubs.get(getAppendEntriesIndex(groupId));
+      return (CallStreamObserver<AppendEntriesRequestProto>) 
stub.appendEntries(responseHandler);
     }
   }
 
+  /** @return the index of the appendEntries channel for the given group, 
always the same one for a group. */
+  int getAppendEntriesIndex(RaftGroupId groupId) {
+    return Math.floorMod(groupId.hashCode(), appendEntriesStubs.size());

Review Comment:
   Why don't we do a round-robin style assignment if we know this hashcode 
approach could have un-even load? 



##########
ratis-grpc/src/main/java/org/apache/ratis/grpc/server/GrpcServerProtocolClient.java:
##########
@@ -61,7 +69,8 @@ class GrpcServerProtocolClient implements Closeable {
   private final RaftPeerId raftPeerId;
   private final EventLoopGroup eventLoopGroup;
 
-  GrpcServerProtocolClient(RaftPeer target, int connections, int 
flowControlWindow,
+  @SuppressWarnings("checkstyle:ParameterNumber")

Review Comment:
   nit: this probably means we need to switch to a builder pattern.



-- 
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]

Reply via email to