Search in sources :

Example 6 with SubtaskConnectionDescriptor

use of org.apache.flink.runtime.io.network.api.SubtaskConnectionDescriptor in project flink by apache.

the class ResultSubpartitionRecoveredStateHandler method recover.

@Override
public void recover(InputChannelInfo channelInfo, int oldSubtaskIndex, BufferWithContext<Buffer> bufferWithContext) throws IOException {
    Buffer buffer = bufferWithContext.context;
    try {
        if (buffer.readableBytes() > 0) {
            for (final RecoveredInputChannel channel : getMappedChannels(channelInfo)) {
                channel.onRecoveredStateBuffer(EventSerializer.toBuffer(new SubtaskConnectionDescriptor(oldSubtaskIndex, channelInfo.getInputChannelIdx()), false));
                channel.onRecoveredStateBuffer(buffer.retainBuffer());
            }
        }
    } finally {
        buffer.recycleBuffer();
    }
}
Also used : Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) RecoveredInputChannel(org.apache.flink.runtime.io.network.partition.consumer.RecoveredInputChannel) SubtaskConnectionDescriptor(org.apache.flink.runtime.io.network.api.SubtaskConnectionDescriptor)

Example 7 with SubtaskConnectionDescriptor

use of org.apache.flink.runtime.io.network.api.SubtaskConnectionDescriptor in project flink by apache.

the class EventSerializer method toSerializedEvent.

// ------------------------------------------------------------------------
// Serialization Logic
// ------------------------------------------------------------------------
public static ByteBuffer toSerializedEvent(AbstractEvent event) throws IOException {
    final Class<?> eventClass = event.getClass();
    if (eventClass == EndOfPartitionEvent.class) {
        return ByteBuffer.wrap(new byte[] { 0, 0, 0, END_OF_PARTITION_EVENT });
    } else if (eventClass == CheckpointBarrier.class) {
        return serializeCheckpointBarrier((CheckpointBarrier) event);
    } else if (eventClass == EndOfSuperstepEvent.class) {
        return ByteBuffer.wrap(new byte[] { 0, 0, 0, END_OF_SUPERSTEP_EVENT });
    } else if (eventClass == EndOfChannelStateEvent.class) {
        return ByteBuffer.wrap(new byte[] { 0, 0, 0, END_OF_CHANNEL_STATE_EVENT });
    } else if (eventClass == EndOfData.class) {
        return ByteBuffer.wrap(new byte[] { 0, 0, 0, END_OF_USER_RECORDS_EVENT, (byte) ((EndOfData) event).getStopMode().ordinal() });
    } else if (eventClass == CancelCheckpointMarker.class) {
        CancelCheckpointMarker marker = (CancelCheckpointMarker) event;
        ByteBuffer buf = ByteBuffer.allocate(12);
        buf.putInt(0, CANCEL_CHECKPOINT_MARKER_EVENT);
        buf.putLong(4, marker.getCheckpointId());
        return buf;
    } else if (eventClass == EventAnnouncement.class) {
        EventAnnouncement announcement = (EventAnnouncement) event;
        ByteBuffer serializedAnnouncedEvent = toSerializedEvent(announcement.getAnnouncedEvent());
        ByteBuffer serializedAnnouncement = ByteBuffer.allocate(2 * Integer.BYTES + serializedAnnouncedEvent.capacity());
        serializedAnnouncement.putInt(0, ANNOUNCEMENT_EVENT);
        serializedAnnouncement.putInt(4, announcement.getSequenceNumber());
        serializedAnnouncement.position(8);
        serializedAnnouncement.put(serializedAnnouncedEvent);
        serializedAnnouncement.flip();
        return serializedAnnouncement;
    } else if (eventClass == SubtaskConnectionDescriptor.class) {
        SubtaskConnectionDescriptor selector = (SubtaskConnectionDescriptor) event;
        ByteBuffer buf = ByteBuffer.allocate(12);
        buf.putInt(VIRTUAL_CHANNEL_SELECTOR_EVENT);
        buf.putInt(selector.getInputSubtaskIndex());
        buf.putInt(selector.getOutputSubtaskIndex());
        buf.flip();
        return buf;
    } else {
        try {
            final DataOutputSerializer serializer = new DataOutputSerializer(128);
            serializer.writeInt(OTHER_EVENT);
            serializer.writeUTF(event.getClass().getName());
            event.write(serializer);
            return serializer.wrapAsByteBuffer();
        } catch (IOException e) {
            throw new IOException("Error while serializing event.", e);
        }
    }
}
Also used : CheckpointBarrier(org.apache.flink.runtime.io.network.api.CheckpointBarrier) EndOfChannelStateEvent(org.apache.flink.runtime.io.network.partition.consumer.EndOfChannelStateEvent) DataOutputSerializer(org.apache.flink.core.memory.DataOutputSerializer) EventAnnouncement(org.apache.flink.runtime.io.network.api.EventAnnouncement) CancelCheckpointMarker(org.apache.flink.runtime.io.network.api.CancelCheckpointMarker) SubtaskConnectionDescriptor(org.apache.flink.runtime.io.network.api.SubtaskConnectionDescriptor) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 8 with SubtaskConnectionDescriptor

use of org.apache.flink.runtime.io.network.api.SubtaskConnectionDescriptor 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)

Aggregations

SubtaskConnectionDescriptor (org.apache.flink.runtime.io.network.api.SubtaskConnectionDescriptor)8 Buffer (org.apache.flink.runtime.io.network.buffer.Buffer)4 BufferBuilder (org.apache.flink.runtime.io.network.buffer.BufferBuilder)4 MemorySegment (org.apache.flink.core.memory.MemorySegment)3 InputChannelInfo (org.apache.flink.runtime.checkpoint.channel.InputChannelInfo)3 BufferBuilderTestUtils.createBufferBuilder (org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils.createBufferBuilder)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 CancelCheckpointMarker (org.apache.flink.runtime.io.network.api.CancelCheckpointMarker)2 EventAnnouncement (org.apache.flink.runtime.io.network.api.EventAnnouncement)2 ByteBuffer (java.nio.ByteBuffer)1 ByteOrder (java.nio.ByteOrder)1 DataInputDeserializer (org.apache.flink.core.memory.DataInputDeserializer)1 DataOutputSerializer (org.apache.flink.core.memory.DataOutputSerializer)1 AbstractEvent (org.apache.flink.runtime.event.AbstractEvent)1 CheckpointBarrier (org.apache.flink.runtime.io.network.api.CheckpointBarrier)1 EndOfData (org.apache.flink.runtime.io.network.api.EndOfData)1 BufferConsumer (org.apache.flink.runtime.io.network.buffer.BufferConsumer)1 CheckpointedResultSubpartition (org.apache.flink.runtime.io.network.partition.CheckpointedResultSubpartition)1 EndOfChannelStateEvent (org.apache.flink.runtime.io.network.partition.consumer.EndOfChannelStateEvent)1