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
IGNITE-19120 Raft client should get leader metadata along while getting leader itself #2255
Open
vldpyatkov
wants to merge
8
commits into
apache:main
Choose a base branch
from
gridgain:ignite-19120
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6c73542
IGNITE-19120 Raft client should get leader metadata along while getti…
vldpyatkov 509272c
Added tests
vldpyatkov 9a46b3f
Fixed test
vldpyatkov 2e710d6
WIP
vldpyatkov 3322026
Code style
vldpyatkov 1d531bf
WIP
vldpyatkov c8531bf
WIP
vldpyatkov 8b4a107
WIP
vldpyatkov File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
modules/raft-api/src/main/java/org/apache/ignite/internal/raft/service/LeaderMetadata.java
This file contains 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,83 @@ | ||
/* | ||
* 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.ignite.internal.raft.service; | ||
|
||
import org.apache.ignite.internal.raft.Peer; | ||
|
||
/** | ||
* The class represents a leader metadata. | ||
*/ | ||
public class LeaderMetadata { | ||
/** Leader peer. */ | ||
private final Peer leader; | ||
|
||
/** Corresponding term to the leader. */ | ||
private final long term; | ||
|
||
/** Index on the moment to formed the metadata. */ | ||
private final long index; | ||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param leader Leader. | ||
* @param term Leader term. | ||
* @param index Raft index. | ||
*/ | ||
public LeaderMetadata(Peer leader, long term, long index) { | ||
this.leader = leader; | ||
this.term = term; | ||
this.index = index; | ||
} | ||
|
||
/** | ||
* Get a leader peer. | ||
* | ||
* @return Leader peer. | ||
*/ | ||
public Peer getLeader() { | ||
return leader; | ||
} | ||
|
||
/** | ||
* Gets a term corresponding to the leader. | ||
* | ||
* @return Leader term. | ||
*/ | ||
public long getTerm() { | ||
return term; | ||
} | ||
|
||
/** | ||
* Gets an index on the moment to formed the metadata. | ||
* | ||
* @return Raft index. | ||
*/ | ||
public long getIndex() { | ||
return index; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "LeaderMetadata{" | ||
+ "leader=" + leader | ||
+ ", term=" + term | ||
+ ", index=" + index | ||
+ '}'; | ||
} | ||
} |
This file contains 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 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
127 changes: 127 additions & 0 deletions
127
...raft/src/integrationTest/java/org/apache/ignite/raft/server/ItReadLeaderMetadataTest.java
This file contains 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,127 @@ | ||
/* | ||
* 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.ignite.raft.server; | ||
|
||
import static org.apache.ignite.internal.raft.server.RaftGroupOptions.defaults; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.function.Supplier; | ||
import org.apache.ignite.internal.raft.Peer; | ||
import org.apache.ignite.internal.raft.RaftNodeId; | ||
import org.apache.ignite.internal.raft.service.LeaderMetadata; | ||
import org.apache.ignite.internal.raft.service.LeaderWithTerm; | ||
import org.apache.ignite.internal.raft.service.RaftGroupService; | ||
import org.apache.ignite.internal.replicator.TestReplicationGroupId; | ||
import org.apache.ignite.raft.server.counter.CounterListener; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Reade metadata tests. | ||
*/ | ||
public class ItReadLeaderMetadataTest extends JraftAbstractTest { | ||
/** | ||
* Listener factory. | ||
*/ | ||
private final Supplier<CounterListener> listenerFactory = CounterListener::new; | ||
|
||
/** Raft group id. */ | ||
private static final TestReplicationGroupId GROUP_ID = new TestReplicationGroupId("GRP_1"); | ||
|
||
@Test | ||
public void test() throws Exception { | ||
log.info("Test here."); | ||
|
||
for (int i = 0; i < 3; i++) { | ||
startServer(i, raftServer -> { | ||
String localNodeName = raftServer.clusterService().topologyService().localMember().name(); | ||
|
||
Peer serverPeer = initialMembersConf.peer(localNodeName); | ||
|
||
raftServer.startRaftNode( | ||
new RaftNodeId(GROUP_ID, serverPeer), initialMembersConf, listenerFactory.get(), defaults() | ||
); | ||
}, opts -> {}); | ||
} | ||
|
||
var raftClient1 = startClient(GROUP_ID); | ||
var raftClient2 = startClient(GROUP_ID); | ||
|
||
raftClient1.refreshMembers(true).get(); | ||
raftClient1.refreshLeader().get(); | ||
raftClient2.refreshMembers(true).get(); | ||
raftClient2.refreshLeader().get(); | ||
|
||
assertEquals(raftClient1.leader(), raftClient2.leader()); | ||
|
||
checkLeaderConsistency(raftClient1, raftClient2); | ||
|
||
Peer curLeader = raftClient1.leader(); | ||
|
||
Peer newLeader = raftClient1.peers().stream().filter(p -> !p.equals(curLeader)).findAny().get(); | ||
|
||
assertNotNull(newLeader); | ||
|
||
log.info("Transfer leadership [cur={}, new={}]", curLeader, newLeader); | ||
|
||
raftClient1.transferLeadership(newLeader).get(); | ||
|
||
assertNotEquals(raftClient1.leader(), raftClient2.leader()); | ||
|
||
log.info("Leader transferred [client1={}, client2={}]", raftClient1.leader(), raftClient2.leader()); | ||
|
||
checkLeaderConsistency(raftClient1, raftClient2); | ||
|
||
assertEquals(raftClient1.leader(), raftClient2.leader()); | ||
} | ||
|
||
/** | ||
* Checks that leader refreshed consistency on both clients. | ||
* | ||
* @param raftClient1 Raft client 1. | ||
* @param raftClient2 Raft client 2 | ||
* @throws Exception If failed. | ||
*/ | ||
private static void checkLeaderConsistency(RaftGroupService raftClient1, RaftGroupService raftClient2) throws Exception { | ||
LeaderWithTerm leaderWithTerm = raftClient1.refreshAndGetLeaderWithTerm().get(); | ||
LeaderMetadata leaderMetadata = raftClient2.readLeaderMetadata().get(); | ||
|
||
long indexOnClient2 = leaderMetadata.getIndex(); | ||
|
||
assertTrue(leaderWithTerm.term() <= leaderMetadata.getTerm()); | ||
|
||
if (leaderWithTerm.term() == leaderMetadata.getTerm()) { | ||
assertEquals(leaderWithTerm.leader(), leaderMetadata.getLeader()); | ||
} | ||
|
||
leaderMetadata = raftClient1.readLeaderMetadata().get(); | ||
leaderWithTerm = raftClient2.refreshAndGetLeaderWithTerm().get(); | ||
|
||
long indexOnClient1 = leaderMetadata.getIndex(); | ||
|
||
assertTrue(leaderWithTerm.term() <= leaderMetadata.getTerm()); | ||
|
||
if (leaderWithTerm.term() == leaderMetadata.getTerm()) { | ||
assertEquals(leaderWithTerm.leader(), leaderMetadata.getLeader()); | ||
} | ||
|
||
assertEquals(indexOnClient2, indexOnClient1); | ||
} | ||
} |
This file contains 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 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 |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
package org.apache.ignite.raft.jraft; | ||
|
||
import java.util.List; | ||
import org.apache.ignite.lang.IgniteBiTuple; | ||
import org.apache.ignite.raft.jraft.closure.ReadIndexClosure; | ||
import org.apache.ignite.raft.jraft.conf.Configuration; | ||
import org.apache.ignite.raft.jraft.core.NodeMetrics; | ||
|
@@ -41,6 +42,14 @@ public interface Node extends Lifecycle<NodeOptions>, Describer { | |
*/ | ||
PeerId getLeaderId(); | ||
|
||
/** | ||
* Returns leader with their term concurrently. | ||
* If the leader is not known, the method returns {@code null}. | ||
* | ||
* @return Leader peer with corresponding term. | ||
*/ | ||
IgniteBiTuple<PeerId, Long> getLeaderWithTer(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
/** | ||
* Get current node id. | ||
*/ | ||
|
This file contains 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.
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.
Could we use
S.toString(LeaderWithTerm.class, this);
?