Skip to content

Commit

Permalink
improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxxoo committed Aug 16, 2022
1 parent faadb23 commit d31b13d
Show file tree
Hide file tree
Showing 10 changed files with 308 additions and 146 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*
* Copyright 2017 HugeGraph Authors
*
* 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.baidu.hugegraph.election;

public interface Config {
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*
* Copyright 2017 HugeGraph Authors
*
* 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.baidu.hugegraph.election;

public interface RoleElectionStateMachine {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*
* Copyright 2017 HugeGraph Authors
*
* 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.baidu.hugegraph.election;

import java.security.SecureRandom;
Expand All @@ -9,15 +28,15 @@
public class RoleElectionStateMachineImpl implements RoleElectionStateMachine {

private volatile boolean shutdown = false;
private Config config;
private final Config config;

private volatile RoleState state;

private final MetaDataAdapter metaDataAdapter;
private final RoleStataDataAdapter roleStataDataAdapter;

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

Expand Down Expand Up @@ -78,36 +97,36 @@ private interface Callback {

private static class UnKnownState implements RoleState {

Integer epoch;
final Integer epoch;

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()) {
RoleStataDataAdapter adapter = context.adapter();
Optional<RoleStateData> stateDataOpt = adapter.query();
if (!stateDataOpt.isPresent()) {
context.reset();
this.epoch = this.epoch == null ? 1 : this.epoch + 1;
context.epoch(this.epoch);
return new CandidateState(this.epoch);
Integer nextEpoch = this.epoch == null ? 1 : this.epoch + 1;
context.epoch(nextEpoch);
return new CandidateState(nextEpoch);
}

MetaData metaData = metaDataOpt.get();
if (this.epoch != null && metaData.epoch() < this.epoch) {
RoleStateData stateData = stateDataOpt.get();
if (this.epoch != null && stateData.epoch() < this.epoch) {
context.reset();
this.epoch = this.epoch == null ? 1 : this.epoch + 1;
context.epoch(this.epoch);
return new CandidateState(this.epoch);
Integer nextEpoch = this.epoch + 1;
context.epoch(nextEpoch);
return new CandidateState(nextEpoch);
}

context.epoch(metaData.epoch());
if (metaData.isMaster(context.node())) {
return new MasterState(metaData);
context.epoch(stateData.epoch());
if (stateData.isMaster(context.node())) {
return new MasterState(stateData);
} else {
return new WorkerState(metaData);
return new WorkerState(stateData);
}
}

Expand All @@ -119,7 +138,7 @@ public Callback callback(StateMachineCallback callback) {

private static class SafeState implements RoleState {

Integer epoch;
private final Integer epoch;

public SafeState(Integer epoch) {
this.epoch = epoch;
Expand All @@ -139,22 +158,22 @@ public Callback callback(StateMachineCallback callback) {

private static class MasterState implements RoleState {

MetaData metaData;
private final RoleStateData stateData;

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

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

@Override
Expand All @@ -165,21 +184,21 @@ public Callback callback(StateMachineCallback callback) {

private static class WorkerState implements RoleState {

private MetaData metaData;
private RoleStateData stateData;
private int count = 0;

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

@Override
public RoleState transform(StateMachineContext context) {
RoleState.heartBeatPark(context);
RoleState nextState = new UnKnownState(this.metaData.epoch()).transform(context);
RoleState nextState = new UnKnownState(this.stateData.epoch()).transform(context);
if (nextState instanceof WorkerState) {
this.merge((WorkerState) nextState);
if (this.count > context.config().exceedsWorkerCount()) {
return new CandidateState(this.metaData.epoch() + 1);
return new CandidateState(this.stateData.epoch() + 1);
} else {
return this;
}
Expand All @@ -194,18 +213,18 @@ public Callback callback(StateMachineCallback callback) {
}

public void merge(WorkerState state) {
if (state.metaData.epoch() > this.metaData.epoch()) {
if (state.stateData.epoch() > this.stateData.epoch()) {
this.count = 0;
this.metaData = state.metaData;
} else if (state.metaData.epoch() < this.metaData.epoch()){
this.stateData = state.stateData;
} else if (state.stateData.epoch() < this.stateData.epoch()){
throw new IllegalStateException("Epoch must increase");
} else if (state.metaData.epoch() == this.metaData.epoch() &&
state.metaData.count() < this.metaData.count()) {
} else if (state.stateData.epoch() == this.stateData.epoch() &&
state.stateData.count() < this.stateData.count()) {
throw new IllegalStateException("Meta count must increase");
} else if (state.metaData.epoch() == this.metaData.epoch() &&
state.metaData.count() > this.metaData.count()) {
} else if (state.stateData.epoch() == this.stateData.epoch() &&
state.stateData.count() > this.stateData.count()) {
this.count = 0;
this.metaData = state.metaData;
this.stateData = state.stateData;
} else {
this.count++;
}
Expand All @@ -214,7 +233,7 @@ public void merge(WorkerState state) {

private static class CandidateState implements RoleState {

Integer epoch;
private final Integer epoch;

public CandidateState(Integer epoch) {
this.epoch = epoch;
Expand All @@ -224,13 +243,13 @@ public CandidateState(Integer epoch) {
public RoleState transform(StateMachineContext context) {
RoleState.randomPark(context);
int epoch = this.epoch == null ? 1 : this.epoch;
MetaData metaData = new MetaData(context.config().node(), epoch);
RoleStateData stateData = new RoleStateData(context.config().node(), epoch);
//failover to master success
context.epoch(metaData.epoch());
if (context.adapter().postDelyIfPresent(metaData, -1)) {
return new MasterState(metaData);
context.epoch(stateData.epoch());
if (context.adapter().delayIfNodePresent(stateData, -1)) {
return new MasterState(stateData);
} else {
return new WorkerState(metaData);
return new WorkerState(stateData);
}
}

Expand Down Expand Up @@ -267,7 +286,7 @@ public void epoch(Integer epoch) {
}

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

Expand All @@ -287,7 +306,7 @@ public void reset() {
}
}

protected MetaDataAdapter adapter() {
return this.metaDataAdapter;
protected RoleStataDataAdapter adapter() {
return this.roleStataDataAdapter;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2017 HugeGraph Authors
*
* 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.baidu.hugegraph.election;

import java.util.Optional;

public interface RoleStataDataAdapter {

boolean delayIfNodePresent(RoleStateData metaData, long delaySecond);

Optional<RoleStateData> queryWithDelay(long delaySecond);

Optional<RoleStateData> query();
}
Loading

0 comments on commit d31b13d

Please sign in to comment.