summaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/container/handler/VipStatus.java
blob: 555dc81057d82e12344583914108be43078f56fa (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.handler;

import com.google.inject.Inject;
import com.yahoo.container.QrSearchersConfig;
import com.yahoo.container.core.VipStatusConfig;

/**
 * API for programmatically removing the container from VIP rotation.
 *
 * This is multithread safe.
 *
 * @author Steinar Knutsen
 * @author bratseth
 */
public class VipStatus {

    private final ClustersStatus clustersStatus;

    /** If this is non-null, its value decides whether this container is in rotation */
    private Boolean rotationOverride = null;

    /** The current state of this */
    private boolean currentlyInRotation;

    private final Object mutex = new Object();

    public VipStatus() {
        this(new QrSearchersConfig(new QrSearchersConfig.Builder()),
             new VipStatusConfig(new VipStatusConfig.Builder()),
             new ClustersStatus());
    }

    public VipStatus(QrSearchersConfig dispatchers) {
        this(dispatchers, new ClustersStatus());
    }

    public VipStatus(ClustersStatus clustersStatus) {
        this.clustersStatus = clustersStatus;
    }

    @Inject
    public VipStatus(QrSearchersConfig dispatchers, ClustersStatus clustersStatus) {
        this.clustersStatus = clustersStatus;
        clustersStatus.setContainerHasClusters(! dispatchers.searchcluster().isEmpty());
        updateCurrentlyInRotation();
    }

    /** @deprecated don't pass VipStatusConfig */
    @Deprecated // TODO: Remove on Vespa 8
    public VipStatus(QrSearchersConfig dispatchers, VipStatusConfig vipStatusConfig, ClustersStatus clustersStatus) {
        this(dispatchers, clustersStatus);
    }

    /**
     * Explicitly set this container in or out of rotation
     *
     * @param inRotation true to set this in rotation regardless of any clusters and of the default value,
     *                   false to set it out, and null to make this decision using the usual cluster-dependent logic
     */
    public void setInRotation(Boolean inRotation) {
        synchronized (mutex) {
            rotationOverride = inRotation;
            updateCurrentlyInRotation();
        }
    }

    /** Note that a cluster (which influences up/down state) is up */
    public void addToRotation(String clusterIdentifier) {
        clustersStatus.setUp(clusterIdentifier);
        updateCurrentlyInRotation();
    }

    /** Note that a cluster (which influences up/down state) is down */
    public void removeFromRotation(String clusterIdentifier) {
        clustersStatus.setDown(clusterIdentifier);
        updateCurrentlyInRotation();
    }

    /** @deprecated use addToRotation(String) instead  */
    @Deprecated // TODO: Remove on Vespa 8
    public void addToRotation(Object clusterIdentifier) {
        addToRotation((String) clusterIdentifier);
    }

    /** @deprecated use removeFromRotation(String) instead  */
    @Deprecated // TODO: Remove on Vespa 8
    public void removeFromRotation(Object clusterIdentifier) {
        removeFromRotation((String) clusterIdentifier);
    }

    private void updateCurrentlyInRotation() {
        synchronized (mutex) {
            if (rotationOverride != null)
                currentlyInRotation = rotationOverride;
            else
                currentlyInRotation = clustersStatus.containerShouldReceiveTraffic();
        }
    }

    /** Returns whether this container should receive traffic at this time */
    public boolean isInRotation() {
        return currentlyInRotation;
    }

}