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();
}
}
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);
}
}
}
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);
}
}
Aggregations