/** * Triggers an asynchronous snapshot of the keyed state backend from RocksDB. This snapshot can be canceled and * is also stopped when the backend is closed through {@link #dispose()}. For each backend, this method must always * be called by the same thread. * * @param checkpointId The Id of the checkpoint. * @param timestamp The timestamp of the checkpoint. * @param streamFactory The factory that we can use for writing our state to streams. * @param checkpointOptions Options for how to perform this checkpoint. * @return Future to the state handle of the snapshot data. * @throws Exception indicating a problem in the synchronous part of the checkpoint. */ @Nonnull @Override public RunnableFuture<SnapshotResult<KeyedStateHandle>> snapshot( final long checkpointId, final long timestamp, @Nonnull final CheckpointStreamFactory streamFactory, @Nonnull CheckpointOptions checkpointOptions) throws Exception { long startTime = System.currentTimeMillis(); // flush everything into db before taking a snapshot writeBatchWrapper.flush(); RocksDBSnapshotStrategyBase<K> chosenSnapshotStrategy = CheckpointType.SAVEPOINT == checkpointOptions.getCheckpointType() ? savepointSnapshotStrategy : checkpointSnapshotStrategy; RunnableFuture<SnapshotResult<KeyedStateHandle>> snapshotRunner = chosenSnapshotStrategy.snapshot(checkpointId, timestamp, streamFactory, checkpointOptions); chosenSnapshotStrategy.logSyncCompleted(streamFactory, startTime); return snapshotRunner; }
private void checkpointState( CheckpointMetaData checkpointMetaData, CheckpointOptions checkpointOptions, CheckpointMetrics checkpointMetrics) throws Exception { CheckpointStreamFactory storage = checkpointStorage.resolveCheckpointStorageLocation( checkpointMetaData.getCheckpointId(), checkpointOptions.getTargetLocation()); CheckpointingOperation checkpointingOperation = new CheckpointingOperation( this, checkpointMetaData, checkpointOptions, storage, checkpointMetrics); checkpointingOperation.executeCheckpointing(); }
@Override public void triggerCheckpoint(long checkpointId) throws FlinkException { // TODO - we need to see how to derive those. We should probably not encode this in the // TODO - source's trigger message, but do a handshake in this task between the trigger // TODO - message from the master, and the source's trigger notification final CheckpointOptions checkpointOptions = CheckpointOptions.forCheckpointWithDefaultLocation(); final long timestamp = System.currentTimeMillis(); final CheckpointMetaData checkpointMetaData = new CheckpointMetaData(checkpointId, timestamp); try { SourceStreamTask.super.triggerCheckpoint(checkpointMetaData, checkpointOptions); } catch (RuntimeException | FlinkException e) { throw e; } catch (Exception e) { throw new FlinkException(e.getMessage(), e); } } };
/** * Returns a checkpoint stream factory for the provided options. * * <p>For {@link CheckpointType#FULL_CHECKPOINT} this returns the shared * factory of this operator. * * <p>For {@link CheckpointType#SAVEPOINT} it creates a custom factory per * savepoint. * * @param checkpointOptions Options for the checkpoint * @return Checkpoint stream factory for the checkpoints * @throws IOException Failures while creating a new stream factory are forwarded */ @VisibleForTesting CheckpointStreamFactory getCheckpointStreamFactory(CheckpointOptions checkpointOptions) throws IOException { CheckpointType checkpointType = checkpointOptions.getCheckpointType(); if (checkpointType == CheckpointType.FULL_CHECKPOINT) { return checkpointStreamFactory; } else if (checkpointType == CheckpointType.SAVEPOINT) { return container.createSavepointStreamFactory(this, checkpointOptions.getTargetLocation()); } else { throw new IllegalStateException("Unknown checkpoint type " + checkpointType); } }
public static CheckpointOptions forSavepoint(String targetDirectory) { checkNotNull(targetDirectory, "targetDirectory"); return new CheckpointOptions(CheckpointType.SAVEPOINT, targetDirectory); }
@Override public boolean equals(Object other) { if (other == this) { return true; } else if (other == null || other.getClass() != CheckpointBarrier.class) { return false; } else { CheckpointBarrier that = (CheckpointBarrier) other; return that.id == this.id && that.timestamp == this.timestamp && this.checkpointOptions.equals(that.checkpointOptions); } }
checkpointOptions = CheckpointOptions.forFullCheckpoint(); } else if (checkpointType == CheckpointType.SAVEPOINT) { int len = buffer.getInt(); String targetLocation = new String(bytes, STRING_CODING_CHARSET); checkpointOptions = CheckpointOptions.forSavepoint(targetLocation); } else { throw new IOException("Unknown checkpoint type: " + checkpointType);
@Override public void triggerCheckpoint(long checkpointId) throws FlinkException { // TODO - we need to see how to derive those. We should probably not encode this in the // TODO - source's trigger message, but do a handshake in this task between the trigger // TODO - message from the master, and the source's trigger notification final CheckpointOptions checkpointOptions = CheckpointOptions.forFullCheckpoint(); final long timestamp = System.currentTimeMillis(); final CheckpointMetaData checkpointMetaData = new CheckpointMetaData(checkpointId, timestamp); try { SourceStreamTask.super.triggerCheckpoint(checkpointMetaData, checkpointOptions); } catch (RuntimeException | FlinkException e) { throw e; } catch (Exception e) { throw new FlinkException(e.getMessage(), e); } } };
private static ByteBuffer serializeCheckpointBarrier(CheckpointBarrier barrier) throws IOException { final CheckpointOptions checkpointOptions = barrier.getCheckpointOptions(); final CheckpointType checkpointType = checkpointOptions.getCheckpointType(); final byte[] locationBytes = checkpointOptions.getTargetLocation().isDefaultReference() ? null : checkpointOptions.getTargetLocation().getReferenceBytes(); final ByteBuffer buf = ByteBuffer.allocate(28 + (locationBytes == null ? 0 : locationBytes.length)); // we do not use checkpointType.ordinal() here to make the serialization robust // against changes in the enum (such as changes in the order of the values) final int typeInt; if (checkpointType == CheckpointType.CHECKPOINT) { typeInt = CHECKPOINT_TYPE_CHECKPOINT; } else if (checkpointType == CheckpointType.SAVEPOINT) { typeInt = CHECKPOINT_TYPE_SAVEPOINT; } else { throw new IOException("Unknown checkpoint type: " + checkpointType); } buf.putInt(CHECKPOINT_BARRIER_EVENT); buf.putLong(barrier.getId()); buf.putLong(barrier.getTimestamp()); buf.putInt(typeInt); if (locationBytes == null) { buf.putInt(-1); } else { buf.putInt(locationBytes.length); buf.put(locationBytes); } buf.flip(); return buf; }
private static CheckpointBarrier deserializeCheckpointBarrier(ByteBuffer buffer) throws IOException { final long id = buffer.getLong(); final long timestamp = buffer.getLong(); final int checkpointTypeCode = buffer.getInt(); final int locationRefLen = buffer.getInt(); final CheckpointType checkpointType; if (checkpointTypeCode == CHECKPOINT_TYPE_CHECKPOINT) { checkpointType = CheckpointType.CHECKPOINT; } else if (checkpointTypeCode == CHECKPOINT_TYPE_SAVEPOINT) { checkpointType = CheckpointType.SAVEPOINT; } else { throw new IOException("Unknown checkpoint type code: " + checkpointTypeCode); } final CheckpointStorageLocationReference locationRef; if (locationRefLen == -1) { locationRef = CheckpointStorageLocationReference.getDefault(); } else { byte[] bytes = new byte[locationRefLen]; buffer.get(bytes); locationRef = new CheckpointStorageLocationReference(bytes); } return new CheckpointBarrier(id, timestamp, new CheckpointOptions(checkpointType, locationRef)); }
@Override public boolean equals(Object other) { if (other == this) { return true; } else if (other == null || other.getClass() != CheckpointBarrier.class) { return false; } else { CheckpointBarrier that = (CheckpointBarrier) other; return that.id == this.id && that.timestamp == this.timestamp && this.checkpointOptions.equals(that.checkpointOptions); } }
private static ByteBuffer serializeCheckpointBarrier(CheckpointBarrier barrier) throws IOException { final CheckpointOptions checkpointOptions = barrier.getCheckpointOptions(); final CheckpointType checkpointType = checkpointOptions.getCheckpointType(); final byte[] locationBytes = checkpointOptions.getTargetLocation().isDefaultReference() ? null : checkpointOptions.getTargetLocation().getReferenceBytes(); final ByteBuffer buf = ByteBuffer.allocate(28 + (locationBytes == null ? 0 : locationBytes.length)); // we do not use checkpointType.ordinal() here to make the serialization robust // against changes in the enum (such as changes in the order of the values) final int typeInt; if (checkpointType == CheckpointType.CHECKPOINT) { typeInt = CHECKPOINT_TYPE_CHECKPOINT; } else if (checkpointType == CheckpointType.SAVEPOINT) { typeInt = CHECKPOINT_TYPE_SAVEPOINT; } else { throw new IOException("Unknown checkpoint type: " + checkpointType); } buf.putInt(CHECKPOINT_BARRIER_EVENT); buf.putLong(barrier.getId()); buf.putLong(barrier.getTimestamp()); buf.putInt(typeInt); if (locationBytes == null) { buf.putInt(-1); } else { buf.putInt(locationBytes.length); buf.put(locationBytes); } buf.flip(); return buf; }
private SupplierWithException<CheckpointStreamWithResultProvider, Exception> createCheckpointStreamSupplier( long checkpointId, CheckpointStreamFactory primaryStreamFactory, CheckpointOptions checkpointOptions) { return localRecoveryConfig.isLocalRecoveryEnabled() && (CheckpointType.SAVEPOINT != checkpointOptions.getCheckpointType()) ? () -> CheckpointStreamWithResultProvider.createDuplicatingStream( checkpointId, CheckpointedStateScope.EXCLUSIVE, primaryStreamFactory, localRecoveryConfig.getLocalStateDirectoryProvider()) : () -> CheckpointStreamWithResultProvider.createSimpleStream( CheckpointedStateScope.EXCLUSIVE, primaryStreamFactory); }
@Override public Boolean call() throws Exception { for (int i = 0; i < numCheckpoints; i++) { long currentCheckpointId = checkpointId.getAndIncrement(); CheckpointMetaData checkpointMetaData = new CheckpointMetaData(currentCheckpointId, 0L); sourceTask.triggerCheckpoint(checkpointMetaData, CheckpointOptions.forCheckpointWithDefaultLocation()); Thread.sleep(checkpointInterval); } return true; } }
private void checkpointState( CheckpointMetaData checkpointMetaData, CheckpointOptions checkpointOptions, CheckpointMetrics checkpointMetrics) throws Exception { CheckpointStreamFactory storage = checkpointStorage.resolveCheckpointStorageLocation( checkpointMetaData.getCheckpointId(), checkpointOptions.getTargetLocation()); CheckpointingOperation checkpointingOperation = new CheckpointingOperation( this, checkpointMetaData, checkpointOptions, storage, checkpointMetrics); checkpointingOperation.executeCheckpointing(); }
private static CheckpointBarrier deserializeCheckpointBarrier(ByteBuffer buffer) throws IOException { final long id = buffer.getLong(); final long timestamp = buffer.getLong(); final int checkpointTypeCode = buffer.getInt(); final int locationRefLen = buffer.getInt(); final CheckpointType checkpointType; if (checkpointTypeCode == CHECKPOINT_TYPE_CHECKPOINT) { checkpointType = CheckpointType.CHECKPOINT; } else if (checkpointTypeCode == CHECKPOINT_TYPE_SAVEPOINT) { checkpointType = CheckpointType.SAVEPOINT; } else { throw new IOException("Unknown checkpoint type code: " + checkpointTypeCode); } final CheckpointStorageLocationReference locationRef; if (locationRefLen == -1) { locationRef = CheckpointStorageLocationReference.getDefault(); } else { byte[] bytes = new byte[locationRefLen]; buffer.get(bytes); locationRef = new CheckpointStorageLocationReference(bytes); } return new CheckpointBarrier(id, timestamp, new CheckpointOptions(checkpointType, locationRef)); }
@Override public boolean equals(Object other) { if (other == this) { return true; } else if (other == null || other.getClass() != CheckpointBarrier.class) { return false; } else { CheckpointBarrier that = (CheckpointBarrier) other; return that.id == this.id && that.timestamp == this.timestamp && this.checkpointOptions.equals(that.checkpointOptions); } }
private static ByteBuffer serializeCheckpointBarrier(CheckpointBarrier barrier) throws IOException { final CheckpointOptions checkpointOptions = barrier.getCheckpointOptions(); final CheckpointType checkpointType = checkpointOptions.getCheckpointType(); final byte[] locationBytes = checkpointOptions.getTargetLocation().isDefaultReference() ? null : checkpointOptions.getTargetLocation().getReferenceBytes(); final ByteBuffer buf = ByteBuffer.allocate(28 + (locationBytes == null ? 0 : locationBytes.length)); // we do not use checkpointType.ordinal() here to make the serialization robust // against changes in the enum (such as changes in the order of the values) final int typeInt; if (checkpointType == CheckpointType.CHECKPOINT) { typeInt = CHECKPOINT_TYPE_CHECKPOINT; } else if (checkpointType == CheckpointType.SAVEPOINT) { typeInt = CHECKPOINT_TYPE_SAVEPOINT; } else { throw new IOException("Unknown checkpoint type: " + checkpointType); } buf.putInt(CHECKPOINT_BARRIER_EVENT); buf.putLong(barrier.getId()); buf.putLong(barrier.getTimestamp()); buf.putInt(typeInt); if (locationBytes == null) { buf.putInt(-1); } else { buf.putInt(locationBytes.length); buf.put(locationBytes); } buf.flip(); return buf; }
checkpointMetaData.getCheckpointId(), checkpointOptions.getCheckpointType(), getName());
private static BufferOrEvent createBarrier(long checkpointId, int channel) { return new BufferOrEvent(new CheckpointBarrier( checkpointId, System.currentTimeMillis(), CheckpointOptions.forCheckpointWithDefaultLocation()), channel); }