javeme commented on code in PR #1943: URL: https://github.com/apache/incubator-hugegraph/pull/1943#discussion_r947353222
########## hugegraph-core/src/main/java/com/baidu/hugegraph/election/RoleStateData.java: ########## @@ -0,0 +1,91 @@ +/* + * 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.Objects; + +public class RoleStateData { + + private String node; + private long count; Review Comment: rename to clock or times? ########## hugegraph-core/src/main/java/com/baidu/hugegraph/election/RoleElectionStateMachineImpl.java: ########## @@ -0,0 +1,310 @@ +/* + * 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; +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; Review Comment: move assignment to the constructor ########## hugegraph-core/src/main/java/com/baidu/hugegraph/election/RoleElectionStateMachineImpl.java: ########## @@ -9,15 +28,13 @@ public class RoleElectionStateMachineImpl implements RoleElectionStateMachine { private volatile boolean shutdown = false; - private Config config; - + private final Config config; private volatile RoleState state; + private final RoleStataDataAdapter roleStataDataAdapter; Review Comment: "Stata" -- typo ########## hugegraph-test/src/main/java/com/baidu/hugegraph/core/RoleElectionStateMachineTest.java: ########## @@ -0,0 +1,324 @@ +/* + * 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.core; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.locks.LockSupport; + +import org.junit.Assert; +import org.junit.Test; + +import com.baidu.hugegraph.election.Config; +import com.baidu.hugegraph.election.RoleStateData; +import com.baidu.hugegraph.election.RoleStataDataAdapter; +import com.baidu.hugegraph.election.RoleElectionStateMachine; +import com.baidu.hugegraph.election.RoleElectionStateMachineImpl; +import com.baidu.hugegraph.election.StateMachineCallback; +import com.baidu.hugegraph.election.StateMachineContext; + +public class RoleElectionStateMachineTest { + + public static class LogEntry { + + Integer epoch; + + String node; + + Role role; + + enum Role { + master, + worker, + candidate, + safe, + unknown + } + + public LogEntry(Integer epoch, String node, Role role) { + this.epoch = epoch; + this.node = node; + this.role = role; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof LogEntry)) return false; + LogEntry logEntry = (LogEntry) o; + return Objects.equals(epoch, logEntry.epoch) && Objects.equals(node, logEntry.node) && role == logEntry.role; + } + + @Override + public int hashCode() { + return Objects.hash(epoch, node, role); + } + + @Override + public String toString() { + return "LogEntry{" + + "epoch=" + epoch + + ", node='" + node + '\'' + + ", role=" + role + + '}'; + } + } + + private static class TestConfig implements Config { + + String node; + + public TestConfig(String node) { + this.node = node; + } + + @Override + public String node() { + return this.node; + } + + @Override + public int exceedsFailCount() { + return 2; + } + + @Override + public long randomTimeoutMillisecond() { + return 400; + } + + @Override + public long heartBeatIntervalSecond() { + return 1; + } + + @Override + public int exceedsWorkerCount() { + return 5; + } + + @Override + public long baseTimeoutMillisecond() { + return 100; + } + } + + @Test + public void testStateMachine() throws InterruptedException { + final CountDownLatch stop = new CountDownLatch(4); + final int MAX_COUNT = 200; + final List<LogEntry> logRecords = Collections.synchronizedList(new ArrayList<>(MAX_COUNT)); + final List<String> masterNodes = Collections.synchronizedList(new ArrayList<>(MAX_COUNT)); + final StateMachineCallback callback = new StateMachineCallback() { + + @Override + public void master(StateMachineContext context) { + Integer epochId = context.epoch(); + String node = context.node(); + logRecords.add(new LogEntry(epochId, node, LogEntry.Role.master)); + if (logRecords.size() > MAX_COUNT) { + context.stateMachine().shutdown(); + } + System.out.println("----master " + node); + masterNodes.add(node); + } + + @Override + public void worker(StateMachineContext context) { + Integer epochId = context.epoch(); + String node = context.node(); + logRecords.add(new LogEntry(epochId, node, LogEntry.Role.worker)); + if (logRecords.size() > MAX_COUNT) { + context.stateMachine().shutdown(); + } + } + + @Override + public void candidate(StateMachineContext context) { + Integer epochId = context.epoch(); + String node = context.node(); + logRecords.add(new LogEntry(epochId, node, LogEntry.Role.candidate)); + if (logRecords.size() > MAX_COUNT) { + context.stateMachine().shutdown(); + } + } + + @Override + public void unknown(StateMachineContext context) { + Integer epochId = context.epoch(); + String node = context.node(); + logRecords.add(new LogEntry(epochId, node, LogEntry.Role.unknown)); + if (logRecords.size() > MAX_COUNT) { + context.stateMachine().shutdown(); + } + } + + @Override + public void safe(StateMachineContext context) { + Integer epochId = context.epoch(); + String node = context.node(); + logRecords.add(new LogEntry(epochId, node, LogEntry.Role.safe)); + if (logRecords.size() > MAX_COUNT) { + context.stateMachine().shutdown(); + } + } + + @Override + public void error(StateMachineContext context, Throwable e) { + System.out.println("----" + context.node() + " " + e.getMessage()); + } + }; + + final List<RoleStateData> metaDataLogs = Collections.synchronizedList(new ArrayList<>(100)); + final RoleStataDataAdapter adapter = new RoleStataDataAdapter() { + + volatile int epoch = 0; + + final Map<Integer, RoleStateData> data = new ConcurrentHashMap<>(); + + RoleStateData copy(RoleStateData stateData) { + if (stateData == null) { + return null; + } + return new RoleStateData(stateData.node(), stateData.epoch(), stateData.count()); + } + + @Override + public boolean delayIfNodePresent(RoleStateData stateData, long delaySecond) { + if (delaySecond > 0) { + LockSupport.parkNanos(delaySecond * 1_000_000_000); + } + if (stateData.epoch() < this.epoch) { + return false; + } + + RoleStateData copy = this.copy(stateData); + RoleStateData newData = data.compute(copy.epoch(), (key, value) -> { + if (copy.epoch() > this.epoch) { + this.epoch = copy.epoch(); + Assert.assertNull(value); + metaDataLogs.add(copy); + System.out.println("----1" + copy); + return copy; + } + + Assert.assertEquals(value.epoch(), copy.epoch()); + if (Objects.equals(value.node(), copy.node()) && + value.count() <= copy.count()) { + System.out.println("----2" + copy); + metaDataLogs.add(copy); + if (value.count() == copy.count()) { + Exception e = new Exception("eq"); + e.printStackTrace(); + } + return copy; + } + return value; + + }); + return Objects.equals(newData, copy); + } + + @Override + public Optional<RoleStateData> queryWithDelay(long delaySecond) { + LockSupport.parkNanos(delaySecond * 1_000_000_000); + return Optional.ofNullable(this.copy(this.data.get(this.epoch))); + } + + @Override + public Optional<RoleStateData> query() { + return Optional.ofNullable(this.copy(this.data.get(this.epoch))); + } + }; + + RoleElectionStateMachine[] machines = new RoleElectionStateMachine[4]; + Thread node1 = new Thread(() -> { + Config config = new TestConfig("1"); + RoleElectionStateMachine stateMachine = new RoleElectionStateMachineImpl(config, adapter); + machines[1] = stateMachine; + stateMachine.apply(callback); + stop.countDown(); + }); + + Thread node2 = new Thread(() -> { + Config config = new TestConfig("2"); + RoleElectionStateMachine stateMachine = new RoleElectionStateMachineImpl(config, adapter); + machines[2] = stateMachine; + stateMachine.apply(callback); + stop.countDown(); + }); + + Thread node3 = new Thread(() -> { + Config config = new TestConfig("3"); + RoleElectionStateMachine stateMachine = new RoleElectionStateMachineImpl(config, adapter); + machines[3] = stateMachine; + stateMachine.apply(callback); + stop.countDown(); + }); + + node1.start(); + node2.start(); + node3.start(); + + Thread randomShutdown = new Thread(() -> { + Set<String> dropNodes = new HashSet<>(); + while (dropNodes.size() < 3) { + LockSupport.parkNanos(5_000_000_000L); + int size = masterNodes.size(); + if (size < 1) { + continue; + } + String node = masterNodes.get(size - 1); + if (dropNodes.contains(node)) { + continue; + } + machines[Integer.parseInt(node)].shutdown(); + dropNodes.add(node); + System.out.println("----shutdown machine " + node); + } + stop.countDown(); + }); + + randomShutdown.start(); + stop.await(); + + Assert.assertTrue(logRecords.size() > 0); + Map<Integer, String> masters = new HashMap<>(); + for (LogEntry entry: logRecords) { + if (entry.role == LogEntry.Role.master) { + String lastNode = masters.putIfAbsent(entry.epoch, entry.node); + if (lastNode != null) { + Assert.assertEquals(lastNode, entry.node); + } + } + } + + Assert.assertTrue(masters.size() > 0); Review Comment: we can import util.Assert and use Assert.assertGt here ########## hugegraph-core/src/main/java/com/baidu/hugegraph/election/StateMachineCallback.java: ########## @@ -0,0 +1,35 @@ +/* + * 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 StateMachineCallback { + + void master(StateMachineContext context); + + void worker(StateMachineContext context); + + void candidate(StateMachineContext context); + + void unknown(StateMachineContext context); + + void safe(StateMachineContext context); Review Comment: rename safe to abdication? ########## hugegraph-core/src/main/java/com/baidu/hugegraph/election/RoleStataDataAdapter.java: ########## @@ -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); Review Comment: updateIfNodePresent ########## hugegraph-core/src/main/java/com/baidu/hugegraph/election/RoleStateData.java: ########## @@ -0,0 +1,91 @@ +/* + * 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.Objects; + +public class RoleStateData { + + private String node; + private long count; + private int epoch; + + public RoleStateData(String node, int epoch) { + this(node, epoch, 1); + } + + public RoleStateData(String node, int epoch, long count) { + this.node = node; + this.epoch = epoch; + this.count = count; + } + + 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; + } + + public void count(long count) { + this.count = count; + } + + public String node() { + return this.node; + } + + @Override + public boolean equals(Object o) { Review Comment: o looks like 0, can rename to obj? ########## hugegraph-core/src/main/java/com/baidu/hugegraph/election/RoleElectionStateMachineImpl.java: ########## @@ -0,0 +1,310 @@ +/* + * 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; +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; + private final Config config; + private volatile RoleState state; + private final RoleStataDataAdapter roleStataDataAdapter; + + public RoleElectionStateMachineImpl(Config config, RoleStataDataAdapter adapter) { + this.config = config; + this.roleStataDataAdapter = 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 { + + SecureRandom secureRandom = new SecureRandom(); + + 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(); + long baseTime = context.config().baseTimeoutMillisecond(); + long timeout = (long) (baseTime + (randomTimeout / 10.0 * secureRandom.nextInt(11))); + LockSupport.parkNanos(timeout * 1_000_000); + } + } + + @FunctionalInterface + private interface Callback { + + void call(StateMachineContext context); + } + + private static class UnKnownState implements RoleState { + + final Integer epoch; + + public UnKnownState(Integer epoch) { + this.epoch = epoch; + } + + @Override + public RoleState transform(StateMachineContext context) { + RoleStataDataAdapter adapter = context.adapter(); + Optional<RoleStateData> stateDataOpt = adapter.query(); + if (!stateDataOpt.isPresent()) { + context.reset(); + Integer nextEpoch = this.epoch == null ? 1 : this.epoch + 1; + context.epoch(nextEpoch); + return new CandidateState(nextEpoch); + } + + RoleStateData stateData = stateDataOpt.get(); + if (this.epoch != null && stateData.epoch() < this.epoch) { + context.reset(); + Integer nextEpoch = this.epoch + 1; + context.epoch(nextEpoch); + return new CandidateState(nextEpoch); + } + + context.epoch(stateData.epoch()); + if (stateData.isMaster(context.node())) { + return new MasterState(stateData); + } else { + return new WorkerState(stateData); + } + } + + @Override + public Callback callback(StateMachineCallback callback) { + return callback::unknown; + } + } + + private static class SafeState implements RoleState { + + private final Integer epoch; + + 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 { + + private final RoleStateData stateData; + + public MasterState(RoleStateData stateData) { + this.stateData = stateData; + } + + @Override + public RoleState transform(StateMachineContext context) { + this.stateData.increaseCount(); + RoleState.heartBeatPark(context); + if (context.adapter().delayIfNodePresent(this.stateData, -1)) { + return this; + } + context.reset(); + context.epoch(this.stateData.epoch()); + return new UnKnownState(this.stateData.epoch()).transform(context); + } + + @Override + public Callback callback(StateMachineCallback callback) { + return callback::master; + } + } + + private static class WorkerState implements RoleState { + + private RoleStateData stateData; + private int count = 0; Review Comment: move assignment to the constructor ########## hugegraph-core/src/main/java/com/baidu/hugegraph/election/RoleElectionStateMachineImpl.java: ########## @@ -0,0 +1,310 @@ +/* + * 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; +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; + private final Config config; + private volatile RoleState state; + private final RoleStataDataAdapter roleStataDataAdapter; + + public RoleElectionStateMachineImpl(Config config, RoleStataDataAdapter adapter) { + this.config = config; + this.roleStataDataAdapter = 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 { + + SecureRandom secureRandom = new SecureRandom(); + + 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(); + long baseTime = context.config().baseTimeoutMillisecond(); + long timeout = (long) (baseTime + (randomTimeout / 10.0 * secureRandom.nextInt(11))); + LockSupport.parkNanos(timeout * 1_000_000); + } + } + + @FunctionalInterface + private interface Callback { + + void call(StateMachineContext context); + } + + private static class UnKnownState implements RoleState { + + final Integer epoch; + + public UnKnownState(Integer epoch) { + this.epoch = epoch; + } + + @Override + public RoleState transform(StateMachineContext context) { + RoleStataDataAdapter adapter = context.adapter(); + Optional<RoleStateData> stateDataOpt = adapter.query(); + if (!stateDataOpt.isPresent()) { + context.reset(); + Integer nextEpoch = this.epoch == null ? 1 : this.epoch + 1; + context.epoch(nextEpoch); + return new CandidateState(nextEpoch); + } + + RoleStateData stateData = stateDataOpt.get(); + if (this.epoch != null && stateData.epoch() < this.epoch) { + context.reset(); + Integer nextEpoch = this.epoch + 1; + context.epoch(nextEpoch); + return new CandidateState(nextEpoch); + } + + context.epoch(stateData.epoch()); + if (stateData.isMaster(context.node())) { + return new MasterState(stateData); + } else { + return new WorkerState(stateData); + } + } + + @Override + public Callback callback(StateMachineCallback callback) { + return callback::unknown; + } + } + + private static class SafeState implements RoleState { Review Comment: rename class name ########## hugegraph-test/src/main/java/com/baidu/hugegraph/core/RoleElectionStateMachineTest.java: ########## @@ -0,0 +1,324 @@ +/* + * 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.core; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.locks.LockSupport; + +import org.junit.Assert; +import org.junit.Test; + +import com.baidu.hugegraph.election.Config; +import com.baidu.hugegraph.election.RoleStateData; +import com.baidu.hugegraph.election.RoleStataDataAdapter; +import com.baidu.hugegraph.election.RoleElectionStateMachine; +import com.baidu.hugegraph.election.RoleElectionStateMachineImpl; +import com.baidu.hugegraph.election.StateMachineCallback; +import com.baidu.hugegraph.election.StateMachineContext; + +public class RoleElectionStateMachineTest { + + public static class LogEntry { + + Integer epoch; + + String node; + + Role role; + + enum Role { + master, + worker, + candidate, + safe, + unknown + } + + public LogEntry(Integer epoch, String node, Role role) { + this.epoch = epoch; + this.node = node; + this.role = role; + } + + @Override + public boolean equals(Object o) { Review Comment: ditto -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
