Example usage for org.joda.time Duration ZERO

List of usage examples for org.joda.time Duration ZERO

Introduction

In this page you can find the example usage for org.joda.time Duration ZERO.

Prototype

Duration ZERO

To view the source code for org.joda.time Duration ZERO.

Click Source Link

Document

Constant representing zero millisecond duration

Usage

From source file:com.facebook.stats.AbstractCompositeCounter.java

License:Apache License

/**
 * Updates the current composite counter so that it is up to date with the
 * current timestamp./*  w w  w  .  ja v  a 2  s . c  o  m*/
 * <p/>
 * This should be called by any method that needs to have the most updated
 * view of the current set of counters.
 */
protected synchronized void trimIfNeeded() {
    Duration delta = new Duration(start, new DateTime()).minus(maxLength);

    if (delta.isLongerThan(Duration.ZERO)) {
        start = start.toDateTime().plus(delta);

        if (start.isAfter(end)) {
            end = start;
        }

        Iterator<C> iter = eventCounters.iterator();

        while (iter.hasNext()) {
            EventCounterIf<C> counter = iter.next();

            // trim any counter with an end up to and including start since our composite counter is
            // [start, ... and each counter is [..., end)
            if (!start.isBefore(counter.getEnd())) {
                iter.remove();
            } else {
                break;
            }
        }
    }
}

From source file:com.google.cloud.dataflow.sdk.io.CountingInput.java

License:Apache License

/**
 * Creates an {@link UnboundedCountingInput} that will produce numbers starting from {@code 0} up
 * to {@link Long#MAX_VALUE}.// www . j  ava  2 s. co  m
 *
 * <p>After {@link Long#MAX_VALUE}, the transform never produces more output. (In practice, this
 * limit should never be reached.)
 *
 * <p>Elements in the resulting {@link PCollection PCollection&lt;Long&gt;} will by default have
 * timestamps corresponding to processing time at element generation, provided by
 * {@link Instant#now}. Use the transform returned by
 * {@link UnboundedCountingInput#withTimestampFn(SerializableFunction)} to control the output
 * timestamps.
 */
public static UnboundedCountingInput unbounded() {
    return new UnboundedCountingInput(new NowTimestampFn(), 1L /* Elements per period */,
            Duration.ZERO /* Period length */, Optional.<Long>absent() /* Maximum number of records */,
            Optional.<Duration>absent() /* Maximum read duration */);
}

From source file:com.google.cloud.dataflow.sdk.io.CountingSource.java

License:Apache License

/**
 * Create a new {@link UnboundedCountingSource}.
 *///w  ww.j  ava  2s.  c  om
// package-private to return a typed UnboundedCountingSource rather than the UnboundedSource type.
static UnboundedCountingSource createUnbounded() {
    return new UnboundedCountingSource(0, 1, 1L, Duration.ZERO, new NowTimestampFn());
}

From source file:com.google.cloud.dataflow.sdk.io.CountingSource.java

License:Apache License

/**
 * Creates an {@link UnboundedSource} that will produce numbers starting from {@code 0} up to
 * {@link Long#MAX_VALUE}, with element timestamps supplied by the specified function.
 *
 * <p>After {@link Long#MAX_VALUE}, the source never produces more output. (In practice, this
 * limit should never be reached.)/*from   w  ww.j  av a  2 s. com*/
 *
 * <p>Note that the timestamps produced by {@code timestampFn} may not decrease.
 *
 * @deprecated use {@link CountingInput#unbounded()} and call
 *             {@link UnboundedCountingInput#withTimestampFn(SerializableFunction)} instead
 */
@Deprecated
public static UnboundedSource<Long, CounterMark> unboundedWithTimestampFn(
        SerializableFunction<Long, Instant> timestampFn) {
    return new UnboundedCountingSource(0, 1, 1L, Duration.ZERO, timestampFn);
}

From source file:com.google.cloud.dataflow.sdk.runners.DataflowPipelineJob.java

License:Apache License

/**
 * Wait for the job to finish and return the final status.
 *
 * @param duration The total time to wait for the job to finish.
 *     Provide a value less than 1 ms for an infinite wait.
 * @param messageHandler If non null this handler will be invoked for each
 *   batch of messages received.//from  w  ww  .  java  2s  . c  o m
 * @param sleeper A sleeper to use to sleep between attempts.
 * @param nanoClock A nanoClock used to time the total time taken.
 * @return The final state of the job or null on timeout or if the
 *   thread is interrupted.
 * @throws IOException If there is a persistent problem getting job
 *   information.
 * @throws InterruptedException
 */
@Nullable
@VisibleForTesting
State waitToFinish(Duration duration, MonitoringUtil.JobMessagesHandler messageHandler, Sleeper sleeper,
        NanoClock nanoClock) throws IOException, InterruptedException {
    MonitoringUtil monitor = new MonitoringUtil(projectId, dataflowClient);

    long lastTimestamp = 0;
    BackOff backoff;
    if (!duration.isLongerThan(Duration.ZERO)) {
        backoff = MESSAGES_BACKOFF_FACTORY.backoff();
    } else {
        backoff = MESSAGES_BACKOFF_FACTORY.withMaxCumulativeBackoff(duration).backoff();
    }

    // This function tracks the cumulative time from the *first request* to enforce the wall-clock
    // limit. Any backoff instance could, at best, track the the time since the first attempt at a
    // given request. Thus, we need to track the cumulative time ourselves.
    long startNanos = nanoClock.nanoTime();

    State state;
    do {
        // Get the state of the job before listing messages. This ensures we always fetch job
        // messages after the job finishes to ensure we have all them.
        state = getStateWithRetries(STATUS_BACKOFF_FACTORY.withMaxRetries(0).backoff(), sleeper);
        boolean hasError = state == State.UNKNOWN;

        if (messageHandler != null && !hasError) {
            // Process all the job messages that have accumulated so far.
            try {
                List<JobMessage> allMessages = monitor.getJobMessages(jobId, lastTimestamp);

                if (!allMessages.isEmpty()) {
                    lastTimestamp = fromCloudTime(allMessages.get(allMessages.size() - 1).getTime())
                            .getMillis();
                    messageHandler.process(allMessages);
                }
            } catch (GoogleJsonResponseException | SocketTimeoutException e) {
                hasError = true;
                LOG.warn("There were problems getting current job messages: {}.", e.getMessage());
                LOG.debug("Exception information:", e);
            }
        }

        if (!hasError) {
            // We can stop if the job is done.
            if (state.isTerminal()) {
                return state;
            }

            // The job is not done, so we must keep polling.
            backoff.reset();

            // If a total duration for all backoff has been set, update the new cumulative sleep time to
            // be the remaining total backoff duration, stopping if we have already exceeded the
            // allotted time.
            if (duration.isLongerThan(Duration.ZERO)) {
                long nanosConsumed = nanoClock.nanoTime() - startNanos;
                Duration consumed = Duration.millis((nanosConsumed + 999999) / 1000000);
                Duration remaining = duration.minus(consumed);
                if (remaining.isLongerThan(Duration.ZERO)) {
                    backoff = MESSAGES_BACKOFF_FACTORY.withMaxCumulativeBackoff(remaining).backoff();
                } else {
                    // If there is no time remaining, don't bother backing off.
                    backoff = BackOff.STOP_BACKOFF;
                }
            }
        }
    } while (BackOffUtils.next(sleeper, backoff));
    LOG.warn("No terminal state was returned.  State value {}", state);
    return null; // Timed out.
}

From source file:com.google.cloud.dataflow.sdk.transforms.DoFn.java

License:Apache License

/**
 * Returns the allowed timestamp skew duration, which is the maximum
 * duration that timestamps can be shifted backward in
 * {@link DoFn.Context#outputWithTimestamp}.
 *
 * <p>The default value is {@code Duration.ZERO}, in which case
 * timestamps can only be shifted forward to future.  For infinite
 * skew, return {@code Duration.millis(Long.MAX_VALUE)}.
 *
 * <p> Note that producing an element whose timestamp is less than the
 * current timestamp may result in late data, i.e. returning a non-zero
 * value here does not impact watermark calculations used for firing
 * windows.//from  www . ja  v a 2 s .c  o m
 *
 * @deprecated does not interact well with the watermark.
 */
@Deprecated
public Duration getAllowedTimestampSkew() {
    return Duration.ZERO;
}

From source file:com.google.cloud.dataflow.sdk.transforms.DoFnWithContext.java

License:Apache License

/**
 * Returns the allowed timestamp skew duration, which is the maximum
 * duration that timestamps can be shifted backward in
 * {@link DoFnWithContext.Context#outputWithTimestamp}.
 *
 * <p>The default value is {@code Duration.ZERO}, in which case
 * timestamps can only be shifted forward to future.  For infinite
 * skew, return {@code Duration.millis(Long.MAX_VALUE)}.
 *///from ww w.  jav a 2 s .c  o  m
public Duration getAllowedTimestampSkew() {
    return Duration.ZERO;
}

From source file:com.google.cloud.dataflow.sdk.transforms.windowing.FixedWindows.java

License:Apache License

/**
 * Partitions the timestamp space into half-open intervals of the form
 * [N * size, (N + 1) * size), where 0 is the epoch.
 *//*from   ww  w.java2 s  .  c o m*/
public static FixedWindows of(Duration size) {
    return new FixedWindows(size, Duration.ZERO);
}

From source file:com.google.cloud.dataflow.sdk.transforms.windowing.FixedWindows.java

License:Apache License

private FixedWindows(Duration size, Duration offset) {
    if (offset.isShorterThan(Duration.ZERO) || !offset.isShorterThan(size)) {
        throw new IllegalArgumentException("FixedWindows WindowingStrategies must have 0 <= offset < size");
    }/*from   w  ww . j a  va  2s  .co m*/
    this.size = size;
    this.offset = offset;
}

From source file:com.google.cloud.dataflow.sdk.transforms.windowing.FixedWindows.java

License:Apache License

@Override
public void populateDisplayData(DisplayData.Builder builder) {
    super.populateDisplayData(builder);
    builder.add(DisplayData.item("size", size).withLabel("Window Duration")).addIfNotDefault(
            DisplayData.item("offset", offset).withLabel("Window Start Offset"), Duration.ZERO);
}