Java Code Examples for io.airlift.units.Duration#toMillis()

The following examples show how to use io.airlift.units.Duration#toMillis() . 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: IterativeOptimizer.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public PlanNode optimize(PlanNode plan, Session session, TypeProvider types, SymbolAllocator symbolAllocator, PlanNodeIdAllocator idAllocator, WarningCollector warningCollector)
{
    // only disable new rules if we have legacy rules to fall back to
    if (useLegacyRules.test(session) && !legacyRules.isEmpty()) {
        for (PlanOptimizer optimizer : legacyRules) {
            plan = optimizer.optimize(plan, session, symbolAllocator.getTypes(), symbolAllocator, idAllocator, warningCollector);
        }

        return plan;
    }

    Memo memo = new Memo(idAllocator, plan);
    Lookup lookup = Lookup.from(planNode -> Stream.of(memo.resolve(planNode)));

    Duration timeout = SystemSessionProperties.getOptimizerTimeout(session);
    Context context = new Context(memo, lookup, idAllocator, symbolAllocator, System.nanoTime(), timeout.toMillis(), session, warningCollector);
    exploreGroup(memo.getRootGroup(), context);

    return memo.extract();
}
 
Example 2
Source File: CachingHiveMetastore.java    From presto with Apache License 2.0 6 votes vote down vote up
public static HiveMetastore cachingHiveMetastore(HiveMetastore delegate, Executor executor, Duration cacheTtl, Optional<Duration> refreshInterval, long maximumSize)
{
    if (cacheTtl.toMillis() == 0 || maximumSize == 0) {
        // caching is disabled
        return delegate;
    }

    return new CachingHiveMetastore(
            delegate,
            executor,
            OptionalLong.of(cacheTtl.toMillis()),
            refreshInterval
                    .map(Duration::toMillis)
                    .map(OptionalLong::of)
                    .orElseGet(OptionalLong::empty),
            maximumSize);
}
 
Example 3
Source File: DriftNettyMethodInvoker.java    From drift with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
DriftNettyMethodInvoker(
        ConnectionParameters connectionParameters,
        ConnectionManager connectionManager,
        ScheduledExecutorService delayService,
        Duration invocationTimeoutGracePeriod)
{
    this.connectionParameters = requireNonNull(connectionParameters, "connectionConfig is null");
    this.connectionManager = requireNonNull(connectionManager, "connectionManager is null");
    this.delayService = listeningDecorator(requireNonNull(delayService, "delayService is null"));

    // an invocation should complete long before this
    this.invokeTimeout = new Duration(
            invocationTimeoutGracePeriod.toMillis() +
                    connectionParameters.getConnectTimeout().toMillis() +
                    connectionParameters.getRequestTimeout().toMillis(),
            MILLISECONDS);
}
 
Example 4
Source File: TaskSystemTable.java    From presto with Apache License 2.0 5 votes vote down vote up
private static Long toMillis(Duration duration)
{
    if (duration == null) {
        return null;
    }
    return duration.toMillis();
}
 
Example 5
Source File: QuerySystemTable.java    From presto with Apache License 2.0 5 votes vote down vote up
private static Long toMillis(Duration duration)
{
    if (duration == null) {
        return null;
    }
    return duration.toMillis();
}
 
Example 6
Source File: TaskResource.java    From presto with Apache License 2.0 5 votes vote down vote up
@ResourceSecurity(INTERNAL_ONLY)
@GET
@Path("{taskId}")
@Produces(MediaType.APPLICATION_JSON)
public void getTaskInfo(
        @PathParam("taskId") final TaskId taskId,
        @HeaderParam(PRESTO_CURRENT_STATE) TaskState currentState,
        @HeaderParam(PRESTO_MAX_WAIT) Duration maxWait,
        @Context UriInfo uriInfo,
        @Suspended AsyncResponse asyncResponse)
{
    requireNonNull(taskId, "taskId is null");

    if (currentState == null || maxWait == null) {
        TaskInfo taskInfo = taskManager.getTaskInfo(taskId);
        if (shouldSummarize(uriInfo)) {
            taskInfo = taskInfo.summarize();
        }
        asyncResponse.resume(taskInfo);
        return;
    }

    Duration waitTime = randomizeWaitTime(maxWait);
    ListenableFuture<TaskInfo> futureTaskInfo = addTimeout(
            taskManager.getTaskInfo(taskId, currentState),
            () -> taskManager.getTaskInfo(taskId),
            waitTime,
            timeoutExecutor);

    if (shouldSummarize(uriInfo)) {
        futureTaskInfo = Futures.transform(futureTaskInfo, TaskInfo::summarize, directExecutor());
    }

    // For hard timeout, add an additional time to max wait for thread scheduling contention and GC
    Duration timeout = new Duration(waitTime.toMillis() + ADDITIONAL_WAIT_TIME.toMillis(), MILLISECONDS);
    bindAsyncResponse(asyncResponse, futureTaskInfo, responseExecutor)
            .withTimeout(timeout);
}
 
Example 7
Source File: TaskResource.java    From presto with Apache License 2.0 5 votes vote down vote up
@ResourceSecurity(INTERNAL_ONLY)
@GET
@Path("{taskId}/status")
@Produces(MediaType.APPLICATION_JSON)
public void getTaskStatus(
        @PathParam("taskId") TaskId taskId,
        @HeaderParam(PRESTO_CURRENT_STATE) TaskState currentState,
        @HeaderParam(PRESTO_MAX_WAIT) Duration maxWait,
        @Context UriInfo uriInfo,
        @Suspended AsyncResponse asyncResponse)
{
    requireNonNull(taskId, "taskId is null");

    if (currentState == null || maxWait == null) {
        TaskStatus taskStatus = taskManager.getTaskStatus(taskId);
        asyncResponse.resume(taskStatus);
        return;
    }

    Duration waitTime = randomizeWaitTime(maxWait);
    // TODO: With current implementation, a newly completed driver group won't trigger immediate HTTP response,
    // leading to a slight delay of approx 1 second, which is not a major issue for any query that are heavy weight enough
    // to justify group-by-group execution. In order to fix this, REST endpoint /v1/{task}/status will need change.
    ListenableFuture<TaskStatus> futureTaskStatus = addTimeout(
            taskManager.getTaskStatus(taskId, currentState),
            () -> taskManager.getTaskStatus(taskId),
            waitTime,
            timeoutExecutor);

    // For hard timeout, add an additional time to max wait for thread scheduling contention and GC
    Duration timeout = new Duration(waitTime.toMillis() + ADDITIONAL_WAIT_TIME.toMillis(), MILLISECONDS);
    bindAsyncResponse(asyncResponse, futureTaskStatus, responseExecutor)
            .withTimeout(timeout);
}
 
Example 8
Source File: FileBasedSystemAccessControl.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public SystemAccessControl create(Map<String, String> config)
{
    requireNonNull(config, "config is null");

    String configFileName = config.get(SECURITY_CONFIG_FILE);
    checkState(configFileName != null, "Security configuration must contain the '%s' property", SECURITY_CONFIG_FILE);

    if (config.containsKey(SECURITY_REFRESH_PERIOD)) {
        Duration refreshPeriod;
        try {
            refreshPeriod = Duration.valueOf(config.get(SECURITY_REFRESH_PERIOD));
        }
        catch (IllegalArgumentException e) {
            throw invalidRefreshPeriodException(config, configFileName);
        }
        if (refreshPeriod.toMillis() == 0) {
            throw invalidRefreshPeriodException(config, configFileName);
        }
        return ForwardingSystemAccessControl.of(memoizeWithExpiration(
                () -> {
                    log.info("Refreshing system access control from %s", configFileName);
                    return create(configFileName);
                },
                refreshPeriod.toMillis(),
                MILLISECONDS));
    }
    return create(configFileName);
}
 
Example 9
Source File: FormatUtils.java    From presto with Apache License 2.0 5 votes vote down vote up
public static String formatFinalTime(Duration duration)
{
    long totalMillis = duration.toMillis();

    if (totalMillis >= MINUTES.toMillis(1)) {
        return formatTime(duration);
    }

    return format("%.2f", (totalMillis / 1000.0));
}
 
Example 10
Source File: TimeoutUtil.java    From yanagishima with Apache License 2.0 5 votes vote down vote up
public static void checkTimeout(TinyORM db, Duration queryMaxRunTime, long start, String datasource, String engine, String queryId, String query, String user) {
    if (System.currentTimeMillis() - start > queryMaxRunTime.toMillis()) {
        String message = format("Query failed (#%s) in %s: Query exceeded maximum time limit of %s", queryId, datasource, queryMaxRunTime.toString());
        storeError(db, datasource, engine, queryId, query, user, message);
        throw new RuntimeException(message);
    }
}
 
Example 11
Source File: TaskResource.java    From presto with Apache License 2.0 4 votes vote down vote up
@ResourceSecurity(INTERNAL_ONLY)
@GET
@Path("{taskId}/results/{bufferId}/{token}")
@Produces(PRESTO_PAGES)
public void getResults(
        @PathParam("taskId") TaskId taskId,
        @PathParam("bufferId") OutputBufferId bufferId,
        @PathParam("token") final long token,
        @HeaderParam(PRESTO_MAX_SIZE) DataSize maxSize,
        @Suspended AsyncResponse asyncResponse)
{
    requireNonNull(taskId, "taskId is null");
    requireNonNull(bufferId, "bufferId is null");

    long start = System.nanoTime();
    ListenableFuture<BufferResult> bufferResultFuture = taskManager.getTaskResults(taskId, bufferId, token, maxSize);
    Duration waitTime = randomizeWaitTime(DEFAULT_MAX_WAIT_TIME);
    bufferResultFuture = addTimeout(
            bufferResultFuture,
            () -> BufferResult.emptyResults(taskManager.getTaskInstanceId(taskId), token, false),
            waitTime,
            timeoutExecutor);

    ListenableFuture<Response> responseFuture = Futures.transform(bufferResultFuture, result -> {
        List<SerializedPage> serializedPages = result.getSerializedPages();

        GenericEntity<?> entity = null;
        Status status;
        if (serializedPages.isEmpty()) {
            status = Status.NO_CONTENT;
        }
        else {
            entity = new GenericEntity<>(serializedPages, new TypeToken<List<SerializedPage>>() {}.getType());
            status = Status.OK;
        }

        return Response.status(status)
                .entity(entity)
                .header(PRESTO_TASK_INSTANCE_ID, result.getTaskInstanceId())
                .header(PRESTO_PAGE_TOKEN, result.getToken())
                .header(PRESTO_PAGE_NEXT_TOKEN, result.getNextToken())
                .header(PRESTO_BUFFER_COMPLETE, result.isBufferComplete())
                .build();
    }, directExecutor());

    // For hard timeout, add an additional time to max wait for thread scheduling contention and GC
    Duration timeout = new Duration(waitTime.toMillis() + ADDITIONAL_WAIT_TIME.toMillis(), MILLISECONDS);
    bindAsyncResponse(asyncResponse, responseFuture, responseExecutor)
            .withTimeout(timeout,
                    Response.status(Status.NO_CONTENT)
                            .header(PRESTO_TASK_INSTANCE_ID, taskManager.getTaskInstanceId(taskId))
                            .header(PRESTO_PAGE_TOKEN, token)
                            .header(PRESTO_PAGE_NEXT_TOKEN, token)
                            .header(PRESTO_BUFFER_COMPLETE, false)
                            .build());

    responseFuture.addListener(() -> readFromOutputBufferTime.add(Duration.nanosSince(start)), directExecutor());
    asyncResponse.register((CompletionCallback) throwable -> resultsRequestTime.add(Duration.nanosSince(start)));
}
 
Example 12
Source File: TaskResource.java    From presto with Apache License 2.0 4 votes vote down vote up
private static Duration randomizeWaitTime(Duration waitTime)
{
    // Randomize in [T/2, T], so wait is not near zero and the client-supplied max wait time is respected
    long halfWaitMillis = waitTime.toMillis() / 2;
    return new Duration(halfWaitMillis + ThreadLocalRandom.current().nextLong(halfWaitMillis), MILLISECONDS);
}
 
Example 13
Source File: ShardCleaner.java    From presto with Apache License 2.0 4 votes vote down vote up
private static Timestamp maxTimestamp(Duration duration)
{
    return new Timestamp(System.currentTimeMillis() - duration.toMillis());
}
 
Example 14
Source File: KinesisRecordSet.java    From presto with Apache License 2.0 4 votes vote down vote up
/**
 * Retrieves the next batch of records from Kinesis using the shard iterator.
 * <p>
 * Most of the time this results in one getRecords call.  However we allow for
 * a call to return an empty list, and we'll try again if we are far enough
 * away from the latest record.
 */
private void getKinesisRecords()
        throws ResourceNotFoundException
{
    // Normally this loop will execute once, but we have to allow for the odd Kinesis
    // behavior, per the docs:
    // A single call to getRecords might return an empty record list, even when the shard contains
    // more records at later sequence numbers
    boolean fetchedRecords = false;
    int attempts = 0;
    while (!fetchedRecords && attempts < fetchAttempts) {
        Duration duration = nanosSince(lastReadTime);
        if (duration.toMillis() <= sleepTime) {
            try {
                Thread.sleep(duration.toMillis());
            }
            catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new RuntimeException("thread interrupted");
            }
        }
        getRecordsRequest = new GetRecordsRequest();
        getRecordsRequest.setShardIterator(shardIterator);
        getRecordsRequest.setLimit(batchSize);

        getRecordsResult = clientManager.getClient().getRecords(getRecordsRequest);
        lastReadTime = System.nanoTime();

        shardIterator = getRecordsResult.getNextShardIterator();
        kinesisRecords = getRecordsResult.getRecords();
        if (isLogBatches) {
            log.info("Fetched %d records from Kinesis.  MillisBehindLatest=%d", kinesisRecords.size(), getRecordsResult.getMillisBehindLatest());
        }

        fetchedRecords = (kinesisRecords.size() > 0 || getMillisBehindLatest() <= MILLIS_BEHIND_LIMIT);
        attempts++;
    }

    listIterator = kinesisRecords.iterator();
    batchesRead++;
    messagesRead += kinesisRecords.size();
}