Search in sources :

Example 21 with LogEntry

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

the class LogManagerImpl method appendEntries.

@Override
public void appendEntries(final List<LogEntry> entries, final StableClosure done) {
    assert (done != null);
    Requires.requireNonNull(done, "done");
    if (this.hasError) {
        entries.clear();
        Utils.runClosureInThread(done, new Status(RaftError.EIO, "Corrupted LogStorage"));
        return;
    }
    boolean doUnlock = true;
    this.writeLock.lock();
    try {
        if (!entries.isEmpty() && !checkAndResolveConflict(entries, done, this.writeLock)) {
            // If checkAndResolveConflict returns false, the done will be called in it.
            entries.clear();
            return;
        }
        for (int i = 0; i < entries.size(); i++) {
            final LogEntry entry = entries.get(i);
            // Set checksum after checkAndResolveConflict
            if (this.raftOptions.isEnableLogEntryChecksum()) {
                entry.setChecksum(entry.checksum());
            }
            if (entry.getType() == EntryType.ENTRY_TYPE_CONFIGURATION) {
                Configuration oldConf = new Configuration();
                if (entry.getOldPeers() != null) {
                    oldConf = new Configuration(entry.getOldPeers(), entry.getOldLearners());
                }
                final ConfigurationEntry conf = new ConfigurationEntry(entry.getId(), new Configuration(entry.getPeers(), entry.getLearners()), oldConf);
                this.configManager.add(conf);
            }
        }
        if (!entries.isEmpty()) {
            done.setFirstLogIndex(entries.get(0).getId().getIndex());
            this.logsInMemory.addAll(entries);
        }
        done.setEntries(entries);
        doUnlock = false;
        if (!wakeupAllWaiter(this.writeLock)) {
            notifyLastLogIndexListeners();
        }
        // publish event out of lock
        this.diskQueue.publishEvent((event, sequence) -> {
            event.reset();
            event.type = EventType.OTHER;
            event.done = done;
        });
    } finally {
        if (doUnlock) {
            this.writeLock.unlock();
        }
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) Configuration(com.alipay.sofa.jraft.conf.Configuration) ConfigurationEntry(com.alipay.sofa.jraft.conf.ConfigurationEntry) LogEntry(com.alipay.sofa.jraft.entity.LogEntry)

Example 22 with LogEntry

use of com.alipay.sofa.jraft.entity.LogEntry 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 23 with LogEntry

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

the class IteratorImplTest method setup.

@Before
public void setup() {
    this.applyingIndex = new AtomicLong(0);
    this.closures = new ArrayList<>();
    for (int i = 0; i < 11; i++) {
        this.closures.add(new MockClosure());
        final LogEntry log = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_NO_OP);
        log.getId().setIndex(i);
        log.getId().setTerm(1);
        Mockito.when(this.logManager.getEntry(i)).thenReturn(log);
    }
    this.iter = new IteratorImpl(fsm, logManager, closures, 0L, 0L, 10L, applyingIndex);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) LogEntry(com.alipay.sofa.jraft.entity.LogEntry) Before(org.junit.Before)

Example 24 with LogEntry

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

the class BaseLogEntryCodecFactoryTest method testEncodeDecodeWithoutData.

@Test
public void testEncodeDecodeWithoutData() {
    LogEntry entry = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_NO_OP);
    entry.setId(new LogId(100, 3));
    entry.setPeers(Arrays.asList(new PeerId("localhost", 99, 1), new PeerId("localhost", 100, 2)));
    assertSame(LogEntry.EMPTY_DATA, entry.getData());
    assertNull(entry.getOldPeers());
    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());
    Assert.assertEquals(EnumOutter.EntryType.ENTRY_TYPE_NO_OP, nentry.getType());
    assertEquals(2, nentry.getPeers().size());
    assertEquals("localhost:99:1", nentry.getPeers().get(0).toString());
    assertEquals("localhost:100:2", nentry.getPeers().get(1).toString());
    assertSame(LogEntry.EMPTY_DATA, nentry.getData());
    assertNull(nentry.getOldPeers());
}
Also used : 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 25 with LogEntry

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

the class BaseLogEntryCodecFactoryTest method testEmptyData.

@Test
public void testEmptyData() {
    LogEntry entry = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_NO_OP);
    entry.setId(new LogId(100, 3));
    entry.setPeers(Arrays.asList(new PeerId("localhost", 99, 1), new PeerId("localhost", 100, 2)));
    entry.setData(ByteBuffer.allocate(0));
    byte[] content = this.encoder.encode(entry);
    assertNotNull(content);
    assertTrue(content.length > 0);
    LogEntry nentry = this.decoder.decode(content);
    assertNotNull(nentry);
    assertNotNull(nentry.getData());
    assertEquals(0, nentry.getData().remaining());
}
Also used : 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

LogEntry (com.alipay.sofa.jraft.entity.LogEntry)50 LogId (com.alipay.sofa.jraft.entity.LogId)27 Test (org.junit.Test)19 PeerId (com.alipay.sofa.jraft.entity.PeerId)13 Status (com.alipay.sofa.jraft.Status)11 BaseStorageTest (com.alipay.sofa.jraft.storage.BaseStorageTest)11 ArrayList (java.util.ArrayList)10 LogManager (com.alipay.sofa.jraft.storage.LogManager)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 ConfigurationEntry (com.alipay.sofa.jraft.conf.ConfigurationEntry)6 ByteBuffer (java.nio.ByteBuffer)6 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)6 Configuration (com.alipay.sofa.jraft.conf.Configuration)5 Closure (com.alipay.sofa.jraft.Closure)3 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)3 ConfigurationManager (com.alipay.sofa.jraft.conf.ConfigurationManager)3 RaftOutter (com.alipay.sofa.jraft.entity.RaftOutter)3 UserLog (com.alipay.sofa.jraft.entity.UserLog)3 FSMCaller (com.alipay.sofa.jraft.FSMCaller)2 JRaftServiceFactory (com.alipay.sofa.jraft.JRaftServiceFactory)2