aboutsummaryrefslogtreecommitdiffstats
path: root/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/model/ClusterPolicyOverride.java
blob: f724a4da9cb7313264876685aa51eed2cc29c3b1 (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
41
42
43
44
45
46
47
48
49
50
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.orchestrator.model;

import com.yahoo.vespa.orchestrator.policy.SuspensionLimit;

import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;

/**
 * @author hakonhall
 */
public record ClusterPolicyOverride(int deployedSize, OptionalInt expectedSize, OptionalInt allowedDown, OptionalDouble allowedDownRatio) {
    public ClusterPolicyOverride {
        if (deployedSize <= 0)
            throw new IllegalArgumentException("deployedSize must be positive");

        if (expectedSize.isPresent() && expectedSize.getAsInt() <= 0)
            throw new IllegalArgumentException("expectedSize must be positive");

        if (allowedDown.isPresent()) {
            if (allowedDown.getAsInt() <= 0)
                throw new IllegalArgumentException("allowedDown must be positive: " + allowedDown.getAsInt());
            if (expectedSize.isPresent() && allowedDown.getAsInt() > expectedSize.getAsInt())
                throw new IllegalArgumentException("allowedDown must be less than or equal to expectedSize (" + expectedSize.getAsInt() +
                                                   "): " + allowedDown.getAsInt());
        }

        if (allowedDownRatio.isPresent() && (allowedDownRatio.getAsDouble() < 0.0 || allowedDownRatio.getAsDouble() > 1.0))
            throw new IllegalArgumentException("allowedDownRatio must be between 0.0 and 1.0: " + allowedDownRatio.getAsDouble());

    }

    public static ClusterPolicyOverride fromDeployedSize(int deployedSize) {
        return new ClusterPolicyOverride(deployedSize, OptionalInt.empty(), OptionalInt.empty(), OptionalDouble.empty());
    }

    public Optional<SuspensionLimit> getSuspensionLimit() {
        return allowedDown.isPresent() || allowedDownRatio.isPresent() ?
               Optional.of(new SuspensionLimit(allowedDown.orElse(0), allowedDownRatio.orElse(0.0))) :
               Optional.empty();
    }

    public OptionalInt allowedDownPercentage() {
        return allowedDownRatio.isPresent() ?
               OptionalInt.of((int) Math.round(allowedDownRatio.getAsDouble() * 100.0)) :
               OptionalInt.empty();
    }

}