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