use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.
the class ReadOnlyServiceTest method testAddRequestOnResponsePending.
@Test
public void testAddRequestOnResponsePending() throws Exception {
final byte[] requestContext = TestUtils.getRandomBytes();
final CountDownLatch latch = new CountDownLatch(1);
this.readOnlyServiceImpl.addRequest(requestContext, new ReadIndexClosure() {
@Override
public void run(final Status status, final long index, final byte[] reqCtx) {
assertTrue(status.isOk());
assertEquals(index, 1);
assertArrayEquals(reqCtx, requestContext);
latch.countDown();
}
});
this.readOnlyServiceImpl.flush();
final ArgumentCaptor<RpcResponseClosure> closureCaptor = ArgumentCaptor.forClass(RpcResponseClosure.class);
Mockito.verify(this.node).handleReadIndexRequest(Mockito.argThat(new ArgumentMatcher<ReadIndexRequest>() {
@Override
public boolean matches(final Object argument) {
if (argument instanceof ReadIndexRequest) {
final ReadIndexRequest req = (ReadIndexRequest) argument;
return req.getGroupId().equals("test") && req.getServerId().equals("localhost:8081:0") && req.getEntriesCount() == 1 && Arrays.equals(requestContext, req.getEntries(0).toByteArray());
}
return false;
}
}), closureCaptor.capture());
final RpcResponseClosure closure = closureCaptor.getValue();
assertNotNull(closure);
closure.setResponse(ReadIndexResponse.newBuilder().setIndex(1).setSuccess(true).build());
assertTrue(this.readOnlyServiceImpl.getPendingNotifyStatus().isEmpty());
closure.run(Status.OK());
assertEquals(this.readOnlyServiceImpl.getPendingNotifyStatus().size(), 1);
this.readOnlyServiceImpl.onApplied(2);
latch.await();
}
use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.
the class ReadOnlyServiceTest method testOverMaxReadIndexLag.
@Test
public void testOverMaxReadIndexLag() throws Exception {
Mockito.when(this.fsmCaller.getLastAppliedIndex()).thenReturn(1L);
this.readOnlyServiceImpl.getRaftOptions().setMaxReadIndexLag(50);
final byte[] requestContext = TestUtils.getRandomBytes();
final CountDownLatch latch = new CountDownLatch(1);
final String errMsg = "Fail to run ReadIndex task, the gap of current node's apply index between leader's commit index over maxReadIndexLag";
this.readOnlyServiceImpl.addRequest(requestContext, new ReadIndexClosure() {
@Override
public void run(final Status status, final long index, final byte[] reqCtx) {
assertFalse(status.isOk());
assertEquals(status.getErrorMsg(), errMsg);
assertEquals(index, -1);
assertArrayEquals(reqCtx, requestContext);
latch.countDown();
}
});
this.readOnlyServiceImpl.flush();
final ArgumentCaptor<RpcResponseClosure> closureCaptor = ArgumentCaptor.forClass(RpcResponseClosure.class);
Mockito.verify(this.node).handleReadIndexRequest(Mockito.argThat(new ArgumentMatcher<ReadIndexRequest>() {
@Override
public boolean matches(final Object argument) {
if (argument instanceof ReadIndexRequest) {
final ReadIndexRequest req = (ReadIndexRequest) argument;
return req.getGroupId().equals("test") && req.getServerId().equals("localhost:8081:0") && req.getEntriesCount() == 1 && Arrays.equals(requestContext, req.getEntries(0).toByteArray());
}
return false;
}
}), closureCaptor.capture());
final RpcResponseClosure closure = closureCaptor.getValue();
assertNotNull(closure);
closure.setResponse(ReadIndexResponse.newBuilder().setIndex(52).setSuccess(true).build());
closure.run(Status.OK());
latch.await();
}
use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.
the class NodeTest method testTripleNodesV1V2Codec.
@Test
public void testTripleNodesV1V2Codec() throws Exception {
final List<PeerId> peers = TestUtils.generatePeers(3);
final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers);
for (int i = 0; i < peers.size(); i++) {
// Peer3 use codec v1
if (i == 2) {
cluster.setRaftServiceFactory(new V1JRaftServiceFactory());
}
assertTrue(cluster.start(peers.get(i).getEndpoint()));
}
// elect leader
cluster.waitLeader();
// get leader
Node leader = cluster.getLeader();
assertNotNull(leader);
assertEquals(3, leader.listPeers().size());
// apply tasks to leader
this.sendTestTaskAndWait(leader);
{
final ByteBuffer data = ByteBuffer.wrap("no closure".getBytes());
final Task task = new Task(data, null);
leader.apply(task);
}
{
// task with TaskClosure
final ByteBuffer data = ByteBuffer.wrap("task closure".getBytes());
final Vector<String> cbs = new Vector<>();
final CountDownLatch latch = new CountDownLatch(1);
final Task task = new Task(data, new TaskClosure() {
@Override
public void run(final Status status) {
cbs.add("apply");
latch.countDown();
}
@Override
public void onCommitted() {
cbs.add("commit");
}
});
leader.apply(task);
latch.await();
assertEquals(2, cbs.size());
assertEquals("commit", cbs.get(0));
assertEquals("apply", cbs.get(1));
}
cluster.ensureSame(-1);
assertEquals(2, cluster.getFollowers().size());
// transfer the leader to v1 codec peer
assertTrue(leader.transferLeadershipTo(peers.get(2)).isOk());
cluster.waitLeader();
leader = cluster.getLeader();
assertNotNull(leader);
assertEquals(leader.getLeaderId(), peers.get(2));
// apply tasks to leader
this.sendTestTaskAndWait(leader);
cluster.ensureSame();
cluster.stopAll();
// start the cluster with v2 codec, should work
final TestCluster newCluster = new TestCluster("unittest", this.dataPath, peers);
for (int i = 0; i < peers.size(); i++) {
assertTrue(newCluster.start(peers.get(i).getEndpoint()));
}
// elect leader
newCluster.waitLeader();
newCluster.ensureSame();
leader = newCluster.getLeader();
assertNotNull(leader);
// apply new tasks
this.sendTestTaskAndWait(leader);
newCluster.ensureSame();
newCluster.stopAll();
}
use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.
the class ReplicatorTest method testOnHeartbeatReturnedRpcError.
@Test
public void testOnHeartbeatReturnedRpcError() {
final Replicator r = getReplicator();
this.id.unlock();
final ScheduledFuture<?> timer = r.getHeartbeatTimer();
assertNotNull(timer);
Replicator.onHeartbeatReturned(this.id, new Status(-1, "test"), createEmptyEntriesRequest(), null, Utils.monotonicMs());
assertNotNull(r.getHeartbeatTimer());
assertNotSame(timer, r.getHeartbeatTimer());
}
use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.
the class ReplicatorTest method testOnInstallSnapshotReturnedRpcError.
@Test
public void testOnInstallSnapshotReturnedRpcError() {
final Replicator r = getReplicator();
this.id.unlock();
assertNull(r.getBlockTimer());
final RpcRequests.InstallSnapshotRequest request = createInstallSnapshotRequest();
final RpcRequests.InstallSnapshotResponse response = RpcRequests.InstallSnapshotResponse.newBuilder().setSuccess(true).setTerm(1).build();
assertEquals(-1, r.getWaitId());
Mockito.when(this.logManager.getTerm(11)).thenReturn(1L);
Replicator.onRpcReturned(this.id, Replicator.RequestType.Snapshot, new Status(-1, "test"), request, response, 0, 0, -1);
assertNotNull(r.getBlockTimer());
assertEquals(-1, r.getWaitId());
}
Aggregations