Search in sources :

Example 1 with FileBackedOutputStream

use of org.opendaylight.controller.cluster.io.FileBackedOutputStream in project controller by opendaylight.

the class MessageSlicerTest method testCancelSlicing.

@Test
public void testCancelSlicing() throws IOException {
    doReturn(1).when(mockInputStream).read(any(byte[].class));
    final MessageSlicer slicer = newMessageSlicer("testCloseAllSlicedMessageState", 1);
    slicer.slice(SliceOptions.builder().identifier(IDENTIFIER).fileBackedOutputStream(mockFiledBackedStream).sendTo(testProbe.ref()).replyTo(testProbe.ref()).onFailureCallback(mockOnFailureCallback).build());
    final FileBackedOutputStream mockFiledBackedStream2 = mock(FileBackedOutputStream.class);
    setupMockFiledBackedStream(mockFiledBackedStream2);
    slicer.slice(SliceOptions.builder().identifier(new StringIdentifier("test2")).fileBackedOutputStream(mockFiledBackedStream2).sendTo(testProbe.ref()).replyTo(testProbe.ref()).onFailureCallback(mockOnFailureCallback).build());
    slicer.cancelSlicing(id -> id.equals(IDENTIFIER));
    verify(mockFiledBackedStream).cleanup();
    verify(mockFiledBackedStream2, never()).cleanup();
    verifyNoMoreInteractions(mockOnFailureCallback);
}
Also used : FileBackedOutputStream(org.opendaylight.controller.cluster.io.FileBackedOutputStream) Test(org.junit.Test)

Example 2 with FileBackedOutputStream

use of org.opendaylight.controller.cluster.io.FileBackedOutputStream in project controller by opendaylight.

the class MessageAssemblerTest method testSingleMessageSlice.

@Test
public void testSingleMessageSlice() {
    try (MessageAssembler assembler = newMessageAssembler("testSingleMessageSlice")) {
        final FileBackedOutputStream fileBackStream = spy(new FileBackedOutputStream(100000000, null));
        doReturn(fileBackStream).when(mockFiledBackedStreamFactory).newInstance();
        final MessageSliceIdentifier identifier = new MessageSliceIdentifier(IDENTIFIER, 1);
        final BytesMessage message = new BytesMessage(new byte[] { 1, 2, 3 });
        final MessageSlice messageSlice = new MessageSlice(identifier, SerializationUtils.serialize(message), 1, 1, SlicedMessageState.INITIAL_SLICE_HASH_CODE, testProbe.ref());
        assembler.handleMessage(messageSlice, testProbe.ref());
        final MessageSliceReply reply = testProbe.expectMsgClass(MessageSliceReply.class);
        assertSuccessfulMessageSliceReply(reply, IDENTIFIER, 1);
        assertAssembledMessage(mockAssembledMessageCallback, message, testProbe.ref());
        assertFalse("MessageAssembler did not remove state for " + identifier, assembler.hasState(identifier));
        verify(fileBackStream).cleanup();
    }
}
Also used : MessageSlicingIntegrationTest.assertFailedMessageSliceReply(org.opendaylight.controller.cluster.messaging.MessageSlicingIntegrationTest.assertFailedMessageSliceReply) MessageSlicingIntegrationTest.assertSuccessfulMessageSliceReply(org.opendaylight.controller.cluster.messaging.MessageSlicingIntegrationTest.assertSuccessfulMessageSliceReply) FileBackedOutputStream(org.opendaylight.controller.cluster.io.FileBackedOutputStream) Test(org.junit.Test)

Example 3 with FileBackedOutputStream

use of org.opendaylight.controller.cluster.io.FileBackedOutputStream in project controller by opendaylight.

the class SnapshotTrackerTest method setup.

@Before
public void setup() {
    MockitoAnnotations.initMocks(this);
    data = new HashMap<>();
    data.put("key1", "value1");
    data.put("key2", "value2");
    data.put("key3", "value3");
    byteString = ByteString.copyFrom(SerializationUtils.serialize((Serializable) data));
    chunk1 = getNextChunk(byteString, 0, 10);
    chunk2 = getNextChunk(byteString, 10, 10);
    chunk3 = getNextChunk(byteString, 20, byteString.size());
    fbos = spy(new FileBackedOutputStream(100000000, "target"));
    FileBackedOutputStreamFactory mockFactory = mock(FileBackedOutputStreamFactory.class);
    doReturn(fbos).when(mockFactory).newInstance();
    doReturn(mockFactory).when(mockContext).getFileBackedOutputStreamFactory();
}
Also used : FileBackedOutputStreamFactory(org.opendaylight.controller.cluster.io.FileBackedOutputStreamFactory) FileBackedOutputStream(org.opendaylight.controller.cluster.io.FileBackedOutputStream) Before(org.junit.Before)

Example 4 with FileBackedOutputStream

use of org.opendaylight.controller.cluster.io.FileBackedOutputStream in project controller by opendaylight.

the class MessageSlicer method slice.

/**
 * Slices a message into chunks based on the serialized size, the maximum message slice size and the given
 * options.
 *
 * @param options the SliceOptions
 * @return true if the message was sliced, false otherwise
 */
public boolean slice(final SliceOptions options) {
    final Identifier identifier = options.getIdentifier();
    final Serializable message = options.getMessage();
    final FileBackedOutputStream fileBackedStream;
    if (message != null) {
        LOG.debug("{}: slice: identifier: {}, message: {}", logContext, identifier, message);
        Preconditions.checkNotNull(fileBackedStreamFactory, "The FiledBackedStreamFactory must be set in order to call this slice method");
        // Serialize the message to a FileBackedOutputStream.
        fileBackedStream = fileBackedStreamFactory.newInstance();
        try (ObjectOutputStream out = new ObjectOutputStream(fileBackedStream)) {
            out.writeObject(message);
        } catch (IOException e) {
            LOG.debug("{}: Error serializing message for {}", logContext, identifier, e);
            fileBackedStream.cleanup();
            options.getOnFailureCallback().accept(e);
            return false;
        }
    } else {
        fileBackedStream = options.getFileBackedStream();
    }
    return initializeSlicing(options, fileBackedStream);
}
Also used : Serializable(java.io.Serializable) Identifier(org.opendaylight.yangtools.concepts.Identifier) FileBackedOutputStream(org.opendaylight.controller.cluster.io.FileBackedOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

Example 5 with FileBackedOutputStream

use of org.opendaylight.controller.cluster.io.FileBackedOutputStream in project controller by opendaylight.

the class MessageSlicingIntegrationTest method testSliceWithFileBackedOutputStream.

@Test
public void testSliceWithFileBackedOutputStream() throws IOException {
    LOG.info("testSliceWithFileBackedOutputStream starting");
    final BytesMessage message = new BytesMessage(new byte[] { 1, 2, 3 });
    FileBackedOutputStream fileBackedStream = FILE_BACKED_STREAM_FACTORY.newInstance();
    try (ObjectOutputStream out = new ObjectOutputStream(fileBackedStream)) {
        out.writeObject(message);
    }
    try (MessageSlicer slicer = newMessageSlicer("testSliceWithFileBackedOutputStream", SerializationUtils.serialize(message).length)) {
        slicer.slice(SliceOptions.builder().identifier(IDENTIFIER).fileBackedOutputStream(fileBackedStream).sendTo(ACTOR_SYSTEM.actorSelection(sendToProbe.ref().path())).replyTo(replyToProbe.ref()).onFailureCallback(mockOnFailureCallback).build());
        final MessageSlice sliceMessage = sendToProbe.expectMsgClass(MessageSlice.class);
        assembler.handleMessage(sliceMessage, sendToProbe.ref());
        assertAssembledMessage(message, replyToProbe.ref());
    }
    LOG.info("testSliceWithFileBackedOutputStream ending");
}
Also used : FileBackedOutputStream(org.opendaylight.controller.cluster.io.FileBackedOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) Test(org.junit.Test)

Aggregations

FileBackedOutputStream (org.opendaylight.controller.cluster.io.FileBackedOutputStream)5 Test (org.junit.Test)3 ObjectOutputStream (java.io.ObjectOutputStream)2 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 Before (org.junit.Before)1 FileBackedOutputStreamFactory (org.opendaylight.controller.cluster.io.FileBackedOutputStreamFactory)1 MessageSlicingIntegrationTest.assertFailedMessageSliceReply (org.opendaylight.controller.cluster.messaging.MessageSlicingIntegrationTest.assertFailedMessageSliceReply)1 MessageSlicingIntegrationTest.assertSuccessfulMessageSliceReply (org.opendaylight.controller.cluster.messaging.MessageSlicingIntegrationTest.assertSuccessfulMessageSliceReply)1 Identifier (org.opendaylight.yangtools.concepts.Identifier)1