use of com.alipay.sofa.jraft.util.ByteBufferCollector in project sofa-jraft by sofastack.
the class LocalFileReaderTest method testReadSmallInitBuffer.
@Test
public void testReadSmallInitBuffer() throws Exception {
final ByteBufferCollector bufRef = ByteBufferCollector.allocate(2);
final String data = writeData();
assertReadResult(bufRef, data);
}
use of com.alipay.sofa.jraft.util.ByteBufferCollector in project sofa-jraft by sofastack.
the class LocalFileReaderTest method testReadBigFile.
@Test
public void testReadBigFile() throws Exception {
final ByteBufferCollector bufRef = ByteBufferCollector.allocate(2);
final File file = new File(this.path + File.separator + "data");
String data = "";
for (int i = 0; i < 4096; i++) {
data += i % 10;
}
FileUtils.writeStringToFile(file, data);
int read = this.fileReader.readFile(bufRef, "data", 0, 1024);
assertEquals(1024, read);
read = this.fileReader.readFile(bufRef, "data", 1024, 1024);
assertEquals(1024, read);
read = this.fileReader.readFile(bufRef, "data", 1024 + 1024, 1024);
assertEquals(1024, read);
read = this.fileReader.readFile(bufRef, "data", 1024 + 1024 + 1024, 1024);
assertEquals(-1, read);
final ByteBuffer buf = bufRef.getBuffer();
buf.flip();
assertEquals(data.length(), buf.remaining());
final byte[] bs = new byte[data.length()];
buf.get(bs);
assertEquals(data, new String(bs));
}
use of com.alipay.sofa.jraft.util.ByteBufferCollector in project sofa-jraft by sofastack.
the class AppendEntriesBenchmark method sendEntries3.
private static byte[] sendEntries3(final int entryCount, final int sizeOfEntry, AdaptiveBufAllocator.Handle allocator) {
final AppendEntriesRequest.Builder rb = AppendEntriesRequest.newBuilder();
fillCommonFields(rb);
final ByteBufferCollector dataBuffer = allocator.allocateByRecyclers();
try {
for (int i = 0; i < entryCount; i++) {
final byte[] bytes = new byte[sizeOfEntry];
ThreadLocalRandom.current().nextBytes(bytes);
final ByteBuffer buf = ByteBuffer.wrap(bytes);
dataBuffer.put(buf.slice());
}
final ByteBuffer buf = dataBuffer.getBuffer();
buf.flip();
final int remaining = buf.remaining();
allocator.record(remaining);
rb.setData(ZeroByteStringHelper.wrap(buf));
return rb.build().toByteArray();
} finally {
RecycleUtil.recycle(dataBuffer);
}
}
use of com.alipay.sofa.jraft.util.ByteBufferCollector in project sofa-jraft by sofastack.
the class AppendEntriesBenchmark method sendEntries1.
private static byte[] sendEntries1(final int entryCount, final int sizeOfEntry) {
final AppendEntriesRequest.Builder rb = AppendEntriesRequest.newBuilder();
fillCommonFields(rb);
final ByteBufferCollector dataBuffer = ByteBufferCollector.allocate();
for (int i = 0; i < entryCount; i++) {
final byte[] bytes = new byte[sizeOfEntry];
ThreadLocalRandom.current().nextBytes(bytes);
final ByteBuffer buf = ByteBuffer.wrap(bytes);
dataBuffer.put(buf.slice());
}
final ByteBuffer buf = dataBuffer.getBuffer();
buf.flip();
rb.setData(ZeroByteStringHelper.wrap(buf));
return rb.build().toByteArray();
}
use of com.alipay.sofa.jraft.util.ByteBufferCollector in project sofa-jraft by sofastack.
the class LocalSnapshotCopier method loadMetaTable.
private void loadMetaTable() throws InterruptedException {
final ByteBufferCollector metaBuf = ByteBufferCollector.allocate(0);
Session session = null;
try {
this.lock.lock();
try {
if (this.cancelled) {
if (isOk()) {
setError(RaftError.ECANCELED, "ECANCELED");
}
return;
}
session = this.copier.startCopy2IoBuffer(Snapshot.JRAFT_SNAPSHOT_META_FILE, metaBuf, null);
this.curSession = session;
} finally {
this.lock.unlock();
}
// join out of lock.
session.join();
this.lock.lock();
try {
this.curSession = null;
} finally {
this.lock.unlock();
}
if (!session.status().isOk() && isOk()) {
LOG.warn("Fail to copy meta file: {}", session.status());
setError(session.status().getCode(), session.status().getErrorMsg());
return;
}
if (!this.remoteSnapshot.getMetaTable().loadFromIoBufferAsRemote(metaBuf.getBuffer())) {
LOG.warn("Bad meta_table format");
setError(-1, "Bad meta_table format from remote");
return;
}
Requires.requireTrue(this.remoteSnapshot.getMetaTable().hasMeta(), "Invalid remote snapshot meta:%s", this.remoteSnapshot.getMetaTable().getMeta());
} finally {
if (session != null) {
Utils.closeQuietly(session);
}
}
}
Aggregations