aboutsummaryrefslogtreecommitdiffstats
path: root/vdslib/src/test/java/com/yahoo/vdslib/distribution/DistributionTestCase.java
blob: b2afe2146fc2524ac0ed7612aef9ae43915654a2 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vdslib.distribution;

import com.yahoo.vespa.config.content.StorDistributionConfig;
import com.yahoo.vdslib.state.ClusterState;
import com.yahoo.document.BucketId;
import com.yahoo.vdslib.state.NodeType;
import org.junit.After;
import org.junit.Test;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.Stack;

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

public class DistributionTestCase {

    private static final int minUsedBits = 16;

    private DistributionTestFactory test;
    /** Build a set of buckets to test that should represent the entire bucket space well. */
    private static List<BucketId> getTestBuckets() {
        List<BucketId> buckets = new ArrayList<>();
        // Get a set of buckets from the same split level
        for (int i = 16; i <= 18; ++ i) {
            for (int j = 0; j < 20; ++ j) {
                buckets.add(new BucketId(i, j));
            }
        }
        // Get a few random buckets at every split level.
        Random randomized = new Random(413);
        long randValue = randomized.nextLong();
        for (int i = minUsedBits; i < 58; ++ i) {
            buckets.add(new BucketId(i, randValue));
        }
        randValue = randomized.nextLong();
        for (int i = minUsedBits; i < 58; ++ i) {
            buckets.add(new BucketId(i, randValue));
        }
        return Collections.unmodifiableList(buckets);
    }

    @After
    public void tearDown() throws Exception {
        if (test != null) {
            System.err.println("Verified " + test.getVerifiedTests() + " test results for test " + test.getName());
            test.recordTestResults();
        }
    }

    @Test
    public void testSimple() {
        test = new DistributionTestFactory("simple");
        List<BucketId> buckets = getTestBuckets();
        Integer [] nodes = { 6, 3, 4, 8, 8, 8, 8, 8, 8, 3 };
        for (int i=0; i<buckets.size(); ++i) {
            BucketId bucket = buckets.get(i);
            DistributionTestFactory.Test t = test.recordResult(bucket).assertNodeCount(1);
            if (i < nodes.length) t.assertNodeUsed(nodes[i]);
        }
    }

    @Test
    public void testDown() throws Exception {
        test = new DistributionTestFactory("down")
                .setUpStates("u")
                .setClusterState(new ClusterState(
                        "distributor:10 .4.s:m .5.s:m .6.s:d .7.s:d .8.s:r .9.s:r"));
        for (BucketId bucket : getTestBuckets()) {
            assertTrue(test.recordResult(bucket).assertNodeCount(1).getNodes().get(0) < 4);
        }
    }

    /**
     * The java side runs unit tests first. Thus java side will generate the distribution tests that the C++ side will verify equality with.
     * The tests serialized by the java side will be checked into version control, such that C++ side can test without java side. When one of the sides
     * change, the failing side can be identified by checking if the serialized files are modified from what is checked into version control.
     */
    private void writeDistributionTest(String name, String clusterState, String distributionConfig) throws IOException, ParseException, Distribution.TooFewBucketBitsInUseException, Distribution.NoDistributorsAvailableException {
        writeFileAtomically("src/tests/distribution/testdata/java_" + name + ".cfg", distributionConfig);
        writeFileAtomically("src/tests/distribution/testdata/java_" + name + ".state", clusterState);
        StringWriter sw = new StringWriter();
        Distribution distribution = new Distribution("raw:" + distributionConfig);
        ClusterState state = new ClusterState(clusterState);
        long maxBucket = 1;
        for (int distributionBits = 0; distributionBits <= 32; ++distributionBits) {
            state.setDistributionBits(distributionBits);
            RandomGen randomizer = new RandomGen(distributionBits);
            for (int bucketIndex = 0; bucketIndex < 64; ++bucketIndex) {
                if (bucketIndex >= maxBucket) break;
                long bucketId = bucketIndex;
                    // Use random bucket if we dont test all
                if (maxBucket > 64) {
                    bucketId = randomizer.nextLong();
                }
                BucketId bucket = new BucketId(distributionBits, bucketId);
                for (int redundancy = 1; redundancy <= distribution.getRedundancy(); ++redundancy) {
                    int distributorIndex = distribution.getIdealDistributorNode(state, bucket, "uim");
                    sw.write(distributionBits + " " + bucket.withoutCountBits() + " " + redundancy + " " + distributorIndex + "\n");
                }
            }
            maxBucket = maxBucket << 1;
        }

        writeFileAtomically("src/tests/distribution/testdata/java_" + name + ".distribution", sw.toString());
    }

    private void writeFileAtomically(String filename, String data) throws IOException {
        FileSystem fs = FileSystems.getDefault();
        Path filePath = fs.getPath(filename);
        Path tempFilePath = fs.getPath(filename + ".tmp");

        try (BufferedWriter bw = Files.newBufferedWriter(tempFilePath, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
            bw.write(data);
        }

        // Try to atomically move temporary file onto file. This is guaranteed to be atomic due to the files existing in the same file system
        Files.move(tempFilePath, filePath, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
    }

    @Test
    public void testWriteDistribution() throws IOException, ParseException, Distribution.TooFewBucketBitsInUseException, Distribution.NoDistributorsAvailableException {
        String clusterState = "distributor:9";
        String distributionConfig =
                """
                        redundancy 3
                        group[4]
                        group[0].index "invalid"
                        group[0].name "invalid"
                        group[0].partitions 1|2|*
                        group[0].nodes[0]
                        group[1].index 1
                        group[1].capacity 2.0
                        group[1].name group1
                        group[1].partitions *
                        group[1].nodes[3]
                        group[1].nodes[0].index 0
                        group[1].nodes[1].index 1
                        group[1].nodes[2].index 2
                        group[2].index 2
                        group[2].capacity 3.0
                        group[2].name group2
                        group[2].partitions *
                        group[2].nodes[3]
                        group[2].nodes[0].index 3
                        group[2].nodes[1].index 4
                        group[2].nodes[2].index 5
                        group[3].index 3
                        group[3].capacity 5.0
                        group[3].name group3
                        group[3].partitions *
                        group[3].nodes[3]
                        group[3].nodes[0].index 6
                        group[3].nodes[1].index 7
                        group[3].nodes[2].index 8
                        """;
        writeDistributionTest("depth2", clusterState, distributionConfig);

        clusterState = "distributor:20 storage:20";
        String complexDistributionConfig =
                """
                        redundancy 5
                        group[7]
                        group[0].partitions "*|*"
                        group[0].index "invalid"
                        group[0].name "invalid"
                        group[0].nodes[0]
                        group[1].partitions "1|*"
                        group[1].index "0"
                        group[1].name "switch0"
                        group[1].nodes[0]
                        group[2].partitions ""
                        group[2].index "0.0"
                        group[2].name "rack0"
                        group[2].nodes[4]
                        group[2].nodes[0].index 0
                        group[2].nodes[1].index 1
                        group[2].nodes[2].index 2
                        group[2].nodes[3].index 3
                        group[3].partitions ""
                        group[3].index "0.1"
                        group[3].name "rack1"
                        group[3].nodes[4]
                        group[3].nodes[0].index 8
                        group[3].nodes[1].index 9
                        group[3].nodes[2].index 14
                        group[3].nodes[3].index 15
                        group[4].partitions "*"
                        group[4].index "1"
                        group[4].name "switch1"
                        group[4].nodes[0]
                        group[5].partitions ""
                        group[5].index "1.0"
                        group[5].name "rack0"
                        group[5].nodes[4]
                        group[5].nodes[0].index 4
                        group[5].nodes[1].index 5
                        group[5].nodes[2].index 6
                        group[5].nodes[3].index 17
                        group[6].partitions ""
                        group[6].index "1.1"
                        group[6].name "rack1"
                        group[6].nodes[4]
                        group[6].nodes[0].index 10
                        group[6].nodes[1].index 12
                        group[6].nodes[2].index 13
                        group[6].nodes[3].index 7""";
        writeDistributionTest("depth3", clusterState, complexDistributionConfig);

        clusterState = "distributor:20 storage:20 .3.c:3 .7.c:2.5 .12.c:1.5";
        writeDistributionTest("capacity", clusterState, complexDistributionConfig);

        clusterState = "distributor:20 storage:20 .3.r:2 .7.r:3 .12.r:5";
        writeDistributionTest("retired", clusterState, complexDistributionConfig);
    }

    @Test
    public void testSplitBeyondSplitBitDoesntAffectDistribution() {
        Random randomized = new Random(7123161);
        long val = randomized.nextLong();
        test = new DistributionTestFactory("abovesplitbit");
        for (int i=16; i<=58; ++i) {
            test.recordResult(new BucketId(i, val)).assertNodeUsed(2);
        }
    }

    @Test
    public void testMinimalMovement() throws Exception {
        test = new DistributionTestFactory("minimal-movement")
                .setClusterState(new ClusterState("distributor:4 .2.s:d"));
        DistributionTestFactory control = new DistributionTestFactory("minimal-movement-reference")
                .setClusterState(new ClusterState("distributor:4"));
        int moved = 0;
        int staying = 0;
        for (BucketId bucket : getTestBuckets()) {
            DistributionTestFactory.Test org = control.recordResult(bucket).assertNodeCount(1);
            DistributionTestFactory.Test res = test.recordResult(bucket).assertNodeCount(1);
            if (org.getNodes().get(0) == 2) {
                assertTrue(res.getNodes().get(0) != 2);
                ++moved;
            } else {
                assertEquals(org, res);
                ++staying;
            }
        }
        assertEquals(63, moved);
        assertEquals(81, staying);
    }

    @Test
    public void testAllDistributionBits() throws Exception {
        for (int distbits=0; distbits<=32; ++distbits) {
            test = new DistributionTestFactory("distbit" + distbits)
                    .setClusterState(new ClusterState("bits:" + distbits + " distributor:10"));
            List<BucketId> buckets = new ArrayList<>();
            for (int i=0; i<100; ++i) {
                buckets.add(new BucketId(distbits, i));
            }
            for (BucketId bucket : buckets) {
                test.recordResult(bucket).assertNodeCount(1);
            }
            test.recordTestResults();
            test = null;
        }
    }

    private int getNodeCount(int depth, int branchCount, int nodesPerLeaf) {
        if (depth <= 1) return branchCount * nodesPerLeaf;
        int count = 0;
        for (int i=0; i<branchCount; ++i) {
            count += getNodeCount(depth - 1, branchCount, nodesPerLeaf);
        }
        return count;
    }
    private StorDistributionConfig.Builder buildHierarchicalConfig(
            int redundancy, int branchCount, int depth, String partitions, int nodesPerLeaf)
    {
        StorDistributionConfig.Builder builder = new StorDistributionConfig.Builder()
                .redundancy(redundancy);
        builder.group(new StorDistributionConfig.Group.Builder()
                .name("invalid").index("invalid").partitions(partitions));
        Stack<Integer> nodeIndexes = new Stack<>();
        for (int i=0, n=getNodeCount(depth, branchCount, nodesPerLeaf); i<n; ++i) nodeIndexes.push(i);
        Collections.shuffle(nodeIndexes, new Random(123));
        addLevel(builder, "top", "", branchCount, depth, partitions, nodesPerLeaf, nodeIndexes);
        return builder;
    }
    private void addLevel(StorDistributionConfig.Builder builder, String namePrefix, String indexPrefix,
                          int branchCount, int depth, String partitions, int nodesPerLeaf,
                          Stack<Integer> nodes)
    {
        for (int i=0; i<branchCount; ++i) {
            StorDistributionConfig.Group.Builder group
                    = new StorDistributionConfig.Group.Builder();
            String gname = namePrefix + "." + i;
            String index = (indexPrefix.isEmpty() ? "" + i : indexPrefix + "." + i);
            group.name(gname).index(index);
            if (depth <= 1) {
                for (int j=0; j<nodesPerLeaf; ++j) {
                    group.nodes(new StorDistributionConfig.Group.Nodes.Builder().index(nodes.pop()));
                }
            } else {
                group.partitions(partitions);
            }
            builder.group(group);
            if (depth > 1) {
                addLevel(builder, gname, index, branchCount, depth - 1, partitions, nodesPerLeaf, nodes);
            }
        }
    }

    @Test
    public void testHierarchicalDistribution() {
        test = new DistributionTestFactory("hierarchical-grouping")
                .setDistribution(buildHierarchicalConfig(6, 3, 1, "1|2|*", 3));
        for (BucketId bucket : getTestBuckets()) {
            test.recordResult(bucket).assertNodeCount(1);
        }
    }

    @Test
    public void testDistributorGroupTakeover() throws Exception {
        test = new DistributionTestFactory("hierarchical-grouping-distributor-takeover")
                .setDistribution(buildHierarchicalConfig(6, 3, 1, "1|2|*", 3).distributor_auto_ownership_transfer_on_whole_group_down(true))
                .setNodeType(NodeType.DISTRIBUTOR)
                .setClusterState(new ClusterState("distributor:2 storage:9"));
        for (BucketId bucket : getTestBuckets()) {
            test.recordResult(bucket).assertNodeCount(1);
        }
    }

    @Test
    public void testHierarchicalDistributionDeep() throws Exception {
        System.out.println(new StorDistributionConfig(buildHierarchicalConfig(8, 5, 3, "*|*", 3)));
        test = new DistributionTestFactory("hierarchical-grouping-deep")
                .setNodeCount(500)
                .setDistribution(buildHierarchicalConfig(8, 5, 3, "*|*", 3));
        for (BucketId bucket : getTestBuckets()) {
            test.recordResult(bucket).assertNodeCount(1);
        }
        Set<ConfiguredNode> nodes = test.getDistribution().getNodes();
        // Despite setNodeCount(500) above, the actual distribution config
        // itself only has 375 actual leaf nodes.
        assertEquals(375, nodes.size());
    }

    @Test
    public void testHierarchicalDistributionCapacity() throws Exception {
        StorDistributionConfig.Builder config = buildHierarchicalConfig(6, 3, 1, "1|*", 3);
        config.group.get(1).capacity(3);
        test = new DistributionTestFactory("group-capacity")
                .setNodeCount(getNodeCount(1, 3, 3)).setDistribution(config);

        int [] counts = new int[9];
        for (int i=0; i<900; ++i) {
            BucketId bucket = new BucketId(16, i);
            ++counts[ test.recordResult(bucket).assertNodeCount(1).getNodes().get(0) ];
        }
        int groupCount = 0;
        for (StorDistributionConfig.Group.Nodes.Builder n : config.group.get(1).nodes) {
            StorDistributionConfig.Group.Nodes node = new StorDistributionConfig.Group.Nodes(n);
            groupCount += counts[ node.index() ];
        }
        int avg3 = groupCount / 3;
        int avg1 = (900 - groupCount) / 6;
        double diff = 1.0 * avg3 / avg1;
        assertTrue(Arrays.toString(counts) + ": Too large diff" + diff, diff < 3.1);
        assertTrue(Arrays.toString(counts) + ": Too small diff" + diff, diff > 2.9);
    }

    @Test(expected = Distribution.NoDistributorsAvailableException.class)
    public void clusterDownInHierarchicSetupThrowsNoDistributorsAvailableException() throws Exception {
        ClusterState clusterState = new ClusterState("cluster:d");

        StorDistributionConfig.Builder config = buildHierarchicalConfig(4, 4, 1, "1|1|1|*", 1);
        Distribution distr = new Distribution(new StorDistributionConfig(config));
        distr.getIdealDistributorNode(clusterState, new BucketId(16, 0), "uim");
    }

}