Search in sources :

Example 61 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.

the class NodeTest method testChangePeersAddMultiNodes.

@Test
public void testChangePeersAddMultiNodes() throws Exception {
    final PeerId peer0 = new PeerId(TestUtils.getMyIp(), TestUtils.INIT_PORT);
    final TestCluster cluster = new TestCluster("testChangePeers", this.dataPath, Collections.singletonList(peer0));
    assertTrue(cluster.start(peer0.getEndpoint()));
    cluster.waitLeader();
    final Node leader = cluster.getLeader();
    this.sendTestTaskAndWait(leader);
    final Configuration conf = new Configuration();
    for (int i = 0; i < 3; i++) {
        final PeerId peer = new PeerId(TestUtils.getMyIp(), TestUtils.INIT_PORT + i);
        conf.addPeer(peer);
    }
    PeerId peer = new PeerId(TestUtils.getMyIp(), peer0.getEndpoint().getPort() + 1);
    // fail, because the peers are not started.
    final SynchronizedClosure done = new SynchronizedClosure();
    leader.changePeers(new Configuration(Collections.singletonList(peer)), done);
    Assert.assertEquals(RaftError.ECATCHUP, done.await().getRaftError());
    // start peer1
    assertTrue(cluster.start(peer.getEndpoint()));
    // still fail, because peer2 is not started
    done.reset();
    leader.changePeers(conf, done);
    Assert.assertEquals(RaftError.ECATCHUP, done.await().getRaftError());
    // start peer2
    peer = new PeerId(TestUtils.getMyIp(), peer0.getEndpoint().getPort() + 2);
    assertTrue(cluster.start(peer.getEndpoint()));
    done.reset();
    // works
    leader.changePeers(conf, done);
    assertTrue(done.await().isOk());
    assertTrue(cluster.ensureSame());
    assertEquals(3, cluster.getFsms().size());
    for (final MockStateMachine fsm : cluster.getFsms()) {
        assertEquals(10, fsm.getLogs().size());
    }
    cluster.stopAll();
}
Also used : SynchronizedClosure(com.alipay.sofa.jraft.closure.SynchronizedClosure) Configuration(com.alipay.sofa.jraft.conf.Configuration) Node(com.alipay.sofa.jraft.Node) Endpoint(com.alipay.sofa.jraft.util.Endpoint) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 62 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.

the class ReplicatorGroupTest method testFindTheNextCandidateWithPriority2.

@Test
public void testFindTheNextCandidateWithPriority2() {
    final PeerId p1 = new PeerId("localhost", 18881, 0, 0);
    final PeerId p2 = new PeerId("localhost", 18882, 0, 0);
    final PeerId p3 = new PeerId("localhost", 18883, 0, -1);
    Mockito.when(this.rpcService.connect(p1.getEndpoint())).thenReturn(true);
    Mockito.when(this.rpcService.connect(p2.getEndpoint())).thenReturn(true);
    Mockito.when(this.rpcService.connect(p3.getEndpoint())).thenReturn(true);
    this.replicatorGroup.resetTerm(1);
    this.replicatorGroup.addReplicator(p1);
    this.replicatorGroup.addReplicator(p2);
    this.replicatorGroup.addReplicator(p3);
    final ConfigurationEntry conf = new ConfigurationEntry();
    conf.setConf(new Configuration(Arrays.asList(p1, p2, p3)));
    final PeerId p = this.replicatorGroup.findTheNextCandidate(conf);
    assertEquals(p3, p);
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) ConfigurationEntry(com.alipay.sofa.jraft.conf.ConfigurationEntry) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 63 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.

the class ReplicatorGroupTest method testFindTheNextCandidateWithPriority1.

@Test
public void testFindTheNextCandidateWithPriority1() {
    final PeerId p1 = new PeerId("localhost", 18881, 0, 60);
    final PeerId p2 = new PeerId("localhost", 18882, 0, 80);
    final PeerId p3 = new PeerId("localhost", 18883, 0, 100);
    Mockito.when(this.rpcService.connect(p1.getEndpoint())).thenReturn(true);
    Mockito.when(this.rpcService.connect(p2.getEndpoint())).thenReturn(true);
    Mockito.when(this.rpcService.connect(p3.getEndpoint())).thenReturn(true);
    this.replicatorGroup.resetTerm(1);
    this.replicatorGroup.addReplicator(p1);
    this.replicatorGroup.addReplicator(p2);
    this.replicatorGroup.addReplicator(p3);
    final ConfigurationEntry conf = new ConfigurationEntry();
    conf.setConf(new Configuration(Arrays.asList(p1, p2, p3)));
    final PeerId p = this.replicatorGroup.findTheNextCandidate(conf);
    assertEquals(p3, p);
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) ConfigurationEntry(com.alipay.sofa.jraft.conf.ConfigurationEntry) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 64 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.

the class RocksDBLogStorage method load.

private void load(final ConfigurationManager confManager) {
    checkState();
    try (final RocksIterator it = this.db.newIterator(this.confHandle, this.totalOrderReadOptions)) {
        it.seekToFirst();
        while (it.isValid()) {
            final byte[] ks = it.key();
            final byte[] bs = it.value();
            // LogEntry index
            if (ks.length == 8) {
                final LogEntry entry = this.logEntryDecoder.decode(bs);
                if (entry != null) {
                    if (entry.getType() == EntryType.ENTRY_TYPE_CONFIGURATION) {
                        final ConfigurationEntry confEntry = new ConfigurationEntry();
                        confEntry.setId(new LogId(entry.getId().getIndex(), entry.getId().getTerm()));
                        confEntry.setConf(new Configuration(entry.getPeers(), entry.getLearners()));
                        if (entry.getOldPeers() != null) {
                            confEntry.setOldConf(new Configuration(entry.getOldPeers(), entry.getOldLearners()));
                        }
                        if (confManager != null) {
                            confManager.add(confEntry);
                        }
                    }
                } else {
                    LOG.warn("Fail to decode conf entry at index {}, the log data is: {}.", Bits.getLong(ks, 0), BytesUtil.toHex(bs));
                }
            } else {
                if (Arrays.equals(FIRST_LOG_IDX_KEY, ks)) {
                    setFirstLogIndex(Bits.getLong(bs, 0));
                    truncatePrefixInBackground(0L, this.firstLogIndex);
                } else {
                    LOG.warn("Unknown entry in configuration storage key={}, value={}.", BytesUtil.toHex(ks), BytesUtil.toHex(bs));
                }
            }
            it.next();
        }
    }
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) RocksIterator(org.rocksdb.RocksIterator) ConfigurationEntry(com.alipay.sofa.jraft.conf.ConfigurationEntry) LogId(com.alipay.sofa.jraft.entity.LogId) LogEntry(com.alipay.sofa.jraft.entity.LogEntry)

Example 65 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.

the class ResetPeerRequestProcessor method processRequest0.

@Override
protected Message processRequest0(final CliRequestContext ctx, final ResetPeerRequest request, final RpcRequestClosure done) {
    final Configuration newConf = new Configuration();
    for (final String peerIdStr : request.getNewPeersList()) {
        final PeerId peer = new PeerId();
        if (peer.parse(peerIdStr)) {
            newConf.addPeer(peer);
        } else {
            return // 
            RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peer id %s", peerIdStr);
        }
    }
    LOG.info("Receive ResetPeerRequest to {} from {}, new conf is {}", ctx.node.getNodeId(), done.getRpcCtx().getRemoteAddress(), newConf);
    final Status st = ctx.node.resetPeers(newConf);
    return // 
    RpcFactoryHelper.responseFactory().newResponse(defaultResp(), st);
}
Also used : Status(com.alipay.sofa.jraft.Status) Configuration(com.alipay.sofa.jraft.conf.Configuration) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Aggregations

Configuration (com.alipay.sofa.jraft.conf.Configuration)81 PeerId (com.alipay.sofa.jraft.entity.PeerId)54 Test (org.junit.Test)28 Node (com.alipay.sofa.jraft.Node)20 Endpoint (com.alipay.sofa.jraft.util.Endpoint)20 Status (com.alipay.sofa.jraft.Status)18 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)18 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)9 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)8 File (java.io.File)8 ArrayList (java.util.ArrayList)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 RpcServer (com.alipay.sofa.jraft.rpc.RpcServer)7 ConfigurationEntry (com.alipay.sofa.jraft.conf.ConfigurationEntry)5 Task (com.alipay.sofa.jraft.entity.Task)5 CliOptions (com.alipay.sofa.jraft.option.CliOptions)5 LogId (com.alipay.sofa.jraft.entity.LogId)4 RheaKVStore (com.alipay.sofa.jraft.rhea.client.RheaKVStore)4 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)3 RaftException (com.alipay.sofa.jraft.error.RaftException)3