Java Code Examples for org.apache.flink.streaming.api.watermark.Watermark#getTimestamp()

The following examples show how to use org.apache.flink.streaming.api.watermark.Watermark#getTimestamp() . 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: ParallelReader.java    From alibaba-flink-connectors with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
	long lastWatermark = 0;
	while (!stopped) {
		Watermark nextWatermark = new Watermark(provider.getWatermark());
		if (lastWatermark != nextWatermark.getTimestamp()) {
			lastWatermark = nextWatermark.getTimestamp();
			ctx.emitWatermark(nextWatermark);
		}
		try {
			Thread.sleep(this.watermarkInterval);
		} catch (InterruptedException e) {
			break;
		}
	}
}
 
Example 2
Source File: WatermarkAssignerOperator.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Override the base implementation to completely ignore watermarks propagated from
 * upstream (we rely only on the {@link WatermarkGenerator} to emit watermarks from here).
 */
@Override
public void processWatermark(Watermark mark) throws Exception {
	// if we receive a Long.MAX_VALUE watermark we forward it since it is used
	// to signal the end of input and to not block watermark progress downstream
	if (mark.getTimestamp() == Long.MAX_VALUE && currentWatermark != Long.MAX_VALUE) {
		if (idleTimeout > 0) {
			// mark the channel active
			streamStatusMaintainer.toggleStreamStatus(StreamStatus.ACTIVE);
		}
		currentWatermark = Long.MAX_VALUE;
		output.emitWatermark(mark);
	}
}
 
Example 3
Source File: TimestampsAndPunctuatedWatermarksOperator.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void processElement(StreamRecord<T> element) throws Exception {
	final T value = element.getValue();
	final long newTimestamp = userFunction.extractTimestamp(value,
			element.hasTimestamp() ? element.getTimestamp() : Long.MIN_VALUE);

	output.collect(element.replace(element.getValue(), newTimestamp));

	final Watermark nextWatermark = userFunction.checkAndGetNextWatermark(value, newTimestamp);
	if (nextWatermark != null && nextWatermark.getTimestamp() > currentWatermark) {
		currentWatermark = nextWatermark.getTimestamp();
		output.emitWatermark(nextWatermark);
	}
}
 
Example 4
Source File: ExtractTimestampsOperator.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void processWatermark(Watermark mark) throws Exception {
	// if we receive a Long.MAX_VALUE watermark we forward it since it is used
	// to signal the end of input and to not block watermark progress downstream
	if (mark.getTimestamp() == Long.MAX_VALUE && mark.getTimestamp() > currentWatermark) {
		currentWatermark = Long.MAX_VALUE;
		output.emitWatermark(mark);
	}
}
 
Example 5
Source File: TimestampsAndPeriodicWatermarksOperator.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void onProcessingTime(long timestamp) throws Exception {
	// register next timer
	Watermark newWatermark = userFunction.getCurrentWatermark();
	if (newWatermark != null && newWatermark.getTimestamp() > currentWatermark) {
		currentWatermark = newWatermark.getTimestamp();
		// emit watermark
		output.emitWatermark(newWatermark);
	}

	long now = getProcessingTimeService().getCurrentProcessingTime();
	getProcessingTimeService().registerTimer(now + watermarkInterval, this);
}
 
Example 6
Source File: Kafka011ITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void processWatermark(Watermark mark) throws Exception {
	wmCount++;

	if (lastWM <= mark.getTimestamp()) {
		lastWM = mark.getTimestamp();
	} else {
		throw new RuntimeException("Received watermark higher than the last one");
	}

	if (mark.getTimestamp() % 11 != 0 && mark.getTimestamp() != Long.MAX_VALUE) {
		throw new RuntimeException("Invalid watermark: " + mark.getTimestamp());
	}
}
 
Example 7
Source File: OneInputStreamOperatorTestHarness.java    From flink with Apache License 2.0 5 votes vote down vote up
public void processWatermark(Watermark mark) throws Exception {
	currentWatermark = mark.getTimestamp();
	if (inputs.isEmpty()) {
		getOneInputOperator().processWatermark(mark);
	}
	else {
		checkState(inputs.size() == 1);
		Input input = inputs.get(0);
		input.processWatermark(mark);
	}
}
 
Example 8
Source File: IngestionTimeExtractorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testMonotonousTimestamps() {
	AssignerWithPeriodicWatermarks<String> assigner = new IngestionTimeExtractor<>();

	long maxRecordSoFar = 0L;
	long maxWatermarkSoFar = 0L;

	for (int i = 0; i < 1343; i++) {
		if (i % 7 == 1) {
			Watermark mark = assigner.getCurrentWatermark();
			assertNotNull(mark);

			// increasing watermarks
			assertTrue(mark.getTimestamp() >= maxWatermarkSoFar);
			maxWatermarkSoFar = mark.getTimestamp();

			// tight watermarks
			assertTrue(mark.getTimestamp() >= maxRecordSoFar - 1);
		} else {
			long next = assigner.extractTimestamp("a", Long.MIN_VALUE);

			// increasing timestamps
			assertTrue(next >= maxRecordSoFar);

			// timestamps are never below or at the watermark
			assertTrue(next > maxWatermarkSoFar);

			maxRecordSoFar = next;
		}

		if (i % 9 == 0) {
			try {
				Thread.sleep(1);
			} catch (InterruptedException ignored) {}
		}
	}
}
 
Example 9
Source File: AbstractStreamOperator.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public void processWatermark2(Watermark mark) throws Exception {
	input2Watermark = mark.getTimestamp();
	long newMin = Math.min(input1Watermark, input2Watermark);
	if (newMin > combinedWatermark) {
		combinedWatermark = newMin;
		processWatermark(new Watermark(combinedWatermark));
	}
}
 
Example 10
Source File: AbstractStreamOperator.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public void processWatermark1(Watermark mark) throws Exception {
	input1Watermark = mark.getTimestamp();
	long newMin = Math.min(input1Watermark, input2Watermark);
	if (newMin > combinedWatermark) {
		combinedWatermark = newMin;
		processWatermark(new Watermark(combinedWatermark));
	}
}
 
Example 11
Source File: GroupAlsoByWindowTest.java    From flink-dataflow with Apache License 2.0 5 votes vote down vote up
@Override
public int compare(Object o1, Object o2) {
	if (o1 instanceof Watermark && o2 instanceof Watermark) {
		Watermark w1 = (Watermark) o1;
		Watermark w2 = (Watermark) o2;
		return (int) (w1.getTimestamp() - w2.getTimestamp());
	} else {
		StreamRecord<WindowedValue<KV<String, Integer>>> sr0 = (StreamRecord<WindowedValue<KV<String, Integer>>>) o1;
		StreamRecord<WindowedValue<KV<String, Integer>>> sr1 = (StreamRecord<WindowedValue<KV<String, Integer>>>) o2;

		int comparison = (int) (sr0.getValue().getTimestamp().getMillis() - sr1.getValue().getTimestamp().getMillis());
		if (comparison != 0) {
			return comparison;
		}

		comparison = sr0.getValue().getValue().getKey().compareTo(sr1.getValue().getValue().getKey());
		if(comparison == 0) {
			comparison = Integer.compare(
					sr0.getValue().getValue().getValue(),
					sr1.getValue().getValue().getValue());
		}
		if(comparison == 0) {
			Collection windowsA = sr0.getValue().getWindows();
			Collection windowsB = sr1.getValue().getWindows();

			if(windowsA.size() != 1 || windowsB.size() != 1) {
				throw new IllegalStateException("A value cannot belong to more than one windows after grouping.");
			}

			BoundedWindow windowA = (BoundedWindow) windowsA.iterator().next();
			BoundedWindow windowB = (BoundedWindow) windowsB.iterator().next();
			comparison = Long.compare(windowA.maxTimestamp().getMillis(), windowB.maxTimestamp().getMillis());
		}
		return comparison;
	}
}
 
Example 12
Source File: TimestampsAndPunctuatedWatermarksOperator.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void processElement(StreamRecord<T> element) throws Exception {
	final T value = element.getValue();
	final long newTimestamp = userFunction.extractTimestamp(value,
			element.hasTimestamp() ? element.getTimestamp() : Long.MIN_VALUE);

	output.collect(element.replace(element.getValue(), newTimestamp));

	final Watermark nextWatermark = userFunction.checkAndGetNextWatermark(value, newTimestamp);
	if (nextWatermark != null && nextWatermark.getTimestamp() > currentWatermark) {
		currentWatermark = nextWatermark.getTimestamp();
		output.emitWatermark(nextWatermark);
	}
}
 
Example 13
Source File: ProcessOperator.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public void processWatermark(Watermark mark) throws Exception {
	super.processWatermark(mark);
	this.currentWatermark = mark.getTimestamp();
}
 
Example 14
Source File: ProcessOperator.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void processWatermark(Watermark mark) throws Exception {
	super.processWatermark(mark);
	this.currentWatermark = mark.getTimestamp();
}
 
Example 15
Source File: KinesisDataFetcher.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Called periodically to emit a watermark. Checks all shards for the current event time
 * watermark, and possibly emits the next watermark.
 *
 * <p>Shards that have not received an update for a certain interval are considered inactive so as
 * to not hold back the watermark indefinitely. When all shards are inactive, the subtask will be
 * marked as temporarily idle to not block downstream operators.
 */
@VisibleForTesting
protected void emitWatermark() {
	LOG.debug("Evaluating watermark for subtask {} time {}", indexOfThisConsumerSubtask, getCurrentTimeMillis());
	long potentialWatermark = Long.MAX_VALUE;
	long potentialNextWatermark = Long.MAX_VALUE;
	long idleTime =
		(shardIdleIntervalMillis > 0)
			? getCurrentTimeMillis() - shardIdleIntervalMillis
			: Long.MAX_VALUE;

	for (Map.Entry<Integer, ShardWatermarkState> e : shardWatermarks.entrySet()) {
		Watermark w = e.getValue().lastEmittedRecordWatermark;
		// consider only active shards, or those that would advance the watermark
		if (w != null && (e.getValue().lastUpdated >= idleTime
			|| e.getValue().emitQueue.getSize() > 0
			|| w.getTimestamp() > lastWatermark)) {
			potentialWatermark = Math.min(potentialWatermark, w.getTimestamp());
			// for sync, use the watermark of the next record, when available
			// otherwise watermark may stall when record is blocked by synchronization
			RecordEmitter.RecordQueue<RecordWrapper<T>> q = e.getValue().emitQueue;
			RecordWrapper<T> nextRecord = q.peek();
			Watermark nextWatermark = (nextRecord != null) ? nextRecord.watermark : w;
			potentialNextWatermark = Math.min(potentialNextWatermark, nextWatermark.getTimestamp());
		}
	}

	// advance watermark if possible (watermarks can only be ascending)
	if (potentialWatermark == Long.MAX_VALUE) {
		if (shardWatermarks.isEmpty() || shardIdleIntervalMillis > 0) {
			LOG.info("No active shard for subtask {}, marking the source idle.",
				indexOfThisConsumerSubtask);
			// no active shard, signal downstream operators to not wait for a watermark
			sourceContext.markAsTemporarilyIdle();
			isIdle = true;
		}
	} else {
		if (potentialWatermark > lastWatermark) {
			LOG.debug("Emitting watermark {} from subtask {}",
				potentialWatermark,
				indexOfThisConsumerSubtask);
			sourceContext.emitWatermark(new Watermark(potentialWatermark));
			lastWatermark = potentialWatermark;
			isIdle = false;
		}
		nextWatermark = potentialNextWatermark;
	}
}
 
Example 16
Source File: CoBroadcastWithNonKeyedOperator.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void processWatermark(Watermark mark) throws Exception {
	super.processWatermark(mark);
	currentWatermark = mark.getTimestamp();
}
 
Example 17
Source File: AbstractProcessStreamOperator.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void processWatermark(Watermark mark) throws Exception {
	super.processWatermark(mark);
	currentWatermark = mark.getTimestamp();
}
 
Example 18
Source File: StreamSink.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void processWatermark(Watermark mark) throws Exception {
	super.processWatermark(mark);
	this.currentWatermark = mark.getTimestamp();
}
 
Example 19
Source File: OneInputStreamOperatorTestHarness.java    From flink with Apache License 2.0 4 votes vote down vote up
public void processWatermark(Watermark mark) throws Exception {
	currentWatermark = mark.getTimestamp();
	oneInputOperator.processWatermark(mark);
}
 
Example 20
Source File: StreamSink.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void processWatermark(Watermark mark) throws Exception {
	super.processWatermark(mark);
	this.currentWatermark = mark.getTimestamp();
}