Search in sources :

Example 1 with LogRecordTooLongException

use of org.apache.distributedlog.exceptions.LogRecordTooLongException in project bookkeeper by apache.

the class EnvelopedEntryWriter method writeRecord.

@Override
public synchronized void writeRecord(LogRecord record, CompletableFuture<DLSN> transmitPromise) throws LogRecordTooLongException, WriteException {
    int logRecordSize = record.getPersistentSize();
    if (logRecordSize > MAX_LOGRECORD_SIZE) {
        throw new LogRecordTooLongException("Log Record of size " + logRecordSize + " written when only " + MAX_LOGRECORD_SIZE + " is allowed");
    }
    try {
        this.writer.writeOp(record);
        int numRecords = 1;
        if (!record.isControl()) {
            hasUserData = true;
        }
        if (record.isRecordSet()) {
            numRecords = LogRecordSet.numRecords(record);
        }
        count += numRecords;
        writeRequests.add(new WriteRequest(numRecords, transmitPromise));
        maxTxId = Math.max(maxTxId, record.getTransactionId());
    } catch (IOException e) {
        logger.error("Failed to append record to record set of {} : ", logName, e);
        throw new WriteException(logName, "Failed to append record to record set of " + logName);
    }
}
Also used : WriteException(org.apache.distributedlog.exceptions.WriteException) IOException(java.io.IOException) LogRecordTooLongException(org.apache.distributedlog.exceptions.LogRecordTooLongException)

Example 2 with LogRecordTooLongException

use of org.apache.distributedlog.exceptions.LogRecordTooLongException in project bookkeeper by apache.

the class TestEntry method testWriteTooLongRecord.

@Test(timeout = 20000)
public void testWriteTooLongRecord() throws Exception {
    Writer writer = Entry.newEntry("test-write-too-long-record", 1024, true, CompressionCodec.Type.NONE);
    assertEquals("zero bytes", HEADER_LENGTH, writer.getNumBytes());
    assertEquals("zero records", 0, writer.getNumRecords());
    LogRecord largeRecord = new LogRecord(1L, new byte[MAX_LOGRECORD_SIZE + 1]);
    try {
        writer.writeRecord(largeRecord, new CompletableFuture<DLSN>());
        Assert.fail("Should fail on writing large record");
    } catch (LogRecordTooLongException lrtle) {
    // expected
    }
    assertEquals("zero bytes", HEADER_LENGTH, writer.getNumBytes());
    assertEquals("zero records", 0, writer.getNumRecords());
    ByteBuf buffer = writer.getBuffer();
    assertEquals("zero bytes", HEADER_LENGTH, buffer.readableBytes());
    buffer.release();
}
Also used : LogRecordTooLongException(org.apache.distributedlog.exceptions.LogRecordTooLongException) ByteBuf(io.netty.buffer.ByteBuf) Writer(org.apache.distributedlog.Entry.Writer) Test(org.junit.Test)

Example 3 with LogRecordTooLongException

use of org.apache.distributedlog.exceptions.LogRecordTooLongException in project bookkeeper by apache.

the class TestLogRecordSet method testWriteRecords.

void testWriteRecords(Type codec) throws Exception {
    Writer writer = LogRecordSet.newWriter(1024, codec);
    assertEquals("zero user bytes", HEADER_LEN, writer.getNumBytes());
    assertEquals("zero records", 0, writer.getNumRecords());
    List<CompletableFuture<DLSN>> writePromiseList = Lists.newArrayList();
    // / write first 5 records
    for (int i = 0; i < 5; i++) {
        ByteBuffer record = ByteBuffer.wrap(("record-" + i).getBytes(UTF_8));
        CompletableFuture<DLSN> writePromise = new CompletableFuture<>();
        writer.writeRecord(record, writePromise);
        writePromiseList.add(writePromise);
        assertEquals((i + 1) + " records", (i + 1), writer.getNumRecords());
    }
    ByteBuffer dataBuf = ByteBuffer.allocate(MAX_LOGRECORD_SIZE + 1);
    try {
        writer.writeRecord(dataBuf, new CompletableFuture<>());
        fail("Should fail on writing large record");
    } catch (LogRecordTooLongException lrtle) {
    // expected
    }
    assertEquals("5 records", 5, writer.getNumRecords());
    // / write another 5 records
    for (int i = 0; i < 5; i++) {
        ByteBuffer record = ByteBuffer.wrap(("record-" + (i + 5)).getBytes(UTF_8));
        CompletableFuture<DLSN> writePromise = new CompletableFuture<>();
        writer.writeRecord(record, writePromise);
        writePromiseList.add(writePromise);
        assertEquals((i + 6) + " records", (i + 6), writer.getNumRecords());
    }
    ByteBuf buffer = writer.getBuffer();
    assertEquals("10 records", 10, writer.getNumRecords());
    // Test transmit complete
    writer.completeTransmit(1L, 1L, 10L);
    List<DLSN> writeResults = FutureUtils.result(FutureUtils.collect(writePromiseList));
    for (int i = 0; i < 10; i++) {
        assertEquals(new DLSN(1L, 1L, 10L + i), writeResults.get(i));
    }
    LogRecordWithDLSN record = new LogRecordWithDLSN(new DLSN(1L, 1L, 10L), 99L, buffer, 999L);
    record.setPositionWithinLogSegment(888);
    record.setRecordSet();
    Reader reader = LogRecordSet.of(record);
    LogRecordWithDLSN readRecord = reader.nextRecord();
    int numReads = 0;
    while (null != readRecord) {
        assertEquals(new DLSN(1L, 1L, 10L + numReads), readRecord.getDlsn());
        assertEquals(99L, readRecord.getTransactionId());
        assertEquals(888 + numReads, readRecord.getPositionWithinLogSegment());
        assertEquals(999L, readRecord.getStartSequenceIdOfCurrentSegment());
        assertEquals(999L + 888 + numReads - 1, readRecord.getSequenceId());
        // read next
        ++numReads;
        readRecord = reader.nextRecord();
    }
    assertEquals(10, numReads);
    reader.release();
}
Also used : Reader(org.apache.distributedlog.LogRecordSet.Reader) LogRecordTooLongException(org.apache.distributedlog.exceptions.LogRecordTooLongException) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer) CompletableFuture(java.util.concurrent.CompletableFuture) Writer(org.apache.distributedlog.LogRecordSet.Writer)

Example 4 with LogRecordTooLongException

use of org.apache.distributedlog.exceptions.LogRecordTooLongException in project bookkeeper by apache.

the class TestLogRecordSet method testWriteTooLongRecord.

@Test(timeout = 60000)
public void testWriteTooLongRecord() throws Exception {
    Writer writer = LogRecordSet.newWriter(1024, Type.NONE);
    assertEquals("zero user bytes", HEADER_LEN, writer.getNumBytes());
    assertEquals("zero records", 0, writer.getNumRecords());
    ByteBuffer dataBuf = ByteBuffer.allocate(MAX_LOGRECORD_SIZE + 1);
    try {
        writer.writeRecord(dataBuf, new CompletableFuture<DLSN>());
        fail("Should fail on writing large record");
    } catch (LogRecordTooLongException lrtle) {
    // expected
    }
    assertEquals("zero user bytes", HEADER_LEN, writer.getNumBytes());
    assertEquals("zero records", 0, writer.getNumRecords());
    ByteBuf buffer = writer.getBuffer();
    assertEquals("zero user bytes", HEADER_LEN, buffer.readableBytes());
    LogRecordWithDLSN record = new LogRecordWithDLSN(new DLSN(1L, 0L, 0L), 1L, buffer, 1L);
    record.setRecordSet();
    Reader reader = LogRecordSet.of(record);
    assertNull("Empty record set should return null", reader.nextRecord());
    reader.release();
}
Also used : Reader(org.apache.distributedlog.LogRecordSet.Reader) LogRecordTooLongException(org.apache.distributedlog.exceptions.LogRecordTooLongException) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer) Writer(org.apache.distributedlog.LogRecordSet.Writer) Test(org.junit.Test)

Example 5 with LogRecordTooLongException

use of org.apache.distributedlog.exceptions.LogRecordTooLongException in project bookkeeper by apache.

the class TestEntry method testWriteRecords.

@Test(timeout = 20000)
public void testWriteRecords() throws Exception {
    Writer writer = Entry.newEntry("test-write-records", 1024, true, CompressionCodec.Type.NONE);
    assertEquals("zero bytes", HEADER_LENGTH, writer.getNumBytes());
    assertEquals("zero records", 0, writer.getNumRecords());
    List<CompletableFuture<DLSN>> writePromiseList = Lists.newArrayList();
    // write first 5 records
    for (int i = 0; i < 5; i++) {
        LogRecord record = new LogRecord(i, ("record-" + i).getBytes(UTF_8));
        record.setPositionWithinLogSegment(i);
        CompletableFuture<DLSN> writePromise = new CompletableFuture<DLSN>();
        writer.writeRecord(record, writePromise);
        writePromiseList.add(writePromise);
        assertEquals((i + 1) + " records", (i + 1), writer.getNumRecords());
    }
    // write large record
    LogRecord largeRecord = new LogRecord(1L, new byte[MAX_LOGRECORD_SIZE + 1]);
    try {
        writer.writeRecord(largeRecord, new CompletableFuture<DLSN>());
        Assert.fail("Should fail on writing large record");
    } catch (LogRecordTooLongException lrtle) {
    // expected
    }
    assertEquals("5 records", 5, writer.getNumRecords());
    // write another 5 records
    for (int i = 0; i < 5; i++) {
        LogRecord record = new LogRecord(i + 5, ("record-" + (i + 5)).getBytes(UTF_8));
        record.setPositionWithinLogSegment(i + 5);
        CompletableFuture<DLSN> writePromise = new CompletableFuture<DLSN>();
        writer.writeRecord(record, writePromise);
        writePromiseList.add(writePromise);
        assertEquals((i + 6) + " records", (i + 6), writer.getNumRecords());
    }
    ByteBuf buffer = writer.getBuffer();
    buffer.retain();
    // Test transmit complete
    writer.completeTransmit(1L, 1L);
    List<DLSN> writeResults = Utils.ioResult(FutureUtils.collect(writePromiseList));
    for (int i = 0; i < 10; i++) {
        assertEquals(new DLSN(1L, 1L, i), writeResults.get(i));
    }
    // Test reading from buffer
    Reader reader = Entry.newBuilder().setEntry(buffer).setLogSegmentInfo(1L, 1L).setEntryId(0L).setEnvelopeEntry(true).buildReader();
    buffer.release();
    LogRecordWithDLSN record = reader.nextRecord();
    int numReads = 0;
    long expectedTxid = 0L;
    while (null != record) {
        assertEquals(expectedTxid, record.getTransactionId());
        assertEquals(expectedTxid, record.getSequenceId());
        assertEquals(new DLSN(1L, 0L, expectedTxid), record.getDlsn());
        ++numReads;
        ++expectedTxid;
        record = reader.nextRecord();
    }
    assertEquals(10, numReads);
    reader.release();
}
Also used : Reader(org.apache.distributedlog.Entry.Reader) LogRecordTooLongException(org.apache.distributedlog.exceptions.LogRecordTooLongException) ByteBuf(io.netty.buffer.ByteBuf) CompletableFuture(java.util.concurrent.CompletableFuture) Writer(org.apache.distributedlog.Entry.Writer) Test(org.junit.Test)

Aggregations

LogRecordTooLongException (org.apache.distributedlog.exceptions.LogRecordTooLongException)6 ByteBuf (io.netty.buffer.ByteBuf)4 Test (org.junit.Test)4 ByteBuffer (java.nio.ByteBuffer)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Writer (org.apache.distributedlog.Entry.Writer)2 Reader (org.apache.distributedlog.LogRecordSet.Reader)2 Writer (org.apache.distributedlog.LogRecordSet.Writer)2 IOException (java.io.IOException)1 Reader (org.apache.distributedlog.Entry.Reader)1 AsyncLogWriter (org.apache.distributedlog.api.AsyncLogWriter)1 WriteException (org.apache.distributedlog.exceptions.WriteException)1