aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/fastsearch/FastSearcher.java
blob: 11b71c2c1592306e54182616a03a0248e7f2f6c2 (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
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.fastsearch;

import com.yahoo.fs4.BasicPacket;
import com.yahoo.fs4.ChannelTimeoutException;
import com.yahoo.fs4.PingPacket;
import com.yahoo.fs4.PongPacket;
import com.yahoo.fs4.QueryPacket;
import com.yahoo.fs4.mplex.Backend;
import com.yahoo.fs4.mplex.FS4Channel;
import com.yahoo.fs4.mplex.InvalidChannelException;
import com.yahoo.prelude.Ping;
import com.yahoo.prelude.Pong;
import com.yahoo.prelude.querytransform.QueryRewrite;
import com.yahoo.processing.request.CompoundName;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.dispatch.Dispatcher;
import com.yahoo.search.dispatch.FillInvoker;
import com.yahoo.search.dispatch.SearchInvoker;
import com.yahoo.search.dispatch.searchcluster.Node;
import com.yahoo.search.grouping.GroupingRequest;
import com.yahoo.search.grouping.request.GroupingOperation;
import com.yahoo.search.query.Ranking;
import com.yahoo.search.result.Coverage;
import com.yahoo.search.result.ErrorMessage;
import com.yahoo.search.searchchain.Execution;
import edu.umd.cs.findbugs.annotations.NonNull;

import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.logging.Level;

import static com.yahoo.container.util.Util.quote;

/**
 * The searcher which forwards queries to fdispatch nodes, using the fnet/fs4
 * network layer.
 *
 * @author  bratseth
 */
// TODO: Clean up all the duplication in the various search methods by
// switching to doing all the error handling using exceptions below doSearch2.
// Right now half is done by exceptions handled in doSearch2 and half by setting
// errors on results and returning them. It could be handy to create a QueryHandlingErrorException
// or similar which could wrap an error message, and then just always throw that and
// catch and unwrap into a results with an error in high level methods.  -Jon
public class FastSearcher extends VespaBackEndSearcher {

    /** If this is turned on this will make search queries directly to the local search node when possible */
    private final static CompoundName dispatchDirect = new CompoundName("dispatch.direct");

    /** Used to dispatch directly to search nodes over RPC, replacing the old fnet communication path */
    private final Dispatcher dispatcher;

    private final Backend dispatchBackend;

    private final FS4InvokerFactory fs4InvokerFactory;

    /**
     * Creates a Fastsearcher.
     *
     * @param dispatchBackend       The backend object containing the connection to the dispatch node this should talk to
     *                      over the fs4 protocol
     * @param fs4ResourcePool the resource pool used to create direct connections to the local search nodes when
     *                        bypassing the dispatch node
     * @param dispatcher the dispatcher used (when enabled) to send summary requests over the rpc protocol.
     *                   Eventually we will move everything to this protocol and never use dispatch nodes.
     *                   At that point we won't need a cluster searcher above this to select and pass the right
     *                   backend.
     * @param docSumParams  document summary parameters
     * @param clusterParams the cluster number, and other cluster backend parameters
     * @param cacheParams   the size, lifetime, and controller of our cache
     * @param documentdbInfoConfig document database parameters
     */
    public FastSearcher(Backend dispatchBackend, FS4ResourcePool fs4ResourcePool,
                        Dispatcher dispatcher, SummaryParameters docSumParams, ClusterParams clusterParams,
                        CacheParams cacheParams, DocumentdbInfoConfig documentdbInfoConfig) {
        init(docSumParams, clusterParams, cacheParams, documentdbInfoConfig);
        this.dispatchBackend = dispatchBackend;
        this.dispatcher = dispatcher;
        this.fs4InvokerFactory = new FS4InvokerFactory(fs4ResourcePool, dispatcher.searchCluster(), this);
    }

    /**
     * Pings the backend. Does not propagate to other searchers.
     */
    @Override
    public Pong ping(Ping ping, Execution execution) {
        return ping(ping, dispatchBackend, getName());
    }

    public static Pong ping(Ping ping, Backend backend, String name) {
        FS4Channel channel = backend.openPingChannel();

        // If you want to change this code, you need to understand
        // com.yahoo.prelude.cluster.ClusterSearcher.ping(Searcher) and
        // com.yahoo.prelude.cluster.TrafficNodeMonitor.failed(ErrorMessage)
        try {
            PingPacket pingPacket = new PingPacket();
            try {
                boolean couldSend = channel.sendPacket(pingPacket);
                if ( ! couldSend) {
                    return new Pong(ErrorMessage.createBackendCommunicationError("Could not ping " + name));
                }
            } catch (InvalidChannelException e) {
                return new Pong(ErrorMessage.createBackendCommunicationError("Invalid channel " + name));
            } catch (IllegalStateException e) {
                return new Pong(ErrorMessage.createBackendCommunicationError("Illegal state in FS4: " + e.getMessage()));
            } catch (IOException e) {
                return new Pong(ErrorMessage.createBackendCommunicationError("IO error while sending ping: " + e.getMessage()));
            }

            // We should only get a single packet
            BasicPacket[] packets;

            try {
                packets = channel.receivePackets(ping.getTimeout(), 1);
            } catch (ChannelTimeoutException e) {
                return new Pong(ErrorMessage.createNoAnswerWhenPingingNode("timeout while waiting for fdispatch for " + name));
            } catch (InvalidChannelException e) {
                return new Pong(ErrorMessage.createBackendCommunicationError("Invalid channel for " + name));
            }

            if (packets.length == 0) {
                return new Pong(ErrorMessage.createBackendCommunicationError(name + " got no packets back"));
            }

            try {
                packets[0].ensureInstanceOf(PongPacket.class, name);
            } catch (TimeoutException e) {
                return new Pong(ErrorMessage.createTimeout(e.getMessage()));
            } catch (IOException e) {
                return new Pong(ErrorMessage.createBackendCommunicationError("Unexpected packet class returned after ping: " + e.getMessage()));
            }
            return new Pong((PongPacket)packets[0]);
        } finally {
            if (channel != null) {
                channel.close();
            }
        }
    }

    @Override
    protected void transformQuery(Query query) {
        QueryRewrite.rewriteSddocname(query);
    }

    @Override
    public Result doSearch2(Query query, QueryPacket queryPacket, CacheKey cacheKey, Execution execution) {
        if (dispatcher.searchCluster().groupSize() == 1)
            forceSinglePassGrouping(query);
        try(SearchInvoker invoker = getSearchInvoker(query)) {
            List<Result> results = invoker.search(query, queryPacket, cacheKey);
            Result result = mergeResults(results, query, execution);

            if (query.properties().getBoolean(Ranking.RANKFEATURES, false)) {
                // There is currently no correct choice for which
                // summary class we want to fetch at this point. If we
                // fetch the one selected by the user it may not
                // contain the data we need. If we fetch the default
                // one we end up fetching docsums twice unless the
                // user also requested the default one.
                fill(result, query.getPresentation().getSummary(), execution); // ARGH
            }
            return result;
        } catch (TimeoutException e) {
            return new Result(query,ErrorMessage.createTimeout(e.getMessage()));
        } catch (IOException e) {
            Result result = new Result(query);
            if (query.getTraceLevel() >= 1)
                query.trace(getName() + " error response: " + result, false, 1);
            result.hits().addError(ErrorMessage.createBackendCommunicationError(getName() + " failed: "+ e.getMessage()));
            return result;
        }
    }

    /**
     * Perform a partial docsum fill for a temporary result
     * representing a partition of the complete fill request.
     *
     * @param result result containing a partition of the unfilled hits
     * @param summaryClass the summary class we want to fill with
     **/
    @Override
    protected void doPartialFill(Result result, String summaryClass) {
        if (result.isFilled(summaryClass)) return;

        Query query = result.getQuery();
        traceQuery(getName(), "fill", query, query.getOffset(), query.getHits(), 1, quotedSummaryClass(summaryClass));

        try (FillInvoker invoker = getFillInvoker(result)) {
            invoker.fill(result, summaryClass);
        }
    }

    /** When we only search a single node, doing all grouping in one pass is more efficient */
    private void forceSinglePassGrouping(Query query) {
        for (GroupingRequest groupingRequest : query.getSelect().getGrouping())
            forceSinglePassGrouping(groupingRequest.getRootOperation());
    }

    private void forceSinglePassGrouping(GroupingOperation operation) {
        operation.setForceSinglePass(true);
        for (GroupingOperation childOperation : operation.getChildren())
            forceSinglePassGrouping(childOperation);
    }

    /**
     * Returns an invocation object for use in a single search request. The specific implementation returned
     * depends on query properties with the default being an invoker that interfaces with a dispatcher
     * on the same host.
     */
    private SearchInvoker getSearchInvoker(Query query) {
        Optional<SearchInvoker> invoker = dispatcher.getSearchInvoker(query, fs4InvokerFactory);
        if (invoker.isPresent()) {
            return invoker.get();
        }

        Optional<Node> direct = getDirectNode(query);
        if(direct.isPresent()) {
            return fs4InvokerFactory.getSearchInvoker(query, direct.get());
        }
        return new FS4SearchInvoker(this, query, dispatchBackend);
    }

    /**
     * Returns an invocation object for use in a single fill request. The specific implementation returned
     * depends on query properties with the default being an invoker that uses RPC to interface with
     * content nodes.
     */
    private FillInvoker getFillInvoker(Result result) {
        Query query = result.getQuery();
        Optional<FillInvoker> invoker = dispatcher.getFillInvoker(result, this, getDocumentDatabase(query), fs4InvokerFactory);
        if (invoker.isPresent()) {
            return invoker.get();
        }

        Optional<Node> direct = getDirectNode(query);
        if (direct.isPresent()) {
            return fs4InvokerFactory.getFillInvoker(query, direct.get());
        }
        return new FS4FillInvoker(this, query, dispatchBackend);
    }

    /**
     * If the query can be directed to a single local content node, returns that node. Otherwise,
     * returns an empty value.
     */
    private Optional<Node> getDirectNode(Query query) {
        if (!query.properties().getBoolean(dispatchDirect, true))
            return Optional.empty();
        if (query.properties().getBoolean(com.yahoo.search.query.Model.ESTIMATE))
            return Optional.empty();

        Optional<Node> directDispatchRecipient = dispatcher.searchCluster().directDispatchTarget();
        if (!directDispatchRecipient.isPresent())
            return Optional.empty();

        // Dispatch directly to the single, local search node
        Node local = directDispatchRecipient.get();
        query.trace(false, 2, "Dispatching directly to ", local);
        return Optional.of(local);
    }

    private Result mergeResults(List<Result> results, Query query, Execution execution) {
        if (results.size() == 1) {
            return results.get(0);
        }

        Result result = new Result(query);
        // keep a separate tally of coverage as the normal merge counts using
        // federated query rules
        Coverage finalCoverage = new Coverage(0, 0);

        for (Result partialResult : results) {
            finalCoverage.mergeWithPartition(partialResult.getCoverage(true));
            result.mergeWith(partialResult);
            result.hits().addAll(partialResult.hits().asUnorderedHits());
        }
        result.setCoverage(finalCoverage);

        if (query.getOffset() != 0 || result.hits().size() > query.getHits()) {
            // with multiple results, each partial result is expected to have
            // offset = 0 to allow correct offset positioning after merge

            if (result.getHitOrderer() != null) {
                // Make sure we have the necessary data for sorting
                fill(result, Execution.ATTRIBUTEPREFETCH, execution);
            }
            result.hits().trim(query.getOffset(), query.getHits());
        }

        return result;
    }

    private static @NonNull Optional<String> quotedSummaryClass(String summaryClass) {
        return Optional.of(summaryClass == null ? "[null]" : quote(summaryClass));
    }

    public String toString() {
        return "fast searcher (" + getName() + ") " + dispatchBackend;
    }

    protected boolean isLoggingFine() {
        return getLogger().isLoggable(Level.FINE);
    }

}