summaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/container/handler/VipStatus.java
blob: d7457140dae928bcf493a187c71685658c6e8597 (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
// 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 Map<Object, Boolean> clusters = new IdentityHashMap<>();
    private final VipStatusConfig vipStatusConfig;

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

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

    // TODO: Why use QrSearchersConfig here? Remove and inject ComponentRegistry<ClusterSearcher> instead?
    @Inject
    public VipStatus(QrSearchersConfig dispatchers, VipStatusConfig vipStatusConfig) {
        // the config is not used for anything, it's just a dummy to create a
        // dependency link to which dispatchers are used
        this.vipStatusConfig = vipStatusConfig;
    }

    /**
     * Set a service or cluster into rotation.
     *
     * @param clusterIdentifier
     *            an object where the object identity will serve to identify the
     *            cluster or service
     */
    public void addToRotation(Object clusterIdentifier) {
        synchronized (clusters) {
            clusters.put(clusterIdentifier, Boolean.TRUE);
        }
    }

    /**
     * Set a service or cluster out of rotation.
     *
     * @param clusterIdentifier
     *            an object where the object identity will serve to identify the
     *            cluster or service
     */
    public void removeFromRotation(Object clusterIdentifier) {
        synchronized (clusters) {
            clusters.put(clusterIdentifier, Boolean.FALSE);
        }
    }

    /**
     * Tell whether the container is connected to any active services at all.
     *
     * @return true if at least one service or cluster is up, or value is taken from config if no services
     *         are registered (yet)
     */
    public boolean isInRotation() {
        synchronized (clusters) {
            // if no stored state, use config to decide whether to serve or not
            if (clusters.size() == 0) {
                return vipStatusConfig.initiallyInRotation();
            }
            for (Boolean inRotation : clusters.values()) {
                if (inRotation) {
                    return true;
                }
            }
        }
        return false;
    }

}