-
Notifications
You must be signed in to change notification settings - Fork 612
/
EntryWrappingInterceptor.java
437 lines (395 loc) · 18.4 KB
/
EntryWrappingInterceptor.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
/*
* Copyright 2011 Red Hat, Inc. and/or its affiliates.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
package org.infinispan.interceptors;
import org.infinispan.commands.AbstractVisitor;
import org.infinispan.commands.CommandsFactory;
import org.infinispan.commands.FlagAffectedCommand;
import org.infinispan.commands.read.GetKeyValueCommand;
import org.infinispan.commands.tx.CommitCommand;
import org.infinispan.commands.tx.PrepareCommand;
import org.infinispan.commands.write.ApplyDeltaCommand;
import org.infinispan.commands.write.ClearCommand;
import org.infinispan.commands.write.EvictCommand;
import org.infinispan.commands.write.InvalidateCommand;
import org.infinispan.commands.write.InvalidateL1Command;
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.EntryFactory;
import org.infinispan.container.entries.CacheEntry;
import org.infinispan.container.entries.InternalCacheEntry;
import org.infinispan.context.Flag;
import org.infinispan.context.InvocationContext;
import org.infinispan.context.SingleKeyNonTxInvocationContext;
import org.infinispan.context.impl.TxInvocationContext;
import org.infinispan.factories.annotations.Inject;
import org.infinispan.factories.annotations.Start;
import org.infinispan.interceptors.base.CommandInterceptor;
import org.infinispan.interceptors.locking.ClusteringDependentLogic;
import org.infinispan.statetransfer.OutdatedTopologyException;
import org.infinispan.statetransfer.StateConsumer;
import org.infinispan.statetransfer.StateTransferLock;
import org.infinispan.transaction.LocalTransaction;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* Interceptor in charge with wrapping entries and add them in caller's context.
*
* @author Mircea Markus
* @since 5.1
*/
public class EntryWrappingInterceptor extends CommandInterceptor {
private EntryFactory entryFactory;
protected DataContainer dataContainer;
protected ClusteringDependentLogic cdl;
protected final EntryWrappingVisitor entryWrappingVisitor = new EntryWrappingVisitor();
private CommandsFactory commandFactory;
private boolean isUsingLockDelegation;
private StateConsumer stateConsumer; // optional
private StateTransferLock stateTransferLock;
private static final Log log = LogFactory.getLog(EntryWrappingInterceptor.class);
private static final boolean trace = log.isTraceEnabled();
@Override
protected Log getLog() {
return log;
}
@Inject
public void init(EntryFactory entryFactory, DataContainer dataContainer, ClusteringDependentLogic cdl,
CommandsFactory commandFactory, StateConsumer stateConsumer, StateTransferLock stateTransferLock) {
this.entryFactory = entryFactory;
this.dataContainer = dataContainer;
this.cdl = cdl;
this.commandFactory = commandFactory;
this.stateConsumer = stateConsumer;
this.stateTransferLock = stateTransferLock;
}
@Start
public void start() {
isUsingLockDelegation = !cacheConfiguration.transaction().transactionMode().isTransactional() &&
cacheConfiguration.locking().supportsConcurrentUpdates() && cacheConfiguration.clustering().cacheMode().isDistributed();
}
@Override
public Object visitPrepareCommand(TxInvocationContext ctx, PrepareCommand command) throws Throwable {
if (!ctx.isOriginLocal() || command.isReplayEntryWrapping()) {
for (WriteCommand c : command.getModifications()) {
c.acceptVisitor(ctx, entryWrappingVisitor);
}
}
Object result = invokeNextInterceptor(ctx, command);
if (command.isOnePhaseCommit()) {
commitContextEntries(ctx, false, isFromStateTransfer(ctx));
}
return result;
}
@Override
public Object visitCommitCommand(TxInvocationContext ctx, CommitCommand command) throws Throwable {
try {
return invokeNextInterceptor(ctx, command);
} finally {
commitContextEntries(ctx, false, isFromStateTransfer(ctx));
}
}
@Override
public final Object visitGetKeyValueCommand(InvocationContext ctx, GetKeyValueCommand command) throws Throwable {
try {
entryFactory.wrapEntryForReading(ctx, command.getKey());
return invokeNextInterceptor(ctx, command);
} finally {
//needed because entries might be added in L1
if (!ctx.isInTxScope())
commitContextEntries(ctx, command.hasFlag(Flag.SKIP_OWNERSHIP_CHECK), false);
}
}
@Override
public final Object visitInvalidateCommand(InvocationContext ctx, InvalidateCommand command) throws Throwable {
if (command.getKeys() != null) {
for (Object key : command.getKeys()) {
entryFactory.wrapEntryForRemove(ctx, key);
}
}
return invokeNextAndApplyChanges(ctx, command);
}
@Override
public final Object visitClearCommand(InvocationContext ctx, ClearCommand command) throws Throwable {
for (InternalCacheEntry entry : dataContainer.entrySet())
entryFactory.wrapEntryForClear(ctx, entry.getKey());
return invokeNextAndApplyChanges(ctx, command);
}
@Override
public Object visitInvalidateL1Command(InvocationContext ctx, InvalidateL1Command command) throws Throwable {
for (Object key : command.getKeys()) {
entryFactory.wrapEntryForRemove(ctx, key);
if (trace)
log.tracef("Entry to be removed: %s", ctx.getLookedUpEntries());
}
return invokeNextAndApplyChanges(ctx, command);
}
@Override
public final Object visitPutKeyValueCommand(InvocationContext ctx, PutKeyValueCommand command) throws Throwable {
if (shouldWrap(command.getKey(), ctx, command)) {
entryFactory.wrapEntryForPut(ctx, command.getKey(), null, !command.isPutIfAbsent(), command);
}
return invokeNextAndApplyChanges(ctx, command);
}
private boolean shouldWrap(Object key, InvocationContext ctx, FlagAffectedCommand command) {
if (command.hasFlag(Flag.SKIP_OWNERSHIP_CHECK)) {
log.tracef("Skipping ownership check and wrapping key %s", key);
return true;
} else if (command.hasFlag(Flag.CACHE_MODE_LOCAL)) {
if (trace) {
log.tracef("CACHE_MODE_LOCAL is set. Wrapping key %s", key);
}
return true;
}
boolean result;
if (cacheConfiguration.transaction().transactionMode().isTransactional()) {
result = true;
} else {
if (isUsingLockDelegation) {
result = cdl.localNodeIsPrimaryOwner(key) || (cdl.localNodeIsOwner(key) && !ctx.isOriginLocal());
} else {
result = cdl.localNodeIsOwner(key);
}
}
log.tracef("Wrapping entry '%s'? %s", key, result);
return result;
}
@Override
public Object visitApplyDeltaCommand(InvocationContext ctx, ApplyDeltaCommand command) throws Throwable {
entryFactory.wrapEntryForDelta(ctx, command.getKey(), command.getDelta());
return invokeNextInterceptor(ctx, command);
}
@Override
public final Object visitRemoveCommand(InvocationContext ctx, RemoveCommand command) throws Throwable {
if (shouldWrap(command.getKey(), ctx, command)) {
entryFactory.wrapEntryForRemove(ctx, command.getKey());
}
return invokeNextAndApplyChanges(ctx, command);
}
@Override
public final Object visitReplaceCommand(InvocationContext ctx, ReplaceCommand command) throws Throwable {
if (shouldWrap(command.getKey(), ctx, command)) {
if (command.isIgnorePreviousValue()) {
//wrap it for put, as the previous value might not be present by now (e.g. might have been deleted)
// but we still need to apply the new value.
entryFactory.wrapEntryForPut(ctx, command.getKey(), null, false, command);
} else {
entryFactory.wrapEntryForReplace(ctx, command.getKey());
}
}
return invokeNextAndApplyChanges(ctx, command);
}
@Override
public Object visitPutMapCommand(InvocationContext ctx, PutMapCommand command) throws Throwable {
for (Object key : command.getMap().keySet()) {
if (shouldWrap(key, ctx, command)) {
entryFactory.wrapEntryForPut(ctx, key, null, true, command);
}
}
return invokeNextAndApplyChanges(ctx, command);
}
@Override
public Object visitEvictCommand(InvocationContext ctx, EvictCommand command) throws Throwable {
return visitRemoveCommand(ctx, command);
}
protected boolean isFromStateTransfer(InvocationContext ctx) {
if (ctx.isInTxScope() && ctx.isOriginLocal()) {
LocalTransaction localTx = (LocalTransaction) ((TxInvocationContext) ctx).getCacheTransaction();
if (localTx.isFromStateTransfer()) {
return true;
}
}
return false;
}
protected boolean isFromStateTransfer(FlagAffectedCommand command) {
return command.hasFlag(Flag.PUT_FOR_STATE_TRANSFER);
}
protected final void commitContextEntries(InvocationContext ctx, boolean skipOwnershipCheck, boolean isPutForStateTransfer) {
if (!isPutForStateTransfer && stateConsumer != null
&& ctx instanceof TxInvocationContext
&& ((TxInvocationContext) ctx).getCacheTransaction().hasModification(ClearCommand.class)) {
// If we are committing a ClearCommand now then no keys should be written by state transfer from
// now on until current rebalance ends.
stateConsumer.stopApplyingState();
}
if (ctx instanceof SingleKeyNonTxInvocationContext) {
SingleKeyNonTxInvocationContext singleKeyCtx = (SingleKeyNonTxInvocationContext) ctx;
commitEntryIfNeeded(ctx, skipOwnershipCheck, singleKeyCtx.getKey(), singleKeyCtx.getCacheEntry(), isPutForStateTransfer);
} else {
Set<Map.Entry<Object, CacheEntry>> entries = ctx.getLookedUpEntries().entrySet();
Iterator<Map.Entry<Object, CacheEntry>> it = entries.iterator();
final Log log = getLog();
while (it.hasNext()) {
Map.Entry<Object, CacheEntry> e = it.next();
CacheEntry entry = e.getValue();
if (!commitEntryIfNeeded(ctx, skipOwnershipCheck, e.getKey(), entry, isPutForStateTransfer)) {
if (trace) {
if (entry == null)
log.tracef("Entry for key %s is null : not calling commitUpdate", e.getKey());
else
log.tracef("Entry for key %s is not changed(%s): not calling commitUpdate", e.getKey(), entry);
}
}
}
}
}
protected void commitContextEntry(CacheEntry entry, InvocationContext ctx, boolean skipOwnershipCheck) {
cdl.commitEntry(entry, null, skipOwnershipCheck, ctx);
}
private Object invokeNextAndApplyChanges(InvocationContext ctx, FlagAffectedCommand command) throws Throwable {
final Object result = invokeNextInterceptor(ctx, command);
if (!ctx.isInTxScope()) {
stateTransferLock.acquireSharedTopologyLock();
try {
// We only retry non-tx write commands
if (command instanceof WriteCommand) {
WriteCommand writeCommand = (WriteCommand) command;
// Can't perform the check during preload or if the cache isn't clustered
boolean useLockForwarding = cacheConfiguration.clustering().cacheMode().isDistributed() &&
cacheConfiguration.locking().supportsConcurrentUpdates();
boolean isSync = (cacheConfiguration.clustering().cacheMode().isSynchronous() &&
!command.hasFlag(Flag.FORCE_ASYNCHRONOUS)) || command.hasFlag(Flag.FORCE_SYNCHRONOUS);
if (writeCommand.isSuccessful() && useLockForwarding && stateConsumer != null &&
stateConsumer.getCacheTopology() != null) {
int commandTopologyId = command.getTopologyId();
int currentTopologyId = stateConsumer.getCacheTopology().getTopologyId();
// TotalOrderStateTransferInterceptor doesn't set the topology id for PFERs.
if ( isSync && currentTopologyId != commandTopologyId && commandTopologyId != -1) {
if (trace) log.tracef("Cache topology changed while the command was executing: expected %d, got %d",
commandTopologyId, currentTopologyId);
if (command instanceof WriteCommand) {
writeCommand.setIgnorePreviousValue(true);
}
throw new OutdatedTopologyException("Cache topology changed while the command was executing: expected " +
commandTopologyId + ", got " + currentTopologyId);
}
}
}
commitContextEntries(ctx, command.hasFlag(Flag.SKIP_OWNERSHIP_CHECK), isFromStateTransfer(command));
} finally {
stateTransferLock.releaseSharedTopologyLock();
}
}
if (trace) log.tracef("The return value is %s", result);
return result;
}
private final class EntryWrappingVisitor extends AbstractVisitor {
@Override
public Object visitClearCommand(InvocationContext ctx, ClearCommand command) throws Throwable {
boolean wrapped = false;
for (Object key : dataContainer.keySet()) {
entryFactory.wrapEntryForClear(ctx, key);
wrapped = true;
}
if (wrapped)
invokeNextInterceptor(ctx, command);
if (stateConsumer != null && !ctx.isInTxScope()) {
// If a non-tx ClearCommand was executed successfully we must stop recording updated keys and do not
// allow any further updates to be written by state transfer from now on until current rebalance ends.
stateConsumer.stopApplyingState();
}
return null;
}
@Override
public Object visitPutMapCommand(InvocationContext ctx, PutMapCommand command) throws Throwable {
Map<Object, Object> newMap = new HashMap<Object, Object>(4);
for (Map.Entry<Object, Object> e : command.getMap().entrySet()) {
Object key = e.getKey();
if (cdl.localNodeIsOwner(key)) {
entryFactory.wrapEntryForPut(ctx, key, null, true, command);
newMap.put(key, e.getValue());
}
}
if (newMap.size() > 0) {
PutMapCommand clonedCommand = commandFactory.buildPutMapCommand(newMap, command.getLifespanMillis(), command.getMaxIdleTimeMillis(), command.getFlags());
invokeNextInterceptor(ctx, clonedCommand);
}
return null;
}
@Override
public Object visitRemoveCommand(InvocationContext ctx, RemoveCommand command) throws Throwable {
if (cdl.localNodeIsOwner(command.getKey())) {
entryFactory.wrapEntryForRemove(ctx, command.getKey());
invokeNextInterceptor(ctx, command);
}
return null;
}
@Override
public Object visitPutKeyValueCommand(InvocationContext ctx, PutKeyValueCommand command) throws Throwable {
if (cdl.localNodeIsOwner(command.getKey())) {
entryFactory.wrapEntryForPut(ctx, command.getKey(), null, !command.isPutIfAbsent(), command);
invokeNextInterceptor(ctx, command);
}
return null;
}
@Override
public Object visitApplyDeltaCommand(InvocationContext ctx, ApplyDeltaCommand command) throws Throwable {
if (cdl.localNodeIsOwner(command.getKey())) {
entryFactory.wrapEntryForDelta(ctx, command.getKey(), command.getDelta());
invokeNextInterceptor(ctx, command);
}
return null;
}
@Override
public Object visitReplaceCommand(InvocationContext ctx, ReplaceCommand command) throws Throwable {
if (cdl.localNodeIsOwner(command.getKey())) {
if (command.isIgnorePreviousValue()) {
//wrap it for put, as the previous value might not be present by now (e.g. might have been deleted)
// but we still need to apply the new value.
entryFactory.wrapEntryForPut(ctx, command.getKey(), null, false, command);
} else {
entryFactory.wrapEntryForReplace(ctx, command.getKey());
}
invokeNextInterceptor(ctx, command);
}
return null;
}
}
private boolean commitEntryIfNeeded(InvocationContext ctx, boolean skipOwnershipCheck, Object key, CacheEntry entry, boolean isPutForStateTransfer) {
if (entry == null) {
if (key != null && !isPutForStateTransfer && stateConsumer != null) {
// this key is not yet stored locally
stateConsumer.addUpdatedKey(key);
}
return false;
}
if (isPutForStateTransfer && stateConsumer.isKeyUpdated(key)) {
// This is a state transfer put command on a key that was already modified by other user commands. We need to back off.
log.tracef("State transfer will not write key/value %s/%s because it was already updated by somebody else", key, entry.getValue());
entry.rollback();
return false;
}
if (entry.isChanged() || entry.isLoaded()) {
log.tracef("About to commit entry %s", entry);
commitContextEntry(entry, ctx, skipOwnershipCheck);
if (!isPutForStateTransfer && stateConsumer != null) {
stateConsumer.addUpdatedKey(key);
}
return true;
}
return false;
}
}