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();
}
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);
}
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);
}
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();
}
}
}
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);
}
Aggregations