org.infinispan.statetransfer.StateProviderImpl Maven / Gradle / Ivy
package org.infinispan.statetransfer;
import static org.infinispan.factories.KnownComponentNames.ASYNC_TRANSPORT_EXECUTOR;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.infinispan.Cache;
import org.infinispan.commands.CommandsFactory;
import org.infinispan.commands.write.WriteCommand;
import org.infinispan.configuration.cache.Configuration;
import org.infinispan.container.DataContainer;
import org.infinispan.container.InternalEntryFactory;
import org.infinispan.distexec.DistributedCallable;
import org.infinispan.distribution.ch.ConsistentHash;
import org.infinispan.distribution.ch.KeyPartitioner;
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.notifications.Listener;
import org.infinispan.notifications.cachelistener.cluster.ClusterCacheNotifier;
import org.infinispan.persistence.manager.PersistenceManager;
import org.infinispan.remoting.rpc.RpcManager;
import org.infinispan.remoting.transport.Address;
import org.infinispan.topology.CacheTopology;
import org.infinispan.transaction.impl.LocalTransaction;
import org.infinispan.transaction.impl.TransactionTable;
import org.infinispan.transaction.xa.CacheTransaction;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;
/**
* {@link StateProvider} implementation.
*
* @author [email protected]
* @since 5.2
*/
@Listener
public class StateProviderImpl implements StateProvider {
private static final Log log = LogFactory.getLog(StateProviderImpl.class);
private static final boolean trace = log.isTraceEnabled();
private String cacheName;
private Configuration configuration;
private RpcManager rpcManager;
private CommandsFactory commandsFactory;
private ClusterCacheNotifier clusterCacheNotifier;
private TransactionTable transactionTable; // optional
private DataContainer dataContainer;
private PersistenceManager persistenceManager; // optional
private ExecutorService executorService;
private StateTransferLock stateTransferLock;
private InternalEntryFactory entryFactory;
private long timeout;
private int chunkSize;
private KeyPartitioner keyPartitioner;
private StateConsumer stateConsumer;
/**
* A map that keeps track of current outbound state transfers by destination address. There could be multiple transfers
* flowing to the same destination (but for different segments) so the values are lists.
*/
private final Map> transfersByDestination = new HashMap<>();
public StateProviderImpl() {
}
@Inject
public void init(Cache cache,
@ComponentName(ASYNC_TRANSPORT_EXECUTOR) ExecutorService executorService, //TODO Use a dedicated ExecutorService
Configuration configuration,
RpcManager rpcManager,
CommandsFactory commandsFactory,
ClusterCacheNotifier clusterCacheNotifier,
PersistenceManager persistenceManager,
DataContainer dataContainer,
TransactionTable transactionTable,
StateTransferLock stateTransferLock,
StateConsumer stateConsumer, InternalEntryFactory entryFactory,
KeyPartitioner keyPartitioner) {
this.cacheName = cache.getName();
this.executorService = executorService;
this.configuration = configuration;
this.rpcManager = rpcManager;
this.commandsFactory = commandsFactory;
this.clusterCacheNotifier = clusterCacheNotifier;
this.persistenceManager = persistenceManager;
this.dataContainer = dataContainer;
this.transactionTable = transactionTable;
this.stateTransferLock = stateTransferLock;
this.stateConsumer = stateConsumer;
this.entryFactory = entryFactory;
timeout = configuration.clustering().stateTransfer().timeout();
this.chunkSize = configuration.clustering().stateTransfer().chunkSize();
this.keyPartitioner = keyPartitioner;
}
public boolean isStateTransferInProgress() {
synchronized (transfersByDestination) {
return !transfersByDestination.isEmpty();
}
}
public void onTopologyUpdate(CacheTopology cacheTopology, boolean isRebalance) {
// Cancel outbound state transfers for destinations that are no longer members in new topology
// If the rebalance was cancelled, stop every outbound transfer. This will prevent "leaking" transfers
// from one rebalance to the next.
Set members = new HashSet<>(cacheTopology.getWriteConsistentHash().getMembers());
synchronized (transfersByDestination) {
for (Iterator it = transfersByDestination.keySet().iterator(); it.hasNext(); ) {
Address destination = it.next();
if (!members.contains(destination)) {
List transfers = transfersByDestination.get(destination);
it.remove();
for (OutboundTransferTask outboundTransfer : transfers) {
outboundTransfer.cancel();
}
}
}
}
//todo [anistor] must cancel transfers for all segments that we no longer own
}
@Start(priority = 60)
@Override
public void start() {
}
@Stop(priority = 0)
@Override
public void stop() {
if (trace) {
log.tracef("Shutting down StateProvider of cache %s on node %s", cacheName, rpcManager.getAddress());
}
// cancel all outbound transfers
try {
synchronized (transfersByDestination) {
for (Iterator> it = transfersByDestination.values().iterator(); it.hasNext(); ) {
List transfers = it.next();
it.remove();
for (OutboundTransferTask outboundTransfer : transfers) {
outboundTransfer.cancel();
}
}
}
} catch (Throwable t) {
log.errorf(t, "Failed to stop StateProvider of cache %s on node %s", cacheName, rpcManager.getAddress());
}
}
public List getTransactionsForSegments(Address destination, int requestTopologyId, Set segments) throws InterruptedException {
if (trace) {
log.tracef("Received request for transactions from node %s for cache %s, topology id %d, segments %s",
destination, cacheName, requestTopologyId, segments);
}
final CacheTopology cacheTopology = getCacheTopology(requestTopologyId, destination, true);
final ConsistentHash readCh = cacheTopology.getReadConsistentHash();
Set ownedSegments = readCh.getSegmentsForOwner(rpcManager.getAddress());
if (!ownedSegments.containsAll(segments)) {
segments.removeAll(ownedSegments);
throw new IllegalArgumentException("Segments " + segments + " are not owned by " + rpcManager.getAddress());
}
List transactions = new ArrayList<>();
//we migrate locks only if the cache is transactional and distributed
if (configuration.transaction().transactionMode().isTransactional()) {
collectTransactionsToTransfer(destination, transactions, transactionTable.getRemoteTransactions(), segments, cacheTopology);
collectTransactionsToTransfer(destination, transactions, transactionTable.getLocalTransactions(), segments, cacheTopology);
if (trace) {
log.tracef("Found %d transaction(s) to transfer", transactions.size());
}
}
return transactions;
}
@Override
public Collection getClusterListenersToInstall() {
return clusterCacheNotifier.retrieveClusterListenerCallablesToInstall();
}
private CacheTopology getCacheTopology(int requestTopologyId, Address destination, boolean isReqForTransactions) throws InterruptedException {
CacheTopology cacheTopology = stateConsumer.getCacheTopology();
int currentTopologyId = cacheTopology != null ? cacheTopology.getTopologyId() : -1;
if (requestTopologyId < currentTopologyId) {
if (isReqForTransactions)
log.debugf("Transactions were requested by node %s with topology %d, older than the local topology (%d)",
destination, requestTopologyId, currentTopologyId);
else
log.debugf("Segments were requested by node %s with topology %d, older than the local topology (%d)",
destination, requestTopologyId, currentTopologyId);
} else if (requestTopologyId > currentTopologyId) {
if (trace) {
log.tracef("%s were requested by node %s with topology %d, greater than the local " +
"topology (%d). Waiting for topology %d to be installed locally.", isReqForTransactions ? "Transactions" : "Segments", destination,
requestTopologyId, currentTopologyId, requestTopologyId);
}
try {
stateTransferLock.waitForTopology(requestTopologyId, timeout, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
throw log.failedWaitingForTopology(requestTopologyId);
}
cacheTopology = stateConsumer.getCacheTopology();
}
return cacheTopology;
}
private void collectTransactionsToTransfer(Address destination,
List transactionsToTransfer,
Collection extends CacheTransaction> transactions,
Set segments, CacheTopology cacheTopology) {
int topologyId = cacheTopology.getTopologyId();
List members = cacheTopology.getMembers();
// no need to filter out state transfer generated transactions because there should not be any such transactions running for any of the requested segments
for (CacheTransaction tx : transactions) {
// Skip transactions whose originators left. The topology id check is needed for joiners.
// Also skip transactions that originates after state transfer starts.
if (tx.getTopologyId() == topologyId || !members.contains(tx.getGlobalTransaction().getAddress())) {
if (trace) log.tracef("Skipping transaction %s as it was started in the current topology or by a leaver", tx);
continue;
}
// transfer only locked keys that belong to requested segments
Set
© 2015 - 2025 Weber Informatics LLC | Privacy Policy