summaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/main/java/com/yahoo/vespa/clustercontroller/core/ContentCluster.java
blob: ad7da755b98aaf3138391fbed857acdfdba22c64 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * @class VdsCluster
 *
 * Represents a VDS cluster.
 */
package com.yahoo.vespa.clustercontroller.core;

import com.yahoo.vdslib.distribution.ConfiguredNode;
import com.yahoo.vdslib.state.*;
import com.yahoo.vdslib.distribution.Distribution;
import com.yahoo.vdslib.distribution.Group;
import com.yahoo.vespa.clustercontroller.core.status.statuspage.VdsClusterHtmlRendrer;
import com.yahoo.vespa.clustercontroller.utils.staterestapi.requests.SetUnitStateRequest;

import java.util.*;

public class ContentCluster {

    private final String clusterName;

    private final ClusterInfo clusterInfo = new ClusterInfo();

    private final Map<Node, Long> nodeStartTimestamps = new TreeMap<>();

    private int slobrokGenerationCount = 0;

    private int pollingFrequency = 5000;

    private Distribution distribution;
    private int minStorageNodesUp;
    private double minRatioOfStorageNodesUp;

    public ContentCluster(String clusterName, Collection<ConfiguredNode> configuredNodes, Distribution distribution,
                          int minStorageNodesUp, double minRatioOfStorageNodesUp) {
        if (configuredNodes == null) throw new IllegalArgumentException("Nodes must be set");
        this.clusterName = clusterName;
        this.distribution = distribution;
        this.minStorageNodesUp = minStorageNodesUp;
        this.minRatioOfStorageNodesUp = minRatioOfStorageNodesUp;
        setNodes(configuredNodes);
    }

    public void writeHtmlState(
            final VdsClusterHtmlRendrer vdsClusterHtmlRendrer,
            final StringBuilder sb,
            final Timer timer,
            final ClusterState state,
            final Distribution distribution,
            final FleetControllerOptions options,
            final EventLog eventLog) {

        final VdsClusterHtmlRendrer.Table table =
                vdsClusterHtmlRendrer.createNewClusterHtmlTable(clusterName, slobrokGenerationCount);

        final List<Group> groups = LeafGroups.enumerateFrom(distribution.getRootGroup());

        for (int j=0; j<groups.size(); ++j) {
            final Group group = groups.get(j);
            assert(group != null);
            final String localName = group.getUnixStylePath();
            assert(localName != null);
            final TreeMap<Integer, NodeInfo> storageNodeInfoByIndex = new TreeMap<>();
            final TreeMap<Integer, NodeInfo> distributorNodeInfoByIndex = new TreeMap<>();
            for (ConfiguredNode configuredNode : group.getNodes()) {
                storeNodeInfo(configuredNode.index(), NodeType.STORAGE, storageNodeInfoByIndex);
                storeNodeInfo(configuredNode.index(), NodeType.DISTRIBUTOR, distributorNodeInfoByIndex);
            }
            table.renderNodes(
                    storageNodeInfoByIndex,
                    distributorNodeInfoByIndex,
                    timer,
                    state,
                    options.maxPrematureCrashes,
                    eventLog,
                    clusterName,
                    localName);
        }
        table.addTable(sb, options.stableStateTimePeriod);
    }

    private void storeNodeInfo(int nodeIndex, NodeType nodeType, Map<Integer, NodeInfo> nodeInfoByIndex) {
        NodeInfo nodeInfo = getNodeInfo(new Node(nodeType, nodeIndex));
        if (nodeInfo == null) return;
        nodeInfoByIndex.put(nodeIndex, nodeInfo);
    }

    public Distribution getDistribution() { return distribution; }

    public void setDistribution(Distribution distribution) {
        this.distribution = distribution;
        for (NodeInfo info : clusterInfo.getAllNodeInfo()) {
            info.setGroup(distribution);
        }
    }

    /** Sets the configured nodes of this cluster */
    public final void setNodes(Collection<ConfiguredNode> configuredNodes) {
        clusterInfo.setNodes(configuredNodes, this, distribution);
    }

    public void setStartTimestamp(Node n, long startTimestamp) {
        nodeStartTimestamps.put(n, startTimestamp);
    }

    public long getStartTimestamp(Node n) {
        Long value = nodeStartTimestamps.get(n);
        return (value == null ? 0 : value);
    }

    public Map<Node, Long> getStartTimestamps() {
        return nodeStartTimestamps;
    }

    public void clearStates() {
        for (NodeInfo info : clusterInfo.getAllNodeInfo()) {
            info.setReportedState(null, 0);
        }
    }

    public boolean allStatesReported() {
        return clusterInfo.allStatesReported();
    }

    public int getPollingFrequency() { return pollingFrequency; }
    public void setPollingFrequency(int millisecs) { pollingFrequency = millisecs; }

    /** Returns the configured nodes of this as a read-only map indexed on node index (distribution key) */
    public Map<Integer, ConfiguredNode> getConfiguredNodes() {
        return clusterInfo.getConfiguredNodes();
    }

    public Collection<NodeInfo> getNodeInfo() {
        return Collections.unmodifiableCollection(clusterInfo.getAllNodeInfo());
    }

    public ClusterInfo clusterInfo() { return clusterInfo; }

    public String getName() { return clusterName; }

    public NodeInfo getNodeInfo(Node node) { return clusterInfo.getNodeInfo(node); }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("ContentCluster(").append(clusterName).append(") {");
        for (NodeInfo node : clusterInfo.getAllNodeInfo()) {
            sb.append("\n  ").append(node);
        }
        sb.append("\n}");
        return sb.toString();
    }

    public int getSlobrokGenerationCount() { return slobrokGenerationCount; }

    public void setSlobrokGenerationCount(int count) { slobrokGenerationCount = count; }

    private void getLeaves(Group node, List<Group> leaves, List<String> names, String name) {
        if (node.isLeafGroup()) {
            leaves.add(node);
            names.add(name + "/" + node.getName());
            return;
        }
        for (Group g : node.getSubgroups().values()) {
            getLeaves(g, leaves, names, name + (node.getName() != null ? "/" + node.getName() : ""));
        }
    }

    public StorageNodeStats getStorageNodeStats(int storageNodeIndex) {
        LatencyStats aggregatePutLatencyStats = new LatencyStats();
        StorageNodeStats aggregateStats = new StorageNodeStats(aggregatePutLatencyStats);
        for (DistributorNodeInfo distributor : clusterInfo.getDistributorNodeInfo()) {
            StorageNodeStats statsFromDistributor = distributor.getStorageNodeStatsOrNull(storageNodeIndex);
            if (statsFromDistributor != null) {
                aggregateStats.add(statsFromDistributor);
            }
        }

        return aggregateStats;
    }

    /**
     * Checks if a node can be upgraded
     *
     * @param node the node to be checked for upgrad
     * @param clusterState the current cluster state version
     * @param condition the upgrade condition
     * @param newState state wanted to be set  @return NodeUpgradePrechecker.Response
     */
    public NodeStateChangeChecker.Result calculateEffectOfNewState(
            Node node, ClusterState clusterState, SetUnitStateRequest.Condition condition, NodeState oldState, NodeState newState) {

        NodeStateChangeChecker nodeStateChangeChecker = new NodeStateChangeChecker(
                minStorageNodesUp,
                minRatioOfStorageNodesUp,
                distribution.getRedundancy(),
                clusterInfo);
        return nodeStateChangeChecker.evaluateTransition(node, clusterState, condition, oldState, newState);
    }

    public void setMinStorageNodesUp(int minStorageNodesUp) {
        this.minStorageNodesUp = minStorageNodesUp;
    }

    public void setMinRatioOfStorageNodesUp(double minRatioOfStorageNodesUp) {
        this.minRatioOfStorageNodesUp = minRatioOfStorageNodesUp;
    }

    public boolean hasConfiguredNode(int index) {
        return clusterInfo.getConfiguredNodes().containsKey(index);
    }

}