Repository navigation
RATIS-2804. Fix gRPC log-appender.connections conf and code cleanup #1685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+210
−115
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
ratis-grpc/src/main/java/org/apache/ratis/grpc/server/GrpcAppendEntriesChannels.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| /* | ||
| * 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.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. | ||
| * | ||
| * @return the index in the other channels for a new stream. | ||
| */ | ||
| int nextChannelIndex() { | ||
| Preconditions.assertNotNull(otherStubs, "The method should not be called when otherStubs == null"); | ||
| return channelIndex.getAndUpdate(n -> (n + 1) % otherStubs.size()); | ||
| } | ||
|
|
||
| List<ManagedChannel> getAppendEntriesChannelsForTesting() { | ||
| return otherChannels != null ? otherChannels : Collections.singletonList(mainChannel); | ||
| } | ||
|
|
||
| List<ManagedChannel> getOtherChannelsForTesting() { | ||
| return otherChannels; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an odd contract that the caller has to be aware of
otherStubsis not null otherwise this call will hit an NPE.The best way to avoid it is to make this
nextChannelIndexas 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
otherStubsis null? Though still not perfect, it avoids surprising NPE. Meanwhile negative values are often used to indicate something wrong in other languages like C++.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the method is not expected to be called when otherStubs is null, throwing an exception is better. Here is some AI suggestion:
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep throw an exception other than NPE also works.