aboutsummaryrefslogtreecommitdiffstats
path: root/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/policy/ClusterParams.java
blob: 8f2c9511f3ec79a3c789f24d3e3e032933d84f74 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.orchestrator.policy;

import java.util.Objects;
import java.util.OptionalDouble;
import java.util.OptionalInt;

/**
 * Parameters controlling orchestration of a particular service cluster of an implied application.
 *
 * @author hakonhall
 */
public class ClusterParams {

    private static final ClusterParams DEFAULT = new ClusterParams.Builder().build();

    private final int size;
    private final int allowedDown;
    private final double allowedDownRatio;

    public static class Builder {
        private int size = -1;
        private int allowedDown = -1;
        private double allowedDownRatio = -1.0;

        public Builder() {}

        public Builder setSize(int size) {
            this.size = size;
            return this;
        }

        public Builder setAllowedDown(int allowedDown) {
            this.allowedDown = allowedDown;
            return this;
        }

        public Builder setAllowedDownRatio(double allowedDownRatio) {
            this.allowedDownRatio = allowedDownRatio;
            return this;
        }

        public ClusterParams build() {
            return new ClusterParams(size, allowedDown, allowedDownRatio);
        }
    }

    public static ClusterParams getDefault() {
        return DEFAULT;
    }

    private ClusterParams(int size, int allowedDown, double allowedDownRatio) {
        this.size = size;
        this.allowedDown = allowedDown;
        this.allowedDownRatio = allowedDownRatio;
    }

    /**
     * The expected and ideal number of members of the cluster:  Count missing services as down,
     * and expected to be added to the application soon.
     */
    public OptionalInt size() {
        return size > 0 ? OptionalInt.of(size) : OptionalInt.empty();
    }

    /** The number of services that are allowed to be down. */
    public OptionalInt allowedDown() {
        return allowedDown > 0 ? OptionalInt.of(allowedDown) : OptionalInt.empty();
    }

    /** The ratio of services that are allowed to be down. */
    public OptionalDouble allowedDownRatio() {
        return 0.0 <= allowedDownRatio && allowedDownRatio <= 1.0 ?
               OptionalDouble.of(allowedDownRatio) :
               OptionalDouble.empty();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ClusterParams that = (ClusterParams) o;
        return size == that.size && allowedDown == that.allowedDown && Double.compare(that.allowedDownRatio, allowedDownRatio) == 0;
    }

    @Override
    public int hashCode() {
        return Objects.hash(size, allowedDown, allowedDownRatio);
    }
}