Search in sources :

Example 1 with TimestampConverter

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);
}
Also used : AttributeValue(io.opencensus.trace.AttributeValue) DaoFactory(org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.DaoFactory) Manual(org.apache.beam.sdk.transforms.splittabledofn.WatermarkEstimators.Manual) ChangeStreamMetrics(org.apache.beam.sdk.io.gcp.spanner.changestreams.ChangeStreamMetrics) PartitionMetadataDao(org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataDao) Duration(org.joda.time.Duration) LoggerFactory(org.slf4j.LoggerFactory) PARTITION_ID_ATTRIBUTE_LABEL(org.apache.beam.sdk.io.gcp.spanner.changestreams.ChangeStreamMetrics.PARTITION_ID_ATTRIBUTE_LABEL) TimestampConverter(org.apache.beam.sdk.io.gcp.spanner.changestreams.TimestampConverter) DataChangeRecord(org.apache.beam.sdk.io.gcp.spanner.changestreams.model.DataChangeRecord) DataChangeRecordAction(org.apache.beam.sdk.io.gcp.spanner.changestreams.action.DataChangeRecordAction) QueryChangeStreamAction(org.apache.beam.sdk.io.gcp.spanner.changestreams.action.QueryChangeStreamAction) Tracing(io.opencensus.trace.Tracing) RestrictionTracker(org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker) DoFn(org.apache.beam.sdk.transforms.DoFn) Tracer(io.opencensus.trace.Tracer) ChangeStreamDao(org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.ChangeStreamDao) Logger(org.slf4j.Logger) HeartbeatRecordAction(org.apache.beam.sdk.io.gcp.spanner.changestreams.action.HeartbeatRecordAction) Scope(io.opencensus.common.Scope) UnboundedPerElement(org.apache.beam.sdk.transforms.DoFn.UnboundedPerElement) ManualWatermarkEstimator(org.apache.beam.sdk.transforms.splittabledofn.ManualWatermarkEstimator) Serializable(java.io.Serializable) ChildPartitionsRecordAction(org.apache.beam.sdk.io.gcp.spanner.changestreams.action.ChildPartitionsRecordAction) PartitionMetadata(org.apache.beam.sdk.io.gcp.spanner.changestreams.model.PartitionMetadata) Instant(org.joda.time.Instant) ReadChangeStreamPartitionRangeTracker(org.apache.beam.sdk.io.gcp.spanner.changestreams.restriction.ReadChangeStreamPartitionRangeTracker) Optional(java.util.Optional) PartitionMetadataMapper(org.apache.beam.sdk.io.gcp.spanner.changestreams.mapper.PartitionMetadataMapper) ActionFactory(org.apache.beam.sdk.io.gcp.spanner.changestreams.action.ActionFactory) MapperFactory(org.apache.beam.sdk.io.gcp.spanner.changestreams.mapper.MapperFactory) OffsetRange(org.apache.beam.sdk.io.range.OffsetRange) ChangeStreamRecordMapper(org.apache.beam.sdk.io.gcp.spanner.changestreams.mapper.ChangeStreamRecordMapper) OffsetRange(org.apache.beam.sdk.io.range.OffsetRange) TimestampConverter(org.apache.beam.sdk.io.gcp.spanner.changestreams.TimestampConverter) Duration(org.joda.time.Duration)

Aggregations

Scope (io.opencensus.common.Scope)1 AttributeValue (io.opencensus.trace.AttributeValue)1 Tracer (io.opencensus.trace.Tracer)1 Tracing (io.opencensus.trace.Tracing)1 Serializable (java.io.Serializable)1 Optional (java.util.Optional)1 ChangeStreamMetrics (org.apache.beam.sdk.io.gcp.spanner.changestreams.ChangeStreamMetrics)1 PARTITION_ID_ATTRIBUTE_LABEL (org.apache.beam.sdk.io.gcp.spanner.changestreams.ChangeStreamMetrics.PARTITION_ID_ATTRIBUTE_LABEL)1 TimestampConverter (org.apache.beam.sdk.io.gcp.spanner.changestreams.TimestampConverter)1 ActionFactory (org.apache.beam.sdk.io.gcp.spanner.changestreams.action.ActionFactory)1 ChildPartitionsRecordAction (org.apache.beam.sdk.io.gcp.spanner.changestreams.action.ChildPartitionsRecordAction)1 DataChangeRecordAction (org.apache.beam.sdk.io.gcp.spanner.changestreams.action.DataChangeRecordAction)1 HeartbeatRecordAction (org.apache.beam.sdk.io.gcp.spanner.changestreams.action.HeartbeatRecordAction)1 QueryChangeStreamAction (org.apache.beam.sdk.io.gcp.spanner.changestreams.action.QueryChangeStreamAction)1 ChangeStreamDao (org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.ChangeStreamDao)1 DaoFactory (org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.DaoFactory)1 PartitionMetadataDao (org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataDao)1 ChangeStreamRecordMapper (org.apache.beam.sdk.io.gcp.spanner.changestreams.mapper.ChangeStreamRecordMapper)1 MapperFactory (org.apache.beam.sdk.io.gcp.spanner.changestreams.mapper.MapperFactory)1 PartitionMetadataMapper (org.apache.beam.sdk.io.gcp.spanner.changestreams.mapper.PartitionMetadataMapper)1