forked from infinispan/infinispan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueryInterceptor.java
438 lines (385 loc) · 17.5 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
package org.infinispan.query.backend;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import org.hibernate.search.util.common.impl.Futures;
import org.infinispan.AdvancedCache;
import org.infinispan.commands.FlagAffectedCommand;
import org.infinispan.commands.SegmentSpecificCommand;
import org.infinispan.commands.VisitableCommand;
import org.infinispan.commands.functional.ReadWriteKeyCommand;
import org.infinispan.commands.functional.ReadWriteKeyValueCommand;
import org.infinispan.commands.functional.ReadWriteManyCommand;
import org.infinispan.commands.functional.ReadWriteManyEntriesCommand;
import org.infinispan.commands.functional.WriteOnlyKeyCommand;
import org.infinispan.commands.functional.WriteOnlyKeyValueCommand;
import org.infinispan.commands.functional.WriteOnlyManyCommand;
import org.infinispan.commands.functional.WriteOnlyManyEntriesCommand;
import org.infinispan.commands.write.ClearCommand;
import org.infinispan.commands.write.ComputeCommand;
import org.infinispan.commands.write.ComputeIfAbsentCommand;
import org.infinispan.commands.write.DataWriteCommand;
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.commons.util.IntSet;
import org.infinispan.container.entries.CacheEntry;
import org.infinispan.context.InvocationContext;
import org.infinispan.context.impl.FlagBitSets;
import org.infinispan.context.impl.TxInvocationContext;
import org.infinispan.distribution.DistributionInfo;
import org.infinispan.distribution.DistributionManager;
import org.infinispan.distribution.ch.KeyPartitioner;
import org.infinispan.encoding.DataConversion;
import org.infinispan.factories.annotations.Inject;
import org.infinispan.factories.annotations.Start;
import org.infinispan.interceptors.DDAsyncInterceptor;
import org.infinispan.interceptors.InvocationSuccessAction;
import org.infinispan.query.logging.Log;
import org.infinispan.search.mapper.mapping.SearchMapping;
import org.infinispan.search.mapper.mapping.SearchMappingHolder;
import org.infinispan.search.mapper.work.SearchIndexer;
import org.infinispan.transaction.xa.GlobalTransaction;
import org.infinispan.util.concurrent.BlockingManager;
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 final class QueryInterceptor extends DDAsyncInterceptor {
private static final Log log = LogFactory.getLog(QueryInterceptor.class, Log.class);
private static final boolean trace = log.isTraceEnabled();
static final Object UNKNOWN = new Object() {
@Override
public String toString() {
return "<UNKNOWN>";
}
};
@Inject DistributionManager distributionManager;
@Inject BlockingManager blockingManager;
@Inject protected KeyPartitioner keyPartitioner;
private final SearchMappingHolder searchMappingHolder;
private final KeyTransformationHandler keyTransformationHandler;
private final AtomicBoolean stopping = new AtomicBoolean(false);
private final ConcurrentMap<GlobalTransaction, Map<Object, Object>> txOldValues;
private final DataConversion valueDataConversion;
private final DataConversion keyDataConversion;
private final boolean isPersistenceEnabled;
private final InvocationSuccessAction<ClearCommand> processClearCommand = this::processClearCommand;
private final boolean isManualIndexing;
private final AdvancedCache<?, ?> cache;
private final Map<String, Class<?>> indexedClasses;
private SegmentListener segmentListener;
public QueryInterceptor(SearchMappingHolder searchMappingHolder, KeyTransformationHandler keyTransformationHandler,
boolean isManualIndexing, ConcurrentMap<GlobalTransaction, Map<Object, Object>> txOldValues,
AdvancedCache<?, ?> cache, Map<String, Class<?>> indexedClasses) {
this.searchMappingHolder = searchMappingHolder;
this.keyTransformationHandler = keyTransformationHandler;
this.isManualIndexing = isManualIndexing;
this.txOldValues = txOldValues;
this.valueDataConversion = cache.getValueDataConversion();
this.keyDataConversion = cache.getKeyDataConversion();
this.isPersistenceEnabled = cache.getCacheConfiguration().persistence().usingStores();
this.cache = cache;
this.indexedClasses = Collections.unmodifiableMap(indexedClasses);
}
@Start
protected void start() {
stopping.set(false);
boolean isClustered = cache.getCacheConfiguration().clustering().cacheMode().isClustered();
if (isClustered) {
segmentListener = new SegmentListener(cache, this::purgeIndex, blockingManager);
this.cache.addListener(segmentListener);
}
}
public void prepareForStopping() {
if (segmentListener != null) cache.removeListener(segmentListener);
stopping.set(true);
}
private boolean shouldModifyIndexes(FlagAffectedCommand command, InvocationContext ctx, Object key) {
if (isManualIndexing) return false;
if (distributionManager == null || key == null) {
return true;
}
DistributionInfo info = distributionManager.getCacheTopology().getDistribution(key);
// If this is a backup node we should modify the entry in the remote context
return info.isPrimary() || info.isWriteOwner() &&
(ctx.isInTxScope() || !ctx.isOriginLocal() || command != null && command.hasAnyFlag(FlagBitSets.PUT_FOR_STATE_TRANSFER));
}
public BlockingManager getBlockingManager() {
return blockingManager;
}
private Object handleDataWriteCommand(InvocationContext ctx, DataWriteCommand command) {
if (command.hasAnyFlag(FlagBitSets.SKIP_INDEXING)) {
return invokeNext(ctx, command);
}
CacheEntry entry = ctx.lookupEntry(command.getKey());
if (ctx.isInTxScope()) {
// replay of modifications on remote node by EntryWrappingVisitor
if (entry != null && !entry.isChanged() && (entry.getValue() != null || !unreliablePreviousValue(command))) {
Map<Object, Object> oldValues = registerOldValues((TxInvocationContext) ctx);
oldValues.putIfAbsent(command.getKey(), entry.getValue());
}
return invokeNext(ctx, command);
} else {
Object prev = entry != null ? entry.getValue() : UNKNOWN;
if (prev == null && unreliablePreviousValue(command)) {
prev = UNKNOWN;
}
Object oldValue = prev;
return invokeNextThenApply(ctx, command, (rCtx, cmd, rv) -> {
if (!cmd.isSuccessful()) {
return rv;
}
CacheEntry entry2 = entry != null ? entry : rCtx.lookupEntry(cmd.getKey());
if (entry2 != null && entry2.isChanged()) {
// TODO: need to reduce the scope of the blocking thread to less if possible later as part of
// https://issues.redhat.com/browse/ISPN-11731
return asyncValue(blockingManager.runBlocking(() -> processChange(rCtx, cmd, cmd.getKey(), oldValue,
entry2.getValue()), cmd)
.thenApply(ignore -> rv));
}
return rv;
});
}
}
private Map<Object, Object> registerOldValues(TxInvocationContext ctx) {
return txOldValues.computeIfAbsent(ctx.getGlobalTransaction(), gid -> {
ctx.getCacheTransaction().addListener(() -> txOldValues.remove(gid));
return new HashMap<>();
});
}
private Object handleManyWriteCommand(InvocationContext ctx, WriteCommand command) {
if (command.hasAnyFlag(FlagBitSets.SKIP_INDEXING)) {
return invokeNext(ctx, command);
}
if (ctx.isInTxScope()) {
Map<Object, Object> oldValues = registerOldValues((TxInvocationContext) ctx);
for (Object key : command.getAffectedKeys()) {
CacheEntry entry = ctx.lookupEntry(key);
if (entry != null && !entry.isChanged() && (entry.getValue() != null || !unreliablePreviousValue(command))) {
oldValues.putIfAbsent(key, entry.getValue());
}
}
return invokeNext(ctx, command);
} else {
Map<Object, Object> oldValues = new HashMap<>();
for (Object key : command.getAffectedKeys()) {
CacheEntry entry = ctx.lookupEntry(key);
if (entry != null && (entry.getValue() != null || !unreliablePreviousValue(command))) {
oldValues.put(key, entry.getValue());
}
}
return invokeNextThenAccept(ctx, command, (rCtx, cmd, rv) -> {
if (!cmd.isSuccessful()) {
return;
}
for (Object key : cmd.getAffectedKeys()) {
CacheEntry entry = rCtx.lookupEntry(key);
// If the entry is null we are not an owner and won't index it
if (entry != null && entry.isChanged()) {
Object oldValue = oldValues.getOrDefault(key, UNKNOWN);
processChange(rCtx, cmd, key, oldValue, entry.getValue());
}
}
});
}
}
private boolean unreliablePreviousValue(WriteCommand command) {
// alternative approach would be changing the flag and forcing load type in an interceptor before EWI
return isPersistenceEnabled && (command.loadType() == VisitableCommand.LoadType.DONT_LOAD
|| command.hasAnyFlag(FlagBitSets.SKIP_CACHE_LOAD));
}
@Override
public Object visitPutKeyValueCommand(InvocationContext ctx, PutKeyValueCommand command) {
return handleDataWriteCommand(ctx, command);
}
@Override
public Object visitRemoveCommand(InvocationContext ctx, RemoveCommand command) {
return handleDataWriteCommand(ctx, command);
}
@Override
public Object visitReplaceCommand(InvocationContext ctx, ReplaceCommand command) {
return handleDataWriteCommand(ctx, command);
}
@Override
public Object visitComputeCommand(InvocationContext ctx, ComputeCommand command) {
return handleDataWriteCommand(ctx, command);
}
@Override
public Object visitComputeIfAbsentCommand(InvocationContext ctx, ComputeIfAbsentCommand command) {
return handleDataWriteCommand(ctx, command);
}
@Override
public Object visitPutMapCommand(InvocationContext ctx, PutMapCommand command) {
return handleManyWriteCommand(ctx, command);
}
@Override
public Object visitClearCommand(InvocationContext ctx, ClearCommand command) {
return invokeNextThenAccept(ctx, command, processClearCommand);
}
@Override
public Object visitReadWriteKeyCommand(InvocationContext ctx, ReadWriteKeyCommand command) {
return handleDataWriteCommand(ctx, command);
}
@Override
public Object visitWriteOnlyKeyCommand(InvocationContext ctx, WriteOnlyKeyCommand command) {
return handleDataWriteCommand(ctx, command);
}
@Override
public Object visitReadWriteKeyValueCommand(InvocationContext ctx, ReadWriteKeyValueCommand command) {
return handleDataWriteCommand(ctx, command);
}
@Override
public Object visitWriteOnlyManyEntriesCommand(InvocationContext ctx, WriteOnlyManyEntriesCommand command) {
return handleManyWriteCommand(ctx, command);
}
@Override
public Object visitWriteOnlyKeyValueCommand(InvocationContext ctx, WriteOnlyKeyValueCommand command) {
return handleDataWriteCommand(ctx, command);
}
@Override
public Object visitWriteOnlyManyCommand(InvocationContext ctx, WriteOnlyManyCommand command) {
return handleManyWriteCommand(ctx, command);
}
@Override
public Object visitReadWriteManyCommand(InvocationContext ctx, ReadWriteManyCommand command) {
return handleManyWriteCommand(ctx, command);
}
@Override
public Object visitReadWriteManyEntriesCommand(InvocationContext ctx, ReadWriteManyEntriesCommand command) {
return handleManyWriteCommand(ctx, command);
}
/**
* Remove all entries from all known indexes
*/
public void purgeAllIndexes() {
SearchMapping searchMapping = searchMappingHolder.getSearchMapping();
if (searchMapping == null) {
return;
}
searchMapping.scopeAll().workspace().purge();
}
public void purgeIndex(Class<?> entityType) {
SearchMapping searchMapping = searchMappingHolder.getSearchMapping();
if (searchMapping == null) {
return;
}
searchMapping.scope(entityType).workspace().purge();
}
/**
* Removes from the index the entries corresponding to the supplied segments, if the index is local.
*/
void purgeIndex(IntSet segments) {
if (segments == null || segments.isEmpty()) return;
SearchMapping searchMapping = searchMappingHolder.getSearchMapping();
if (searchMapping == null) {
return;
}
Set<String> routingKeys = segments.intStream().boxed().map(Objects::toString).collect(Collectors.toSet());
searchMapping.scopeAll().workspace().purge(routingKeys);
}
/**
* Remove entries from all indexes by key.
*/
void removeFromIndexes(Object key, int segment) {
Futures.unwrappedExceptionJoin(getSearchIndexer().purge(keyToString(key), String.valueOf(segment)));
}
// Method that will be called when data needs to be removed from Lucene.
private void removeFromIndexes(Object value, Object key, int segment) {
Futures.unwrappedExceptionJoin(getSearchIndexer().delete(keyToString(key), String.valueOf(segment), value));
}
private void updateIndexes(boolean usingSkipIndexCleanupFlag, Object value, Object key, int segment) {
// Note: it's generally unsafe to assume there is no previous entry to cleanup: always use UPDATE
// unless the specific flag is allowing this.
if (usingSkipIndexCleanupFlag) {
Futures.unwrappedExceptionJoin(getSearchIndexer().add(keyToString(key), String.valueOf(segment), value));
} else {
Futures.unwrappedExceptionJoin(getSearchIndexer().addOrUpdate(keyToString(key), String.valueOf(segment), value));
}
}
/**
* The indexed classes.
*
* @deprecated since 11
*/
@Deprecated
public Map<String, Class<?>> indexedEntities() {
return indexedClasses;
}
private SearchIndexer getSearchIndexer() {
return searchMappingHolder.getSearchMapping().getSearchIndexer();
}
private Object extractValue(Object storedValue) {
return valueDataConversion.extractIndexable(storedValue);
}
private Object extractKey(Object storedKey) {
return keyDataConversion.extractIndexable(storedKey);
}
private String keyToString(Object key) {
return keyTransformationHandler.keyToString(key);
}
public KeyTransformationHandler getKeyTransformationHandler() {
return keyTransformationHandler;
}
void processChange(InvocationContext ctx, FlagAffectedCommand command, Object storedKey, Object storedOldValue, Object storedNewValue) {
int segment = SegmentSpecificCommand.extractSegment(command, storedKey, keyPartitioner);
Object key = extractKey(storedKey);
Object oldValue = storedOldValue == UNKNOWN ? UNKNOWN : extractValue(storedOldValue);
Object newValue = extractValue(storedNewValue);
boolean skipIndexCleanup = command != null && command.hasAnyFlag(FlagBitSets.SKIP_INDEX_CLEANUP);
if (!skipIndexCleanup) {
if (oldValue == UNKNOWN) {
if (shouldModifyIndexes(command, ctx, storedKey)) {
removeFromIndexes(key, segment);
}
} else if (searchMappingHolder.isIndexedType(oldValue) && (newValue == null || shouldRemove(newValue, oldValue))
&& shouldModifyIndexes(command, ctx, storedKey)) {
removeFromIndexes(oldValue, key, segment);
} else if (trace) {
log.tracef("Index cleanup not needed for %s -> %s", oldValue, newValue);
}
} else if (trace) {
log.tracef("Skipped index cleanup for command %s", command);
}
if (searchMappingHolder.isIndexedType(newValue)) {
if (shouldModifyIndexes(command, ctx, storedKey)) {
// This means that the entry is just modified so we need to update the indexes and not add to them.
updateIndexes(skipIndexCleanup, newValue, key, segment);
} else {
if (trace) {
log.tracef("Not modifying index for %s (%s)", storedKey, command);
}
}
} else if (trace) {
log.tracef("Update not needed for %s", newValue);
}
}
private boolean shouldRemove(Object value, Object previousValue) {
return value != null && previousValue != null && value.getClass() != previousValue.getClass();
}
private void processClearCommand(InvocationContext ctx, ClearCommand command, Object rv) {
if (shouldModifyIndexes(command, ctx, null)) {
purgeAllIndexes();
}
}
public boolean isStopping() {
return stopping.get();
}
}