summaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/test/java/com/yahoo/vespa/clustercontroller/core/ZooKeeperDatabaseTest.java
blob: 1ce7586adea838806fc0d59996c4edd768f7bb21 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.core;

import com.yahoo.vespa.clustercontroller.core.database.CasWriteFailed;
import com.yahoo.vespa.clustercontroller.core.database.Database;
import com.yahoo.vespa.clustercontroller.core.database.ZooKeeperDatabase;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.io.IOException;
import java.time.Duration;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;

public class ZooKeeperDatabaseTest {

    @SuppressWarnings("deprecation")
    @Rule
    public ExpectedException expectedException = ExpectedException.none();

    private static class Fixture implements AutoCloseable {
        final ZooKeeperTestServer zkServer;
        ClusterFixture clusterFixture;
        ZooKeeperDatabase zkDatabase;
        int nodeIndex = 1;
        Duration sessionTimeout = Duration.ofSeconds(60);
        Database.DatabaseListener mockListener = mock(Database.DatabaseListener.class);

        Fixture() throws IOException {
            zkServer = new ZooKeeperTestServer();
            clusterFixture = ClusterFixture.forFlatCluster(10);
        }

        void createDatabase() throws Exception {
            closeDatabaseIfOpen();
            var id = new FleetControllerId(clusterFixture.cluster.getName(), nodeIndex);
            var context = new TestFleetControllerContext(id);
            zkDatabase = new ZooKeeperDatabase(context, zkServer.getAddress(),
                                               (int)sessionTimeout.toMillis(), mockListener);
        }

        ZooKeeperDatabase db() { return zkDatabase; }

        void closeDatabaseIfOpen() {
            if (zkDatabase != null) {
                zkDatabase.close();
                zkDatabase = null;
            }
        }

        @Override
        public void close() {
            closeDatabaseIfOpen();
            zkServer.shutdown(true);
        }
    }

    @Test
    public void can_store_and_load_cluster_state_bundle_from_database() throws Exception {
        try (Fixture f = new Fixture()) {
            f.createDatabase();
            f.db().retrieveLastPublishedStateBundle(); // Must be called once prior to prime last known znode version
            ClusterStateBundle bundleToStore = dummyBundle();
            f.db().storeLastPublishedStateBundle(bundleToStore);

            ClusterStateBundle bundleReceived = f.db().retrieveLastPublishedStateBundle();
            assertEquals(bundleToStore, bundleReceived);
        }
    }

    private static ClusterStateBundle dummyBundle() {
        return ClusterStateBundleUtil.makeBundle("distributor:2 storage:2",
                StateMapping.of("default", "distributor:2 storage:2 .0.s:d"),
                StateMapping.of("upsidedown", "distributor:2 .0.s:d storage:2"));
    }

    @Test
    public void storing_cluster_state_bundle_with_mismatching_expected_znode_version_throws_exception() throws Exception {
        expectedException.expect(CasWriteFailed.class);
        expectedException.expectMessage("version mismatch in cluster state bundle znode (expected -2)");
        try (Fixture f = new Fixture()) {
            f.createDatabase();
            f.db().storeLastPublishedStateBundle(dummyBundle());
        }
    }

    @Test
    public void storing_cluster_state_version_with_mismatching_expected_znode_version_throws_exception() throws Exception {
        expectedException.expect(CasWriteFailed.class);
        expectedException.expectMessage("version mismatch in cluster state version znode (expected -2)");
        try (Fixture f = new Fixture()) {
            f.createDatabase();
            f.db().storeLatestSystemStateVersion(12345);
        }
    }

    @Test
    public void empty_state_bundle_is_returned_if_no_bundle_already_stored_in_database() throws Exception {
        try (Fixture f = new Fixture()) {
            f.createDatabase();
            ClusterStateBundle bundleReceived = f.db().retrieveLastPublishedStateBundle();

            assertEquals(ClusterStateBundle.empty(), bundleReceived);
        }
    }

}