1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.search.solr.internal; |
21 |
|
|
22 |
|
import java.util.Arrays; |
23 |
|
import java.util.concurrent.BlockingQueue; |
24 |
|
import java.util.concurrent.LinkedBlockingQueue; |
25 |
|
|
26 |
|
import javax.inject.Inject; |
27 |
|
import javax.inject.Provider; |
28 |
|
import javax.inject.Singleton; |
29 |
|
|
30 |
|
import org.apache.solr.common.SolrInputDocument; |
31 |
|
import org.slf4j.Logger; |
32 |
|
import org.xwiki.component.annotation.Component; |
33 |
|
import org.xwiki.component.annotation.DisposePriority; |
34 |
|
import org.xwiki.component.manager.ComponentLifecycleException; |
35 |
|
import org.xwiki.component.manager.ComponentLookupException; |
36 |
|
import org.xwiki.component.manager.ComponentManager; |
37 |
|
import org.xwiki.component.phase.Disposable; |
38 |
|
import org.xwiki.component.phase.Initializable; |
39 |
|
import org.xwiki.component.phase.InitializationException; |
40 |
|
import org.xwiki.context.Execution; |
41 |
|
import org.xwiki.context.ExecutionContext; |
42 |
|
import org.xwiki.context.ExecutionContextException; |
43 |
|
import org.xwiki.context.ExecutionContextManager; |
44 |
|
import org.xwiki.job.JobException; |
45 |
|
import org.xwiki.job.JobExecutor; |
46 |
|
import org.xwiki.model.EntityType; |
47 |
|
import org.xwiki.model.reference.EntityReference; |
48 |
|
import org.xwiki.search.solr.internal.api.SolrConfiguration; |
49 |
|
import org.xwiki.search.solr.internal.api.SolrIndexer; |
50 |
|
import org.xwiki.search.solr.internal.api.SolrIndexerException; |
51 |
|
import org.xwiki.search.solr.internal.api.SolrInstance; |
52 |
|
import org.xwiki.search.solr.internal.job.IndexerJob; |
53 |
|
import org.xwiki.search.solr.internal.job.IndexerRequest; |
54 |
|
import org.xwiki.search.solr.internal.metadata.LengthSolrInputDocument; |
55 |
|
import org.xwiki.search.solr.internal.metadata.SolrMetadataExtractor; |
56 |
|
import org.xwiki.search.solr.internal.reference.SolrReferenceResolver; |
57 |
|
|
58 |
|
import com.xpn.xwiki.util.AbstractXWikiRunnable; |
59 |
|
|
60 |
|
|
61 |
|
@link |
62 |
|
|
63 |
|
|
64 |
|
@link |
65 |
|
|
66 |
|
|
67 |
|
@version |
68 |
|
@since |
69 |
|
|
70 |
|
@Component |
71 |
|
@Singleton |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
@DisposePriority(500) |
|
|
| 85.4% |
Uncovered Elements: 18 (123) |
Complexity: 33 |
Complexity Density: 0.39 |
|
76 |
|
public class DefaultSolrIndexer implements SolrIndexer, Initializable, Disposable, Runnable |
77 |
|
{ |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
@version |
82 |
|
|
|
|
| 27.3% |
Uncovered Elements: 16 (22) |
Complexity: 6 |
Complexity Density: 0.32 |
|
83 |
|
private static class IndexQueueEntry |
84 |
|
{ |
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
public EntityReference reference; |
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
public String deleteQuery; |
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
public IndexOperation operation; |
99 |
|
|
100 |
|
|
101 |
|
@param |
102 |
|
@param |
103 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
104 |
4469 |
public IndexQueueEntry(EntityReference indexReference, IndexOperation operation)... |
105 |
|
{ |
106 |
4469 |
this.reference = indexReference; |
107 |
4469 |
this.operation = operation; |
108 |
|
} |
109 |
|
|
110 |
|
|
111 |
|
@param |
112 |
|
@param |
113 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
114 |
3 |
public IndexQueueEntry(String deleteQuery, IndexOperation operation)... |
115 |
|
{ |
116 |
3 |
this.deleteQuery = deleteQuery; |
117 |
3 |
this.operation = operation; |
118 |
|
} |
119 |
|
|
|
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 4 |
Complexity Density: 0.27 |
|
120 |
0 |
@Override... |
121 |
|
public String toString() |
122 |
|
{ |
123 |
0 |
String str; |
124 |
|
|
125 |
0 |
switch (operation) { |
126 |
0 |
case INDEX: |
127 |
0 |
str = "INDEX " + this.reference; |
128 |
0 |
break; |
129 |
0 |
case DELETE: |
130 |
0 |
str = "DELETE " + this.deleteQuery; |
131 |
0 |
break; |
132 |
0 |
case STOP: |
133 |
0 |
str = "STOP"; |
134 |
0 |
break; |
135 |
0 |
default: |
136 |
0 |
str = ""; |
137 |
0 |
break; |
138 |
|
} |
139 |
|
|
140 |
0 |
return str; |
141 |
|
} |
142 |
|
} |
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
@version |
148 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.33 |
|
149 |
|
private static class ResolveQueueEntry |
150 |
|
{ |
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
public EntityReference reference; |
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
public boolean recurse; |
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
public IndexOperation operation; |
165 |
|
|
166 |
|
|
167 |
|
@param |
168 |
|
@param |
169 |
|
@param |
170 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
171 |
1972 |
public ResolveQueueEntry(EntityReference reference, boolean recurse, IndexOperation operation)... |
172 |
|
{ |
173 |
1972 |
this.reference = reference; |
174 |
1972 |
this.recurse = recurse; |
175 |
1972 |
this.operation = operation; |
176 |
|
} |
177 |
|
} |
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
@version |
183 |
|
|
|
|
| 81.6% |
Uncovered Elements: 7 (38) |
Complexity: 9 |
Complexity Density: 0.36 |
|
184 |
|
private class Resolver extends AbstractXWikiRunnable |
185 |
|
{ |
|
|
| 81.1% |
Uncovered Elements: 7 (37) |
Complexity: 9 |
Complexity Density: 0.36 |
|
186 |
3 |
@Override... |
187 |
|
public void runInternal() |
188 |
|
{ |
189 |
3 |
logger.debug("Start SOLR resolver thread"); |
190 |
|
|
191 |
1972 |
while (!Thread.interrupted()) { |
192 |
1972 |
ResolveQueueEntry queueEntry; |
193 |
1972 |
try { |
194 |
1972 |
queueEntry = resolveQueue.take(); |
195 |
|
} catch (InterruptedException e) { |
196 |
0 |
logger.warn("The SOLR resolve thread has been interrupted", e); |
197 |
0 |
queueEntry = RESOLVE_QUEUE_ENTRY_STOP; |
198 |
|
} |
199 |
|
|
200 |
1972 |
if (queueEntry == RESOLVE_QUEUE_ENTRY_STOP) { |
201 |
|
|
202 |
3 |
indexQueue.clear(); |
203 |
3 |
indexQueue.offer(INDEX_QUEUE_ENTRY_STOP); |
204 |
3 |
break; |
205 |
|
} |
206 |
|
|
207 |
1969 |
try { |
208 |
1969 |
if (queueEntry.operation == IndexOperation.INDEX) { |
209 |
1536 |
Iterable<EntityReference> references; |
210 |
1536 |
if (queueEntry.recurse) { |
211 |
482 |
references = solrRefereceResolver.getReferences(queueEntry.reference); |
212 |
|
} else { |
213 |
1054 |
references = Arrays.asList(queueEntry.reference); |
214 |
|
} |
215 |
|
|
216 |
1536 |
for (EntityReference reference : references) { |
217 |
4036 |
indexQueue.put(new IndexQueueEntry(reference, queueEntry.operation)); |
218 |
|
} |
219 |
|
} else { |
220 |
433 |
if (queueEntry.recurse) { |
221 |
0 |
indexQueue.put(new IndexQueueEntry(solrRefereceResolver.getQuery(queueEntry.reference), |
222 |
|
queueEntry.operation)); |
223 |
433 |
} else if (queueEntry.reference != null) { |
224 |
433 |
indexQueue.put(new IndexQueueEntry(queueEntry.reference, queueEntry.operation)); |
225 |
|
} |
226 |
|
} |
227 |
|
} catch (Throwable e) { |
228 |
0 |
logger.warn("Failed to apply operation [{}] on root reference [{}]", queueEntry.operation, |
229 |
|
queueEntry.reference, e); |
230 |
|
} |
231 |
|
} |
232 |
|
|
233 |
3 |
logger.debug("Stop SOLR resolver thread"); |
234 |
|
} |
235 |
|
} |
236 |
|
|
237 |
|
|
238 |
|
|
239 |
|
|
240 |
|
private static final ResolveQueueEntry RESOLVE_QUEUE_ENTRY_STOP = |
241 |
|
new ResolveQueueEntry(null, false, IndexOperation.STOP); |
242 |
|
|
243 |
|
|
244 |
|
|
245 |
|
|
246 |
|
private static final IndexQueueEntry INDEX_QUEUE_ENTRY_STOP = |
247 |
|
new IndexQueueEntry((String) null, IndexOperation.STOP); |
248 |
|
|
249 |
|
|
250 |
|
|
251 |
|
|
252 |
|
@Inject |
253 |
|
private Logger logger; |
254 |
|
|
255 |
|
|
256 |
|
|
257 |
|
|
258 |
|
@Inject |
259 |
|
private ComponentManager componentManager; |
260 |
|
|
261 |
|
|
262 |
|
|
263 |
|
|
264 |
|
@Inject |
265 |
|
private SolrConfiguration configuration; |
266 |
|
|
267 |
|
|
268 |
|
|
269 |
|
|
270 |
|
@Inject |
271 |
|
private Provider<SolrInstance> solrInstanceProvider; |
272 |
|
|
273 |
|
|
274 |
|
|
275 |
|
|
276 |
|
@Inject |
277 |
|
private SolrReferenceResolver solrRefereceResolver; |
278 |
|
|
279 |
|
@Inject |
280 |
|
private Execution execution; |
281 |
|
|
282 |
|
@Inject |
283 |
|
private ExecutionContextManager ecim; |
284 |
|
|
285 |
|
@Inject |
286 |
|
private JobExecutor jobs; |
287 |
|
|
288 |
|
|
289 |
|
|
290 |
|
|
291 |
|
private BlockingQueue<IndexQueueEntry> indexQueue; |
292 |
|
|
293 |
|
|
294 |
|
|
295 |
|
|
296 |
|
private BlockingQueue<ResolveQueueEntry> resolveQueue; |
297 |
|
|
298 |
|
|
299 |
|
|
300 |
|
|
301 |
|
private Thread indexThread; |
302 |
|
|
303 |
|
|
304 |
|
|
305 |
|
|
306 |
|
private Thread resolveThread; |
307 |
|
|
308 |
|
|
309 |
|
|
310 |
|
|
311 |
|
private boolean disposed; |
312 |
|
|
313 |
|
|
314 |
|
|
315 |
|
|
316 |
|
private volatile int batchSize; |
317 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
|
318 |
3 |
@Override... |
319 |
|
public void initialize() throws InitializationException |
320 |
|
{ |
321 |
|
|
322 |
3 |
this.resolveQueue = new LinkedBlockingQueue<>(); |
323 |
3 |
this.indexQueue = new LinkedBlockingQueue<>(this.configuration.getIndexerQueueCapacity()); |
324 |
|
|
325 |
|
|
326 |
3 |
this.resolveThread = new Thread(new Resolver()); |
327 |
3 |
this.resolveThread.setName("XWiki Solr resolve thread"); |
328 |
3 |
this.resolveThread.setDaemon(true); |
329 |
3 |
this.resolveThread.start(); |
330 |
3 |
this.resolveThread.setPriority(Thread.NORM_PRIORITY - 1); |
331 |
|
|
332 |
|
|
333 |
3 |
this.indexThread = new Thread(this); |
334 |
3 |
this.indexThread.setName("XWiki Solr index thread"); |
335 |
3 |
this.indexThread.setDaemon(true); |
336 |
3 |
this.indexThread.start(); |
337 |
3 |
this.indexThread.setPriority(Thread.NORM_PRIORITY - 1); |
338 |
|
} |
339 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
340 |
3 |
@Override... |
341 |
|
public void dispose() throws ComponentLifecycleException |
342 |
|
{ |
343 |
|
|
344 |
3 |
this.disposed = true; |
345 |
|
|
346 |
|
|
347 |
|
|
348 |
3 |
this.resolveQueue.clear(); |
349 |
3 |
this.resolveQueue.offer(RESOLVE_QUEUE_ENTRY_STOP); |
350 |
|
|
351 |
|
|
352 |
|
|
353 |
|
|
354 |
|
|
355 |
3 |
this.indexQueue.clear(); |
356 |
3 |
this.indexQueue.offer(INDEX_QUEUE_ENTRY_STOP); |
357 |
|
} |
358 |
|
|
|
|
| 78.6% |
Uncovered Elements: 3 (14) |
Complexity: 4 |
Complexity Density: 0.4 |
|
359 |
3 |
@Override... |
360 |
|
public void run() |
361 |
|
{ |
362 |
3 |
this.logger.debug("Start SOLR indexer thread"); |
363 |
|
|
364 |
96 |
while (!Thread.interrupted()) { |
365 |
|
|
366 |
96 |
IndexQueueEntry queueEntry = null; |
367 |
96 |
try { |
368 |
96 |
queueEntry = this.indexQueue.take(); |
369 |
|
} catch (InterruptedException e) { |
370 |
0 |
this.logger.warn("The SOLR index thread has been interrupted", e); |
371 |
|
|
372 |
0 |
queueEntry = INDEX_QUEUE_ENTRY_STOP; |
373 |
|
} |
374 |
|
|
375 |
|
|
376 |
|
|
377 |
96 |
if (!processBatch(queueEntry)) { |
378 |
3 |
break; |
379 |
|
} |
380 |
|
} |
381 |
|
|
382 |
3 |
this.logger.debug("Stop SOLR indexer thread"); |
383 |
|
} |
384 |
|
|
385 |
|
|
386 |
|
|
387 |
|
|
388 |
|
|
389 |
|
@param |
390 |
|
@return |
391 |
|
|
|
|
| 88.4% |
Uncovered Elements: 5 (43) |
Complexity: 10 |
Complexity Density: 0.37 |
|
392 |
96 |
private boolean processBatch(IndexQueueEntry queueEntry)... |
393 |
|
{ |
394 |
96 |
SolrInstance solrInstance = this.solrInstanceProvider.get(); |
395 |
|
|
396 |
96 |
int length = 0; |
397 |
|
|
398 |
4565 |
for (IndexQueueEntry batchEntry = queueEntry; batchEntry != null; batchEntry = this.indexQueue.poll()) { |
399 |
4472 |
if (batchEntry == INDEX_QUEUE_ENTRY_STOP) { |
400 |
|
|
401 |
3 |
return false; |
402 |
|
} |
403 |
|
|
404 |
4469 |
IndexOperation operation = batchEntry.operation; |
405 |
|
|
406 |
|
|
407 |
4469 |
try { |
408 |
4469 |
this.ecim.initialize(new ExecutionContext()); |
409 |
|
|
410 |
4469 |
if (IndexOperation.INDEX.equals(operation)) { |
411 |
4036 |
LengthSolrInputDocument solrDocument = getSolrDocument(batchEntry.reference); |
412 |
4036 |
if (solrDocument != null) { |
413 |
4031 |
solrInstance.add(solrDocument); |
414 |
4031 |
length += solrDocument.getLength(); |
415 |
4031 |
++this.batchSize; |
416 |
|
} |
417 |
433 |
} else if (IndexOperation.DELETE.equals(operation)) { |
418 |
433 |
if (batchEntry.reference == null) { |
419 |
0 |
solrInstance.deleteByQuery(batchEntry.deleteQuery); |
420 |
|
} else { |
421 |
433 |
solrInstance.delete(this.solrRefereceResolver.getId(batchEntry.reference)); |
422 |
|
} |
423 |
|
|
424 |
433 |
++this.batchSize; |
425 |
|
} |
426 |
|
} catch (Throwable e) { |
427 |
0 |
this.logger.error("Failed to process entry [{}]", batchEntry, e); |
428 |
|
} finally { |
429 |
4469 |
this.execution.removeContext(); |
430 |
|
} |
431 |
|
|
432 |
|
|
433 |
|
|
434 |
4469 |
if (shouldCommit(length, this.batchSize)) { |
435 |
235 |
commit(); |
436 |
235 |
length = 0; |
437 |
|
} |
438 |
|
} |
439 |
|
|
440 |
|
|
441 |
93 |
if (this.batchSize > 0) { |
442 |
93 |
commit(); |
443 |
|
} |
444 |
|
|
445 |
93 |
return true; |
446 |
|
} |
447 |
|
|
448 |
|
|
449 |
|
|
450 |
|
|
|
|
| 50% |
Uncovered Elements: 4 (8) |
Complexity: 3 |
Complexity Density: 0.38 |
|
451 |
328 |
private void commit()... |
452 |
|
{ |
453 |
328 |
SolrInstance solrInstance = this.solrInstanceProvider.get(); |
454 |
|
|
455 |
328 |
try { |
456 |
328 |
solrInstance.commit(); |
457 |
|
} catch (Exception e) { |
458 |
0 |
this.logger.error("Failed to commit index changes to the Solr server. Rolling back.", e); |
459 |
|
|
460 |
0 |
try { |
461 |
0 |
solrInstance.rollback(); |
462 |
|
} catch (Exception ex) { |
463 |
|
|
464 |
0 |
this.logger.error("Failed to rollback index changes.", ex); |
465 |
|
} |
466 |
|
} |
467 |
|
|
468 |
328 |
this.batchSize = 0; |
469 |
|
} |
470 |
|
|
471 |
|
|
472 |
|
|
473 |
|
|
474 |
|
@param |
475 |
|
@param |
476 |
|
@return |
477 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
478 |
4469 |
private boolean shouldCommit(int length, int size)... |
479 |
|
{ |
480 |
|
|
481 |
4469 |
if (length >= this.configuration.getIndexerBatchMaxLengh()) { |
482 |
235 |
return true; |
483 |
|
} |
484 |
|
|
485 |
|
|
486 |
4234 |
return size >= this.configuration.getIndexerBatchSize(); |
487 |
|
} |
488 |
|
|
489 |
|
|
490 |
|
@param |
491 |
|
@return@link |
492 |
|
|
493 |
|
@throws |
494 |
|
@throws |
495 |
|
@throws |
496 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
497 |
4036 |
private LengthSolrInputDocument getSolrDocument(EntityReference reference)... |
498 |
|
throws SolrIndexerException, IllegalArgumentException, ExecutionContextException |
499 |
|
{ |
500 |
4036 |
SolrMetadataExtractor metadataExtractor = getMetadataExtractor(reference.getType()); |
501 |
|
|
502 |
|
|
503 |
4036 |
if (metadataExtractor != null) { |
504 |
4036 |
return metadataExtractor.getSolrDocument(reference); |
505 |
|
} |
506 |
|
|
507 |
0 |
return null; |
508 |
|
} |
509 |
|
|
510 |
|
|
511 |
|
@param |
512 |
|
@return |
513 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
514 |
4036 |
private SolrMetadataExtractor getMetadataExtractor(EntityType entityType)... |
515 |
|
{ |
516 |
4036 |
SolrMetadataExtractor result = null; |
517 |
4036 |
try { |
518 |
4036 |
result = this.componentManager.getInstance(SolrMetadataExtractor.class, entityType.name().toLowerCase()); |
519 |
|
} catch (ComponentLookupException e) { |
520 |
0 |
this.logger.warn("Unsupported entity type: [{}]", entityType.toString(), e); |
521 |
|
} |
522 |
|
|
523 |
4036 |
return result; |
524 |
|
} |
525 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
526 |
1536 |
@Override... |
527 |
|
public void index(EntityReference reference, boolean recurse) |
528 |
|
{ |
529 |
1536 |
addToQueue(reference, recurse, IndexOperation.INDEX); |
530 |
|
} |
531 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
532 |
433 |
@Override... |
533 |
|
public void delete(EntityReference reference, boolean recurse) |
534 |
|
{ |
535 |
433 |
addToQueue(reference, recurse, IndexOperation.DELETE); |
536 |
|
} |
537 |
|
|
538 |
|
|
539 |
|
|
540 |
|
|
541 |
|
@param |
542 |
|
@param |
543 |
|
@param |
544 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
545 |
1969 |
private void addToQueue(EntityReference reference, boolean recurse, IndexOperation operation)... |
546 |
|
{ |
547 |
1969 |
if (!this.disposed) { |
548 |
|
|
549 |
1969 |
try { |
550 |
1969 |
this.resolveQueue.put(new ResolveQueueEntry(reference, recurse, operation)); |
551 |
|
} catch (InterruptedException e) { |
552 |
0 |
this.logger.error("Failed to add reference [{}] to Solr indexing queue", reference, e); |
553 |
|
} |
554 |
|
} |
555 |
|
} |
556 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
557 |
4821 |
@Override... |
558 |
|
public int getQueueSize() |
559 |
|
{ |
560 |
4821 |
return this.indexQueue.size() + this.resolveQueue.size() + this.batchSize; |
561 |
|
} |
562 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
563 |
3 |
@Override... |
564 |
|
public IndexerJob startIndex(IndexerRequest request) throws SolrIndexerException |
565 |
|
{ |
566 |
3 |
try { |
567 |
3 |
return (IndexerJob) this.jobs.execute(IndexerJob.JOBTYPE, request); |
568 |
|
} catch (JobException e) { |
569 |
0 |
throw new SolrIndexerException("Failed to start index job", e); |
570 |
|
} |
571 |
|
} |
572 |
|
} |