aboutsummaryrefslogtreecommitdiffstats
path: root/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/policy/HostedVespaPolicy.java
blob: a54ca98d4df78415da0211d0a1ebbc3c4b8bef5c (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.orchestrator.policy;

import com.yahoo.vespa.applicationmodel.ApplicationInstance;
import com.yahoo.vespa.applicationmodel.HostName;
import com.yahoo.vespa.flags.BooleanFlag;
import com.yahoo.vespa.flags.FetchVector;
import com.yahoo.vespa.flags.FlagSource;
import com.yahoo.vespa.flags.Flags;
import com.yahoo.vespa.orchestrator.OrchestratorContext;
import com.yahoo.vespa.orchestrator.controller.ClusterControllerClientFactory;
import com.yahoo.vespa.orchestrator.controller.ClusterControllerNodeState;
import com.yahoo.vespa.orchestrator.model.ApplicationApi;
import com.yahoo.vespa.orchestrator.model.ApplicationApiFactory;
import com.yahoo.vespa.orchestrator.model.ClusterApi;
import com.yahoo.vespa.orchestrator.model.NodeGroup;
import com.yahoo.vespa.orchestrator.model.StorageNode;
import com.yahoo.vespa.orchestrator.status.ApplicationInstanceStatus;
import com.yahoo.vespa.orchestrator.status.HostStatus;
import com.yahoo.vespa.orchestrator.status.ApplicationLock;

/**
 * @author oyving
 */
public class HostedVespaPolicy implements Policy {

    public static final String APPLICATION_SUSPENDED_CONSTRAINT = "application-suspended";
    public static final String ENOUGH_SERVICES_UP_CONSTRAINT = "enough-services-up";
    public static final String UNKNOWN_SERVICE_STATUS = "unknown-service-status";
    public static final String SET_NODE_STATE_CONSTRAINT = "controller-set-node-state";
    public static final String CLUSTER_CONTROLLER_AVAILABLE_CONSTRAINT = "controller-available";
    public static final String DEADLINE_CONSTRAINT = "deadline";

    private final HostedVespaClusterPolicy clusterPolicy;
    private final ClusterControllerClientFactory clusterControllerClientFactory;
    private final ApplicationApiFactory applicationApiFactory;

    public HostedVespaPolicy(HostedVespaClusterPolicy clusterPolicy,
                             ClusterControllerClientFactory clusterControllerClientFactory,
                             ApplicationApiFactory applicationApiFactory,
                             FlagSource flagSource) {
        this.clusterPolicy = clusterPolicy;
        this.clusterControllerClientFactory = clusterControllerClientFactory;
        this.applicationApiFactory = applicationApiFactory;
    }

    @Override
    public SuspensionReasons grantSuspensionRequest(OrchestratorContext context, ApplicationApi application)
            throws HostStateChangeDeniedException {
        var suspensionReasons = new SuspensionReasons();

        // Apply per-cluster policy
        for (ClusterApi cluster : application.getClusters()) {
            suspensionReasons.mergeWith(clusterPolicy.verifyGroupGoingDownIsFine(cluster));
        }

        // Ask Cluster Controller to set storage nodes in maintenance, unless the node is already allowed
        // to be down (or permanently down) in case they are guaranteed to be in maintenance already.
        for (StorageNode storageNode : application.getNoRemarksStorageNodesInGroupInClusterOrder()) {
            storageNode.setStorageNodeState(context, ClusterControllerNodeState.MAINTENANCE);
        }

        // Ensure all nodes in the group are marked as allowed to be down
        for (HostName hostName : application.getNodesInGroupWithStatus(HostStatus.NO_REMARKS)) {
            application.setHostState(context, hostName, HostStatus.ALLOWED_TO_BE_DOWN);
        }

        return suspensionReasons;
    }

    @Override
    public void releaseSuspensionGrant(OrchestratorContext context, ApplicationApi application)
            throws HostStateChangeDeniedException {
        // Always defer to Cluster Controller whether it's OK to resume storage node
        for (StorageNode storageNode : application.getSuspendedStorageNodesInGroupInReverseClusterOrder()) {
            storageNode.setStorageNodeState(context, ClusterControllerNodeState.UP);
        }

        // In particular, we're not modifying the state of PERMANENTLY_DOWN nodes.
        for (HostName hostName : application.getNodesInGroupWithStatus(HostStatus.ALLOWED_TO_BE_DOWN)) {
            application.setHostState(context, hostName, HostStatus.NO_REMARKS);
        }
    }

    @Override
    public void acquirePermissionToRemove(OrchestratorContext context, ApplicationApi applicationApi)
            throws HostStateChangeDeniedException {
        ApplicationInstanceStatus applicationStatus = applicationApi.getApplicationStatus();
        if (applicationStatus == ApplicationInstanceStatus.ALLOWED_TO_BE_DOWN) {
            throw new HostStateChangeDeniedException(
                    applicationApi.getNodeGroup(),
                    HostedVespaPolicy.APPLICATION_SUSPENDED_CONSTRAINT,
                    "Unable to test availability constraints as the application " +
                            applicationApi.applicationId() + " is allowed to be down");
        }

        // Apply per-cluster policy
        for (ClusterApi cluster : applicationApi.getClusters()) {
            clusterPolicy.verifyGroupGoingDownPermanentlyIsFine(cluster);
        }

        // Get permission from the Cluster Controller to remove the content nodes.
        for (StorageNode storageNode : applicationApi.getStorageNodesInGroupInClusterOrder()) {
            // Consider changing the semantics of setting storage node state to DOWN in cluster controller, to avoid 2 calls.
            storageNode.setStorageNodeState(context.createSubcontextForSingleAppOp(true), ClusterControllerNodeState.DOWN);
            storageNode.forceDistributorState(context, ClusterControllerNodeState.DOWN);
        }

        // Ensure all nodes in the group are marked as permanently down
        for (HostName hostName : applicationApi.getNodesInGroupWith(status -> status != HostStatus.PERMANENTLY_DOWN)) {
            applicationApi.setHostState(context, hostName, HostStatus.PERMANENTLY_DOWN);
        }
    }

    // TODO: Remove later - currently used for backward compatibility testing
    @Override
    public void releaseSuspensionGrant(
            OrchestratorContext context,
            ApplicationInstance applicationInstance,
            HostName hostName,
            ApplicationLock lock) throws HostStateChangeDeniedException {
        NodeGroup nodeGroup = new NodeGroup(applicationInstance, hostName);
        ApplicationApi applicationApi = applicationApiFactory.create(nodeGroup, lock, clusterControllerClientFactory);
        releaseSuspensionGrant(context, applicationApi);
    }

}