Search in sources :

Example 16 with AbstractEvent

use of org.apache.flink.runtime.event.AbstractEvent in project flink by apache.

the class RecordWriterTest method parseBuffer.

// ---------------------------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------------------------
static BufferOrEvent parseBuffer(Buffer buffer, int targetChannel) throws IOException {
    if (buffer.isBuffer()) {
        return new BufferOrEvent(buffer, new InputChannelInfo(0, targetChannel));
    } else {
        // is event:
        AbstractEvent event = EventSerializer.fromBuffer(buffer, RecordWriterTest.class.getClassLoader());
        // the buffer is not needed anymore
        buffer.recycleBuffer();
        return new BufferOrEvent(event, new InputChannelInfo(0, targetChannel));
    }
}
Also used : InputChannelInfo(org.apache.flink.runtime.checkpoint.channel.InputChannelInfo) AbstractEvent(org.apache.flink.runtime.event.AbstractEvent) BufferOrEvent(org.apache.flink.runtime.io.network.partition.consumer.BufferOrEvent)

Example 17 with AbstractEvent

use of org.apache.flink.runtime.event.AbstractEvent in project flink by apache.

the class EventSerializerTest method testSerializeDeserializeEvent.

@Test
public void testSerializeDeserializeEvent() throws Exception {
    for (AbstractEvent evt : events) {
        ByteBuffer serializedEvent = EventSerializer.toSerializedEvent(evt);
        assertTrue(serializedEvent.hasRemaining());
        AbstractEvent deserialized = EventSerializer.fromSerializedEvent(serializedEvent, getClass().getClassLoader());
        assertNotNull(deserialized);
        assertEquals(evt, deserialized);
    }
}
Also used : AbstractEvent(org.apache.flink.runtime.event.AbstractEvent) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 18 with AbstractEvent

use of org.apache.flink.runtime.event.AbstractEvent in project flink by apache.

the class EventSerializer method fromSerializedEvent.

public static AbstractEvent fromSerializedEvent(ByteBuffer buffer, ClassLoader classLoader) throws IOException {
    if (buffer.remaining() < 4) {
        throw new IOException("Incomplete event");
    }
    final ByteOrder bufferOrder = buffer.order();
    buffer.order(ByteOrder.BIG_ENDIAN);
    try {
        final int type = buffer.getInt();
        if (type == END_OF_PARTITION_EVENT) {
            return EndOfPartitionEvent.INSTANCE;
        } else if (type == CHECKPOINT_BARRIER_EVENT) {
            return deserializeCheckpointBarrier(buffer);
        } else if (type == END_OF_SUPERSTEP_EVENT) {
            return EndOfSuperstepEvent.INSTANCE;
        } else if (type == END_OF_CHANNEL_STATE_EVENT) {
            return EndOfChannelStateEvent.INSTANCE;
        } else if (type == END_OF_USER_RECORDS_EVENT) {
            return new EndOfData(StopMode.values()[buffer.get()]);
        } else if (type == CANCEL_CHECKPOINT_MARKER_EVENT) {
            long id = buffer.getLong();
            return new CancelCheckpointMarker(id);
        } else if (type == ANNOUNCEMENT_EVENT) {
            int sequenceNumber = buffer.getInt();
            AbstractEvent announcedEvent = fromSerializedEvent(buffer, classLoader);
            return new EventAnnouncement(announcedEvent, sequenceNumber);
        } else if (type == VIRTUAL_CHANNEL_SELECTOR_EVENT) {
            return new SubtaskConnectionDescriptor(buffer.getInt(), buffer.getInt());
        } else if (type == OTHER_EVENT) {
            try {
                final DataInputDeserializer deserializer = new DataInputDeserializer(buffer);
                final String className = deserializer.readUTF();
                final Class<? extends AbstractEvent> clazz;
                try {
                    clazz = classLoader.loadClass(className).asSubclass(AbstractEvent.class);
                } catch (ClassNotFoundException e) {
                    throw new IOException("Could not load event class '" + className + "'.", e);
                } catch (ClassCastException e) {
                    throw new IOException("The class '" + className + "' is not a valid subclass of '" + AbstractEvent.class.getName() + "'.", e);
                }
                final AbstractEvent event = InstantiationUtil.instantiate(clazz, AbstractEvent.class);
                event.read(deserializer);
                return event;
            } catch (Exception e) {
                throw new IOException("Error while deserializing or instantiating event.", e);
            }
        } else {
            throw new IOException("Corrupt byte stream for event");
        }
    } finally {
        buffer.order(bufferOrder);
    }
}
Also used : EventAnnouncement(org.apache.flink.runtime.io.network.api.EventAnnouncement) CancelCheckpointMarker(org.apache.flink.runtime.io.network.api.CancelCheckpointMarker) IOException(java.io.IOException) ByteOrder(java.nio.ByteOrder) AbstractEvent(org.apache.flink.runtime.event.AbstractEvent) IOException(java.io.IOException) EndOfData(org.apache.flink.runtime.io.network.api.EndOfData) SubtaskConnectionDescriptor(org.apache.flink.runtime.io.network.api.SubtaskConnectionDescriptor) DataInputDeserializer(org.apache.flink.core.memory.DataInputDeserializer)

Example 19 with AbstractEvent

use of org.apache.flink.runtime.event.AbstractEvent in project flink by apache.

the class RecoveredInputChannel method isEndOfChannelStateEvent.

private boolean isEndOfChannelStateEvent(Buffer buffer) throws IOException {
    if (buffer.isBuffer()) {
        return false;
    }
    AbstractEvent event = EventSerializer.fromBuffer(buffer, getClass().getClassLoader());
    buffer.setReaderIndex(0);
    return event.getClass() == EndOfChannelStateEvent.class;
}
Also used : AbstractEvent(org.apache.flink.runtime.event.AbstractEvent)

Example 20 with AbstractEvent

use of org.apache.flink.runtime.event.AbstractEvent in project flink by apache.

the class RemoteInputChannel method convertToPriorityEvent.

@Override
public void convertToPriorityEvent(int sequenceNumber) throws IOException {
    boolean firstPriorityEvent;
    synchronized (receivedBuffers) {
        checkState(channelStatePersister.hasBarrierReceived());
        int numPriorityElementsBeforeRemoval = receivedBuffers.getNumPriorityElements();
        SequenceBuffer toPrioritize = receivedBuffers.getAndRemove(sequenceBuffer -> sequenceBuffer.sequenceNumber == sequenceNumber);
        checkState(lastBarrierSequenceNumber == sequenceNumber);
        checkState(!toPrioritize.buffer.isBuffer());
        checkState(numPriorityElementsBeforeRemoval == receivedBuffers.getNumPriorityElements(), "Attempted to convertToPriorityEvent an event [%s] that has already been prioritized [%s]", toPrioritize, numPriorityElementsBeforeRemoval);
        // set the priority flag (checked on poll)
        // don't convert the barrier itself (barrier controller might not have been switched
        // yet)
        AbstractEvent e = EventSerializer.fromBuffer(toPrioritize.buffer, this.getClass().getClassLoader());
        toPrioritize.buffer.setReaderIndex(0);
        toPrioritize = new SequenceBuffer(EventSerializer.toBuffer(e, true), toPrioritize.sequenceNumber);
        firstPriorityEvent = addPriorityBuffer(// note that only position of the element is changed
        toPrioritize);
    // converting the event itself would require switching the controller sooner
    }
    if (firstPriorityEvent) {
        // forcibly notify about the priority event
        notifyPriorityEventForce();
    // instead of passing barrier SQN to be checked
    // because this SQN might have be seen by the input gate during the announcement
    }
}
Also used : AbstractEvent(org.apache.flink.runtime.event.AbstractEvent)

Aggregations

AbstractEvent (org.apache.flink.runtime.event.AbstractEvent)24 CheckpointBarrier (org.apache.flink.runtime.io.network.api.CheckpointBarrier)6 IOException (java.io.IOException)5 Buffer (org.apache.flink.runtime.io.network.buffer.Buffer)5 Test (org.junit.Test)5 EventAnnouncement (org.apache.flink.runtime.io.network.api.EventAnnouncement)4 BufferConsumer (org.apache.flink.runtime.io.network.buffer.BufferConsumer)4 ByteBuffer (java.nio.ByteBuffer)3 EndOfData (org.apache.flink.runtime.io.network.api.EndOfData)3 BufferOrEvent (org.apache.flink.runtime.io.network.partition.consumer.BufferOrEvent)3 StreamElement (org.apache.flink.streaming.runtime.streamrecord.StreamElement)3 ByteOrder (java.nio.ByteOrder)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Nullable (javax.annotation.Nullable)2 CheckpointMetaData (org.apache.flink.runtime.checkpoint.CheckpointMetaData)2 CheckpointMetricsBuilder (org.apache.flink.runtime.checkpoint.CheckpointMetricsBuilder)2 CheckpointOptions (org.apache.flink.runtime.checkpoint.CheckpointOptions)2 CancelCheckpointMarker (org.apache.flink.runtime.io.network.api.CancelCheckpointMarker)2 EndOfPartitionEvent (org.apache.flink.runtime.io.network.api.EndOfPartitionEvent)2 DeserializationResult (org.apache.flink.runtime.io.network.api.serialization.RecordDeserializer.DeserializationResult)2