aboutsummaryrefslogtreecommitdiffstats
path: root/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/resources/HostRequestHandler.java
blob: a73297fc29c69aa59fbd6263dc75e90db1e75bff (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
213
214
215
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.orchestrator.resources;

import ai.vespa.http.HttpURL.Path;
import com.yahoo.component.annotation.Inject;
import com.yahoo.concurrent.UncheckedTimeoutException;
import com.yahoo.container.jdisc.ThreadedHttpRequestHandler;
import com.yahoo.jdisc.Response;
import com.yahoo.restapi.JacksonJsonResponse;
import com.yahoo.restapi.RestApi;
import com.yahoo.restapi.RestApiException;
import com.yahoo.restapi.RestApiRequestHandler;
import com.yahoo.vespa.applicationmodel.HostName;
import com.yahoo.vespa.orchestrator.Host;
import com.yahoo.vespa.orchestrator.HostNameNotFoundException;
import com.yahoo.vespa.orchestrator.OrchestrationException;
import com.yahoo.vespa.orchestrator.Orchestrator;
import com.yahoo.vespa.orchestrator.policy.HostStateChangeDeniedException;
import com.yahoo.vespa.orchestrator.policy.HostedVespaPolicy;
import com.yahoo.vespa.orchestrator.restapi.wire.GetHostResponse;
import com.yahoo.vespa.orchestrator.restapi.wire.HostService;
import com.yahoo.vespa.orchestrator.restapi.wire.HostStateChangeDenialReason;
import com.yahoo.vespa.orchestrator.restapi.wire.PatchHostRequest;
import com.yahoo.vespa.orchestrator.restapi.wire.PatchHostResponse;
import com.yahoo.vespa.orchestrator.restapi.wire.UpdateHostResponse;
import com.yahoo.vespa.orchestrator.status.HostStatus;

import java.net.URI;
import java.time.Instant;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
 * @author oyving
 * @author bjorncs
 */
public class HostRequestHandler extends RestApiRequestHandler<HostRequestHandler> {

    private static final Logger log = Logger.getLogger(HostRequestHandler.class.getName());

    private final Orchestrator orchestrator;

    @Inject
    public HostRequestHandler(ThreadedHttpRequestHandler.Context context, Orchestrator orchestrator) {
        super(context, HostRequestHandler::createRestApiDefinition);
        this.orchestrator = orchestrator;
    }

    private static RestApi createRestApiDefinition(HostRequestHandler self) {
        return RestApi.builder()
                .addRoute(RestApi.route("/orchestrator/v1/hosts/{hostname}")
                        .get(self::getHost)
                        .patch(PatchHostRequest.class, self::patch))
                .addRoute(RestApi.route("/orchestrator/v1/hosts/{hostname}/suspended")
                        .put(self::suspend)
                        .delete(self::resume))
                .registerJacksonRequestEntity(PatchHostRequest.class)
                .registerJacksonResponseEntity(GetHostResponse.class)
                .registerJacksonResponseEntity(PatchHostResponse.class)
                .registerJacksonResponseEntity(UpdateHostResponse.class)
                .build();
    }

    /**
     * Shows the Orchestrator state of a host.
     */
    private GetHostResponse getHost(RestApi.RequestContext context) {
        String hostNameString = context.pathParameters().getStringOrThrow("hostname");
        HostName hostName = new HostName(hostNameString);
        try {
            Host host = orchestrator.getHost(hostName);

            URI applicationUri = context.baseRequestURL()
                    .withPath(Path.parse( "/orchestrator/v1/instances/" + host.getApplicationInstanceReference().asString()))
                    .asURI();

            List<HostService> hostServices = host.getServiceInstances().stream()
                    .map(serviceInstance -> new HostService(
                            serviceInstance.getServiceCluster().clusterId().s(),
                            serviceInstance.getServiceCluster().serviceType().s(),
                            serviceInstance.configId().s(),
                            serviceInstance.serviceStatus().name()))
                    .toList();

            return new GetHostResponse(
                    host.getHostName().s(),
                    host.getHostInfo().status().name(),
                    host.getHostInfo().suspendedSince().map(Instant::toString).orElse(null),
                    applicationUri.toString(),
                    hostServices);
        } catch (UncheckedTimeoutException e) {
            log.log(Level.FINE, () -> "Failed to get host " + hostName + ": " + e.getMessage());
            throw restApiExceptionFromTimeout("getHost", hostName, e);
        } catch (HostNameNotFoundException e) {
            log.log(Level.FINE, () -> "Host not found: " + hostName);
            throw new RestApiException.NotFound(e);
        }
    }

    /**
     * Tweak internal Orchestrator state for host.
     */
    private PatchHostResponse patch(RestApi.RequestContext context, PatchHostRequest request) {
        String hostNameString = context.pathParameters().getStringOrThrow("hostname");
        HostName hostName = new HostName(hostNameString);

        if (request.state != null) {
            HostStatus state;
            try {
                state = HostStatus.valueOf(request.state);
            } catch (IllegalArgumentException dummy) {
                throw new RestApiException.BadRequest("Bad state in request: '" + request.state + "'");
            }

            try {
                orchestrator.setNodeStatus(hostName, state);
            } catch (HostNameNotFoundException e) {
                log.log(Level.FINE, () -> "Host not found: " + hostName);
                throw new RestApiException.NotFound(e);
            } catch (UncheckedTimeoutException e) {
                log.log(Level.FINE, () -> "Failed to patch " + hostName + ": " + e.getMessage());
                throw restApiExceptionFromTimeout("patch", hostName, e);
            } catch (OrchestrationException e) {
                String message = "Failed to set " + hostName + " to " + state + ": " + e.getMessage();
                log.log(Level.FINE, message, e);
                throw new RestApiException.InternalServerError(message);
            }
        }

        PatchHostResponse response = new PatchHostResponse();
        response.description = "ok";
        return response;
    }

    /**
     * Ask for permission to temporarily suspend all services on a host.
     *
     * On success, none, some, or all services on the host may already have been effectively suspended,
     * e.g. as of Feb 2015, a content node would already be set in the maintenance state.
     *
     * Once the host is ready to resume normal operations, it must finish with resume() (see below).
     *
     * If the host has already been granted permission to suspend all services, requesting
     * suspension again is idempotent and will succeed.
     */
    private UpdateHostResponse suspend(RestApi.RequestContext context) {
        String hostNameString = context.pathParameters().getStringOrThrow("hostname");
        HostName hostName = new HostName(hostNameString);
        try {
            orchestrator.suspend(hostName);
        } catch (HostNameNotFoundException e) {
            log.log(Level.FINE, () -> "Host not found: " + hostName);
            throw new RestApiException.NotFound(e);
        } catch (UncheckedTimeoutException e) {
            log.log(Level.FINE, () -> "Failed to suspend " + hostName + ": " + e.getMessage());
            throw restApiExceptionFromTimeout("suspend", hostName, e);
        } catch (HostStateChangeDeniedException e) {
            log.log(Level.FINE, () -> "Failed to suspend " + hostName + ": " + e.getMessage());
            throw restApiExceptionWithDenialReason("suspend", hostName, e);
        }
        return new UpdateHostResponse(hostName.s(), null);
    }
    /**
     * Resume normal operations for all services on a host that has previously been allowed suspension.
     *
     * If the host is already registered as running normal operations, then resume() is idempotent
     * and will succeed.
     */
    private UpdateHostResponse resume(RestApi.RequestContext context) {
        String hostNameString = context.pathParameters().getStringOrThrow("hostname");
        HostName hostName = new HostName(hostNameString);
        try {
            orchestrator.resume(hostName);
        } catch (HostNameNotFoundException e) {
            log.log(Level.FINE, () -> "Host not found: " + hostName);
            throw new RestApiException.NotFound(e);
        } catch (UncheckedTimeoutException e) {
            log.log(Level.FINE, () -> "Failed to resume " + hostName + ": " + e.getMessage());
            throw restApiExceptionFromTimeout("resume", hostName, e);
        } catch (HostStateChangeDeniedException e) {
            log.log(Level.FINE, () -> "Failed to resume " + hostName + ": " + e.getMessage());
            throw restApiExceptionWithDenialReason("resume", hostName, e);
        }
        return new UpdateHostResponse(hostName.s(), null);
    }

    private RestApiException restApiExceptionFromTimeout(String operationDescription,
                                                                HostName hostName,
                                                                UncheckedTimeoutException e) {
        // Return timeouts as 409 Conflict instead of 504 Gateway Timeout to reduce noise in 5xx graphs.
        return createRestApiException(operationDescription, hostName, e,
                HostedVespaPolicy.DEADLINE_CONSTRAINT, e.getMessage(), Response.Status.CONFLICT);
    }

    private RestApiException restApiExceptionWithDenialReason(
            String operationDescription,
            HostName hostName,
            HostStateChangeDeniedException e) {
        return createRestApiException(operationDescription, hostName, e, e.getConstraintName(), e.getMessage(),
                Response.Status.CONFLICT);
    }

    private RestApiException createRestApiException(
            String operationDescription, HostName hostname, Exception e, String constraint, String message, int status) {
        HostStateChangeDenialReason hostStateChangeDenialReason = new HostStateChangeDenialReason(
                constraint, operationDescription + " failed: " + message);
        UpdateHostResponse response = new UpdateHostResponse(hostname.s(), hostStateChangeDenialReason);
        return new RestApiException(
                new JacksonJsonResponse<>(status, response, restApi().jacksonJsonMapper(), true),
                hostStateChangeDenialReason.toString(),
                e);
    }
}