Package net.kuujo.copycat.functional

Source Code of net.kuujo.copycat.functional.LeaderElectionTest

/*
* Copyright 2014 the original author or authors.
*
* Licensed 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 net.kuujo.copycat.functional;

import net.kuujo.copycat.CopycatState;
import net.kuujo.copycat.cluster.ClusterConfig;
import net.kuujo.copycat.cluster.Member;
import net.kuujo.copycat.internal.log.ConfigurationEntry;
import net.kuujo.copycat.internal.log.OperationEntry;
import net.kuujo.copycat.protocol.LocalProtocol;
import net.kuujo.copycat.spi.protocol.Protocol;
import net.kuujo.copycat.test.TestCluster;
import net.kuujo.copycat.test.TestLog;
import net.kuujo.copycat.test.TestNode;
import net.kuujo.copycat.test.TestStateMachine;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.Arrays;

/**
* Leader election tests.
*
* @author <a href="http://github.com/kuujo">Jordan Halterman</a>
*/
@Test
@SuppressWarnings("unchecked")
public class LeaderElectionTest {

  /**
   * Tests that a leader is elected in a single-node cluster.
   */
  public void testSingleNodeClusterLeaderIsElected() {
    Protocol<Member> protocol = new LocalProtocol();
    TestCluster cluster = new TestCluster();
    TestNode node1 = new TestNode().withCluster("foo").withProtocol(protocol);
    cluster.addNode(node1);
    cluster.start();
    node1.await().electedLeader();
    Assert.assertTrue(node1.instance().isLeader());
    cluster.stop();
  }

  /**
   * Tests that a leader is elected in a double-node cluster.
   */
  public void testTwoNodeClusterLeaderIsElected() {
    Protocol<Member> protocol = new LocalProtocol();
    TestCluster cluster = new TestCluster();
    TestNode node1 = new TestNode().withCluster("foo", "bar").withProtocol(protocol);
    cluster.addNode(node1);
    TestNode node2 = new TestNode().withCluster("bar", "foo").withProtocol(protocol);
    cluster.addNode(node2);
    cluster.start();
    node1.await().leaderElected();
    Assert.assertTrue(node1.instance().isLeader() || node2.instance().isLeader());
    cluster.stop();
  }

  /**
   * Tests that a leader is elected in a triple-node cluster.
   */
  public void testThreeNodeClusterLeaderIsElected() {
    Protocol<Member> protocol = new LocalProtocol();
    TestCluster cluster = new TestCluster();
    TestNode node1 = new TestNode().withCluster("foo", "bar", "baz").withProtocol(protocol);
    cluster.addNode(node1);
    TestNode node2 = new TestNode().withCluster("bar", "foo", "baz").withProtocol(protocol);
    cluster.addNode(node2);
    TestNode node3 = new TestNode().withCluster("baz", "foo", "bar").withProtocol(protocol);
    cluster.addNode(node3);
    cluster.start();
    node1.await().leaderElected();
    Assert.assertTrue(node1.instance().isLeader() || node2.instance().isLeader() || node3.instance().isLeader());
    cluster.stop();
  }

  /**
   * Tests that the candidate with the most up-to-date log is elected on startup.
   */
  public void testCandidateWithMostUpToDateLogIsElectedOnStartup() {
    Protocol<Member> protocol = new LocalProtocol();
    TestCluster cluster = new TestCluster();
    TestNode node1 = new TestNode()
      .withCluster("foo", "bar", "baz")
      .withProtocol(protocol)
      .withTerm(3)
      .withLeader(null)
      .withStateMachine(new TestStateMachine())
      .withLog(new TestLog()
        .withEntry(new ConfigurationEntry(1, new ClusterConfig()
          .withLocalMember(new Member("foo"))
          .withRemoteMembers(new Member("bar"), new Member("baz"))))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(2, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(2, "foo", Arrays.asList("bar", "baz"))))
      .withState(CopycatState.CANDIDATE)
      .withCommitIndex(6)
      .withLastApplied(6);
    cluster.addNode(node1);

    TestNode node2 = new TestNode()
      .withCluster("bar", "foo", "baz")
      .withProtocol(protocol)
      .withTerm(3)
      .withLeader(null)
      .withStateMachine(new TestStateMachine())
      .withLog(new TestLog()
        .withEntry(new ConfigurationEntry(1, new ClusterConfig()
          .withLocalMember(new Member("bar"))
          .withRemoteMembers(new Member("foo"), new Member("baz"))))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(2, "foo", Arrays.asList("bar", "baz"))))
      .withState(CopycatState.CANDIDATE)
      .withCommitIndex(5)
      .withLastApplied(5);
    cluster.addNode(node2);

    cluster.start();

    node1.await().electedLeader();
    Assert.assertTrue(node1.instance().isLeader());
    cluster.stop();
  }

  /**
   * Test candidate with most up-to-date log elected after failure.
   */
  public void testCandidateWithMostUpToDateLogIsElectedAfterFailure() {
    Protocol<Member> protocol = new LocalProtocol();
    TestCluster cluster = new TestCluster();
    TestNode node1 = new TestNode()
      .withCluster("foo", "bar", "baz")
      .withProtocol(protocol)
      .withTerm(3)
      .withLeader("baz")
      .withStateMachine(new TestStateMachine())
      .withLog(new TestLog()
        .withEntry(new ConfigurationEntry(1, new ClusterConfig()
          .withLocalMember(new Member("foo"))
          .withRemoteMembers(new Member("bar"), new Member("baz"))))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(2, "foo", Arrays.asList("bar", "baz"))))
      .withState(CopycatState.FOLLOWER)
      .withCommitIndex(5)
      .withLastApplied(5)
      .withVotedFor(null);
    cluster.addNode(node1);

    TestNode node2 = new TestNode()
      .withCluster("bar", "foo", "baz")
      .withProtocol(protocol)
      .withTerm(3)
      .withLeader("baz")
      .withStateMachine(new TestStateMachine())
      .withLog(new TestLog()
        .withEntry(new ConfigurationEntry(1, new ClusterConfig()
          .withLocalMember(new Member("bar"))
          .withRemoteMembers(new Member("foo"), new Member("baz"))))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(2, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(2, "foo", Arrays.asList("bar", "baz"))))
      .withState(CopycatState.FOLLOWER)
      .withCommitIndex(6)
      .withLastApplied(6)
      .withVotedFor(null);
    cluster.addNode(node2);

    cluster.start();

    node2.await().electedLeader();
    Assert.assertTrue(node2.instance().isLeader());
    cluster.stop();
  }

  /**
   * Tests that candidates restart an election during a split vote.
   */
  public void testCandidatesIncrementTermAndRestartElectionDuringSplitVote() {
    Protocol<Member> protocol = new LocalProtocol();
    TestCluster cluster = new TestCluster();
    TestNode node1 = new TestNode()
      .withCluster("foo", "bar", "baz")
      .withProtocol(protocol)
      .withTerm(3)
      .withLeader(null)
      .withStateMachine(new TestStateMachine())
      .withLog(new TestLog()
        .withEntry(new ConfigurationEntry(1, new ClusterConfig()
          .withLocalMember(new Member("foo"))
          .withRemoteMembers(new Member("bar"), new Member("baz"))))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(2, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(2, "foo", Arrays.asList("bar", "baz"))))
      .withState(CopycatState.CANDIDATE)
      .withCommitIndex(6)
      .withLastApplied(6)
      .withVotedFor("foo");
    cluster.addNode(node1);

    TestNode node2 = new TestNode()
      .withCluster("bar", "foo", "baz")
      .withProtocol(protocol)
      .withTerm(3)
      .withLeader(null)
      .withStateMachine(new TestStateMachine())
      .withLog(new TestLog()
        .withEntry(new ConfigurationEntry(1, new ClusterConfig()
          .withLocalMember(new Member("bar"))
          .withRemoteMembers(new Member("foo"), new Member("baz"))))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(2, "foo", Arrays.asList("bar", "baz"))))
      .withState(CopycatState.CANDIDATE)
      .withCommitIndex(5)
      .withLastApplied(5)
      .withVotedFor("bar");
    cluster.addNode(node2);

    cluster.start();

    node1.await().electedLeader();
    Assert.assertTrue(node1.instance().isLeader());
    Assert.assertTrue(node1.instance().currentTerm() > 3);
    cluster.stop();
  }

  /**
   * Tests that only a single leader is elected when more than one node is equal in terms of state.
   */
  public void testThatOneLeaderElectedWhenTwoNodesAreEqual() {
    Protocol<Member> protocol = new LocalProtocol();
    TestCluster cluster = new TestCluster();
    TestNode node1 = new TestNode()
      .withCluster("foo", "bar", "baz", "foobar", "barbaz")
      .withProtocol(protocol)
      .withTerm(3)
      .withLeader(null)
      .withStateMachine(new TestStateMachine())
      .withLog(new TestLog()
        .withEntry(new ConfigurationEntry(1, new ClusterConfig()
          .withLocalMember(new Member("foo"))
          .withRemoteMembers(new Member("bar"), new Member("baz"), new Member("foobar"), new Member("barbaz"))))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(2, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(2, "foo", Arrays.asList("bar", "baz"))))
      .withState(CopycatState.FOLLOWER)
      .withCommitIndex(6)
      .withLastApplied(6);
    cluster.addNode(node1);

    TestNode node2 = new TestNode()
      .withCluster("bar", "foo", "baz", "foobar", "barbaz")
      .withProtocol(protocol)
      .withTerm(3)
      .withLeader(null)
      .withStateMachine(new TestStateMachine())
      .withLog(new TestLog()
        .withEntry(new ConfigurationEntry(1, new ClusterConfig()
          .withLocalMember(new Member("bar"))
          .withRemoteMembers(new Member("foo"), new Member("baz"), new Member("foobar"), new Member("barbaz"))))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(2, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(2, "foo", Arrays.asList("bar", "baz"))))
      .withState(CopycatState.FOLLOWER)
      .withCommitIndex(6)
      .withLastApplied(6);
    cluster.addNode(node2);

    TestNode node3 = new TestNode()
      .withCluster("baz", "bar", "foo", "foobar", "barbaz")
      .withProtocol(protocol)
      .withTerm(3)
      .withLeader(null)
      .withStateMachine(new TestStateMachine())
      .withLog(new TestLog()
        .withEntry(new ConfigurationEntry(1, new ClusterConfig()
          .withLocalMember(new Member("baz"))
          .withRemoteMembers(new Member("bar"), new Member("foo"), new Member("foobar"), new Member("barbaz"))))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(2, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(2, "foo", Arrays.asList("bar", "baz"))))
      .withState(CopycatState.FOLLOWER)
      .withCommitIndex(6)
      .withLastApplied(6);
    cluster.addNode(node3);

    TestNode node4 = new TestNode()
      .withCluster("foobar", "foo", "bar", "baz", "barbaz")
      .withProtocol(protocol)
      .withTerm(3)
      .withLeader(null)
      .withStateMachine(new TestStateMachine())
      .withLog(new TestLog()
        .withEntry(new ConfigurationEntry(1, new ClusterConfig()
          .withLocalMember(new Member("foobar"))
          .withRemoteMembers(new Member("bar"), new Member("baz"), new Member("foo"), new Member("barbaz"))))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(1, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(2, "foo", Arrays.asList("bar", "baz")))
        .withEntry(new OperationEntry(2, "foo", Arrays.asList("bar", "baz"))))
      .withState(CopycatState.FOLLOWER)
      .withCommitIndex(6)
      .withLastApplied(6);
    cluster.addNode(node4);

    cluster.start();

    node1.await().leaderElected();

    int leaderCount = 0;
    for (TestNode node : cluster.nodes()) {
      if (node.instance().isLeader()) {
        leaderCount++;
      }
    }

    Assert.assertEquals(1, leaderCount);
    cluster.stop();
  }

}
TOP

Related Classes of net.kuujo.copycat.functional.LeaderElectionTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.