org.apache.flink.runtime.state.FunctionSnapshotContext Java Examples

The following examples show how to use org.apache.flink.runtime.state.FunctionSnapshotContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RescalingITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext context) throws Exception {

	counterPartitions.clear();

	checkCorrectSnapshot[getRuntimeContext().getIndexOfThisSubtask()] = counter;

	int div = counter / NUM_PARTITIONS;
	int mod = counter % NUM_PARTITIONS;

	for (int i = 0; i < NUM_PARTITIONS; ++i) {
		int partitionValue = div;
		if (mod > 0) {
			--mod;
			++partitionValue;
		}
		counterPartitions.add(partitionValue);
	}
}
 
Example #2
Source File: MessageAcknowledgingSourceBase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext context) throws Exception {
	Preconditions.checkState(this.checkpointedState != null,
		"The " + getClass().getSimpleName() + " has not been properly initialized.");

	if (LOG.isDebugEnabled()) {
		LOG.debug("Checkpointing: Messages: {}, checkpoint id: {}, timestamp: {}",
			idsForCurrentCheckpoint, context.getCheckpointId(), context.getCheckpointTimestamp());
	}

	pendingCheckpoints.addLast(new Tuple2<>(context.getCheckpointId(), idsForCurrentCheckpoint));
	idsForCurrentCheckpoint = new HashSet<>(64);

	this.checkpointedState.clear();
	this.checkpointedState.add(SerializedCheckpointData.fromDeque(pendingCheckpoints, idSerializer));
}
 
Example #3
Source File: FlinkKafkaProducer011.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext context) throws Exception {
	super.snapshotState(context);

	nextTransactionalIdHintState.clear();
	// To avoid duplication only first subtask keeps track of next transactional id hint. Otherwise all of the
	// subtasks would write exactly same information.
	if (getRuntimeContext().getIndexOfThisSubtask() == 0 && semantic == Semantic.EXACTLY_ONCE) {
		checkState(nextTransactionalIdHint != null, "nextTransactionalIdHint must be set for EXACTLY_ONCE");
		long nextFreeTransactionalId = nextTransactionalIdHint.nextFreeTransactionalId;

		// If we scaled up, some (unknown) subtask must have created new transactional ids from scratch. In that
		// case we adjust nextFreeTransactionalId by the range of transactionalIds that could be used for this
		// scaling up.
		if (getRuntimeContext().getNumberOfParallelSubtasks() > nextTransactionalIdHint.lastParallelism) {
			nextFreeTransactionalId += getRuntimeContext().getNumberOfParallelSubtasks() * kafkaProducersPoolSize;
		}

		nextTransactionalIdHintState.add(new NextTransactionalIdHint(
			getRuntimeContext().getNumberOfParallelSubtasks(),
			nextFreeTransactionalId));
	}
}
 
Example #4
Source File: CollectSinkFunction.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext context) throws Exception {
	bufferLock.lock();
	try {
		bufferState.clear();
		bufferState.addAll(buffer);

		offsetState.clear();
		offsetState.add(offset);

		uncompletedCheckpointMap.put(context.getCheckpointId(), offset);

		if (LOG.isDebugEnabled()) {
			LOG.debug("Checkpoint begin with checkpointId = " + context.getCheckpointId() +
				", lastCheckpointedOffset = " + lastCheckpointedOffset +
				", buffered results bytes = " + currentBufferBytes);
		}
	} finally {
		bufferLock.unlock();
	}
}
 
Example #5
Source File: ArtificalOperatorStateMapper.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext context) throws Exception {
	final int numSubtasks = getRuntimeContext().getNumberOfParallelSubtasks();
	final int thisSubtaskIndex = getRuntimeContext().getIndexOfThisSubtask();

	// store total number of subtasks as broadcast state
	lastNumSubtasksBroadcastState.clear();
	lastNumSubtasksBroadcastState.put(LAST_NUM_SUBTASKS_STATE_KEY, numSubtasks);

	// populate broadcast state (identical across all subtasks)
	broadcastElementsState.clear();
	for (int i = 0; i < numSubtasks; i++) {
		broadcastElementsState.put(i, getBroadcastStateEntryValue(i));
	}

	// each subtask only stores its own subtask index as a subset of the union set
	unionElementsState.clear();
	unionElementsState.add(thisSubtaskIndex);
}
 
Example #6
Source File: ZooKeeperHighAvailabilityITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext context) throws Exception {
	if (stateRecorded.get()) {
		minimalCheckpointIdIncludingData.compareAndSet(Long.MAX_VALUE,
			context.getCheckpointId());
	}
	if (checkpointCompletedIncludingData.get()) {
		// there is a checkpoint completed with state data, we can trigger the failure now
		waitForCheckpointLatch.trigger();
		failInCheckpointLatch.await();
		if (!failedAlready.getAndSet(true)) {
			throw new RuntimeException("Failing on purpose.");
		} else {
			// make sure there would be no more successful checkpoint before job failing
			// otherwise there might be an unexpected successful checkpoint even
			// CheckpointFailureManager has decided to fail the job
			blockSnapshotLatch.await();
		}
	}
}
 
Example #7
Source File: ArtificalOperatorStateMapper.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext context) throws Exception {
	final int numSubtasks = getRuntimeContext().getNumberOfParallelSubtasks();
	final int thisSubtaskIndex = getRuntimeContext().getIndexOfThisSubtask();

	// store total number of subtasks as broadcast state
	lastNumSubtasksBroadcastState.clear();
	lastNumSubtasksBroadcastState.put(LAST_NUM_SUBTASKS_STATE_KEY, numSubtasks);

	// populate broadcast state (identical across all subtasks)
	broadcastElementsState.clear();
	for (int i = 0; i < numSubtasks; i++) {
		broadcastElementsState.put(i, getBroadcastStateEntryValue(i));
	}

	// each subtask only stores its own subtask index as a subset of the union set
	unionElementsState.clear();
	unionElementsState.add(thisSubtaskIndex);
}
 
Example #8
Source File: FlinkPulsarProducer.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext context) throws Exception {
    // check for asynchronous errors and fail the checkpoint if necessary
    checkErroneous();

    if (flushOnCheckpoint) {
        // wait until all the messages are acknowledged
        synchronized (pendingRecordsLock) {
            while (pendingRecords > 0) {
                pendingRecordsLock.wait(100);
            }
        }

        // if the flushed requests has errors, we should propagate it also and fail the checkpoint
        checkErroneous();
    }
}
 
Example #9
Source File: TwoPhaseCommitSinkFunction.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext context) throws Exception {
	// this is like the pre-commit of a 2-phase-commit transaction
	// we are ready to commit and remember the transaction

	checkState(currentTransactionHolder != null, "bug: no transaction object when performing state snapshot");

	long checkpointId = context.getCheckpointId();
	LOG.debug("{} - checkpoint {} triggered, flushing transaction '{}'", name(), context.getCheckpointId(), currentTransactionHolder);

	preCommit(currentTransactionHolder.handle);
	pendingCommitTransactions.put(checkpointId, currentTransactionHolder);
	LOG.debug("{} - stored pending transactions {}", name(), pendingCommitTransactions);

	currentTransactionHolder = beginTransactionInternal();
	LOG.debug("{} - started new transaction '{}'", name(), currentTransactionHolder);

	state.clear();
	state.add(new State<>(
		this.currentTransactionHolder,
		new ArrayList<>(pendingCommitTransactions.values()),
		userContext));
}
 
Example #10
Source File: MessageAcknowledgingSourceBase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext context) throws Exception {
	Preconditions.checkState(this.checkpointedState != null,
		"The " + getClass().getSimpleName() + " has not been properly initialized.");

	if (LOG.isDebugEnabled()) {
		LOG.debug("{} checkpointing: Messages: {}, checkpoint id: {}, timestamp: {}",
			idsForCurrentCheckpoint, context.getCheckpointId(), context.getCheckpointTimestamp());
	}

	pendingCheckpoints.addLast(new Tuple2<>(context.getCheckpointId(), idsForCurrentCheckpoint));
	idsForCurrentCheckpoint = new HashSet<>(64);

	this.checkpointedState.clear();
	this.checkpointedState.add(SerializedCheckpointData.fromDeque(pendingCheckpoints, idSerializer));
}
 
Example #11
Source File: FlinkKafkaProducerBase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext ctx) throws Exception {
	// check for asynchronous errors and fail the checkpoint if necessary
	checkErroneous();

	if (flushOnCheckpoint) {
		// flushing is activated: We need to wait until pendingRecords is 0
		flush();
		synchronized (pendingRecordsLock) {
			if (pendingRecords != 0) {
				throw new IllegalStateException("Pending record count must be zero at this point: " + pendingRecords);
			}

			// if the flushed requests has errors, we should propagate it also and fail the checkpoint
			checkErroneous();
		}
	}
}
 
Example #12
Source File: TwoPhaseCommitSinkFunction.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext context) throws Exception {
	// this is like the pre-commit of a 2-phase-commit transaction
	// we are ready to commit and remember the transaction

	checkState(currentTransactionHolder != null, "bug: no transaction object when performing state snapshot");

	long checkpointId = context.getCheckpointId();
	LOG.debug("{} - checkpoint {} triggered, flushing transaction '{}'", name(), context.getCheckpointId(), currentTransactionHolder);

	preCommit(currentTransactionHolder.handle);
	pendingCommitTransactions.put(checkpointId, currentTransactionHolder);
	LOG.debug("{} - stored pending transactions {}", name(), pendingCommitTransactions);

	currentTransactionHolder = beginTransactionInternal();
	LOG.debug("{} - started new transaction '{}'", name(), currentTransactionHolder);

	state.clear();
	state.add(new State<>(
		this.currentTransactionHolder,
		new ArrayList<>(pendingCommitTransactions.values()),
		userContext));
}
 
Example #13
Source File: FlinkKafkaProducer011.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext context) throws Exception {
	super.snapshotState(context);

	nextTransactionalIdHintState.clear();
	// To avoid duplication only first subtask keeps track of next transactional id hint. Otherwise all of the
	// subtasks would write exactly same information.
	if (getRuntimeContext().getIndexOfThisSubtask() == 0 && semantic == Semantic.EXACTLY_ONCE) {
		checkState(nextTransactionalIdHint != null, "nextTransactionalIdHint must be set for EXACTLY_ONCE");
		long nextFreeTransactionalId = nextTransactionalIdHint.nextFreeTransactionalId;

		// If we scaled up, some (unknown) subtask must have created new transactional ids from scratch. In that
		// case we adjust nextFreeTransactionalId by the range of transactionalIds that could be used for this
		// scaling up.
		if (getRuntimeContext().getNumberOfParallelSubtasks() > nextTransactionalIdHint.lastParallelism) {
			nextFreeTransactionalId += getRuntimeContext().getNumberOfParallelSubtasks() * kafkaProducersPoolSize;
		}

		nextTransactionalIdHintState.add(new NextTransactionalIdHint(
			getRuntimeContext().getNumberOfParallelSubtasks(),
			nextFreeTransactionalId));
	}
}
 
Example #14
Source File: FlinkKafkaProducer.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext context) throws Exception {
	super.snapshotState(context);

	nextTransactionalIdHintState.clear();
	// To avoid duplication only first subtask keeps track of next transactional id hint. Otherwise all of the
	// subtasks would write exactly same information.
	if (getRuntimeContext().getIndexOfThisSubtask() == 0 && semantic == FlinkKafkaProducer.Semantic.EXACTLY_ONCE) {
		checkState(nextTransactionalIdHint != null, "nextTransactionalIdHint must be set for EXACTLY_ONCE");
		long nextFreeTransactionalId = nextTransactionalIdHint.nextFreeTransactionalId;

		// If we scaled up, some (unknown) subtask must have created new transactional ids from scratch. In that
		// case we adjust nextFreeTransactionalId by the range of transactionalIds that could be used for this
		// scaling up.
		if (getRuntimeContext().getNumberOfParallelSubtasks() > nextTransactionalIdHint.lastParallelism) {
			nextFreeTransactionalId += getRuntimeContext().getNumberOfParallelSubtasks() * kafkaProducersPoolSize;
		}

		nextTransactionalIdHintState.add(new FlinkKafkaProducer.NextTransactionalIdHint(
			getRuntimeContext().getNumberOfParallelSubtasks(),
			nextFreeTransactionalId));
	}
}
 
Example #15
Source File: MigrationTestUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext context) throws Exception {
	unionListState.clear();
	unionListState.add(CHECKPOINTED_STRING);
	unionListState.add(CHECKPOINTED_STRING_1);
	unionListState.add(CHECKPOINTED_STRING_2);
	unionListState.add(CHECKPOINTED_STRING_3);
}
 
Example #16
Source File: UpdatableTopNFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext context) throws Exception {
	Iterator<Map.Entry<BaseRow, Map<BaseRow, RankRow>>> iter = kvRowKeyMap.entrySet().iterator();
	while (iter.hasNext()) {
		Map.Entry<BaseRow, Map<BaseRow, RankRow>> entry = iter.next();
		BaseRow partitionKey = entry.getKey();
		Map<BaseRow, RankRow> currentRowKeyMap = entry.getValue();
		keyContext.setCurrentKey(partitionKey);
		flushBufferToState(currentRowKeyMap);
	}
}
 
Example #17
Source File: PvStatLocalKeyByExactlyOnce.java    From flink-learning with Apache License 2.0 5 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext functionSnapshotContext) throws Exception {
    // 将 buffer 中的数据保存到状态中,来保证 Exactly Once
    localPvStatListState.clear();
    for (Map.Entry<String, Long> appIdPv : localPvStat.entrySet()) {
        localPvStatListState.add(Tuple2.of(appIdPv.getKey(), appIdPv.getValue()));
        log.info("snapshot   subtask: {}    appId: {}   pv: {}", subtaskIndex, appIdPv.getKey(), appIdPv.getValue());
    }
}
 
Example #18
Source File: BucketingSink.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext context) throws Exception {
	Preconditions.checkNotNull(restoredBucketStates, "The operator has not been properly initialized.");

	restoredBucketStates.clear();

	synchronized (state.bucketStates) {
		int subtaskIdx = getRuntimeContext().getIndexOfThisSubtask();

		for (Map.Entry<String, BucketState<T>> bucketStateEntry : state.bucketStates.entrySet()) {
			BucketState<T> bucketState = bucketStateEntry.getValue();

			if (bucketState.isWriterOpen) {
				bucketState.currentFileValidLength = bucketState.writer.flush();
			}

			synchronized (bucketState.pendingFilesPerCheckpoint) {
				bucketState.pendingFilesPerCheckpoint.put(context.getCheckpointId(), bucketState.pendingFiles);
			}
			bucketState.pendingFiles = new ArrayList<>();
		}
		restoredBucketStates.add(state);

		if (LOG.isDebugEnabled()) {
			LOG.debug("{} idx {} checkpointed {}.", getClass().getSimpleName(), subtaskIdx, state);
		}
	}
}
 
Example #19
Source File: FlinkSink.java    From sylph with Apache License 2.0 5 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext context)
        throws Exception
{
    realTimeSink.flush();
    //unionState.add(thisState);
}
 
Example #20
Source File: RocketMQSource.java    From flink-learning with Apache License 2.0 5 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext context) throws Exception {
    // called when a snapshot for a checkpoint is requested

    if (!runningChecker.isRunning()) {
        LOG.debug("snapshotState() called on closed source; returning null.");
        return;
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("Snapshotting state {} ...", context.getCheckpointId());
    }

    unionOffsetStates.clear();

    if (LOG.isDebugEnabled()) {
        LOG.debug("Snapshotted state, last processed offsets: {}, checkpoint id: {}, timestamp: {}",
                offsetTable, context.getCheckpointId(), context.getCheckpointTimestamp());
    }

    // remove the unassigned queues in order to avoid read the wrong offset when the source restart
    Set<MessageQueue> assignedQueues = consumer.fetchMessageQueuesInBalance(topic);
    offsetTable.entrySet().removeIf(item -> !assignedQueues.contains(item.getKey()));

    for (Map.Entry<MessageQueue, Long> entry : offsetTable.entrySet()) {
        unionOffsetStates.add(Tuple2.of(entry.getKey(), entry.getValue()));
    }
}
 
Example #21
Source File: KuduSink.java    From flink-learning with Apache License 2.0 5 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext context) throws Exception {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Snapshotting state {} ...", context.getCheckpointId());
    }
    this.connector.flush();
}
 
Example #22
Source File: StreamingFileSink.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext context) throws Exception {
	Preconditions.checkState(bucketStates != null && maxPartCountersState != null, "sink has not been initialized");

	buckets.snapshotState(
			context.getCheckpointId(),
			bucketStates,
			maxPartCountersState);
}
 
Example #23
Source File: PubSubSink.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext context) throws Exception {
	//before checkpoints make sure all the batched / buffered pubsub messages have actually been sent
	publisher.publishAllOutstanding();

	// At this point, no new messages will be published because this thread has successfully acquired
	// the checkpoint lock. So we just wait for all the pending futures to complete.
	waitForFuturesToComplete();
	if (exceptionAtomicReference.get() != null) {
		throw exceptionAtomicReference.get();
	}
}
 
Example #24
Source File: FlinkKinesisProducer.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext context) throws Exception {
	// check for asynchronous errors and fail the checkpoint if necessary
	checkAndPropagateAsyncError();

	flushSync();
	if (producer.getOutstandingRecordsCount() > 0) {
		throw new IllegalStateException(
			"Number of outstanding records must be zero at this point: " + producer.getOutstandingRecordsCount());
	}

	// if the flushed requests has errors, we should propagate it also and fail the checkpoint
	checkAndPropagateAsyncError();
}
 
Example #25
Source File: FlinkPulsarSinkTest.java    From pulsar-flink with Apache License 2.0 5 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext context) throws Exception {
    isFlushed = false;
    super.snapshotState(context);
    // if the snapshot implementation doesn't wait until
    // all pending records are flushed, we should fail the test
    if (flushOnCheckpoint && !isFlushed) {
        throw new RuntimeException("Flushing is enabled; snapshots should be blocked" +
                " until all pending records are flushed");
    }
}
 
Example #26
Source File: FromElementsFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext context) throws Exception {
	Preconditions.checkState(this.checkpointedState != null,
		"The " + getClass().getSimpleName() + " has not been properly initialized.");

	this.checkpointedState.clear();
	this.checkpointedState.add(this.numElementsEmitted);
}
 
Example #27
Source File: ContinuousFileMonitoringFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext context) throws Exception {
	Preconditions.checkState(this.checkpointedState != null,
		"The " + getClass().getSimpleName() + " state has not been properly initialized.");

	this.checkpointedState.clear();
	this.checkpointedState.add(this.globalModificationTime);

	if (LOG.isDebugEnabled()) {
		LOG.debug("{} checkpointed {}.", getClass().getSimpleName(), globalModificationTime);
	}
}
 
Example #28
Source File: FromElementsFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext context) throws Exception {
	Preconditions.checkState(this.checkpointedState != null,
		"The " + getClass().getSimpleName() + " has not been properly initialized.");

	this.checkpointedState.clear();
	this.checkpointedState.add(this.numElementsEmitted);
}
 
Example #29
Source File: FromElementsFunction.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext context) throws Exception {
	Preconditions.checkState(this.checkpointedState != null,
		"The " + getClass().getSimpleName() + " has not been properly initialized.");

	this.checkpointedState.clear();
	this.checkpointedState.add(this.numElementsEmitted);
}
 
Example #30
Source File: ParallelFromStreamRecordsFunction.java    From flink-spector with Apache License 2.0 5 votes vote down vote up
@Override
public void snapshotState(FunctionSnapshotContext context) throws Exception {
    Preconditions.checkState(this.checkpointedState != null,
            "The " + getClass().getSimpleName() + " has not been properly initialized.");

    this.checkpointedState.clear();
    this.checkpointedState.add(this.numElementsEmitted);
}