use of org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxMapping in project ignite by apache.
the class GridNearOptimisticTxPrepareFuture method prepareSingle.
/**
* @param write Write.
* @param topLocked {@code True} if thread already acquired lock preventing topology change.
* @param remap Remap flag.
*/
private void prepareSingle(IgniteTxEntry write, boolean topLocked, boolean remap) {
write.clearEntryReadVersion();
AffinityTopologyVersion topVer = tx.topologyVersion();
assert topVer.topologyVersion() > 0;
txMapping = new GridDhtTxMapping();
GridDistributedTxMapping mapping = map(write, topVer, null, topLocked, remap);
if (mapping.primary().isLocal()) {
if (write.context().isNear())
tx.nearLocallyMapped(true);
else if (write.context().isColocated())
tx.colocatedLocallyMapped(true);
}
if (isDone()) {
if (log.isDebugEnabled())
log.debug("Abandoning (re)map because future is done: " + this);
return;
}
if (keyLockFut != null)
keyLockFut.onAllKeysAdded();
tx.addSingleEntryMapping(mapping, write);
cctx.mvcc().recheckPendingLocks();
mapping.last(true);
tx.transactionNodes(txMapping.transactionNodes());
if (!write.context().isNear())
checkOnePhase(txMapping);
assert !(mapping.hasColocatedCacheEntries() && mapping.hasNearCacheEntries()) : mapping;
proceedPrepare(mapping, null);
}
use of org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxMapping in project ignite by apache.
the class GridNearPessimisticTxPrepareFuture method preparePessimistic.
/**
*
*/
private void preparePessimistic() {
Map<UUID, GridDistributedTxMapping> mappings = new HashMap<>();
AffinityTopologyVersion topVer = tx.topologyVersion();
GridDhtTxMapping txMapping = new GridDhtTxMapping();
boolean hasNearCache = false;
for (IgniteTxEntry txEntry : tx.allEntries()) {
txEntry.clearEntryReadVersion();
GridCacheContext cacheCtx = txEntry.context();
if (cacheCtx.isNear())
hasNearCache = true;
List<ClusterNode> nodes;
if (!cacheCtx.isLocal()) {
GridDhtPartitionTopology top = cacheCtx.topology();
nodes = top.nodes(cacheCtx.affinity().partition(txEntry.key()), topVer);
} else
nodes = cacheCtx.affinity().nodesByKey(txEntry.key(), topVer);
assert !nodes.isEmpty();
ClusterNode primary = nodes.get(0);
GridDistributedTxMapping nodeMapping = mappings.get(primary.id());
if (nodeMapping == null)
mappings.put(primary.id(), nodeMapping = new GridDistributedTxMapping(primary));
txEntry.nodeId(primary.id());
nodeMapping.add(txEntry);
txMapping.addMapping(nodes);
}
tx.transactionNodes(txMapping.transactionNodes());
if (!hasNearCache)
checkOnePhase(txMapping);
long timeout = tx.remainingTime();
if (timeout == -1) {
onDone(new IgniteTxTimeoutCheckedException("Transaction timed out and was rolled back: " + tx));
return;
}
int miniId = 0;
Map<UUID, Collection<UUID>> txNodes = txMapping.transactionNodes();
for (final GridDistributedTxMapping m : mappings.values()) {
final ClusterNode primary = m.primary();
if (primary.isLocal()) {
if (m.hasNearCacheEntries() && m.hasColocatedCacheEntries()) {
GridNearTxPrepareRequest nearReq = createRequest(txMapping.transactionNodes(), m, timeout, m.nearEntriesReads(), m.nearEntriesWrites());
prepareLocal(nearReq, m, ++miniId, true);
GridNearTxPrepareRequest colocatedReq = createRequest(txNodes, m, timeout, m.colocatedEntriesReads(), m.colocatedEntriesWrites());
prepareLocal(colocatedReq, m, ++miniId, false);
} else {
GridNearTxPrepareRequest req = createRequest(txNodes, m, timeout, m.reads(), m.writes());
prepareLocal(req, m, ++miniId, m.hasNearCacheEntries());
}
} else {
GridNearTxPrepareRequest req = createRequest(txNodes, m, timeout, m.reads(), m.writes());
final MiniFuture fut = new MiniFuture(m, ++miniId);
req.miniId(fut.futureId());
add(fut);
try {
cctx.io().send(primary, req, tx.ioPolicy());
if (msgLog.isDebugEnabled()) {
msgLog.debug("Near pessimistic prepare, sent request [txId=" + tx.nearXidVersion() + ", node=" + primary.id() + ']');
}
} catch (ClusterTopologyCheckedException e) {
e.retryReadyFuture(cctx.nextAffinityReadyFuture(topVer));
fut.onNodeLeft(e);
} catch (IgniteCheckedException e) {
if (msgLog.isDebugEnabled()) {
msgLog.debug("Near pessimistic prepare, failed send request [txId=" + tx.nearXidVersion() + ", node=" + primary.id() + ", err=" + e + ']');
}
fut.onError(e);
break;
}
}
}
markInitialized();
}
use of org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxMapping in project ignite by apache.
the class GridNearOptimisticSerializableTxPrepareFuture method prepare.
/**
* @param reads Read entries.
* @param writes Write entries.
* @param remap Remap flag.
* @param topLocked Topology locked flag.
*/
@SuppressWarnings("unchecked")
private void prepare(Iterable<IgniteTxEntry> reads, Iterable<IgniteTxEntry> writes, boolean remap, boolean topLocked) {
AffinityTopologyVersion topVer = tx.topologyVersion();
assert topVer.topologyVersion() > 0;
GridDhtTxMapping txMapping = new GridDhtTxMapping();
Map<UUID, GridDistributedTxMapping> mappings = new HashMap<>();
boolean hasNearCache = false;
for (IgniteTxEntry write : writes) {
map(write, topVer, mappings, txMapping, remap, topLocked);
if (write.context().isNear())
hasNearCache = true;
}
for (IgniteTxEntry read : reads) map(read, topVer, mappings, txMapping, remap, topLocked);
if (keyLockFut != null)
keyLockFut.onAllKeysAdded();
if (isDone()) {
if (log.isDebugEnabled())
log.debug("Abandoning (re)map because future is done: " + this);
return;
}
tx.addEntryMapping(mappings.values());
cctx.mvcc().recheckPendingLocks();
tx.transactionNodes(txMapping.transactionNodes());
if (!hasNearCache)
checkOnePhase(txMapping);
MiniFuture locNearEntriesFut = null;
// Create futures in advance to have all futures when process {@link GridNearTxPrepareResponse#clientRemapVersion}.
for (GridDistributedTxMapping m : mappings.values()) {
assert !m.empty();
MiniFuture fut = new MiniFuture(this, m, ++miniId);
add(fut);
if (m.primary().isLocal() && m.hasNearCacheEntries() && m.hasColocatedCacheEntries()) {
assert locNearEntriesFut == null;
locNearEntriesFut = fut;
add(new MiniFuture(this, m, ++miniId));
}
}
Collection<IgniteInternalFuture<?>> futs = (Collection) futures();
Iterator<IgniteInternalFuture<?>> it = futs.iterator();
while (it.hasNext()) {
IgniteInternalFuture<?> fut0 = it.next();
if (skipFuture(remap, fut0))
continue;
MiniFuture fut = (MiniFuture) fut0;
IgniteCheckedException err = prepare(fut, txMapping.transactionNodes(), locNearEntriesFut);
if (err != null) {
while (it.hasNext()) {
fut0 = it.next();
if (skipFuture(remap, fut0))
continue;
fut = (MiniFuture) fut0;
tx.removeMapping(fut.mapping().primary().id());
fut.onResult(new IgniteCheckedException("Failed to prepare transaction.", err));
}
break;
}
}
markInitialized();
}
use of org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxMapping in project ignite by apache.
the class GridNearOptimisticTxPrepareFuture method prepare.
/**
* @param writes Write entries.
* @param topLocked {@code True} if thread already acquired lock preventing topology change.
* @param remap Remap flag.
*/
private void prepare(Iterable<IgniteTxEntry> writes, boolean topLocked, boolean remap) {
AffinityTopologyVersion topVer = tx.topologyVersion();
assert topVer.topologyVersion() > 0;
txMapping = new GridDhtTxMapping();
Map<Object, GridDistributedTxMapping> map = new HashMap<>();
// Assign keys to primary nodes.
GridDistributedTxMapping cur = null;
Queue<GridDistributedTxMapping> mappings = new ArrayDeque<>();
boolean hasNearCache = false;
for (IgniteTxEntry write : writes) {
write.clearEntryReadVersion();
GridDistributedTxMapping updated = map(write, topVer, cur, topLocked, remap);
if (write.context().isNear())
hasNearCache = true;
if (cur != updated) {
mappings.offer(updated);
updated.last(true);
ClusterNode primary = updated.primary();
assert !primary.isLocal() || !cctx.kernalContext().clientNode();
// Minor optimization to not create MappingKey: on client node can not have mapping for local node.
Object key = cctx.kernalContext().clientNode() ? primary.id() : new MappingKey(primary.id(), primary.isLocal() && updated.hasNearCacheEntries());
GridDistributedTxMapping prev = map.put(key, updated);
if (prev != null)
prev.last(false);
if (updated.primary().isLocal()) {
if (write.context().isNear())
tx.nearLocallyMapped(true);
else if (write.context().isColocated())
tx.colocatedLocallyMapped(true);
}
cur = updated;
}
}
if (isDone()) {
if (log.isDebugEnabled())
log.debug("Abandoning (re)map because future is done: " + this);
return;
}
if (keyLockFut != null)
keyLockFut.onAllKeysAdded();
tx.addEntryMapping(mappings);
cctx.mvcc().recheckPendingLocks();
tx.transactionNodes(txMapping.transactionNodes());
if (!hasNearCache)
checkOnePhase(txMapping);
proceedPrepare(mappings);
}
Aggregations