Example usage for org.joda.time Duration millis

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

Introduction

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

Prototype

public static Duration millis(long millis) 

Source Link

Document

Create a duration with the specified number of milliseconds.

Usage

From source file:com.datatorrent.benchmark.window.AbstractWindowedOperatorBenchmarkApp.java

License:Apache License

protected SpillableStateStore createStore(Configuration conf) {
    String basePath = getStoreBasePath(conf);
    ManagedTimeUnifiedStateSpillableStateStore store = new ManagedTimeUnifiedStateSpillableStateStore();
    store.setTimeBucketAssigner(new UnboundedTimeBucketAssigner());
    store.getTimeBucketAssigner().setBucketSpan(Duration.millis(10000));
    ((TFileImpl.DTFileImpl) store.getFileAccess()).setBasePath(basePath);

    return store;
}

From source file:com.example.dataflow.DataflowExampleUtils.java

License:Apache License

/**
 * If {@literal DataflowPipelineRunner} or {@literal BlockingDataflowPipelineRunner} is used,
 * waits for the pipeline to finish and cancels it (and the injector) before the program exists.
 *//* w w w .  java 2  s  .  c o  m*/
void waitToFinish(PipelineResult result) {
    if (result instanceof DataflowPipelineJob) {
        final DataflowPipelineJob job = (DataflowPipelineJob) result;
        jobsToCancel.add(job);
        if (!options.as(DataflowExampleOptions.class).getKeepJobsRunning()) {
            addShutdownHook(jobsToCancel);
        }
        try {
            job.waitUntilFinish(Duration.millis(-1L));
        } catch (Exception e) {
            throw new RuntimeException("Failed to wait for job to finish: " + job.getJobId());
        }
    } else {
        // Do nothing if the given PipelineResult doesn't support waitToFinish(),
        // such as EvaluationResults returned by DirectPipelineRunner.
    }
}

From source file:com.google.api.codegen.config.InterfaceConfig.java

License:Apache License

private static ImmutableMap<String, RetrySettings> createRetrySettingsDefinition(DiagCollector diagCollector,
        InterfaceConfigProto interfaceConfigProto) {
    ImmutableMap.Builder<String, RetrySettings> builder = ImmutableMap.<String, RetrySettings>builder();
    for (RetryParamsDefinitionProto retryDef : interfaceConfigProto.getRetryParamsDefList()) {
        try {/* w  w  w.j  av a  2 s. c o m*/
            RetrySettings settings = RetrySettings.newBuilder()
                    .setInitialRetryDelay(Duration.millis(retryDef.getInitialRetryDelayMillis()))
                    .setRetryDelayMultiplier(retryDef.getRetryDelayMultiplier())
                    .setMaxRetryDelay(Duration.millis(retryDef.getMaxRetryDelayMillis()))
                    .setInitialRpcTimeout(Duration.millis(retryDef.getInitialRpcTimeoutMillis()))
                    .setRpcTimeoutMultiplier(retryDef.getRpcTimeoutMultiplier())
                    .setMaxRpcTimeout(Duration.millis(retryDef.getMaxRpcTimeoutMillis()))
                    .setTotalTimeout(Duration.millis(retryDef.getTotalTimeoutMillis())).build();
            builder.put(retryDef.getName(), settings);
        } catch (IllegalStateException | NullPointerException e) {
            diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL,
                    "error while creating retry params: %s (in interface %s)", e,
                    interfaceConfigProto.getName()));
        }
    }
    if (diagCollector.getErrorCount() > 0) {
        return null;
    }
    return builder.build();
}

From source file:com.google.api.codegen.config.MethodConfig.java

License:Apache License

/**
 * Creates an instance of MethodConfig based on MethodConfigProto, linking it up with the provided
 * method. On errors, null will be returned, and diagnostics are reported to the diag collector.
 */// w w w . ja  v  a2 s.  c  om
@Nullable
public static MethodConfig createMethodConfig(DiagCollector diagCollector, String language,
        MethodConfigProto methodConfigProto, Method method, ResourceNameMessageConfigs messageConfigs,
        ImmutableMap<String, ResourceNameConfig> resourceNameConfigs,
        ImmutableSet<String> retryCodesConfigNames, ImmutableSet<String> retryParamsConfigNames) {

    boolean error = false;

    PageStreamingConfig pageStreaming = null;
    if (!PageStreamingConfigProto.getDefaultInstance().equals(methodConfigProto.getPageStreaming())) {
        pageStreaming = PageStreamingConfig.createPageStreaming(diagCollector, messageConfigs,
                resourceNameConfigs, methodConfigProto, method);
        if (pageStreaming == null) {
            error = true;
        }
    }

    GrpcStreamingConfig grpcStreaming = null;
    if (isGrpcStreamingMethod(method)) {
        if (PageStreamingConfigProto.getDefaultInstance().equals(methodConfigProto.getGrpcStreaming())) {
            grpcStreaming = GrpcStreamingConfig.createGrpcStreaming(diagCollector, method);
        } else {
            grpcStreaming = GrpcStreamingConfig.createGrpcStreaming(diagCollector,
                    methodConfigProto.getGrpcStreaming(), method);
            if (grpcStreaming == null) {
                error = true;
            }
        }
    }

    ImmutableList<FlatteningConfig> flattening = null;
    if (!FlatteningConfigProto.getDefaultInstance().equals(methodConfigProto.getFlattening())) {
        flattening = createFlattening(diagCollector, messageConfigs, resourceNameConfigs, methodConfigProto,
                method);
        if (flattening == null) {
            error = true;
        }
    }

    BundlingConfig bundling = null;
    if (!BundlingConfigProto.getDefaultInstance().equals(methodConfigProto.getBundling())) {
        bundling = BundlingConfig.createBundling(diagCollector, methodConfigProto.getBundling(), method);
        if (bundling == null) {
            error = true;
        }
    }

    String retryCodesName = methodConfigProto.getRetryCodesName();
    if (!retryCodesName.isEmpty() && !retryCodesConfigNames.contains(retryCodesName)) {
        diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL,
                "Retry codes config used but not defined: '%s' (in method %s)", retryCodesName,
                method.getFullName()));
        error = true;
    }

    String retryParamsName = methodConfigProto.getRetryParamsName();
    if (!retryParamsConfigNames.isEmpty() && !retryParamsConfigNames.contains(retryParamsName)) {
        diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL,
                "Retry parameters config used but not defined: %s (in method %s)", retryParamsName,
                method.getFullName()));
        error = true;
    }

    Duration timeout = Duration.millis(methodConfigProto.getTimeoutMillis());
    if (timeout.getMillis() <= 0) {
        diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL,
                "Default timeout not found or has invalid value (in method %s)", method.getFullName()));
        error = true;
    }

    boolean hasRequestObjectMethod = methodConfigProto.getRequestObjectMethod();

    ImmutableMap<String, String> fieldNamePatterns = ImmutableMap
            .copyOf(methodConfigProto.getFieldNamePatterns());

    ResourceNameTreatment defaultResourceNameTreatment = methodConfigProto.getResourceNameTreatment();
    if (defaultResourceNameTreatment == null
            || defaultResourceNameTreatment.equals(ResourceNameTreatment.UNSET_TREATMENT)) {
        defaultResourceNameTreatment = ResourceNameTreatment.NONE;
    }

    Iterable<FieldConfig> requiredFieldConfigs = createFieldNameConfigs(diagCollector, messageConfigs,
            defaultResourceNameTreatment, fieldNamePatterns, resourceNameConfigs,
            getRequiredFields(diagCollector, method, methodConfigProto.getRequiredFieldsList()));

    Iterable<FieldConfig> optionalFieldConfigs = createFieldNameConfigs(diagCollector, messageConfigs,
            defaultResourceNameTreatment, fieldNamePatterns, resourceNameConfigs,
            getOptionalFields(method, methodConfigProto.getRequiredFieldsList()));

    List<String> sampleCodeInitFields = new ArrayList<>();
    sampleCodeInitFields.addAll(methodConfigProto.getRequiredFieldsList());
    sampleCodeInitFields.addAll(methodConfigProto.getSampleCodeInitFieldsList());

    String rerouteToGrpcInterface = Strings.emptyToNull(methodConfigProto.getRerouteToGrpcInterface());

    VisibilityConfig visibility = VisibilityConfig.PUBLIC;
    for (SurfaceTreatmentProto treatment : methodConfigProto.getSurfaceTreatmentsList()) {
        if (!treatment.getIncludeLanguagesList().contains(language)) {
            continue;
        }
        if (treatment.getVisibility() != VisibilityProto.UNSET) {
            visibility = VisibilityConfig.fromProto(treatment.getVisibility());
        }
    }

    LongRunningConfig longRunningConfig = null;
    if (!LongRunningConfigProto.getDefaultInstance().equals(methodConfigProto.getLongRunning())) {
        longRunningConfig = LongRunningConfig.createLongRunningConfig(method.getModel(), diagCollector,
                methodConfigProto.getLongRunning());
        if (longRunningConfig == null) {
            error = true;
        }
    }

    if (error) {
        return null;
    } else {
        return new AutoValue_MethodConfig(method, pageStreaming, grpcStreaming, flattening, retryCodesName,
                retryParamsName, timeout, requiredFieldConfigs, optionalFieldConfigs,
                defaultResourceNameTreatment, bundling, hasRequestObjectMethod, fieldNamePatterns,
                sampleCodeInitFields, rerouteToGrpcInterface, visibility, longRunningConfig);
    }
}

From source file:com.google.api.codegen.MethodConfig.java

License:Apache License

/**
 * Creates an instance of MethodConfig based on MethodConfigProto, linking it up with the provided
 * method. On errors, null will be returned, and diagnostics are reported to the diag collector.
 *//*  w ww.j av a 2s  . c om*/
@Nullable
public static MethodConfig createMethodConfig(DiagCollector diagCollector,
        final MethodConfigProto methodConfigProto, Method method, ImmutableSet<String> retryCodesConfigNames,
        ImmutableSet<String> retryParamsConfigNames) {

    boolean error = false;

    PageStreamingConfig pageStreaming;
    if (PageStreamingConfigProto.getDefaultInstance().equals(methodConfigProto.getPageStreaming())) {
        pageStreaming = null;
    } else {
        pageStreaming = PageStreamingConfig.createPageStreaming(diagCollector,
                methodConfigProto.getPageStreaming(), method);
        if (pageStreaming == null) {
            error = true;
        }
    }

    FlatteningConfig flattening;
    if (FlatteningConfigProto.getDefaultInstance().equals(methodConfigProto.getFlattening())) {
        flattening = null;
    } else {
        flattening = FlatteningConfig.createFlattening(diagCollector, methodConfigProto.getFlattening(),
                method);
        if (flattening == null) {
            error = true;
        }
    }

    BundlingConfig bundling;
    if (BundlingConfigProto.getDefaultInstance().equals(methodConfigProto.getBundling())) {
        bundling = null;
    } else {
        bundling = BundlingConfig.createBundling(diagCollector, methodConfigProto.getBundling(), method);
        if (bundling == null) {
            error = true;
        }
    }

    String retryCodesName = methodConfigProto.getRetryCodesName();
    if (!retryCodesName.isEmpty() && !retryCodesConfigNames.contains(retryCodesName)) {
        diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL,
                "Retry codes config used but not defined: '%s' (in method %s)", retryCodesName,
                method.getFullName()));
        error = true;
    }

    String retryParamsName = methodConfigProto.getRetryParamsName();
    if (!retryParamsConfigNames.isEmpty() && !retryParamsConfigNames.contains(retryParamsName)) {
        diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL,
                "Retry parameters config used but not defined: %s (in method %s)", retryParamsName,
                method.getFullName()));
        error = true;
    }

    Duration timeout = Duration.millis(methodConfigProto.getTimeoutMillis());
    if (timeout.getMillis() <= 0) {
        diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL,
                "Default timeout not found or has invalid value (in method %s)", method.getFullName()));
        error = true;
    }

    boolean hasRequestObjectMethod = methodConfigProto.getRequestObjectMethod();

    List<String> requiredFieldNames = methodConfigProto.getRequiredFieldsList();
    ImmutableSet.Builder<Field> builder = ImmutableSet.builder();
    for (String fieldName : requiredFieldNames) {
        Field requiredField = method.getInputMessage().lookupField(fieldName);
        if (requiredField != null) {
            builder.add(requiredField);
        } else {
            Diag.error(SimpleLocation.TOPLEVEL, "Required field '%s' not found (in method %s)", fieldName,
                    method.getFullName());
            error = true;
        }
    }
    Set<Field> requiredFields = builder.build();

    Iterable<Field> optionalFields = Iterables.filter(method.getInputType().getMessageType().getFields(),
            new Predicate<Field>() {
                @Override
                public boolean apply(Field input) {
                    return !(methodConfigProto.getRequiredFieldsList().contains(input.getSimpleName()));
                }
            });

    ImmutableMap<String, String> fieldNamePatterns = ImmutableMap
            .copyOf(methodConfigProto.getFieldNamePatterns());

    List<String> sampleCodeInitFields = new ArrayList<>();
    sampleCodeInitFields.addAll(methodConfigProto.getRequiredFieldsList());
    sampleCodeInitFields.addAll(methodConfigProto.getSampleCodeInitFieldsList());

    String rerouteToGrpcInterface = methodConfigProto.getRerouteToGrpcInterface();
    if (Strings.isNullOrEmpty(rerouteToGrpcInterface)) {
        rerouteToGrpcInterface = null;
    }

    if (error) {
        return null;
    } else {
        return new MethodConfig(method, pageStreaming, flattening, retryCodesName, retryParamsName, timeout,
                bundling, hasRequestObjectMethod, requiredFields, optionalFields, fieldNamePatterns,
                sampleCodeInitFields, rerouteToGrpcInterface);
    }
}

From source file:com.google.cloud.bigquery.testing.RemoteBigQueryHelper.java

License:Open Source License

private static RetrySettings retrySettings() {
    return RetrySettings.newBuilder().setMaxAttempts(10).setMaxRetryDelay(Duration.millis(30000L))
            .setTotalTimeout(Duration.millis(120000L)).setInitialRetryDelay(Duration.millis(250L))
            .setRetryDelayMultiplier(1.0).setInitialRpcTimeout(Duration.millis(120000L))
            .setRpcTimeoutMultiplier(1.0).setMaxRpcTimeout(Duration.millis(120000L)).build();
}

From source file:com.google.cloud.dataflow.examples.common.DataflowExampleUtils.java

License:Apache License

/**
 * Sets up external resources that are required by the example,
 * such as Pub/Sub topics and BigQuery tables.
 *
 * @throws IOException if there is a problem setting up the resources
 *//*from w  w  w . j  a va2 s. com*/
public void setup() throws IOException {
    Sleeper sleeper = Sleeper.DEFAULT;
    BackOff backOff = FluentBackoff.DEFAULT.withMaxRetries(3).withInitialBackoff(Duration.millis(200))
            .backoff();
    Throwable lastException = null;
    try {
        do {
            try {
                setupPubsub();
                setupBigQueryTable();
                return;
            } catch (GoogleJsonResponseException e) {
                lastException = e;
            }
        } while (BackOffUtils.next(sleeper, backOff));
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        // Ignore InterruptedException
    }
    throw new RuntimeException(lastException);
}

From source file:com.google.cloud.dataflow.sdk.coders.DurationCoder.java

License:Apache License

private ReadableDuration fromLong(Long decoded) {
    return Duration.millis(decoded);
}

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

License:Apache License

/**
 * Waits for the job to finish and return the final status.
 *
 * @param timeToWait The time to wait in units timeUnit for the job to finish.
 *     Provide a value less than 1 ms for an infinite wait.
 * @param timeUnit The unit of time for timeToWait.
 * @param messageHandler If non null this handler will be invoked for each
 *   batch of messages received./*from  ww  w  .j  a  v a 2  s  .  com*/
 * @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
public State waitToFinish(long timeToWait, TimeUnit timeUnit, MonitoringUtil.JobMessagesHandler messageHandler)
        throws IOException, InterruptedException {
    Duration duration = Duration.millis(timeUnit.toMillis(timeToWait));
    return waitToFinish(duration, messageHandler, Sleeper.DEFAULT, NanoClock.SYSTEM);
}

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 w  w. ja v a  2s . co  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.
}