Search in sources :

Example 21 with LogId

use of com.alipay.sofa.jraft.entity.LogId in project sofa-jraft by sofastack.

the class LogManagerImpl method appendToStorage.

private LogId appendToStorage(final List<LogEntry> toAppend) {
    LogId lastId = null;
    if (!this.hasError) {
        final long startMs = Utils.monotonicMs();
        final int entriesCount = toAppend.size();
        this.nodeMetrics.recordSize("append-logs-count", entriesCount);
        try {
            int writtenSize = 0;
            for (int i = 0; i < entriesCount; i++) {
                final LogEntry entry = toAppend.get(i);
                writtenSize += entry.getData() != null ? entry.getData().remaining() : 0;
            }
            this.nodeMetrics.recordSize("append-logs-bytes", writtenSize);
            final int nAppent = this.logStorage.appendEntries(toAppend);
            if (nAppent != entriesCount) {
                LOG.error("**Critical error**, fail to appendEntries, nAppent={}, toAppend={}", nAppent, toAppend.size());
                reportError(RaftError.EIO.getNumber(), "Fail to append log entries");
            }
            if (nAppent > 0) {
                lastId = toAppend.get(nAppent - 1).getId();
            }
            toAppend.clear();
        } finally {
            this.nodeMetrics.recordLatency("append-logs", Utils.monotonicMs() - startMs);
        }
    }
    return lastId;
}
Also used : LogId(com.alipay.sofa.jraft.entity.LogId) LogEntry(com.alipay.sofa.jraft.entity.LogEntry)

Example 22 with LogId

use of com.alipay.sofa.jraft.entity.LogId in project sofa-jraft by sofastack.

the class LogManagerImpl method unsafeGetTerm.

private long unsafeGetTerm(final long index) {
    if (index == 0) {
        return 0;
    }
    final LogId lss = this.lastSnapshotId;
    if (index == lss.getIndex()) {
        return lss.getTerm();
    }
    if (index > this.lastLogIndex || index < this.firstLogIndex) {
        return 0;
    }
    final LogEntry entry = getEntryFromMemory(index);
    if (entry != null) {
        return entry.getId().getTerm();
    }
    return getTermFromLogStorage(index);
}
Also used : LogId(com.alipay.sofa.jraft.entity.LogId) LogEntry(com.alipay.sofa.jraft.entity.LogEntry)

Example 23 with LogId

use of com.alipay.sofa.jraft.entity.LogId in project sofa-jraft by sofastack.

the class FSMCallerTest method setup.

@Before
public void setup() {
    this.fsmCaller = new FSMCallerImpl();
    this.closureQueue = new ClosureQueueImpl();
    final FSMCallerOptions opts = new FSMCallerOptions();
    Mockito.when(this.node.getNodeMetrics()).thenReturn(new NodeMetrics(false));
    opts.setNode(this.node);
    opts.setFsm(this.fsm);
    opts.setLogManager(this.logManager);
    opts.setBootstrapId(new LogId(10, 1));
    opts.setClosureQueue(this.closureQueue);
    assertTrue(this.fsmCaller.init(opts));
}
Also used : FSMCallerOptions(com.alipay.sofa.jraft.option.FSMCallerOptions) ClosureQueueImpl(com.alipay.sofa.jraft.closure.ClosureQueueImpl) LogId(com.alipay.sofa.jraft.entity.LogId) Before(org.junit.Before)

Example 24 with LogId

use of com.alipay.sofa.jraft.entity.LogId in project sofa-jraft by sofastack.

the class FSMCallerTest method testOnCommitted.

@Test
public void testOnCommitted() throws Exception {
    final LogEntry log = new LogEntry(EntryType.ENTRY_TYPE_DATA);
    log.getId().setIndex(11);
    log.getId().setTerm(1);
    Mockito.when(this.logManager.getTerm(11)).thenReturn(1L);
    Mockito.when(this.logManager.getEntry(11)).thenReturn(log);
    final ArgumentCaptor<Iterator> itArg = ArgumentCaptor.forClass(Iterator.class);
    assertTrue(this.fsmCaller.onCommitted(11));
    this.fsmCaller.flush();
    assertEquals(this.fsmCaller.getLastAppliedIndex(), 11);
    Mockito.verify(this.fsm).onApply(itArg.capture());
    final Iterator it = itArg.getValue();
    assertFalse(it.hasNext());
    assertEquals(it.getIndex(), 12);
    Mockito.verify(this.logManager).setAppliedId(new LogId(11, 1));
    assertTrue(this.fsmCaller.getError().getStatus().isOk());
}
Also used : Iterator(com.alipay.sofa.jraft.Iterator) LogId(com.alipay.sofa.jraft.entity.LogId) LogEntry(com.alipay.sofa.jraft.entity.LogEntry) Test(org.junit.Test)

Example 25 with LogId

use of com.alipay.sofa.jraft.entity.LogId in project sofa-jraft by sofastack.

the class ConfigurationManagerTest method testGetStuff.

@Test
public void testGetStuff() {
    ConfigurationEntry lastConf = this.confManager.getLastConfiguration();
    ConfigurationEntry snapshot = this.confManager.getSnapshot();
    assertSame(snapshot, lastConf);
    assertSame(snapshot, this.confManager.get(0));
    ConfigurationEntry confEntry1 = TestUtils.getConfEntry("localhost:8080", null);
    confEntry1.setId(new LogId(0, 0));
    assertTrue(this.confManager.add(confEntry1));
    lastConf = this.confManager.getLastConfiguration();
    assertNotSame(snapshot, lastConf);
    assertSame(confEntry1, lastConf);
    assertSame(confEntry1, this.confManager.get(0));
    assertSame(confEntry1, this.confManager.get(1));
    assertSame(confEntry1, this.confManager.get(2));
    ConfigurationEntry confEntry2 = TestUtils.getConfEntry("localhost:8080,localhost:8081", "localhost:8080");
    confEntry2.setId(new LogId(1, 1));
    assertTrue(this.confManager.add(confEntry2));
    lastConf = this.confManager.getLastConfiguration();
    assertNotSame(snapshot, lastConf);
    assertSame(confEntry2, lastConf);
    assertSame(confEntry1, this.confManager.get(0));
    assertSame(confEntry2, this.confManager.get(1));
    assertSame(confEntry2, this.confManager.get(2));
    ConfigurationEntry confEntry3 = TestUtils.getConfEntry("localhost:8080,localhost:8081,localhost:8082", "localhost:8080,localhost:8081");
    confEntry3.setId(new LogId(2, 1));
    assertTrue(this.confManager.add(confEntry3));
    lastConf = this.confManager.getLastConfiguration();
    assertNotSame(snapshot, lastConf);
    assertSame(confEntry3, lastConf);
    assertSame(confEntry1, this.confManager.get(0));
    assertSame(confEntry2, this.confManager.get(1));
    assertSame(confEntry3, this.confManager.get(2));
}
Also used : LogId(com.alipay.sofa.jraft.entity.LogId) 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