use of com.alipay.sofa.jraft.entity.LogId in project sofa-jraft by sofastack.
the class ReplicatorTest method testContinueSendingEntries.
@Test
public void testContinueSendingEntries() throws Exception {
testOnRpcReturnedWaitMoreEntries();
final Replicator r = getReplicator();
this.id.unlock();
mockSendEmptyEntries();
final Future<Message> rpcInFly = r.getRpcInFly();
assertNotNull(rpcInFly);
final RpcRequests.AppendEntriesRequest.Builder rb = //
RpcRequests.AppendEntriesRequest.newBuilder().setGroupId(//
"test").setServerId(//
new PeerId("localhost", 8082).toString()).setPeerId(//
this.peerId.toString()).setTerm(//
1).setPrevLogIndex(//
10).setPrevLogTerm(//
1).setCommittedIndex(0);
int totalDataLen = 0;
for (int i = 0; i < 10; i++) {
totalDataLen += i;
final LogEntry value = new LogEntry();
value.setData(ByteBuffer.allocate(i));
value.setType(EnumOutter.EntryType.ENTRY_TYPE_DATA);
value.setId(new LogId(11 + i, 1));
Mockito.when(this.logManager.getEntry(11 + i)).thenReturn(value);
rb.addEntries(RaftOutter.EntryMeta.newBuilder().setTerm(1).setType(EnumOutter.EntryType.ENTRY_TYPE_DATA).setDataLen(i));
}
rb.setData(ByteString.copyFrom(new byte[totalDataLen]));
final RpcRequests.AppendEntriesRequest request = rb.build();
Mockito.when(this.rpcService.appendEntries(eq(this.peerId.getEndpoint()), eq(request), eq(-1), Mockito.any())).thenReturn(new FutureImpl<>());
assertEquals(11, r.statInfo.firstLogIndex);
assertEquals(10, r.statInfo.lastLogIndex);
Mockito.when(this.logManager.getTerm(20)).thenReturn(1L);
assertTrue(Replicator.continueSending(this.id, 0));
assertNotNull(r.getRpcInFly());
assertNotSame(rpcInFly, r.getRpcInFly());
assertEquals(11, r.statInfo.firstLogIndex);
assertEquals(20, r.statInfo.lastLogIndex);
assertEquals(0, r.getWaitId());
assertEquals(r.statInfo.runningState, Replicator.RunningState.IDLE);
}
use of com.alipay.sofa.jraft.entity.LogId in project sofa-jraft by sofastack.
the class ReplicatorTest method createEntriesRequest.
private RpcRequests.AppendEntriesRequest createEntriesRequest(final int n) {
final RpcRequests.AppendEntriesRequest.Builder rb = //
RpcRequests.AppendEntriesRequest.newBuilder().setGroupId(//
"test").setServerId(//
new PeerId("localhost", 8082).toString()).setPeerId(//
this.peerId.toString()).setTerm(//
1).setPrevLogIndex(//
10).setPrevLogTerm(//
1).setCommittedIndex(0);
for (int i = 0; i < n; i++) {
final LogEntry log = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_DATA);
log.setData(ByteBuffer.wrap(new byte[i]));
log.setId(new LogId(i + 11, 1));
Mockito.when(this.logManager.getEntry(i + 11)).thenReturn(log);
Mockito.when(this.logManager.getTerm(i + 11)).thenReturn(1L);
rb.addEntries(RaftOutter.EntryMeta.newBuilder().setDataLen(i).setTerm(1).setType(EnumOutter.EntryType.ENTRY_TYPE_DATA).build());
}
return rb.build();
}
use of com.alipay.sofa.jraft.entity.LogId in project sofa-jraft by sofastack.
the class LogEntryV2CodecFactoryTest method testDecodeV1LogEntry.
@Test
public void testDecodeV1LogEntry() {
ByteBuffer buf = ByteBuffer.wrap("hello".getBytes());
LogEntry entry = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_NO_OP);
entry.setId(new LogId(100, 3));
entry.setData(buf);
entry.setPeers(Arrays.asList(new PeerId("localhost", 99, 1), new PeerId("localhost", 100, 2)));
assertEquals(buf, entry.getData());
byte[] content = V1Encoder.INSTANCE.encode(entry);
assertNotNull(content);
assertTrue(content.length > 0);
// Decode by auto detect decoder
LogEntry nentry = this.decoder.decode(content);
assertNotNull(nentry);
assertEquals(100, nentry.getId().getIndex());
assertEquals(3, nentry.getId().getTerm());
assertEquals(2, nentry.getPeers().size());
assertEquals("localhost:99:1", nentry.getPeers().get(0).toString());
assertEquals("localhost:100:2", nentry.getPeers().get(1).toString());
assertEquals(buf, nentry.getData());
assertEquals(0, nentry.getData().position());
assertEquals(5, nentry.getData().remaining());
assertNull(nentry.getOldPeers());
}
use of com.alipay.sofa.jraft.entity.LogId in project sofa-jraft by sofastack.
the class FSMCallerTest method testOnCommittedError.
@Test
public void testOnCommittedError() throws Exception {
Mockito.when(this.logManager.getTerm(10)).thenReturn(1L);
Mockito.when(this.logManager.getEntry(11)).thenReturn(null);
assertTrue(this.fsmCaller.onCommitted(11));
this.fsmCaller.flush();
assertEquals(this.fsmCaller.getLastAppliedIndex(), 10);
Mockito.verify(this.logManager).setAppliedId(new LogId(10, 1));
assertFalse(this.fsmCaller.getError().getStatus().isOk());
assertEquals("Fail to get entry at index=11 while committed_index=11", this.fsmCaller.getError().getStatus().getErrorMsg());
}
use of com.alipay.sofa.jraft.entity.LogId in project sofa-jraft by sofastack.
the class BaseLogEntryCodecFactoryTest method testEncodeDecodeWithData.
@Test
public void testEncodeDecodeWithData() {
ByteBuffer buf = ByteBuffer.wrap("hello".getBytes());
LogEntry entry = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_NO_OP);
entry.setId(new LogId(100, 3));
entry.setData(buf);
entry.setPeers(Arrays.asList(new PeerId("localhost", 99, 1), new PeerId("localhost", 100, 2)));
assertEquals(buf, entry.getData());
byte[] content = this.encoder.encode(entry);
assertNotNull(content);
assertTrue(content.length > 0);
LogEntry nentry = this.decoder.decode(content);
assertNotNull(nentry);
assertEquals(100, nentry.getId().getIndex());
assertEquals(3, nentry.getId().getTerm());
assertEquals(2, nentry.getPeers().size());
assertEquals("localhost:99:1", nentry.getPeers().get(0).toString());
assertEquals("localhost:100:2", nentry.getPeers().get(1).toString());
assertEquals(buf, nentry.getData());
assertEquals(0, nentry.getData().position());
assertEquals(5, nentry.getData().remaining());
assertNull(nentry.getOldPeers());
}
Aggregations