/
StateConsumerImpl.java
563 lines (500 loc) · 24.7 KB
/
StateConsumerImpl.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
/*
* JBoss, Home of Professional Open Source
* Copyright 2012 Red Hat Inc. and/or its affiliates and other
* contributors as indicated by the @author tags. All rights reserved.
* See the copyright.txt in the distribution for a full listing of
* individual contributors.
*
* 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 software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.infinispan.statetransfer;
import org.infinispan.Cache;
import org.infinispan.CacheException;
import org.infinispan.commands.CommandsFactory;
import org.infinispan.commands.write.InvalidateCommand;
import org.infinispan.commands.write.PutKeyValueCommand;
import org.infinispan.configuration.cache.Configuration;
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.InvocationContextContainer;
import org.infinispan.distribution.ch.ConsistentHash;
import org.infinispan.factories.annotations.Inject;
import org.infinispan.factories.annotations.Start;
import org.infinispan.factories.annotations.Stop;
import org.infinispan.interceptors.InterceptorChain;
import org.infinispan.loaders.CacheLoaderException;
import org.infinispan.loaders.CacheLoaderManager;
import org.infinispan.loaders.CacheStore;
import org.infinispan.remoting.rpc.RpcManager;
import org.infinispan.remoting.transport.Address;
import org.infinispan.topology.CacheTopology;
import org.infinispan.topology.LocalTopologyManager;
import org.infinispan.transaction.LockingMode;
import org.infinispan.transaction.RemoteTransaction;
import org.infinispan.transaction.TransactionTable;
import org.infinispan.transaction.xa.CacheTransaction;
import org.infinispan.util.ReadOnlyDataContainerBackedKeySet;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import static org.infinispan.context.Flag.*;
/**
* // TODO [anistor] Document this
*
* @author anistor@redhat.com
* @since 5.2
*/
public class StateConsumerImpl implements StateConsumer {
private static final Log log = LogFactory.getLog(StateConsumerImpl.class);
private static final boolean trace = log.isTraceEnabled();
private LocalTopologyManager localTopologyManager;
private String cacheName;
private Configuration configuration;
private RpcManager rpcManager;
private CommandsFactory commandsFactory;
private TransactionTable transactionTable; // optional
private DataContainer dataContainer;
private CacheLoaderManager cacheLoaderManager; // optional
private InterceptorChain interceptorChain;
private InvocationContextContainer icc;
private StateTransferLock stateTransferLock;
private long timeout;
private boolean useVersionedPut;
private boolean fetchEnabled;
private volatile CacheTopology cacheTopology;
/**
* The number of topology updates that are being processed concurrently (in method onTopologyUpdate()).
* This is needed to be able to detect completion.
*/
private AtomicInteger activeTopologyUpdates = new AtomicInteger(0);
/**
* Indicates if the currently executing topology update is a rebalance.
*/
private AtomicBoolean rebalanceInProgress = new AtomicBoolean(false);
/**
* A map that keeps track of current inbound state transfers by source address. There could be multiple transfers
* flowing in from the same source (but for different segments) so the values are lists. This works in tandem with
* transfersBySegment so they always need to be kept in sync and updates to both of them need to be atomic.
*/
private Map<Address, List<InboundTransferTask>> transfersBySource = new HashMap<Address, List<InboundTransferTask>>();
/**
* A map that keeps track of current inbound state transfers by segment id. There is at most one transfers per segment.
* This works in tandem with transfersBySource so they always need to be kept in sync and updates to both of them
* need to be atomic.
*/
private Map<Integer, InboundTransferTask> transfersBySegment = new HashMap<Integer, InboundTransferTask>();
public StateConsumerImpl() {
}
@Inject
public void init(Cache cache,
LocalTopologyManager localTopologyManager,
InterceptorChain interceptorChain,
InvocationContextContainer icc,
Configuration configuration,
RpcManager rpcManager,
CommandsFactory commandsFactory,
CacheLoaderManager cacheLoaderManager,
DataContainer dataContainer,
TransactionTable transactionTable,
StateTransferLock stateTransferLock) {
this.cacheName = cache.getName();
this.localTopologyManager = localTopologyManager;
this.interceptorChain = interceptorChain;
this.icc = icc;
this.configuration = configuration;
this.rpcManager = rpcManager;
this.commandsFactory = commandsFactory;
this.cacheLoaderManager = cacheLoaderManager;
this.dataContainer = dataContainer;
this.transactionTable = transactionTable;
this.stateTransferLock = stateTransferLock;
// we need to use a special form of PutKeyValueCommand that can apply versions too
useVersionedPut = configuration.transaction().transactionMode().isTransactional() &&
configuration.versioning().enabled() &&
configuration.locking().writeSkewCheck() &&
configuration.transaction().lockingMode() == LockingMode.OPTIMISTIC &&
configuration.clustering().cacheMode().isClustered();
timeout = configuration.clustering().stateTransfer().timeout();
}
public boolean isStateTransferInProgress() {
// TODO This is called quite often, use an extra volatile, a concurrent collection or a RWLock instead
synchronized (this) {
return !transfersBySource.isEmpty();
}
}
@Override
public boolean isStateTransferInProgressForKey(Object key) {
if (configuration.clustering().cacheMode().isInvalidation()) {
return false;
}
// todo [anistor] also return true for keys to be removed (now we report only keys to be added)
synchronized (this) {
return cacheTopology != null && transfersBySegment.containsKey(getSegment(key));
}
}
@Override
public void onTopologyUpdate(CacheTopology cacheTopology, boolean isRebalance) {
if (trace) log.tracef("Received new CH: %s", cacheTopology.getWriteConsistentHash());
activeTopologyUpdates.incrementAndGet();
if (isRebalance) {
rebalanceInProgress.set(true);
}
ConsistentHash previousCh = this.cacheTopology != null ? this.cacheTopology.getWriteConsistentHash() : null;
// Ensures writes to the data container use the right consistent hash
// No need for a try/finally block, since it's just an assignment
stateTransferLock.acquireExclusiveTopologyLock();
this.cacheTopology = cacheTopology;
stateTransferLock.releaseExclusiveTopologyLock();
stateTransferLock.topologyInstalled(cacheTopology.getTopologyId());
try {
Set<Integer> addedSegments;
if (previousCh == null) {
// we start fresh, without any data, so we need to pull everything we own according to writeCh
addedSegments = getOwnedSegments(cacheTopology.getWriteConsistentHash());
} else {
Set<Integer> previousSegments = getOwnedSegments(previousCh);
Set<Integer> newSegments = getOwnedSegments(cacheTopology.getWriteConsistentHash());
// we need to diff the routing tables of the two CHes
Set<Integer> removedSegments = new HashSet<Integer>(previousSegments);
removedSegments.removeAll(newSegments);
// remove inbound transfers and any data for segments we no longer own
if (trace) {
log.tracef("Discarding removed segments: %s; new segments: %s; old segments: %s",
removedSegments, newSegments, previousSegments);
}
discardSegments(removedSegments);
Set<Integer> currentSegments = getOwnedSegments(cacheTopology.getReadConsistentHash());
addedSegments = new HashSet<Integer>(newSegments);
addedSegments.removeAll(currentSegments);
// check if any of the existing transfers should be restarted from a different source because the initial source is no longer a member
Set<Address> members = new HashSet<Address>(cacheTopology.getReadConsistentHash().getMembers());
synchronized (this) {
for (Address source : transfersBySource.keySet()) {
if (!members.contains(source)) {
List<InboundTransferTask> inboundTransfers = transfersBySource.remove(source);
if (inboundTransfers != null) {
for (InboundTransferTask inboundTransfer : inboundTransfers) {
// these segments will be restarted if they are still in new write CH
transfersBySegment.keySet().removeAll(inboundTransfer.getSegments());
}
}
}
}
}
}
if (addedSegments != null && !addedSegments.isEmpty()) {
addTransfers(addedSegments); // add transfers for new or restarted segments
}
} finally {
stateTransferLock.transactionDataReceived(cacheTopology.getTopologyId());
if (activeTopologyUpdates.decrementAndGet() == 0 && !isStateTransferInProgress()) {
notifyEndOfTopologyUpdate(cacheTopology.getTopologyId());
}
}
}
private void notifyEndOfTopologyUpdate(int topologyId) {
if (rebalanceInProgress.compareAndSet(true, false)) {
localTopologyManager.confirmRebalance(cacheName, topologyId, null);
}
}
private Set<Integer> getOwnedSegments(ConsistentHash consistentHash) {
Address address = rpcManager.getAddress();
return consistentHash.getMembers().contains(address) ? consistentHash.getSegmentsForOwner(address)
: Collections.<Integer>emptySet();
}
public void applyState(Address sender, int topologyId, int segmentId, Collection<InternalCacheEntry> cacheEntries, boolean isLastChunk) {
// it's possible to receive a late message so we must be prepared to ignore segments we no longer own
//todo [anistor] this check should be based on topologyId
if (!cacheTopology.getWriteConsistentHash().getSegmentsForOwner(rpcManager.getAddress()).contains(segmentId)) {
if (trace) {
log.warnf("Discarding received cache entries for segment %d because they do not belong to this node.", segmentId);
}
return;
}
if (cacheEntries != null) {
doApplyState(sender, segmentId, cacheEntries);
}
// notify the inbound task that a chunk of cache entries was received
InboundTransferTask inboundTransfer;
synchronized (this) {
inboundTransfer = transfersBySegment.get(segmentId);
}
if (inboundTransfer != null) {
inboundTransfer.onStateReceived(segmentId, isLastChunk);
} else {
log.debugf("Received unsolicited state for segment %d from node %s", segmentId, sender);
return;
}
if (trace) {
log.tracef("After applying the received state the data container has %d keys", dataContainer.size());
synchronized (this) {
log.tracef("Segments not received yet: %s", transfersBySource);
}
}
}
private void doApplyState(Address sender, int segmentId, Collection<InternalCacheEntry> cacheEntries) {
log.debugf("Applying new state for segment %d from %s: received %d cache entries", segmentId, sender, cacheEntries.size());
if (trace) {
List<Object> keys = new ArrayList<Object>(cacheEntries.size());
for (InternalCacheEntry e : cacheEntries) {
keys.add(e.getKey());
}
log.tracef("Received keys: %s", keys);
}
// CACHE_MODE_LOCAL avoids handling by StateTransferInterceptor and any potential locks in StateTransferLock
EnumSet<Flag> flags = EnumSet.of(CACHE_MODE_LOCAL, IGNORE_RETURN_VALUES, SKIP_SHARED_CACHE_STORE, SKIP_OWNERSHIP_CHECK, SKIP_XSITE_BACKUP);
for (InternalCacheEntry e : cacheEntries) {
InvocationContext ctx = icc.createRemoteInvocationContext(sender);
try {
PutKeyValueCommand put = useVersionedPut ?
commandsFactory.buildVersionedPutKeyValueCommand(e.getKey(), e.getValue(), e.getLifespan(), e.getMaxIdle(), e.getVersion(), flags)
: commandsFactory.buildPutKeyValueCommand(e.getKey(), e.getValue(), e.getLifespan(), e.getMaxIdle(), flags);
put.setPutIfAbsent(true); //todo [anistor] this still does not solve removal cases. we need tombstones for deleted keys. we need to keep a separate set of deleted keys an use it during apply state
interceptorChain.invoke(ctx, put);
} catch (Exception ex) {
log.problemApplyingStateForKey(ex.getMessage(), e.getKey());
}
}
}
public void applyTransactions(Address sender, int topologyId, Collection<TransactionInfo> transactions) {
log.debugf("Applying %d transactions transferred from %s", transactions.size(), sender);
if (configuration.transaction().transactionMode().isTransactional()) {
for (TransactionInfo transactionInfo : transactions) {
CacheTransaction tx = transactionTable.getLocalTransaction(transactionInfo.getGlobalTransaction());
if (tx == null) {
tx = transactionTable.getRemoteTransaction(transactionInfo.getGlobalTransaction());
if (tx == null) {
tx = transactionTable.createRemoteTransaction(transactionInfo.getGlobalTransaction(), transactionInfo.getModifications());
((RemoteTransaction) tx).setMissingLookedUpEntries(true);
}
}
for (Object key : transactionInfo.getLockedKeys()) {
tx.addBackupLockForKey(key);
}
}
}
}
// Must run after the CacheLoaderManager
@Start(priority = 20)
public void start() {
fetchEnabled = configuration.clustering().stateTransfer().fetchInMemoryState() || cacheLoaderManager.isFetchPersistentState();
}
@Stop(priority = 20)
@Override
public void stop() {
if (trace) {
log.tracef("Shutting down StateConsumer of cache %s on node %s", cacheName, rpcManager.getAddress());
}
try {
synchronized (this) {
// cancel all inbound transfers
for (Iterator<List<InboundTransferTask>> it = transfersBySource.values().iterator(); it.hasNext(); ) {
List<InboundTransferTask> inboundTransfers = it.next();
it.remove();
for (InboundTransferTask inboundTransfer : inboundTransfers) {
inboundTransfer.cancel();
}
}
transfersBySource.clear();
transfersBySegment.clear();
}
} catch (Throwable t) {
log.errorf(t, "Failed to stop StateConsumer of cache %s on node %s", cacheName, rpcManager.getAddress());
}
}
@Override
public CacheTopology getCacheTopology() {
return cacheTopology;
}
private void addTransfers(Set<Integer> segments) {
log.debugf("Adding state transfer for segments: %s", segments);
Set<Integer> segmentsToProcess = new HashSet<Integer>(segments);
Set<Address> blacklistedSources = new HashSet<Address>();
// ignore all segments for which there are no other owners to pull data from.
// these segments are considered empty (or lost) and do not require a state transfer
for (Iterator<Integer> it = segmentsToProcess.iterator(); it.hasNext(); ) {
Integer segmentId = it.next();
Address source = pickSourceOwner(segmentId, blacklistedSources);
if (source == null) {
it.remove();
}
}
synchronized (this) {
// already active transfers do not need to be added again
segmentsToProcess.removeAll(transfersBySegment.keySet());
}
while (!segmentsToProcess.isEmpty()) {
Map<Address, Set<Integer>> segmentsBySource = new HashMap<Address, Set<Integer>>();
for (int segmentId : segmentsToProcess) {
Address source = pickSourceOwner(segmentId, blacklistedSources);
if (source != null) {
Set<Integer> segmentsFromSource = segmentsBySource.get(source);
if (segmentsFromSource == null) {
segmentsFromSource = new HashSet<Integer>();
segmentsBySource.put(source, segmentsFromSource);
}
segmentsFromSource.add(segmentId);
}
}
Set<Integer> failedSegments = new HashSet<Integer>();
for (Address source : segmentsBySource.keySet()) {
Set<Integer> segmentsFromSource = segmentsBySource.get(source);
InboundTransferTask inboundTransfer = new InboundTransferTask(segmentsFromSource, source, cacheTopology.getTopologyId(), this, rpcManager, commandsFactory, timeout);
synchronized (this) {
for (int segmentId : segmentsFromSource) {
transfersBySegment.put(segmentId, inboundTransfer);
}
List<InboundTransferTask> inboundTransfers = transfersBySource.get(inboundTransfer.getSource());
if (inboundTransfers == null) {
inboundTransfers = new ArrayList<InboundTransferTask>();
transfersBySource.put(inboundTransfer.getSource(), inboundTransfers);
}
inboundTransfers.add(inboundTransfer);
}
// if requesting the transactions fails we need to retry from another source
if (configuration.transaction().transactionMode().isTransactional()) {
if (!inboundTransfer.requestTransactions()) {
log.errorf("Failed to retrieve transactions for segments %s from node %s (node will be blacklisted)", segmentsFromSource, source);
failedSegments.addAll(segmentsFromSource);
blacklistedSources.add(source);
removeTransfer(inboundTransfer); // will be retried from another source
continue;
}
}
// if requesting the segments fails we need to retry from another source
if (fetchEnabled) {
if (!inboundTransfer.requestSegments()) {
log.errorf("Failed to request segments %s from node %s (node will be blacklisted)", segmentsFromSource, source);
failedSegments.addAll(segmentsFromSource);
blacklistedSources.add(source);
removeTransfer(inboundTransfer); // will be retried from another source
}
} else {
removeTransfer(inboundTransfer); // we consider it complete
}
}
segmentsToProcess = failedSegments;
}
}
private Address pickSourceOwner(int segmentId, Set<Address> blacklistedSources) {
List<Address> owners = cacheTopology.getReadConsistentHash().locateOwnersForSegment(segmentId);
if (owners.size() == 1 && owners.get(0).equals(rpcManager.getAddress())) {
return null;
}
for (int i = owners.size() - 1; i >= 0; i--) { // iterate backwards because we prefer to fetch from newer nodes
Address o = owners.get(i);
if (!o.equals(rpcManager.getAddress()) && !blacklistedSources.contains(o)) {
return o;
}
}
log.errorf("No live owners found for segment %d. Current owners are: %s. Blacklisted owners: %s", segmentId, owners, blacklistedSources);
return null;
}
/**
* Remove the segment's data from the data container and cache store because we no longer own it.
*
* @param segments to be cancelled and discarded
*/
private void discardSegments(Set<Integer> segments) {
synchronized (this) {
List<Integer> segmentsToCancel = new ArrayList<Integer>(segments);
while (!segmentsToCancel.isEmpty()) {
int segmentId = segmentsToCancel.remove(0);
log.debugf("Removing state transfer for segment %d", segmentId);
InboundTransferTask inboundTransfer = transfersBySegment.remove(segmentId);
if (inboundTransfer != null) { // we need to check the transfer was not already completed
Set<Integer> cancelledSegments = new HashSet<Integer>(segmentsToCancel);
cancelledSegments.retainAll(inboundTransfer.getSegments());
segmentsToCancel.removeAll(cancelledSegments);
inboundTransfer.cancelSegments(cancelledSegments); //this will also remove it from transfersBySource if the entire task gets cancelled
}
}
}
Set<Object> keysToRemove = new HashSet<Object>();
for (InternalCacheEntry ice : dataContainer) {
Object key = ice.getKey();
if (segments.contains(getSegment(key))) {
keysToRemove.add(key);
}
}
// we also remove keys from the cache store
CacheStore cacheStore = getCacheStore();
if (cacheStore != null) {
//todo [anistor] extend CacheStore interface to be able to specify a filter when loading keys (ie. keys should belong to desired segments)
try {
Set<Object> storedKeys = cacheStore.loadAllKeys(new ReadOnlyDataContainerBackedKeySet(dataContainer));
for (Object key : storedKeys) {
if (segments.contains(getSegment(key))) {
keysToRemove.add(key);
}
}
} catch (CacheLoaderException e) {
log.failedLoadingKeysFromCacheStore(e);
}
}
if (!keysToRemove.isEmpty()) {
try {
InvalidateCommand invalidateCmd = commandsFactory.buildInvalidateFromL1Command(true, EnumSet.of(CACHE_MODE_LOCAL, SKIP_LOCKING), keysToRemove);
InvocationContext ctx = icc.createNonTxInvocationContext();
interceptorChain.invoke(ctx, invalidateCmd);
log.debugf("Invalidated %d keys, data container now has %d keys", keysToRemove.size(), dataContainer.size());
if (trace) log.tracef("Invalidated keys: %s", keysToRemove);
} catch (CacheException e) {
log.failedToInvalidateKeys(e);
}
}
//todo [anistor] call CacheNotifier.notifyDataRehashed
}
private int getSegment(Object key) {
// there we can use any CH version because the routing table is not involved
return cacheTopology.getReadConsistentHash().getSegment(key);
}
/**
* Obtains the CacheStore that will be used for purging segments that are no longer owned by this node.
* The CacheStore will be purged only if it is enabled and it is not shared.
*/
private CacheStore getCacheStore() {
if (cacheLoaderManager != null && cacheLoaderManager.isEnabled() && !cacheLoaderManager.isShared()) {
return cacheLoaderManager.getCacheStore();
}
return null;
}
private void removeTransfer(InboundTransferTask inboundTransfer) {
synchronized (this) {
List<InboundTransferTask> transfers = transfersBySource.get(inboundTransfer.getSource());
if (transfers != null) {
if (transfers.remove(inboundTransfer)) {
if (transfers.isEmpty()) {
transfersBySource.remove(inboundTransfer.getSource());
}
for (int segmentId : inboundTransfer.getSegments()) {
transfersBySegment.remove(segmentId);
}
}
}
}
}
void onTaskCompletion(InboundTransferTask inboundTransfer) {
removeTransfer(inboundTransfer);
if (activeTopologyUpdates.get() == 0 && !isStateTransferInProgress()) {
notifyEndOfTopologyUpdate(cacheTopology.getTopologyId());
}
}
}