aboutsummaryrefslogtreecommitdiffstats
path: root/vdslib/src/test/java/com/yahoo/vdslib/distribution/GroupTestCase.java
blob: c3c5ddbe43192702d322a8b0a25e90114a6b2703 (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
// 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 org.junit.Test;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

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

public class GroupTestCase {

    private void assertDistribution(String spec, int redundancy, String expectedResult) throws ParseException {
        Group.Distribution distribution = new Group.Distribution(spec, redundancy);
        assertEquals(spec, distribution.toString());
        int[] resultArray = distribution.getRedundancyArray(redundancy);
        StringBuilder sb = new StringBuilder();
        for (int i=0; i<resultArray.length; ++i) {
            if (i != 0) sb.append(',');
            sb.append(resultArray[i]);
        }
        assertEquals("Spec: \"" + spec + "\", redundancy " + redundancy + " got unexpected result", expectedResult, sb.toString());
    }

    private void assertDistributionFailure(String spec, int redundancy, String expectedError) {
        try{
            new Group.Distribution(spec, redundancy);
            fail("Failed to fail parsing of spec \"" + spec + "\", redundancy " + redundancy + " with failure: " + expectedError);
        } catch (Exception e) {
            assertEquals(expectedError, e.getMessage());
        }
    }

    @Test
    public void testStarConversion() throws ParseException {
        assertDistribution("1|*|*", 5, "2,2,1");
        assertDistribution("1|*|*", 6, "3,2,1");
        assertDistribution("1|*|*", 3, "1,1,1");
        assertDistribution("1|*|*", 2, "1,1");
        assertDistribution("4|*", 3, "3");
        assertDistribution("2|*", 3, "2,1");
        assertDistribution("*|*", 3, "2,1");
        assertDistribution("*|*|*", 4, "2,1,1");
        assertDistribution("*|*|*", 5, "2,2,1");
        assertDistribution("*|*|*|*", 5, "2,1,1,1");

        assertDistributionFailure("2|*", 0, "The max redundancy (0) must be a positive number in the range 1-255.");
        assertDistributionFailure("*|2", 3, "Illegal distribution spec \"*|2\". Asterix specification must be tailing the specification.");
        assertDistributionFailure("*|2|*", 3, "Illegal distribution spec \"*|2|*\". Asterix specification must be tailing the specification.");
        assertDistributionFailure("0|*", 3, "Illegal distribution spec \"0|*\". Copy counts must be in the range 1-255.");
        assertDistributionFailure("1|0|*", 3, "Illegal distribution spec \"1|0|*\". Copy counts must be in the range 1-255.");
        assertDistributionFailure("1|a|*", 3, "Illegal distribution spec \"1|a|*\". Copy counts must be integer values in the range 1-255.");
        assertDistributionFailure("1|999|*", 3, "Illegal distribution spec \"1|999|*\". Copy counts must be in the range 1-255.");
    }

    private void setNodes(Group g, String nodes) {
        StringTokenizer st = new StringTokenizer(nodes, ",");
        List<ConfiguredNode> nodeList = new ArrayList<>();
        while (st.hasMoreTokens()) {
            nodeList.add(new ConfiguredNode(Integer.parseInt(st.nextToken()), false));
        }
        g.setNodes(nodeList);
    }

    private void verifyUniqueHashes(Group g, Set<Integer> hashes) {
        assertFalse(g.getName(), hashes.contains(g.getDistributionHash()));
        hashes.add(g.getDistributionHash());
    }

    private Group buildGroupTree() throws ParseException {
        Group root = new Group(5, "myroot", new Group.Distribution("2|*", 7));
        List<Group> level_one = new ArrayList<>();
        level_one.add(new Group(0, "br0", new Group.Distribution("1|1|*", 7)));
        level_one.add(new Group(1, "br1", new Group.Distribution("*", 7)));
        level_one.add(new Group(3, "br3", new Group.Distribution("8|*", 7)));
        level_one.add(new Group(4, "br4", new Group.Distribution("1|*", 7)));
        level_one.add(new Group(5, "br5", new Group.Distribution("*|*|*", 7)));
        level_one.add(new Group(6, "br6", new Group.Distribution("*|*|*|*|*|*", 7)));
        level_one.add(new Group(7, "br7", new Group.Distribution("*", 7)));
        level_one.add(new Group(9, "br9", new Group.Distribution("1|*", 7)));
        for(Group g : level_one) {
            root.addSubGroup(g);
            for (int i=0; i<5; ++i) {
                Group child =  new Group(i, g.getName() + "." + i);
                g.addSubGroup(child);
                List<ConfiguredNode> nodeList = new ArrayList<>();
                for (int j=0; j<5; ++j) nodeList.add(new ConfiguredNode(g.getIndex() * 10 + j, false));
                child.setNodes(nodeList);
            }
        }
            // Create some irregularities
        setNodes(level_one.get(3).getSubgroups().get(2), "19,7,8,17");
        try{
            Group br8 = new Group(5, "br8", new Group.Distribution("*", 5));
            root.addSubGroup(br8);
            fail(); // Should fail index 5 is in use at that level
        } catch (Exception e) {
            assertEquals("A subgroup with index 5 already exist.", e.getMessage());
        }
        try{
            Group br8 = new Group(5, "br8");
            Group br9 = new Group(2, "br9");
            br8.addSubGroup(br9);
            fail(); // Should fail as we want distribution to be set on non-leaf node
        } catch (Exception e) {
            assertEquals("Cannot add sub groups to a node without distribution set.", e.getMessage());
        }
        try{
            Group br8 = new Group(5, "br8", new Group.Distribution("*", 5));
            setNodes(br8, "1,2,3");
            fail(); // Should fail as we can't have distribution on leaf node.
        } catch (Exception e) {
            assertEquals("Cannot add nodes to non-leaf group with distribution set", e.getMessage());
        }
        root.calculateDistributionHashValues();
        Set<Integer> distributionHashes = new HashSet<>();
        verifyUniqueHashes(root, distributionHashes);
        return root;
    }

    @Test
    public void testNormalusage() throws ParseException {
        Group root = new Group(2, "myroot", new Group.Distribution("*", 2));
        assertFalse(root.isLeafGroup());

        Group branch = new Group(5, "myleaf");
        assertTrue(branch.isLeafGroup());

        root = buildGroupTree();

        String expected = "Group(name: myroot, index: 5, distribution: 2|*, subgroups: 8) {\n"
                        + "  Group(name: br0, index: 0, distribution: 1|1|*, subgroups: 5) {\n"
                        + "    Group(name: br0.0, index: 0, nodes( 0 1 2 3 4 )) {\n"
                        + "    }\n"
                        + "    Group(name: br0.1, index: 1, nodes( 0 1 2 3 4 )) {\n"
                        + "    }\n"
                        + "    Group(name: br0.2, index: 2, nodes( 0 1 2 3 4 )) {\n"
                        + "    }\n"
                        + "    Group(name: br0.3, index: 3, nodes( 0 1 2 3 4 )) {\n"
                        + "    }\n"
                        + "    Group(name: br0.4, index: 4, nodes( 0 1 2 3 4 )) {\n"
                        + "    }\n"
                        + "  }\n";
        assertEquals(expected, root.toString().substring(0, expected.length()));

        assertEquals("br5.br5.0", root.getGroupForNode(52).getPath());
    }

    private Group.Distribution dummyDistribution() throws Exception {
        return new Group.Distribution("*", 1);
    }

    @Test
    public void testRootGroupDoesNotIncludeGroupNameWhenNoChildren() {
        Group g = new Group(0, "donkeykong");
        assertEquals("/", g.getUnixStylePath());
    }

    @Test
    public void testChildNamesDoNotIncludeRootGroupName() throws Exception {
        Group g = new Group(0, "donkeykong", dummyDistribution());
        Group child = new Group(1, "mario");
        g.addSubGroup(child);
        assertEquals("/mario", child.getUnixStylePath());
    }

    @Test
    public void testNestedGroupsAreSlashSeparated() throws Exception {
        Group g = new Group(0, "donkeykong", dummyDistribution());
        Group mario = new Group(1, "mario", dummyDistribution());
        Group toad = new Group(2, "toad");
        mario.addSubGroup(toad);
        g.addSubGroup(mario);

        assertEquals("/mario/toad", toad.getUnixStylePath());
    }

    @Test
    public void testMultipleLeafGroupsAreEnumerated() throws Exception {
        Group g = new Group(0, "donkeykong", dummyDistribution());
        Group mario = new Group(1, "mario", dummyDistribution());
        Group toad = new Group(2, "toad");
        mario.addSubGroup(toad);
        Group yoshi = new Group(3, "yoshi");
        mario.addSubGroup(yoshi);
        g.addSubGroup(mario);
        Group luigi = new Group(4, "luigi");
        g.addSubGroup(luigi);

        assertEquals("/mario/toad", toad.getUnixStylePath());
        assertEquals("/mario/yoshi", yoshi.getUnixStylePath());
        assertEquals("/luigi", luigi.getUnixStylePath());
    }

}