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

import com.yahoo.vespa.config.content.StorDistributionConfig;
import com.yahoo.vespa.model.content.cluster.ContentCluster;
import com.yahoo.vespa.model.search.SearchNode;
import com.yahoo.yolean.Exceptions;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Optional;

import static com.yahoo.config.model.test.TestUtil.joinLines;
import static com.yahoo.vespa.model.content.utils.ContentClusterUtils.createCluster;
import static com.yahoo.vespa.model.content.utils.ContentClusterUtils.createClusterXml;
import static org.junit.jupiter.api.Assertions.*;

/**
 * Unit tests for hierarchic distribution in an indexed content cluster.
 *
 * @author geirst
 */
public class IndexedHierarchicDistributionTest {

    private ContentCluster getOneGroupCluster() throws Exception {
        String groupXml = joinLines("  <group>",
                          "    <node distribution-key='0' hostalias='mockhost'/>",
                          "    <node distribution-key='1' hostalias='mockhost'/>",
                          "    <node distribution-key='2' hostalias='mockhost'/>",
                          "  </group>", "");
        return createCluster(createClusterXml(groupXml, 2, 2));
    }

    private String getTwoGroupsXml(String partitions) {
        return joinLines("  <group>",
               "    <distribution partitions='" + partitions + "'/>",
               "    <group distribution-key='0' name='group0'>",
               "      <node distribution-key='0' hostalias='mockhost'/>",
               "      <node distribution-key='1' hostalias='mockhost'/>",
               "      <node distribution-key='2' hostalias='mockhost'/>",
               "    </group>",
               "    <group distribution-key='1' name='group1'>",
               "      <node distribution-key='3' hostalias='mockhost'/>",
               "      <node distribution-key='4' hostalias='mockhost'/>",
               "      <node distribution-key='5' hostalias='mockhost'/>",
               "    </group>",
               "  </group>", "");
    }

    private ContentCluster getTwoGroupsCluster() throws Exception {
        return createCluster(createClusterXml(getTwoGroupsXml("3|*"), 6, 6));
    }

    private ContentCluster getTwoGroupsCluster(int redundancy, int searchableCopies, String partitions) throws Exception {
        return createCluster(createClusterXml(getTwoGroupsXml(partitions), redundancy, searchableCopies));
    }

    private void assertSearchNode(int expRowId, int expPartitionId, int expDistibutionKey, SearchNode node) {
        assertEquals(expRowId, node.getNodeSpec().groupIndex());
        assertEquals(expPartitionId, node.getNodeSpec().partitionId());
        assertEquals(expDistibutionKey, ((ContentNode)node.getServiceLayerService()).getDistributionKey());
    }

    private StorDistributionConfig getStorDistributionConfig(ContentCluster c) {
        StorDistributionConfig.Builder b = new StorDistributionConfig.Builder();
        c.getConfig(b);
        return new StorDistributionConfig(b);
    }

    @Test
    void requireThatSearchNodesAreCorrectWithOneGroup() throws Exception {
        ContentCluster c = getOneGroupCluster();
        List<SearchNode> searchNodes = c.getSearch().getSearchNodes();

        assertEquals(3, searchNodes.size());
        assertSearchNode(0, 0, 0, searchNodes.get(0));
        assertSearchNode(0, 1, 1, searchNodes.get(1));
        assertSearchNode(0, 2, 2, searchNodes.get(2));
    }

    @Test
    void requireThatActivePerLeafGroupIsDefaultWithOneGroup() throws Exception {
        ContentCluster c = getOneGroupCluster();
        assertFalse(getStorDistributionConfig(c).active_per_leaf_group());
    }

    @Test
    void requireThatSearchNodesAreCorrectWithTwoGroups() throws Exception {
        ContentCluster c = getTwoGroupsCluster();
        List<SearchNode> searchNodes = c.getSearch().getSearchNodes();

        assertEquals(6, searchNodes.size());
        assertSearchNode(0, 0, 0, searchNodes.get(0));
        assertSearchNode(0, 1, 1, searchNodes.get(1));
        assertSearchNode(0, 2, 2, searchNodes.get(2));
        assertSearchNode(1, 0, 3, searchNodes.get(3));
        assertSearchNode(1, 1, 4, searchNodes.get(4));
        assertSearchNode(1, 2, 5, searchNodes.get(5));
    }

    @Test
    void requireThatActivePerLeafGroupIsSetWithTwoGroups() throws Exception {
        ContentCluster c = getTwoGroupsCluster();
        assertTrue(getStorDistributionConfig(c).active_per_leaf_group());
    }

    private ContentCluster getIllegalMultipleGroupsLevelCluster() throws Exception {
        String groupXml = joinLines("  <group>",
                "    <distribution partitions='2|*'/>",
                "    <group distribution-key='0' name='group0'>",
                "      <distribution partitions='1|*'/>",
                "      <group distribution-key='0' name='group00'>",
                "        <node distribution-key='0' hostalias='mockhost'/>",
                "      </group>",
                "      <group distribution-key='1' name='group01'>",
                "        <node distribution-key='1' hostalias='mockhost'/>",
                "      </group>",
                "    </group>",
                "  </group>", "");
        return createCluster(createClusterXml(groupXml, 2, 2));
    }

    private String getOddGroupsClusterXml() {
        return joinLines("  <group>",
                "    <distribution partitions='2|*'/>",
                "    <group distribution-key='0' name='group0'>",
                "      <node distribution-key='0' hostalias='mockhost'/>",
                "    </group>",
                "    <group distribution-key='1' name='group1'>",
                "      <node distribution-key='1' hostalias='mockhost'/>",
                "      <node distribution-key='2' hostalias='mockhost'/>",
                "    </group>",
                "  </group>", "");
    }
    private ContentCluster getIllegalGroupsCluster() throws Exception {
        return createCluster(createClusterXml(getOddGroupsClusterXml(), Optional.of(getRoundRobinDispatchXml()), 4, 4));
    }

    private String getRoundRobinDispatchXml() {
        return joinLines("<tuning>",
                "  <dispatch>",
                "    <dispatch-policy>round-robin</dispatch-policy>",
                "  </dispatch>",
                "</tuning>");
    }

    @Test
    void requireThatWeMustHaveOnlyOneGroupLevel() {
        try {
            getIllegalMultipleGroupsLevelCluster();
            fail("Did not get expected Exception");
        } catch (Exception e) {
            assertEquals("Expected all groups under root group 'null' to be leaf groups only containing nodes, but sub group 'group0' contains 2 sub groups",
                         Exceptions.toMessageString(e));
        }
    }

    @Test
    void requireThatLeafGroupsMustHaveEqualNumberOfNodes() {
        try {
            getIllegalGroupsCluster();
            fail("Did not get expected Exception");
        } catch (Exception e) {
            assertTrue(e.getMessage().contains("leaf group 'group0' contains 1 node(s) while leaf group 'group1' contains 2 node(s)"));
        }
    }

    @Test
    void requireThatLeafGroupsCountMustBeAFactorOfRedundancy() {
        try {
            getTwoGroupsCluster(3, 3, "2|*");
            fail("Did not get expected Exception");
        } catch (Exception e) {
            assertEquals("In content cluster 'mycluster': Expected number of leaf groups (2) to be a factor of redundancy (3), but it is not",
                         Exceptions.toMessageString(e));
        }
    }

    @Test
    void requireThatRedundancyPerGroupMustBeIsEqual() {
        try {
            getTwoGroupsCluster(4, 4, "1|*");
            fail("Did not get expected Exception");
        } catch (Exception e) {
            assertTrue(e.getMessage().contains("Expected distribution partitions should be '2|*'"));
        }
    }

    @Test
    void requireThatReadyCopiesMustBeEqualToRedundancy() {
        try {
            getTwoGroupsCluster(4, 3, "2|*");
            fail("Did not get expected Exception");
        } catch (Exception e) {
            assertEquals("In content cluster 'mycluster': Expected equal amount of ready copies per group, but 3 ready copies is specified with 2 groups", Exceptions.toMessageString(e));
        }
    }

    @Test
    void allowLessReadyCopiesThanRedundancy() throws Exception {
        getTwoGroupsCluster(4, 2, "2|*");
    }

}