summaryrefslogtreecommitdiffstats
path: root/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandler.java
blob: 458a3899d4c367cf80b8fd1627cf02e860f13a6b (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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.http.v2;

import com.yahoo.config.provision.*;
import com.yahoo.container.jdisc.HttpRequest;
import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.container.logging.AccessLog;
import com.yahoo.jdisc.Response;
import com.yahoo.jdisc.application.BindingMatch;
import com.yahoo.log.LogLevel;
import com.yahoo.vespa.config.server.RotationsCache;
import com.yahoo.vespa.config.server.Tenant;
import com.yahoo.vespa.config.server.Tenants;
import com.yahoo.vespa.config.server.TimeoutBudget;
import com.yahoo.vespa.config.server.application.Application;
import com.yahoo.vespa.config.server.application.ApplicationConvergenceChecker;
import com.yahoo.vespa.config.server.application.ApplicationRepo;
import com.yahoo.vespa.config.server.application.LogServerLogGrabber;
import com.yahoo.vespa.config.server.http.*;
import com.yahoo.vespa.config.server.provision.HostProvisionerProvider;
import com.yahoo.vespa.config.server.session.LocalSession;
import com.yahoo.vespa.config.server.session.LocalSessionRepo;
import com.yahoo.vespa.config.server.session.RemoteSession;
import com.yahoo.vespa.config.server.session.RemoteSessionRepo;

import java.io.IOException;
import java.time.Clock;
import java.time.Duration;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Executor;

/**
 * Handler for deleting a currently active application for a tenant.
 *
 * @author musum
 * @since 5.4
 */
public class ApplicationHandler extends HttpHandler {

    private static final String REQUEST_PROPERTY_TIMEOUT = "timeout";
    private final Tenants tenants;
    private final ContentHandler contentHandler = new ContentHandler();
    private final Optional<Provisioner> hostProvisioner;
    private final ApplicationConvergenceChecker convergeChecker;
    private final Zone zone;
    private final LogServerLogGrabber logServerLogGrabber;

    public ApplicationHandler(Executor executor, AccessLog accessLog, Tenants tenants,
                              HostProvisionerProvider hostProvisionerProvider, Zone zone,
                              ApplicationConvergenceChecker convergeChecker,
                              LogServerLogGrabber logServerLogGrabber) {
        super(executor, accessLog);
        this.tenants = tenants;
        this.hostProvisioner = hostProvisionerProvider.getHostProvisioner();
        this.zone = zone;
        this.convergeChecker = convergeChecker;
        this.logServerLogGrabber = logServerLogGrabber;
    }

    @Override
    public HttpResponse handleDELETE(HttpRequest request) {
        ApplicationId applicationId = getApplicationIdFromRequest(request);
        Tenant tenant = verifyTenantAndApplication(applicationId);
        ApplicationRepo applicationRepo = tenant.getApplicationRepo();
        final long sessionId = applicationRepo.getSessionIdForApplication(applicationId);
        final LocalSessionRepo localSessionRepo = tenant.getLocalSessionRepo();
        final LocalSession session = localSessionRepo.getSession(sessionId);
        if (session == null) {
            return HttpErrorResponse.notFoundError("Unable to delete " + applicationId + " (session id " + sessionId + "):" +
                                                   "No local deployment for this application found on this config server");
        }
        log.log(LogLevel.INFO, "Deleting " + applicationId);
        localSessionRepo.removeSession(session.getSessionId());
        session.delete();
        RotationsCache rotationsCache = new RotationsCache(tenant.getCurator(), tenant.getPath());
        rotationsCache.deleteRotationFromZooKeeper(applicationId);
        applicationRepo.deleteApplication(applicationId);
        if (hostProvisioner.isPresent()) {
            hostProvisioner.get().removed(applicationId);
        }
        return new DeleteApplicationResponse(Response.Status.OK, applicationId);
    }


    @Override
    public HttpResponse handleGET(HttpRequest request) {
        ApplicationId applicationId = getApplicationIdFromRequest(request);
        Tenant tenant = verifyTenantAndApplication(applicationId);

        if (isServiceConvergeRequest(request)) {
            Application application = getApplication(tenant, applicationId);
            return convergeChecker.nodeConvergenceCheck(application, getHostFromRequest(request), request.getUri());
        }
        if (isContentRequest(request)) {
            LocalSession session = SessionHandler.getSessionFromRequest(tenant.getLocalSessionRepo(), tenant.getApplicationRepo().getSessionIdForApplication(applicationId));
            return contentHandler.get(ApplicationContentRequest.create(request, session, applicationId, zone));
        }
        Application application = getApplication(tenant, applicationId);

        // TODO: Remove this once the config convegence logic is moved to client and is live for all clusters.
        if (isConvergeRequest(request)) {
            try {
                convergeChecker.waitForConfigConverged(application, new TimeoutBudget(Clock.systemUTC(), durationFromRequestTimeout(request)));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        if (isServiceConvergeListRequest(request)) {
             return convergeChecker.listConfigConvergence(application, request.getUri());
        }
        return new GetApplicationResponse(Response.Status.OK, application.getApplicationGeneration());
    }

    @Override
    public HttpResponse handlePOST(HttpRequest request) {
        ApplicationId applicationId = getApplicationIdFromRequest(request);
        Tenant tenant = verifyTenantAndApplication(applicationId);
        if (request.getUri().getPath().endsWith("restart"))
            return handlePostRestart(request, applicationId);
        if (request.getUri().getPath().endsWith("log"))
            return handlePostLog(request, applicationId, tenant);
        throw new NotFoundException("Illegal POST request '" + request.getUri() + "': Must end by /restart or /log");
    }

    private HttpResponse handlePostRestart(HttpRequest request, ApplicationId applicationId) {
        if (getBindingMatch(request).groupCount() != 7)
            throw new NotFoundException("Illegal POST restart request '" + request.getUri() +
                    "': Must have 6 arguments but had " + ( getBindingMatch(request).groupCount()-1 ) );
        if (hostProvisioner.isPresent())
            hostProvisioner.get().restart(applicationId, hostFilterFrom(request));
        return new JSONResponse(Response.Status.OK); // return empty
    }

    private HttpResponse handlePostLog(HttpRequest request, ApplicationId applicationId, Tenant tenant) {
        if (getBindingMatch(request).groupCount() != 7)
            throw new NotFoundException("Illegal POST log request '" + request.getUri() +
                    "': Must have 6 arguments but had " + ( getBindingMatch(request).groupCount()-1 ) );
        Application application = getApplication(tenant, applicationId);
        return logServerLogGrabber.grabLog(application);
    }

    private HostFilter hostFilterFrom(HttpRequest request) {
        return HostFilter.from(request.getProperty("hostname"),
                               request.getProperty("flavor"),
                               request.getProperty("clusterType"),
                               request.getProperty("clusterId"));
    }

    private Tenant verifyTenantAndApplication(ApplicationId applicationId) {
        Tenant tenant = Utils.checkThatTenantExists(tenants, applicationId.tenant());
        List<ApplicationId> applicationIds = listApplicationIds(tenant);
        if ( ! applicationIds.contains(applicationId)) {
            throw new NotFoundException("No such application id: " + applicationId);
        }
        return tenant;
    }

    private Duration durationFromRequestTimeout(HttpRequest request) {
        long timeoutInSeconds = 60;
        if (request.hasProperty(REQUEST_PROPERTY_TIMEOUT)) {
            timeoutInSeconds = Long.parseLong(request.getProperty(REQUEST_PROPERTY_TIMEOUT));
        }
        return Duration.ofSeconds(timeoutInSeconds);
    }

    private Application getApplication(Tenant tenant, ApplicationId applicationId) {
        ApplicationRepo applicationRepo = tenant.getApplicationRepo();
        RemoteSessionRepo remoteSessionRepo = tenant.getRemoteSessionRepo();
        long sessionId = applicationRepo.getSessionIdForApplication(applicationId);
        RemoteSession session = remoteSessionRepo.getSession(sessionId, 0);
        return session.ensureApplicationLoaded().getForVersionOrLatest(Optional.empty());
    }

    private List<ApplicationId> listApplicationIds(Tenant tenant) {
        ApplicationRepo applicationRepo = tenant.getApplicationRepo();
        return applicationRepo.listApplications();
    }

    // Note: Update src/main/resources/configserver-app/services.xml if you do any changes to the bindings
    private static BindingMatch<?> getBindingMatch(HttpRequest request) {
        return HttpConfigRequests.getBindingMatch(request,
                "http://*/application/v2/tenant/*/application/*/environment/*/region/*/instance/*/content/*",
                "http://*/application/v2/tenant/*/application/*/environment/*/region/*/instance/*/log",
                "http://*/application/v2/tenant/*/application/*/environment/*/region/*/instance/*/restart",
                "http://*/application/v2/tenant/*/application/*/environment/*/region/*/instance/*/converge",
                "http://*/application/v2/tenant/*/application/*/environment/*/region/*/instance/*/serviceconverge",
                "http://*/application/v2/tenant/*/application/*/environment/*/region/*/instance/*/serviceconverge/*",
                "http://*/application/v2/tenant/*/application/*/environment/*/region/*/instance/*",
                "http://*/application/v2/tenant/*/application/*");
    }

    private static boolean isConvergeRequest(HttpRequest request) {
        return getBindingMatch(request).groupCount() == 7 &&
                request.getUri().getPath().endsWith("converge");
    }

    private static boolean isServiceConvergeListRequest(HttpRequest request) {
        return getBindingMatch(request).groupCount() == 7 &&
                request.getUri().getPath().endsWith("serviceconverge");
    }

    private static boolean isServiceConvergeRequest(HttpRequest request) {
        return getBindingMatch(request).groupCount() == 8 &&
                request.getUri().getPath().contains("/serviceconverge/");
    }


    private static boolean isContentRequest(HttpRequest request) {
        return getBindingMatch(request).groupCount() > 7;
    }

    private static String getHostFromRequest(HttpRequest req) {
        BindingMatch<?> bm = getBindingMatch(req);
        return bm.group(7);
    }

    private static ApplicationId getApplicationIdFromRequest(HttpRequest req) {
        // Two bindings for this: with full app id or only application name
        BindingMatch<?> bm = getBindingMatch(req);
        if (bm.groupCount() > 4) return createFromRequestFullAppId(bm);
        return createFromRequestSimpleAppId(bm);
    }

    // The URL pattern with only tenant and application given
    private static ApplicationId createFromRequestSimpleAppId(BindingMatch<?> bm) {
        TenantName tenant = TenantName.from(bm.group(2));
        ApplicationName application = ApplicationName.from(bm.group(3));
        return new ApplicationId.Builder().tenant(tenant).applicationName(application).build();
    }

    // The URL pattern with full app id given
    private static ApplicationId createFromRequestFullAppId(BindingMatch<?> bm) {
        String tenant = bm.group(2);
        String application = bm.group(3);
        String instance = bm.group(6);
        return new ApplicationId.Builder()
            .tenant(tenant)
            .applicationName(application).instanceName(instance)
            .build();
    }

    private static class DeleteApplicationResponse extends JSONResponse {
        public DeleteApplicationResponse(int status, ApplicationId applicationId) {
            super(status);
            object.setString("message", "Application '" + applicationId + "' deleted");
        }
    }

    private static class GetApplicationResponse extends JSONResponse {
        public GetApplicationResponse(int status, long generation) {
            super(status);
            object.setLong("generation", generation);
        }
    }
}