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
|
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.dispatch;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.yahoo.search.dispatch.searchcluster.Group;
import com.yahoo.search.dispatch.searchcluster.Node;
import com.yahoo.search.dispatch.searchcluster.SearchCluster;
import com.yahoo.vespa.config.search.DispatchConfig;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
/**
* @author ollivir
*/
public class MockSearchCluster extends SearchCluster {
private final int numGroups;
private final int numNodesPerGroup;
private final ImmutableList<Group> orderedGroups;
private final ImmutableMap<Integer, Group> groups;
private final ImmutableMultimap<String, Node> nodesByHost;
public MockSearchCluster(String clusterId, int groups, int nodesPerGroup) {
this(clusterId, createDispatchConfig(), groups, nodesPerGroup);
}
public MockSearchCluster(String clusterId, DispatchConfig dispatchConfig, int groups, int nodesPerGroup) {
super(clusterId, dispatchConfig, 1, null);
ImmutableList.Builder<Group> orderedGroupBuilder = ImmutableList.builder();
ImmutableMap.Builder<Integer, Group> groupBuilder = ImmutableMap.builder();
ImmutableMultimap.Builder<String, Node> hostBuilder = ImmutableMultimap.builder();
int dk = 1;
for (int group = 0; group < groups; group++) {
List<Node> nodes = new ArrayList<>();
for (int node = 0; node < nodesPerGroup; node++) {
Node n = new Node(dk, "host" + dk, -1, group);
n.setWorking(true);
nodes.add(n);
hostBuilder.put(n.hostname(), n);
dk++;
}
Group g = new Group(group, nodes);
groupBuilder.put(group, g);
orderedGroupBuilder.add(g);
}
this.orderedGroups = orderedGroupBuilder.build();
this.groups = groupBuilder.build();
this.nodesByHost = hostBuilder.build();
this.numGroups = groups;
this.numNodesPerGroup = nodesPerGroup;
}
@Override
public ImmutableList<Group> orderedGroups() {
return orderedGroups;
}
@Override
public int size() {
return numGroups * numNodesPerGroup;
}
@Override
public ImmutableMap<Integer, Group> groups() {
return groups;
}
@Override
public int groupSize() {
return numNodesPerGroup;
}
@Override
public int groupsWithSufficientCoverage() {
return numGroups;
}
@Override
public Optional<Group> group(int n) {
if (n < numGroups) {
return Optional.of(groups.get(n));
} else {
return Optional.empty();
}
}
@Override
public Optional<Node> directDispatchTarget() {
return Optional.empty();
}
@Override
public void working(Node node) {
node.setWorking(true);
}
@Override
public void failed(Node node) {
node.setWorking(false);
}
public static DispatchConfig createDispatchConfig(Node... nodes) {
return createDispatchConfig(100.0, nodes);
}
public static DispatchConfig createDispatchConfig(double minSearchCoverage, Node... nodes) {
DispatchConfig.Builder builder = new DispatchConfig.Builder();
builder.minActivedocsPercentage(88.0);
builder.minGroupCoverage(99.0);
builder.maxNodesDownPerGroup(0);
builder.minSearchCoverage(minSearchCoverage);
if (minSearchCoverage < 100.0) {
builder.minWaitAfterCoverageFactor(0);
builder.maxWaitAfterCoverageFactor(0.5);
}
for (Node n : nodes) {
builder.node(new DispatchConfig.Node.Builder().key(n.key()).host(n.hostname()).port(n.fs4port()).group(n.group()));
}
return new DispatchConfig(builder);
}
}
|