aboutsummaryrefslogtreecommitdiffstats
path: root/vespaclient-java/src/test/java/com/yahoo/vespavisit/VdsVisitTestCase.java
blob: a0db3f973a47a2294ec0e498d24e7595012b0a11 (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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespavisit;

import com.yahoo.document.fieldset.DocIdOnly;
import com.yahoo.document.select.parser.ParseException;
import com.yahoo.documentapi.AckToken;
import com.yahoo.documentapi.ProgressToken;
import com.yahoo.documentapi.VisitorControlHandler;
import com.yahoo.documentapi.VisitorParameters;
import com.yahoo.documentapi.VisitorResponse;
import com.yahoo.documentapi.VisitorSession;
import com.yahoo.documentapi.messagebus.protocol.DocumentProtocol;
import com.yahoo.messagebus.StaticThrottlePolicy;
import com.yahoo.messagebus.Trace;
import com.yahoo.vespaclient.ClusterDef;
import com.yahoo.vespaclient.ClusterList;
import org.apache.commons.cli.Options;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

public class VdsVisitTestCase {

    private VdsVisit.ArgumentParser createMockArgumentParser() {
        Options opts = VdsVisit.createOptions();
        return new VdsVisit.ArgumentParser(opts);
    }

    @Test
    void testCommandLineShortOptions() throws Exception {
        // short options testing (for options that do not collide with each other)
        String[] args = new String[]{
                "-d", "foo.remote",
                "-s", "'id.user=1234'",
                "-f", "5678",
                "-t", "9012",
                "-l", "foodoc.bar,foodoc.baz",
                "-m", "6000",
                "-b", "5",
                "-p", "foo-progress.txt",
                "-u", "123456789",
                "-c", "kittens",
                "-r",
                "-v"
        };
        VdsVisit.ArgumentParser parser = createMockArgumentParser();
        VdsVisit.VdsVisitParameters allParams = parser.parse(args);
        assertNotNull(allParams);

        VisitorParameters params = allParams.getVisitorParameters();
        assertNotNull(params);
        assertEquals("foo.remote", params.getRemoteDataHandler());
        assertEquals("'id.user=1234'", params.getDocumentSelection());
        assertEquals(5678, params.getFromTimestamp());
        assertEquals(9012, params.getToTimestamp());
        assertEquals("foodoc.bar,foodoc.baz", params.getFieldSet());
        assertEquals(6000, params.getMaxPending());
        assertEquals(5, params.getMaxBucketsPerVisitor());
        assertEquals("foo-progress.txt", params.getResumeFileName());
        assertEquals(123456789, params.getTimeoutMs());
        assertEquals(7 * 24 * 60 * 60 * 1000, allParams.getFullTimeout());
        assertEquals("kittens", allParams.getCluster());
        assertTrue(allParams.isVerbose());
    }

    /**
     * Test the parameters that could not be used in conjunction with
     * those in the first parameter test.
     */
    @Test
    void testCommandLineShortOptions2() throws Exception {
        // Short options testing (for options that do not collide with each other)
        String[] args = new String[]{
                "-o", "654321",
                "-i"
        };
        VdsVisit.ArgumentParser parser = createMockArgumentParser();
        VdsVisit.VdsVisitParameters allParams = parser.parse(args);
        assertNotNull(allParams);

        VisitorParameters params = allParams.getVisitorParameters();
        assertNotNull(params);
        assertEquals(654321, allParams.getFullTimeout());
        assertEquals(654321, params.getTimeoutMs());
        assertEquals(DocIdOnly.NAME, params.getFieldSet());
    }

    @Test
    void testCommandLineShortOptionsPrintIdsOnly() throws Exception {
        // Short options testing (for options that do not collide with each other)
        String[] args = new String[]{
                "-i"
        };
        VdsVisit.ArgumentParser parser = createMockArgumentParser();
        VdsVisit.VdsVisitParameters allParams = parser.parse(args);
        assertNotNull(allParams);

        VisitorParameters params = allParams.getVisitorParameters();
        assertNotNull(params);
        assertEquals(DocIdOnly.NAME, params.getFieldSet());
        assertTrue(allParams.isPrintIdsOnly());
    }

    private static String joinLines(String... lines) {
        String nl = System.getProperty("line.separator"); // in case of \r\n instead of just \n...
        return String.join(nl, lines) + nl;
    }

    @Test
    void testCommandLineLongOptions() throws Exception {
        // short options testing (for options that do not collide with each other)
        String[] args = new String[]{
                "--datahandler", "foo.remote",
                "--selection", "'id.user=1234'",
                "--from", "5678",
                "--to", "9012",
                "--fieldset", "foodoc.bar,foodoc.baz",
                "--maxpending", "6000",
                "--maxbuckets", "5",
                "--progress", "foo-progress.txt",
                "--maxpendingsuperbuckets", "3",
                "--buckettimeout", "123456789",
                "--cluster", "kittens",
                "--visitinconsistentbuckets",
                "--visitlibrary", "fnord",
                "--libraryparam", "asdf", "rargh",
                "--libraryparam", "pinkie", "pie",
                "--processtime", "555",
                "--maxtotalhits", "2002",
                "--tracelevel", "8",
                "--priority", "NORMAL_1",
                "--skipbucketsonfatalerrors",
                "--abortonclusterdown",
                "--visitremoves",
                "--bucketspace", "outerspace",
                "--shorttensors",
                "--slices", "16",
                "--sliceid", "5"
        };
        VdsVisit.ArgumentParser parser = createMockArgumentParser();
        VdsVisit.VdsVisitParameters allParams = parser.parse(args);
        assertNotNull(allParams);

        VisitorParameters params = allParams.getVisitorParameters();
        assertNotNull(params);

        assertEquals("foo.remote", params.getRemoteDataHandler());
        assertEquals("'id.user=1234'", params.getDocumentSelection());
        assertEquals(5678, params.getFromTimestamp());
        assertEquals(9012, params.getToTimestamp());
        assertEquals("foodoc.bar,foodoc.baz", params.getFieldSet());
        assertEquals(6000, params.getMaxPending());
        assertEquals(5, params.getMaxBucketsPerVisitor());
        assertEquals("foo-progress.txt", params.getResumeFileName());
        assertEquals(123456789, params.getTimeoutMs());
        assertEquals(7 * 24 * 60 * 60 * 1000, allParams.getFullTimeout());
        assertEquals("kittens", allParams.getCluster());
        assertTrue(allParams.tensorShortForm());

        assertTrue(params.getThrottlePolicy() instanceof StaticThrottlePolicy);
        assertEquals(3, ((StaticThrottlePolicy) params.getThrottlePolicy()).getMaxPendingCount());

        assertTrue(params.visitInconsistentBuckets());
        assertEquals("fnord", params.getVisitorLibrary());
        // TODO: FIXME? multiple library params doesn't work
        assertArrayEquals("rargh".getBytes(), params.getLibraryParameters().get("asdf"));
        //assertTrue(Arrays.equals("pie".getBytes(), params.getLibraryParameters().get("pinkie")));
        assertEquals(555, allParams.getProcessTime());
        assertEquals(2002, params.getMaxTotalHits());
        assertEquals(8, params.getTraceLevel());
        assertEquals(DocumentProtocol.Priority.NORMAL_1, params.getPriority());
        assertTrue(allParams.getAbortOnClusterDown());
        assertTrue(params.visitRemoves());

        assertEquals(16, params.getSlices());
        assertEquals(5, params.getSliceId());

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PrintStream printStream = new PrintStream(outputStream);
        VdsVisit.verbosePrintParameters(allParams, printStream);
        printStream.flush();
        String nl = System.getProperty("line.separator"); // the joys of running tests on windows
        assertEquals(joinLines(
                "Time out visitor after 123456789 ms.",
                "Visiting documents matching: 'id.user=1234'",
                "Visiting bucket space: outerspace",
                "Visiting in the inclusive timestamp range 5678 - 9012.",
                "Visiting field set foodoc.bar,foodoc.baz.",
                "Visiting inconsistent buckets.",
                "Including remove entries.",
                "Tracking progress in file: foo-progress.txt",
                "Let visitor have maximum 6000 replies pending on data handlers per storage node visitor.",
                "Visit maximum 5 buckets per visitor.",
                "Sending data to data handler at: foo.remote",
                "Using visitor library 'fnord'.",
                "Adding the following library specific parameters:",
                "  asdf = rargh",
                "Visitor priority NORMAL_1",
                "Skip visiting super buckets with fatal errors.",
                "Visiting slice 5 out of 16 slices"),
                outputStream.toString(StandardCharsets.UTF_8));
    }

    private static String[] emptyArgList() { return new String[]{}; }

    @Test
    void slicing_is_disabled_by_default() throws Exception {
        var allParams = createMockArgumentParser().parse(emptyArgList());
        assertEquals(1, allParams.slices()); // 1 slice; the entire cluster
        assertEquals(0, allParams.sliceId());
    }

    // TODO Vespa 9: change default from long to short
    @Test
    void tensor_output_format_is_long_by_default() throws Exception {
        var allParams = createMockArgumentParser().parse(emptyArgList());
        assertFalse(allParams.tensorShortForm());
    }

    @Test
    void visitor_priority_is_low1_by_default() throws Exception {
        VdsVisit.VdsVisitParameters allParams = createMockArgumentParser().parse(emptyArgList());

        VisitorParameters params = allParams.getVisitorParameters();
        assertEquals(DocumentProtocol.Priority.LOW_1, params.getPriority());
    }

    @Test
    void testBadPriorityValue() throws Exception {
        String[] args = new String[]{
                "--priority", "super_hyper_important"
        };
        VdsVisit.ArgumentParser parser = createMockArgumentParser();
        try {
            parser.parse(args);
            fail("no exception thrown");
        } catch (IllegalArgumentException e) {
            assertTrue(e.getMessage().contains("Unknown priority name"));
        }
    }

    @Test
    void testCommandLineShortOptionsInvokeHelp() throws Exception {
        // Short options testing (for options that do not collide with each other)
        String[] args = new String[]{
                "-h"
        };
        VdsVisit.ArgumentParser parser = createMockArgumentParser();
        VdsVisit.VdsVisitParameters allParams = parser.parse(args);
        assertNull(allParams);
    }

    @Test
    void testAutoSelectClusterRoute() throws Exception {
        List<ClusterDef> clusterDefs = new ArrayList<>();
        clusterDefs.add(new ClusterDef("storage"));
        ClusterList clusterList = new ClusterList(clusterDefs);

        String route = VdsVisit.resolveClusterRoute(clusterList, null);
        assertEquals("[Content:cluster=storage]", route);
    }

    @Test
    void testBadClusterName() throws Exception {
        List<ClusterDef> clusterDefs = new ArrayList<>();
        clusterDefs.add(new ClusterDef("storage"));
        ClusterList clusterList = new ClusterList(clusterDefs);
        try {
            VdsVisit.resolveClusterRoute(clusterList, "borkbork");
        } catch (IllegalArgumentException e) {
            assertEquals("Your vespa cluster contains the content clusters 'storage', not 'borkbork'. " +
                    "Please select a valid vespa cluster.",
                    e.getMessage());
        }
    }

    @Test
    void testRequireClusterOptionIfMultipleClusters() {
        List<ClusterDef> clusterDefs = new ArrayList<>();
        clusterDefs.add(new ClusterDef("storage"));
        clusterDefs.add(new ClusterDef("storage2"));
        ClusterList clusterList = new ClusterList(clusterDefs);
        try {
            VdsVisit.resolveClusterRoute(clusterList, null);
        } catch (IllegalArgumentException e) {
            assertTrue(e.getMessage().contains("Please use the -c option to select one of them"));
        }
    }

    @Test
    void testExplicitClusterOptionWithMultipleClusters() {
        List<ClusterDef> clusterDefs = new ArrayList<>();
        clusterDefs.add(new ClusterDef("storage"));
        clusterDefs.add(new ClusterDef("storage2"));
        ClusterList clusterList = new ClusterList(clusterDefs);

        String route = VdsVisit.resolveClusterRoute(clusterList, "storage2");
        assertEquals("[Content:cluster=storage2]", route);
    }

    @Test
    void testFailIfNoContentClustersAvailable() {
        List<ClusterDef> clusterDefs = new ArrayList<>();
        ClusterList clusterList = new ClusterList(clusterDefs);
        try {
            VdsVisit.resolveClusterRoute(clusterList, null);
        } catch (IllegalArgumentException e) {
            assertTrue(e.getMessage().contains("Your Vespa cluster does not have any content clusters"));
        }
    }

    @Test
    void testStatistics() throws Exception {
        String[] args = new String[]{
                "--statistics", "foo"
        };
        VdsVisit.ArgumentParser parser = createMockArgumentParser();
        VdsVisit.VdsVisitParameters allParams = parser.parse(args);
        assertNotNull(allParams);

        VisitorParameters params = allParams.getVisitorParameters();
        assertNotNull(params);
        assertEquals("foo", allParams.getStatisticsParts());
        assertEquals(DocIdOnly.NAME, params.getFieldSet());
        assertEquals("CountVisitor", params.getVisitorLibrary());
    }

    // TODO: use DummyVisitorSession instead?
    private static class MockVisitorSession implements VisitorSession {
        private VisitorParameters params;

        public MockVisitorSession(VisitorParameters params) {
            this.params = params;
            params.getLocalDataHandler().setSession(this);
        }

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

        @Override
        public ProgressToken getProgress() {
            return null;
        }

        @Override
        public Trace getTrace() {
            return null;
        }

        @Override
        public boolean waitUntilDone(long l) throws InterruptedException {
            params.getControlHandler().onDone(VisitorControlHandler.CompletionCode.SUCCESS, "woo!");
            // Return immediately
            return true;
        }

        @Override
        public void ack(AckToken ackToken) {
        }

        @Override
        public void abort() {
        }

        @Override
        public VisitorResponse getNext() {
            return null;
        }

        @Override
        public VisitorResponse getNext(int i) throws InterruptedException {
            return null;
        }

        @Override
        public void destroy() {
        }
    }

    private static class MockVisitorSessionAccessor implements VdsVisit.VisitorSessionAccessor {
        boolean shutdown = false;
        @Override
        public VisitorSession createVisitorSession(VisitorParameters params) throws ParseException {
            return new MockVisitorSession(params);
        }

        @Override
        public void shutdown() {
            shutdown = true;
        }

        public boolean isShutdown() {
            return shutdown;
        }
    }

    private static class MockVisitorSessionAccessorFactory implements VdsVisit.VisitorSessionAccessorFactory {

        private MockVisitorSessionAccessor lastCreatedAccessor = null;

        @Override
        public VdsVisit.VisitorSessionAccessor createVisitorSessionAccessor() {
            lastCreatedAccessor = new MockVisitorSessionAccessor();
            return lastCreatedAccessor;
        }

        public MockVisitorSessionAccessor getLastCreatedAccessor() {
            return lastCreatedAccessor;
        }
    }

    private static class MockShutdownHookRegistrar implements VdsVisit.ShutdownHookRegistrar {
        Thread cleanUpThread;

        @Override
        public void registerShutdownHook(Thread thread) {
            cleanUpThread = thread;
        }

        public Thread getCleanUpThread() {
            return cleanUpThread;
        }
    }

    @Test
    void testVdsVisitRunLogic() {
        MockVisitorSessionAccessorFactory accessorFactory = new MockVisitorSessionAccessorFactory();
        MockShutdownHookRegistrar shutdownHookRegistrar = new MockShutdownHookRegistrar();
        VdsVisit vdsVisit = new VdsVisit(accessorFactory, shutdownHookRegistrar);

        VdsVisit.VdsVisitParameters params = new VdsVisit.VdsVisitParameters();
        VisitorParameters visitorParameters = new VisitorParameters("");
        params.setVisitorParameters(visitorParameters);

        visitorParameters.setResumeFileName("src/test/files/progress.txt");
        vdsVisit.setVdsVisitParameters(params);

        int code = vdsVisit.doRun();
        assertEquals(0, code);

        assertNotNull(shutdownHookRegistrar.getCleanUpThread());
        shutdownHookRegistrar.getCleanUpThread().run();

        assertNotNull(accessorFactory.getLastCreatedAccessor());
        assertTrue(accessorFactory.getLastCreatedAccessor().isShutdown());

        // Ensure progress token stuff was read from file
        ProgressToken progress = visitorParameters.getResumeToken();
        assertNotNull(progress);
        assertEquals(14, progress.getDistributionBitCount());
        assertEquals(3, progress.getPendingBucketCount());
    }

    @Test
    void testVdsVisitRunLogicProgressFileNotYetCreated() {
        MockVisitorSessionAccessorFactory accessorFactory = new MockVisitorSessionAccessorFactory();
        MockShutdownHookRegistrar shutdownHookRegistrar = new MockShutdownHookRegistrar();
        VdsVisit vdsVisit = new VdsVisit(accessorFactory, shutdownHookRegistrar);

        VdsVisit.VdsVisitParameters params = new VdsVisit.VdsVisitParameters();
        VisitorParameters visitorParameters = new VisitorParameters("");
        params.setVisitorParameters(visitorParameters);

        visitorParameters.setResumeFileName("src/test/files/progress-not-existing.txt");
        vdsVisit.setVdsVisitParameters(params);

        // Should not fail with file not found
        int code = vdsVisit.doRun();
        assertEquals(0, code);

        assertNotNull(shutdownHookRegistrar.getCleanUpThread());
        shutdownHookRegistrar.getCleanUpThread().run();

        assertNotNull(accessorFactory.getLastCreatedAccessor());
        assertTrue(accessorFactory.getLastCreatedAccessor().isShutdown());
    }
}