summaryrefslogtreecommitdiffstats
path: root/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/OrchestratorContext.java
blob: 6577b4b96cc1f5f4358a5641000d6df60ed33802 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.orchestrator;

import com.google.common.util.concurrent.UncheckedTimeoutException;
import com.yahoo.time.TimeBudget;

import java.time.Clock;
import java.time.Duration;

/**
 * Context for the Orchestrator, e.g. timeout management.
 *
 * @author hakon
 */
public class OrchestratorContext {
    private static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(10);

    private TimeBudget timeBudget;

    public OrchestratorContext(Clock clock) {
        this.timeBudget = TimeBudget.fromNow(clock, DEFAULT_TIMEOUT);
    }

    /** Get the original timeout in seconds. */
    public long getOriginalTimeoutInSeconds() {
        return timeBudget.originalTimeout().get().getSeconds();
    }

    /**
     * Get timeout for a suboperation that should take up {@code shareOfRemaining} of the
     * remaining time, or throw an {@link UncheckedTimeoutException} if timed out.
     */
    public float getSuboperationTimeoutInSeconds(float shareOfRemaining) {
        if (!(0f <= shareOfRemaining && shareOfRemaining <= 1.0f)) {
            throw new IllegalArgumentException("Share of remaining time must be between 0 and 1: " + shareOfRemaining);
        }

        return shareOfRemaining * timeBudget.timeLeftOrThrow().get().toMillis() / 1000.0f;
    }
}