Search in sources :

Example 1 with CheckpointType

use of org.apache.flink.runtime.checkpoint.CheckpointOptions.CheckpointType in project flink by apache.

the class EventSerializer method toSerializedEvent.

// ------------------------------------------------------------------------
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) {
        CheckpointBarrier barrier = (CheckpointBarrier) event;
        CheckpointOptions checkpointOptions = barrier.getCheckpointOptions();
        CheckpointType checkpointType = checkpointOptions.getCheckpointType();
        ByteBuffer buf;
        if (checkpointType == CheckpointType.FULL_CHECKPOINT) {
            buf = ByteBuffer.allocate(24);
            buf.putInt(0, CHECKPOINT_BARRIER_EVENT);
            buf.putLong(4, barrier.getId());
            buf.putLong(12, barrier.getTimestamp());
            buf.putInt(20, checkpointType.ordinal());
        } else if (checkpointType == CheckpointType.SAVEPOINT) {
            String targetLocation = checkpointOptions.getTargetLocation();
            assert (targetLocation != null);
            byte[] locationBytes = targetLocation.getBytes(STRING_CODING_CHARSET);
            buf = ByteBuffer.allocate(24 + 4 + locationBytes.length);
            buf.putInt(0, CHECKPOINT_BARRIER_EVENT);
            buf.putLong(4, barrier.getId());
            buf.putLong(12, barrier.getTimestamp());
            buf.putInt(20, checkpointType.ordinal());
            buf.putInt(24, locationBytes.length);
            for (int i = 0; i < locationBytes.length; i++) {
                buf.put(28 + i, locationBytes[i]);
            }
        } else {
            throw new IOException("Unknown checkpoint type: " + checkpointType);
        }
        return buf;
    } else if (eventClass == EndOfSuperstepEvent.class) {
        return ByteBuffer.wrap(new byte[] { 0, 0, 0, END_OF_SUPERSTEP_EVENT });
    } 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 {
        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) DataOutputSerializer(org.apache.flink.runtime.util.DataOutputSerializer) CheckpointType(org.apache.flink.runtime.checkpoint.CheckpointOptions.CheckpointType) CheckpointOptions(org.apache.flink.runtime.checkpoint.CheckpointOptions) CancelCheckpointMarker(org.apache.flink.runtime.io.network.api.CancelCheckpointMarker) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 2 with CheckpointType

use of org.apache.flink.runtime.checkpoint.CheckpointOptions.CheckpointType 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 {
        int type = buffer.getInt();
        if (type == END_OF_PARTITION_EVENT) {
            return EndOfPartitionEvent.INSTANCE;
        } else if (type == CHECKPOINT_BARRIER_EVENT) {
            long id = buffer.getLong();
            long timestamp = buffer.getLong();
            CheckpointOptions checkpointOptions;
            int checkpointTypeOrdinal = buffer.getInt();
            Preconditions.checkElementIndex(type, CheckpointType.values().length, "Illegal CheckpointType ordinal");
            CheckpointType checkpointType = CheckpointType.values()[checkpointTypeOrdinal];
            if (checkpointType == CheckpointType.FULL_CHECKPOINT) {
                checkpointOptions = CheckpointOptions.forFullCheckpoint();
            } else if (checkpointType == CheckpointType.SAVEPOINT) {
                int len = buffer.getInt();
                byte[] bytes = new byte[len];
                buffer.get(bytes);
                String targetLocation = new String(bytes, STRING_CODING_CHARSET);
                checkpointOptions = CheckpointOptions.forSavepoint(targetLocation);
            } else {
                throw new IOException("Unknown checkpoint type: " + checkpointType);
            }
            return new CheckpointBarrier(id, timestamp, checkpointOptions);
        } else if (type == END_OF_SUPERSTEP_EVENT) {
            return EndOfSuperstepEvent.INSTANCE;
        } else if (type == CANCEL_CHECKPOINT_MARKER_EVENT) {
            long id = buffer.getLong();
            return new CancelCheckpointMarker(id);
        } 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 : CheckpointType(org.apache.flink.runtime.checkpoint.CheckpointOptions.CheckpointType) 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) CheckpointBarrier(org.apache.flink.runtime.io.network.api.CheckpointBarrier) CheckpointOptions(org.apache.flink.runtime.checkpoint.CheckpointOptions) DataInputDeserializer(org.apache.flink.runtime.util.DataInputDeserializer)

Aggregations

IOException (java.io.IOException)2 CheckpointOptions (org.apache.flink.runtime.checkpoint.CheckpointOptions)2 CheckpointType (org.apache.flink.runtime.checkpoint.CheckpointOptions.CheckpointType)2 CancelCheckpointMarker (org.apache.flink.runtime.io.network.api.CancelCheckpointMarker)2 CheckpointBarrier (org.apache.flink.runtime.io.network.api.CheckpointBarrier)2 ByteBuffer (java.nio.ByteBuffer)1 ByteOrder (java.nio.ByteOrder)1 AbstractEvent (org.apache.flink.runtime.event.AbstractEvent)1 DataInputDeserializer (org.apache.flink.runtime.util.DataInputDeserializer)1 DataOutputSerializer (org.apache.flink.runtime.util.DataOutputSerializer)1