Search in sources :

Example 26 with Endpoint

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

the class NodeTest method testRemoveLeaderWithPriority.

@Test
public void testRemoveLeaderWithPriority() throws Exception {
    final List<Integer> priorities = new ArrayList<Integer>();
    priorities.add(100);
    priorities.add(60);
    priorities.add(10);
    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()));
    }
    // elect leader
    cluster.waitLeader();
    // get leader
    Node leader = cluster.getLeader();
    assertNotNull(leader);
    assertEquals(100, leader.getNodeTargetPriority());
    assertEquals(100, leader.getNodeId().getPeerId().getPriority());
    final 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);
    assertEquals(60, leader.getNodeTargetPriority());
    // stop and clean old leader
    LOG.info("Stop and clean old leader {}", oldLeader);
    assertTrue(cluster.stop(oldLeaderAddr));
    cluster.clean(oldLeaderAddr);
    // elect new leader
    cluster.waitLeader();
    leader = cluster.getLeader();
    LOG.info("New leader is {}", leader);
    assertNotNull(leader);
    assertNotSame(leader, oldLeader);
    cluster.stopAll();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Endpoint(com.alipay.sofa.jraft.util.Endpoint) Node(com.alipay.sofa.jraft.Node) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 27 with Endpoint

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

the class NodeTest method testSingleNodeWithLearner.

@Test
public void testSingleNodeWithLearner() throws Exception {
    final Endpoint addr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT);
    final PeerId peer = new PeerId(addr, 0);
    final Endpoint learnerAddr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT + 1);
    final PeerId learnerPeer = new PeerId(learnerAddr, 0);
    NodeManager.getInstance().addAddress(addr);
    NodeManager.getInstance().addAddress(learnerAddr);
    MockStateMachine learnerFsm = null;
    Node learner = null;
    RaftGroupService learnerServer = null;
    {
        // Start learner
        final NodeOptions nodeOptions = createNodeOptionsWithSharedTimer();
        learnerFsm = new MockStateMachine(learnerAddr);
        nodeOptions.setFsm(learnerFsm);
        nodeOptions.setLogUri(this.dataPath + File.separator + "log1");
        nodeOptions.setRaftMetaUri(this.dataPath + File.separator + "meta1");
        nodeOptions.setSnapshotUri(this.dataPath + File.separator + "snapshot1");
        nodeOptions.setInitialConf(new Configuration(Collections.singletonList(peer), Collections.singletonList(learnerPeer)));
        final RpcServer rpcServer = RaftRpcServerFactory.createRaftRpcServer(learnerAddr);
        learnerServer = new RaftGroupService("unittest", new PeerId(learnerAddr, 0), nodeOptions, rpcServer);
        learner = learnerServer.start();
    }
    {
        // Start leader
        final NodeOptions nodeOptions = createNodeOptionsWithSharedTimer();
        final MockStateMachine fsm = new MockStateMachine(addr);
        nodeOptions.setFsm(fsm);
        nodeOptions.setLogUri(this.dataPath + File.separator + "log");
        nodeOptions.setRaftMetaUri(this.dataPath + File.separator + "meta");
        nodeOptions.setSnapshotUri(this.dataPath + File.separator + "snapshot");
        nodeOptions.setInitialConf(new Configuration(Collections.singletonList(peer), Collections.singletonList(learnerPeer)));
        final Node node = new NodeImpl("unittest", peer);
        assertTrue(node.init(nodeOptions));
        assertEquals(1, node.listPeers().size());
        assertTrue(node.listPeers().contains(peer));
        while (!node.isLeader()) {
            ;
        }
        sendTestTaskAndWait(node);
        assertEquals(10, fsm.getLogs().size());
        int i = 0;
        for (final ByteBuffer data : fsm.getLogs()) {
            assertEquals("hello" + i++, new String(data.array()));
        }
        // wait for entries to be replicated to learner.
        Thread.sleep(1000);
        node.shutdown();
        node.join();
    }
    {
        // assert learner fsm
        assertEquals(10, learnerFsm.getLogs().size());
        int i = 0;
        for (final ByteBuffer data : learnerFsm.getLogs()) {
            assertEquals("hello" + i++, new String(data.array()));
        }
        learnerServer.shutdown();
        learnerServer.join();
    }
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) Configuration(com.alipay.sofa.jraft.conf.Configuration) Node(com.alipay.sofa.jraft.Node) RaftGroupService(com.alipay.sofa.jraft.RaftGroupService) RpcServer(com.alipay.sofa.jraft.rpc.RpcServer) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) ByteBuffer(java.nio.ByteBuffer) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 28 with Endpoint

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

the class PeerIdTest method testIsPriorityNotElected.

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

Example 29 with Endpoint

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

the class RheaBenchmarkCluster method start.

protected void start() throws IOException, InterruptedException {
    SystemPropertyUtil.setProperty(Configs.NETTY_BUFFER_LOW_WATERMARK, Integer.toString(256 * 1024));
    SystemPropertyUtil.setProperty(Configs.NETTY_BUFFER_HIGH_WATERMARK, Integer.toString(512 * 1024));
    File file = new File("benchmark_rhea_db");
    if (file.exists()) {
        FileUtils.forceDelete(file);
    }
    file = new File("benchmark_rhea_db");
    if (file.mkdir()) {
        this.tempDbPath = file.getAbsolutePath();
        System.out.println("make dir: " + this.tempDbPath);
    }
    file = new File("benchmark_rhea_raft");
    if (file.exists()) {
        FileUtils.forceDelete(file);
    }
    file = new File("benchmark_rhea_raft");
    if (file.mkdir()) {
        this.tempRaftPath = file.getAbsolutePath();
        System.out.println("make dir: " + this.tempRaftPath);
    }
    for (String c : CONF) {
        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        final RheaKVStoreOptions opts = mapper.readValue(new File(c), RheaKVStoreOptions.class);
        RheaKVStore rheaKVStore = new DefaultRheaKVStore();
        if (rheaKVStore.init(opts)) {
            stores.add(rheaKVStore);
        } else {
            throw new RuntimeException("Fail to init rhea kv store witch conf: " + c);
        }
    }
    PlacementDriverClient pdClient = stores.get(0).getPlacementDriverClient();
    Endpoint leader1 = pdClient.getLeader(1, true, 10000);
    System.out.println("The region 1 leader is: " + leader1);
// Endpoint leader2 = pdClient.getLeader(2, true, 10000);
// System.out.println("The region 2 leader is: " + leader2);
// Endpoint leader3 = pdClient.getLeader(3, true, 10000);
// System.out.println("The region 3 leader is: " + leader3);
}
Also used : RheaKVStoreOptions(com.alipay.sofa.jraft.rhea.options.RheaKVStoreOptions) DefaultRheaKVStore(com.alipay.sofa.jraft.rhea.client.DefaultRheaKVStore) RheaKVStore(com.alipay.sofa.jraft.rhea.client.RheaKVStore) Endpoint(com.alipay.sofa.jraft.util.Endpoint) PlacementDriverClient(com.alipay.sofa.jraft.rhea.client.pd.PlacementDriverClient) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) File(java.io.File) DefaultRheaKVStore(com.alipay.sofa.jraft.rhea.client.DefaultRheaKVStore) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 30 with Endpoint

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

the class RemoteFileCopier method init.

public boolean init(String uri, final SnapshotThrottle snapshotThrottle, final SnapshotCopierOptions opts) {
    this.rpcService = opts.getRaftClientService();
    this.timerManager = opts.getTimerManager();
    this.raftOptions = opts.getRaftOptions();
    this.snapshotThrottle = snapshotThrottle;
    final int prefixSize = Snapshot.REMOTE_SNAPSHOT_URI_SCHEME.length();
    if (uri == null || !uri.startsWith(Snapshot.REMOTE_SNAPSHOT_URI_SCHEME)) {
        LOG.error("Invalid uri {}.", uri);
        return false;
    }
    uri = uri.substring(prefixSize);
    final int slasPos = uri.indexOf('/');
    final String ipAndPort = uri.substring(0, slasPos);
    uri = uri.substring(slasPos + 1);
    try {
        this.readId = Long.parseLong(uri);
        final String[] ipAndPortStrs = ipAndPort.split(":");
        this.endpoint = new Endpoint(ipAndPortStrs[0], Integer.parseInt(ipAndPortStrs[1]));
    } catch (final Exception e) {
        LOG.error("Fail to parse readerId or endpoint.", e);
        return false;
    }
    if (!this.rpcService.connect(this.endpoint)) {
        LOG.error("Fail to init channel to {}.", this.endpoint);
        return false;
    }
    return true;
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) Endpoint(com.alipay.sofa.jraft.util.Endpoint) IOException(java.io.IOException)

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