javeme commented on code in PR #1943:
URL: 
https://github.com/apache/incubator-hugegraph/pull/1943#discussion_r952128694


##########
hugegraph-test/src/main/java/com/baidu/hugegraph/core/RoleElectionStateMachineTest.java:
##########
@@ -0,0 +1,320 @@
+/*
+ * 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.Test;
+
+import com.baidu.hugegraph.election.Config;
+import com.baidu.hugegraph.election.RoleTypeData;
+import com.baidu.hugegraph.election.RoleTypeDataAdapter;
+import com.baidu.hugegraph.election.RoleElectionStateMachine;
+import com.baidu.hugegraph.election.RoleElectionStateMachineImpl;
+import com.baidu.hugegraph.election.StateMachineCallback;
+import com.baidu.hugegraph.election.StateMachineContext;
+import com.baidu.hugegraph.testutil.Assert;
+
+public class RoleElectionStateMachineTest {
+
+    public static class LogEntry {
+
+        Integer epoch;
+
+        String node;
+
+        Role role;
+
+        enum Role {
+            master,
+            worker,
+            candidate,
+            abdication,
+            unknown
+        }
+
+        public LogEntry(Integer epoch, String node, Role role) {
+            this.epoch = epoch;
+            this.node = node;
+            this.role = role;
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (this == obj) {
+                return true;
+            }
+            if (!(obj instanceof LogEntry)) {
+                return false;
+            }
+            LogEntry logEntry = (LogEntry) obj;
+            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 abdication(StateMachineContext context) {
+                Integer epochId = context.epoch();
+                String node = context.node();
+                logRecords.add(new LogEntry(epochId, node, 
LogEntry.Role.abdication));
+                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<RoleTypeData> metaDataLogs = 
Collections.synchronizedList(new ArrayList<>(100));
+        final RoleTypeDataAdapter adapter = new RoleTypeDataAdapter() {
+
+            volatile int epoch = 0;
+
+            final Map<Integer, RoleTypeData> data = new ConcurrentHashMap<>();
+
+            RoleTypeData copy(RoleTypeData stateData) {
+                if (stateData == null) {
+                    return null;
+                }
+                return new RoleTypeData(stateData.node(), stateData.epoch(), 
stateData.clock());
+            }
+
+            @Override
+            public boolean updateIfNodePresent(RoleTypeData stateData) {
+                if (stateData.epoch() < this.epoch) {
+                    return false;
+                }
+
+                RoleTypeData copy = this.copy(stateData);
+                RoleTypeData 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.clock() <= copy.clock()) {
+                        System.out.println("----2" + copy);

Review Comment:
   ditto



##########
hugegraph-test/src/main/java/com/baidu/hugegraph/core/RoleElectionStateMachineTest.java:
##########
@@ -0,0 +1,320 @@
+/*
+ * 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.Test;
+
+import com.baidu.hugegraph.election.Config;
+import com.baidu.hugegraph.election.RoleTypeData;
+import com.baidu.hugegraph.election.RoleTypeDataAdapter;
+import com.baidu.hugegraph.election.RoleElectionStateMachine;
+import com.baidu.hugegraph.election.RoleElectionStateMachineImpl;
+import com.baidu.hugegraph.election.StateMachineCallback;
+import com.baidu.hugegraph.election.StateMachineContext;
+import com.baidu.hugegraph.testutil.Assert;
+
+public class RoleElectionStateMachineTest {
+
+    public static class LogEntry {
+
+        Integer epoch;
+
+        String node;
+
+        Role role;
+
+        enum Role {
+            master,
+            worker,
+            candidate,
+            abdication,
+            unknown
+        }
+
+        public LogEntry(Integer epoch, String node, Role role) {
+            this.epoch = epoch;
+            this.node = node;
+            this.role = role;
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (this == obj) {
+                return true;
+            }
+            if (!(obj instanceof LogEntry)) {
+                return false;
+            }
+            LogEntry logEntry = (LogEntry) obj;
+            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 abdication(StateMachineContext context) {
+                Integer epochId = context.epoch();
+                String node = context.node();
+                logRecords.add(new LogEntry(epochId, node, 
LogEntry.Role.abdication));
+                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<RoleTypeData> metaDataLogs = 
Collections.synchronizedList(new ArrayList<>(100));
+        final RoleTypeDataAdapter adapter = new RoleTypeDataAdapter() {
+
+            volatile int epoch = 0;
+
+            final Map<Integer, RoleTypeData> data = new ConcurrentHashMap<>();
+
+            RoleTypeData copy(RoleTypeData stateData) {
+                if (stateData == null) {
+                    return null;
+                }
+                return new RoleTypeData(stateData.node(), stateData.epoch(), 
stateData.clock());
+            }
+
+            @Override
+            public boolean updateIfNodePresent(RoleTypeData stateData) {
+                if (stateData.epoch() < this.epoch) {
+                    return false;
+                }
+
+                RoleTypeData copy = this.copy(stateData);
+                RoleTypeData 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.clock() <= copy.clock()) {
+                        System.out.println("----2" + copy);
+                        metaDataLogs.add(copy);
+                        if (value.clock() == copy.clock()) {
+                            Exception e = new Exception("eq");
+                            e.printStackTrace();

Review Comment:
   call assertFail?



##########
hugegraph-test/src/main/java/com/baidu/hugegraph/core/RoleElectionStateMachineTest.java:
##########
@@ -0,0 +1,320 @@
+/*
+ * 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.Test;
+
+import com.baidu.hugegraph.election.Config;
+import com.baidu.hugegraph.election.RoleTypeData;
+import com.baidu.hugegraph.election.RoleTypeDataAdapter;
+import com.baidu.hugegraph.election.RoleElectionStateMachine;
+import com.baidu.hugegraph.election.RoleElectionStateMachineImpl;
+import com.baidu.hugegraph.election.StateMachineCallback;
+import com.baidu.hugegraph.election.StateMachineContext;
+import com.baidu.hugegraph.testutil.Assert;
+
+public class RoleElectionStateMachineTest {
+
+    public static class LogEntry {
+
+        Integer epoch;
+
+        String node;
+
+        Role role;
+
+        enum Role {
+            master,
+            worker,
+            candidate,
+            abdication,
+            unknown
+        }
+
+        public LogEntry(Integer epoch, String node, Role role) {
+            this.epoch = epoch;
+            this.node = node;
+            this.role = role;
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (this == obj) {
+                return true;
+            }
+            if (!(obj instanceof LogEntry)) {
+                return false;
+            }
+            LogEntry logEntry = (LogEntry) obj;
+            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 abdication(StateMachineContext context) {
+                Integer epochId = context.epoch();
+                String node = context.node();
+                logRecords.add(new LogEntry(epochId, node, 
LogEntry.Role.abdication));
+                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<RoleTypeData> metaDataLogs = 
Collections.synchronizedList(new ArrayList<>(100));
+        final RoleTypeDataAdapter adapter = new RoleTypeDataAdapter() {
+
+            volatile int epoch = 0;
+
+            final Map<Integer, RoleTypeData> data = new ConcurrentHashMap<>();
+
+            RoleTypeData copy(RoleTypeData stateData) {
+                if (stateData == null) {
+                    return null;
+                }
+                return new RoleTypeData(stateData.node(), stateData.epoch(), 
stateData.clock());
+            }
+
+            @Override
+            public boolean updateIfNodePresent(RoleTypeData stateData) {
+                if (stateData.epoch() < this.epoch) {
+                    return false;
+                }
+
+                RoleTypeData copy = this.copy(stateData);
+                RoleTypeData 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);

Review Comment:
   improve print message



-- 
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]

Reply via email to