use of com.alipay.sofa.jraft.entity.LogEntry in project sofa-jraft by sofastack.
the class LogEntryCodecPerfTest method testEncodeDecode.
private void testEncodeDecode(final LogEntryEncoder encoder, final LogEntryDecoder decoder, final CyclicBarrier barrier) throws Exception {
ByteBuffer buf = ByteBuffer.wrap(DATA);
LogEntry entry = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_NO_OP);
entry.setData(buf);
entry.setPeers(Arrays.asList(new PeerId("localhost", 99, 1), new PeerId("localhost", 100, 2)));
if (barrier != null) {
barrier.await();
}
for (int i = 0; i < TIMES; i++) {
entry.setId(new LogId(i, i));
byte[] content = encoder.encode(entry);
assert (content.length > 0);
this.logSize.addAndGet(content.length);
LogEntry nLog = decoder.decode(content);
assertEquals(2, nLog.getPeers().size());
assertArrayEquals(DATA, nLog.getData().array());
assertEquals(i, nLog.getId().getIndex());
assertEquals(i, nLog.getId().getTerm());
}
if (barrier != null) {
barrier.await();
}
}
use of com.alipay.sofa.jraft.entity.LogEntry in project sofa-jraft by sofastack.
the class LogEntryV2CodecFactoryTest method testEncodeDecodeWithLearners.
@Test
public void testEncodeDecodeWithLearners() {
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)));
List<PeerId> theLearners = createLearners("192.168.1.1:8081", "192.168.1.2:8081");
entry.setLearners(theLearners);
assertSame(entry.getData(), LogEntry.EMPTY_DATA);
assertNull(entry.getOldPeers());
byte[] content = this.encoder.encode(entry);
assertNotNull(content);
assertTrue(content.length > 0);
LogEntry nentry = this.decoder.decode(content);
assertNotNull(nentry);
assertNull(nentry.getOldLearners());
assertPeersAndLearners(theLearners, nentry);
// test old learners
List<PeerId> theOldLearners = createLearners("192.168.1.1:8081");
entry.setOldLearners(theOldLearners);
content = this.encoder.encode(entry);
assertNotNull(content);
assertTrue(content.length > 0);
nentry = this.decoder.decode(content);
assertNotNull(nentry);
assertPeersAndLearners(theLearners, nentry);
List<PeerId> oldLearners = nentry.getOldLearners();
assertNotNull(oldLearners);
assertEquals(1, oldLearners.size());
assertEquals(oldLearners, theOldLearners);
}
use of com.alipay.sofa.jraft.entity.LogEntry in project sofa-jraft by sofastack.
the class BaseLogStorageTest method testAppendMantyLargeEntries.
@Test
public void testAppendMantyLargeEntries() {
final long start = Utils.monotonicMs();
final int totalLogs = 100000;
final int logSize = 16 * 1024;
final int batch = 100;
appendLargeEntries(totalLogs, logSize, batch);
System.out.println("Inserted " + totalLogs + " large logs, cost " + (Utils.monotonicMs() - start) + " ms.");
for (int i = 0; i < totalLogs; i++) {
final LogEntry log = this.logStorage.getEntry(i);
assertNotNull(log);
assertEquals(i, log.getId().getIndex());
assertEquals(i, log.getId().getTerm());
assertEquals(logSize, log.getData().remaining());
}
this.logStorage.shutdown();
this.logStorage.init(newLogStorageOptions());
for (int i = 0; i < totalLogs; i++) {
final LogEntry log = this.logStorage.getEntry(i);
assertNotNull(log);
assertEquals(i, log.getId().getIndex());
assertEquals(i, log.getId().getTerm());
assertEquals(logSize, log.getData().remaining());
}
}
use of com.alipay.sofa.jraft.entity.LogEntry in project sofa-jraft by sofastack.
the class BaseLogStorageTest method testAddManyEntries.
@Test
public void testAddManyEntries() {
final List<LogEntry> entries = TestUtils.mockEntries();
assertEquals(10, this.logStorage.appendEntries(entries));
assertEquals(0, this.logStorage.getFirstLogIndex());
assertEquals(9, this.logStorage.getLastLogIndex());
for (int i = 0; i < 10; i++) {
assertEquals(i, this.logStorage.getTerm(i));
final LogEntry entry = this.logStorage.getEntry(i);
assertNotNull(entry);
assertEquals(entries.get(i), entry);
}
}
use of com.alipay.sofa.jraft.entity.LogEntry in project sofa-jraft by sofastack.
the class LogManagerTest method testSetAppliedId.
@Test
public void testSetAppliedId() throws Exception {
final List<LogEntry> mockEntries = mockAddEntries();
for (int i = 0; i < 10; i++) {
// it's in memory
Assert.assertEquals(mockEntries.get(i), this.logManager.getEntryFromMemory(i + 1));
}
// waiting for setDiskId()
Thread.sleep(200);
this.logManager.setAppliedId(new LogId(10, 10));
for (int i = 0; i < 10; i++) {
assertNull(this.logManager.getEntryFromMemory(i + 1));
Assert.assertEquals(mockEntries.get(i), this.logManager.getEntry(i + 1));
}
}
Aggregations