Java Code Examples for org.apache.flink.api.common.state.ValueState#clear()

The following examples show how to use org.apache.flink.api.common.state.ValueState#clear() . 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: StateBackendTestBase.java    From flink with Apache License 2.0 7 votes vote down vote up
/**
 * Verify that an empty {@code ValueState} will yield the default value.
 */
@Test
public void testValueStateDefaultValue() throws Exception {
	AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);

	ValueStateDescriptor<String> kvId = new ValueStateDescriptor<>("id", String.class, "Hello");

	ValueState<String> state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);

	backend.setCurrentKey(1);
	assertEquals("Hello", state.value());

	state.update("Ciao");
	assertEquals("Ciao", state.value());

	state.clear();
	assertEquals("Hello", state.value());

	backend.dispose();
}
 
Example 2
Source File: StateBackendTestBase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Verify that {@link ValueStateDescriptor} allows {@code null} as default.
 */
@Test
public void testValueStateNullAsDefaultValue() throws Exception {
	AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);

	ValueStateDescriptor<String> kvId = new ValueStateDescriptor<>("id", String.class, null);

	ValueState<String> state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);

	backend.setCurrentKey(1);
	assertNull(state.value());

	state.update("Ciao");
	assertEquals("Ciao", state.value());

	state.clear();
	assertNull(state.value());

	backend.dispose();
}
 
Example 3
Source File: KeyedProcessOperatorTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void processElement(Integer value, Context ctx, Collector<String> out) throws Exception {
	final TimerService timerService = ctx.timerService();
	final ValueState<Integer> state = getRuntimeContext().getState(this.state);
	if (state.value() == null) {
		out.collect("INPUT:" + value);
		state.update(value);
		if (expectedTimeDomain.equals(TimeDomain.EVENT_TIME)) {
			timerService.registerEventTimeTimer(timerService.currentWatermark() + 5);
		} else {
			timerService.registerProcessingTimeTimer(timerService.currentProcessingTime() + 5);
		}
	} else {
		state.clear();
		if (expectedTimeDomain.equals(TimeDomain.EVENT_TIME)) {
			timerService.deleteEventTimeTimer(timerService.currentWatermark() + 4);
		} else {
			timerService.deleteProcessingTimeTimer(timerService.currentProcessingTime() + 4);
		}
	}
}
 
Example 4
Source File: ProcessingTimeoutTrigger.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public TriggerResult onElement(T element, long timestamp, W window, TriggerContext ctx)
		throws Exception {
	TriggerResult triggerResult = this.nestedTrigger.onElement(element, timestamp, window, ctx);
	if (triggerResult.isFire()) {
		this.clear(window, ctx);
		return triggerResult;
	}

	ValueState<Long> timeoutState = ctx.getPartitionedState(this.timeoutStateDesc);
	long nextFireTimestamp = ctx.getCurrentProcessingTime() + this.interval;
	Long timeoutTimestamp = timeoutState.value();
	if (timeoutTimestamp != null && resetTimerOnNewRecord) {
		ctx.deleteProcessingTimeTimer(timeoutTimestamp);
		timeoutState.clear();
		timeoutTimestamp = null;
	}

	if (timeoutTimestamp == null) {
		timeoutState.update(nextFireTimestamp);
		ctx.registerProcessingTimeTimer(nextFireTimestamp);
	}

	return triggerResult;
}
 
Example 5
Source File: KeyedProcessOperatorTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void processElement(Integer value, Context ctx, Collector<String> out) throws Exception {
	final TimerService timerService = ctx.timerService();
	final ValueState<Integer> state = getRuntimeContext().getState(this.state);
	if (state.value() == null) {
		out.collect("INPUT:" + value);
		state.update(value);
		if (expectedTimeDomain.equals(TimeDomain.EVENT_TIME)) {
			timerService.registerEventTimeTimer(timerService.currentWatermark() + 5);
		} else {
			timerService.registerProcessingTimeTimer(timerService.currentProcessingTime() + 5);
		}
	} else {
		state.clear();
		if (expectedTimeDomain.equals(TimeDomain.EVENT_TIME)) {
			timerService.deleteEventTimeTimer(timerService.currentWatermark() + 4);
		} else {
			timerService.deleteProcessingTimeTimer(timerService.currentProcessingTime() + 4);
		}
	}
}
 
Example 6
Source File: StateBackendTestBase.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Verify that an empty {@code ValueState} will yield the default value.
 */
@Test
public void testValueStateDefaultValue() throws Exception {
	AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);

	ValueStateDescriptor<String> kvId = new ValueStateDescriptor<>("id", String.class, "Hello");

	ValueState<String> state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);

	backend.setCurrentKey(1);
	assertEquals("Hello", state.value());

	state.update("Ciao");
	assertEquals("Ciao", state.value());

	state.clear();
	assertEquals("Hello", state.value());

	backend.dispose();
}
 
Example 7
Source File: KeyedCoProcessOperatorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void handleValue(
	Object value,
	Collector<String> out,
	TimerService timerService,
	int channel) throws IOException {
	final ValueState<String> state = getRuntimeContext().getState(this.state);
	if (state.value() == null) {
		out.collect("INPUT" + channel + ":" + value);
		state.update(String.valueOf(value));
		timerService.registerProcessingTimeTimer(timerService.currentProcessingTime() + 5);
	} else {
		state.clear();
		timerService.deleteProcessingTimeTimer(timerService.currentProcessingTime() + 4);
	}
}
 
Example 8
Source File: DedupingOperator.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public void onProcessingTime(InternalTimer<ByteBuffer, VoidNamespace> internalTimer)
    throws Exception {
  ValueState<Long> dedupingState = getPartitionedState(dedupingStateDescriptor);

  Long lastSeenTimestamp = dedupingState.value();
  if (lastSeenTimestamp != null
      && lastSeenTimestamp.equals(internalTimer.getTimestamp() - MAX_RETENTION_SINCE_ACCESS)) {
    dedupingState.clear();
  }
}
 
Example 9
Source File: CustomWindow.java    From examples-java with Apache License 2.0 5 votes vote down vote up
@Override
public void clear(TimeWindow w, TriggerContext ctx) throws Exception {
    // clear trigger state
    ValueState<Boolean> firstSeen = ctx.getPartitionedState(
        new ValueStateDescriptor<>("firstSeen", Types.BOOLEAN));
    firstSeen.clear();
}
 
Example 10
Source File: KeyedCoProcessOperatorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void handleValue(
	Object value,
	Collector<String> out,
	TimerService timerService,
	int channel) throws IOException {
	final ValueState<String> state = getRuntimeContext().getState(this.state);
	if (state.value() == null) {
		out.collect("INPUT" + channel + ":" + value);
		state.update(String.valueOf(value));
		timerService.registerProcessingTimeTimer(timerService.currentProcessingTime() + 5);
	} else {
		state.clear();
		timerService.deleteProcessingTimeTimer(timerService.currentProcessingTime() + 4);
	}
}
 
Example 11
Source File: KeyedCoProcessOperatorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void handleValue(
	Object value,
	Collector<String> out,
	TimerService timerService,
	int channel) throws IOException {
	final ValueState<String> state = getRuntimeContext().getState(this.state);
	if (state.value() == null) {
		out.collect("INPUT" + channel + ":" + value);
		state.update(String.valueOf(value));
		timerService.registerEventTimeTimer(timerService.currentWatermark() + 5);
	} else {
		state.clear();
		timerService.deleteEventTimeTimer(timerService.currentWatermark() + 4);
	}
}
 
Example 12
Source File: LegacyKeyedCoProcessOperatorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void handleValue(
	Object value,
	Collector<String> out,
	TimerService timerService,
	int channel) throws IOException {
	final ValueState<String> state = getRuntimeContext().getState(this.state);
	if (state.value() == null) {
		out.collect("INPUT" + channel + ":" + value);
		state.update(String.valueOf(value));
		timerService.registerProcessingTimeTimer(timerService.currentProcessingTime() + 5);
	} else {
		state.clear();
		timerService.deleteProcessingTimeTimer(timerService.currentProcessingTime() + 4);
	}
}
 
Example 13
Source File: LegacyKeyedCoProcessOperatorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void handleValue(
	Object value,
	Collector<String> out,
	TimerService timerService,
	int channel) throws IOException {
	final ValueState<String> state = getRuntimeContext().getState(this.state);
	if (state.value() == null) {
		out.collect("INPUT" + channel + ":" + value);
		state.update(String.valueOf(value));
		timerService.registerEventTimeTimer(timerService.currentWatermark() + 5);
	} else {
		state.clear();
		timerService.deleteEventTimeTimer(timerService.currentWatermark() + 4);
	}
}
 
Example 14
Source File: KeyedCoProcessOperatorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void handleValue(
	Object value,
	Collector<String> out,
	TimerService timerService,
	int channel) throws IOException {
	final ValueState<String> state = getRuntimeContext().getState(this.state);
	if (state.value() == null) {
		out.collect("INPUT" + channel + ":" + value);
		state.update(String.valueOf(value));
		timerService.registerEventTimeTimer(timerService.currentWatermark() + 5);
	} else {
		state.clear();
		timerService.deleteEventTimeTimer(timerService.currentWatermark() + 4);
	}
}
 
Example 15
Source File: ProcessingTimeoutTrigger.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void clear(W window, TriggerContext ctx) throws Exception {
	ValueState<Long> timeoutTimestampState = ctx.getPartitionedState(this.timeoutStateDesc);
	Long timeoutTimestamp = timeoutTimestampState.value();
	if (timeoutTimestamp != null) {
		ctx.deleteProcessingTimeTimer(timeoutTimestamp);
		timeoutTimestampState.clear();
	}
	this.nestedTrigger.clear(window, ctx);
}
 
Example 16
Source File: KeyedCoProcessOperatorTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void handleValue(
	Object value,
	Collector<String> out,
	TimerService timerService,
	int channel) throws IOException {
	final ValueState<String> state = getRuntimeContext().getState(this.state);
	if (state.value() == null) {
		out.collect("INPUT" + channel + ":" + value);
		state.update(String.valueOf(value));
		timerService.registerProcessingTimeTimer(timerService.currentProcessingTime() + 5);
	} else {
		state.clear();
		timerService.deleteProcessingTimeTimer(timerService.currentProcessingTime() + 4);
	}
}
 
Example 17
Source File: StateBackendTestBase.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * This test verifies that passing {@code null} to {@link ValueState#update(Object)} acts
 * the same as {@link ValueState#clear()}.
 *
 * @throws Exception
 */
@Test
@SuppressWarnings("unchecked")
public void testValueStateNullUpdate() throws Exception {
	// precondition: LongSerializer must fail on null value. this way the test would fail
	// later if null values where actually stored in the state instead of acting as clear()
	try {
		LongSerializer.INSTANCE.serialize(null,
			new DataOutputViewStreamWrapper(new ByteArrayOutputStream()));
		fail("Should fail with NullPointerException");
	} catch (NullPointerException e) {
		// alrighty
	}

	CheckpointStreamFactory streamFactory = createStreamFactory();
	SharedStateRegistry sharedStateRegistry = new SharedStateRegistry();
	AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);

	ValueStateDescriptor<Long> kvId = new ValueStateDescriptor<>("id", LongSerializer.INSTANCE, 42L);

	ValueState<Long> state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);

	// some modifications to the state
	backend.setCurrentKey(1);

	// verify default value
	assertEquals(42L, (long) state.value());
	state.update(1L);
	assertEquals(1L, (long) state.value());

	backend.setCurrentKey(2);
	assertEquals(42L, (long) state.value());

	backend.setCurrentKey(1);
	state.clear();
	assertEquals(42L, (long) state.value());

	state.update(17L);
	assertEquals(17L, (long) state.value());

	state.update(null);
	assertEquals(42L, (long) state.value());

	// draw a snapshot
	KeyedStateHandle snapshot1 = runSnapshot(
		backend.snapshot(682375462378L, 2, streamFactory, CheckpointOptions.forCheckpointWithDefaultLocation()),
		sharedStateRegistry);

	backend.dispose();
	backend = restoreKeyedBackend(IntSerializer.INSTANCE, snapshot1);

	snapshot1.discardState();

	backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);

	backend.dispose();
}
 
Example 18
Source File: StateBackendTestBase.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * This test verifies that passing {@code null} to {@link ValueState#update(Object)} acts
 * the same as {@link ValueState#clear()}.
 *
 * @throws Exception
 */
@Test
@SuppressWarnings("unchecked")
public void testValueStateNullUpdate() throws Exception {
	// precondition: LongSerializer must fail on null value. this way the test would fail
	// later if null values where actually stored in the state instead of acting as clear()
	try {
		LongSerializer.INSTANCE.serialize(null,
			new DataOutputViewStreamWrapper(new ByteArrayOutputStream()));
		fail("Should fail with NullPointerException");
	} catch (NullPointerException e) {
		// alrighty
	}

	CheckpointStreamFactory streamFactory = createStreamFactory();
	SharedStateRegistry sharedStateRegistry = new SharedStateRegistry();
	AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);

	ValueStateDescriptor<Long> kvId = new ValueStateDescriptor<>("id", LongSerializer.INSTANCE, 42L);

	ValueState<Long> state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);

	// some modifications to the state
	backend.setCurrentKey(1);

	// verify default value
	assertEquals(42L, (long) state.value());
	state.update(1L);
	assertEquals(1L, (long) state.value());

	backend.setCurrentKey(2);
	assertEquals(42L, (long) state.value());

	backend.setCurrentKey(1);
	state.clear();
	assertEquals(42L, (long) state.value());

	state.update(17L);
	assertEquals(17L, (long) state.value());

	state.update(null);
	assertEquals(42L, (long) state.value());

	// draw a snapshot
	KeyedStateHandle snapshot1 = runSnapshot(
		backend.snapshot(682375462378L, 2, streamFactory, CheckpointOptions.forCheckpointWithDefaultLocation()),
		sharedStateRegistry);

	backend.dispose();
	backend = restoreKeyedBackend(IntSerializer.INSTANCE, snapshot1);

	snapshot1.discardState();

	backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);

	backend.dispose();
}
 
Example 19
Source File: StateBackendTestBase.java    From flink with Apache License 2.0 3 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testNumStateEntries() throws Exception {
	AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);

	ValueStateDescriptor<String> kvId = new ValueStateDescriptor<>("id", String.class);

	assertEquals(0, backend.numKeyValueStateEntries());

	ValueState<String> state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);

	backend.setCurrentKey(0);
	state.update("hello");
	state.update("ciao");

	assertEquals(1, backend.numKeyValueStateEntries());

	backend.setCurrentKey(42);
	state.update("foo");

	assertEquals(2, backend.numKeyValueStateEntries());

	backend.setCurrentKey(0);
	state.clear();

	assertEquals(1, backend.numKeyValueStateEntries());

	backend.setCurrentKey(42);
	state.clear();

	assertEquals(0, backend.numKeyValueStateEntries());

	backend.dispose();
}
 
Example 20
Source File: StateBackendTestBase.java    From Flink-CEPplus with Apache License 2.0 3 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testNumStateEntries() throws Exception {
	AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);

	ValueStateDescriptor<String> kvId = new ValueStateDescriptor<>("id", String.class);

	assertEquals(0, backend.numKeyValueStateEntries());

	ValueState<String> state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);

	backend.setCurrentKey(0);
	state.update("hello");
	state.update("ciao");

	assertEquals(1, backend.numKeyValueStateEntries());

	backend.setCurrentKey(42);
	state.update("foo");

	assertEquals(2, backend.numKeyValueStateEntries());

	backend.setCurrentKey(0);
	state.clear();

	assertEquals(1, backend.numKeyValueStateEntries());

	backend.setCurrentKey(42);
	state.clear();

	assertEquals(0, backend.numKeyValueStateEntries());

	backend.dispose();
}