aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/container/handler/VipStatus.java
blob: b928585378b44cc1cbb0e9e1dfcecd2af1b57352 (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
// Copyright 2016 Yahoo Inc. 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;

/**
 * API for programmatically removing the container from VIP rotation.
 *
 * @author steinar
 */
public class VipStatus {

    private final Map<Object, Boolean> clusters = new IdentityHashMap<>();

    public VipStatus() {
        this(null);
    }

    @Inject
    public VipStatus(QrSearchersConfig dispatchers) {
        // the config is not used for anything, it's just a dummy to create a
        // dependency link to which dispatchers are used
    }

    /**
     * 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 if no services
     *         are registered (yet)
     */
    public boolean isInRotation() {
        synchronized (clusters) {
            // if no stored state, try serving
            if (clusters.size() == 0) {
                return true;
            }
            for (Boolean inRotation : clusters.values()) {
                if (inRotation) {
                    return true;
                }
            }
        }
        return false;
    }

}