aboutsummaryrefslogtreecommitdiffstats
path: root/zookeeper-server/zookeeper-server-common/src/test/java/com/yahoo/vespa/zookeeper/ReconfigurerTest.java
blob: ea8dcac945cd7aef87cb821a7ae0731962212cf4 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.zookeeper;

import com.yahoo.cloud.config.ZookeeperServerConfig;
import com.yahoo.net.HostName;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.time.Duration;
import java.util.Arrays;
import java.util.concurrent.Phaser;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;

/**
 * Tests dynamic reconfiguration of zookeeper cluster.
 *
 * @author hmusum
 */
public class ReconfigurerTest {

    private File cfgFile;
    private File idFile;
    private TestableReconfigurer reconfigurer;

    @Rule
    public TemporaryFolder folder = new TemporaryFolder();

    @Before
    public void setup() throws IOException {
        cfgFile = folder.newFile();
        idFile = folder.newFile("myid");
        reconfigurer = new TestableReconfigurer(new TestableVespaZooKeeperAdmin());
    }

    @Test
    public void testReconfigure() {
        ZookeeperServerConfig initialConfig = createConfig(3, true);
        reconfigurer.startOrReconfigure(initialConfig);
        assertSame(initialConfig, reconfigurer.activeConfig());

        // Cluster grows
        ZookeeperServerConfig nextConfig = createConfig(5, true);
        reconfigurer.startOrReconfigure(nextConfig);
        assertEquals("node1:2181", reconfigurer.connectionSpec());
        assertEquals("server.0=node0:2182:2183;2181,server.1=node1:2182:2183;2181,server.2=node2:2182:2183;2181,server.3=node3:2182:2183;2181,server.4=node4:2182:2183;2181",
                     reconfigurer.servers());
        assertEquals(2, reconfigurer.reconfigurations());
        assertSame(nextConfig, reconfigurer.activeConfig());

        // Cluster shrinks
        nextConfig = createConfig(3, true);
        reconfigurer.startOrReconfigure(nextConfig);
        assertEquals(3, reconfigurer.reconfigurations());
        assertEquals("node1:2181", reconfigurer.connectionSpec());
        assertEquals("server.0=node0:2182:2183;2181,server.1=node1:2182:2183;2181,server.2=node2:2182:2183;2181",
                     reconfigurer.servers());
        assertSame(nextConfig, reconfigurer.activeConfig());

        // Cluster loses node1, but node3 joins. Indices are shuffled.
        nextConfig = createConfig(3, true, 1);
        reconfigurer.startOrReconfigure(nextConfig);
        assertEquals(4, reconfigurer.reconfigurations());
        assertEquals("server.0=node0:2182:2183;2181,server.1=node2:2182:2183;2181,server.2=node3:2182:2183;2181",
                     reconfigurer.servers());
        assertSame(nextConfig, reconfigurer.activeConfig());
    }

    @Test
    public void testReconfigureFailsWithReconfigInProgressThenSucceeds() {
        reconfigurer = new TestableReconfigurer(new TestableVespaZooKeeperAdmin().failures(3));
        ZookeeperServerConfig initialConfig = createConfig(3, true);
        reconfigurer.startOrReconfigure(initialConfig);
        assertSame(initialConfig, reconfigurer.activeConfig());

        ZookeeperServerConfig nextConfig = createConfig(5, true);
        reconfigurer.startOrReconfigure(nextConfig);
        assertEquals("node1:2181", reconfigurer.connectionSpec());
        assertEquals("server.0=node0:2182:2183;2181,server.1=node1:2182:2183;2181,server.2=node2:2182:2183;2181,server.3=node3:2182:2183;2181,server.4=node4:2182:2183;2181",
                     reconfigurer.servers());
        assertEquals(2, reconfigurer.reconfigurations());
        assertSame(nextConfig, reconfigurer.activeConfig());
    }

    @Test
    public void testDynamicReconfigurationDisabled() {
        ZookeeperServerConfig initialConfig = createConfig(3, false);
        reconfigurer.startOrReconfigure(initialConfig);
        assertSame(initialConfig, reconfigurer.activeConfig());

        ZookeeperServerConfig nextConfig = createConfig(5, false);
        reconfigurer.startOrReconfigure(nextConfig);
        assertSame(initialConfig, reconfigurer.activeConfig());
        assertEquals(0, reconfigurer.reconfigurations());
    }

    @After
    public void stopReconfigurer() {
       reconfigurer.deconstruct();
    }

    private ZookeeperServerConfig createConfig(int numberOfServers, boolean dynamicReconfiguration, int... retiredIndices) {
        Arrays.sort(retiredIndices);
        ZookeeperServerConfig.Builder builder = new ZookeeperServerConfig.Builder();
        builder.zooKeeperConfigFile(cfgFile.getAbsolutePath());
        builder.myidFile(idFile.getAbsolutePath());
        for (int i = 0, index = 0; i < numberOfServers; i++, index++) {
            boolean retired = Arrays.binarySearch(retiredIndices, index) >= 0;
            if (retired) i--;
            builder.server(newServer(i, "node" + index, retired));
        }

        builder.myid(0);
        builder.dynamicReconfiguration(dynamicReconfiguration);
        return builder.build();
    }

    private ZookeeperServerConfig.Server.Builder newServer(int id, String hostName, boolean retired) {
        ZookeeperServerConfig.Server.Builder builder = new ZookeeperServerConfig.Server.Builder();
        builder.id(id);
        builder.hostname(hostName);
        builder.retired(retired);
        return builder;
    }

    private static class MockQuorumPeer implements QuorumPeer {
        final Phaser phaser = new Phaser(2); // Runner and test thread.
        @Override public void start(Path path) { assertEquals(1, phaser.arriveAndAwaitAdvance()); }
        @Override public void shutdown(Duration timeout) { assertEquals(1, phaser.arriveAndAwaitAdvance()); }
    }

    private static class TestableReconfigurer extends Reconfigurer implements VespaZooKeeperServer {

        private final TestableVespaZooKeeperAdmin zooKeeperAdmin;
        private final Phaser phaser = new Phaser(2);
        private QuorumPeer serverPeer;

        TestableReconfigurer(TestableVespaZooKeeperAdmin zooKeeperAdmin) {
            super(zooKeeperAdmin, new Sleeper() {
                      @Override
                      public void sleep(Duration duration) {
                          // Do nothing
                      }
                  },
                  false,
                  Reconfigurer.TIMEOUT);
            this.zooKeeperAdmin = zooKeeperAdmin;
            HostName.setHostNameForTestingOnly("node1");
        }

        void startOrReconfigure(ZookeeperServerConfig newConfig) {
            serverPeer = startOrReconfigure(newConfig, this, MockQuorumPeer::new);
            phaser.arriveAndAwaitAdvance(); // Let runner thread see us setting the peer—used below in start(Path)—the first time.
        }

        String connectionSpec() {
            return zooKeeperAdmin.connectionSpec;
        }

        String servers() {
            return zooKeeperAdmin.servers;
        }

        int reconfigurations() {
            return zooKeeperAdmin.reconfigurations;
        }

        @Override
        public void shutdown() {
            serverPeer.shutdown(Duration.ofSeconds(1));
        }

        @Override
        public void start(Path configFilePath) {
            phaser.awaitAdvance(phaser.arriveAndDeregister());
            serverPeer.start(configFilePath);
        }

        @Override
        public boolean reconfigurable() {
            return true;
        }

    }

    private static class TestableVespaZooKeeperAdmin implements VespaZooKeeperAdmin {

        String connectionSpec;
        String servers;
        int reconfigurations = 0;

        private int failures = 0;
        private int attempts = 0;

        public TestableVespaZooKeeperAdmin failures(int failures) {
            this.failures = failures;
            return this;
        }

        @Override
        public void reconfigure(String connectionSpec, String servers) throws ReconfigException {
            if (++attempts < failures)
                throw new ReconfigException("Reconfig failed");
            this.connectionSpec = connectionSpec;
            this.servers = servers;
            this.reconfigurations++;
        }

    }


}