Search in sources :

Example 36 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.

the class NodeTest method testChangePeersChaosWithoutSnapshot.

@Test
public void testChangePeersChaosWithoutSnapshot() throws Exception {
    // start cluster
    final List<PeerId> peers = new ArrayList<>();
    peers.add(new PeerId("127.0.0.1", TestUtils.INIT_PORT));
    final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers, 1000);
    assertTrue(cluster.start(peers.get(0).getEndpoint(), false, 100000));
    // start other peers
    for (int i = 1; i < 10; i++) {
        final PeerId peer = new PeerId("127.0.0.1", TestUtils.INIT_PORT + i);
        peers.add(peer);
        assertTrue(cluster.start(peer.getEndpoint(), true, 10000));
    }
    final ChangeArg arg = new ChangeArg(cluster, peers, false, true);
    final Future<?> future = startChangePeersThread(arg);
    final int tasks = 5000;
    for (int i = 0; i < tasks; ) {
        cluster.waitLeader();
        final Node leader = cluster.getLeader();
        if (leader == null) {
            continue;
        }
        final SynchronizedClosure done = new SynchronizedClosure();
        final Task task = new Task(ByteBuffer.wrap(("hello" + i).getBytes()), done);
        leader.apply(task);
        final Status status = done.await();
        if (status.isOk()) {
            if (++i % 100 == 0) {
                System.out.println("Progress:" + i);
            }
        } else {
            assertEquals(RaftError.EPERM, status.getRaftError());
        }
    }
    arg.stop = true;
    future.get();
    cluster.waitLeader();
    final SynchronizedClosure done = new SynchronizedClosure();
    final Node leader = cluster.getLeader();
    leader.changePeers(new Configuration(peers), done);
    assertTrue(done.await().isOk());
    cluster.ensureSame();
    assertEquals(10, cluster.getFsms().size());
    for (final MockStateMachine fsm : cluster.getFsms()) {
        assertTrue(fsm.getLogs().size() >= tasks);
        assertTrue(fsm.getLogs().size() - tasks < 100);
    }
    cluster.stopAll();
}
Also used : Status(com.alipay.sofa.jraft.Status) SynchronizedClosure(com.alipay.sofa.jraft.closure.SynchronizedClosure) Task(com.alipay.sofa.jraft.entity.Task) Configuration(com.alipay.sofa.jraft.conf.Configuration) Node(com.alipay.sofa.jraft.Node) ArrayList(java.util.ArrayList) Endpoint(com.alipay.sofa.jraft.util.Endpoint) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 37 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.

the class NodeTest method testRestoreSnasphot.

@Test
public void testRestoreSnasphot() throws Exception {
    final List<PeerId> peers = TestUtils.generatePeers(3);
    final TestCluster cluster = new TestCluster("unitest", this.dataPath, peers);
    for (final PeerId peer : peers) {
        assertTrue(cluster.start(peer.getEndpoint()));
    }
    cluster.waitLeader();
    // get leader
    final Node leader = cluster.getLeader();
    assertNotNull(leader);
    // apply tasks to leader
    this.sendTestTaskAndWait(leader);
    cluster.ensureSame();
    triggerLeaderSnapshot(cluster, leader);
    // stop leader
    final Endpoint leaderAddr = leader.getNodeId().getPeerId().getEndpoint().copy();
    assertTrue(cluster.stop(leaderAddr));
    Thread.sleep(2000);
    // restart leader
    cluster.waitLeader();
    assertEquals(0, cluster.getLeaderFsm().getLoadSnapshotTimes());
    assertTrue(cluster.start(leaderAddr));
    cluster.ensureSame();
    assertEquals(0, cluster.getLeaderFsm().getLoadSnapshotTimes());
    cluster.stopAll();
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) Node(com.alipay.sofa.jraft.Node) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 38 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.

the class ReadOnlyServiceTest method setup.

@Before
public void setup() {
    this.readOnlyServiceImpl = new ReadOnlyServiceImpl();
    final ReadOnlyServiceOptions opts = new ReadOnlyServiceOptions();
    opts.setFsmCaller(this.fsmCaller);
    opts.setNode(this.node);
    opts.setRaftOptions(new RaftOptions());
    Mockito.when(this.node.getNodeMetrics()).thenReturn(new NodeMetrics(false));
    Mockito.when(this.node.getOptions()).thenReturn(new NodeOptions());
    Mockito.when(this.node.getGroupId()).thenReturn("test");
    Mockito.when(this.node.getServerId()).thenReturn(new PeerId("localhost:8081", 0));
    assertTrue(this.readOnlyServiceImpl.init(opts));
}
Also used : RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) ReadOnlyServiceOptions(com.alipay.sofa.jraft.option.ReadOnlyServiceOptions) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) PeerId(com.alipay.sofa.jraft.entity.PeerId) Before(org.junit.Before)

Example 39 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.

the class ReplicatorGroupTest method setup.

@Before
public void setup() {
    this.timerManager = new TimerManager(5);
    this.replicatorGroup = new ReplicatorGroupImpl();
    final ReplicatorGroupOptions rgOpts = new ReplicatorGroupOptions();
    rgOpts.setHeartbeatTimeoutMs(heartbeatTimeout(this.options.getElectionTimeoutMs()));
    rgOpts.setElectionTimeoutMs(this.options.getElectionTimeoutMs());
    rgOpts.setLogManager(this.logManager);
    rgOpts.setBallotBox(this.ballotBox);
    rgOpts.setNode(this.node);
    rgOpts.setRaftRpcClientService(this.rpcService);
    rgOpts.setSnapshotStorage(this.snapshotStorage);
    rgOpts.setRaftOptions(this.raftOptions);
    rgOpts.setTimerManager(this.timerManager);
    Mockito.when(this.logManager.getLastLogIndex()).thenReturn(10L);
    Mockito.when(this.logManager.getTerm(10)).thenReturn(1L);
    Mockito.when(this.node.getNodeMetrics()).thenReturn(new NodeMetrics(false));
    Mockito.when(this.node.getNodeId()).thenReturn(new NodeId("test", new PeerId("localhost", 8081)));
    mockSendEmptyEntries();
    assertTrue(this.replicatorGroup.init(this.node.getNodeId(), rgOpts));
}
Also used : ReplicatorGroupOptions(com.alipay.sofa.jraft.option.ReplicatorGroupOptions) NodeId(com.alipay.sofa.jraft.entity.NodeId) PeerId(com.alipay.sofa.jraft.entity.PeerId) Before(org.junit.Before)

Example 40 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.

the class NodeTest method testNoLeaderWithZeroValPriorityElection.

@Test
public void testNoLeaderWithZeroValPriorityElection() throws Exception {
    List<Integer> priorities = new ArrayList<>();
    priorities.add(0);
    priorities.add(0);
    priorities.add(0);
    final List<PeerId> peers = TestUtils.generatePriorityPeers(3, priorities);
    final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers);
    for (final PeerId peer : peers) {
        assertTrue(cluster.start(peer.getEndpoint(), peer.getPriority()));
    }
    Thread.sleep(200);
    final List<Node> followers = cluster.getFollowers();
    assertEquals(3, followers.size());
    for (Node follower : followers) {
        assertEquals(0, follower.getNodeId().getPeerId().getPriority());
    }
    cluster.stopAll();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Node(com.alipay.sofa.jraft.Node) ArrayList(java.util.ArrayList) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Aggregations

PeerId (com.alipay.sofa.jraft.entity.PeerId)236 Test (org.junit.Test)107 Node (com.alipay.sofa.jraft.Node)70 Configuration (com.alipay.sofa.jraft.conf.Configuration)54 Status (com.alipay.sofa.jraft.Status)49 Endpoint (com.alipay.sofa.jraft.util.Endpoint)43 ArrayList (java.util.ArrayList)32 CountDownLatch (java.util.concurrent.CountDownLatch)28 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)24 LogId (com.alipay.sofa.jraft.entity.LogId)17 Message (com.google.protobuf.Message)15 ByteBuffer (java.nio.ByteBuffer)15 Task (com.alipay.sofa.jraft.entity.Task)13 File (java.io.File)12 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)11 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)11 JRaftException (com.alipay.sofa.jraft.error.JRaftException)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 RpcServer (com.alipay.sofa.jraft.rpc.RpcServer)9 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)8