Search in sources :

Example 51 with Endpoint

use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.

the class NodeTest method testRemoveLeader.

@Test
public void testRemoveLeader() throws Exception {
    List<PeerId> peers = TestUtils.generatePeers(3);
    final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers);
    for (final PeerId peer : peers) {
        assertTrue(cluster.start(peer.getEndpoint()));
    }
    // elect leader
    cluster.waitLeader();
    // get leader
    Node leader = cluster.getLeader();
    assertNotNull(leader);
    // apply tasks to leader
    this.sendTestTaskAndWait(leader);
    cluster.ensureSame();
    List<Node> followers = cluster.getFollowers();
    assertEquals(2, followers.size());
    final PeerId oldLeader = leader.getNodeId().getPeerId().copy();
    final Endpoint oldLeaderAddr = oldLeader.getEndpoint();
    // remove old leader
    LOG.info("Remove old leader {}", oldLeader);
    CountDownLatch latch = new CountDownLatch(1);
    leader.removePeer(oldLeader, new ExpectClosure(latch));
    waitLatch(latch);
    Thread.sleep(100);
    // elect new leader
    cluster.waitLeader();
    leader = cluster.getLeader();
    LOG.info("New leader is {}", leader);
    assertNotNull(leader);
    // apply tasks to new leader
    this.sendTestTaskAndWait(leader, 10, RaftError.SUCCESS);
    // stop and clean old leader
    LOG.info("Stop and clean old leader {}", oldLeader);
    assertTrue(cluster.stop(oldLeaderAddr));
    cluster.clean(oldLeaderAddr);
    // Add and start old leader
    LOG.info("Start and add old leader {}", oldLeader);
    assertTrue(cluster.start(oldLeaderAddr));
    peers = TestUtils.generatePeers(3);
    assertTrue(peers.remove(oldLeader));
    latch = new CountDownLatch(1);
    leader.addPeer(oldLeader, new ExpectClosure(latch));
    waitLatch(latch);
    followers = cluster.getFollowers();
    assertEquals(2, followers.size());
    cluster.ensureSame();
    assertEquals(3, cluster.getFsms().size());
    for (final MockStateMachine fsm : cluster.getFsms()) {
        assertEquals(20, fsm.getLogs().size());
    }
    cluster.stopAll();
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) Node(com.alipay.sofa.jraft.Node) CountDownLatch(java.util.concurrent.CountDownLatch) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 52 with Endpoint

use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.

the class NodeTest method testRemoveFollower.

@Test
public void testRemoveFollower() throws Exception {
    List<PeerId> peers = TestUtils.generatePeers(3);
    final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers);
    for (final PeerId peer : peers) {
        assertTrue(cluster.start(peer.getEndpoint()));
    }
    // elect leader
    cluster.waitLeader();
    // get leader
    final Node leader = cluster.getLeader();
    assertNotNull(leader);
    // apply tasks to leader
    this.sendTestTaskAndWait(leader);
    cluster.ensureSame();
    List<Node> followers = cluster.getFollowers();
    assertEquals(2, followers.size());
    final PeerId followerPeer = followers.get(0).getNodeId().getPeerId();
    final Endpoint followerAddr = followerPeer.getEndpoint();
    // stop and clean follower
    LOG.info("Stop and clean follower {}", followerPeer);
    assertTrue(cluster.stop(followerAddr));
    cluster.clean(followerAddr);
    // remove follower
    LOG.info("Remove follower {}", followerPeer);
    CountDownLatch latch = new CountDownLatch(1);
    leader.removePeer(followerPeer, new ExpectClosure(latch));
    waitLatch(latch);
    this.sendTestTaskAndWait(leader, 10, RaftError.SUCCESS);
    followers = cluster.getFollowers();
    assertEquals(1, followers.size());
    peers = TestUtils.generatePeers(3);
    assertTrue(peers.remove(followerPeer));
    // start follower
    LOG.info("Start and add follower {}", followerPeer);
    assertTrue(cluster.start(followerAddr));
    // re-add follower
    latch = new CountDownLatch(1);
    leader.addPeer(followerPeer, new ExpectClosure(latch));
    waitLatch(latch);
    followers = cluster.getFollowers();
    assertEquals(2, followers.size());
    cluster.ensureSame();
    assertEquals(3, cluster.getFsms().size());
    for (final MockStateMachine fsm : cluster.getFsms()) {
        assertEquals(20, fsm.getLogs().size());
    }
    cluster.stopAll();
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) Node(com.alipay.sofa.jraft.Node) CountDownLatch(java.util.concurrent.CountDownLatch) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 53 with Endpoint

use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.

the class PeerIdTest method testIsPriorityDisabled.

@Test
public void testIsPriorityDisabled() {
    final Endpoint endpoint1 = new Endpoint("192.168.1.1", 8081);
    final PeerId peer1 = new PeerId(endpoint1, 0);
    assertEquals("192.168.1.1:8081", peer1.toString());
    assertTrue(peer1.isPriorityDisabled());
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) Test(org.junit.Test)

Example 54 with Endpoint

use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.

the class PeerIdTest method testToStringParseWithIdxAndPriority.

@Test
public void testToStringParseWithIdxAndPriority() {
    // 1.String format is, ip:port::priority
    final Endpoint endpoint1 = new Endpoint("192.168.1.1", 8081);
    final PeerId peer1 = new PeerId(endpoint1, 0, 100);
    assertEquals("192.168.1.1:8081::100", peer1.toString());
    final PeerId p1 = new PeerId();
    final String str1 = "192.168.1.1:8081::100";
    assertTrue(p1.parse(str1));
    assertEquals(8081, p1.getPort());
    assertEquals("192.168.1.1", p1.getIp());
    assertEquals(0, p1.getIdx());
    assertEquals(100, p1.getPriority());
    assertEquals(p1, peer1);
    assertEquals(p1.hashCode(), peer1.hashCode());
    // 2.String format is, ip:port:idx:priority
    final Endpoint endpoint2 = new Endpoint("192.168.1.1", 8081);
    final PeerId peer2 = new PeerId(endpoint2, 100, 200);
    assertEquals("192.168.1.1:8081:100:200", peer2.toString());
    final PeerId p2 = new PeerId();
    final String str2 = "192.168.1.1:8081:100:200";
    assertTrue(p2.parse(str2));
    assertEquals(8081, p2.getPort());
    assertEquals("192.168.1.1", p2.getIp());
    assertEquals(100, p2.getIdx());
    assertEquals(200, p2.getPriority());
    assertEquals(p2, peer2);
    assertEquals(p2.hashCode(), peer2.hashCode());
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) Test(org.junit.Test)

Example 55 with Endpoint

use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.

the class LocalSnapshotCopierTest method testInterrupt.

@Test
public void testInterrupt() throws Exception {
    final FutureImpl<Message> future = new FutureImpl<>();
    final RpcRequests.GetFileRequest.Builder rb = RpcRequests.GetFileRequest.newBuilder().setReaderId(99).setFilename(Snapshot.JRAFT_SNAPSHOT_META_FILE).setCount(Integer.MAX_VALUE).setOffset(0).setReadPartly(true);
    // mock get metadata
    final ArgumentCaptor<RpcResponseClosure> argument = ArgumentCaptor.forClass(RpcResponseClosure.class);
    Mockito.when(this.raftClientService.getFile(eq(new Endpoint("localhost", 8081)), eq(rb.build()), eq(this.copyOpts.getTimeoutMs()), argument.capture())).thenReturn(future);
    this.copier.start();
    Thread.sleep(10);
    Utils.runInThread(new Runnable() {

        @Override
        public void run() {
            LocalSnapshotCopierTest.this.copier.cancel();
        }
    });
    this.copier.join();
    // start timer
    final SnapshotReader reader = this.copier.getReader();
    assertNull(reader);
    Assert.assertEquals(RaftError.ECANCELED.getNumber(), this.copier.getCode());
    Assert.assertEquals("Cancel the copier manually.", this.copier.getErrorMsg());
}
Also used : Message(com.google.protobuf.Message) FutureImpl(com.alipay.sofa.jraft.rpc.impl.FutureImpl) Endpoint(com.alipay.sofa.jraft.util.Endpoint) RpcResponseClosure(com.alipay.sofa.jraft.rpc.RpcResponseClosure) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) Test(org.junit.Test) BaseStorageTest(com.alipay.sofa.jraft.storage.BaseStorageTest)

Aggregations

Endpoint (com.alipay.sofa.jraft.util.Endpoint)80 Test (org.junit.Test)34 PeerId (com.alipay.sofa.jraft.entity.PeerId)28 Node (com.alipay.sofa.jraft.Node)23 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)18 Configuration (com.alipay.sofa.jraft.conf.Configuration)12 CountDownLatch (java.util.concurrent.CountDownLatch)11 Status (com.alipay.sofa.jraft.Status)8 File (java.io.File)7 ByteBuffer (java.nio.ByteBuffer)7 RaftOptions (com.alipay.sofa.jraft.option.RaftOptions)6 Region (com.alipay.sofa.jraft.rhea.metadata.Region)6 PlacementDriverClient (com.alipay.sofa.jraft.rhea.client.pd.PlacementDriverClient)5 RheaKVStoreOptions (com.alipay.sofa.jraft.rhea.options.RheaKVStoreOptions)5 RpcResponseClosure (com.alipay.sofa.jraft.rpc.RpcResponseClosure)5 RpcServer (com.alipay.sofa.jraft.rpc.RpcServer)5 FutureImpl (com.alipay.sofa.jraft.rpc.impl.FutureImpl)5 SnapshotReader (com.alipay.sofa.jraft.storage.snapshot.SnapshotReader)5 Message (com.google.protobuf.Message)5 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)4