aboutsummaryrefslogtreecommitdiffstats
path: root/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/configserver/NodeRepository.java
blob: 9a44a76b145f9886e372f53ba06cddcde70b3a27 (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
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.api.integration.configserver;

import com.yahoo.component.Version;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.NodeType;
import com.yahoo.config.provision.zone.ZoneId;
import com.yahoo.vespa.hosted.controller.api.integration.noderepository.NodeList;
import com.yahoo.vespa.hosted.controller.api.integration.noderepository.NodeMembership;
import com.yahoo.vespa.hosted.controller.api.integration.noderepository.NodeRepositoryNode;
import com.yahoo.vespa.hosted.controller.api.integration.noderepository.NodeState;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * A minimal interface to the node repository, providing only the operations used by the controller.
 *
 * @author mpolden
 */
public interface NodeRepository {

    void addNodes(ZoneId zone, Collection<NodeRepositoryNode> nodes);

    void deleteNode(ZoneId zone, String hostname);

    void setState(ZoneId zone, NodeState nodeState, String nodename);

    NodeRepositoryNode getNode(ZoneId zone, String hostname);

    NodeList listNodes(ZoneId zone);

    NodeList listNodes(ZoneId zone, ApplicationId application);

    /** List all nodes in given zone */
    default List<Node> list(ZoneId zone) {
        return listNodes(zone).nodes().stream()
                              .map(NodeRepository::toNode)
                              .collect(Collectors.toUnmodifiableList());
    }

    /** List all nodes in zone owned by given application */
    default List<Node> list(ZoneId zone, ApplicationId application) {
        return listNodes(zone, application).nodes().stream()
                                           .map(NodeRepository::toNode)
                                           .collect(Collectors.toUnmodifiableList());
    }

    /** List all nodes in states, in zone owned by given application */
    default List<Node> list(ZoneId zone, ApplicationId application, Set<Node.State> states) {
        return list(zone, application).stream()
                                      .filter(node -> states.contains(node.state()))
                                      .collect(Collectors.toList());
    }

    /** Upgrade all nodes of given type to a new version */
    void upgrade(ZoneId zone, NodeType type, Version version);

    /** Upgrade OS for all nodes of given type to a new version */
    void upgradeOs(ZoneId zone, NodeType type, Version version);

    /** Requests firmware checks on all hosts in the given zone. */
    void requestFirmwareCheck(ZoneId zone);

    /** Cancels firmware checks on all hosts in the given zone. */
    void cancelFirmwareCheck(ZoneId zone);

    private static Node toNode(NodeRepositoryNode node) {
        var application = Optional.ofNullable(node.getOwner())
                                  .map(owner -> ApplicationId.from(owner.getTenant(), owner.getApplication(),
                                                                   owner.getInstance()));
        return new Node(com.yahoo.config.provision.HostName.from(node.getHostname()),
                        fromJacksonState(node.getState()),
                        fromJacksonType(node.getType()),
                        application,
                        versionFrom(node.getVespaVersion()),
                        versionFrom(node.getWantedVespaVersion()),
                        versionFrom(node.getCurrentOsVersion()),
                        versionFrom(node.getWantedOsVersion()),
                        fromBoolean(node.getAllowedToBeDown()),
                        toInt(node.getCurrentRestartGeneration()),
                        toInt(node.getRestartGeneration()),
                        toInt(node.getCurrentRebootGeneration()),
                        toInt(node.getRebootGeneration()),
                        toDouble(node.getMinCpuCores()),
                        toDouble(node.getMinMainMemoryAvailableGb()),
                        toDouble(node.getMinDiskAvailableGb()),
                        toDouble(node.getBandwidth()) / 1000,
                        toBoolean(node.getFastDisk()),
                        toInt(node.getCost()),
                        node.getCanonicalFlavor(),
                        clusterIdOf(node.getMembership()),
                        clusterTypeOf(node.getMembership()));
    }

    private static String clusterIdOf(NodeMembership nodeMembership) {
        return nodeMembership == null ? "" : nodeMembership.clusterid;
    }

    private static Node.ClusterType clusterTypeOf(NodeMembership nodeMembership) {
        switch (nodeMembership.clustertype) {
            case "admin": return Node.ClusterType.admin;
            case "content": return Node.ClusterType.content;
            case "container": return Node.ClusterType.container;
        }
        return Node.ClusterType.unknown;
    }

    // Convert Jackson type to config.provision type
    private static NodeType fromJacksonType(com.yahoo.vespa.hosted.controller.api.integration.noderepository.NodeType nodeType) {
        switch (nodeType) {
            case tenant: return NodeType.tenant;
            case host: return NodeType.host;
            case proxy: return NodeType.proxy;
            case proxyhost: return NodeType.proxyhost;
            case config: return NodeType.config;
            case confighost: return NodeType.confighost;
            default: throw new IllegalArgumentException("Unknown type: " + nodeType);
        }
    }

    private static com.yahoo.vespa.hosted.controller.api.integration.configserver.Node.State fromJacksonState(NodeState state) {
        switch (state) {
            case provisioned: return Node.State.provisioned;
            case ready: return Node.State.ready;
            case reserved: return Node.State.reserved;
            case active: return Node.State.active;
            case inactive: return Node.State.inactive;
            case dirty: return Node.State.dirty;
            case failed: return Node.State.failed;
            case parked: return Node.State.parked;
        }
        return Node.State.unknown;
    }

    private static Node.ServiceState fromBoolean(Boolean allowedDown) {
        return (allowedDown == null)
                ? Node.ServiceState.unorchestrated
                : allowedDown ? Node.ServiceState.allowedDown : Node.ServiceState.expectedUp;
    }

    private static boolean toBoolean(Boolean b) {
        return b == null ? false : b;
    }

    private static double toDouble(Double d) {
        return d == null ? 0 : d;
    }

    private static int toInt(Integer i) {
        return i == null ? 0 : i;
    }

    private static Version versionFrom(String s) {
        return s == null ? Version.emptyVersion : Version.fromString(s);
    }

}