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

feat(core): cluster role automatic management #1943

Merged
merged 9 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.baidu.hugegraph.election;
zyxxoo marked this conversation as resolved.
Show resolved Hide resolved

public interface Config {

String node();

int exceedsFailCount();

long randomTimeoutMillisecond();

long heartBeatIntervalSecond();

int exceedsWorkerCount();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.baidu.hugegraph.election;

import java.util.Objects;

public class MetaData {
zyxxoo marked this conversation as resolved.
Show resolved Hide resolved

String node;
long count;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need long?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe int is enough

int epoch;
zyxxoo marked this conversation as resolved.
Show resolved Hide resolved

public MetaData(String node, int epoch) {
this.node = node;
this.epoch = epoch;
this.count = 1;
}

public void increaseCount() {
this.count++;
}

public boolean isMaster(String node) {
return Objects.equals(this.node, node);
}

public int epoch() {
return this.epoch;
}

public long count() {
return this.count;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
zyxxoo marked this conversation as resolved.
Show resolved Hide resolved
if (!(o instanceof MetaData)) return false;
zyxxoo marked this conversation as resolved.
Show resolved Hide resolved
MetaData metaData = (MetaData) o;
return count == metaData.count && epoch == metaData.epoch && Objects.equals(node, metaData.node);
}

@Override
public int hashCode() {
return Objects.hash(node, count, epoch);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.baidu.hugegraph.election;

import java.util.Optional;

public interface MetaDataAdapter {
zyxxoo marked this conversation as resolved.
Show resolved Hide resolved

boolean postDelyIfPresent(MetaData metaData, long delySecond);
zyxxoo marked this conversation as resolved.
Show resolved Hide resolved
zyxxoo marked this conversation as resolved.
Show resolved Hide resolved

Optional<MetaData> queryDelay(long delySecond);
zyxxoo marked this conversation as resolved.
Show resolved Hide resolved
zyxxoo marked this conversation as resolved.
Show resolved Hide resolved

Optional<MetaData> query();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.baidu.hugegraph.election;

public interface RoleElectionStateMachine {

void shutdown();

void apply(StateMachineCallback stateMachineCallback);

zyxxoo marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
package com.baidu.hugegraph.election;

import java.util.Optional;
import java.util.concurrent.locks.LockSupport;

import com.baidu.hugegraph.util.E;

public class RoleElectionStateMachineImpl implements RoleElectionStateMachine {

private volatile boolean shutdown = false;
zyxxoo marked this conversation as resolved.
Show resolved Hide resolved
private Config config;
zyxxoo marked this conversation as resolved.
Show resolved Hide resolved

private volatile RoleState state;

private final MetaDataAdapter metaDataAdapter;

public RoleElectionStateMachineImpl(Config config, MetaDataAdapter adapter) {
this.config = config;
this.metaDataAdapter = adapter;
this.state = new UnKnownState(null);
}

@Override
public void shutdown() {
this.shutdown = true;
}

@Override
public void apply(StateMachineCallback stateMachineCallback) {
int failCount = 0;
StateMachineContextImpl context = new StateMachineContextImpl(this);
while (!this.shutdown) {
E.checkArgumentNotNull(this.state, "State don't be null");
try {
this.state = state.transform(context);
Callback runnable = this.state.callback(stateMachineCallback);
runnable.call(context);
failCount = 0;
} catch (Throwable e) {
stateMachineCallback.error(context, e);
failCount ++;
if (failCount >= this.config.exceedsFailCount()) {
this.state = new SafeState(context.epoch());
Callback runnable = this.state.callback(stateMachineCallback);
runnable.call(context);
}
}
}
}

private interface RoleState {

RoleState transform(StateMachineContext context);

Callback callback(StateMachineCallback callback);

static void heartBeatPark(StateMachineContext context) {
long heartBeatIntervalSecond = context.config().heartBeatIntervalSecond();
LockSupport.parkNanos(heartBeatIntervalSecond * 1_000_000_000);
}

static void randomPark(StateMachineContext context) {
long randomTimeout = context.config().randomTimeoutMillisecond();
LockSupport.parkNanos(randomTimeout * 1_000_000);
}
}

@FunctionalInterface
private interface Callback {

void call(StateMachineContext context);
}

private static class UnKnownState implements RoleState {
zyxxoo marked this conversation as resolved.
Show resolved Hide resolved

Integer epoch;
zyxxoo marked this conversation as resolved.
Show resolved Hide resolved

public UnKnownState(Integer epoch) {
this.epoch = epoch;
}

@Override
public RoleState transform(StateMachineContext context) {
MetaDataAdapter adapter = context.adapter();
Optional<MetaData> metaDataOpt = adapter.query();
if (!metaDataOpt.isPresent()) {
context.reset();
this.epoch = this.epoch == null ? 1 : this.epoch + 1;
context.epoch(this.epoch);
return new CandidateState(this.epoch);
}

MetaData metaData = metaDataOpt.get();
context.epoch(metaData.epoch());
if (metaData.isMaster(context.node())) {
return new MasterState(metaData);
} else {
return new WorkerState(metaData);
}
}

@Override
public Callback callback(StateMachineCallback callback) {
return callback::unknown;
}
}

private static class SafeState implements RoleState {
zyxxoo marked this conversation as resolved.
Show resolved Hide resolved

Integer epoch;
zyxxoo marked this conversation as resolved.
Show resolved Hide resolved

public SafeState(Integer epoch) {
this.epoch = epoch;
}

@Override
public RoleState transform(StateMachineContext context) {
RoleState.heartBeatPark(context);
return new UnKnownState(this.epoch).transform(context);
}

@Override
public Callback callback(StateMachineCallback callback) {
return callback::safe;
}
}

private static class MasterState implements RoleState {

MetaData metaData;
zyxxoo marked this conversation as resolved.
Show resolved Hide resolved

public MasterState(MetaData metaData) {
this.metaData = metaData;
}

@Override
public RoleState transform(StateMachineContext context) {
this.metaData.increaseCount();
RoleState.heartBeatPark(context);
if (context.adapter().postDelyIfPresent(this.metaData, -1)) {
return this;
}
context.reset();
context.epoch(this.metaData.epoch());
return new UnKnownState(this.metaData.epoch());
}

@Override
public Callback callback(StateMachineCallback callback) {
return callback::master;
}
}

private static class WorkerState implements RoleState {

private MetaData metaData;
private int count = 0;
zyxxoo marked this conversation as resolved.
Show resolved Hide resolved

public WorkerState(MetaData metaData) {
this.metaData = metaData;
}

@Override
public RoleState transform(StateMachineContext context) {
RoleState.heartBeatPark(context);
RoleState nextState = new UnKnownState(this.metaData.epoch()).transform(context);
if (nextState instanceof WorkerState) {
this.merge((WorkerState) nextState);
if (this.count > context.config().exceedsWorkerCount()) {
return new CandidateState(this.metaData.epoch() + 1);
} else {
return this;
}
} else {
return nextState;
}
}

@Override
public Callback callback(StateMachineCallback callback) {
return callback::worker;
}

public void merge(WorkerState state) {
if (state.metaData.epoch() > this.metaData.epoch()) {
this.count = 0;
this.metaData = state.metaData;
} else if (state.metaData.epoch() < this.metaData.epoch()){
throw new IllegalStateException("Epoch must increase");
} else if (state.metaData.epoch() == this.metaData.epoch() &&
state.metaData.count() < this.metaData.count()) {
zyxxoo marked this conversation as resolved.
Show resolved Hide resolved
throw new IllegalStateException("Meta count must increase");
} else if (state.metaData.epoch() == this.metaData.epoch() &&
state.metaData.count() > this.metaData.count()) {
this.count = 0;
this.metaData = state.metaData;
} else {
this.count++;
}
}
}

private static class CandidateState implements RoleState {

Integer epoch;
zyxxoo marked this conversation as resolved.
Show resolved Hide resolved

public CandidateState(Integer epoch) {
this.epoch = epoch;
}

@Override
public RoleState transform(StateMachineContext context) {
RoleState.randomPark(context);
int epoch = this.epoch == null ? 1 : this.epoch;
MetaData metaData = new MetaData(context.config().node(), epoch);
//failover to master success
context.epoch(metaData.epoch());
if (context.adapter().postDelyIfPresent(metaData, -1)) {
return new MasterState(metaData);
} else {
return new WorkerState(metaData);
}
}

@Override
public Callback callback(StateMachineCallback callback) {
return callback::candidate;
}
}

private static class StateMachineContextImpl implements StateMachineContext {

private Integer epoch;
private final String node;
private final RoleElectionStateMachineImpl machine;

public StateMachineContextImpl(RoleElectionStateMachineImpl machine) {
this.node = machine.config.node();
this.machine = machine;
}

@Override
public Integer epoch() {
return this.epoch;
}

@Override
public String node() {
return this.node;
}

@Override
public void epoch(Integer epoch) {
this.epoch = epoch;
}

@Override
public MetaDataAdapter adapter() {
return this.machine.adapter();
}

@Override
public Config config() {
return this.machine.config;
}

@Override
public RoleElectionStateMachine stateMachine() {
return this.machine;
}

@Override
public void reset() {
this.epoch = null;
}
}

protected MetaDataAdapter adapter() {
return this.metaDataAdapter;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.baidu.hugegraph.election;

public interface StateMachineCallback {

void master(StateMachineContext context);

void worker(StateMachineContext context);

void candidate(StateMachineContext context);

void unknown(StateMachineContext context);

void safe(StateMachineContext context);
zyxxoo marked this conversation as resolved.
Show resolved Hide resolved

void error(StateMachineContext context, Throwable e);
}
Loading