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();
}
}
}
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());
}
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);
}
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());
}
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());
}
Aggregations