summaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/container/handler/VipStatus.java
blob: be386b1b84e3cc6d89d2624fa678fe10266b0e76 (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
// 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 java.util.IdentityHashMap;
import java.util.Map;

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.
 *
 * @author Steinar Knutsen
 */
public class VipStatus {

    private final ClustersStatus clustersStatus;

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

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

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

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

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

    /**
     * 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) {
        this.inRotationOverride = inRotation;
    }

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

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

    /** Returns whether this container should receive traffic at this time */
    public boolean isInRotation() {
        if (inRotationOverride != null) return inRotationOverride;
        return clustersStatus.containerShouldReceiveTraffic();
    }

}