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


##########
ratis-grpc/src/main/java/org/apache/ratis/grpc/server/GrpcAppendEntriesChannels.java:
##########
@@ -0,0 +1,102 @@
+/*
+ * 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.server;
+
+import org.apache.ratis.proto.RaftProtos.AppendEntriesReplyProto;
+import org.apache.ratis.proto.RaftProtos.AppendEntriesRequestProto;
+import org.apache.ratis.proto.grpc.RaftServerProtocolServiceGrpc;
+import 
org.apache.ratis.proto.grpc.RaftServerProtocolServiceGrpc.RaftServerProtocolServiceStub;
+import org.apache.ratis.thirdparty.io.grpc.ManagedChannel;
+import org.apache.ratis.thirdparty.io.grpc.stub.CallStreamObserver;
+import org.apache.ratis.thirdparty.io.grpc.stub.StreamObserver;
+import org.apache.ratis.util.Preconditions;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.Collectors;
+
+/**
+ * Channels for creating appendEntries streams to a peer.
+ * <p>
+ * When there are no other channels, use only the main channel to create 
streams,
+ * which is shared with the other RPCs (requestVote, installSnapshot, etc.).
+ * Otherwise, use only the other channels, which are dedicated to 
appendEntries;
+ * each stream uses one channel and the new streams take the channels in turn.
+ * <p>
+ * All the channels, including the other channels, are owned by the caller,
+ * which is also responsible for shutting them down.
+ */
+class GrpcAppendEntriesChannels {
+  // The main channel; null if the other channels are used for appendEntries.
+  private final ManagedChannel mainChannel;
+  private final RaftServerProtocolServiceStub mainStub;
+  // The channels dedicated to appendEntries; null if the main channel is used 
for appendEntries.
+  private final List<ManagedChannel> otherChannels;
+  private final List<RaftServerProtocolServiceStub> otherStubs;
+  // The index of the channel in otherChannels for the next stream
+  private final AtomicInteger channelIndex = new AtomicInteger();
+
+  /**
+   * @param mainChannel   the main channel, used for appendEntries only if 
there are no other channels;
+   *                      otherwise, it is ignored.
+   * @param otherChannels the channels dedicated to appendEntries;
+   *                      null means that there are no other channels, 
otherwise it must be non-empty.
+   */
+  GrpcAppendEntriesChannels(ManagedChannel mainChannel, List<ManagedChannel> 
otherChannels) {
+    if (otherChannels == null) {
+      this.mainChannel = mainChannel;
+      this.mainStub = RaftServerProtocolServiceGrpc.newStub(mainChannel);
+      this.otherChannels = null;
+      this.otherStubs = null;
+    } else {
+      this.mainChannel = null;
+      this.mainStub = null;
+      Preconditions.assertTrue(!otherChannels.isEmpty(), "otherChannels is 
empty");
+      this.otherChannels = Collections.unmodifiableList(otherChannels);
+      this.otherStubs = Collections.unmodifiableList(otherChannels.stream()
+          .map(RaftServerProtocolServiceGrpc::newStub)
+          .collect(Collectors.toList()));
+    }
+  }
+
+  /** Create a new {@link CallStreamObserver} for appendEntries. */
+  CallStreamObserver<AppendEntriesRequestProto> 
newStream(StreamObserver<AppendEntriesReplyProto> responseHandler) {
+    final RaftServerProtocolServiceStub stub = otherStubs != null ? 
otherStubs.get(nextChannelIndex()) : mainStub;
+    return (CallStreamObserver<AppendEntriesRequestProto>) 
stub.appendEntries(responseHandler);
+  }
+
+  /**
+   * This method must be called only when there are other channels.

Review Comment:
   This is an odd contract that the caller has to be aware of `otherStubs` is 
not null otherwise this call will hit an NPE. 
   
   The best way to avoid it is to make this `nextChannelIndex` as private thus 
only this class will use it and we know what we are doing. However I see that 
we need it in testing as well so have to keep this as package private.  But 
then package private means other Ratis developer might still call it and cause 
unseen issue when testing is not enough or reviewers not having enough context.
   
   I am wondering if we can return -1 when `otherStubs` is null? Though still 
not perfect, it avoids surprising NPE. Meanwhile negative values are often used 
to indicate something wrong in other languages like C++.



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