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