Search in sources :

Example 1 with LogRecordTooLongException

use of com.twitter.distributedlog.exceptions.LogRecordTooLongException in project distributedlog by twitter.

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, NullStatsLogger.INSTANCE);
    assertEquals("zero bytes", 0, writer.getNumBytes());
    assertEquals("zero records", 0, writer.getNumRecords());
    List<Future<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);
        Promise<DLSN> writePromise = new Promise<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 Promise<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);
        Promise<DLSN> writePromise = new Promise<DLSN>();
        writer.writeRecord(record, writePromise);
        writePromiseList.add(writePromise);
        assertEquals((i + 6) + " records", (i + 6), writer.getNumRecords());
    }
    Buffer buffer = writer.getBuffer();
    // Test transmit complete
    writer.completeTransmit(1L, 1L);
    List<DLSN> writeResults = Await.result(Future.collect(writePromiseList));
    for (int i = 0; i < 10; i++) {
        Assert.assertEquals(new DLSN(1L, 1L, i), writeResults.get(i));
    }
    // Test reading from buffer
    Entry recordSet = Entry.newBuilder().setData(buffer.getData(), 0, buffer.size()).setLogSegmentInfo(1L, 1L).setEntryId(0L).build();
    Reader reader = recordSet.reader();
    LogRecordWithDLSN record = reader.nextRecord();
    int numReads = 0;
    long expectedTxid = 0L;
    while (null != record) {
        Assert.assertEquals(expectedTxid, record.getTransactionId());
        Assert.assertEquals(expectedTxid, record.getSequenceId());
        Assert.assertEquals(new DLSN(1L, 0L, expectedTxid), record.getDlsn());
        ++numReads;
        ++expectedTxid;
        record = reader.nextRecord();
    }
    Assert.assertEquals(10, numReads);
}
Also used : ByteBuffer(java.nio.ByteBuffer) Buffer(com.twitter.distributedlog.io.Buffer) Reader(com.twitter.distributedlog.Entry.Reader) LogRecordTooLongException(com.twitter.distributedlog.exceptions.LogRecordTooLongException) Promise(com.twitter.util.Promise) Future(com.twitter.util.Future) Writer(com.twitter.distributedlog.Entry.Writer) Test(org.junit.Test)

Example 2 with LogRecordTooLongException

use of com.twitter.distributedlog.exceptions.LogRecordTooLongException in project distributedlog by twitter.

the class TestEntry method testWriteTooLongRecord.

@Test(timeout = 20000)
public void testWriteTooLongRecord() throws Exception {
    Writer writer = Entry.newEntry("test-write-too-long-record", 1024, false, CompressionCodec.Type.NONE, NullStatsLogger.INSTANCE);
    assertEquals("zero bytes", 0, writer.getNumBytes());
    assertEquals("zero records", 0, writer.getNumRecords());
    LogRecord largeRecord = new LogRecord(1L, new byte[MAX_LOGRECORD_SIZE + 1]);
    try {
        writer.writeRecord(largeRecord, new Promise<DLSN>());
        Assert.fail("Should fail on writing large record");
    } catch (LogRecordTooLongException lrtle) {
    // expected
    }
    assertEquals("zero bytes", 0, writer.getNumBytes());
    assertEquals("zero records", 0, writer.getNumRecords());
    Buffer buffer = writer.getBuffer();
    Assert.assertEquals("zero bytes", 0, buffer.size());
}
Also used : ByteBuffer(java.nio.ByteBuffer) Buffer(com.twitter.distributedlog.io.Buffer) LogRecordTooLongException(com.twitter.distributedlog.exceptions.LogRecordTooLongException) Writer(com.twitter.distributedlog.Entry.Writer) Test(org.junit.Test)

Example 3 with LogRecordTooLongException

use of com.twitter.distributedlog.exceptions.LogRecordTooLongException in project distributedlog by twitter.

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 Promise<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());
    ByteBuffer buffer = writer.getBuffer();
    assertEquals("zero user bytes", HEADER_LEN, buffer.remaining());
    byte[] data = new byte[buffer.remaining()];
    buffer.get(data);
    LogRecordWithDLSN record = new LogRecordWithDLSN(new DLSN(1L, 0L, 0L), 1L, data, 1L);
    record.setRecordSet();
    Reader reader = LogRecordSet.of(record);
    assertNull("Empty record set should return null", reader.nextRecord());
}
Also used : Reader(com.twitter.distributedlog.LogRecordSet.Reader) LogRecordTooLongException(com.twitter.distributedlog.exceptions.LogRecordTooLongException) ByteBuffer(java.nio.ByteBuffer) Writer(com.twitter.distributedlog.LogRecordSet.Writer) Test(org.junit.Test)

Example 4 with LogRecordTooLongException

use of com.twitter.distributedlog.exceptions.LogRecordTooLongException in project distributedlog by twitter.

the class EnvelopedEntryWriter method writeRecord.

@Override
public synchronized void writeRecord(LogRecord record, Promise<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(com.twitter.distributedlog.exceptions.WriteException) IOException(java.io.IOException) LogRecordTooLongException(com.twitter.distributedlog.exceptions.LogRecordTooLongException)

Example 5 with LogRecordTooLongException

use of com.twitter.distributedlog.exceptions.LogRecordTooLongException in project distributedlog by twitter.

the class DistributedLogMultiStreamWriter method write.

public synchronized Future<DLSN> write(ByteBuffer buffer) {
    int logRecordSize = buffer.remaining();
    if (logRecordSize > MAX_LOGRECORD_SIZE) {
        return Future.exception(new LogRecordTooLongException("Log record of size " + logRecordSize + " written when only " + MAX_LOGRECORD_SIZE + " is allowed"));
    }
    // if exceed max number of bytes
    if ((recordSetWriter.getNumBytes() + logRecordSize) > MAX_LOGRECORDSET_SIZE) {
        flush();
    }
    Promise<DLSN> writePromise = new Promise<DLSN>();
    try {
        recordSetWriter.writeRecord(buffer, writePromise);
    } catch (LogRecordTooLongException e) {
        return Future.exception(e);
    } catch (WriteException e) {
        recordSetWriter.abortTransmit(e);
        recordSetWriter = newRecordSetWriter();
        return Future.exception(e);
    }
    if (recordSetWriter.getNumBytes() >= bufferSize) {
        flush();
    }
    return writePromise;
}
Also used : Promise(com.twitter.util.Promise) WriteException(com.twitter.distributedlog.exceptions.WriteException) DLSN(com.twitter.distributedlog.DLSN) LogRecordTooLongException(com.twitter.distributedlog.exceptions.LogRecordTooLongException)

Aggregations

LogRecordTooLongException (com.twitter.distributedlog.exceptions.LogRecordTooLongException)8 ByteBuffer (java.nio.ByteBuffer)5 Test (org.junit.Test)4 WriteException (com.twitter.distributedlog.exceptions.WriteException)3 Promise (com.twitter.util.Promise)3 DLSN (com.twitter.distributedlog.DLSN)2 Writer (com.twitter.distributedlog.Entry.Writer)2 Reader (com.twitter.distributedlog.LogRecordSet.Reader)2 Writer (com.twitter.distributedlog.LogRecordSet.Writer)2 Buffer (com.twitter.distributedlog.io.Buffer)2 Future (com.twitter.util.Future)2 IOException (java.io.IOException)2 Reader (com.twitter.distributedlog.Entry.Reader)1 DistributedLogClient (com.twitter.distributedlog.service.DistributedLogClient)1