aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/main/java/com/yahoo/vespa/clustercontroller/core/ClusterStateView.java
blob: 51d075b9a1f031b21c3daebf77fa74079617b4f5 (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
107
108
109
110
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.core;

import com.yahoo.vdslib.state.ClusterState;
import com.yahoo.vdslib.state.Node;
import com.yahoo.vdslib.state.NodeState;
import com.yahoo.vdslib.state.NodeType;
import com.yahoo.vdslib.state.State;
import com.yahoo.vespa.clustercontroller.core.hostinfo.HostInfo;
import com.yahoo.vespa.clustercontroller.core.hostinfo.StorageNodeStatsBridge;

import java.text.ParseException;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * The Cluster Controller's view of the cluster given a particular state version. Some parts of the view
 * are static and only depend on the state version, e.g. which nodes are UP or DOWN. These static parts
 * are mostly represented by the ClusterState. The dynamic parts include stats for tracking outstanding
 * merges before steady-state is reached.
 *
 * @author hakonhall
 */
public class ClusterStateView {

    private static final Logger log = Logger.getLogger(ClusterStateView.class.getName());
    private final ClusterState clusterState;
    private final ClusterStatsAggregator statsAggregator;

    public static ClusterStateView create(String serializedClusterState) throws ParseException {
        ClusterState clusterState = new ClusterState(serializedClusterState);
        return new ClusterStateView(clusterState, createNewAggregator(clusterState));
    }

    public static ClusterStateView create(final ClusterState clusterState) {
        return new ClusterStateView(clusterState, createNewAggregator(clusterState));
    }

    private static ClusterStatsAggregator createNewAggregator(ClusterState clusterState) {
        Set<Integer> upDistributors = getIndicesOfUpNodes(clusterState, NodeType.DISTRIBUTOR);
        Set<Integer> upStorageNodes = getIndicesOfUpNodes(clusterState, NodeType.STORAGE);
        return new ClusterStatsAggregator(upDistributors, upStorageNodes);
    }

    ClusterStateView(ClusterState clusterState, ClusterStatsAggregator statsAggregator) {
        this.clusterState = clusterState;
        this.statsAggregator = statsAggregator;
    }

    /**
     * Returns the set of nodes that are up for a given node type. Non-private for testing.
     */
    static Set<Integer> getIndicesOfUpNodes(ClusterState clusterState, NodeType type) {
        int nodeCount = clusterState.getNodeCount(type);

        Set<Integer> nodesBeingUp = new HashSet<>();
        for (int i = 0; i < nodeCount; ++i) {
            Node node = new Node(type, i);
            NodeState nodeState = clusterState.getNodeState(node);
            State state = nodeState.getState();
            if (state == State.UP || state == State.INITIALIZING ||
                state == State.RETIRED || state == State.MAINTENANCE) {
                nodesBeingUp.add(i);
            }
        }

        return nodesBeingUp;
    }

    public ClusterState getClusterState() { return clusterState; }

    public void handleUpdatedHostInfo(NodeInfo node, HostInfo hostInfo) {
        if ( ! node.isDistributor()) return;

        final int hostVersion;
        if (hostInfo.getClusterStateVersionOrNull() == null) {
            // TODO: Consider logging a warning in the future (>5.36).
            // For now, a missing cluster state version probably means the content
            // node has not been updated yet.
            return;
        } else {
            hostVersion = hostInfo.getClusterStateVersionOrNull();
        }
        int currentStateVersion = clusterState.getVersion();

        if (hostVersion != currentStateVersion) {
            // The distributor may be old (null), or the distributor may not have updated
            // to the latest state version just yet. We log here with fine, because it may
            // also be a symptom of something wrong.
            log.log(Level.FINE, () -> "Current state version is " + currentStateVersion +
                    ", while host info received from distributor " + node.getNodeIndex() +
                    " is " + hostVersion);
            return;
        }

        statsAggregator.updateForDistributor(node.getNodeIndex(),
                StorageNodeStatsBridge.generate(hostInfo.getDistributor()));
    }

    public ClusterStatsAggregator getStatsAggregator() {
        return statsAggregator;
    }

    public String toString() {
        return clusterState.toString();
    }

}