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
|
// 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.google.common.collect.ImmutableMap;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.dispatch.CloseableInvoker;
import com.yahoo.search.dispatch.FillInvoker;
import com.yahoo.search.dispatch.InterleavedFillInvoker;
import com.yahoo.search.dispatch.InterleavedSearchInvoker;
import com.yahoo.search.dispatch.SearchCluster;
import com.yahoo.search.dispatch.SearchInvoker;
import com.yahoo.search.result.Hit;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
/**
* FS4InvokerFactory constructs {@link FillInvoker} and {@link SearchInvoker} objects that communicate with
* content nodes or dispatchers over the fnet/FS4 protocol
*
* @author ollivir
*/
public class FS4InvokerFactory {
private final FS4ResourcePool fs4ResourcePool;
private final VespaBackEndSearcher searcher;
private final ImmutableMap<Integer, SearchCluster.Node> nodesByKey;
public FS4InvokerFactory(FS4ResourcePool fs4ResourcePool, SearchCluster searchCluster, VespaBackEndSearcher searcher) {
this.fs4ResourcePool = fs4ResourcePool;
this.searcher = searcher;
ImmutableMap.Builder<Integer, SearchCluster.Node> builder = ImmutableMap.builder();
searchCluster.groups().values().forEach(group -> group.nodes().forEach(node -> builder.put(node.key(), node)));
this.nodesByKey = builder.build();
}
public SearchInvoker getSearchInvoker(Query query, SearchCluster.Node node) {
return new FS4SearchInvoker(searcher, query, fs4ResourcePool, node.hostname(), node.fs4port(), node.key());
}
public Optional<SearchInvoker> getSearchInvoker(Query query, SearchCluster.Group group) {
return getInvoker(group.nodes(), node -> getSearchInvoker(query, node), InterleavedSearchInvoker::new);
}
public FillInvoker getFillInvoker(Query query, SearchCluster.Node node) {
return new FS4FillInvoker(searcher, query, fs4ResourcePool, node.hostname(), node.fs4port(), node.key());
}
public Optional<FillInvoker> getFillInvoker(Result result) {
Collection<Integer> requiredNodes = requiredFillNodes(result);
List<SearchCluster.Node> nodes = new ArrayList<>(requiredNodes.size());
for (Integer distKey : requiredNodes) {
SearchCluster.Node node = nodesByKey.get(distKey);
if (node == null) {
return Optional.empty();
}
nodes.add(node);
}
Query query = result.getQuery();
return getInvoker(nodes, node -> getFillInvoker(query, node), InterleavedFillInvoker::new);
}
private static Collection<Integer> requiredFillNodes(Result result) {
Set<Integer> requiredNodes = new HashSet<>();
for (Iterator<Hit> i = result.hits().unorderedDeepIterator(); i.hasNext();) {
Hit h = i.next();
if (h instanceof FastHit) {
FastHit hit = (FastHit) h;
requiredNodes.add(hit.getDistributionKey());
}
}
return requiredNodes;
}
@FunctionalInterface
private interface InvokerConstructor<INVOKER> {
INVOKER construct(SearchCluster.Node node);
}
@FunctionalInterface
private interface ClusterInvokerConstructor<CLUSTERINVOKER extends INVOKER, INVOKER> {
CLUSTERINVOKER construct(Map<Integer, INVOKER> subinvokers);
}
private <INVOKER extends CloseableInvoker, CLUSTERINVOKER extends INVOKER> Optional<INVOKER> getInvoker(
Collection<SearchCluster.Node> nodes, InvokerConstructor<INVOKER> singleNodeCtor,
ClusterInvokerConstructor<CLUSTERINVOKER, INVOKER> clusterCtor) {
if (nodes.size() == 1) {
SearchCluster.Node node = nodes.iterator().next();
return Optional.of(singleNodeCtor.construct(node));
} else {
Map<Integer, INVOKER> nodeInvokers = new HashMap<>();
for (SearchCluster.Node node : nodes) {
if (node.isWorking()) {
nodeInvokers.put(node.key(), singleNodeCtor.construct(node));
}
}
if (nodeInvokers.size() == 1) {
return Optional.of(nodeInvokers.values().iterator().next());
} else {
return Optional.of(clusterCtor.construct(nodeInvokers));
}
}
}
}
|