use of com.alipay.sofa.jraft.closure.ReadIndexClosure in project sofa-jraft by sofastack.
the class NodeTest method testReadIndex.
@Test
public void testReadIndex() throws Exception {
final 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(), false, 300, true));
}
// elect leader
cluster.waitLeader();
// get leader
final Node leader = cluster.getLeader();
assertNotNull(leader);
assertEquals(3, leader.listPeers().size());
// apply tasks to leader
this.sendTestTaskAndWait(leader);
// first call will fail-fast when no connection
if (!assertReadIndex(leader, 11)) {
assertTrue(assertReadIndex(leader, 11));
}
// read from follower
for (final Node follower : cluster.getFollowers()) {
assertNotNull(follower);
assertReadIndex(follower, 11);
}
// read with null request context
final CountDownLatch latch = new CountDownLatch(1);
leader.readIndex(null, new ReadIndexClosure() {
@Override
public void run(final Status status, final long index, final byte[] reqCtx) {
assertNull(reqCtx);
assertTrue(status.isOk());
latch.countDown();
}
});
latch.await();
cluster.stopAll();
}
use of com.alipay.sofa.jraft.closure.ReadIndexClosure in project sofa-jraft by sofastack.
the class NodeTest method testReadIndexChaos.
@Test
public void testReadIndexChaos() throws Exception {
final 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(), false, 300, true));
}
// elect leader
cluster.waitLeader();
// get leader
final Node leader = cluster.getLeader();
assertNotNull(leader);
assertEquals(3, leader.listPeers().size());
final CountDownLatch latch = new CountDownLatch(10);
for (int i = 0; i < 10; i++) {
new Thread() {
@Override
public void run() {
try {
for (int i = 0; i < 100; i++) {
try {
sendTestTaskAndWait(leader);
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
}
readIndexRandom(cluster);
}
} finally {
latch.countDown();
}
}
private void readIndexRandom(final TestCluster cluster) {
final CountDownLatch readLatch = new CountDownLatch(1);
final byte[] requestContext = TestUtils.getRandomBytes();
cluster.getNodes().get(ThreadLocalRandom.current().nextInt(3)).readIndex(requestContext, new ReadIndexClosure() {
@Override
public void run(final Status status, final long index, final byte[] reqCtx) {
if (status.isOk()) {
assertTrue(status.toString(), status.isOk());
assertTrue(index > 0);
assertArrayEquals(requestContext, reqCtx);
}
readLatch.countDown();
}
});
try {
readLatch.await();
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}.start();
}
latch.await();
cluster.ensureSame();
for (final MockStateMachine fsm : cluster.getFsms()) {
assertEquals(10000, fsm.getLogs().size());
}
cluster.stopAll();
}
use of com.alipay.sofa.jraft.closure.ReadIndexClosure in project sofa-jraft by sofastack.
the class ReadOnlyServiceTest method testAddRequestOnResponseSuccess.
@Test
public void testAddRequestOnResponseSuccess() throws Exception {
Mockito.when(this.fsmCaller.getLastAppliedIndex()).thenReturn(2L);
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());
closure.run(Status.OK());
latch.await();
}
use of com.alipay.sofa.jraft.closure.ReadIndexClosure in project sofa-jraft by sofastack.
the class ReadOnlyServiceTest method testOnApplied.
@Test
public void testOnApplied() throws Exception {
final ArrayList<ReadIndexState> states = new ArrayList<>();
final byte[] reqContext = TestUtils.getRandomBytes();
final CountDownLatch latch = new CountDownLatch(1);
final ReadIndexState state = new ReadIndexState(new Bytes(reqContext), new ReadIndexClosure() {
@Override
public void run(final Status status, final long index, final byte[] reqCtx) {
assertTrue(status.isOk());
assertEquals(index, 1);
assertArrayEquals(reqCtx, reqContext);
latch.countDown();
}
}, Utils.monotonicMs());
state.setIndex(1);
states.add(state);
final ReadIndexStatus readIndexStatus = new ReadIndexStatus(states, null, 1);
this.readOnlyServiceImpl.getPendingNotifyStatus().put(1L, Arrays.asList(readIndexStatus));
this.readOnlyServiceImpl.onApplied(2);
latch.await();
assertTrue(this.readOnlyServiceImpl.getPendingNotifyStatus().isEmpty());
}
use of com.alipay.sofa.jraft.closure.ReadIndexClosure 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();
}
Aggregations