use of org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey in project ignite by apache.
the class GridDhtColocatedCache method removeLocks.
/**
* Removes locks regardless of whether they are owned or not for given
* version and keys.
*
* @param threadId Thread ID.
* @param ver Lock version.
* @param keys Keys.
*/
public void removeLocks(long threadId, GridCacheVersion ver, Collection<KeyCacheObject> keys) {
if (keys.isEmpty())
return;
try {
int keyCnt = -1;
Map<ClusterNode, GridNearUnlockRequest> map = null;
Collection<KeyCacheObject> locKeys = new LinkedList<>();
for (KeyCacheObject key : keys) {
IgniteTxKey txKey = ctx.txKey(key);
GridCacheMvccCandidate lock = ctx.mvcc().removeExplicitLock(threadId, txKey, ver);
if (lock != null) {
AffinityTopologyVersion topVer = lock.topologyVersion();
if (map == null) {
Collection<ClusterNode> affNodes = CU.allNodes(ctx, topVer);
keyCnt = (int) Math.ceil((double) keys.size() / affNodes.size());
map = U.newHashMap(affNodes.size());
}
ClusterNode primary = ctx.affinity().primaryByKey(key, topVer);
if (primary == null) {
if (log.isDebugEnabled())
log.debug("Failed to remove locks (all partition nodes left the grid).");
continue;
}
if (!primary.isLocal()) {
// Send request to remove from remote nodes.
GridNearUnlockRequest req = map.get(primary);
if (req == null) {
map.put(primary, req = new GridNearUnlockRequest(ctx.cacheId(), keyCnt, ctx.deploymentEnabled()));
req.version(ver);
}
GridCacheEntryEx entry = peekEx(key);
KeyCacheObject key0 = entry != null ? entry.key() : key;
req.addKey(key0, ctx);
} else
locKeys.add(key);
}
}
if (!locKeys.isEmpty())
removeLocks(ctx.localNodeId(), ver, locKeys, true);
if (map == null || map.isEmpty())
return;
IgnitePair<Collection<GridCacheVersion>> versPair = ctx.tm().versions(ver);
Collection<GridCacheVersion> committed = versPair.get1();
Collection<GridCacheVersion> rolledback = versPair.get2();
for (Map.Entry<ClusterNode, GridNearUnlockRequest> mapping : map.entrySet()) {
ClusterNode n = mapping.getKey();
GridDistributedUnlockRequest req = mapping.getValue();
if (!F.isEmpty(req.keys())) {
req.completedVersions(committed, rolledback);
try {
// We don't wait for reply to this message.
ctx.io().send(n, req, ctx.ioPolicy());
} catch (ClusterTopologyCheckedException e) {
if (log.isDebugEnabled())
log.debug("Failed to send unlock request (node has left the grid) [keys=" + req.keys() + ", n=" + n + ", e=" + e + ']');
} catch (IgniteCheckedException e) {
U.error(log, "Failed to send unlock request [keys=" + req.keys() + ", n=" + n + ']', e);
}
}
}
} catch (IgniteCheckedException ex) {
U.error(log, "Failed to unlock the lock for keys: " + keys, ex);
}
}
use of org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey in project ignite by apache.
the class GridDhtColocatedLockFuture method requestedKeys0.
/**
* @return Keys for which locks requested from remote nodes but response isn't received.
*/
private Set<IgniteTxKey> requestedKeys0() {
for (IgniteInternalFuture<Boolean> miniFut : futures()) {
if (isMini(miniFut) && !miniFut.isDone()) {
MiniFuture mini = (MiniFuture) miniFut;
Set<IgniteTxKey> requestedKeys = U.newHashSet(mini.keys.size());
for (KeyCacheObject key : mini.keys) requestedKeys.add(new IgniteTxKey(key, cctx.cacheId()));
return requestedKeys;
}
}
return null;
}
use of org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey in project ignite by apache.
the class GridDhtColocatedLockFuture method addEntry.
/**
* Adds entry to future.
*
* @param entry Entry to add.
* @return Non-reentry candidate if lock should be acquired on remote node,
* reentry candidate if locks has been already acquired and {@code null} if explicit locks is held and
* implicit transaction accesses locked entry.
* @throws IgniteCheckedException If failed to add entry due to external locking.
*/
@Nullable
private GridCacheMvccCandidate addEntry(GridDistributedCacheEntry entry) throws IgniteCheckedException {
IgniteTxKey txKey = entry.txKey();
GridCacheMvccCandidate cand = cctx.mvcc().explicitLock(threadId, txKey);
if (inTx()) {
if (cand != null) {
if (!tx.implicit())
throw new IgniteCheckedException("Cannot access key within transaction if lock is " + "externally held [key=" + entry.key() + ", entry=" + entry + ']');
else
return null;
} else {
IgniteTxEntry txEntry = tx.entry(txKey);
txEntry.cached(entry);
// Check transaction entries (corresponding tx entries must be enlisted in transaction).
cand = new GridCacheMvccCandidate(entry, cctx.localNodeId(), null, null, threadId, lockVer, true, tx.entry(txKey).locked(), inTx(), inTx() && tx.implicitSingle(), false, false, null, false);
cand.topologyVersion(topVer);
}
} else {
if (cand == null) {
cand = new GridCacheMvccCandidate(entry, cctx.localNodeId(), null, null, threadId, lockVer, true, false, inTx(), inTx() && tx.implicitSingle(), false, false, null, false);
cand.topologyVersion(topVer);
} else
cand = cand.reenter();
cctx.mvcc().addExplicitLock(threadId, cand, topVer);
}
return cand;
}
use of org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey in project ignite by apache.
the class GridDhtTxPrepareRequest method prepareMarshal.
/**
* {@inheritDoc}
*
* @param ctx
*/
@Override
public void prepareMarshal(GridCacheSharedContext ctx) throws IgniteCheckedException {
super.prepareMarshal(ctx);
if (owned != null && ownedKeys == null) {
ownedKeys = owned.keySet();
ownedVals = owned.values();
for (IgniteTxKey key : ownedKeys) {
GridCacheContext cctx = ctx.cacheContext(key.cacheId());
key.prepareMarshal(cctx);
if (addDepInfo)
prepareObject(key, cctx);
}
}
if (nearWrites != null)
marshalTx(nearWrites, ctx);
}
use of org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey in project ignite by apache.
the class GridDhtTxPrepareResponse method prepareMarshal.
/** {@inheritDoc} */
@Override
public void prepareMarshal(GridCacheSharedContext ctx) throws IgniteCheckedException {
super.prepareMarshal(ctx);
if (nearEvicted != null) {
for (IgniteTxKey key : nearEvicted) {
GridCacheContext cctx = ctx.cacheContext(key.cacheId());
key.prepareMarshal(cctx);
}
}
if (preloadEntries != null) {
for (GridCacheEntryInfo info : preloadEntries) {
GridCacheContext cctx = ctx.cacheContext(info.cacheId());
info.marshal(cctx);
}
}
}
Aggregations