Java Code Examples for org.apache.flink.runtime.state.StateInitializationContext#isRestored()

The following examples show how to use org.apache.flink.runtime.state.StateInitializationContext#isRestored() . 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: StatefulOperatorChainedTaskTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public void initializeState(StateInitializationContext context) throws Exception {
	super.initializeState(context);

	counterState = context
		.getKeyedStateStore()
		.getState(new ValueStateDescriptor<>(prefix + "counter-state", LongSerializer.INSTANCE));

	// set key manually to make RocksDBListState get the serialized key.
	setCurrentKey("10");

	if (context.isRestored()) {
		counter =  counterState.value();
		assertEquals(snapshotOutData, counter);
		counterState.clear();
	}
}
 
Example 2
Source File: StatefulOperatorChainedTaskTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void initializeState(StateInitializationContext context) throws Exception {
	super.initializeState(context);

	counterState = context
		.getKeyedStateStore()
		.getState(new ValueStateDescriptor<>(prefix + "counter-state", LongSerializer.INSTANCE));

	// set key manually to make RocksDBListState get the serialized key.
	setCurrentKey("10");

	if (context.isRestored()) {
		counter =  counterState.value();
		assertEquals(snapshotOutData, counter);
		counterState.clear();
	}
}
 
Example 3
Source File: GenericWriteAheadSink.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void initializeState(StateInitializationContext context) throws Exception {
	super.initializeState(context);

	Preconditions.checkState(this.checkpointedState == null,
		"The reader state has already been initialized.");

	checkpointedState = context.getOperatorStateStore()
		.getSerializableListState("pending-checkpoints");

	int subtaskIdx = getRuntimeContext().getIndexOfThisSubtask();
	if (context.isRestored()) {
		LOG.info("Restoring state for the GenericWriteAheadSink (taskIdx={}).", subtaskIdx);

		for (PendingCheckpoint pendingCheckpoint : checkpointedState.get()) {
			this.pendingCheckpoints.add(pendingCheckpoint);
		}

		if (LOG.isDebugEnabled()) {
			LOG.debug("GenericWriteAheadSink idx {} restored {}.", subtaskIdx, this.pendingCheckpoints);
		}
	} else {
		LOG.info("No state to restore for the GenericWriteAheadSink (taskIdx={}).", subtaskIdx);
	}
}
 
Example 4
Source File: StatefulOperatorChainedTaskTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void initializeState(StateInitializationContext context) throws Exception {
	super.initializeState(context);

	counterState = context
		.getKeyedStateStore()
		.getState(new ValueStateDescriptor<>(prefix + "counter-state", LongSerializer.INSTANCE));

	// set key manually to make RocksDBListState get the serialized key.
	setCurrentKey("10");

	if (context.isRestored()) {
		counter =  counterState.value();
		assertEquals(snapshotOutData, counter);
		counterState.clear();
	}
}
 
Example 5
Source File: GenericWriteAheadSink.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public void initializeState(StateInitializationContext context) throws Exception {
	super.initializeState(context);

	Preconditions.checkState(this.checkpointedState == null,
		"The reader state has already been initialized.");

	checkpointedState = context.getOperatorStateStore()
		.getSerializableListState("pending-checkpoints");

	int subtaskIdx = getRuntimeContext().getIndexOfThisSubtask();
	if (context.isRestored()) {
		LOG.info("Restoring state for the GenericWriteAheadSink (taskIdx={}).", subtaskIdx);

		for (PendingCheckpoint pendingCheckpoint : checkpointedState.get()) {
			this.pendingCheckpoints.add(pendingCheckpoint);
		}

		if (LOG.isDebugEnabled()) {
			LOG.debug("GenericWriteAheadSink idx {} restored {}.", subtaskIdx, this.pendingCheckpoints);
		}
	} else {
		LOG.info("No state to restore for the GenericWriteAheadSink (taskIdx={}).", subtaskIdx);
	}
}
 
Example 6
Source File: RestoreStreamTaskTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void initializeState(StateInitializationContext context) throws Exception {
	super.initializeState(context);

	counterState = context
		.getOperatorStateStore()
		.getListState(new ListStateDescriptor<>("counter-state", LongSerializer.INSTANCE));

	if (context.isRestored()) {
		for (Long value : counterState.get()) {
			counter += value;
		}
		counterState.clear();
	}
}
 
Example 7
Source File: ContinuousFileReaderOperator.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void initializeState(StateInitializationContext context) throws Exception {
	super.initializeState(context);

	checkState(checkpointedState == null,	"The reader state has already been initialized.");

	checkpointedState = context.getOperatorStateStore().getSerializableListState("splits");

	int subtaskIdx = getRuntimeContext().getIndexOfThisSubtask();
	if (context.isRestored()) {
		LOG.info("Restoring state for the {} (taskIdx={}).", getClass().getSimpleName(), subtaskIdx);

		// this may not be null in case we migrate from a previous Flink version.
		if (restoredReaderState == null) {
			restoredReaderState = new ArrayList<>();
			for (TimestampedFileInputSplit split : checkpointedState.get()) {
				restoredReaderState.add(split);
			}

			if (LOG.isDebugEnabled()) {
				LOG.debug("{} (taskIdx={}) restored {}.", getClass().getSimpleName(), subtaskIdx, restoredReaderState);
			}
		}
	} else {
		LOG.info("No state to restore for the {} (taskIdx={}).", getClass().getSimpleName(), subtaskIdx);
	}
}
 
Example 8
Source File: ContinuousFileReaderOperator.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void initializeState(StateInitializationContext context) throws Exception {
	super.initializeState(context);

	checkState(checkpointedState == null,	"The reader state has already been initialized.");

	checkpointedState = context.getOperatorStateStore().getSerializableListState("splits");

	int subtaskIdx = getRuntimeContext().getIndexOfThisSubtask();
	if (context.isRestored()) {
		LOG.info("Restoring state for the {} (taskIdx={}).", getClass().getSimpleName(), subtaskIdx);

		// this may not be null in case we migrate from a previous Flink version.
		if (restoredReaderState == null) {
			restoredReaderState = new ArrayList<>();
			for (TimestampedFileInputSplit split : checkpointedState.get()) {
				restoredReaderState.add(split);
			}

			if (LOG.isDebugEnabled()) {
				LOG.debug("{} (taskIdx={}) restored {}.", getClass().getSimpleName(), subtaskIdx, restoredReaderState);
			}
		}
	} else {
		LOG.info("No state to restore for the {} (taskIdx={}).", getClass().getSimpleName(), subtaskIdx);
	}
}
 
Example 9
Source File: StreamingFunctionUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
private static boolean tryRestoreFunction(
		StateInitializationContext context,
		Function userFunction) throws Exception {

	if (userFunction instanceof CheckpointedFunction) {
		((CheckpointedFunction) userFunction).initializeState(context);

		return true;
	}

	if (context.isRestored() && userFunction instanceof ListCheckpointed) {
		@SuppressWarnings("unchecked")
		ListCheckpointed<Serializable> listCheckpointedFun = (ListCheckpointed<Serializable>) userFunction;

		ListState<Serializable> listState = context.getOperatorStateStore().
				getSerializableListState(DefaultOperatorStateBackend.DEFAULT_OPERATOR_STATE_NAME);

		List<Serializable> list = new ArrayList<>();

		for (Serializable serializable : listState.get()) {
			list.add(serializable);
		}

		try {
			listCheckpointedFun.restoreState(list);
		} catch (Exception e) {

			throw new Exception("Failed to restore state to function: " + e.getMessage(), e);
		}

		return true;
	}

	return false;
}
 
Example 10
Source File: RestoreStreamTaskTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void initializeState(StateInitializationContext context) throws Exception {
	super.initializeState(context);

	counterState = context
		.getOperatorStateStore()
		.getListState(new ListStateDescriptor<>("counter-state", LongSerializer.INSTANCE));

	if (context.isRestored()) {
		for (Long value : counterState.get()) {
			counter += value;
		}
		counterState.clear();
	}
}
 
Example 11
Source File: AbstractSiddhiOperator.java    From flink-siddhi with Apache License 2.0 5 votes vote down vote up
@Override
public void initializeState(StateInitializationContext context) throws Exception {
    super.initializeState(context);
    if (siddhiRuntimeState == null) {
        siddhiRuntimeState = context.getOperatorStateStore().getUnionListState(new ListStateDescriptor<>(SIDDHI_RUNTIME_STATE_NAME,
                new BytePrimitiveArraySerializer()));
    }
    if (queuedRecordsState == null) {
        queuedRecordsState = context.getOperatorStateStore().getListState(
            new ListStateDescriptor<>(QUEUED_RECORDS_STATE_NAME, new BytePrimitiveArraySerializer()));
    }
    if (context.isRestored()) {
        restoreState();
    }
}
 
Example 12
Source File: AbstractSiddhiOperator.java    From bahir-flink with Apache License 2.0 5 votes vote down vote up
@Override
public void initializeState(StateInitializationContext context) throws Exception {
    super.initializeState(context);
    if (siddhiRuntimeState == null) {
        siddhiRuntimeState = context.getOperatorStateStore().getUnionListState(new ListStateDescriptor<>(SIDDHI_RUNTIME_STATE_NAME,
                new BytePrimitiveArraySerializer()));
    }
    if (queuedRecordsState == null) {
        queuedRecordsState = context.getOperatorStateStore().getListState(
            new ListStateDescriptor<>(QUEUED_RECORDS_STATE_NAME, new BytePrimitiveArraySerializer()));
    }
    if (context.isRestored()) {
        restoreState();
    }
}
 
Example 13
Source File: StreamingFunctionUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
private static boolean tryRestoreFunction(
		StateInitializationContext context,
		Function userFunction) throws Exception {

	if (userFunction instanceof CheckpointedFunction) {
		((CheckpointedFunction) userFunction).initializeState(context);

		return true;
	}

	if (context.isRestored() && userFunction instanceof ListCheckpointed) {
		@SuppressWarnings("unchecked")
		ListCheckpointed<Serializable> listCheckpointedFun = (ListCheckpointed<Serializable>) userFunction;

		// We are using JavaSerializer from the flink-runtime module here. This is very naughty and
		// we shouldn't be doing it because ideally nothing in the API modules/connector depends
		// directly on flink-runtime. We are doing it here because we need to maintain backwards
		// compatibility with old state and because we will have to rework/remove this code soon.
		ListStateDescriptor<Serializable> listStateDescriptor =
			new ListStateDescriptor<>(DefaultOperatorStateBackend.DEFAULT_OPERATOR_STATE_NAME, new JavaSerializer<>());
		ListState<Serializable> listState = context.getOperatorStateStore().getListState(listStateDescriptor);

		List<Serializable> list = new ArrayList<>();

		for (Serializable serializable : listState.get()) {
			list.add(serializable);
		}

		try {
			listCheckpointedFun.restoreState(list);
		} catch (Exception e) {

			throw new Exception("Failed to restore state to function: " + e.getMessage(), e);
		}

		return true;
	}

	return false;
}
 
Example 14
Source File: ContinuousFileReaderOperator.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void initializeState(StateInitializationContext context) throws Exception {
	super.initializeState(context);

	checkState(checkpointedState == null, "The reader state has already been initialized.");

	// We are using JavaSerializer from the flink-runtime module here. This is very naughty and
	// we shouldn't be doing it because ideally nothing in the API modules/connector depends
	// directly on flink-runtime. We are doing it here because we need to maintain backwards
	// compatibility with old state and because we will have to rework/remove this code soon.
	checkpointedState = context.getOperatorStateStore().getListState(new ListStateDescriptor<>("splits", new JavaSerializer<>()));

	int subtaskIdx = getRuntimeContext().getIndexOfThisSubtask();
	if (!context.isRestored()) {
		LOG.info("No state to restore for the {} (taskIdx={}).", getClass().getSimpleName(), subtaskIdx);
		return;
	}

	LOG.info("Restoring state for the {} (taskIdx={}).", getClass().getSimpleName(), subtaskIdx);

	splits = splits == null ? new PriorityQueue<>() : splits;
	for (T split : checkpointedState.get()) {
		splits.add(split);
	}

	if (LOG.isDebugEnabled()) {
		LOG.debug("{} (taskIdx={}) restored {}.", getClass().getSimpleName(), subtaskIdx, splits);
	}
}
 
Example 15
Source File: GenericWriteAheadSink.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void initializeState(StateInitializationContext context) throws Exception {
	super.initializeState(context);

	Preconditions.checkState(this.checkpointedState == null,
		"The reader state has already been initialized.");

	// We are using JavaSerializer from the flink-runtime module here. This is very naughty and
	// we shouldn't be doing it because ideally nothing in the API modules/connector depends
	// directly on flink-runtime. We are doing it here because we need to maintain backwards
	// compatibility with old state and because we will have to rework/remove this code soon.
	checkpointedState = context
						.getOperatorStateStore()
						.getListState(new ListStateDescriptor<>("pending-checkpoints", new JavaSerializer<>()));

	int subtaskIdx = getRuntimeContext().getIndexOfThisSubtask();
	if (context.isRestored()) {
		LOG.info("Restoring state for the GenericWriteAheadSink (taskIdx={}).", subtaskIdx);

		for (PendingCheckpoint pendingCheckpoint : checkpointedState.get()) {
			this.pendingCheckpoints.add(pendingCheckpoint);
		}

		if (LOG.isDebugEnabled()) {
			LOG.debug("GenericWriteAheadSink idx {} restored {}.", subtaskIdx, this.pendingCheckpoints);
		}
	} else {
		LOG.info("No state to restore for the GenericWriteAheadSink (taskIdx={}).", subtaskIdx);
	}
}
 
Example 16
Source File: RestoreStreamTaskTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void initializeState(StateInitializationContext context) throws Exception {
	if (context.isRestored()) {
		RESTORED_OPERATORS.add(getOperatorID());
	}
}
 
Example 17
Source File: RestoreStreamTaskTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public void initializeState(StateInitializationContext context) throws Exception {
	if (context.isRestored()) {
		RESTORED_OPERATORS.add(getOperatorID());
	}
}
 
Example 18
Source File: StatefulOperatorChainedTaskTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void initializeState(StateInitializationContext context) throws Exception {
	if (context.isRestored()) {
		RESTORED_OPERATORS.add(getOperatorID());
	}
}
 
Example 19
Source File: RestoreStreamTaskTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void initializeState(StateInitializationContext context) throws Exception {
	if (context.isRestored()) {
		RESTORED_OPERATORS.add(getOperatorID());
	}
}
 
Example 20
Source File: StatefulOperatorChainedTaskTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public void initializeState(StateInitializationContext context) throws Exception {
	if (context.isRestored()) {
		RESTORED_OPERATORS.add(getOperatorID());
	}
}