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

[ISSUE #90] suppport multiple preferred leader #91

Merged
merged 2 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 13 additions & 3 deletions src/main/java/io/openmessaging/storage/dledger/DLedgerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class DLedgerConfig {
private boolean enablePushToFollower = true;

@Parameter(names = {"--preferred-leader-id"}, description = "Preferred LeaderId")
private String preferredLeaderId;
private String preferredLeaderIds;
private long maxLeadershipTransferWaitIndex = 1000;
private int minTakeLeadershipVoteIntervalMs = 30;
private int maxTakeLeadershipVoteIntervalMs = 100;
Expand Down Expand Up @@ -341,12 +341,22 @@ public void setCheckPointInterval(long checkPointInterval) {
this.checkPointInterval = checkPointInterval;
}

@Deprecated
public String getPreferredLeaderId() {
return preferredLeaderId;
return preferredLeaderIds;
}

@Deprecated
public void setPreferredLeaderId(String preferredLeaderId) {
this.preferredLeaderId = preferredLeaderId;
this.preferredLeaderIds = preferredLeaderId;
}

public String getPreferredLeaderIds() {
return preferredLeaderIds;
}

public void setPreferredLeaderIds(String preferredLeaderIds) {
this.preferredLeaderIds = preferredLeaderIds;
}

public long getMaxLeadershipTransferWaitIndex() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.openmessaging.storage.dledger.utils.DLedgerUtils;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -372,8 +373,11 @@ private List<CompletableFuture<VoteResponse>> voteForQuorumResponses(long term,
}

private boolean isTakingLeadership() {
return memberState.getSelfId().equals(dLedgerConfig.getPreferredLeaderId())
|| memberState.getTermToTakeLeadership() == memberState.currTerm();
if (dLedgerConfig.getPreferredLeaderIds() != null && memberState.getTermToTakeLeadership() == memberState.currTerm()) {
List<String> preferredLeaderIds = Arrays.asList(dLedgerConfig.getPreferredLeaderIds().split(";"));
return preferredLeaderIds.contains(memberState.getSelfId());
}
return false;
}

private long getNextTimeToRequestVote() {
Expand Down
43 changes: 34 additions & 9 deletions src/main/java/io/openmessaging/storage/dledger/DLedgerServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@
import io.openmessaging.storage.dledger.utils.PreConditions;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -313,32 +316,54 @@ private void checkPreferredLeader() {
if (!memberState.isLeader()) {
return;
}
String preferredLeaderId = dLedgerConfig.getPreferredLeaderId();
if (preferredLeaderId == null || preferredLeaderId.equals(dLedgerConfig.getSelfId())) {

if (dLedgerConfig.getPreferredLeaderIds() == null) {
return;
}

if (!memberState.isPeerMember(preferredLeaderId)) {
logger.warn("preferredLeaderId = {} is not a peer member", preferredLeaderId);
if (memberState.getTransferee() != null) {
return;
}

if (memberState.getTransferee() != null) {
List<String> preferredLeaderIds = new ArrayList<>(Arrays.asList(dLedgerConfig.getPreferredLeaderIds().split(";")));
if (preferredLeaderIds.contains(dLedgerConfig.getSelfId())) {
return;
}

if (!memberState.getPeersLiveTable().containsKey(preferredLeaderId) ||
memberState.getPeersLiveTable().get(preferredLeaderId) == Boolean.FALSE) {
logger.warn("preferredLeaderId = {} is not online", preferredLeaderId);
Iterator<String> it = preferredLeaderIds.iterator();
while (it.hasNext()) {
String preferredLeaderId = it.next();
if (!memberState.isPeerMember(preferredLeaderId)) {
it.remove();
logger.warn("preferredLeaderId = {} is not a peer member", preferredLeaderId);
continue;
}

if (!memberState.getPeersLiveTable().containsKey(preferredLeaderId) ||
memberState.getPeersLiveTable().get(preferredLeaderId) == Boolean.FALSE.booleanValue()) {
it.remove();
logger.warn("preferredLeaderId = {} is not online", preferredLeaderId);
continue;
}

long fallBehind = dLedgerStore.getLedgerEndIndex() - dLedgerEntryPusher.getPeerWaterMark(memberState.currTerm(), preferredLeaderId);
if (fallBehind >= dLedgerConfig.getMaxLeadershipTransferWaitIndex()) {
logger.warn("preferredLeaderId = {} transferee fall behind index : {}", preferredLeaderId, fallBehind);
continue;
}
}

if (preferredLeaderIds.size() == 0) {
return;
}

String preferredLeaderId = preferredLeaderIds.get(0);
long fallBehind = dLedgerStore.getLedgerEndIndex() - dLedgerEntryPusher.getPeerWaterMark(memberState.currTerm(), preferredLeaderId);
logger.info("transferee fall behind index : {}", fallBehind);
if (fallBehind < dLedgerConfig.getMaxLeadershipTransferWaitIndex()) {
LeadershipTransferRequest request = new LeadershipTransferRequest();
request.setTerm(memberState.currTerm());
request.setTransfereeId(dLedgerConfig.getPreferredLeaderId());
request.setTransfereeId(preferredLeaderId);

try {
long startTransferTime = System.currentTimeMillis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void testThreeServerInFileWithAsyncRequests() throws Exception {
request.setBody(("testThreeServerInFileWithAsyncRequests" + i).getBytes());
futures.add(dLedgerServer1.handleAppend(request));
}
Thread.sleep(500);
Thread.sleep(1000);
Assert.assertEquals(9, dLedgerServer0.getdLedgerStore().getLedgerEndIndex());
Assert.assertEquals(9, dLedgerServer1.getdLedgerStore().getLedgerEndIndex());
Assert.assertEquals(9, dLedgerServer2.getdLedgerStore().getLedgerEndIndex());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void testBatchPushWithAsyncRequests() throws Exception {
request.setBody(("testBatchPushWithAsyncRequests" + i).getBytes());
futures.add(dLedgerServer1.handleAppend(request));
}
Thread.sleep(500);
Thread.sleep(1000);
Assert.assertEquals(9, dLedgerServer0.getdLedgerStore().getLedgerEndIndex());
Assert.assertEquals(9, dLedgerServer1.getdLedgerStore().getLedgerEndIndex());
Assert.assertEquals(9, dLedgerServer2.getdLedgerStore().getLedgerEndIndex());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public void testThreeServerAndShutdownLeader() throws Exception {
while (parseServers(servers, leaderNum, followerNum) == null && DLedgerUtils.elapsed(start) < 1000) {
Thread.sleep(100);
}
Thread.sleep(300);
Thread.sleep(1000);
leaderNum.set(0);
followerNum.set(0);
DLedgerServer leaderServer = parseServers(servers, leaderNum, followerNum);
Expand All @@ -170,7 +170,7 @@ public void testThreeServerAndShutdownLeader() throws Exception {
while (parseServers(leftServers, leaderNum, followerNum) == null && DLedgerUtils.elapsed(start) < 3 * leaderServer.getdLedgerConfig().getHeartBeatTimeIntervalMs()) {
Thread.sleep(100);
}
Thread.sleep(300);
Thread.sleep(1000);
leaderNum.set(0);
followerNum.set(0);
Assert.assertNotNull(parseServers(leftServers, leaderNum, followerNum));
Expand Down