Search in sources :

Example 6 with LogId

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);
}
Also used : Message(com.google.protobuf.Message) RpcRequests(com.alipay.sofa.jraft.rpc.RpcRequests) LogId(com.alipay.sofa.jraft.entity.LogId) LogEntry(com.alipay.sofa.jraft.entity.LogEntry) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 7 with LogId

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();
}
Also used : LogId(com.alipay.sofa.jraft.entity.LogId) LogEntry(com.alipay.sofa.jraft.entity.LogEntry) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 8 with LogId

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());
}
Also used : ByteBuffer(java.nio.ByteBuffer) LogId(com.alipay.sofa.jraft.entity.LogId) LogEntry(com.alipay.sofa.jraft.entity.LogEntry) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test) BaseLogEntryCodecFactoryTest(com.alipay.sofa.jraft.entity.codec.BaseLogEntryCodecFactoryTest)

Example 9 with LogId

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());
}
Also used : LogId(com.alipay.sofa.jraft.entity.LogId) Test(org.junit.Test)

Example 10 with LogId

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());
}
Also used : ByteBuffer(java.nio.ByteBuffer) LogId(com.alipay.sofa.jraft.entity.LogId) LogEntry(com.alipay.sofa.jraft.entity.LogEntry) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Aggregations

LogId (com.alipay.sofa.jraft.entity.LogId)44 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)26 PeerId (com.alipay.sofa.jraft.entity.PeerId)18 Test (org.junit.Test)18 Status (com.alipay.sofa.jraft.Status)10 BaseStorageTest (com.alipay.sofa.jraft.storage.BaseStorageTest)9 ArrayList (java.util.ArrayList)8 ConfigurationEntry (com.alipay.sofa.jraft.conf.ConfigurationEntry)7 Configuration (com.alipay.sofa.jraft.conf.Configuration)5 ByteBuffer (java.nio.ByteBuffer)5 LogManager (com.alipay.sofa.jraft.storage.LogManager)4 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 ConfigurationManager (com.alipay.sofa.jraft.conf.ConfigurationManager)3 EntryType (com.alipay.sofa.jraft.entity.EnumOutter.EntryType)2 BaseLogEntryCodecFactoryTest (com.alipay.sofa.jraft.entity.codec.BaseLogEntryCodecFactoryTest)2 RaftException (com.alipay.sofa.jraft.error.RaftException)2 LogStorageOptions (com.alipay.sofa.jraft.option.LogStorageOptions)2 RaftOptions (com.alipay.sofa.jraft.option.RaftOptions)2 DisruptorMetricSet (com.alipay.sofa.jraft.util.DisruptorMetricSet)2