aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/SessionPrepareHandlerTest.java
blob: 11c0cf057cc140a2bc9e64e9d78642373bb9a007 (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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// Copyright 2017 Yahoo Holdings. 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.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.UncheckedTimeoutException;
import com.yahoo.config.application.api.ApplicationFile;
import com.yahoo.config.application.api.DeployLogger;
import com.yahoo.config.model.api.ServiceInfo;
import com.yahoo.config.model.test.MockApplicationPackage;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.ApplicationLockException;
import com.yahoo.config.provision.OutOfCapacityException;
import com.yahoo.config.provision.TenantName;
import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.jdisc.http.HttpRequest;
import com.yahoo.path.Path;
import com.yahoo.slime.JsonDecoder;
import com.yahoo.slime.Slime;
import com.yahoo.transaction.NestedTransaction;
import com.yahoo.transaction.Transaction;
import com.yahoo.vespa.config.server.ApplicationRepository;
import com.yahoo.vespa.config.server.MockReloadHandler;
import com.yahoo.vespa.config.server.TestComponentRegistry;
import com.yahoo.vespa.config.server.application.ApplicationSet;
import com.yahoo.vespa.config.server.application.OrchestratorMock;
import com.yahoo.vespa.config.server.application.TenantApplications;
import com.yahoo.vespa.config.server.host.HostRegistry;
import com.yahoo.vespa.config.server.configchange.ConfigChangeActions;
import com.yahoo.vespa.config.server.configchange.MockRefeedAction;
import com.yahoo.vespa.config.server.configchange.MockRestartAction;
import com.yahoo.vespa.config.server.http.*;
import com.yahoo.vespa.config.server.session.*;
import com.yahoo.vespa.config.server.tenant.TenantBuilder;
import com.yahoo.vespa.config.server.tenant.TenantRepository;
import com.yahoo.vespa.curator.Curator;
import com.yahoo.vespa.curator.mock.MockCurator;
import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.Clock;
import java.time.Instant;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import static com.yahoo.jdisc.Response.Status.*;
import static com.yahoo.jdisc.Response.Status.BAD_REQUEST;
import static com.yahoo.jdisc.Response.Status.NOT_FOUND;
import static com.yahoo.vespa.config.server.http.HandlerTest.assertHttpStatusCodeErrorCodeAndMessage;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

/**
 * @author hmusum
 */
public class SessionPrepareHandlerTest extends SessionHandlerTest {
    private static final TenantName tenant = TenantName.from("test");

    private final TestComponentRegistry componentRegistry = new TestComponentRegistry.Builder().build();
    private final Clock clock = componentRegistry.getClock();

    private Curator curator;
    private LocalSessionRepo localRepo;

    private String preparedMessage = " prepared.\"}";
    private String tenantMessage = "";
    private RemoteSessionRepo remoteSessionRepo;
    private TenantRepository tenantRepository;

    @Before
    public void setupRepo() {
        curator = new MockCurator();
        localRepo = new LocalSessionRepo(clock, curator);
        pathPrefix = "/application/v2/tenant/" + tenant + "/session/";
        preparedMessage = " for tenant '" + tenant + "' prepared.\"";
        tenantMessage = ",\"tenant\":\"" + tenant + "\"";
        tenantRepository = new TenantRepository(componentRegistry, false);
        remoteSessionRepo = new RemoteSessionRepo(tenant);
        TenantBuilder tenantBuilder = TenantBuilder.create(componentRegistry, tenant)
                .withSessionFactory(new MockSessionFactory())
                .withLocalSessionRepo(localRepo)
                .withRemoteSessionRepo(remoteSessionRepo)
                .withApplicationRepo(TenantApplications.create(curator, new MockReloadHandler(), tenant));
        tenantRepository.addTenant(tenantBuilder);
    }

    @Test
    public void require_error_when_session_id_does_not_exist() throws Exception {
        // No session with this id exists
        HttpResponse response = createHandler().handle(SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.PUT, Cmd.PREPARED, 9999L));
        assertHttpStatusCodeErrorCodeAndMessage(response, NOT_FOUND, HttpErrorResponse.errorCodes.NOT_FOUND, "Session 9999 was not found");
    }

    @Test
    public void require_error_when_session_id_not_a_number() throws Exception {
        final String session = "notanumber/prepared";
        HttpResponse response = createHandler().handle(SessionHandlerTest.createTestRequest(pathPrefix + session));
        assertHttpStatusCodeErrorCodeAndMessage(response, BAD_REQUEST,
                                                HttpErrorResponse.errorCodes.BAD_REQUEST,
                                                "Session id in request is not a number, request was 'http://" + hostname + ":" + port + pathPrefix + session + "'");
    }

    @Test
    public void require_that_handler_gives_error_for_unsupported_methods() throws Exception {
        testUnsupportedMethod(SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.POST, Cmd.PREPARED, 1L));
        testUnsupportedMethod(SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.DELETE, Cmd.PREPARED, 1L));
    }

    private void testUnsupportedMethod(com.yahoo.container.jdisc.HttpRequest request) throws Exception {
        HttpResponse response = createHandler().handle(request);
        assertHttpStatusCodeErrorCodeAndMessage(response, METHOD_NOT_ALLOWED,
                                                HttpErrorResponse.errorCodes.METHOD_NOT_ALLOWED,
                                                "Method '" + request.getMethod().name() + "' is not supported");
    }

    @Test
    public void require_that_activate_url_is_returned_on_success() throws Exception {
        MockSession session = new MockSession(1, null);
        localRepo.addSession(session);
        HttpResponse response = createHandler().handle(
                SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.PUT, Cmd.PREPARED, 1L));
        assertThat(session.getStatus(), is(Session.Status.PREPARE));
        assertNotNull(response);
        assertThat(response.getStatus(), is(OK));
        assertResponseContains(response, "\"activate\":\"http://foo:1337" + pathPrefix + "1/active\",\"message\":\"Session 1" + preparedMessage);
    }

    @Test
    public void require_debug() throws Exception {
        HttpResponse response = createHandler().handle(
                SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.PUT, Cmd.PREPARED, 9999L, "?debug=true"));
        assertThat(response.getStatus(), is(NOT_FOUND));
        assertThat(SessionHandlerTest.getRenderedString(response), containsString("NotFoundException"));
    }

    @Test
    public void require_verbose() throws Exception {
        MockSession session = new MockSession(1, null);
        session.doVerboseLogging = true;
        localRepo.addSession(session);
        HttpResponse response = createHandler().handle(
                SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.PUT, Cmd.PREPARED, 1L, "?verbose=true"));
        assertThat(response.getStatus(), is(OK));
        assertThat(SessionHandlerTest.getRenderedString(response), containsString("debuglog"));
    }

    /**
     * A mock remote session repo based on contents of local repo. Only works when there is just one session in local repo
     */
    // TODO: Fix this mess
    private SessionZooKeeperClient fromLocalSessionRepo(LocalSessionRepo localRepo) {
        SessionZooKeeperClient zooKeeperClient = null;
        for (LocalSession ls : localRepo.listSessions()) {
            zooKeeperClient = new MockSessionZKClient(curator, tenant, ls.getSessionId());
            if (ls.getStatus()!=null) zooKeeperClient.writeStatus(ls.getStatus());
            RemoteSession remSess = new RemoteSession(tenant, ls.getSessionId(),
                                                      new TestComponentRegistry.Builder().curator(curator).build(),
                                                      zooKeeperClient);
            remoteSessionRepo.addSession(remSess);
        }
        return zooKeeperClient;
    }

    @Test
    public void require_get_response_activate_url_on_ok() throws Exception {
        MockSession session = new MockSession(1, null);
        localRepo.addSession(session);
        SessionHandler sessHandler = createHandler();
        sessHandler.handle(SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.PUT, Cmd.PREPARED, 1L));
        session.setStatus(Session.Status.PREPARE);
        SessionZooKeeperClient zooKeeperClient = fromLocalSessionRepo(localRepo);
        zooKeeperClient.writeStatus(Session.Status.PREPARE);
        HttpResponse getResponse = sessHandler.handle(
                SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.GET, Cmd.PREPARED, 1L));
        assertResponseContains(getResponse, "\"activate\":\"http://foo:1337" + pathPrefix +
                "1/active\",\"message\":\"Session 1" + preparedMessage);
    }

    @Test
    public void require_get_response_error_on_not_prepared() throws Exception {
        MockSession session = new MockSession(1, null);
        localRepo.addSession(session);
        SessionHandler sessHandler = createHandler();
        session.setStatus(Session.Status.NEW);
        SessionZooKeeperClient zooKeeperClient = fromLocalSessionRepo(localRepo);
        zooKeeperClient.writeStatus(Session.Status.NEW);
        HttpResponse getResponse = sessHandler.handle(
                SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.GET, Cmd.PREPARED, 1L));
        assertHttpStatusCodeErrorCodeAndMessage(getResponse, BAD_REQUEST,
                                                HttpErrorResponse.errorCodes.BAD_REQUEST,
                                                "Session not prepared: 1");
        session.setStatus(Session.Status.ACTIVATE);
        zooKeeperClient.writeStatus(Session.Status.ACTIVATE);
        getResponse = sessHandler.handle(
                SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.GET, Cmd.PREPARED, 1L));
        assertHttpStatusCodeErrorCodeAndMessage(getResponse, BAD_REQUEST,
                                                HttpErrorResponse.errorCodes.BAD_REQUEST,
                                                "Session is active: 1");
    }

    @Test
    public void require_cannot_prepare_active_session() throws Exception {
        MockSession session = new MockSession(1, null);
        localRepo.addSession(session);
        session.setStatus(Session.Status.ACTIVATE);
        SessionHandler sessionHandler = createHandler();
        HttpResponse putResponse = sessionHandler.handle(
                SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.PUT, Cmd.PREPARED, 1L));
        assertHttpStatusCodeErrorCodeAndMessage(putResponse, BAD_REQUEST,
                                                HttpErrorResponse.errorCodes.BAD_REQUEST,
                                                "Session is active: 1");
    }

    @Test
    public void require_get_response_error_when_session_id_does_not_exist() throws Exception {
        MockSession session = new MockSession(1, null);
        localRepo.addSession(session);
        SessionHandler sessHandler = createHandler();
        HttpResponse getResponse = sessHandler.handle(
                SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.GET, Cmd.PREPARED, 9999L));
        assertHttpStatusCodeErrorCodeAndMessage(getResponse, NOT_FOUND,
                                                HttpErrorResponse.errorCodes.NOT_FOUND,
                                                "Session 9999 was not found");
    }

    @Test
    public void require_that_tenant_is_in_response() throws Exception {
        MockSession session = new MockSession(1, null);
        localRepo.addSession(session);
        HttpResponse response = createHandler().handle(
                SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.PUT, Cmd.PREPARED, 1L));
        assertNotNull(response);
        assertThat(response.getStatus(), is(OK));
        assertThat(session.getStatus(), is(Session.Status.PREPARE));
        assertResponseContains(response, tenantMessage);
    }

    @Test
    public void require_that_preparing_with_multiple_tenants_work() throws Exception {
        // Need different repo for 'test2' tenant
        LocalSessionRepo localRepoDefault = new LocalSessionRepo(clock, curator);
        final TenantName defaultTenant = TenantName.from("test2");
        TenantBuilder defaultTenantBuilder = TenantBuilder.create(componentRegistry, defaultTenant)
                .withLocalSessionRepo(localRepoDefault)
                .withRemoteSessionRepo(new RemoteSessionRepo(defaultTenant))
                .withSessionFactory(new MockSessionFactory());
        tenantRepository.addTenant(defaultTenantBuilder);
        final SessionHandler handler = createHandler();

        long sessionId = 1;
        // Deploy with default tenant
        MockSession session = new MockSession(sessionId, null);
        localRepoDefault.addSession(session);
        pathPrefix = "/application/v2/tenant/" + defaultTenant + "/session/";

        HttpResponse response = handler.handle(
                SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.PUT, Cmd.PREPARED, sessionId));
        assertNotNull(response);
        assertThat(SessionHandlerTest.getRenderedString(response), response.getStatus(), is(OK));
        assertThat(session.getStatus(), is(Session.Status.PREPARE));

        // Same session id, as this is for another tenant
        session = new MockSession(sessionId, null);
        localRepo.addSession(session);
        String applicationName = "myapp";
        pathPrefix = "/application/v2/tenant/" + tenant + "/session/" + sessionId +
                "/prepared?applicationName=" + applicationName;
        response = handler.handle(SessionHandlerTest.createTestRequest(pathPrefix));
        assertNotNull(response);
        assertThat(SessionHandlerTest.getRenderedString(response), response.getStatus(), is(OK));
        assertThat(session.getStatus(), is(Session.Status.PREPARE));

        sessionId++;
        session = new MockSession(sessionId, null);
        localRepo.addSession(session);
        pathPrefix = "/application/v2/tenant/" + tenant + "/session/" + sessionId +
                "/prepared?applicationName=" + applicationName + "&instance=quux";
        response = handler.handle(SessionHandlerTest.createTestRequest(pathPrefix));
        assertNotNull(response);
        assertThat(SessionHandlerTest.getRenderedString(response), response.getStatus(), is(OK));
        assertThat(session.getStatus(), is(Session.Status.PREPARE));
    }

    @Test
    public void require_that_config_change_actions_are_in_response() throws Exception {
        MockSession session = new MockSession(1, null);
        localRepo.addSession(session);
        HttpResponse response = createHandler().handle(
                SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.PUT, Cmd.PREPARED, 1L));
        assertResponseContains(response, "\"configChangeActions\":{\"restart\":[],\"refeed\":[]}");
    }

    @Test
    public void require_that_config_change_actions_are_logged_if_existing() throws Exception {
        List<ServiceInfo> services = Collections.singletonList(
                new ServiceInfo("serviceName", "serviceType", null,
                                ImmutableMap.of("clustername", "foo", "clustertype", "bar"), "configId", "hostName"));
        ConfigChangeActions actions = new ConfigChangeActions(Arrays.asList(
                new MockRestartAction("change", services),
                new MockRefeedAction("change-id", false, "other change", services, "test")));
        MockSession session = new MockSession(1, null, actions);
        localRepo.addSession(session);
        HttpResponse response = createHandler().handle(
                SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.PUT, Cmd.PREPARED, 1L));
        assertResponseContains(response,
                               "Change(s) between active and new application that require restart:\\nIn cluster 'foo' of type 'bar");
        assertResponseContains(response,
                               "Change(s) between active and new application that may require re-feed:\\nchange-id: Consider removing data and re-feed document type 'test'");
    }

    @Test
    public void require_that_config_change_actions_are_not_logged_if_not_existing() throws Exception {
        MockSession session = new MockSession(1, null);
        localRepo.addSession(session);
        HttpResponse response = createHandler().handle(
                SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.PUT, Cmd.PREPARED, 1L));
        assertResponseNotContains(response, "Change(s) between active and new application that require restart");
        assertResponseNotContains(response, "Change(s) between active and new application that require re-feed");
    }

    @Test
    public void test_out_of_capacity_response() throws InterruptedException, IOException {
        String message = "Internal error";
        SessionThrowingException session = new SessionThrowingException(new OutOfCapacityException(message));
        localRepo.addSession(session);
        HttpResponse response = createHandler()
                .handle(SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.PUT, Cmd.PREPARED, 1L));
        assertEquals(400, response.getStatus());
        Slime data = getData(response);
        assertThat(data.get().field("error-code").asString(), is(HttpErrorResponse.errorCodes.OUT_OF_CAPACITY.name()));
        assertThat(data.get().field("message").asString(), is(message));
    }

    @Test
    public void test_that_nullpointerexception_gives_internal_server_error() throws InterruptedException, IOException {
        String message = "No nodes available";
        SessionThrowingException session = new SessionThrowingException(new NullPointerException(message));
        localRepo.addSession(session);
        HttpResponse response = createHandler()
                .handle(SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.PUT, Cmd.PREPARED, 1L));
        assertEquals(500, response.getStatus());
        Slime data = getData(response);
        assertThat(data.get().field("error-code").asString(), is(HttpErrorResponse.errorCodes.INTERNAL_SERVER_ERROR.name()));
        assertThat(data.get().field("message").asString(), is(message));
    }

    @Test
    public void test_application_lock_failure() throws InterruptedException, IOException {
        String message = "Timed out after waiting PT1M to acquire lock '/provision/v1/locks/foo/bar/default'";
        SessionThrowingException session =
                new SessionThrowingException(new ApplicationLockException(new UncheckedTimeoutException(message)));
        localRepo.addSession(session);
        HttpResponse response = createHandler()
                .handle(SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.PUT, Cmd.PREPARED, 1L));
        assertEquals(500, response.getStatus());
        Slime data = getData(response);
        assertThat(data.get().field("error-code").asString(), is(HttpErrorResponse.errorCodes.APPLICATION_LOCK_FAILURE.name()));
        assertThat(data.get().field("message").asString(), is(message));
    }

    private Slime getData(HttpResponse response) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        response.render(baos);
        Slime data = new Slime();
        new JsonDecoder().decode(data, baos.toByteArray());
        return data;
    }

    private static void assertResponseContains(HttpResponse response, String string) throws IOException {
        assertThat(SessionHandlerTest.getRenderedString(response), containsString(string));
    }

    private static void assertResponseNotContains(HttpResponse response, String string) throws IOException {
        assertThat(SessionHandlerTest.getRenderedString(response), not(containsString(string)));
    }

    private SessionHandler createHandler() {
        return new SessionPrepareHandler(
                SessionPrepareHandler.testOnlyContext(),
                new ApplicationRepository(tenantRepository,
                                          new MockProvisioner(),
                                          new OrchestratorMock(),
                                          clock),
                tenantRepository,
                componentRegistry.getConfigserverConfig());

    }

    public static class SessionThrowingException extends LocalSession {
        private final RuntimeException exception;

        SessionThrowingException(RuntimeException exception) {
            super(TenantName.defaultName(), 1, null,
                  new SessionContext(null,
                                     new MockSessionZKClient(MockApplicationPackage.createEmpty()),
                                     null,
                                     null,
                                     new HostRegistry<>(),
                                     null,
                                     null));
            this.exception = exception;
        }

        @Override
        public ConfigChangeActions prepare(DeployLogger logger,
                                           PrepareParams params,
                                           Optional<ApplicationSet> application,
                                           Path tenantPath,
                                           Instant now) {
            throw exception;
        }

        @Override
        public Session.Status getStatus() {
            return null;
        }

        @Override
        public Transaction createDeactivateTransaction() {
            return null;
        }

        @Override
        public Transaction createActivateTransaction() {
            return null;
        }

        @Override
        public ApplicationFile getApplicationFile(Path relativePath, Mode mode) {
            return null;
        }

        @Override
        public ApplicationId getApplicationId() {
            return null;
        }

        @Override
        public long getCreateTime() {
            return 0;
        }

        @Override
        public void delete(NestedTransaction transaction) {  }
    }
}