use of org.apache.beam.sdk.io.gcp.spanner.changestreams.TimestampConverter in project beam by apache.
the class ReadChangeStreamPartitionDoFn method initialRestriction.
/**
* The restriction for a partition will be defined from the start and end timestamp to query the
* partition for. These timestamps are converted to microseconds. The {@link OffsetRange}
* restriction represents a closed-open interval, while the start / end timestamps represent a
* closed-closed interval, so we add 1 microsecond to the end timestamp to convert it to
* closed-open.
*
* <p>In this function we also update the partition state to {@link
* PartitionMetadata.State#RUNNING}.
*
* @param partition the partition to be queried
* @return the offset range from the partition start timestamp to the partition end timestamp + 1
* microsecond
*/
@GetInitialRestriction
public OffsetRange initialRestriction(@Element PartitionMetadata partition) {
final String token = partition.getPartitionToken();
final com.google.cloud.Timestamp startTimestamp = partition.getStartTimestamp();
final long startMicros = TimestampConverter.timestampToMicros(startTimestamp);
// Offset range represents closed-open interval
final long endMicros = Optional.ofNullable(partition.getEndTimestamp()).map(TimestampConverter::timestampToMicros).map(micros -> micros + 1).orElse(TimestampConverter.MAX_MICROS + 1);
final com.google.cloud.Timestamp partitionScheduledAt = partition.getScheduledAt();
final com.google.cloud.Timestamp partitionRunningAt = daoFactory.getPartitionMetadataDao().updateToRunning(token);
if (partitionScheduledAt != null && partitionRunningAt != null) {
metrics.updatePartitionScheduledToRunning(new Duration(partitionScheduledAt.toSqlTimestamp().getTime(), partitionRunningAt.toSqlTimestamp().getTime()));
}
return new OffsetRange(startMicros, endMicros);
}
Aggregations