aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/cluster/test/ClusteredConnectionTestCase.java
blob: 8c98b344394ced321089bd749993cf8605fa60c4 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.cluster.test;

import com.yahoo.component.ComponentId;
import com.yahoo.concurrent.DaemonThreadFactory;
import com.yahoo.prelude.Ping;
import com.yahoo.prelude.Pong;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.cluster.ClusterSearcher;
import com.yahoo.search.cluster.Hasher;
import com.yahoo.search.result.ErrorMessage;
import com.yahoo.search.result.Hit;
import com.yahoo.search.searchchain.Execution;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

/**
 * @author bratseth
 */
public class ClusteredConnectionTestCase {

    @Test
    void testClustering() {
        Connection connection0 = new Connection("0");
        Connection connection1 = new Connection("1");
        Connection connection2 = new Connection("2");
        List<Connection> connections = new ArrayList<>();
        connections.add(connection0);
        connections.add(connection1);
        connections.add(connection2);
        MyBackend myBackend = new MyBackend(new ComponentId("test"), connections);

        Result r;
        r = new Execution(myBackend, Execution.Context.createContextStub()).search(new SimpleQuery(0));
        assertEquals("from:0", r.hits().get(0).getId().stringValue());
        r = new Execution(myBackend, Execution.Context.createContextStub()).search(new SimpleQuery(1));
        assertEquals("from:2", r.hits().get(0).getId().stringValue());
        r = new Execution(myBackend, Execution.Context.createContextStub()).search(new SimpleQuery(2));
        assertEquals("from:1", r.hits().get(0).getId().stringValue());
        r = new Execution(myBackend, Execution.Context.createContextStub()).search(new SimpleQuery(3));
        assertEquals("from:0", r.hits().get(0).getId().stringValue());

        connection2.setInService(false);
        r = new Execution(myBackend, Execution.Context.createContextStub()).search(new SimpleQuery(0));
        assertEquals("from:0", r.hits().get(0).getId().stringValue());
        r = new Execution(myBackend, Execution.Context.createContextStub()).search(new SimpleQuery(1));
        assertEquals("from:0", r.hits().get(0).getId().stringValue());
        r = new Execution(myBackend, Execution.Context.createContextStub()).search(new SimpleQuery(2));
        assertEquals("from:1", r.hits().get(0).getId().stringValue());
        r = new Execution(myBackend, Execution.Context.createContextStub()).search(new SimpleQuery(3));
        assertEquals("from:0", r.hits().get(0).getId().stringValue());

        connection1.setInService(false);
        r = new Execution(myBackend, Execution.Context.createContextStub()).search(new SimpleQuery(0));
        assertEquals("from:0", r.hits().get(0).getId().stringValue());
        r = new Execution(myBackend, Execution.Context.createContextStub()).search(new SimpleQuery(1));
        assertEquals("from:0", r.hits().get(0).getId().stringValue());
        r = new Execution(myBackend, Execution.Context.createContextStub()).search(new SimpleQuery(2));
        assertEquals("from:0", r.hits().get(0).getId().stringValue());
        r = new Execution(myBackend, Execution.Context.createContextStub()).search(new SimpleQuery(3));
        assertEquals("from:0", r.hits().get(0).getId().stringValue());

        connection0.setInService(false);
        r = new Execution(myBackend, Execution.Context.createContextStub()).search(new SimpleQuery(0));
        assertEquals("Failed calling connection '2' in searcher 'test' for query 'NULL': Connection failed",
                r.hits().getError().getDetailedMessage());

        connection0.setInService(true);
        r = new Execution(myBackend, Execution.Context.createContextStub()).search(new SimpleQuery(0));
        assertEquals("from:0", r.hits().get(0).getId().stringValue());
        r = new Execution(myBackend, Execution.Context.createContextStub()).search(new SimpleQuery(1));
        assertEquals("from:0", r.hits().get(0).getId().stringValue());
        r = new Execution(myBackend, Execution.Context.createContextStub()).search(new SimpleQuery(2));
        assertEquals("from:0", r.hits().get(0).getId().stringValue());
        r = new Execution(myBackend, Execution.Context.createContextStub()).search(new SimpleQuery(3));
        assertEquals("from:0", r.hits().get(0).getId().stringValue());

        connection1.setInService(true);
        connection2.setInService(true);
        r = new Execution(myBackend, Execution.Context.createContextStub()).search(new SimpleQuery(0));
        assertEquals("from:0", r.hits().get(0).getId().stringValue());
        r = new Execution(myBackend, Execution.Context.createContextStub()).search(new SimpleQuery(1));
        assertEquals("from:2", r.hits().get(0).getId().stringValue());
        r = new Execution(myBackend, Execution.Context.createContextStub()).search(new SimpleQuery(2));
        assertEquals("from:1", r.hits().get(0).getId().stringValue());
        r = new Execution(myBackend, Execution.Context.createContextStub()).search(new SimpleQuery(3));
        assertEquals("from:0", r.hits().get(0).getId().stringValue());
    }

    @Test
    void testClusteringWithPing() {
        Connection connection0 = new Connection("0");
        Connection connection1 = new Connection("1");
        Connection connection2 = new Connection("2");
        List<Connection> connections = new ArrayList<>();
        connections.add(connection0);
        connections.add(connection1);
        connections.add(connection2);
        MyBackend myBackend = new MyBackend(new ComponentId("test"), connections);

        Result r;

        // Note that we cannot make any successful queries here or we have to wait 10 seconds for
        // the traffic monitor to agree that these nodes are really not responding

        connection2.setInService(false);
        connection1.setInService(false);
        connection0.setInService(false);
        forcePing(myBackend);
        r = new Execution(myBackend, Execution.Context.createContextStub()).search(new SimpleQuery(0));
        assertEquals("No backends in service. Try later", r.hits().getError().getMessage());

        connection2.setInService(true);
        connection1.setInService(true);
        connection0.setInService(true);
        forcePing(myBackend);
        r = new Execution(myBackend, Execution.Context.createContextStub()).search(new SimpleQuery(0));
        assertNull(r.hits().getError());
    }

    private void forcePing(MyBackend myBackend) {
        myBackend.getMonitor().ping(Executors.newCachedThreadPool(new DaemonThreadFactory()));
        Thread.yield();
    }

    /** Represents a connection, e.g over http, in this test */
    private static class Connection {

        private final String id;

        private boolean inService = true;

        public Connection(String id) {
            this.id=id;
        }

        /** This is used for both fill, pings and queries */
        public String getResponse() {
            if ( ! inService) throw new RuntimeException("Connection failed");
            return id;
        }

        public void setInService(boolean inservice) {
            this.inService = inservice;
        }

        public String toString() {
            return "connection '" + id + "'";
        }

    }

    /**
     * This is the kind of searcher which will be implemented by those who wish to create a searcher which is a
     * client to a clustered service.
     * The goal is to make writing this correctly as simple as possible.
     */
    private static class MyBackend extends ClusterSearcher<Connection> {

        public MyBackend(ComponentId componentId, List<Connection> connections) {
            super(componentId,connections, new Hasher<>(), false, false);
        }

        @Override
        public Result search(Query query,Execution execution, Connection connection) {
            Result result = new Result(query);
            result.hits().add(new Hit("from:" + connection.getResponse()));
            return result;
        }

        @Override
        public void fill(Result result,String summary, Execution execution, Connection connection) {
            result.hits().get(0).fields().put("filled",connection.getResponse());
        }

        @Override
        public Pong ping(Ping ping,Connection connection) {
            if (connection.getResponse() != null)
                return new Pong();
            else
                return new Pong(ErrorMessage.createBackendCommunicationError("No ping response from '" + connection + "'"));
        }

    }

    /** A query with a predictable hash function */
    private static class SimpleQuery extends Query {

        int hashValue;

        public SimpleQuery(int hashValue) {
            this.hashValue = hashValue;
            this.setTimeout(50);
        }

        @Override
        public int hashCode() {
            return hashValue;
        }

        @Override
        public long getTimeout() {
            return 5000;
        }

    }

}