summaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/search/MultilevelDispatchValidator.java
blob: e07427277bb526ff7743c70eb2dab38ddb243cfd (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
// Copyright 2016 Yahoo Inc. 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.vespa.model.content.DispatchSpec;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Class used to validate that multilevel dispatch is correctly setup in an indexed content cluster.
 *
 * @author geirst
 */
public class MultilevelDispatchValidator {

    private final String clusterName;
    private final DispatchSpec dispatchSpec;
    private final List<SearchNode> searchNodes;

    public MultilevelDispatchValidator(String clusterName,
                                       DispatchSpec dispatchSpec,
                                       List<SearchNode> searchNodes) {
        this.clusterName = clusterName;
        this.dispatchSpec = dispatchSpec;
        this.searchNodes = searchNodes;
    }

    public void validate() {
        validateThatWeReferenceNodesOnlyOnce();
        validateThatWeReferenceAllNodes();
        validateThatWeUseValidNodeReferences();
    }

    private void validateThatWeReferenceNodesOnlyOnce() {
        Set<Integer> distKeys = new HashSet<>();
        for (DispatchSpec.Group group : dispatchSpec.getGroups()) {
            for (DispatchSpec.Node node : group.getNodes()) {
                int distKey = node.getDistributionKey();
                if (distKeys.contains(distKey)) {
                    throw new IllegalArgumentException(getErrorMsgPrefix() + "Expected nodes to be referenced only once in dispatch groups, but node with distribution key '" + distKey + "' is referenced multiple times.");
                }
                distKeys.add(distKey);
            }
        }
    }

    private void validateThatWeReferenceAllNodes() {
        Set<Integer> distKeys = createDistributionKeysSet();
        for (DispatchSpec.Group group : dispatchSpec.getGroups()) {
            for (DispatchSpec.Node node : group.getNodes()) {
                distKeys.remove(node.getDistributionKey());
            }
        }
        if (!distKeys.isEmpty()) {
            Object[] sorted = distKeys.toArray();
            Arrays.sort(sorted);
            throw new IllegalArgumentException(getErrorMsgPrefix() + "Expected all nodes to be referenced in dispatch groups, but " + distKeys.size() +
                    " node(s) with distribution keys " + Arrays.toString(sorted) + " are not referenced.");
        }
    }

    private void validateThatWeUseValidNodeReferences() {
        Set<Integer> distKeys = createDistributionKeysSet();
        for (DispatchSpec.Group group : dispatchSpec.getGroups()) {
            for (DispatchSpec.Node node : group.getNodes()) {
                int distKey = node.getDistributionKey();
                if (!distKeys.contains(distKey)) {
                    throw new IllegalArgumentException(getErrorMsgPrefix() + "Expected all node references in dispatch groups to reference existing nodes, " +
                            "but node with distribution key '" + distKey + "' does not exists.");
                }
            }
        }
    }

    private Set<Integer> createDistributionKeysSet() {
        Set<Integer> distKeys = new HashSet<>();
        for (SearchNode node : searchNodes) {
            distKeys.add(node.getDistributionKey());
        }
        return distKeys;
    }

    private String getErrorMsgPrefix() {
        return "In indexed content cluster '" + clusterName + "': ";
    }

}