Java Code Examples for com.google.api.client.util.NanoClock#nanoTime()
The following examples show how to use
com.google.api.client.util.NanoClock#nanoTime() .
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: DataflowPipelineJob.java From beam with Apache License 2.0 | 6 votes |
/** * Reset backoff. If duration is limited, calculate time remaining, otherwise just reset retry * count. * * <p>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. */ private static BackOff resetBackoff(Duration duration, NanoClock nanoClock, long startNanos) { BackOff backoff; 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 = getMessagesBackoff(remaining); } else { backoff = BackOff.STOP_BACKOFF; } } else { backoff = getMessagesBackoff(duration); } return backoff; }
Example 2
Source File: DataflowPipelineJob.java From beam with Apache License 2.0 | 4 votes |
/** * Waits until the pipeline finishes and returns the final status. * * @param duration The 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. * @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. * @throws IOException If there is a persistent problem getting job information. * @throws InterruptedException if the thread is interrupted. */ @Nullable @VisibleForTesting State waitUntilFinish( Duration duration, @Nullable MonitoringUtil.JobMessagesHandler messageHandler, Sleeper sleeper, NanoClock nanoClock, MonitoringUtil monitor) throws IOException, InterruptedException { BackOff backoff = getMessagesBackoff(duration); // 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 = State.UNKNOWN; Exception exception; do { exception = null; try { // 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( BackOffAdapter.toGcpBackOff(STATUS_BACKOFF_FACTORY.withMaxRetries(0).backoff()), sleeper); } catch (IOException e) { exception = e; LOG.warn("Failed to get job state: {}", e.getMessage()); LOG.debug("Failed to get job state: {}", e); continue; } exception = processJobMessages(messageHandler, monitor); if (exception != null) { continue; } // We can stop if the job is done. if (state.isTerminal()) { logTerminalState(state); return state; } // Reset attempts count and update cumulative wait time. backoff = resetBackoff(duration, nanoClock, startNanos); } while (BackOffUtils.next(sleeper, backoff)); // At this point Backoff decided that we retried enough times. // This can be either due to exceeding allowed timeout for job to complete, or receiving // error multiple times in a row. if (exception == null) { LOG.warn("No terminal state was returned within allotted timeout. State value {}", state); } else { LOG.error("Failed to fetch job metadata with error: {}", exception); } return null; }