org.apache.flink.streaming.api.windowing.triggers.Trigger Java Examples

The following examples show how to use org.apache.flink.streaming.api.windowing.triggers.Trigger. 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: CoGroupedStreams.java    From flink with Apache License 2.0 6 votes vote down vote up
protected WithWindow(DataStream<T1> input1,
		DataStream<T2> input2,
		KeySelector<T1, KEY> keySelector1,
		KeySelector<T2, KEY> keySelector2,
		TypeInformation<KEY> keyType,
		WindowAssigner<? super TaggedUnion<T1, T2>, W> windowAssigner,
		Trigger<? super TaggedUnion<T1, T2>, ? super W> trigger,
		Evictor<? super TaggedUnion<T1, T2>, ? super W> evictor,
		Time allowedLateness) {
	this.input1 = input1;
	this.input2 = input2;

	this.keySelector1 = keySelector1;
	this.keySelector2 = keySelector2;
	this.keyType = keyType;

	this.windowAssigner = windowAssigner;
	this.trigger = trigger;
	this.evictor = evictor;

	this.allowedLateness = allowedLateness;
}
 
Example #2
Source File: EvictingWindowOperator.java    From flink with Apache License 2.0 6 votes vote down vote up
public EvictingWindowOperator(WindowAssigner<? super IN, W> windowAssigner,
		TypeSerializer<W> windowSerializer,
		KeySelector<IN, K> keySelector,
		TypeSerializer<K> keySerializer,
		StateDescriptor<? extends ListState<StreamRecord<IN>>, ?> windowStateDescriptor,
		InternalWindowFunction<Iterable<IN>, OUT, K, W> windowFunction,
		Trigger<? super IN, ? super W> trigger,
		Evictor<? super IN, ? super W> evictor,
		long allowedLateness,
		OutputTag<IN> lateDataOutputTag) {

	super(windowAssigner, windowSerializer, keySelector,
		keySerializer, null, windowFunction, trigger, allowedLateness, lateDataOutputTag);

	this.evictor = checkNotNull(evictor);
	this.evictingWindowStateDescriptor = checkNotNull(windowStateDescriptor);
}
 
Example #3
Source File: EvictingWindowOperator.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
public EvictingWindowOperator(WindowAssigner<? super IN, W> windowAssigner,
		TypeSerializer<W> windowSerializer,
		KeySelector<IN, K> keySelector,
		TypeSerializer<K> keySerializer,
		StateDescriptor<? extends ListState<StreamRecord<IN>>, ?> windowStateDescriptor,
		InternalWindowFunction<Iterable<IN>, OUT, K, W> windowFunction,
		Trigger<? super IN, ? super W> trigger,
		Evictor<? super IN, ? super W> evictor,
		long allowedLateness,
		OutputTag<IN> lateDataOutputTag) {

	super(windowAssigner, windowSerializer, keySelector,
		keySerializer, null, windowFunction, trigger, allowedLateness, lateDataOutputTag);

	this.evictor = checkNotNull(evictor);
	this.evictingWindowStateDescriptor = checkNotNull(windowStateDescriptor);
}
 
Example #4
Source File: JoinedStreams.java    From flink with Apache License 2.0 6 votes vote down vote up
@PublicEvolving
protected WithWindow(DataStream<T1> input1,
		DataStream<T2> input2,
		KeySelector<T1, KEY> keySelector1,
		KeySelector<T2, KEY> keySelector2,
		TypeInformation<KEY> keyType,
		WindowAssigner<? super TaggedUnion<T1, T2>, W> windowAssigner,
		Trigger<? super TaggedUnion<T1, T2>, ? super W> trigger,
		Evictor<? super TaggedUnion<T1, T2>, ? super W> evictor,
		Time allowedLateness) {

	this.input1 = requireNonNull(input1);
	this.input2 = requireNonNull(input2);

	this.keySelector1 = requireNonNull(keySelector1);
	this.keySelector2 = requireNonNull(keySelector2);
	this.keyType = requireNonNull(keyType);

	this.windowAssigner = requireNonNull(windowAssigner);

	this.trigger = trigger;
	this.evictor = evictor;

	this.allowedLateness = allowedLateness;
}
 
Example #5
Source File: WindowOperatorContractTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void testNoGarbageCollectionTimerForGlobalWindow(TimeDomainAdaptor timeAdaptor) throws Exception {

		WindowAssigner<Integer, GlobalWindow> mockAssigner = mockGlobalWindowAssigner();
		timeAdaptor.setIsEventTime(mockAssigner);
		Trigger<Integer, GlobalWindow> mockTrigger = mockTrigger();
		InternalWindowFunction<Iterable<Integer>, Void, Integer, GlobalWindow> mockWindowFunction = mockWindowFunction();

		// this needs to be true for the test to succeed
		assertEquals(Long.MAX_VALUE, GlobalWindow.get().maxTimestamp());

		KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness =
				createWindowOperator(mockAssigner, mockTrigger, 0L, mockWindowFunction);

		testHarness.open();

		assertEquals(0, testHarness.getOutput().size());
		assertEquals(0, testHarness.numKeyedStateEntries());

		testHarness.processElement(new StreamRecord<>(0, 0L));

		// just the window contents
		assertEquals(1, testHarness.numKeyedStateEntries());

		// verify we have no timers for either time domain
		assertEquals(0, testHarness.numEventTimeTimers());
		assertEquals(0, testHarness.numProcessingTimeTimers());
	}
 
Example #6
Source File: WindowOperatorContractTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private static <T> void shouldDeleteProcessingTimeTimerOnElement(Trigger<T, TimeWindow> mockTrigger, final long timestamp) throws Exception {
	doAnswer(new Answer<TriggerResult>() {
		@Override
		public TriggerResult answer(InvocationOnMock invocation) throws Exception {
			@SuppressWarnings("unchecked")
			Trigger.TriggerContext context =
					(Trigger.TriggerContext) invocation.getArguments()[3];
			context.deleteProcessingTimeTimer(timestamp);
			return TriggerResult.CONTINUE;
		}
	})
			.when(mockTrigger).onElement(Matchers.<T>anyObject(), anyLong(), anyTimeWindow(), anyTriggerContext());
}
 
Example #7
Source File: WindowOperatorContractTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private static <T> void shouldRegisterProcessingTimeTimerOnElement(Trigger<T, TimeWindow> mockTrigger, final long timestamp) throws Exception {
	doAnswer(new Answer<TriggerResult>() {
		@Override
		public TriggerResult answer(InvocationOnMock invocation) throws Exception {
			@SuppressWarnings("unchecked")
			Trigger.TriggerContext context =
					(Trigger.TriggerContext) invocation.getArguments()[3];
			context.registerProcessingTimeTimer(timestamp);
			return TriggerResult.CONTINUE;
		}
	})
			.when(mockTrigger).onElement(Matchers.<T>anyObject(), anyLong(), anyTimeWindow(), anyTriggerContext());
}
 
Example #8
Source File: WindowOperatorContractTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
static <T> void shouldRegisterEventTimeTimerOnElement(Trigger<T, TimeWindow> mockTrigger, final long timestamp) throws Exception {
	doAnswer(new Answer<TriggerResult>() {
		@Override
		public TriggerResult answer(InvocationOnMock invocation) throws Exception {
			@SuppressWarnings("unchecked")
			Trigger.TriggerContext context =
					(Trigger.TriggerContext) invocation.getArguments()[3];
			context.registerEventTimeTimer(timestamp);
			return TriggerResult.CONTINUE;
		}
	})
	.when(mockTrigger).onElement(Matchers.<T>anyObject(), anyLong(), anyTimeWindow(), anyTriggerContext());
}
 
Example #9
Source File: WindowOperatorContractTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testOnElementPurgeDoesNotCleanupMergingSet() throws Exception {

	MergingWindowAssigner<Integer, TimeWindow> mockAssigner = mockMergingAssigner();
	Trigger<Integer, TimeWindow> mockTrigger = mockTrigger();
	InternalWindowFunction<Iterable<Integer>, Void, Integer, TimeWindow> mockWindowFunction = mockWindowFunction();

	KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness =
			createWindowOperator(mockAssigner, mockTrigger, 0L, mockWindowFunction);

	testHarness.open();

	when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext()))
			.thenReturn(Arrays.asList(new TimeWindow(0, 2)));

	assertEquals(0, testHarness.getOutput().size());
	assertEquals(0, testHarness.numKeyedStateEntries());

	doAnswer(new Answer<TriggerResult>() {
		@Override
		public TriggerResult answer(InvocationOnMock invocation) throws Exception {
			return TriggerResult.PURGE;
		}
	}).when(mockTrigger).onElement(Matchers.<Integer>anyObject(), anyLong(), anyTimeWindow(), anyTriggerContext());

	testHarness.processElement(new StreamRecord<>(0, 0L));

	assertEquals(1, testHarness.numKeyedStateEntries()); // the merging window set

	assertEquals(1, testHarness.numEventTimeTimers()); // one cleanup timer

	assertEquals(0, testHarness.getOutput().size());
}
 
Example #10
Source File: TriggerTestHarness.java    From flink with Apache License 2.0 5 votes vote down vote up
public TriggerTestHarness(
		Trigger<T, W> trigger,
		TypeSerializer<W> windowSerializer) throws Exception {
	this.trigger = trigger;
	this.windowSerializer = windowSerializer;

	// we only ever use one key, other tests make sure that windows work across different
	// keys
	DummyEnvironment dummyEnv = new DummyEnvironment("test", 1, 0);
	MemoryStateBackend backend = new MemoryStateBackend();

	@SuppressWarnings("unchecked")
	HeapKeyedStateBackend<Integer> stateBackend = (HeapKeyedStateBackend<Integer>) backend.createKeyedStateBackend(
		dummyEnv,
		new JobID(),
		"test_op",
		IntSerializer.INSTANCE,
		1,
		new KeyGroupRange(0, 0),
		new KvStateRegistry().createTaskRegistry(new JobID(), new JobVertexID()),
		TtlTimeProvider.DEFAULT,
		new UnregisteredMetricsGroup(),
		Collections.emptyList(),
		new CloseableRegistry());
	this.stateBackend = stateBackend;

	this.stateBackend.setCurrentKey(KEY);

	this.internalTimerService = new TestInternalTimerService<>(new KeyContext() {
		@Override
		public void setCurrentKey(Object key) {
			// ignore
		}

		@Override
		public Object getCurrentKey() {
			return KEY;
		}
	});
}
 
Example #11
Source File: WindowOperatorContractTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testProcessingElementsWithinAllowedLateness() throws Exception {
	WindowAssigner<Integer, TimeWindow> mockAssigner = mockTimeWindowAssigner();
	Trigger<Integer, TimeWindow> mockTrigger = mockTrigger();
	InternalWindowFunction<Iterable<Integer>, Void, Integer, TimeWindow> mockWindowFunction = mockWindowFunction();

	KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness =
			createWindowOperator(mockAssigner, mockTrigger, 20L, mockWindowFunction);

	testHarness.open();

	when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext()))
			.thenReturn(Arrays.asList(new TimeWindow(0, 2)));

	assertEquals(0, testHarness.getOutput().size());
	assertEquals(0, testHarness.numKeyedStateEntries());

	shouldFireOnElement(mockTrigger);

	// 20 is just at the limit, window.maxTime() is 1 and allowed lateness is 20
	testHarness.processWatermark(new Watermark(20));

	testHarness.processElement(new StreamRecord<>(0, 0L));

	verify(mockWindowFunction, times(1)).process(eq(0), eq(new TimeWindow(0, 2)), anyInternalWindowContext(), intIterable(0), WindowOperatorContractTest.<Void>anyCollector());

	// clear is only called at cleanup time/GC time
	verify(mockTrigger, never()).clear(anyTimeWindow(), anyTriggerContext());

	// FIRE should not purge contents
	assertEquals(1, testHarness.numKeyedStateEntries()); // window contents plus trigger state
	assertEquals(1, testHarness.numEventTimeTimers()); // just the GC timer
}
 
Example #12
Source File: WindowOperator.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new {@code WindowOperator} based on the given policies and user functions.
 */
public WindowOperator(
		WindowAssigner<? super IN, W> windowAssigner,
		TypeSerializer<W> windowSerializer,
		KeySelector<IN, K> keySelector,
		TypeSerializer<K> keySerializer,
		StateDescriptor<? extends AppendingState<IN, ACC>, ?> windowStateDescriptor,
		InternalWindowFunction<ACC, OUT, K, W> windowFunction,
		Trigger<? super IN, ? super W> trigger,
		long allowedLateness,
		OutputTag<IN> lateDataOutputTag) {

	super(windowFunction);

	checkArgument(!(windowAssigner instanceof BaseAlignedWindowAssigner),
		"The " + windowAssigner.getClass().getSimpleName() + " cannot be used with a WindowOperator. " +
			"This assigner is only used with the AccumulatingProcessingTimeWindowOperator and " +
			"the AggregatingProcessingTimeWindowOperator");

	checkArgument(allowedLateness >= 0);

	checkArgument(windowStateDescriptor == null || windowStateDescriptor.isSerializerInitialized(),
			"window state serializer is not properly initialized");

	this.windowAssigner = checkNotNull(windowAssigner);
	this.windowSerializer = checkNotNull(windowSerializer);
	this.keySelector = checkNotNull(keySelector);
	this.keySerializer = checkNotNull(keySerializer);
	this.windowStateDescriptor = windowStateDescriptor;
	this.trigger = checkNotNull(trigger);
	this.allowedLateness = allowedLateness;
	this.lateDataOutputTag = lateDataOutputTag;

	setChainingStrategy(ChainingStrategy.ALWAYS);
}
 
Example #13
Source File: WindowedStream.java    From flink with Apache License 2.0 5 votes vote down vote up
private static String generateOperatorName(
		WindowAssigner<?, ?> assigner,
		Trigger<?, ?> trigger,
		@Nullable Evictor<?, ?> evictor,
		Function function1,
		@Nullable Function function2) {
	return "Window(" +
		assigner + ", " +
		trigger.getClass().getSimpleName() + ", " +
		(evictor == null ? "" : (evictor.getClass().getSimpleName() + ", ")) +
		generateFunctionName(function1) +
		(function2 == null ? "" : (", " + generateFunctionName(function2))) +
		")";
}
 
Example #14
Source File: WindowOperatorContractTest.java    From flink with Apache License 2.0 5 votes vote down vote up
static <T> void shouldRegisterEventTimeTimerOnElement(Trigger<T, TimeWindow> mockTrigger, final long timestamp) throws Exception {
	doAnswer(new Answer<TriggerResult>() {
		@Override
		public TriggerResult answer(InvocationOnMock invocation) throws Exception {
			@SuppressWarnings("unchecked")
			Trigger.TriggerContext context =
					(Trigger.TriggerContext) invocation.getArguments()[3];
			context.registerEventTimeTimer(timestamp);
			return TriggerResult.CONTINUE;
		}
	})
	.when(mockTrigger).onElement(Matchers.<T>anyObject(), anyLong(), anyTimeWindow(), anyTriggerContext());
}
 
Example #15
Source File: TriggerTestHarness.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public TriggerTestHarness(
		Trigger<T, W> trigger,
		TypeSerializer<W> windowSerializer) throws Exception {
	this.trigger = trigger;
	this.windowSerializer = windowSerializer;

	// we only ever use one key, other tests make sure that windows work across different
	// keys
	DummyEnvironment dummyEnv = new DummyEnvironment("test", 1, 0);
	MemoryStateBackend backend = new MemoryStateBackend();

	@SuppressWarnings("unchecked")
	HeapKeyedStateBackend<Integer> stateBackend = (HeapKeyedStateBackend<Integer>) backend.createKeyedStateBackend(
		dummyEnv,
		new JobID(),
		"test_op",
		IntSerializer.INSTANCE,
		1,
		new KeyGroupRange(0, 0),
		new KvStateRegistry().createTaskRegistry(new JobID(), new JobVertexID()),
		TtlTimeProvider.DEFAULT,
		new UnregisteredMetricsGroup(),
		Collections.emptyList(),
		new CloseableRegistry());
	this.stateBackend = stateBackend;

	this.stateBackend.setCurrentKey(KEY);

	this.internalTimerService = new TestInternalTimerService<>(new KeyContext() {
		@Override
		public void setCurrentKey(Object key) {
			// ignore
		}

		@Override
		public Object getCurrentKey() {
			return KEY;
		}
	});
}
 
Example #16
Source File: WindowOperatorContractTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void testMergingWindowSetClearedAtGarbageCollection(TimeDomainAdaptor timeAdaptor) throws Exception {
	WindowAssigner<Integer, TimeWindow> mockAssigner = mockMergingAssigner();
	timeAdaptor.setIsEventTime(mockAssigner);
	Trigger<Integer, TimeWindow> mockTrigger = mockTrigger();
	InternalWindowFunction<Iterable<Integer>, Void, Integer, TimeWindow> mockWindowFunction = mockWindowFunction();

	KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness =
			createWindowOperator(mockAssigner, mockTrigger, 20L, mockWindowFunction);

	testHarness.open();

	when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext()))
			.thenReturn(Arrays.asList(new TimeWindow(0, 20)));

	assertEquals(0, testHarness.getOutput().size());
	assertEquals(0, testHarness.numKeyedStateEntries());

	testHarness.processElement(new StreamRecord<>(0, 0L));

	assertEquals(2, testHarness.numKeyedStateEntries()); // window contents plus merging window set
	assertEquals(1, timeAdaptor.numTimers(testHarness)); // gc timers

	timeAdaptor.advanceTime(testHarness, 19 + 20); // 19 is maxTime of the window

	assertEquals(0, testHarness.numKeyedStateEntries());
	assertEquals(0, timeAdaptor.numTimers(testHarness));
}
 
Example #17
Source File: RegularWindowOperatorContractTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Special method for creating a {@link WindowOperator} with a custom {@link StateDescriptor}
 * for the window contents state.
 */
private <W extends Window, ACC, OUT> KeyedOneInputStreamOperatorTestHarness<Integer, Integer, OUT> createWindowOperator(
		WindowAssigner<Integer, W> assigner,
		Trigger<Integer, W> trigger,
		long allowedLatenss,
		StateDescriptor<? extends AppendingState<Integer, ACC>, ?> stateDescriptor,
		InternalWindowFunction<ACC, OUT, Integer, W> windowFunction) throws Exception {

	KeySelector<Integer, Integer> keySelector = new KeySelector<Integer, Integer>() {
		private static final long serialVersionUID = 1L;

		@Override
		public Integer getKey(Integer value) throws Exception {
			return value;
		}
	};

	@SuppressWarnings("unchecked")
	WindowOperator<Integer, Integer, ACC, OUT, W> operator = new WindowOperator<>(
			assigner,
			assigner.getWindowSerializer(new ExecutionConfig()),
			keySelector,
			IntSerializer.INSTANCE,
			stateDescriptor,
			windowFunction,
			trigger,
			allowedLatenss,
			null /* late output tag */);

	return new KeyedOneInputStreamOperatorTestHarness<>(
			operator,
			keySelector,
			BasicTypeInfo.INT_TYPE_INFO);
}
 
Example #18
Source File: WindowOperator.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new {@code WindowOperator} based on the given policies and user functions.
 */
public WindowOperator(
		WindowAssigner<? super IN, W> windowAssigner,
		TypeSerializer<W> windowSerializer,
		KeySelector<IN, K> keySelector,
		TypeSerializer<K> keySerializer,
		StateDescriptor<? extends AppendingState<IN, ACC>, ?> windowStateDescriptor,
		InternalWindowFunction<ACC, OUT, K, W> windowFunction,
		Trigger<? super IN, ? super W> trigger,
		long allowedLateness,
		OutputTag<IN> lateDataOutputTag) {

	super(windowFunction);

	checkArgument(!(windowAssigner instanceof BaseAlignedWindowAssigner),
		"The " + windowAssigner.getClass().getSimpleName() + " cannot be used with a WindowOperator. " +
			"This assigner is only used with the AccumulatingProcessingTimeWindowOperator and " +
			"the AggregatingProcessingTimeWindowOperator");

	checkArgument(allowedLateness >= 0);

	checkArgument(windowStateDescriptor == null || windowStateDescriptor.isSerializerInitialized(),
			"window state serializer is not properly initialized");

	this.windowAssigner = checkNotNull(windowAssigner);
	this.windowSerializer = checkNotNull(windowSerializer);
	this.keySelector = checkNotNull(keySelector);
	this.keySerializer = checkNotNull(keySerializer);
	this.windowStateDescriptor = windowStateDescriptor;
	this.trigger = checkNotNull(trigger);
	this.allowedLateness = allowedLateness;
	this.lateDataOutputTag = lateDataOutputTag;

	setChainingStrategy(ChainingStrategy.ALWAYS);
}
 
Example #19
Source File: WindowOperatorContractTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testLateWindowDropping() throws Exception {
	WindowAssigner<Integer, TimeWindow> mockAssigner = mockTimeWindowAssigner();
	Trigger<Integer, TimeWindow> mockTrigger = mockTrigger();
	InternalWindowFunction<Iterable<Integer>, Void, Integer, TimeWindow> mockWindowFunction = mockWindowFunction();

	KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness =
			createWindowOperator(mockAssigner, mockTrigger, 20L, mockWindowFunction);

	testHarness.open();

	when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext()))
			.thenReturn(Arrays.asList(new TimeWindow(0, 2)));

	assertEquals(0, testHarness.getOutput().size());
	assertEquals(0, testHarness.numKeyedStateEntries());

	shouldFireOnElement(mockTrigger);

	// window.maxTime() == 1 plus 20L of allowed lateness
	testHarness.processWatermark(new Watermark(21));

	testHarness.processElement(new StreamRecord<>(0, 0L));

	// there should be nothing
	assertEquals(0, testHarness.numKeyedStateEntries());
	assertEquals(0, testHarness.numEventTimeTimers());
	assertEquals(0, testHarness.numProcessingTimeTimers());

	// there should be two elements now
	assertEquals(0, testHarness.extractOutputStreamRecords().size());
}
 
Example #20
Source File: WindowOperatorContractTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void testMergingWindowSetClearedAtGarbageCollection(TimeDomainAdaptor timeAdaptor) throws Exception {
	WindowAssigner<Integer, TimeWindow> mockAssigner = mockMergingAssigner();
	timeAdaptor.setIsEventTime(mockAssigner);
	Trigger<Integer, TimeWindow> mockTrigger = mockTrigger();
	InternalWindowFunction<Iterable<Integer>, Void, Integer, TimeWindow> mockWindowFunction = mockWindowFunction();

	KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness =
			createWindowOperator(mockAssigner, mockTrigger, 20L, mockWindowFunction);

	testHarness.open();

	when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext()))
			.thenReturn(Arrays.asList(new TimeWindow(0, 20)));

	assertEquals(0, testHarness.getOutput().size());
	assertEquals(0, testHarness.numKeyedStateEntries());

	testHarness.processElement(new StreamRecord<>(0, 0L));

	assertEquals(2, testHarness.numKeyedStateEntries()); // window contents plus merging window set
	assertEquals(1, timeAdaptor.numTimers(testHarness)); // gc timers

	timeAdaptor.advanceTime(testHarness, 19 + 20); // 19 is maxTime of the window

	assertEquals(0, testHarness.numKeyedStateEntries());
	assertEquals(0, timeAdaptor.numTimers(testHarness));
}
 
Example #21
Source File: WindowOperatorContractTest.java    From flink with Apache License 2.0 4 votes vote down vote up
private void testOnTimeFire(final TimeDomainAdaptor timeAdaptor) throws Exception {

		WindowAssigner<Integer, TimeWindow> mockAssigner = mockTimeWindowAssigner();
		timeAdaptor.setIsEventTime(mockAssigner);
		Trigger<Integer, TimeWindow> mockTrigger = mockTrigger();
		InternalWindowFunction<Iterable<Integer>, Void, Integer, TimeWindow> mockWindowFunction = mockWindowFunction();

		KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness =
				createWindowOperator(mockAssigner, mockTrigger, 0L, mockWindowFunction);

		testHarness.open();

		timeAdaptor.advanceTime(testHarness, Long.MIN_VALUE);

		when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext()))
				.thenReturn(Arrays.asList(new TimeWindow(2, 4), new TimeWindow(0, 2)));

		assertEquals(0, testHarness.extractOutputStreamRecords().size());
		assertEquals(0, testHarness.numKeyedStateEntries());

		doAnswer(new Answer<TriggerResult>() {
			@Override
			public TriggerResult answer(InvocationOnMock invocation) throws Exception {
				Trigger.TriggerContext context = (Trigger.TriggerContext) invocation.getArguments()[3];
				// don't interfere with cleanup timers
				timeAdaptor.registerTimer(context, 0L);
				context.getPartitionedState(valueStateDescriptor).update("hello");
				return TriggerResult.CONTINUE;
			}
		}).when(mockTrigger).onElement(Matchers.<Integer>anyObject(), anyLong(), anyTimeWindow(), anyTriggerContext());

		timeAdaptor.shouldFireOnTime(mockTrigger);

		testHarness.processElement(new StreamRecord<>(0, 0L));

		assertEquals(4, testHarness.numKeyedStateEntries()); // window-contents and trigger state for two windows
		assertEquals(4, timeAdaptor.numTimers(testHarness)); // timers/gc timers for two windows

		timeAdaptor.advanceTime(testHarness, 0L);

		verify(mockWindowFunction, times(2)).process(eq(0), anyTimeWindow(), anyInternalWindowContext(), anyIntIterable(), WindowOperatorContractTest.<Void>anyCollector());
		verify(mockWindowFunction, times(1)).process(eq(0), eq(new TimeWindow(0, 2)), anyInternalWindowContext(), intIterable(0), WindowOperatorContractTest.<Void>anyCollector());
		verify(mockWindowFunction, times(1)).process(eq(0), eq(new TimeWindow(2, 4)), anyInternalWindowContext(), intIterable(0), WindowOperatorContractTest.<Void>anyCollector());

		// clear is only called at cleanup time/GC time
		verify(mockTrigger, never()).clear(anyTimeWindow(), anyTriggerContext());

		// FIRE should not purge contents
		assertEquals(4, testHarness.numKeyedStateEntries());
		assertEquals(2, timeAdaptor.numTimers(testHarness)); // only gc timers left
	}
 
Example #22
Source File: WindowOperatorContractTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void shouldFireOnTime(Trigger<?, TimeWindow> mockTrigger) throws Exception {
	shouldFireOnEventTime(mockTrigger);
}
 
Example #23
Source File: JoinedStreams.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Sets the {@code Trigger} that should be used to trigger window emission.
 */
@PublicEvolving
public WithWindow<T1, T2, KEY, W> trigger(Trigger<? super TaggedUnion<T1, T2>, ? super W> newTrigger) {
	return new WithWindow<>(input1, input2, keySelector1, keySelector2, keyType,
			windowAssigner, newTrigger, evictor, allowedLateness);
}
 
Example #24
Source File: WindowOperatorContractTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
private void testOnTimeFire(final TimeDomainAdaptor timeAdaptor) throws Exception {

		WindowAssigner<Integer, TimeWindow> mockAssigner = mockTimeWindowAssigner();
		timeAdaptor.setIsEventTime(mockAssigner);
		Trigger<Integer, TimeWindow> mockTrigger = mockTrigger();
		InternalWindowFunction<Iterable<Integer>, Void, Integer, TimeWindow> mockWindowFunction = mockWindowFunction();

		KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness =
				createWindowOperator(mockAssigner, mockTrigger, 0L, mockWindowFunction);

		testHarness.open();

		timeAdaptor.advanceTime(testHarness, Long.MIN_VALUE);

		when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext()))
				.thenReturn(Arrays.asList(new TimeWindow(2, 4), new TimeWindow(0, 2)));

		assertEquals(0, testHarness.extractOutputStreamRecords().size());
		assertEquals(0, testHarness.numKeyedStateEntries());

		doAnswer(new Answer<TriggerResult>() {
			@Override
			public TriggerResult answer(InvocationOnMock invocation) throws Exception {
				Trigger.TriggerContext context = (Trigger.TriggerContext) invocation.getArguments()[3];
				// don't interfere with cleanup timers
				timeAdaptor.registerTimer(context, 0L);
				context.getPartitionedState(valueStateDescriptor).update("hello");
				return TriggerResult.CONTINUE;
			}
		}).when(mockTrigger).onElement(Matchers.<Integer>anyObject(), anyLong(), anyTimeWindow(), anyTriggerContext());

		timeAdaptor.shouldFireOnTime(mockTrigger);

		testHarness.processElement(new StreamRecord<>(0, 0L));

		assertEquals(4, testHarness.numKeyedStateEntries()); // window-contents and trigger state for two windows
		assertEquals(4, timeAdaptor.numTimers(testHarness)); // timers/gc timers for two windows

		timeAdaptor.advanceTime(testHarness, 0L);

		verify(mockWindowFunction, times(2)).process(eq(0), anyTimeWindow(), anyInternalWindowContext(), anyIntIterable(), WindowOperatorContractTest.<Void>anyCollector());
		verify(mockWindowFunction, times(1)).process(eq(0), eq(new TimeWindow(0, 2)), anyInternalWindowContext(), intIterable(0), WindowOperatorContractTest.<Void>anyCollector());
		verify(mockWindowFunction, times(1)).process(eq(0), eq(new TimeWindow(2, 4)), anyInternalWindowContext(), intIterable(0), WindowOperatorContractTest.<Void>anyCollector());

		// clear is only called at cleanup time/GC time
		verify(mockTrigger, never()).clear(anyTimeWindow(), anyTriggerContext());

		// FIRE should not purge contents
		assertEquals(4, testHarness.numKeyedStateEntries());
		assertEquals(2, timeAdaptor.numTimers(testHarness)); // only gc timers left
	}
 
Example #25
Source File: WindowOperatorContractTest.java    From flink with Apache License 2.0 4 votes vote down vote up
static Trigger.TriggerContext anyTriggerContext() {
	return Mockito.any();
}
 
Example #26
Source File: WindowOperatorContractTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * A misbehaving {@code WindowAssigner} can cause a window to become late by merging if
 * it moves the end-of-window time before the watermark. This verifies that we don't allow that.
 */
void testRejectShrinkingMergingWindows(final TimeDomainAdaptor timeAdaptor) throws Exception {
	int allowedLateness = 10;

	if (timeAdaptor instanceof ProcessingTimeAdaptor) {
		// we don't have allowed lateness for processing time
		allowedLateness = 0;
	}

	MergingWindowAssigner<Integer, TimeWindow> mockAssigner = mockMergingAssigner();
	timeAdaptor.setIsEventTime(mockAssigner);
	Trigger<Integer, TimeWindow> mockTrigger = mockTrigger();
	InternalWindowFunction<Iterable<Integer>, Void, Integer, TimeWindow> mockWindowFunction = mockWindowFunction();

	KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness =
			createWindowOperator(mockAssigner, mockTrigger, allowedLateness, mockWindowFunction);

	testHarness.open();

	timeAdaptor.advanceTime(testHarness, 0);

	assertEquals(0, testHarness.extractOutputStreamRecords().size());
	assertEquals(0, testHarness.numKeyedStateEntries());

	when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext()))
			.thenReturn(Arrays.asList(new TimeWindow(0, 22)));

	testHarness.processElement(new StreamRecord<>(0, 0L));

	assertEquals(2, testHarness.numKeyedStateEntries()); // window contents and merging window set
	assertEquals(1, timeAdaptor.numTimers(testHarness)); // cleanup timer

	when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext()))
			.thenReturn(Arrays.asList(new TimeWindow(0, 25)));

	timeAdaptor.advanceTime(testHarness, 20);

	// our window should still be there
	assertEquals(2, testHarness.numKeyedStateEntries()); // window contents and merging window set
	assertEquals(1, timeAdaptor.numTimers(testHarness)); // cleanup timer

	// the result timestamp is ... + 2 because a watermark t says no element with
	// timestamp <= t will come in the future and because window ends are exclusive:
	// a window (0, 12) will have 11 as maxTimestamp. With the watermark at 20, 10 would
	// already be considered late
	shouldMergeWindows(
			mockAssigner,
			new ArrayList<>(Arrays.asList(new TimeWindow(0, 22), new TimeWindow(0, 25))),
			new ArrayList<>(Arrays.asList(new TimeWindow(0, 22), new TimeWindow(0, 25))),
			new TimeWindow(0, 20 - allowedLateness + 2));

	testHarness.processElement(new StreamRecord<>(0, 0L));

	// now merge it to a window that is just late
	when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext()))
			.thenReturn(Arrays.asList(new TimeWindow(0, 25)));

	shouldMergeWindows(
			mockAssigner,
			new ArrayList<>(Arrays.asList(new TimeWindow(0, 20 - allowedLateness + 2), new TimeWindow(0, 25))),
			new ArrayList<>(Arrays.asList(new TimeWindow(0, 20 - allowedLateness + 2), new TimeWindow(0, 25))),
			new TimeWindow(0, 20 - allowedLateness + 1));

	expectedException.expect(UnsupportedOperationException.class);
	testHarness.processElement(new StreamRecord<>(0, 0L));
}
 
Example #27
Source File: WindowOperatorContractTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void shouldPurgeOnTime(Trigger<?, TimeWindow> mockTrigger) throws Exception {
	shouldPurgeOnProcessingTime(mockTrigger);
}
 
Example #28
Source File: DynamicProcessingTimeSessionWindows.java    From flink with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Trigger<T, TimeWindow> getDefaultTrigger(StreamExecutionEnvironment env) {
	return (Trigger<T, TimeWindow>) ProcessingTimeTrigger.create();
}
 
Example #29
Source File: WindowOperatorContractTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Test
public void testOnElementFire() throws Exception {

	WindowAssigner<Integer, TimeWindow> mockAssigner = mockTimeWindowAssigner();
	Trigger<Integer, TimeWindow> mockTrigger = mockTrigger();
	InternalWindowFunction<Iterable<Integer>, Void, Integer, TimeWindow> mockWindowFunction = mockWindowFunction();

	KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness =
			createWindowOperator(mockAssigner, mockTrigger, 0L, mockWindowFunction);

	testHarness.open();

	when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext()))
			.thenReturn(Arrays.asList(new TimeWindow(2, 4), new TimeWindow(0, 2)));

	assertEquals(0, testHarness.getOutput().size());
	assertEquals(0, testHarness.numKeyedStateEntries());

	doAnswer(new Answer<TriggerResult>() {
		@Override
		public TriggerResult answer(InvocationOnMock invocation) throws Exception {
			TimeWindow window = (TimeWindow) invocation.getArguments()[2];
			Trigger.TriggerContext context = (Trigger.TriggerContext) invocation.getArguments()[3];
			context.registerEventTimeTimer(window.getEnd());
			context.getPartitionedState(valueStateDescriptor).update("hello");
			return TriggerResult.FIRE;
		}
	}).when(mockTrigger).onElement(Matchers.<Integer>anyObject(), anyLong(), anyTimeWindow(), anyTriggerContext());

	testHarness.processElement(new StreamRecord<>(0, 0L));

	verify(mockWindowFunction, times(2)).process(eq(0), anyTimeWindow(), anyInternalWindowContext(), anyIntIterable(), WindowOperatorContractTest.<Void>anyCollector());
	verify(mockWindowFunction, times(1)).process(eq(0), eq(new TimeWindow(0, 2)), anyInternalWindowContext(), intIterable(0), WindowOperatorContractTest.<Void>anyCollector());
	verify(mockWindowFunction, times(1)).process(eq(0), eq(new TimeWindow(2, 4)), anyInternalWindowContext(), intIterable(0), WindowOperatorContractTest.<Void>anyCollector());

	// clear is only called at cleanup time/GC time
	verify(mockTrigger, never()).clear(anyTimeWindow(), anyTriggerContext());

	// FIRE should not purge contents
	assertEquals(4, testHarness.numKeyedStateEntries()); // window contents plus trigger state
	assertEquals(4, testHarness.numEventTimeTimers()); // window timers/gc timers
}
 
Example #30
Source File: WindowOperatorContractTest.java    From flink with Apache License 2.0 4 votes vote down vote up
private static <T> void shouldFireAndPurgeOnElement(Trigger<T, TimeWindow> mockTrigger) throws Exception {
	when(mockTrigger.onElement(Matchers.<T>anyObject(), anyLong(), anyTimeWindow(), anyTriggerContext())).thenReturn(TriggerResult.FIRE_AND_PURGE);
}