Search in sources :

Example 1 with SerializationException

use of io.pravega.common.io.SerializationException in project pravega by pravega.

the class RevisionDataOutputStreamTests method testNonSeekableOutputLongerLength.

/**
 * Tests the NonSeekableRevisionDataOutput class when we provide a shorter length than expected.
 */
@Test
public void testNonSeekableOutputLongerLength() throws Exception {
    byte b = 1;
    short sn = 2;
    int n = 3;
    @Cleanup val s = new ByteArrayOutputStream();
    // Wrap the stream, but do not auto-close it since we expect close() to fail, which is verified below.
    val impl = RevisionDataOutputStream.wrap(s);
    int correctLength = Byte.BYTES + Short.BYTES + Integer.BYTES;
    // Shorter length.
    impl.length(correctLength + 1);
    impl.writeByte(b);
    impl.writeShort(sn);
    impl.writeInt(n);
    // Verify close() fails.
    AssertExtensions.assertThrows("RevisionDataOutputStream.close() did not throw for byte mismatch.", impl::close, ex -> ex instanceof SerializationException);
    // Verify the written data can be read back.
    val inputStream = RevisionDataInputStream.wrap(new ByteArrayInputStream(s.toByteArray()));
    Assert.assertEquals("Unexpected byte read back.", b, inputStream.read());
    Assert.assertEquals("Unexpected short read back.", sn, inputStream.readShort());
    Assert.assertEquals("Unexpected int read back.", n, inputStream.readInt());
    // And verify we can't read anything else and we'll get an exception upon close indicating we read fewer bytes than expected.
    AssertExtensions.assertThrows("Expecting EOF.", () -> inputStream.readFully(new byte[1]), ex -> ex instanceof EOFException);
    AssertExtensions.assertThrows("Expecting an exception when reading fewer bytes than declared.", inputStream::close, ex -> ex instanceof SerializationException);
}
Also used : lombok.val(lombok.val) SerializationException(io.pravega.common.io.SerializationException) ByteArrayInputStream(java.io.ByteArrayInputStream) EOFException(java.io.EOFException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Example 2 with SerializationException

use of io.pravega.common.io.SerializationException in project pravega by pravega.

the class DataFrameReaderTests method testReadsWithDeserializationFailure.

/**
 * Tests the case when the DataFrameReader reads from a log and it encounters LogItem SerializationExceptions.
 */
@Test
public void testReadsWithDeserializationFailure() throws Exception {
    // Fail deserialization every X records (write-wise).
    int failDeserializationEvery = 11;
    ArrayList<TestLogItem> records = DataFrameTestHelpers.generateLogItems(100, SMALL_RECORD_MIN_SIZE, SMALL_RECORD_MAX_SIZE, 0);
    records.addAll(DataFrameTestHelpers.generateLogItems(100, LARGE_RECORD_MIN_SIZE, LARGE_RECORD_MAX_SIZE, records.size()));
    try (TestDurableDataLog dataLog = TestDurableDataLog.create(CONTAINER_ID, FRAME_SIZE, executorService())) {
        dataLog.initialize(TIMEOUT);
        BiConsumer<Throwable, DataFrameBuilder.CommitArgs> errorCallback = (ex, a) -> Assert.fail(String.format("Unexpected error occurred upon commit. %s", ex));
        val args = new DataFrameBuilder.Args(Callbacks::doNothing, Callbacks::doNothing, errorCallback, executorService());
        try (DataFrameBuilder<TestLogItem> b = new DataFrameBuilder<>(dataLog, SERIALIZER, args)) {
            for (TestLogItem r : records) {
                b.append(r);
            }
        }
        ErrorInjector<SerializationException> errorInjector = new ErrorInjector<>(count -> count % failDeserializationEvery == 0, () -> new SerializationException("TestLogItem.deserialize intentional"));
        TestSerializer logItemFactory = new TestSerializer();
        logItemFactory.setDeserializationErrorInjector(errorInjector);
        testReadWithException(dataLog, logItemFactory, ex -> ex instanceof DataCorruptionException);
    }
}
Also used : ObjectClosedException(io.pravega.common.ObjectClosedException) Callbacks(io.pravega.common.function.Callbacks) DurableDataLog(io.pravega.segmentstore.storage.DurableDataLog) AssertExtensions(io.pravega.test.common.AssertExtensions) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Exceptions(io.pravega.common.Exceptions) lombok.val(lombok.val) IOException(java.io.IOException) Test(org.junit.Test) TestDurableDataLog(io.pravega.segmentstore.server.TestDurableDataLog) ErrorInjector(io.pravega.test.common.ErrorInjector) DataLogNotAvailableException(io.pravega.segmentstore.storage.DataLogNotAvailableException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) Rule(org.junit.Rule) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) Duration(java.time.Duration) BiConsumer(java.util.function.BiConsumer) Timeout(org.junit.rules.Timeout) SerializationException(io.pravega.common.io.SerializationException) DataCorruptionException(io.pravega.segmentstore.server.DataCorruptionException) Assert(org.junit.Assert) lombok.val(lombok.val) SerializationException(io.pravega.common.io.SerializationException) TestDurableDataLog(io.pravega.segmentstore.server.TestDurableDataLog) ErrorInjector(io.pravega.test.common.ErrorInjector) Callbacks(io.pravega.common.function.Callbacks) DataCorruptionException(io.pravega.segmentstore.server.DataCorruptionException) Test(org.junit.Test)

Example 3 with SerializationException

use of io.pravega.common.io.SerializationException in project pravega by pravega.

the class StreamSegmentAppendOperationTests method testSerializationV0V1.

/**
 * Tests the following scenarios:
 * - Serialization and deserialization using V1.
 * - Serialization using V1 and deserialization using V0.
 * - Serialization using V0 and deserialization using V1.
 * <p>
 * NOTE: {@link #testSerialization()} validates that {@link StreamSegmentAppendOperation.Serializer} works well
 * with default settings (for normal scenarios). This method validates backwards compatibility.
 */
@Test
public void testSerializationV0V1() throws IOException {
    // Writes V0, reads both V0, V1.
    val defaultSerializer = new StreamSegmentAppendOperation.Serializer();
    // Writes and reads V0 only.
    val serializerV0 = new StreamSegmentAppendOperation.SerializerV0();
    // Writes V1, reads both V0, V1.
    val serializerV1 = new SerializerV1();
    val random = RandomFactory.create();
    val operations = Arrays.asList(createOperation(random), new StreamSegmentAppendOperation(1, new ByteArraySegment(new byte[1]), null));
    for (val baseOp : operations) {
        val baseOpUUIDOnly = new StreamSegmentAppendOperation(baseOp.getStreamSegmentId(), baseOp.getData(), baseOp.getAttributeUpdates() == null ? null : AttributeUpdateCollection.from(baseOp.getAttributeUpdates().getUUIDAttributeUpdates()));
        baseOp.setSequenceNumber(MathHelpers.abs(random.nextLong()));
        baseOpUUIDOnly.setSequenceNumber(baseOp.getSequenceNumber());
        configurePreSerialization(baseOp, random);
        baseOpUUIDOnly.setStreamSegmentOffset(baseOp.getStreamSegmentOffset());
        // 1. Deserialize the V0 serialization using ALL serializers. This simulates an upgrade scenario from old code.
        // This can only do UUIDs.
        val v0Serialized = serializerV0.serialize(baseOpUUIDOnly);
        val v0v0Deserialized = serializerV0.deserialize(v0Serialized);
        OperationComparer.DEFAULT.assertEquals(baseOpUUIDOnly, v0v0Deserialized);
        val v0v1Deserialized = serializerV1.deserialize(v0Serialized);
        OperationComparer.DEFAULT.assertEquals(baseOpUUIDOnly, v0v1Deserialized);
        val v0vdDeserialized = defaultSerializer.deserialize(v0Serialized);
        OperationComparer.DEFAULT.assertEquals(baseOpUUIDOnly, v0vdDeserialized);
        // 2. Deserialize the V1 serializer using V1 and Default serializer. This simulates normal operations.
        val v1Serialized = serializerV1.serialize(baseOp);
        val v1v1Deserialized = serializerV1.deserialize(v1Serialized);
        OperationComparer.DEFAULT.assertEquals(baseOp, v1v1Deserialized);
        val v1vdDeserialized = defaultSerializer.deserialize(v1Serialized);
        OperationComparer.DEFAULT.assertEquals(baseOp, v1vdDeserialized);
        AssertExtensions.assertThrows("V0 should not be able to deserialize V1", () -> serializerV0.deserialize(v1Serialized), ex -> ex instanceof SerializationException);
        // 3. Deserialize the Default serializer using ALL serializers. This simulates normal operations and "rollback".
        val dSerialized = defaultSerializer.serialize(baseOp);
        val vdv0Deserialized = serializerV0.deserialize(dSerialized);
        // V0 doesn't know about Variable IDs.
        OperationComparer.DEFAULT.assertEquals(baseOpUUIDOnly, vdv0Deserialized);
        val vdv1Deserialized = serializerV1.deserialize(dSerialized);
        OperationComparer.DEFAULT.assertEquals(baseOp, vdv1Deserialized);
        val vdvdDeserialized = defaultSerializer.deserialize(dSerialized);
        OperationComparer.DEFAULT.assertEquals(baseOp, vdvdDeserialized);
    }
}
Also used : lombok.val(lombok.val) ByteArraySegment(io.pravega.common.util.ByteArraySegment) SerializationException(io.pravega.common.io.SerializationException) Test(org.junit.Test)

Example 4 with SerializationException

use of io.pravega.common.io.SerializationException in project pravega by pravega.

the class AsyncTableEntryReaderTests method testReadEntryResultTooShort.

/**
 * Tests the ability to handle a case where the key could not be read before the read result was done.
 */
@Test
public void testReadEntryResultTooShort() {
    val testItems = generateTestItems();
    for (val e : testItems) {
        // Start a new reader & processor for this key-serialization pair.
        val entryReader = AsyncTableEntryReader.readEntry(new ByteArraySegment(e.key), 0L, SERIALIZER, new TimeoutTimer(TIMEOUT));
        @Cleanup val rr = new ReadResultMock(e.serialization, e.serialization.length - 1, 1);
        AsyncReadResultProcessor.process(rr, entryReader, executorService());
        AssertExtensions.assertThrows("Unexpected behavior for shorter read result..", () -> entryReader.getResult().get(BASE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS), ex -> ex instanceof SerializationException);
    }
}
Also used : lombok.val(lombok.val) ByteArraySegment(io.pravega.common.util.ByteArraySegment) SerializationException(io.pravega.common.io.SerializationException) ReadResultMock(io.pravega.segmentstore.server.ReadResultMock) Cleanup(lombok.Cleanup) TimeoutTimer(io.pravega.common.TimeoutTimer) Test(org.junit.Test)

Example 5 with SerializationException

use of io.pravega.common.io.SerializationException in project pravega by pravega.

the class AsyncTableEntryReaderTests method testReadEmptyKey.

/**
 * Tests the ability to read an empty key (this should result in an exception).
 */
@Test
public void testReadEmptyKey() {
    val testItem = generateTestItem(new byte[0], new byte[0], false, false);
    // Start a new reader & processor for this key-serialization pair.
    val keyReader = AsyncTableEntryReader.readKey(1L, SERIALIZER, new TimeoutTimer(TIMEOUT));
    @Cleanup val rr = new ReadResultMock(testItem.serialization, testItem.serialization.length, 1);
    AsyncReadResultProcessor.process(rr, keyReader, executorService());
    AssertExtensions.assertThrows("Unexpected behavior for empty key.", () -> keyReader.getResult().get(BASE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS), ex -> ex instanceof SerializationException);
    // When the result is done, whether with error or not, this should be set to 0.
    Assert.assertEquals("Unexpected final suggested read length.", 0, keyReader.getMaxReadAtOnce());
}
Also used : lombok.val(lombok.val) SerializationException(io.pravega.common.io.SerializationException) ReadResultMock(io.pravega.segmentstore.server.ReadResultMock) Cleanup(lombok.Cleanup) TimeoutTimer(io.pravega.common.TimeoutTimer) Test(org.junit.Test)

Aggregations

SerializationException (io.pravega.common.io.SerializationException)13 Test (org.junit.Test)9 lombok.val (lombok.val)8 Cleanup (lombok.Cleanup)7 TimeoutTimer (io.pravega.common.TimeoutTimer)4 ByteArraySegment (io.pravega.common.util.ByteArraySegment)4 ReadResultMock (io.pravega.segmentstore.server.ReadResultMock)3 AssertExtensions (io.pravega.test.common.AssertExtensions)3 ArrayList (java.util.ArrayList)3 Assert (org.junit.Assert)3 Exceptions (io.pravega.common.Exceptions)2 BufferView (io.pravega.common.util.BufferView)2 DurableDataLog (io.pravega.segmentstore.storage.DurableDataLog)2 ThreadPooledTestSuite (io.pravega.test.common.ThreadPooledTestSuite)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 EOFException (java.io.EOFException)2 IOException (java.io.IOException)2 Duration (java.time.Duration)2 List (java.util.List)2