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

import com.yahoo.config.model.producer.AbstractConfigProducer;
import com.yahoo.config.model.test.MockApplicationPackage;
import com.yahoo.config.model.test.MockRoot;
import com.yahoo.config.model.test.TestDriver;
import com.yahoo.config.model.test.TestRoot;
import com.yahoo.vespa.config.search.core.PartitionsConfig;
import com.yahoo.vespa.model.Host;
import com.yahoo.vespa.model.HostResource;
import com.yahoo.vespa.model.SimpleConfigProducer;
import com.yahoo.vespa.model.content.DispatchSpec;
import com.yahoo.vespa.model.content.cluster.ContentCluster;
import com.yahoo.vespa.model.content.utils.ContentClusterUtils;
import com.yahoo.vespa.model.search.utils.DispatchUtils;
import org.junit.Test;

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

import static com.yahoo.vespa.model.content.utils.ContentClusterUtils.createClusterXml;
import static com.yahoo.vespa.model.search.utils.DispatchUtils.getDataset;
import static com.yahoo.vespa.model.search.utils.DispatchUtils.getFdispatchrcConfig;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.containsString;

/**
 * Unit tests for multi-level dispatchers in an indexed content cluster.
 *
 * @author geirst
 */
public class MultilevelDispatchTest {

    private static class EngineAsserter {
        private List<PartitionsConfig.Dataset.Engine> engines;
        private int engineIdx = 0;
        public EngineAsserter(int numParts, int numEngines, Dispatch dispatch) {
           PartitionsConfig.Dataset dataset = getDataset(dispatch);
           assertEquals(numParts, dataset.numparts());
           assertEquals(PartitionsConfig.Dataset.Querydistribution.AUTOMATIC, dataset.querydistribution());
           engines = dataset.engine();
           assertEquals(numEngines, engines.size());
        }
        EngineAsserter assertEngine(int rowId, int partitionId, String connectSpec) {
            DispatchUtils.assertEngine(rowId, partitionId, connectSpec, engines.get(engineIdx++));
            return this;
        }
    }

    private String getGroupXml() {
         return "  <group>\n" +
                "    <node distribution-key='10' hostalias='mh0'/>\n" +
                "    <node distribution-key='11' hostalias='mh1'/>\n" +
                "    <node distribution-key='12' hostalias='mh2'/>\n" +
                "    <node distribution-key='13' hostalias='mh3'/>\n" +
                "    <node distribution-key='14' hostalias='mh4'/>\n" +
                "    <node distribution-key='15' hostalias='mh5'/>\n" +
                "  </group>\n";
    }

    private String getSimpleDispatchXml() {
        return  "  <dispatch>\n" +
                "    <num-dispatch-groups>2</num-dispatch-groups>\n" +
                "  </dispatch>\n";
    }

    private String getDispatchXml() {
        return "  <dispatch>\n" +
               "    <group>\n" +
               "      <node distribution-key='10'/>\n" +
               "      <node distribution-key='12'/>\n" +
               "      <node distribution-key='14'/>\n" +
               "    </group>\n" +
               "    <group>\n" +
               "      <node distribution-key='11'/>\n" +
               "      <node distribution-key='13'/>\n" +
               "      <node distribution-key='15'/>\n" +
               "    </group>\n" +
               "  </dispatch>\n";
    }

    private ContentCluster createCluster(String dispatchXml) throws Exception {
        String[] hosts = {"mh0", "mh1", "mh2", "mh3", "mh4", "mh5"};
        MockRoot root = ContentClusterUtils.createMockRoot(hosts);
        ContentCluster cluster = ContentClusterUtils.createCluster(createClusterXml(getGroupXml(), Optional.of(dispatchXml), 1, 1), root);

        AbstractConfigProducer<Dispatch> dispatchParent = new SimpleConfigProducer<>(root, "tlds");
        HostResource hostResource = new HostResource(new Host(root, "mockhost"));
        IndexedSearchCluster index = cluster.getSearch().getIndexed();
        index.addTld(root.deployLogger(), dispatchParent, hostResource);
        index.setupDispatchGroups(root.deployLogger());

        root.freezeModelTopology();
        cluster.validate();
        return cluster;
    }

    private List<Dispatch> getDispatchers(Dispatch tld) {
        DispatchGroup group = tld.getDispatchGroup();
        List<Dispatch> dispatchers = new ArrayList<>();
        for (SearchInterface dispatch : group.getSearchersIterable()) {
            dispatchers.add((Dispatch)dispatch);
        }
        return dispatchers;
    }

    private void assertDispatchAndSearchNodes(int partId, Dispatch[] dispatchers, String[] connectSpecs, SearchNode[] searchNodes) {
        assertEquals(dispatchers.length, connectSpecs.length);
        assertEquals(connectSpecs.length, searchNodes.length);
        int searchNodeIdx = 0;
        for (int rowId = 0; rowId < dispatchers.length; ++rowId) {
            assertDispatchAndSearchNodes(rowId, partId, searchNodes[searchNodeIdx++].getDistributionKey(),
                    dispatchers[rowId], connectSpecs, searchNodes);
        }
    }

    private void assertDispatchAndSearchNodes(int expRowId, int expPartId, int expDistributionKey, Dispatch dispatch, String[] connectSpecs, SearchNode[] searchNodes) {
        assertEquals(expRowId, dispatch.getNodeSpec().groupIndex());
        assertEquals(expPartId, dispatch.getNodeSpec().partitionId());
        assertEquals("mycluster/search/cluster.mycluster/dispatchers/dispatch." + expDistributionKey, dispatch.getConfigId());
        assertEquals(expPartId, getFdispatchrcConfig(dispatch).partition());
        assertEquals(1, getFdispatchrcConfig(dispatch).dispatchlevel());

        int numEngines = connectSpecs.length;
        EngineAsserter ea = new EngineAsserter(numEngines, numEngines, dispatch);
        for (int i = 0; i < numEngines; ++i) {
            ea.assertEngine(0, i, connectSpecs[i]);
            assertEquals(i, searchNodes[i].getNodeSpec().partitionId());
        }
    }

    @Test
    public void requireThatDispatchGroupsCanBeAutomaticallySetup() throws Exception {
        ContentCluster cr = createCluster(getSimpleDispatchXml());
        IndexedSearchCluster ix = cr.getSearch().getIndexed();
        Dispatch tld = cr.getSearch().getIndexed().getTLDs().get(0);

        assertEquals("tlds/tld.0", tld.getConfigId());
        assertEquals(0, getFdispatchrcConfig(tld).dispatchlevel());
        new EngineAsserter(2, 6, tld).
                assertEngine(0, 0, "tcp/mh0:19113").
                assertEngine(1, 0, "tcp/mh1:19113").
                assertEngine(2, 0, "tcp/mh2:19113").
                assertEngine(0, 1, "tcp/mh3:19113").
                assertEngine(1, 1, "tcp/mh4:19113").
                assertEngine(2, 1, "tcp/mh5:19113");

        List<Dispatch> ds = getDispatchers(tld);
        assertEquals(6, ds.size());
        { // dispatch group 1
            Dispatch[] dispatchers = {ds.get(0), ds.get(1), ds.get(2)};
            String[] specs = {"tcp/mh0:19104", "tcp/mh1:19104", "tcp/mh2:19104"};
            SearchNode[] searchNodes = {ix.getSearchNode(0), ix.getSearchNode(1), ix.getSearchNode(2)};
            assertDispatchAndSearchNodes(0, dispatchers, specs, searchNodes);
        }
        { // dispatch group 2
            Dispatch[] dispatchers = {ds.get(3), ds.get(4), ds.get(5)};
            String[] specs = {"tcp/mh3:19104", "tcp/mh4:19104", "tcp/mh5:19104"};
            SearchNode[] searchNodes = {ix.getSearchNode(3), ix.getSearchNode(4), ix.getSearchNode(5)};
            assertDispatchAndSearchNodes(1, dispatchers, specs, searchNodes);
        }
    }

    @Test
    public void requireThatMaxHitsIsScaled() throws Exception {
        ContentCluster cr = createCluster(getSimpleDispatchXml() + getMaxhitsTuning());
        Dispatch tld = cr.getSearch().getIndexed().getTLDs().get(0);
        PartitionsConfig.Builder builder = new PartitionsConfig.Builder();
        tld.getConfig(builder);
        PartitionsConfig config = new PartitionsConfig(builder);
        assertThat(config.dataset().size(), is(1));
        assertThat(config.dataset(0).maxhitspernode(), is(300));
        for (Dispatch dispatch : getDispatchers(tld)) {
            PartitionsConfig.Builder b = new PartitionsConfig.Builder();
            dispatch.getConfig(b);
            PartitionsConfig c= new PartitionsConfig(b);
            assertThat(c.dataset().size(), is(1));
            assertThat(c.dataset(0).maxhitspernode(), is(100));
        }
    }

    private String getMaxhitsTuning() {
        return "<tuning>" +
               "  <dispatch>" +
               "    <max-hits-per-partition>100</max-hits-per-partition>" +
               "  </dispatch>" +
                "</tuning>";
    }


    @Test
    public void requireThatSearchCoverageIsSetInMultilevelSetup() throws Exception {
        ContentCluster cr = createCluster(getSimpleDispatchXml() + getCoverage());
        Dispatch tld = cr.getSearch().getIndexed().getTLDs().get(0);
        PartitionsConfig.Builder builder = new PartitionsConfig.Builder();
        tld.getConfig(builder);
        PartitionsConfig config = new PartitionsConfig(builder);
        assertThat(config.dataset().size(), is(1));
        assertEquals(95.0, config.dataset(0).minimal_searchcoverage(), 0.1);
        for (Dispatch dispatch : getDispatchers(tld)) {
            PartitionsConfig.Builder b = new PartitionsConfig.Builder();
            dispatch.getConfig(b);
            PartitionsConfig c= new PartitionsConfig(b);
            assertThat(c.dataset().size(), is(1));
            assertEquals(95.0, c.dataset(0).minimal_searchcoverage(), 0.1);
        }
    }

    @Test
    public void requireThatSearchCoverageIsSetInSingleLevelSetup() {
        TestRoot root = new TestDriver(true).buildModel(new MockApplicationPackage.Builder()
                                                                .withServices("<services version='1.0'>" +
                                                                                      "<content id='stateful' version='1.0'>" +
                                                                                      "  <redundancy>1</redundancy>" +
                                                                                      "  <documents><document mode='index' type='music' /></documents>" +
                                                                                      "  <nodes>" +
                                                                                      "    <node distribution-key='1' hostalias='mockroot' />" +
                                                                                      "  </nodes>" +
                                                                                      "  <search><coverage><minimum>0.95</minimum></coverage></search>" +
                                                                                      "</content>" +
                                                                                      "<container id='foo' version='1.0'>" +
                                                                                      "  <search />" +
                                                                                      "  <nodes><node hostalias='mockroot' /></nodes>" +
                                                                                      "</container>" +
                                                                                      "</services>")
                                                                .withSearchDefinition(MockApplicationPackage.MUSIC_SEARCHDEFINITION)
                                                                .build());
        PartitionsConfig config = root.getConfig(PartitionsConfig.class, "stateful/search/cluster.stateful/tlds/foo.0.tld.0");
        assertThat(config.dataset().size(), is(1));
        assertEquals(95.0, config.dataset(0).minimal_searchcoverage(), 0.1);
    }

    private String getCoverage() {
        return "<search>" +
                "  <coverage>" +
                "    <minimum>0.95</minimum>" +
                "  </coverage>" +
                "</search>";
    }

    @Test
    public void requireThatDispatchGroupsCanBeExplicitlySpecified() throws Exception {
        ContentCluster cr = createCluster(getDispatchXml());
        IndexedSearchCluster ix = cr.getSearch().getIndexed();
        Dispatch tld = cr.getSearch().getIndexed().getTLDs().get(0);

        assertEquals("tlds/tld.0", tld.getConfigId());
        assertEquals(0, getFdispatchrcConfig(tld).dispatchlevel());
        new EngineAsserter(2, 6, tld).
                assertEngine(0, 0, "tcp/mh0:19113").
                assertEngine(1, 0, "tcp/mh2:19113").
                assertEngine(2, 0, "tcp/mh4:19113").
                assertEngine(0, 1, "tcp/mh1:19113").
                assertEngine(1, 1, "tcp/mh3:19113").
                assertEngine(2, 1, "tcp/mh5:19113");

        List<Dispatch> ds = getDispatchers(tld);
        assertEquals(6, ds.size());
        { // dispatch group 1
            Dispatch[] dispatchers = {ds.get(0), ds.get(1), ds.get(2)};
            String[] specs = {"tcp/mh0:19104", "tcp/mh2:19104", "tcp/mh4:19104"};
            SearchNode[] searchNodes = {ix.getSearchNode(0), ix.getSearchNode(2), ix.getSearchNode(4)};
            assertDispatchAndSearchNodes(0, dispatchers, specs, searchNodes);
        }
        { // dispatch group 2
            Dispatch[] dispatchers = {ds.get(3), ds.get(4), ds.get(5)};
            String[] specs = {"tcp/mh1:19104", "tcp/mh3:19104", "tcp/mh5:19104"};
            SearchNode[] searchNodes = {ix.getSearchNode(1), ix.getSearchNode(3), ix.getSearchNode(5)};
            assertDispatchAndSearchNodes(1, dispatchers, specs, searchNodes);
        }
    }

    @Test
    public void requireThatUnevenDispatchGroupsCanBeCreated() {
        List<SearchNode> searchNodes = createSearchNodes(5);
        List<DispatchSpec.Group> groups = DispatchGroupBuilder.createDispatchGroups(searchNodes, 3);
        assertEquals(3, groups.size());
        assertGroup(new int[]{0, 1}, groups.get(0));
        assertGroup(new int[]{2, 3}, groups.get(1));
        assertGroup(new int[]{4}, groups.get(2));
    }

    private List<SearchNode> createSearchNodes(int numNodes) {
        List<SearchNode> searchNodes = new ArrayList<>();
        MockRoot root = new MockRoot("");
        for (int i = 0; i < numNodes; ++i) {
            searchNodes.add(SearchNode.create(root, "mynode" + i, i, new NodeSpec(0, i), "mycluster", null, false,
                    Optional.empty(), Optional.empty(), root.getDeployState().isHosted()));
        }
        return searchNodes;
    }

    private void assertGroup(int[] nodes, DispatchSpec.Group group) {
        assertEquals(nodes.length, group.getNodes().size());
        for (int i = 0; i < nodes.length; ++i) {
            assertEquals(nodes[i], group.getNodes().get(i).getDistributionKey());
        }
    }

    private ContentCluster createIllegalSetupWithMultipleNodeReferences() throws Exception {
        String dispatchXml = "  <dispatch>\n" +
               "    <group>\n" +
               "      <node distribution-key='10'/>\n" +
               "      <node distribution-key='11'/>\n" +
               "      <node distribution-key='12'/>\n" +
               "    </group>\n" +
               "    <group>\n" +
               "      <node distribution-key='12'/>\n" +
               "      <node distribution-key='13'/>\n" +
               "      <node distribution-key='14'/>\n" +
               "    </group>\n" +
               "  </dispatch>\n";
        return createCluster(dispatchXml);
    }

    private ContentCluster createIllegalSetupWithMissingNodeReferences() throws Exception {
        String dispatchXml = "  <dispatch>\n" +
               "    <group>\n" +
               "      <node distribution-key='10'/>\n" +
               "      <node distribution-key='11'/>\n" +
               "    </group>\n" +
               "    <group>\n" +
               "      <node distribution-key='13'/>\n" +
               "      <node distribution-key='14'/>\n" +
               "    </group>\n" +
               "  </dispatch>\n";
        return createCluster(dispatchXml);
    }

    private ContentCluster createIllegalSetupWithIllegalNodeReference() throws Exception {
        String dispatchXml = "  <dispatch>\n" +
               "    <group>\n" +
               "      <node distribution-key='10'/>\n" +
               "      <node distribution-key='11'/>\n" +
               "      <node distribution-key='12'/>\n" +
               "    </group>\n" +
               "    <group>\n" +
               "      <node distribution-key='13'/>\n" +
               "      <node distribution-key='14'/>\n" +
               "      <node distribution-key='15'/>\n" +
               "      <node distribution-key='19'/>\n" +
               "    </group>\n" +
               "  </dispatch>\n";
        return createCluster(dispatchXml);
    }

    @Test
    public void requireThatWeReferenceNodesOnlyOnceWhenSettingUpDispatchGroups() {
        try {
            createIllegalSetupWithMultipleNodeReferences();
            assertFalse("Did not get expected Exception", true);
        } catch (Exception e) {
            assertThat(e.getMessage(), containsString("node with distribution key '12' is referenced multiple times"));
        }
    }

    @Test
    public void requireThatWeReferenceAllNodesWhenSettingUpDispatchGroups() {
        try {
            createIllegalSetupWithMissingNodeReferences();
            assertFalse("Did not get expected Exception", true);
        } catch (Exception e) {
            assertThat(e.getMessage(), containsString("2 node(s) with distribution keys [12, 15] are not referenced"));
        }
    }

    @Test
    public void requireThatWeReferenceValidNodesWhenSettingUpDispatchGroups() {
        try {
            createIllegalSetupWithIllegalNodeReference();
            assertFalse("Did not get expected Exception", true);
        } catch (Exception e) {
            assertThat(e.getMessage(), containsString("node with distribution key '19' does not exists"));
        }
    }

}