forked from infinispan/infinispan
-
Notifications
You must be signed in to change notification settings - Fork 1
/
QueryInterceptor.java
572 lines (512 loc) · 23.7 KB
/
QueryInterceptor.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
package org.infinispan.query.backend;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
import javax.transaction.TransactionSynchronizationRegistry;
import org.hibernate.search.backend.TransactionContext;
import org.hibernate.search.backend.spi.Work;
import org.hibernate.search.backend.spi.WorkType;
import org.hibernate.search.backend.spi.Worker;
import org.hibernate.search.engine.spi.EntityIndexBinding;
import org.hibernate.search.engine.spi.SearchFactoryImplementor;
import org.hibernate.search.spi.SearchFactoryIntegrator;
import org.infinispan.Cache;
import org.infinispan.commands.FlagAffectedCommand;
import org.infinispan.commands.LocalFlagAffectedCommand;
import org.infinispan.commands.tx.PrepareCommand;
import org.infinispan.commands.write.ClearCommand;
import org.infinispan.commands.write.PutKeyValueCommand;
import org.infinispan.commands.write.PutMapCommand;
import org.infinispan.commands.write.RemoveCommand;
import org.infinispan.commands.write.ReplaceCommand;
import org.infinispan.commands.write.WriteCommand;
import org.infinispan.container.DataContainer;
import org.infinispan.container.entries.InternalCacheEntry;
import org.infinispan.context.Flag;
import org.infinispan.context.InvocationContext;
import org.infinispan.context.impl.TxInvocationContext;
import org.infinispan.factories.KnownComponentNames;
import org.infinispan.factories.annotations.ComponentName;
import org.infinispan.factories.annotations.Inject;
import org.infinispan.factories.annotations.Start;
import org.infinispan.factories.annotations.Stop;
import org.infinispan.interceptors.base.CommandInterceptor;
import org.infinispan.marshall.core.MarshalledValue;
import org.infinispan.notifications.Listener;
import org.infinispan.notifications.cachelistener.annotation.CacheEntryCreated;
import org.infinispan.notifications.cachelistener.annotation.CacheEntryModified;
import org.infinispan.notifications.cachelistener.event.CacheEntryCreatedEvent;
import org.infinispan.notifications.cachelistener.event.CacheEntryModifiedEvent;
import org.infinispan.query.Transformer;
import org.infinispan.query.impl.DefaultSearchWorkCreator;
import org.infinispan.query.logging.Log;
import org.infinispan.registry.ClusterRegistry;
import org.infinispan.registry.ScopedKey;
import org.infinispan.util.logging.LogFactory;
/**
* This interceptor will be created when the System Property "infinispan.query.indexLocalOnly" is "false"
* <p/>
* This type of interceptor will allow the indexing of data even when it comes from other caches within a cluster.
* <p/>
* However, if the a cache would not be putting the data locally, the interceptor will not index it.
*
* @author Navin Surtani
* @author Sanne Grinovero <sanne@hibernate.org> (C) 2011 Red Hat Inc.
* @author Marko Luksa
* @author anistor@redhat.com
* @since 4.0
*/
public class QueryInterceptor extends CommandInterceptor {
private final boolean isManualIndexing;
private final SearchFactoryIntegrator searchFactory;
private final Lock mutating = new ReentrantLock();
private final KeyTransformationHandler keyTransformationHandler = new KeyTransformationHandler();
private final KnownClassesRegistryListener registryListener = new KnownClassesRegistryListener();
private ReadIntensiveClusterRegistryWrapper<String, Class<?>, Boolean> clusterRegistry;
private SearchWorkCreator<Object> searchWorkCreator = new DefaultSearchWorkCreator<Object>();
private DataContainer dataContainer;
protected TransactionManager transactionManager;
protected TransactionSynchronizationRegistry transactionSynchronizationRegistry;
protected ExecutorService asyncExecutor;
private static final Log log = LogFactory.getLog(QueryInterceptor.class, Log.class);
@Override
protected Log getLog() {
return log;
}
public QueryInterceptor(SearchFactoryIntegrator searchFactory) {
this.searchFactory = searchFactory;
isManualIndexing = ((SearchFactoryImplementor)searchFactory).getIndexingStrategy().equals("manual");
}
@Inject
@SuppressWarnings("unused")
protected void injectDependencies(TransactionManager transactionManager,
TransactionSynchronizationRegistry transactionSynchronizationRegistry,
Cache cache,
ClusterRegistry<String, Class<?>, Boolean> clusterRegistry,
DataContainer dataContainer,
@ComponentName(KnownComponentNames.ASYNC_TRANSPORT_EXECUTOR) ExecutorService e) {
this.transactionManager = transactionManager;
this.transactionSynchronizationRegistry = transactionSynchronizationRegistry;
this.asyncExecutor = e;
this.dataContainer = dataContainer;
this.clusterRegistry = new ReadIntensiveClusterRegistryWrapper(clusterRegistry, "QueryKnownClasses#" + cache.getName());
}
@Start
protected void start() {
clusterRegistry.addListener(registryListener);
for (Class<?> c : clusterRegistry.keys()) {
enableClass(c);
}
}
@Stop
protected void stop() {
clusterRegistry.removeListener(registryListener);
}
@Listener
class KnownClassesRegistryListener {
@CacheEntryCreated
public void created(CacheEntryCreatedEvent<ScopedKey<String, Class>, Boolean> e) {
if (!e.isOriginLocal() && !e.isPre() && e.getValue()) {
enableClass(e.getKey().getKey());
}
}
@CacheEntryModified
public void modified(CacheEntryModifiedEvent<ScopedKey<String, Class>, Boolean> e) {
if (!e.isOriginLocal() && !e.isPre() && e.getValue()) {
enableClass(e.getKey().getKey());
}
}
}
protected boolean shouldModifyIndexes(FlagAffectedCommand command, InvocationContext ctx) {
return !isManualIndexing && !command.hasFlag(Flag.SKIP_INDEXING);
}
/**
* Use this executor for Async operations
* @return
*/
public ExecutorService getAsyncExecutor() {
return asyncExecutor;
}
@Override
public Object visitPutKeyValueCommand(InvocationContext ctx, PutKeyValueCommand command) throws Throwable {
Object toReturn = invokeNextInterceptor(ctx, command);
processPutKeyValueCommand(command, ctx, toReturn, null);
return toReturn;
}
@Override
public Object visitRemoveCommand(InvocationContext ctx, RemoveCommand command) throws Throwable {
// remove the object out of the cache first.
Object valueRemoved = invokeNextInterceptor(ctx, command);
processRemoveCommand(command, ctx, valueRemoved, null);
return valueRemoved;
}
@Override
public Object visitReplaceCommand(InvocationContext ctx, ReplaceCommand command) throws Throwable {
Object valueReplaced = invokeNextInterceptor(ctx, command);
processReplaceCommand(command, ctx, valueReplaced, null);
return valueReplaced;
}
@Override
public Object visitPutMapCommand(InvocationContext ctx, PutMapCommand command) throws Throwable {
Map<Object, Object> previousValues = (Map<Object, Object>) invokeNextInterceptor(ctx, command);
processPutMapCommand(command, ctx, previousValues, null);
return previousValues;
}
@Override
public Object visitClearCommand(final InvocationContext ctx, final ClearCommand command) throws Throwable {
// This method is called when somebody calls a cache.clear() and we will need to wipe everything in the indexes.
Object returnValue = invokeNextInterceptor(ctx, command);
processClearCommand(command, ctx, null);
return returnValue;
}
/**
* Remove all entries from all known indexes
*/
public void purgeAllIndexes() {
purgeAllIndexes(null);
}
private void purgeAllIndexes(TransactionContext transactionContext) {
transactionContext = transactionContext == null ? makeTransactionalEventContext() : transactionContext;
for (Class c : clusterRegistry.keys()) {
if (isIndexed(c)) {
//noinspection unchecked
performSearchWorks(searchWorkCreator.createPerEntityTypeWorks(c, WorkType.PURGE_ALL), transactionContext);
}
}
}
// Method that will be called when data needs to be removed from Lucene.
protected void removeFromIndexes(final Object value, final Object key, final TransactionContext transactionContext) {
performSearchWork(value, keyToString(key), WorkType.DELETE, transactionContext);
}
protected void updateIndexes(final boolean usingSkipIndexCleanupFlag, final Object value, final Object key, final TransactionContext transactionContext) {
// Note: it's generally unsafe to assume there is no previous entry to cleanup: always use UPDATE
// unless the specific flag is allowing this.
performSearchWork(value, keyToString(key), usingSkipIndexCleanupFlag ? WorkType.ADD : WorkType.UPDATE, transactionContext);
}
private void performSearchWork(Object value, Serializable id, WorkType workType, TransactionContext transactionContext) {
if (value == null) throw new NullPointerException("Cannot handle a null value!");
Collection<Work> works = searchWorkCreator.createPerEntityWorks(value, id, workType);
performSearchWorks(works, transactionContext);
}
private void performSearchWorks(Collection<Work> works, TransactionContext transactionContext) {
Worker worker = searchFactory.getWorker();
for (Work work : works) {
worker.performWork(work, transactionContext);
}
}
public boolean isIndexed(final Class<?> c) {
final EntityIndexBinding indexBinding = this.searchFactory.getIndexBinding(c);
return indexBinding != null;
}
private Object extractValue(Object wrappedValue) {
if (wrappedValue instanceof MarshalledValue)
return ((MarshalledValue) wrappedValue).get();
else
return wrappedValue;
}
public void enableClasses(Class<?>[] classes) {
if (classes == null || classes.length == 0) {
return;
}
enableClassesIncrementally(classes, false);
}
private void enableClassesIncrementally(Class<?>[] classes, boolean locked) {
ArrayList<Class<?>> toAdd = null;
for (Class<?> type : classes) {
if (!clusterRegistry.containsKey(type)) {
if (toAdd==null)
toAdd = new ArrayList<Class<?>>(classes.length);
toAdd.add(type);
}
}
if (toAdd == null) {
return;
}
if (locked) {
final Transaction transaction = suspend();
try {
// we need to preserve the state of this flag manually because addClasses will cause reconfiguration and the flag is lost
boolean isStatisticsEnabled = searchFactory.getStatistics().isStatisticsEnabled();
searchFactory.addClasses(toAdd.toArray(new Class[toAdd.size()]));
searchFactory.getStatistics().setStatisticsEnabled(isStatisticsEnabled);
} finally {
resume(transaction);
}
for (Class<?> type : toAdd) {
clusterRegistry.put(type, isIndexed(type));
}
} else {
mutating.lock();
try {
enableClassesIncrementally(classes, true);
} finally {
mutating.unlock();
}
}
}
private void enableClass(Class<?> clazz) {
if (isIndexed(clazz)) {
return;
}
mutating.lock();
try {
final Transaction transaction = suspend();
try {
searchFactory.addClasses(new Class[]{clazz});
} finally {
resume(transaction);
}
} finally {
mutating.unlock();
}
}
public boolean updateKnownTypesIfNeeded(Object value) {
if ( value != null ) {
Class<?> potentialNewType = value.getClass();
if (!clusterRegistry.containsKey(potentialNewType)) {
mutating.lock();
try {
enableClassesIncrementally( new Class[]{potentialNewType}, true);
}
finally {
mutating.unlock();
}
}
return clusterRegistry.get(potentialNewType);
}
else {
return false;
}
}
public void registerKeyTransformer(Class<?> keyClass, Class<? extends Transformer> transformerClass) {
keyTransformationHandler.registerTransformer(keyClass, transformerClass);
}
private String keyToString(Object key) {
return keyTransformationHandler.keyToString(key);
}
public KeyTransformationHandler getKeyTransformationHandler() {
return keyTransformationHandler;
}
public void enableClasses(Set<Class> knownIndexedTypes) {
Class[] classes = knownIndexedTypes.toArray(new Class[knownIndexedTypes.size()]);
enableClasses(classes);
}
public SearchFactoryIntegrator getSearchFactory() {
return searchFactory;
}
public void setSearchWorkCreator(SearchWorkCreator<Object> searchWorkCreator) {
this.searchWorkCreator = searchWorkCreator;
}
public SearchWorkCreator<Object> getSearchWorkCreator() {
return searchWorkCreator;
}
/**
* In case of a remotely originating transactions we don't have a chance to visit the single
* commands but receive this "batch". We then need the before-apply snapshot of some types
* to route the cleanup commands to the correct indexes.
* Note we don't need to visit the CommitCommand as the indexing context is registered
* as a transaction sync.
*/
@Override
public Object visitPrepareCommand(TxInvocationContext ctx, PrepareCommand command) throws Throwable {
final WriteCommand[] writeCommands = command.getModifications();
final Object[] stateBeforePrepare = new Object[writeCommands.length];
for (int i=0; i<writeCommands.length; i++) {
final WriteCommand writeCommand = writeCommands[i];
if (writeCommand instanceof PutKeyValueCommand) {
InternalCacheEntry internalCacheEntry = dataContainer.get(((PutKeyValueCommand) writeCommand).getKey());
stateBeforePrepare[i] = internalCacheEntry != null ? internalCacheEntry.getValue() : null;
}
else if (writeCommand instanceof PutMapCommand) {
stateBeforePrepare[i] = getPreviousValues(((PutMapCommand) writeCommand).getMap().keySet());
}
else if (writeCommand instanceof RemoveCommand) {
InternalCacheEntry internalCacheEntry = dataContainer.get(((RemoveCommand) writeCommand).getKey());
stateBeforePrepare[i] = internalCacheEntry != null ? internalCacheEntry.getValue() : null;
}
else if (writeCommand instanceof ReplaceCommand) {
InternalCacheEntry internalCacheEntry = dataContainer.get(((ReplaceCommand) writeCommand).getKey());
stateBeforePrepare[i] = internalCacheEntry != null ? internalCacheEntry.getValue() : null;
}
}
final Object toReturn = super.visitPrepareCommand(ctx, command);
if (ctx.isTransactionValid()) {
final TransactionContext transactionContext = makeTransactionalEventContext();
for (int i=0; i<writeCommands.length; i++) {
final WriteCommand writeCommand = writeCommands[i];
if (writeCommand instanceof PutKeyValueCommand) {
processPutKeyValueCommand((PutKeyValueCommand) writeCommand, ctx, stateBeforePrepare[i], transactionContext);
}
else if (writeCommand instanceof PutMapCommand) {
processPutMapCommand((PutMapCommand) writeCommand, ctx, (Map<Object, Object>) stateBeforePrepare[i], transactionContext);
}
else if (writeCommand instanceof RemoveCommand) {
processRemoveCommand((RemoveCommand) writeCommand, ctx, stateBeforePrepare[i], transactionContext);
}
else if (writeCommand instanceof ReplaceCommand) {
processReplaceCommand((ReplaceCommand) writeCommand, ctx, stateBeforePrepare[i], transactionContext);
}
else if (writeCommand instanceof ClearCommand) {
processClearCommand((ClearCommand)writeCommand, ctx, transactionContext);
}
}
}
return toReturn;
}
private Map<Object, Object> getPreviousValues(Set<Object> keySet) {
HashMap<Object, Object> previousValues = new HashMap<Object, Object>();
for (Object key : keySet) {
InternalCacheEntry internalCacheEntry = dataContainer.get(key);
Object previousValue = internalCacheEntry != null ? internalCacheEntry.getValue() : null;
previousValues.put(key, previousValue);
}
return previousValues;
}
/**
* Indexing management of a ReplaceCommand
*
* @param command the ReplaceCommand
* @param ctx the InvocationContext
* @param valueReplaced the previous value on this key
* @param transactionContext Optional for lazy initialization, or reuse an existing context.
*/
private void processReplaceCommand(final ReplaceCommand command, final InvocationContext ctx, final Object valueReplaced, TransactionContext transactionContext) {
if (valueReplaced != null && command.isSuccessful() && shouldModifyIndexes(command, ctx)) {
final boolean usingSkipIndexCleanupFlag = usingSkipIndexCleanup(command);
Object[] parameters = command.getParameters();
Object p2 = extractValue(parameters[2]);
final boolean newValueIsIndexed = updateKnownTypesIfNeeded( p2 );
Object key = extractValue(command.getKey());
if (! usingSkipIndexCleanupFlag) {
final Object p1 = extractValue(parameters[1]);
final boolean originalIsIndexed = updateKnownTypesIfNeeded( p1 );
if (p1 != null && originalIsIndexed) {
transactionContext = transactionContext == null ? makeTransactionalEventContext() : transactionContext;
removeFromIndexes(p1, key, transactionContext);
}
}
if (newValueIsIndexed) {
transactionContext = transactionContext == null ? makeTransactionalEventContext() : transactionContext;
updateIndexes(usingSkipIndexCleanupFlag, p2, key, transactionContext);
}
}
}
/**
* Indexing management of a RemoveCommand
*
* @param command the visited RemoveCommand
* @param ctx the InvocationContext of the RemoveCommand
* @param valueRemoved the value before the removal
* @param transactionContext Optional for lazy initialization, or reuse an existing context.
*/
private void processRemoveCommand(final RemoveCommand command, final InvocationContext ctx, final Object valueRemoved, TransactionContext transactionContext) {
if (command.isSuccessful() && !command.isNonExistent() && shouldModifyIndexes(command, ctx)) {
final Object value = extractValue(valueRemoved);
if (updateKnownTypesIfNeeded(value)) {
transactionContext = transactionContext == null ? makeTransactionalEventContext() : transactionContext;
removeFromIndexes(value, extractValue(command.getKey()), transactionContext);
}
}
}
/**
* Indexing management of a PutMapCommand
*
* @param command the visited PutMapCommand
* @param ctx the InvocationContext of the PutMapCommand
* @param previousValues a map with the previous values, before processing the given PutMapCommand
* @param transactionContext
*/
private void processPutMapCommand(final PutMapCommand command, final InvocationContext ctx, final Map<Object, Object> previousValues, TransactionContext transactionContext) {
if (shouldModifyIndexes(command, ctx)) {
Map<Object, Object> dataMap = command.getMap();
final boolean usingSkipIndexCleanupFlag = usingSkipIndexCleanup(command);
// Loop through all the keys and put those key-value pairings into lucene.
for (Map.Entry<Object, Object> entry : dataMap.entrySet()) {
final Object key = extractValue(entry.getKey());
final Object value = extractValue(entry.getValue());
final Object previousValue = previousValues.get(key);
if (!usingSkipIndexCleanupFlag && updateKnownTypesIfNeeded(previousValue)) {
transactionContext = transactionContext == null ? makeTransactionalEventContext() : transactionContext;
removeFromIndexes(previousValue, key, transactionContext);
}
if (updateKnownTypesIfNeeded(value)) {
transactionContext = transactionContext == null ? makeTransactionalEventContext() : transactionContext;
updateIndexes(usingSkipIndexCleanupFlag, value, key, transactionContext);
}
}
}
}
/**
* Indexing management of a PutKeyValueCommand
*
* @param command the visited PutKeyValueCommand
* @param ctx the InvocationContext of the PutKeyValueCommand
* @param previousValue the value being replaced by the put operation
* @param transactionContext Optional for lazy initialization, or reuse an existing context.
*/
private void processPutKeyValueCommand(final PutKeyValueCommand command, final InvocationContext ctx, final Object previousValue, TransactionContext transactionContext) {
final boolean usingSkipIndexCleanupFlag = usingSkipIndexCleanup(command);
//whatever the new type, we might still need to cleanup for the previous value (and schedule removal first!)
if (!usingSkipIndexCleanupFlag && updateKnownTypesIfNeeded(previousValue)) {
if (shouldModifyIndexes(command, ctx)) {
transactionContext = transactionContext == null ? makeTransactionalEventContext() : transactionContext;
removeFromIndexes(previousValue, extractValue(command.getKey()), transactionContext);
}
}
Object value = extractValue(command.getValue());
if (updateKnownTypesIfNeeded(value)) {
if (shouldModifyIndexes(command, ctx)) {
// This means that the entry is just modified so we need to update the indexes and not add to them.
transactionContext = transactionContext == null ? makeTransactionalEventContext() : transactionContext;
updateIndexes(usingSkipIndexCleanupFlag, value, extractValue(command.getKey()), transactionContext);
}
}
}
/**
* Indexing management of the Clear command
*
* @param command the ClearCommand
* @param ctx the InvocationContext of the PutKeyValueCommand
* @param transactionContext Optional for lazy initialization, or to reuse an existing transactional context.
*/
private void processClearCommand(final ClearCommand command, final InvocationContext ctx, TransactionContext transactionContext) {
if (shouldModifyIndexes(command, ctx)) {
purgeAllIndexes(transactionContext);
}
}
private TransactionContext makeTransactionalEventContext() {
return new TransactionalEventTransactionContext(transactionManager, transactionSynchronizationRegistry);
}
private Transaction suspend() {
if (transactionManager == null) {
return null;
}
try {
return transactionManager.suspend();
} catch (Exception e) {
//ignored
}
return null;
}
private void resume(Transaction transaction) {
if (transaction == null || transactionManager == null) {
return;
}
try {
transactionManager.resume(transaction);
} catch (Exception e) {
//ignored;
}
}
private boolean usingSkipIndexCleanup(final LocalFlagAffectedCommand command) {
return command != null && command.hasFlag(Flag.SKIP_INDEX_CLEANUP);
}
}