-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Implementing Flexible Raft with NWR #1040
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
Open
1294566108
wants to merge
17
commits into
sofastack:master
Choose a base branch
from
1294566108:feature/flexible_raft
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
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
17 commits
Select commit
Hold shift + click to select a range
49d8842
Implementing Flexible Raft with NWR (#1017)
1294566108 7b2f0d5
support flexible raft
d7b9b94
Merge branch 'sofastack:master' into feature/flexible_raft
1294566108 aa4b4a1
Fix known issues
f8137df
Merge remote-tracking branch 'origin/feature/flexible_raft' into feat…
2af46e3
Fix known issues
14b4f6f
Fix known issues
0f6efbe
Fix known issues
08c4c21
Fix known issues
ad0bc3c
Fix known issues
8f7338e
Fix known issues
cf674f1
Fix known issues
55cc015
Fix known issues
434b499
Fix known issues
1253369
Fix known issues
004adb5
Fix known issues
4bf40db
Fix known issues
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
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
73 changes: 73 additions & 0 deletions
73
jraft-core/src/main/java/com/alipay/sofa/jraft/Quorum.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,73 @@ | ||
| /* | ||
| * 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 com.alipay.sofa.jraft; | ||
|
|
||
| /** | ||
| * | ||
| * Quorum is used to display r and w during the operation of the raft cluster. | ||
| * | ||
| * @author Akai | ||
| */ | ||
| public class Quorum { | ||
| private int w; | ||
|
|
||
| private int r; | ||
|
|
||
| public Quorum(int w, int r) { | ||
| this.w = w; | ||
| this.r = r; | ||
| } | ||
|
|
||
| public int getW() { | ||
| return w; | ||
| } | ||
|
|
||
| public void setW(int w) { | ||
| this.w = w; | ||
| } | ||
|
|
||
| public int getR() { | ||
| return r; | ||
| } | ||
|
|
||
| public void setR(int r) { | ||
| this.r = r; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Quorum{w=" + w + ", r=" + r + '}'; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| int result = 17; | ||
| result = 31 * result + w; | ||
| result = 31 * result + r; | ||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (obj instanceof Quorum) { | ||
| Quorum quorum = (Quorum) obj; | ||
| return quorum.getR() == this.getR() && quorum.getW() == this.getW(); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| } | ||
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 |
|---|---|---|
|
|
@@ -22,19 +22,23 @@ | |
| import java.util.Iterator; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
|
|
||
| import org.apache.commons.lang.StringUtils; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import com.alipay.sofa.jraft.entity.LogEntry; | ||
| import com.alipay.sofa.jraft.entity.PeerId; | ||
| import com.alipay.sofa.jraft.Quorum; | ||
| import com.alipay.sofa.jraft.util.Copiable; | ||
| import com.alipay.sofa.jraft.util.Requires; | ||
|
|
||
| /** | ||
| * A configuration with a set of peers. | ||
| * @author boyan ([email protected]) | ||
| * @author Akai | ||
| * | ||
| * 2018-Mar-15 11:00:26 AM | ||
| */ | ||
|
|
@@ -44,6 +48,14 @@ public class Configuration implements Iterable<PeerId>, Copiable<Configuration> | |
|
|
||
| private static final String LEARNER_POSTFIX = "/learner"; | ||
|
|
||
| private Quorum quorum; | ||
|
|
||
| private int readFactor; | ||
|
|
||
| private int writeFactor; | ||
|
|
||
| private boolean enableFlexible = false; | ||
|
|
||
| private List<PeerId> peers = new ArrayList<>(); | ||
|
|
||
| // use LinkedHashSet to keep insertion order. | ||
|
|
@@ -68,16 +80,34 @@ public Configuration(final Iterable<PeerId> conf) { | |
| * @param conf configuration | ||
| */ | ||
| public Configuration(final Configuration conf) { | ||
| this(conf.getPeers(), conf.getLearners()); | ||
| this(conf.getPeers(), conf.getLearners(), conf.getQuorum(), conf.getReadFactor(), conf.getWriteFactor(), conf | ||
| .isEnableFlexible()); | ||
| } | ||
|
|
||
| /** | ||
| * Construct a Configuration instance with peers and learners. | ||
| * | ||
| * @param conf peers configuration | ||
| * @param learners learners | ||
| * @since 1.3.0 | ||
| * @param conf peers configuration | ||
| * @param learners learners | ||
| * @param quorum quorum | ||
| * @param readFactor read factor | ||
| * @param writeFactor write factor | ||
| * @param enableFlexible enable flexible mode or not | ||
| * @since 1.3.14 | ||
| */ | ||
| public Configuration(final Iterable<PeerId> conf, final Iterable<PeerId> learners, final Quorum quorum, | ||
| final int readFactor, final int writeFactor, final boolean enableFlexible) { | ||
| Requires.requireNonNull(conf, "conf"); | ||
| for (final PeerId peer : conf) { | ||
| this.peers.add(peer.copy()); | ||
| } | ||
| addLearners(learners); | ||
| this.quorum = quorum; | ||
| this.readFactor = readFactor; | ||
| this.writeFactor = writeFactor; | ||
| this.enableFlexible = enableFlexible; | ||
| } | ||
|
|
||
| public Configuration(final Iterable<PeerId> conf, final Iterable<PeerId> learners) { | ||
| Requires.requireNonNull(conf, "conf"); | ||
| for (final PeerId peer : conf) { | ||
|
|
@@ -86,6 +116,48 @@ public Configuration(final Iterable<PeerId> conf, final Iterable<PeerId> learner | |
| addLearners(learners); | ||
| } | ||
|
|
||
| public Configuration fromEntry(LogEntry logEntry) { | ||
| Configuration conf = new Configuration(logEntry.getPeers()); | ||
| conf.setEnableFlexible(logEntry.getEnableFlexible()); | ||
| conf.setReadFactor(logEntry.getReadFactor()); | ||
| conf.setWriteFactor(logEntry.getWriteFactor()); | ||
| Quorum quorum = new Quorum(logEntry.getQuorum().getW(), logEntry.getQuorum().getR()); | ||
| conf.setQuorum(quorum); | ||
| return conf; | ||
| } | ||
|
|
||
| public int getReadFactor() { | ||
| return readFactor; | ||
| } | ||
|
|
||
| public void setReadFactor(int readFactor) { | ||
| this.readFactor = readFactor; | ||
| } | ||
|
|
||
| public int getWriteFactor() { | ||
| return writeFactor; | ||
| } | ||
|
|
||
| public void setWriteFactor(int writeFactor) { | ||
| this.writeFactor = writeFactor; | ||
| } | ||
|
|
||
| public Quorum getQuorum() { | ||
| return this.quorum; | ||
| } | ||
|
|
||
| public void setQuorum(Quorum quorum) { | ||
| this.quorum = quorum; | ||
| } | ||
|
|
||
| public boolean isEnableFlexible() { | ||
| return enableFlexible; | ||
| } | ||
|
|
||
| public void setEnableFlexible(boolean enableFlexible) { | ||
| this.enableFlexible = enableFlexible; | ||
| } | ||
|
|
||
| public void setLearners(final LinkedHashSet<PeerId> learners) { | ||
| this.learners = learners; | ||
| } | ||
|
|
@@ -148,7 +220,7 @@ public List<PeerId> listLearners() { | |
|
|
||
| @Override | ||
| public Configuration copy() { | ||
| return new Configuration(this.peers, this.learners); | ||
| return new Configuration(this); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -251,12 +323,28 @@ public boolean equals(final Object obj) { | |
| if (this.peers == null) { | ||
| return other.peers == null; | ||
| } else { | ||
| return this.peers.equals(other.peers); | ||
| return this.peers.equals(other.peers) && Objects.equals(this.quorum, other.quorum) | ||
| && Objects.equals(this.readFactor, other.readFactor) | ||
| && Objects.equals(this.writeFactor, other.writeFactor) | ||
| && Objects.equals(this.enableFlexible, other.enableFlexible); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| StringBuilder sb = new StringBuilder(toBasicString()); | ||
|
|
||
| sb.append(",enableFlexible:").append(enableFlexible); | ||
|
|
||
| sb.append(",readFactor:").append(readFactor).append(",writeFactor:").append(writeFactor); | ||
|
|
||
| if (Objects.nonNull(quorum)) { | ||
| sb.append(",quorum:").append(quorum); | ||
| } | ||
| return sb.toString(); | ||
| } | ||
|
|
||
| public String toBasicString() { | ||
| final StringBuilder sb = new StringBuilder(); | ||
| final List<PeerId> peers = listPeers(); | ||
| int i = 0; | ||
|
|
@@ -278,7 +366,6 @@ public String toString() { | |
| } | ||
| i++; | ||
| } | ||
|
|
||
| return sb.toString(); | ||
| } | ||
|
|
||
|
|
@@ -311,9 +398,9 @@ public boolean parse(final String conf) { | |
| } | ||
|
|
||
| /** | ||
| * Get the difference between |*this| and |rhs| | ||
| * |included| would be assigned to |*this| - |rhs| | ||
| * |excluded| would be assigned to |rhs| - |*this| | ||
| * Get the difference between |*this| and |rhs| | ||
| * |included| would be assigned to |*this| - |rhs| | ||
| * |excluded| would be assigned to |rhs| - |*this| | ||
| */ | ||
| public void diff(final Configuration rhs, final Configuration included, final Configuration excluded) { | ||
| included.peers = new ArrayList<>(this.peers); | ||
|
|
||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.