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

import com.yahoo.config.subscription.ConfigSourceSet;
import com.yahoo.jrt.Acceptor;
import com.yahoo.jrt.ListenFailedException;
import com.yahoo.jrt.Request;
import com.yahoo.jrt.Spec;
import com.yahoo.jrt.StringValue;
import com.yahoo.jrt.Supervisor;
import com.yahoo.jrt.Target;
import com.yahoo.jrt.Transport;
import com.yahoo.vespa.config.RawConfig;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;

/**
 * @author hmusum
 * @author bjorncs
 */
public class ConfigProxyRpcServerTest {
    private static final String hostname = "localhost";
    private static final int port = 12345;
    private static final String configSourceAddress = "tcp/" + hostname + ":" + port;
    private static TestServer server;
    private static TestClient client;

    @TempDir
    public File temporaryFolder;

    @BeforeAll
    public static void setup() throws ListenFailedException {
        server = new TestServer();
        client = new TestClient(server.listenPort());
    }

    @AfterAll
    public static void teardown() {
        client.close();
        server.close();
    }

    private static void reset() throws ListenFailedException {
        teardown();
        setup();
    }

    @Test
    void basic() {
        ProxyServer proxy = createTestServer(new MockConfigSource());
        Spec spec = new Spec("localhost", 12345);
        ConfigProxyRpcServer server = new ConfigProxyRpcServer(proxy, new Supervisor(new Transport()), spec);
        assertEquals(spec, server.getSpec());
    }

    /**
     * Tests ping RPC command
     */
    @Test
    void testRpcMethodPing() {
        Request req = new Request("ping");
        client.invoke(req);

        assertFalse(req.isError(), req.errorMessage());
        assertEquals(1, req.returnValues().size());
        assertEquals(0, req.returnValues().get(0).asInt32());
    }

    /**
     * Tests listCachedConfig RPC command
     */
    @Test
    void testRpcMethodListCachedConfig() throws ListenFailedException {
        reset();

        Request req = new Request("listCachedConfig");
        client.invoke(req);

        assertFalse(req.isError(), req.errorMessage());
        String[] ret = req.returnValues().get(0).asStringArray();
        assertEquals(1, req.returnValues().size());
        assertEquals(0, ret.length);

        final RawConfig config = ProxyServerTest.fooConfig;
        server.proxyServer().memoryCache().update(config);
        req = new Request("listCachedConfig");
        client.invoke(req);
        assertFalse(req.isError(), req.errorMessage());
        assertEquals(1, req.returnValues().size());
        ret = req.returnValues().get(0).asStringArray();
        assertEquals(1, ret.length);
        assertEquals(config.getNamespace() + "." + config.getName() + "," + config.getConfigId() + "," +
                                 config.getGeneration() + "," + config.getPayloadChecksums(),
                                 ret[0]);
    }

    /**
     * Tests listCachedConfig RPC command
     */
    @Test
    void testRpcMethodListCachedConfigFull() {
        Request req = new Request("listCachedConfigFull");
        client.invoke(req);

        assertFalse(req.isError(), req.errorMessage());
        assertEquals(1, req.returnValues().size());
        String[] ret = req.returnValues().get(0).asStringArray();
        assertEquals(0, ret.length);

        final RawConfig config = ProxyServerTest.fooConfig;
        server.proxyServer().memoryCache().update(config);
        req = new Request("listCachedConfigFull");
        client.invoke(req);
        assertFalse(req.isError(), req.errorMessage());
        ret = req.returnValues().get(0).asStringArray();
        assertEquals(1, ret.length);
        assertEquals(config.getNamespace() + "." + config.getName() + "," + config.getConfigId() + "," +
                                 config.getGeneration() + "," + config.getPayloadChecksums() + "," + config.getPayload().getData(),
                                 ret[0]);
    }

    /**
     * Tests listSourceConnections RPC command
     */
    @Test
    void testRpcMethodListSourceConnections() throws ListenFailedException {
        reset();

        Request req = new Request("listSourceConnections");
        client.invoke(req);

        assertFalse(req.isError(), req.errorMessage());
        assertEquals(1, req.returnValues().size());
        final String[] ret = req.returnValues().get(0).asStringArray();
        assertEquals(2, ret.length);
        assertEquals("Current source: " + configSourceAddress, ret[0]);
        assertEquals("All sources:\n" + configSourceAddress + "\n", ret[1]);
    }

    /**
     * Tests invalidateCache RPC command
     */
    @Test
    void testRpcMethodInvalidateCache() {
        Request req = new Request("invalidateCache");
        client.invoke(req);

        assertFalse(req.isError(), req.errorMessage());
        assertEquals(1, req.returnValues().size());
        final String[] ret = req.returnValues().get(0).asStringArray();
        assertEquals(2, ret.length);
        assertEquals("0", ret[0]);
        assertEquals("success", ret[1]);
    }

    /**
     * Tests getMode and setMode RPC commands
     */
    @Test
    void testRpcMethodGetModeAndSetMode() {
        Request req = new Request("getMode");
        client.invoke(req);
        assertFalse(req.isError(), req.errorMessage());
        assertEquals(1, req.returnValues().size());
        assertEquals("default", req.returnValues().get(0).asString());

        req = new Request("setMode");
        String mode = "memorycache";
        req.parameters().add(new StringValue(mode));
        client.invoke(req);
        assertFalse(req.isError(), req.errorMessage());
        assertEquals(1, req.returnValues().size());
        String[] ret = req.returnValues().get(0).asStringArray();
        assertEquals(2, ret.length);
        assertEquals("0", ret[0]);
        assertEquals("success", ret[1]);
        assertEquals(mode, server.proxyServer().getMode().name());

        req = new Request("getMode");
        client.invoke(req);
        assertFalse(req.isError(), req.errorMessage());
        assertEquals(1, req.returnValues().size());
        assertEquals(mode, req.returnValues().get(0).asString());

        req = new Request("setMode");
        String oldMode = mode;
        mode = "invalid";
        req.parameters().add(new StringValue(mode));
        client.invoke(req);

        assertFalse(req.isError(), req.errorMessage());
        ret = req.returnValues().get(0).asStringArray();
        assertEquals(2, ret.length);
        assertEquals("1", ret[0]);
        assertEquals("Unrecognized mode '" + mode + "' supplied. Legal modes are '" + Mode.modes() + "'", ret[1]);
        assertEquals(oldMode, server.proxyServer().getMode().name());
    }

    /**
     * Tests updateSources RPC command
     */
    @Test
    void testRpcMethodUpdateSources() throws ListenFailedException {
        reset();

        Request req = new Request("updateSources");
        String spec1 = "tcp/a:19070";
        String spec2 = "tcp/b:19070";
        req.parameters().add(new StringValue(spec1 + "," + spec2));
        client.invoke(req);
        assertFalse(req.isError(), req.errorMessage());
        assertEquals(1, req.returnValues().size());
        assertEquals("Updated config sources to: " + spec1 + "," + spec2, req.returnValues().get(0).asString());


        server.proxyServer().setMode(Mode.ModeName.MEMORYCACHE.name());

        req = new Request("updateSources");
        req.parameters().add(new StringValue(spec1 + "," + spec2));
        client.invoke(req);
        assertFalse(req.isError(), req.errorMessage());
        assertEquals(1, req.returnValues().size());
        assertEquals("Cannot update sources when in '" + Mode.ModeName.MEMORYCACHE.name().toLowerCase() + "' mode", req.returnValues().get(0).asString());

        // TODO source connections needs to have deterministic order to work
        /*req = new Request("listSourceConnections");
        rpcServer.listSourceConnections(req);
        assertFalse(req.errorMessage(), req.isError());
        final String[] ret = req.returnValues().get(0).asStringArray();
        assertEquals(ret.length, is(2));
        assertEquals(ret[0], is("Current source: " + spec1));
        assertEquals(ret[1], is("All sources:\n" + spec2 + "\n" + spec1 + "\n"));
        */
    }

    /**
     * Tests dumpCache RPC command
     */
    @Test
    void testRpcMethodDumpCache() throws IOException {
        Request req = new Request("dumpCache");
        String path = temporaryFolder.getAbsolutePath();
        req.parameters().add(new StringValue(path));
        client.invoke(req);
        assertFalse(req.isError(), req.errorMessage());
        assertEquals(1, req.returnValues().size());
        assertEquals("success", req.returnValues().get(0).asString());
    }

    private static ProxyServer createTestServer(ConfigSourceSet source) {
        return new ProxyServer(null, source, new RpcConfigSourceClient(new ResponseHandler(), source));
    }

    private static class TestServer implements AutoCloseable {

        private static final Spec SPEC = new Spec(0);

        private final ProxyServer proxyServer = createTestServer(new ConfigSourceSet(configSourceAddress));
        private final Supervisor supervisor = new Supervisor(new Transport());
        private final ConfigProxyRpcServer rpcServer = new ConfigProxyRpcServer(proxyServer, supervisor, SPEC);
        private final Acceptor acceptor;

        TestServer() throws ListenFailedException {
            acceptor = supervisor.listen(SPEC);
        }

        ProxyServer proxyServer() {
            return proxyServer;
        }

        int listenPort() {
            return acceptor.port();
        }

        @Override
        public void close() {
            acceptor.shutdown().join();
            supervisor.transport().shutdown().join();
            rpcServer.shutdown();
        }

        private static File newFolder(File root, String... subDirs) throws IOException {
            String subFolder = String.join("/", subDirs);
            File result = new File(root, subFolder);
            if (!result.mkdirs()) {
                throw new IOException("Couldn't create folders " + root);
            }
            return result;
        }
    }

    private static class TestClient implements AutoCloseable {

        private final Supervisor supervisor;
        private final Target target;

        TestClient(int rpcPort) {
            this.supervisor = new Supervisor(new Transport());
            this.target = supervisor.connect(new Spec(rpcPort));
        }

        void invoke(Request request) {
            target.invokeSync(request, Duration.ofMinutes(10).getSeconds());
        }

        @Override
        public void close() {
            target.close();
            supervisor.transport().shutdown().join();
        }

        private static File newFolder(File root, String... subDirs) throws IOException {
            String subFolder = String.join("/", subDirs);
            File result = new File(root, subFolder);
            if (!result.mkdirs()) {
                throw new IOException("Couldn't create folders " + root);
            }
            return result;
        }
    }

    private static File newFolder(File root, String... subDirs) throws IOException {
        String subFolder = String.join("/", subDirs);
        File result = new File(root, subFolder);
        if (!result.mkdirs()) {
            throw new IOException("Couldn't create folders " + root);
        }
        return result;
    }
}