This repository has been archived by the owner on Oct 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 173
/
CoreSenseiServiceImpl.java
369 lines (318 loc) · 13.1 KB
/
CoreSenseiServiceImpl.java
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/**
* This software is licensed to you under the Apache License, Version 2.0 (the
* "Apache License").
*
* LinkedIn's contributions are made under the Apache License. If you contribute
* to the Software, the contributions will be deemed to have been made under the
* Apache License, unless you expressly indicate otherwise. Please do not make any
* contributions that would be inconsistent with the Apache License.
*
* You may obtain a copy of the Apache License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, this software
* distributed under the Apache License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the Apache
* License for the specific language governing permissions and limitations for the
* software governed under the Apache License.
*
* © 2012 LinkedIn Corp. All Rights Reserved.
*/
package com.senseidb.svc.impl;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicInteger;
import com.browseengine.bobo.api.FacetAccessible;
import com.browseengine.bobo.facets.FacetHandler;
import com.sensei.search.req.protobuf.SenseiReqProtoSerializer;
import com.senseidb.search.req.*;
import org.apache.log4j.Logger;
import org.apache.lucene.search.Collector;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Weight;
import proj.zoie.api.ZoieIndexReader;
import proj.zoie.api.ZoieIndexReader.SubReaderAccessor;
import proj.zoie.api.ZoieIndexReader.SubReaderInfo;
import com.browseengine.bobo.api.BoboBrowser;
import com.browseengine.bobo.api.BoboIndexReader;
import com.browseengine.bobo.api.BrowseException;
import com.browseengine.bobo.api.BrowseHit;
import com.browseengine.bobo.api.BrowseRequest;
import com.browseengine.bobo.api.BrowseResult;
import com.browseengine.bobo.api.MultiBoboBrowser;
import com.browseengine.bobo.sort.SortCollector;
import com.linkedin.norbert.network.JavaSerializer;
import com.linkedin.norbert.network.Serializer;
import com.senseidb.indexing.SenseiIndexPruner;
import com.senseidb.indexing.SenseiIndexPruner.IndexReaderSelector;
import com.senseidb.search.node.ResultMerger;
import com.senseidb.search.node.SenseiCore;
import com.senseidb.search.node.SenseiQueryBuilderFactory;
import com.senseidb.search.req.SenseiHit;
import com.senseidb.search.req.SenseiRequest;
import com.senseidb.search.req.SenseiResult;
import com.senseidb.search.req.mapred.impl.SenseiMapFunctionWrapper;
import com.senseidb.util.RequestConverter;
import com.yammer.metrics.core.Timer;
import static com.senseidb.servlet.SenseiSearchServletParams.PARAM_RESULT_HIT_UID;
public class CoreSenseiServiceImpl extends AbstractSenseiCoreService<SenseiRequest, SenseiResult> {
public static final Serializer<SenseiRequest, SenseiResult> JAVA_SERIALIZER =
JavaSerializer.apply("SenseiRequest", SenseiRequest.class, SenseiResult.class);
public static final Serializer<SenseiRequest, SenseiResult> PROTO_SERIALIZER =
new SenseiReqProtoSerializer();
public static final Serializer<SenseiRequest, SenseiResult> PROTO_V2_SERIALIZER =
new SenseiSnappyProtoSerializer();
private static final Logger logger = Logger.getLogger(CoreSenseiServiceImpl.class);
private static final String TOP_DOCS_METHOD = "topDocs";
private static final String GET_HITS_FEATURES_METHOD = "getHitsFeatures";
private final Timer _timerMetric;
public CoreSenseiServiceImpl(SenseiCore core) {
super(core);
_timerMetric = registerTimer("prune");
}
@Override
protected String getMetricScope()
{
return "node";
}
private SenseiResult browse(SenseiRequest senseiRequest, MultiBoboBrowser browser, BrowseRequest req, SubReaderAccessor<BoboIndexReader> subReaderAccessor) throws BrowseException, IOException, Exception {
final SenseiResult result = new SenseiResult();
long start = System.currentTimeMillis();
int offset = req.getOffset();
int count = req.getCount();
if (offset < 0 || count < 0)
{
throw new IllegalArgumentException("both offset and count must be > 0: " + offset + "/" + count);
}
final Collector collector = senseiRequest.buildCollector(req.getQuery());
BrowseResult res = null;
BrowseHit[] hits = null;
ScoreDoc[] scoreDocs = null;
float [][] features = null;
if (collector == null) {
res = browser.browse(req);
hits = res.getHits();
}
else {
Map<String, FacetAccessible> facetCollectors = new HashMap<String, FacetAccessible>();
Weight w = req.getQuery().createWeight(browser);
browser.browse(req, w, collector, facetCollectors, offset);
try {
/**
* A custom collector must implement topDocs() method and a getHitsFeatures() method.
*/
scoreDocs = (ScoreDoc[]) collector.getClass().getMethod(TOP_DOCS_METHOD).invoke(collector);
features = (float[][]) collector.getClass().getMethod(GET_HITS_FEATURES_METHOD).invoke(collector);
hits = new BrowseHit[scoreDocs.length];
Map<String, FacetHandler<?>> facetHandlerMap = browser.getFacetHandlerMap();
int i = 0;
for (ScoreDoc doc : scoreDocs) {
BrowseHit hit = new BrowseHit();
hit.setScore(doc.score);
hit.setDocid(doc.doc);
Map<String,String[]> map = new HashMap<String,String[]>();
Map<String,Object[]> rawMap = new HashMap<String,Object[]>();
for (Map.Entry<String, FacetHandler<?>> entry : facetHandlerMap.entrySet()) {
map.put(entry.getKey(), browser.getFieldVal(doc.doc, entry.getKey()));
rawMap.put(entry.getKey(), browser.getRawFieldVal(doc.doc, entry.getKey()));
}
hit.setFieldValues(map);
hit.setRawFieldValues(rawMap);
hits[i++] = hit;
}
res = new BrowseResult();
res.setHits(hits);
res.setNumHits(hits.length);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
if (req.getMapReduceWrapper() != null) {
result.setMapReduceResult(req.getMapReduceWrapper().getResult());
}
SenseiHit[] senseiHits = new SenseiHit[hits.length];
Set<String> selectSet = senseiRequest.getSelectSet();
for (int i = 0; i < hits.length; i++)
{
BrowseHit hit = hits[i];
SenseiHit senseiHit = new SenseiHit();
int docid = hit.getDocid();
SubReaderInfo<BoboIndexReader> readerInfo = subReaderAccessor.getSubReaderInfo(docid);
Long uid = (Long)hit.getRawField(PARAM_RESULT_HIT_UID);
if (uid == null)
uid = ((ZoieIndexReader<BoboIndexReader>) readerInfo.subreader.getInnerReader()).getUID(readerInfo.subdocid);
senseiHit.setUID(uid);
senseiHit.setDocid(docid);
senseiHit.setScore(hit.getScore());
senseiHit.setComparable(hit.getComparable());
if (selectSet != null && selectSet.size() != 0)
{
// Clear the data those are not used:
if (hit.getFieldValues() != null)
{
Iterator<String> iter = hit.getFieldValues().keySet().iterator();
while (iter.hasNext())
{
if (!selectSet.contains(iter.next()))
{
iter.remove();
}
}
}
if (hit.getRawFieldValues() != null)
{
Iterator<String> iter = hit.getRawFieldValues().keySet().iterator();
while (iter.hasNext())
{
if (!selectSet.contains(iter.next()))
{
iter.remove();
}
}
}
}
if (features != null) {
senseiHit.setFeatures(features[i]);
}
senseiHit.setFieldValues(hit.getFieldValues());
senseiHit.setRawFieldValues(hit.getRawFieldValues());
senseiHit.setStoredFields(hit.getStoredFields());
senseiHit.setExplanation(hit.getExplanation());
senseiHit.setGroupField(hit.getGroupField());
senseiHit.setGroupValue(hit.getGroupValue());
senseiHit.setRawGroupValue(hit.getRawGroupValue());
senseiHit.setGroupHitsCount(hit.getGroupHitsCount());
senseiHit.setTermFreqMap(hit.getTermFreqMap());
senseiHits[i] = senseiHit;
}
result.setHits(senseiHits);
result.setNumHits(res.getNumHits());
result.setNumGroups(res.getNumGroups());
result.setGroupAccessibles(res.getGroupAccessibles());
result.setSortCollector(res.getSortCollector());
result.setTotalDocs(browser.numDocs());
result.addAll(res.getFacetMap());
// Defer the closing of facetAccessibles till result merging time.
// Collection<FacetAccessible> facetAccessibles = facetMap.values();
// for (FacetAccessible facetAccessible : facetAccessibles){
// facetAccessible.close();
// }
long end = System.currentTimeMillis();
result.setTime(end - start);
// set the transaction ID to trace transactions
result.setTid(req.getTid());
Query parsedQ = req.getQuery();
if (parsedQ != null)
{
result.setParsedQuery(parsedQ.toString());
} else
{
result.setParsedQuery("*:*");
}
return result;
}
@Override
public SenseiResult handlePartitionedRequest(final SenseiRequest request,
List<BoboIndexReader> readerList,SenseiQueryBuilderFactory queryBuilderFactory) throws Exception {
MultiBoboBrowser browser = null;
try
{
final List<BoboIndexReader> segmentReaders = BoboBrowser.gatherSubReaders(readerList);
if (segmentReaders!=null && segmentReaders.size() > 0) {
final AtomicInteger skipDocs = new AtomicInteger(0);
final SenseiIndexPruner pruner = _core.getIndexPruner();
List<BoboIndexReader> validatedSegmentReaders = _timerMetric.time(new Callable<List<BoboIndexReader>>(){
@Override
public List<BoboIndexReader> call() throws Exception {
IndexReaderSelector readerSelector = pruner.getReaderSelector(request);
List<BoboIndexReader> validatedReaders = new ArrayList<BoboIndexReader>(segmentReaders.size());
for (BoboIndexReader segmentReader : segmentReaders){
if (readerSelector.isSelected(segmentReader)){
validatedReaders.add(segmentReader);
}
else{
skipDocs.addAndGet(segmentReader.numDocs());
}
}
return validatedReaders;
}
});
pruner.sort(validatedSegmentReaders);
browser = new MultiBoboBrowser(BoboBrowser.createBrowsables(validatedSegmentReaders));
request.setSearchable(browser);
request.setQueryBuilderFactory(queryBuilderFactory);
BrowseRequest breq = RequestConverter.convert(request, queryBuilderFactory);
if (request.getMapReduceFunction() != null) {
SenseiMapFunctionWrapper mapWrapper = new SenseiMapFunctionWrapper(request.getMapReduceFunction(), _core.getSystemInfo().getFacetInfos());
breq.setMapReduceWrapper(mapWrapper);
}
SubReaderAccessor<BoboIndexReader> subReaderAccessor =
ZoieIndexReader.getSubReaderAccessor(validatedSegmentReaders);
SenseiResult res = browse(request, browser, breq, subReaderAccessor);
int totalDocs = res.getTotalDocs()+skipDocs.get();
res.setTotalDocs(totalDocs);
// For debugging serialization issues:
// byte[] responseBytes = getSerializer().responseToBytes(res);
// SenseiResult response2 = getSerializer().responseFromBytes(responseBytes);
// if(!res.equals(response2)) {
// throw new IllegalArgumentException("Cant serialize response");
// }
return res;
}
else{
return new SenseiResult();
}
} catch (Exception e)
{
logger.error(e.getMessage(), e);
throw e;
} finally
{
if (browser != null)
{
try
{
browser.close();
} catch (IOException ioe)
{
logger.error(ioe.getMessage(), ioe);
}
}
}
}
@Override
public SenseiResult mergePartitionedResults(SenseiRequest r,
List<SenseiResult> resultList) {
try
{
return ResultMerger.merge(r, resultList, true);
}
finally
{
if (resultList != null)
{
for (SenseiResult res : resultList)
{
SortCollector sortCollector = res.getSortCollector();
if (sortCollector != null)
{
sortCollector.close();
}
}
}
}
}
@Override
public SenseiResult getEmptyResultInstance(Throwable error) {
return new SenseiResult();
}
@Override
public Serializer<SenseiRequest, SenseiResult> getSerializer() {
return PROTO_V2_SERIALIZER;
}
}