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

import com.yahoo.config.provision.ApplicationId;
import com.yahoo.exception.ExceptionUtils;
import com.yahoo.jdisc.Metric;
import com.yahoo.jdisc.Timer;
import com.yahoo.jdisc.test.TestTimer;
import com.yahoo.test.ManualClock;
import com.yahoo.vespa.applicationmodel.ApplicationInstanceReference;
import com.yahoo.vespa.applicationmodel.HostName;
import com.yahoo.vespa.curator.Curator;
import com.yahoo.vespa.orchestrator.OrchestratorContext;
import com.yahoo.vespa.orchestrator.OrchestratorUtil;
import com.yahoo.vespa.orchestrator.TestIds;
import com.yahoo.vespa.service.monitor.AntiServiceMonitor;
import com.yahoo.vespa.service.monitor.CriticalRegion;
import com.yahoo.yolean.Exceptions;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.test.KillSession;
import org.apache.curator.test.TestingServer;
import org.apache.zookeeper.data.Stat;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.junit.MockitoJUnitRunner;

import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class ZkStatusServiceTest {
    private TestingServer testingServer;
    private ZkStatusService statusService;
    private Curator curator;
    private final Timer timer = mock(Timer.class);
    private final Metric metric = mock(Metric.class);
    private final OrchestratorContext context = mock(OrchestratorContext.class);
    private final CriticalRegion criticalRegion = mock(CriticalRegion.class);
    private final AntiServiceMonitor antiServiceMonitor = mock(AntiServiceMonitor.class);

    @Captor
    private ArgumentCaptor<Map<String, String>> captor;

    @Before
    public void setUp() throws Exception {
        Logger.getLogger("").setLevel(Level.WARNING);

        testingServer = new TestingServer();
        curator = createConnectedCurator(testingServer);
        statusService = new ZkStatusService(curator, metric, timer, antiServiceMonitor);
        when(context.getTimeLeft()).thenReturn(Duration.ofSeconds(10));
        when(context.isProbe()).thenReturn(false);
        when(timer.currentTime()).thenReturn(Instant.ofEpochMilli(1));
        when(timer.toUtcClock()).thenReturn(new ManualClock(Instant.ofEpochMilli(1)));
        when(antiServiceMonitor.disallowDuperModelLockAcquisition(any())).thenReturn(criticalRegion);
    }

    private static Curator createConnectedCurator(TestingServer server) throws InterruptedException {
        Curator curator = Curator.create(server.getConnectString(), Optional.empty());
        curator.framework().blockUntilConnected(1, TimeUnit.MINUTES);
        return curator;
    }

    @After
    public void tearDown() throws Exception {
        if (curator != null) { //teardown is called even if setUp fails.
            curator.close();
            curator = null;
        }
        if (testingServer != null) {
            testingServer.close();
        }
    }

    @Test
    public void host_state_for_unknown_hosts_is_no_remarks() {
        assertEquals(HostStatus.NO_REMARKS,
                statusService.getHostInfo(TestIds.APPLICATION_INSTANCE_REFERENCE, TestIds.HOST_NAME1).status());
    }

    @Test
    public void setting_host_state_is_idempotent() {
        when(timer.currentTime()).thenReturn(
                Instant.ofEpochMilli((1)),
                Instant.ofEpochMilli((3)),
                Instant.ofEpochMilli(6));

        try (ApplicationLock lock = statusService.lockApplication(context, TestIds.APPLICATION_INSTANCE_REFERENCE)) {

            //shuffling to catch "clean database" failures for all cases.
            for (HostStatus hostStatus: shuffledList(HostStatus.NO_REMARKS, HostStatus.ALLOWED_TO_BE_DOWN)) {
                for (int i = 0; i < 2; i++) {
                    lock.setHostState(TestIds.HOST_NAME1, hostStatus);

                    assertEquals(hostStatus, lock.getHostInfos().getOrNoRemarks(TestIds.HOST_NAME1).status());
                }
            }
        }
    }

    @Test
    public void locks_are_exclusive() throws Exception {
        ZkStatusService zkStatusService2 =
                new ZkStatusService(curator, mock(Metric.class), new TestTimer(), antiServiceMonitor);

        final CompletableFuture<Void> lockedSuccessfullyFuture;
        try (ApplicationLock lock = statusService
                .lockApplication(context, TestIds.APPLICATION_INSTANCE_REFERENCE)) {

            lockedSuccessfullyFuture = CompletableFuture.runAsync(() -> {
                try (ApplicationLock lock2 = zkStatusService2
                        .lockApplication(context, TestIds.APPLICATION_INSTANCE_REFERENCE))
                {
                }
            });

            try {
                lockedSuccessfullyFuture.get(3, TimeUnit.SECONDS);
                fail("Both zookeeper host status services locked simultaneously for the same application instance");
            } catch (TimeoutException ignored) {
            }
        }

        lockedSuccessfullyFuture.get(1, TimeUnit.MINUTES);
    }

    @Test
    public void failing_to_get_lock_closes_SessionFailRetryLoop() {
        when(context.getTimeLeft()).thenReturn(Duration.ofMillis(100));
        ZkStatusService zkStatusService2 =
                new ZkStatusService(curator, mock(Metric.class), new TestTimer(), antiServiceMonitor);

        try (ApplicationLock lock = statusService
                .lockApplication(context, TestIds.APPLICATION_INSTANCE_REFERENCE)) {

            // must run in separate thread, since having 2 locks in the same thread fails
            CompletableFuture<Void> resultOfZkOperationAfterLockFailure = CompletableFuture.runAsync(() -> {
                try {
                    zkStatusService2.lockApplication(context, TestIds.APPLICATION_INSTANCE_REFERENCE);
                    fail("Both zookeeper host status services locked simultaneously for the same application instance");
                }
                catch (RuntimeException expected) { }
                finally {
                    killSession(curator.framework(), testingServer);
                }

                // Throws SessionFailedException if the SessionFailRetryLoop has not been closed.
                lock.getHostInfos().getOrNoRemarks(TestIds.HOST_NAME1);
            });

            assertThat(resultOfZkOperationAfterLockFailure, notHoldsException());
        }
    }

    //IsNot does not delegate to matcher.describeMismatch. See the related issue
    //https://code.google.com/p/hamcrest/issues/detail?id=107  Confusing failure description when using negation
    //Creating not(holdsException) directly instead.
    private Matcher<Future<?>> notHoldsException() {
        return new TypeSafeMatcher<>() {
            @Override
            protected boolean matchesSafely(Future<?> item) {
                return getException(item).isEmpty();
            }

            private Optional<Throwable> getException(Future<?> item) {
                try {
                    item.get();
                    return Optional.empty();
                }
                catch (ExecutionException e) {
                    return Optional.of(e.getCause());
                }
                catch (InterruptedException e) {
                    return Optional.of(e);
                }
            }

            @Override
            public void describeTo(Description description) {
                description.appendText("notHoldsException()");
            }

            @Override
            protected void describeMismatchSafely(Future<?> item, Description mismatchDescription) {
                getException(item).ifPresent(throwable ->
                                                     mismatchDescription
                                                             .appendText("Got exception: ")
                                                             .appendText(Exceptions.toMessageString(throwable))
                                                             .appendText(ExceptionUtils.getStackTraceRecursivelyAsString(throwable)));
            }
        };
    }

    @SuppressWarnings("deprecation")
    private static void killSession(CuratorFramework curatorFramework, TestingServer testingServer) {
        try {
            KillSession.kill(curatorFramework.getZookeeperClient().getZooKeeper(), testingServer.getConnectString());
        } catch (Exception e) {
            throw new RuntimeException("Failed killing session. ", e);
        }
    }

    @Test
    public void suspend_and_resume_application_works_and_is_symmetric() {

        // Initial state is NO_REMARK
        assertEquals(ApplicationInstanceStatus.NO_REMARKS,
                statusService.getApplicationInstanceStatus(TestIds.APPLICATION_INSTANCE_REFERENCE));

        // Suspend
        try (ApplicationLock lock = statusService
                .lockApplication(context, TestIds.APPLICATION_INSTANCE_REFERENCE)) {
            lock.setApplicationInstanceStatus(ApplicationInstanceStatus.ALLOWED_TO_BE_DOWN);
        }

        assertEquals(ApplicationInstanceStatus.ALLOWED_TO_BE_DOWN,
                statusService.getApplicationInstanceStatus(TestIds.APPLICATION_INSTANCE_REFERENCE));

        // Resume
        try (ApplicationLock lock = statusService
                .lockApplication(context, TestIds.APPLICATION_INSTANCE_REFERENCE)) {
            lock.setApplicationInstanceStatus(ApplicationInstanceStatus.NO_REMARKS);
        }

        assertEquals(ApplicationInstanceStatus.NO_REMARKS,
                statusService.getApplicationInstanceStatus(TestIds.APPLICATION_INSTANCE_REFERENCE));
    }

    @Test
    public void suspending_two_applications_returns_two_applications() {
        Set<ApplicationInstanceReference> suspendedApps = statusService.getAllSuspendedApplications();
        assertEquals(0, suspendedApps.size());

        try (ApplicationLock statusRegistry = statusService
                .lockApplication(context, TestIds.APPLICATION_INSTANCE_REFERENCE)) {
            statusRegistry.setApplicationInstanceStatus(ApplicationInstanceStatus.ALLOWED_TO_BE_DOWN);
        }

        try (ApplicationLock lock = statusService
                .lockApplication(context, TestIds.APPLICATION_INSTANCE_REFERENCE2)) {
            lock.setApplicationInstanceStatus(ApplicationInstanceStatus.ALLOWED_TO_BE_DOWN);
        }

        suspendedApps = statusService.getAllSuspendedApplications();
        assertEquals(2, suspendedApps.size());
        assertTrue(suspendedApps.contains(TestIds.APPLICATION_INSTANCE_REFERENCE));
        assertTrue(suspendedApps.contains(TestIds.APPLICATION_INSTANCE_REFERENCE2));
    }

    @Test
    public void zookeeper_cleanup() {
        HostName strayHostname = new HostName("stray1.com");

        verify(antiServiceMonitor, times(0)).disallowDuperModelLockAcquisition(any());
        try (ApplicationLock lock = statusService.lockApplication(context, TestIds.APPLICATION_INSTANCE_REFERENCE)) {
            verify(antiServiceMonitor, times(1)).disallowDuperModelLockAcquisition(any());

            lock.setApplicationInstanceStatus(ApplicationInstanceStatus.ALLOWED_TO_BE_DOWN);
            lock.setHostState(TestIds.HOST_NAME1, HostStatus.ALLOWED_TO_BE_DOWN);

            lock.setHostState(strayHostname, HostStatus.PERMANENTLY_DOWN);

            verify(criticalRegion, times(0)).close();
        }
        verify(criticalRegion, times(1)).close();

        verify(antiServiceMonitor, times(1)).disallowDuperModelLockAcquisition(any());
        try (ApplicationLock lock = statusService.lockApplication(context, TestIds.APPLICATION_INSTANCE_REFERENCE2)) {
            verify(antiServiceMonitor, times(2)).disallowDuperModelLockAcquisition(any());

            lock.setApplicationInstanceStatus(ApplicationInstanceStatus.ALLOWED_TO_BE_DOWN);

            verify(criticalRegion, times(1)).close();
        }
        verify(criticalRegion, times(2)).close();

        ApplicationId applicationId = OrchestratorUtil.toApplicationId(TestIds.APPLICATION_INSTANCE_REFERENCE);
        assertEquals("test-tenant:test-application:test-environment:test-region:test-instance-key",
                TestIds.APPLICATION_INSTANCE_REFERENCE.asString());
        assertEquals("test-tenant:test-application:test-instance-key", applicationId.serializedForm());
        assertEquals("host1", TestIds.HOST_NAME1.s());

        String hostStatusPath = "/vespa/host-status/" + applicationId.serializedForm() + "/hosts/" + TestIds.HOST_NAME1.s();
        String lock2Path = "/vespa/host-status-service/" + TestIds.APPLICATION_INSTANCE_REFERENCE.asString() + "/lock2";
        String applicationStatusPath = "/vespa/application-status-service/" + TestIds.APPLICATION_INSTANCE_REFERENCE.asString();
        assertZkPathExists(true, hostStatusPath);
        assertZkPathExists(true, lock2Path);
        assertZkPathExists(true, applicationStatusPath);

        String strayHostStatusPath = "/vespa/host-status/" + applicationId.serializedForm() + "/hosts/" + strayHostname.s();
        String strayHostStatusServicePath = "/vespa/host-status-service/" + TestIds.APPLICATION_INSTANCE_REFERENCE.asString() +
                "/hosts-allowed-down/stray2.com";
        String strayApplicationStatusPath = "/vespa/application-status-service/" + TestIds.APPLICATION_INSTANCE_REFERENCE2.asString();

        createZkNodes(strayHostStatusServicePath);
        assertZkPathExists(true, strayHostStatusPath);
        assertZkPathExists(true, strayHostStatusServicePath);
        assertZkPathExists(true, strayApplicationStatusPath);

        statusService.onApplicationActivate(TestIds.APPLICATION_INSTANCE_REFERENCE2, makeHostnameSet("host1", "host2"));

        // Nothing has been deleted
        assertZkPathExists(true, hostStatusPath);
        assertZkPathExists(true, lock2Path);
        assertZkPathExists(true, applicationStatusPath);
        assertZkPathExists(true, strayHostStatusPath);
        assertZkPathExists(true, strayHostStatusServicePath);
        assertZkPathExists(true, strayApplicationStatusPath);

        statusService.onApplicationActivate(TestIds.APPLICATION_INSTANCE_REFERENCE,
                makeHostnameSet(TestIds.HOST_NAME1.s(), "host3"));

        // Stray hosts for app1 has been deleted
        assertZkPathExists(true, hostStatusPath);
        assertZkPathExists(true, lock2Path);
        assertZkPathExists(true, applicationStatusPath);
        assertZkPathExists(false, strayHostStatusPath);
        assertZkPathExists(true, strayHostStatusServicePath);
        assertZkPathExists(true, strayApplicationStatusPath);

        statusService.onApplicationRemove(TestIds.APPLICATION_INSTANCE_REFERENCE);

        // Application removed => only lock2 path and other apps are left
        assertZkPathExists(false, hostStatusPath);
        assertZkPathExists(true, lock2Path);
        assertZkPathExists(false, applicationStatusPath);
        assertZkPathExists(false, strayHostStatusPath);
        assertZkPathExists(false, strayHostStatusServicePath);
        assertZkPathExists(true, strayApplicationStatusPath);

    }

    private Set<HostName> makeHostnameSet(String... hostnames) {
        return Stream.of(hostnames).map(HostName::new).collect(Collectors.toSet());
    }

    private void assertZkPathExists(boolean exists, String path) {
        final Stat stat;
        try {
            stat = curator.framework().checkExists().forPath(path);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        assertEquals(exists, stat != null);
    }

    private void createZkNodes(String... paths) {
        Stream.of(paths).forEach(path -> {
            try {
                curator.framework().create().creatingParentsIfNeeded().forPath(path);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
    }

    //TODO: move to vespajlib
    @SafeVarargs
    @SuppressWarnings("varargs")
    private static <T> List<T> shuffledList(T... values) {
        //new ArrayList necessary to avoid "write through" behaviour
        List<T> list = new ArrayList<>(Arrays.asList(values));
        Collections.shuffle(list);
        return list;
    }

}