Skip to content
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

Implementing Flexible Raft with NWR #1017

Merged
merged 102 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
102 commits
Select commit Hold shift + click to select a range
214ec93
feat(iterator):Auto commit mode for applying log iterator
1294566108 Apr 9, 2023
8ed148d
feat(iterator):Auto commit mode for applying log iterator
1294566108 Apr 10, 2023
21ac53a
feat(iterator):Correction parameter name
1294566108 Apr 10, 2023
e5d9986
feat(iterator):mvn compile
1294566108 Apr 11, 2023
8cdff52
feat(iterator):Modify comments and annotations
1294566108 Apr 12, 2023
88a6ce2
feat(iterator):Modify comments
1294566108 Apr 12, 2023
535d876
feat(iterator):Modify comments
1294566108 Apr 12, 2023
9034f71
feat(rfcs):Add rfcs for Flexible-Raft-with-NWR
1294566108 Jul 1, 2023
fce77c4
Merge branch 'sofastack:master' into master
1294566108 Jul 1, 2023
9e6a785
feat(rfcs):Add stepDown change logic and modify parameter name for rfcs
1294566108 Jul 3, 2023
9d14232
Merge remote-tracking branch 'origin/master'
1294566108 Jul 3, 2023
ffd2136
feat(rfcs):Modify factor calculation rules for rfcs
1294566108 Jul 4, 2023
9eec73b
feat(rfcs):Modify some language expressions
1294566108 Jul 6, 2023
0e75dbb
feat(rfcs):Implementing Flexible Raft with NWR
1294566108 Jul 6, 2023
f9e766f
feat(rfcs):compile project
1294566108 Jul 6, 2023
e7d657a
Merge remote-tracking branch 'origin/master'
1294566108 Jul 6, 2023
c4fd678
feat(rfcs):compile project
1294566108 Jul 6, 2023
46f95f1
feat(rfcs):Remove useless and redundant code
1294566108 Jul 6, 2023
11311de
feat(rfcs):Add rfcs
1294566108 Jul 7, 2023
5ed30b6
feat(flexible):add flexible mode fot Jraft
1294566108 Jul 18, 2023
f1c3647
feat(flexible):Modify rfc doc
1294566108 Jul 18, 2023
5d5dddc
feat(flexible):Modify quorum code design
1294566108 Jul 26, 2023
fdd1aca
feat(factor):Add ResetFactor API
1294566108 Jul 26, 2023
3627697
feat(flexible):compile code
1294566108 Jul 26, 2023
d4ae075
feat(flexible):modify code
1294566108 Jul 26, 2023
5fce197
feat(flexible):modify code
1294566108 Jul 26, 2023
2c10bf2
feat(flexible):compile code
1294566108 Jul 26, 2023
a6d904b
fix(flexible):modify raft.desc
1294566108 Jul 26, 2023
5f36899
fix(flexible):modify MarshallerHelper
1294566108 Jul 26, 2023
a8f1bd3
fix(flexible):modify MarshallerHelper
1294566108 Jul 26, 2023
790a3a2
fix(flexible):modify RAFT.DESC
1294566108 Jul 26, 2023
de1edf6
fix(flexible):Modifying comments and code formats
1294566108 Jul 26, 2023
c7ee54e
fix(flexible):Added factor persistence for log storage and Adapt to w…
1294566108 Jul 29, 2023
f7be903
fix(flexible):compile code
1294566108 Jul 29, 2023
1e376f3
fix(flexible):modify test
1294566108 Jul 29, 2023
e79a41a
fix(flexible):modify test
1294566108 Jul 29, 2023
f04018f
fix(flexible):modify code with proto version changing
1294566108 Aug 1, 2023
f5d2e52
fix(flexible):modify code with proto version changing
1294566108 Aug 1, 2023
02eba00
fix(flexible):modify code format
1294566108 Aug 1, 2023
72f67ab
fix(flexible):modify code format
1294566108 Aug 1, 2023
cf620cc
fix(flexible):change protoc version from 2.6.1 to 3.5.1
1294566108 Aug 2, 2023
2933732
fix(flexible):modify Class RaftError
1294566108 Aug 2, 2023
d7534d4
fix(flexible):modify Class format
1294566108 Aug 2, 2023
cc8959d
fix(flexible):modify Class format
1294566108 Aug 2, 2023
8032338
fix(flexible):modify Class format
1294566108 Aug 2, 2023
d255078
fix(flexible):change int64 to int32 in proto
1294566108 Aug 2, 2023
a43518e
fix(flexible):change int64 to int32 in proto
1294566108 Aug 2, 2023
e0c5b97
fix(flexible):avoid importing *
1294566108 Aug 2, 2023
7210bad
fix(flexible):change comment
1294566108 Aug 2, 2023
341377f
fix(flexible):add this for voteCtx and preVoteCtx
1294566108 Aug 2, 2023
2e4b3c9
fix(flexible):change import order
1294566108 Aug 2, 2023
840c2d8
fix(flexible):change import order
1294566108 Aug 2, 2023
a706938
feat(flexible):Place attributes such as quorum, factor, and isEnableF…
1294566108 Aug 17, 2023
90199f5
feat(flexible):check format
1294566108 Aug 17, 2023
566f755
feat(flexible):Add test code for resetFactor API
1294566108 Aug 18, 2023
4cb0e09
feat(flexible):modify code format
1294566108 Aug 18, 2023
1be0405
feat(flexible):enable flexible check
1294566108 Aug 18, 2023
f3ed97c
feat(flexible):comment useless code
1294566108 Aug 20, 2023
8261bcc
feat(flexible):modify code
1294566108 Aug 20, 2023
8448183
feat(flexible):Persisting the quorum attribute
1294566108 Aug 21, 2023
8075eb9
fix(flexible):Delete redundant haveFactor judgments
1294566108 Aug 22, 2023
6a54afb
fix(flexible):Add necessary null judgment
1294566108 Aug 22, 2023
31f8b98
fix(flexible):remove config null parameter
1294566108 Aug 22, 2023
3b6ebdd
fix(flexible):modify code
1294566108 Aug 22, 2023
b810708
fix(flexible):modify some logic mistakes、remove unused method and add…
1294566108 Aug 25, 2023
d534436
fix(flexible):modify config check
1294566108 Aug 26, 2023
4191811
fix(flexible):modify config check
1294566108 Aug 26, 2023
5d3a44b
fix(flexible):remove Redundant code
1294566108 Aug 26, 2023
f564dd1
fix(flexible):remove some check and add NodeOptions.setFactor method
1294566108 Aug 26, 2023
b0ebc3a
fix(flexible):remove redundant code
1294566108 Aug 26, 2023
51b64d9
fix(flexible):解决majority模式下,空指针异常问题
1294566108 Aug 28, 2023
c55dcc6
fix(flexible):修改部分set quorum逻辑,
1294566108 Sep 4, 2023
5c3341a
feature(flexible):添加flexible raft 测试模块
1294566108 Sep 10, 2023
e8ee175
feature(flexible):check code format
1294566108 Sep 11, 2023
2f4a6dd
feature(flexible):remove code
1294566108 Sep 11, 2023
e5e29a7
feature(flexible):remove redundant blank lines
1294566108 Sep 17, 2023
76043ad
feature(flexible):modify test module
1294566108 Sep 18, 2023
1c6d0cf
fix(LogManagerTest):modify code format
1294566108 Sep 19, 2023
462c18c
fix(LogManagerTest):modify code format
1294566108 Sep 19, 2023
cca6c1c
fix(LogManagerTest):modify code format
1294566108 Sep 19, 2023
4885aa0
feature(flexible):modify test module in core
1294566108 Sep 19, 2023
26014b5
fix(test_core):modify test—core code
1294566108 Sep 20, 2023
93ec6c2
fix(test_module):modify test_core
1294566108 Sep 21, 2023
381cdf6
fix(test_module):modify testTripleNodesV1V2Codec temporarily
1294566108 Sep 22, 2023
6a1bf03
fix(test_module):modify testTripleNodesV1V2Codec temporarily
1294566108 Sep 22, 2023
c55a1e6
fix(test_module):modify testTripleNodesV1V2Codec temporarily
1294566108 Sep 22, 2023
a568a35
fix(test_module):modify testTripleNodesV1V2Codec temporarily
1294566108 Sep 22, 2023
22162cf
fix(test_module):modify V1V2Codec error
1294566108 Sep 23, 2023
2e94d92
fix(test_module):modify V1V2Codec error
1294566108 Sep 23, 2023
dbbb321
fix(test_module):modify V1V2Codec error
1294566108 Sep 23, 2023
98552a0
fix(test_module):modify resetPeers error
1294566108 Sep 24, 2023
b4a995d
fix(test_module):modify changePeerChaps test error
1294566108 Sep 24, 2023
53ba339
fix(test_module):modify v1decoder error
1294566108 Sep 24, 2023
a3e87d6
fix(test_module):modify v1decoder error
1294566108 Sep 24, 2023
8270ce2
fix(test_module):modify storage test
1294566108 Sep 24, 2023
b1cfb1a
fix(test_module):modify storage test
1294566108 Sep 24, 2023
36bb588
fix(test_module):modify node test
1294566108 Sep 25, 2023
2049c01
Merge branch 'sofastack:master' into reset-factor
1294566108 Oct 25, 2023
42f2f6c
fix(test_module):modify node test
Oct 25, 2023
5ff9a11
modify code
1294566108 Oct 28, 2023
bbb3b88
modify code
1294566108 Oct 28, 2023
058d5d5
modify code
1294566108 Oct 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions jraft-core/src/main/java/com/alipay/sofa/jraft/CliService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@
*/
public interface CliService extends Lifecycle<CliOptions> {

/**
* Reset the size of the read or write factor of a raft group in flexible mode
*
* @param groupId the raft group id
* @param conf current configuration
* @param readFactor read factor in flexible raft mode
* @param writeFactor write factor in flexible raft mode
* @return operation status
*/
Status resetFactor(final String groupId, final Configuration conf, final Integer readFactor,
1294566108 marked this conversation as resolved.
Show resolved Hide resolved
final Integer writeFactor);

/**
* Add a new peer into the replicating group which consists of |conf|.
* return OK status when success.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.alipay.sofa.jraft.conf.Configuration;
import com.alipay.sofa.jraft.core.NodeImpl;
import com.alipay.sofa.jraft.entity.BallotFactory;
import com.alipay.sofa.jraft.entity.PeerId;
import com.alipay.sofa.jraft.option.BootstrapOptions;
import com.alipay.sofa.jraft.util.Endpoint;
Expand Down Expand Up @@ -112,6 +113,9 @@ public static Configuration getConfiguration(final String s) {
return conf;
}
if (conf.parse(s)) {
conf.setEnableFlexible(false);
Quorum quorum = BallotFactory.buildMajorityQuorum(conf.size());
conf.setQuorum(quorum);
return conf;
}
throw new IllegalArgumentException("Invalid conf str:" + s);
Expand Down
12 changes: 12 additions & 0 deletions jraft-core/src/main/java/com/alipay/sofa/jraft/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,18 @@ public interface Node extends Lifecycle<NodeOptions>, Describer {
*/
void changePeers(final Configuration newPeers, final Closure done);

/**
* This method can be called to reset the size of the read or write factor
*
* It should be noted that we cannot change the factory size while changing
* the number of cluster nodes.
*
* @param readFactor read factor for flexible raft
* @param writeFactor write factor for flexible raft
* @since 1.3.14
*/
void resetFactor(final Integer readFactor, final Integer writeFactor, final Closure done);

/**
* Reset the configuration of this node individually, without any replication
* to other peers before this node becomes the leader. This function is
Expand Down
67 changes: 67 additions & 0 deletions jraft-core/src/main/java/com/alipay/sofa/jraft/Quorum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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;

/**
* @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() {
return super.hashCode();
}

@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;
}

}
108 changes: 94 additions & 14 deletions jraft-core/src/main/java/com/alipay/sofa/jraft/conf/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,43 @@
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.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
*/
public class Configuration implements Iterable<PeerId>, Copiable<Configuration> {

private static final Logger LOG = LoggerFactory.getLogger(Configuration.class);
private static final Logger LOG = LoggerFactory.getLogger(Configuration.class);

private static final String LEARNER_POSTFIX = "/learner";
private static final String LEARNER_POSTFIX = "/learner";

private List<PeerId> peers = new ArrayList<>();
private Quorum quorum;

private Integer readFactor;
1294566108 marked this conversation as resolved.
Show resolved Hide resolved

private Integer writeFactor;

private Boolean isEnableFlexible = false;

private List<PeerId> peers = new ArrayList<>();

// use LinkedHashSet to keep insertion order.
private LinkedHashSet<PeerId> learners = new LinkedHashSet<>();
private LinkedHashSet<PeerId> learners = new LinkedHashSet<>();

public Configuration() {
super();
Expand All @@ -68,16 +79,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 isEnableFlexible enable flexible mode or not
* @since 1.3.14
*/
public Configuration(final Iterable<PeerId> conf, final Iterable<PeerId> learners, final Quorum quorum,
final Integer readFactor, final Integer writeFactor, final Boolean isEnableFlexible) {
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.isEnableFlexible = isEnableFlexible;
}

public Configuration(final Iterable<PeerId> conf, final Iterable<PeerId> learners) {
Requires.requireNonNull(conf, "conf");
for (final PeerId peer : conf) {
Expand All @@ -86,6 +115,38 @@ public Configuration(final Iterable<PeerId> conf, final Iterable<PeerId> learner
addLearners(learners);
}

public Integer getReadFactor() {
1294566108 marked this conversation as resolved.
Show resolved Hide resolved
return readFactor;
}

public void setReadFactor(Integer readFactor) {
this.readFactor = readFactor;
}

public Integer getWriteFactor() {
return writeFactor;
}

public void setWriteFactor(Integer writeFactor) {
this.writeFactor = writeFactor;
}

public Quorum getQuorum() {
return this.quorum;
}

public void setQuorum(Quorum quorum) {
this.quorum = quorum;
}

public Boolean isEnableFlexible() {
return isEnableFlexible;
}

public void setEnableFlexible(Boolean enableFlexible) {
isEnableFlexible = enableFlexible;
}

public void setLearners(final LinkedHashSet<PeerId> learners) {
this.learners = learners;
}
Expand Down Expand Up @@ -148,7 +209,7 @@ public List<PeerId> listLearners() {

@Override
public Configuration copy() {
return new Configuration(this.peers, this.learners);
return new Configuration(this);
}

/**
Expand Down Expand Up @@ -251,12 +312,32 @@ 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.isEnableFlexible, other.isEnableFlexible);
}
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder(toBasicString());

if (Objects.nonNull(isEnableFlexible) && !isEmpty()) {
sb.append(",isEnableFlexible:").append(isEnableFlexible);
}

if (Objects.nonNull(readFactor) || Objects.nonNull(writeFactor)) {
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;
Expand All @@ -278,7 +359,6 @@ public String toString() {
}
i++;
}

return sb.toString();
}

Expand Down Expand Up @@ -311,9 +391,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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import javax.annotation.concurrent.ThreadSafe;

import com.alipay.sofa.jraft.entity.Ballot;
import com.alipay.sofa.jraft.entity.PeerId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -28,8 +30,6 @@
import com.alipay.sofa.jraft.Lifecycle;
import com.alipay.sofa.jraft.closure.ClosureQueue;
import com.alipay.sofa.jraft.conf.Configuration;
import com.alipay.sofa.jraft.entity.Ballot;
import com.alipay.sofa.jraft.entity.PeerId;
import com.alipay.sofa.jraft.option.BallotBoxOptions;
import com.alipay.sofa.jraft.util.Describer;
import com.alipay.sofa.jraft.util.OnlyForTest;
Expand Down Expand Up @@ -114,12 +114,13 @@ public boolean commitAt(final long firstLogIndex, final long lastLogIndex, final
final long startAt = Math.max(this.pendingIndex, firstLogIndex);
Ballot.PosHint hint = new Ballot.PosHint();
for (long logIndex = startAt; logIndex <= lastLogIndex; logIndex++) {
final Ballot bl = this.pendingMetaQueue.get((int) (logIndex - this.pendingIndex));
hint = bl.grant(peer, hint);
if (bl.isGranted()) {
final Ballot ballot = this.pendingMetaQueue.get((int) (logIndex - this.pendingIndex));
hint = ballot.grant(peer, hint);
if (ballot.isGranted()) {
lastCommittedIndex = logIndex;
}
}

if (lastCommittedIndex == 0) {
return true;
}
Expand Down Expand Up @@ -198,8 +199,8 @@ public boolean resetPendingIndex(final long newPendingIndex) {
* @return returns true on success
*/
public boolean appendPendingTask(final Configuration conf, final Configuration oldConf, final Closure done) {
final Ballot bl = new Ballot();
if (!bl.init(conf, oldConf)) {
final Ballot ballot = new Ballot();
if (!ballot.init(conf, oldConf)) {
LOG.error("Fail to init ballot.");
return false;
}
Expand All @@ -209,7 +210,7 @@ public boolean appendPendingTask(final Configuration conf, final Configuration o
LOG.error("Node {} fail to appendingTask, pendingIndex={}.", this.opts.getNodeId(), this.pendingIndex);
return false;
}
this.pendingMetaQueue.add(bl);
this.pendingMetaQueue.add(ballot);
this.closureQueue.appendPendingClosure(done);
return true;
} finally {
Expand Down
Loading