org.apache.flink.cep.time.TimerService Java Examples

The following examples show how to use org.apache.flink.cep.time.TimerService. 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: CEPOperatorTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testProcessingTimestampisPassedToNFA() throws Exception {

	final NFA<Event> nfa = NFACompiler.compileFactory(Pattern.<Event>begin("begin"), true).createNFA();
	final NFA<Event> spyNFA = spy(nfa);

	try (
		OneInputStreamOperatorTestHarness<Event, Map<String, List<Event>>> harness =
			CepOperatorTestUtilities.getCepTestHarness(createOperatorForNFA(spyNFA).build())) {

		long timestamp = 5;
		harness.open();
		harness.setProcessingTime(timestamp);
		StreamRecord<Event> event = event().withTimestamp(3).asStreamRecord();
		harness.processElement(event);
		verify(spyNFA).process(
			any(SharedBufferAccessor.class),
			any(NFAState.class),
			eq(event.getValue()),
			eq(timestamp),
			any(AfterMatchSkipStrategy.class),
			any(TimerService.class));
	}
}
 
Example #2
Source File: CEPOperatorTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testProcessingTimestampisPassedToNFA() throws Exception {

	final NFA<Event> nfa = NFACompiler.compileFactory(Pattern.<Event>begin("begin"), true).createNFA();
	final NFA<Event> spyNFA = spy(nfa);

	try (
		OneInputStreamOperatorTestHarness<Event, Map<String, List<Event>>> harness =
			CepOperatorTestUtilities.getCepTestHarness(createOperatorForNFA(spyNFA).build())) {

		long timestamp = 5;
		harness.open();
		harness.setProcessingTime(timestamp);
		StreamRecord<Event> event = event().withTimestamp(3).asStreamRecord();
		harness.processElement(event);
		verify(spyNFA).process(
			any(SharedBufferAccessor.class),
			any(NFAState.class),
			eq(event.getValue()),
			eq(timestamp),
			any(AfterMatchSkipStrategy.class),
			any(TimerService.class));
	}
}
 
Example #3
Source File: CEPOperatorTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testProcessingTimestampisPassedToNFA() throws Exception {

	final NFA<Event> nfa = NFACompiler.compileFactory(Pattern.<Event>begin("begin"), true).createNFA();
	final NFA<Event> spyNFA = spy(nfa);

	try (
		OneInputStreamOperatorTestHarness<Event, Map<String, List<Event>>> harness =
			CepOperatorTestUtilities.getCepTestHarness(createOperatorForNFA(spyNFA).build())) {

		long timestamp = 5;
		harness.open();
		harness.setProcessingTime(timestamp);
		StreamRecord<Event> event = event().withTimestamp(3).asStreamRecord();
		harness.processElement(event);
		verify(spyNFA).process(
			any(SharedBufferAccessor.class),
			any(NFAState.class),
			eq(event.getValue()),
			eq(timestamp),
			any(AfterMatchSkipStrategy.class),
			any(TimerService.class));
	}
}
 
Example #4
Source File: NFATestHarness.java    From flink with Apache License 2.0 5 votes vote down vote up
private NFATestHarness(
		SharedBuffer<Event> sharedBuffer,
		NFA<Event> nfa,
		NFAState nfaState,
		AfterMatchSkipStrategy afterMatchSkipStrategy,
		TimerService timerService) {
	this.sharedBuffer = sharedBuffer;
	this.nfa = nfa;
	this.nfaState = nfaState;
	this.afterMatchSkipStrategy = afterMatchSkipStrategy;
	this.timerService = timerService;
}
 
Example #5
Source File: NFA.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
ConditionContext(
		final SharedBufferAccessor<T> sharedBufferAccessor,
		final ComputationState computationState,
		final TimerService timerService,
		final long eventTimestamp) {
	this.computationState = computationState;
	this.sharedBufferAccessor = sharedBufferAccessor;
	this.timerService = timerService;
	this.eventTimestamp = eventTimestamp;
}
 
Example #6
Source File: NFATestHarness.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private NFATestHarness(
		SharedBuffer<Event> sharedBuffer,
		NFA<Event> nfa,
		NFAState nfaState,
		AfterMatchSkipStrategy afterMatchSkipStrategy,
		TimerService timerService) {
	this.sharedBuffer = sharedBuffer;
	this.nfa = nfa;
	this.nfaState = nfaState;
	this.afterMatchSkipStrategy = afterMatchSkipStrategy;
	this.timerService = timerService;
}
 
Example #7
Source File: NFA.java    From flink with Apache License 2.0 5 votes vote down vote up
ConditionContext(
		final SharedBufferAccessor<T> sharedBufferAccessor,
		final ComputationState computationState,
		final TimerService timerService,
		final long eventTimestamp) {
	this.computationState = computationState;
	this.sharedBufferAccessor = sharedBufferAccessor;
	this.timerService = timerService;
	this.eventTimestamp = eventTimestamp;
}
 
Example #8
Source File: NFA.java    From flink with Apache License 2.0 5 votes vote down vote up
ConditionContext(
		final SharedBufferAccessor<T> sharedBufferAccessor,
		final ComputationState computationState,
		final TimerService timerService,
		final long eventTimestamp) {
	this.computationState = computationState;
	this.sharedBufferAccessor = sharedBufferAccessor;
	this.timerService = timerService;
	this.eventTimestamp = eventTimestamp;
}
 
Example #9
Source File: NFATestHarness.java    From flink with Apache License 2.0 5 votes vote down vote up
private NFATestHarness(
		SharedBuffer<Event> sharedBuffer,
		NFA<Event> nfa,
		NFAState nfaState,
		AfterMatchSkipStrategy afterMatchSkipStrategy,
		TimerService timerService) {
	this.sharedBuffer = sharedBuffer;
	this.nfa = nfa;
	this.nfaState = nfaState;
	this.afterMatchSkipStrategy = afterMatchSkipStrategy;
	this.timerService = timerService;
}
 
Example #10
Source File: NFATestHarness.java    From flink with Apache License 2.0 4 votes vote down vote up
public NFATestHarnessBuilderBase withTimerService(TimerService timerService) {
	this.timerService = timerService;
	return this;
}
 
Example #11
Source File: NFA.java    From flink with Apache License 2.0 4 votes vote down vote up
private Collection<Map<String, List<T>>> doProcess(
		final SharedBufferAccessor<T> sharedBufferAccessor,
		final NFAState nfaState,
		final EventWrapper event,
		final AfterMatchSkipStrategy afterMatchSkipStrategy,
		final TimerService timerService) throws Exception {

	final PriorityQueue<ComputationState> newPartialMatches = new PriorityQueue<>(NFAState.COMPUTATION_STATE_COMPARATOR);
	final PriorityQueue<ComputationState> potentialMatches = new PriorityQueue<>(NFAState.COMPUTATION_STATE_COMPARATOR);

	// iterate over all current computations
	for (ComputationState computationState : nfaState.getPartialMatches()) {
		final Collection<ComputationState> newComputationStates = computeNextStates(
			sharedBufferAccessor,
			computationState,
			event,
			timerService);

		if (newComputationStates.size() != 1) {
			nfaState.setStateChanged();
		} else if (!newComputationStates.iterator().next().equals(computationState)) {
			nfaState.setStateChanged();
		}

		//delay adding new computation states in case a stop state is reached and we discard the path.
		final Collection<ComputationState> statesToRetain = new ArrayList<>();
		//if stop state reached in this path
		boolean shouldDiscardPath = false;
		for (final ComputationState newComputationState : newComputationStates) {

			if (isFinalState(newComputationState)) {
				potentialMatches.add(newComputationState);
			} else if (isStopState(newComputationState)) {
				//reached stop state. release entry for the stop state
				shouldDiscardPath = true;
				sharedBufferAccessor.releaseNode(newComputationState.getPreviousBufferEntry());
			} else {
				// add new computation state; it will be processed once the next event arrives
				statesToRetain.add(newComputationState);
			}
		}

		if (shouldDiscardPath) {
			// a stop state was reached in this branch. release branch which results in removing previous event from
			// the buffer
			for (final ComputationState state : statesToRetain) {
				sharedBufferAccessor.releaseNode(state.getPreviousBufferEntry());
			}
		} else {
			newPartialMatches.addAll(statesToRetain);
		}
	}

	if (!potentialMatches.isEmpty()) {
		nfaState.setStateChanged();
	}

	List<Map<String, List<T>>> result = new ArrayList<>();
	if (afterMatchSkipStrategy.isSkipStrategy()) {
		processMatchesAccordingToSkipStrategy(sharedBufferAccessor,
			nfaState,
			afterMatchSkipStrategy,
			potentialMatches,
			newPartialMatches,
			result);
	} else {
		for (ComputationState match : potentialMatches) {
			Map<String, List<T>> materializedMatch =
				sharedBufferAccessor.materializeMatch(
					sharedBufferAccessor.extractPatterns(
						match.getPreviousBufferEntry(),
						match.getVersion()).get(0)
				);

			result.add(materializedMatch);
			sharedBufferAccessor.releaseNode(match.getPreviousBufferEntry());
		}
	}

	nfaState.setNewPartialMatches(newPartialMatches);

	return result;
}
 
Example #12
Source File: NFATestHarness.java    From flink with Apache License 2.0 4 votes vote down vote up
public NFATestHarnessBuilderBase withTimerService(TimerService timerService) {
	this.timerService = timerService;
	return this;
}
 
Example #13
Source File: NFA.java    From flink with Apache License 2.0 4 votes vote down vote up
private Collection<Map<String, List<T>>> doProcess(
		final SharedBufferAccessor<T> sharedBufferAccessor,
		final NFAState nfaState,
		final EventWrapper event,
		final AfterMatchSkipStrategy afterMatchSkipStrategy,
		final TimerService timerService) throws Exception {

	final PriorityQueue<ComputationState> newPartialMatches = new PriorityQueue<>(NFAState.COMPUTATION_STATE_COMPARATOR);
	final PriorityQueue<ComputationState> potentialMatches = new PriorityQueue<>(NFAState.COMPUTATION_STATE_COMPARATOR);

	// iterate over all current computations
	for (ComputationState computationState : nfaState.getPartialMatches()) {
		final Collection<ComputationState> newComputationStates = computeNextStates(
			sharedBufferAccessor,
			computationState,
			event,
			timerService);

		if (newComputationStates.size() != 1) {
			nfaState.setStateChanged();
		} else if (!newComputationStates.iterator().next().equals(computationState)) {
			nfaState.setStateChanged();
		}

		//delay adding new computation states in case a stop state is reached and we discard the path.
		final Collection<ComputationState> statesToRetain = new ArrayList<>();
		//if stop state reached in this path
		boolean shouldDiscardPath = false;
		for (final ComputationState newComputationState : newComputationStates) {

			if (isFinalState(newComputationState)) {
				potentialMatches.add(newComputationState);
			} else if (isStopState(newComputationState)) {
				//reached stop state. release entry for the stop state
				shouldDiscardPath = true;
				sharedBufferAccessor.releaseNode(newComputationState.getPreviousBufferEntry());
			} else {
				// add new computation state; it will be processed once the next event arrives
				statesToRetain.add(newComputationState);
			}
		}

		if (shouldDiscardPath) {
			// a stop state was reached in this branch. release branch which results in removing previous event from
			// the buffer
			for (final ComputationState state : statesToRetain) {
				sharedBufferAccessor.releaseNode(state.getPreviousBufferEntry());
			}
		} else {
			newPartialMatches.addAll(statesToRetain);
		}
	}

	if (!potentialMatches.isEmpty()) {
		nfaState.setStateChanged();
	}

	List<Map<String, List<T>>> result = new ArrayList<>();
	if (afterMatchSkipStrategy.isSkipStrategy()) {
		processMatchesAccordingToSkipStrategy(sharedBufferAccessor,
			nfaState,
			afterMatchSkipStrategy,
			potentialMatches,
			newPartialMatches,
			result);
	} else {
		for (ComputationState match : potentialMatches) {
			Map<String, List<T>> materializedMatch =
				sharedBufferAccessor.materializeMatch(
					sharedBufferAccessor.extractPatterns(
						match.getPreviousBufferEntry(),
						match.getVersion()).get(0)
				);

			result.add(materializedMatch);
			sharedBufferAccessor.releaseNode(match.getPreviousBufferEntry());
		}
	}

	nfaState.setNewPartialMatches(newPartialMatches);

	return result;
}
 
Example #14
Source File: NFATestHarness.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
public NFATestHarnessBuilderBase withTimerService(TimerService timerService) {
	this.timerService = timerService;
	return this;
}
 
Example #15
Source File: NFA.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
private Collection<Map<String, List<T>>> doProcess(
		final SharedBufferAccessor<T> sharedBufferAccessor,
		final NFAState nfaState,
		final EventWrapper event,
		final AfterMatchSkipStrategy afterMatchSkipStrategy,
		final TimerService timerService) throws Exception {

	final PriorityQueue<ComputationState> newPartialMatches = new PriorityQueue<>(NFAState.COMPUTATION_STATE_COMPARATOR);
	final PriorityQueue<ComputationState> potentialMatches = new PriorityQueue<>(NFAState.COMPUTATION_STATE_COMPARATOR);

	// iterate over all current computations
	for (ComputationState computationState : nfaState.getPartialMatches()) {
		final Collection<ComputationState> newComputationStates = computeNextStates(
			sharedBufferAccessor,
			computationState,
			event,
			timerService);

		if (newComputationStates.size() != 1) {
			nfaState.setStateChanged();
		} else if (!newComputationStates.iterator().next().equals(computationState)) {
			nfaState.setStateChanged();
		}

		//delay adding new computation states in case a stop state is reached and we discard the path.
		final Collection<ComputationState> statesToRetain = new ArrayList<>();
		//if stop state reached in this path
		boolean shouldDiscardPath = false;
		for (final ComputationState newComputationState : newComputationStates) {

			if (isFinalState(newComputationState)) {
				potentialMatches.add(newComputationState);
			} else if (isStopState(newComputationState)) {
				//reached stop state. release entry for the stop state
				shouldDiscardPath = true;
				sharedBufferAccessor.releaseNode(newComputationState.getPreviousBufferEntry());
			} else {
				// add new computation state; it will be processed once the next event arrives
				statesToRetain.add(newComputationState);
			}
		}

		if (shouldDiscardPath) {
			// a stop state was reached in this branch. release branch which results in removing previous event from
			// the buffer
			for (final ComputationState state : statesToRetain) {
				sharedBufferAccessor.releaseNode(state.getPreviousBufferEntry());
			}
		} else {
			newPartialMatches.addAll(statesToRetain);
		}
	}

	if (!potentialMatches.isEmpty()) {
		nfaState.setStateChanged();
	}

	List<Map<String, List<T>>> result = new ArrayList<>();
	if (afterMatchSkipStrategy.isSkipStrategy()) {
		processMatchesAccordingToSkipStrategy(sharedBufferAccessor,
			nfaState,
			afterMatchSkipStrategy,
			potentialMatches,
			newPartialMatches,
			result);
	} else {
		for (ComputationState match : potentialMatches) {
			Map<String, List<T>> materializedMatch =
				sharedBufferAccessor.materializeMatch(
					sharedBufferAccessor.extractPatterns(
						match.getPreviousBufferEntry(),
						match.getVersion()).get(0)
				);

			result.add(materializedMatch);
			sharedBufferAccessor.releaseNode(match.getPreviousBufferEntry());
		}
	}

	nfaState.setNewPartialMatches(newPartialMatches);

	return result;
}
 
Example #16
Source File: NFA.java    From flink with Apache License 2.0 3 votes vote down vote up
/**
 * Processes the next input event. If some of the computations reach a final state then the
 * resulting event sequences are returned. If computations time out and timeout handling is
 * activated, then the timed out event patterns are returned.
 *
 * <p>If computations reach a stop state, the path forward is discarded and currently constructed path is returned
 * with the element that resulted in the stop state.
 *
 * @param sharedBufferAccessor the accessor to SharedBuffer object that we need to work upon while processing
 * @param nfaState The NFAState object that we need to affect while processing
 * @param event The current event to be processed or null if only pruning shall be done
 * @param timestamp The timestamp of the current event
 * @param afterMatchSkipStrategy The skip strategy to use after per match
 * @param timerService gives access to processing time and time characteristic, needed for condition evaluation
 * @return Tuple of the collection of matched patterns (e.g. the result of computations which have
 * reached a final state) and the collection of timed out patterns (if timeout handling is
 * activated)
 * @throws Exception Thrown if the system cannot access the state.
 */
public Collection<Map<String, List<T>>> process(
		final SharedBufferAccessor<T> sharedBufferAccessor,
		final NFAState nfaState,
		final T event,
		final long timestamp,
		final AfterMatchSkipStrategy afterMatchSkipStrategy,
		final TimerService timerService) throws Exception {
	try (EventWrapper eventWrapper = new EventWrapper(event, timestamp, sharedBufferAccessor)) {
		return doProcess(sharedBufferAccessor, nfaState, eventWrapper, afterMatchSkipStrategy, timerService);
	}
}
 
Example #17
Source File: NFA.java    From flink with Apache License 2.0 3 votes vote down vote up
/**
 * Processes the next input event. If some of the computations reach a final state then the
 * resulting event sequences are returned. If computations time out and timeout handling is
 * activated, then the timed out event patterns are returned.
 *
 * <p>If computations reach a stop state, the path forward is discarded and currently constructed path is returned
 * with the element that resulted in the stop state.
 *
 * @param sharedBufferAccessor the accessor to SharedBuffer object that we need to work upon while processing
 * @param nfaState The NFAState object that we need to affect while processing
 * @param event The current event to be processed or null if only pruning shall be done
 * @param timestamp The timestamp of the current event
 * @param afterMatchSkipStrategy The skip strategy to use after per match
 * @param timerService gives access to processing time and time characteristic, needed for condition evaluation
 * @return Tuple of the collection of matched patterns (e.g. the result of computations which have
 * reached a final state) and the collection of timed out patterns (if timeout handling is
 * activated)
 * @throws Exception Thrown if the system cannot access the state.
 */
public Collection<Map<String, List<T>>> process(
		final SharedBufferAccessor<T> sharedBufferAccessor,
		final NFAState nfaState,
		final T event,
		final long timestamp,
		final AfterMatchSkipStrategy afterMatchSkipStrategy,
		final TimerService timerService) throws Exception {
	try (EventWrapper eventWrapper = new EventWrapper(event, timestamp, sharedBufferAccessor)) {
		return doProcess(sharedBufferAccessor, nfaState, eventWrapper, afterMatchSkipStrategy, timerService);
	}
}
 
Example #18
Source File: NFA.java    From Flink-CEPplus with Apache License 2.0 3 votes vote down vote up
/**
 * Processes the next input event. If some of the computations reach a final state then the
 * resulting event sequences are returned. If computations time out and timeout handling is
 * activated, then the timed out event patterns are returned.
 *
 * <p>If computations reach a stop state, the path forward is discarded and currently constructed path is returned
 * with the element that resulted in the stop state.
 *
 * @param sharedBufferAccessor the accessor to SharedBuffer object that we need to work upon while processing
 * @param nfaState The NFAState object that we need to affect while processing
 * @param event The current event to be processed or null if only pruning shall be done
 * @param timestamp The timestamp of the current event
 * @param afterMatchSkipStrategy The skip strategy to use after per match
 * @param timerService gives access to processing time and time characteristic, needed for condition evaluation
 * @return Tuple of the collection of matched patterns (e.g. the result of computations which have
 * reached a final state) and the collection of timed out patterns (if timeout handling is
 * activated)
 * @throws Exception Thrown if the system cannot access the state.
 */
public Collection<Map<String, List<T>>> process(
		final SharedBufferAccessor<T> sharedBufferAccessor,
		final NFAState nfaState,
		final T event,
		final long timestamp,
		final AfterMatchSkipStrategy afterMatchSkipStrategy,
		final TimerService timerService) throws Exception {
	try (EventWrapper eventWrapper = new EventWrapper(event, timestamp, sharedBufferAccessor)) {
		return doProcess(sharedBufferAccessor, nfaState, eventWrapper, afterMatchSkipStrategy, timerService);
	}
}