summaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/dispatch/searchcluster/SearchClusterTest.java
blob: df1049f499d8248093069661012b146c28113f73 (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
package com.yahoo.search.dispatch.searchcluster;

import com.yahoo.container.QrSearchersConfig;
import com.yahoo.container.handler.ClustersStatus;
import com.yahoo.container.handler.VipStatus;
import com.yahoo.net.HostName;
import com.yahoo.prelude.Pong;
import com.yahoo.search.cluster.ClusterMonitor;
import com.yahoo.search.dispatch.MockSearchCluster;
import com.yahoo.search.result.ErrorMessage;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicInteger;

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

/**
 * @author baldersheim
 */
public class SearchClusterTest {

    static class State {
        final String clusterId;
        final int nodesPerGroup;
        final VipStatus vipStatus;
        final SearchCluster searchCluster;
        final List<AtomicInteger> numDocsPerNode;
        List<AtomicInteger> pingCounts;
        State(String clusterId, int nodesPergroup, String ... nodeNames) {
            this(clusterId, nodesPergroup, Arrays.asList(nodeNames));
        }
        State(String clusterId, int nodesPergroup, List<String> nodeNames) {
            this.clusterId = clusterId;
            this.nodesPerGroup = nodesPergroup;
            vipStatus = new VipStatus(new QrSearchersConfig.Builder().searchcluster(new QrSearchersConfig.Searchcluster.Builder().name(clusterId)).build(), new ClustersStatus());
            numDocsPerNode = new ArrayList<>(nodeNames.size());
            pingCounts = new ArrayList<>(nodeNames.size());
            List<Node> nodes = new ArrayList<>(nodeNames.size());

            for (String name : nodeNames) {
                int key = nodes.size() % nodesPergroup;
                int group = nodes.size() / nodesPergroup;
                nodes.add(new Node(key, name, 13333, group));
                numDocsPerNode.add(new AtomicInteger(1));
                pingCounts.add(new AtomicInteger(0));
            }
            searchCluster = new SearchCluster(clusterId, MockSearchCluster.createDispatchConfig(nodes), nodes.size() / nodesPergroup, vipStatus);
        }
        void startMonitoring() {
            searchCluster.startClusterMonitoring(new Factory(nodesPerGroup, numDocsPerNode, pingCounts));
        }
        private static int getMaxValue(List<AtomicInteger> list) {
            int max = list.get(0).get();
            for (AtomicInteger v : list) {
                if (v.get() > max) {
                    max = v.get();
                }
            }
            return max;
        }
        private static int getMinValue(List<AtomicInteger> list) {
            int min = list.get(0).get();
            for (AtomicInteger v : list) {
                if (v.get() < min) {
                    min = v.get();
                }
            }
            return min;
        }
        private static void waitAtLeast(int atLeast, List<AtomicInteger> list) {
            while (getMinValue(list) < atLeast) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {}
            }
        }
        void waitOneFullPingRound() {
            waitAtLeast(getMaxValue(pingCounts) + 1, pingCounts);
        }
        static class Factory implements PingFactory {
            static class Pinger implements Callable<Pong> {
                private final AtomicInteger numDocs;
                private final AtomicInteger pingCount;
                Pinger(AtomicInteger numDocs, AtomicInteger pingCount) {
                    this.numDocs = numDocs;
                    this.pingCount = pingCount;
                }
                @Override
                public Pong call() throws Exception {
                    int docs = numDocs.get();
                    pingCount.incrementAndGet();
                    return (docs < 0)
                            ? new Pong(ErrorMessage.createBackendCommunicationError("Negative numDocs = " + docs))
                            : new Pong(docs);
                }
            }

            private final List<AtomicInteger> activeDocs;
            private final List<AtomicInteger> pingCounts;
            private final int numPerGroup;
            Factory(int numPerGroup, List<AtomicInteger> activeDocs, List<AtomicInteger> pingCounts) {
                this.numPerGroup = numPerGroup;
                this.activeDocs = activeDocs;
                this.pingCounts = pingCounts;
            }

            @Override
            public Callable<Pong> createPinger(Node node, ClusterMonitor<Node> monitor) {
                int index = node.group*numPerGroup + node.key();
                return new Pinger(activeDocs.get(index), pingCounts.get(index));
            }
        }
    }

    @Test
    public void requireThatVipStatusIsDefaultDownButComesUpAfterPinging() {
        State test = new State("cluster.1", 2, "a", "b");
        assertTrue(test.searchCluster.localCorpusDispatchTarget().isEmpty());

        assertFalse(test.vipStatus.isInRotation());
        test.startMonitoring();
        test.waitOneFullPingRound();
        assertTrue(test.vipStatus.isInRotation());
    }

    @Test
    public void requireThatZeroDocsAreFine() {
        State test = new State("cluster.1", 2,"a", "b");
        test.startMonitoring();
        test.waitOneFullPingRound();

        assertTrue(test.vipStatus.isInRotation());
        assertTrue(test.searchCluster.localCorpusDispatchTarget().isEmpty());

        test.numDocsPerNode.get(0).set(-1);
        test.numDocsPerNode.get(1).set(-1);
        test.waitOneFullPingRound();
        assertFalse(test.vipStatus.isInRotation());
        test.numDocsPerNode.get(0).set(0);
        test.waitOneFullPingRound();
        assertTrue(test.vipStatus.isInRotation());
    }

    @Test
    public void requireThatVipStatusIsDefaultDownWithLocalDispatch() {
        State test = new State("cluster.1", 1, HostName.getLocalhost(), "b");
        assertTrue(test.searchCluster.localCorpusDispatchTarget().isPresent());

        assertFalse(test.vipStatus.isInRotation());
        test.startMonitoring();
        test.waitOneFullPingRound();
        assertTrue(test.vipStatus.isInRotation());
    }

    @Test
    public void requireThatVipStatusDownWhenLocalIsDown() {
        State test = new State("cluster.1",1,HostName.getLocalhost(), "b");

        test.startMonitoring();
        test.waitOneFullPingRound();
        assertTrue(test.vipStatus.isInRotation());
        assertTrue(test.searchCluster.localCorpusDispatchTarget().isPresent());

        test.waitOneFullPingRound();
        assertTrue(test.vipStatus.isInRotation());
        test.numDocsPerNode.get(0).set(-1);
        test.waitOneFullPingRound();
        assertFalse(test.vipStatus.isInRotation());

        test.numDocsPerNode.get(0).set(1);
        test.waitOneFullPingRound();
        assertTrue(test.vipStatus.isInRotation());

        test.numDocsPerNode.get(1).set(-1);
        test.waitOneFullPingRound();
        assertTrue(test.vipStatus.isInRotation());

        test.numDocsPerNode.get(0).set(-1);
        test.numDocsPerNode.get(1).set(-1);
        test.waitOneFullPingRound();
        assertFalse(test.vipStatus.isInRotation());
        test.numDocsPerNode.get(1).set(1);
        test.waitOneFullPingRound();
        assertFalse(test.vipStatus.isInRotation());
        test.numDocsPerNode.get(0).set(1);
        test.waitOneFullPingRound();
        assertTrue(test.vipStatus.isInRotation());
    }

    private void verifyThatVipStatusDownRequireAllNodesDown(int numGroups, int nodesPerGroup) {
        List<String> nodeNames = generateNodeNames(numGroups, nodesPerGroup);
        State test = new State("cluster.1", nodesPerGroup, nodeNames);
        test.startMonitoring();
        test.waitOneFullPingRound();
        assertTrue(test.vipStatus.isInRotation());
        assertTrue(test.searchCluster.localCorpusDispatchTarget().isEmpty());

        test.waitOneFullPingRound();
        assertTrue(test.vipStatus.isInRotation());

        for (int i=0; i < test.numDocsPerNode.size()-1; i++) {
            test.numDocsPerNode.get(i).set(-1);
        }
        test.waitOneFullPingRound();
        assertTrue(test.vipStatus.isInRotation());
        test.numDocsPerNode.get(test.numDocsPerNode.size()-1).set(-1);
        test.waitOneFullPingRound();
        assertFalse(test.vipStatus.isInRotation());
    }
    @Test
    public void requireThatVipStatusDownRequireAllNodesDown() {
        verifyThatVipStatusDownRequireAllNodesDown(1,2);
        verifyThatVipStatusDownRequireAllNodesDown(3, 3);
    }

    static private List<String> generateNodeNames(int numGroups, int nodesPerGroup) {
        List<String> nodeNames = new ArrayList<>(numGroups*nodesPerGroup);
        for (int g = 0; g < numGroups; g++) {
            for (int n=0; n < nodesPerGroup; n++) {
                nodeNames.add(new StringBuilder("node.").append(g).append('.').append(n).toString());
            }
        }
        return nodeNames;
    }

    private void verifyThatVipStatusUpRequireOnlyOneOnlineNode(int numGroups, int nodesPerGroup) {
        List<String> nodeNames = generateNodeNames(numGroups, nodesPerGroup);
        State test = new State("cluster.1", nodesPerGroup, nodeNames);
        test.startMonitoring();
        test.waitOneFullPingRound();
        assertTrue(test.vipStatus.isInRotation());
        assertTrue(test.searchCluster.localCorpusDispatchTarget().isEmpty());

        for (int i=0; i < test.numDocsPerNode.size()-1; i++) {
            test.numDocsPerNode.get(i).set(-1);
        }
        test.waitOneFullPingRound();
        assertTrue(test.vipStatus.isInRotation());
        test.numDocsPerNode.get(test.numDocsPerNode.size()-1).set(-1);
        test.waitOneFullPingRound();
        assertFalse(test.vipStatus.isInRotation());

        test.numDocsPerNode.get(0).set(0);
        test.waitOneFullPingRound();
        assertTrue(test.vipStatus.isInRotation());
    }
    @Test
    public void requireThatVipStatusUpRequireOnlyOneOnlineNode() {
        verifyThatVipStatusUpRequireOnlyOneOnlineNode(1, 2);
        verifyThatVipStatusUpRequireOnlyOneOnlineNode(3, 3);
    }

}