use of org.apache.ignite.internal.processors.cache.CacheObject in project ignite by apache.
the class GridNearAtomicSingleUpdateFuture method mapSingleUpdate.
/**
* @param topVer Topology version.
* @param futId Future ID.
* @return Request.
* @throws Exception If failed.
*/
private PrimaryRequestState mapSingleUpdate(AffinityTopologyVersion topVer, long futId) throws Exception {
if (key == null)
throw new NullPointerException("Null key.");
Object val = this.val;
if (val == null && op != GridCacheOperation.DELETE)
throw new NullPointerException("Null value.");
KeyCacheObject cacheKey = cctx.toCacheKeyObject(key);
if (op != TRANSFORM) {
val = cctx.toCacheObject(val);
if (op == CREATE || op == UPDATE)
cctx.validateKeyAndValue(cacheKey, (CacheObject) val);
} else
val = EntryProcessorResourceInjectorProxy.wrap(cctx.kernalContext(), (EntryProcessor) val);
boolean mappingKnown = cctx.topology().rebalanceFinished(topVer);
List<ClusterNode> nodes = cctx.affinity().nodesByKey(cacheKey, topVer);
if (F.isEmpty(nodes))
throw new ClusterTopologyServerNotFoundException("Failed to map keys for cache (all partition nodes " + "left the grid).");
ClusterNode primary = nodes.get(0);
boolean needPrimaryRes = !mappingKnown || primary.isLocal() || nodes.size() == 1 || nearEnabled;
GridNearAtomicAbstractUpdateRequest req;
byte flags = GridNearAtomicAbstractUpdateRequest.flags(nearEnabled, topLocked, retval, mappingKnown, needPrimaryRes, skipStore, keepBinary, recovery);
if (canUseSingleRequest()) {
if (op == TRANSFORM) {
req = new GridNearAtomicSingleUpdateInvokeRequest(cctx.cacheId(), primary.id(), futId, topVer, syncMode, op, invokeArgs, subjId, taskNameHash, flags, cctx.deploymentEnabled());
} else {
if (filter == null || filter.length == 0) {
req = new GridNearAtomicSingleUpdateRequest(cctx.cacheId(), primary.id(), futId, topVer, syncMode, op, subjId, taskNameHash, flags, cctx.deploymentEnabled());
} else {
req = new GridNearAtomicSingleUpdateFilterRequest(cctx.cacheId(), primary.id(), futId, topVer, syncMode, op, filter, subjId, taskNameHash, flags, cctx.deploymentEnabled());
}
}
} else {
req = new GridNearAtomicFullUpdateRequest(cctx.cacheId(), primary.id(), futId, topVer, syncMode, op, expiryPlc, invokeArgs, filter, subjId, taskNameHash, flags, cctx.deploymentEnabled(), 1);
}
req.addUpdateEntry(cacheKey, val, CU.TTL_NOT_CHANGED, CU.EXPIRE_TIME_CALCULATE, null);
return new PrimaryRequestState(req, nodes, true);
}
use of org.apache.ignite.internal.processors.cache.CacheObject in project ignite by apache.
the class GridNearTxLocal method enlistWriteEntry.
/**
* @param cacheCtx Cache context.
* @param cacheKey Key.
* @param val Value.
* @param entryProcessor Entry processor.
* @param invokeArgs Optional arguments for EntryProcessor.
* @param expiryPlc Explicitly specified expiry policy for entry.
* @param retval Return value flag.
* @param lockOnly Lock only flag.
* @param filter Filter.
* @param drVer DR version.
* @param drTtl DR ttl.
* @param drExpireTime DR expire time.
* @param ret Return value.
* @param enlisted Enlisted keys collection.
* @param skipStore Skip store flag.
* @param singleRmv {@code True} for single remove operation.
* @param hasFilters {@code True} if filters not empty.
* @param needVal {@code True} if value is needed.
* @param needReadVer {@code True} if need read entry version.
* @return {@code True} if entry value should be loaded.
* @throws IgniteCheckedException If failed.
*/
private boolean enlistWriteEntry(GridCacheContext cacheCtx, @Nullable AffinityTopologyVersion entryTopVer, final KeyCacheObject cacheKey, @Nullable final Object val, @Nullable final EntryProcessor<?, ?, ?> entryProcessor, @Nullable final Object[] invokeArgs, @Nullable final ExpiryPolicy expiryPlc, final boolean retval, final boolean lockOnly, final CacheEntryPredicate[] filter, final GridCacheVersion drVer, final long drTtl, long drExpireTime, final GridCacheReturn ret, @Nullable final Collection<KeyCacheObject> enlisted, boolean skipStore, boolean singleRmv, boolean hasFilters, final boolean needVal, boolean needReadVer, boolean keepBinary, boolean recovery) throws IgniteCheckedException {
boolean loadMissed = false;
final boolean rmv = val == null && entryProcessor == null;
IgniteTxKey txKey = cacheCtx.txKey(cacheKey);
IgniteTxEntry txEntry = entry(txKey);
// First time access.
if (txEntry == null) {
while (true) {
GridCacheEntryEx entry = entryEx(cacheCtx, txKey, entryTopVer != null ? entryTopVer : topologyVersion());
try {
entry.unswap(false);
// Check if lock is being explicitly acquired by the same thread.
if (!implicit && cctx.kernalContext().config().isCacheSanityCheckEnabled() && entry.lockedByThread(threadId, xidVer)) {
throw new IgniteCheckedException("Cannot access key within transaction if lock is " + "externally held [key=" + CU.value(cacheKey, cacheCtx, false) + ", entry=" + entry + ", xidVer=" + xidVer + ", threadId=" + threadId + ", locNodeId=" + cctx.localNodeId() + ']');
}
CacheObject old = null;
GridCacheVersion readVer = null;
if (optimistic() && !implicit()) {
try {
if (needReadVer) {
EntryGetResult res = primaryLocal(entry) ? entry.innerGetVersioned(null, this, /*metrics*/
retval, /*events*/
retval, CU.subjectId(this, cctx), entryProcessor, resolveTaskName(), null, keepBinary, null) : null;
if (res != null) {
old = res.value();
readVer = res.version();
}
} else {
old = entry.innerGet(null, this, /*read through*/
false, /*metrics*/
retval, /*events*/
retval, CU.subjectId(this, cctx), entryProcessor, resolveTaskName(), null, keepBinary);
}
} catch (ClusterTopologyCheckedException e) {
entry.context().evicts().touch(entry, topologyVersion());
throw e;
}
} else
old = entry.rawGet();
final GridCacheOperation op = lockOnly ? NOOP : rmv ? DELETE : entryProcessor != null ? TRANSFORM : old != null ? UPDATE : CREATE;
if (old != null && hasFilters && !filter(entry.context(), cacheKey, old, filter)) {
ret.set(cacheCtx, old, false, keepBinary);
if (!readCommitted()) {
if (optimistic() && serializable()) {
txEntry = addEntry(op, old, entryProcessor, invokeArgs, entry, expiryPlc, filter, true, drTtl, drExpireTime, drVer, skipStore, keepBinary, CU.isNearEnabled(cacheCtx));
} else {
txEntry = addEntry(READ, old, null, null, entry, null, CU.empty0(), false, -1L, -1L, null, skipStore, keepBinary, CU.isNearEnabled(cacheCtx));
}
txEntry.markValid();
if (needReadVer) {
assert readVer != null;
txEntry.entryReadVersion(singleRmv ? SER_READ_NOT_EMPTY_VER : readVer);
}
}
if (readCommitted())
cacheCtx.evicts().touch(entry, topologyVersion());
// While.
break;
}
CacheObject cVal = cacheCtx.toCacheObject(val);
if (op == CREATE || op == UPDATE)
cacheCtx.validateKeyAndValue(cacheKey, cVal);
txEntry = addEntry(op, cVal, entryProcessor, invokeArgs, entry, expiryPlc, filter, true, drTtl, drExpireTime, drVer, skipStore, keepBinary, CU.isNearEnabled(cacheCtx));
if (enlisted != null)
enlisted.add(cacheKey);
if (!pessimistic() && !implicit()) {
txEntry.markValid();
if (old == null) {
if (needVal)
loadMissed = true;
else {
assert !implicit() || !transform : this;
assert txEntry.op() != TRANSFORM : txEntry;
if (retval)
ret.set(cacheCtx, null, true, keepBinary);
else
ret.success(true);
}
} else {
if (needReadVer) {
assert readVer != null;
txEntry.entryReadVersion(singleRmv ? SER_READ_NOT_EMPTY_VER : readVer);
}
if (retval && !transform)
ret.set(cacheCtx, old, true, keepBinary);
else {
if (txEntry.op() == TRANSFORM) {
GridCacheVersion ver;
try {
ver = entry.version();
} catch (GridCacheEntryRemovedException ex) {
assert optimistic() : txEntry;
if (log.isDebugEnabled())
log.debug("Failed to get entry version " + "[err=" + ex.getMessage() + ']');
ver = null;
}
addInvokeResult(txEntry, old, ret, ver);
} else
ret.success(true);
}
}
} else // Pessimistic.
{
if (retval && !transform)
ret.set(cacheCtx, old, true, keepBinary);
else
ret.success(true);
}
// While.
break;
} catch (GridCacheEntryRemovedException ignore) {
if (log.isDebugEnabled())
log.debug("Got removed entry in transaction putAll0 method: " + entry);
}
}
} else {
if (entryProcessor == null && txEntry.op() == TRANSFORM)
throw new IgniteCheckedException("Failed to enlist write value for key (cannot have update value in " + "transaction after EntryProcessor is applied): " + CU.value(cacheKey, cacheCtx, false));
GridCacheEntryEx entry = txEntry.cached();
CacheObject v = txEntry.value();
boolean del = txEntry.op() == DELETE && rmv;
if (!del) {
if (hasFilters && !filter(entry.context(), cacheKey, v, filter)) {
ret.set(cacheCtx, v, false, keepBinary);
return loadMissed;
}
GridCacheOperation op = rmv ? DELETE : entryProcessor != null ? TRANSFORM : v != null ? UPDATE : CREATE;
CacheObject cVal = cacheCtx.toCacheObject(val);
if (op == CREATE || op == UPDATE)
cacheCtx.validateKeyAndValue(cacheKey, cVal);
txEntry = addEntry(op, cVal, entryProcessor, invokeArgs, entry, expiryPlc, filter, true, drTtl, drExpireTime, drVer, skipStore, keepBinary, CU.isNearEnabled(cacheCtx));
if (enlisted != null)
enlisted.add(cacheKey);
if (txEntry.op() == TRANSFORM) {
GridCacheVersion ver;
try {
ver = entry.version();
} catch (GridCacheEntryRemovedException e) {
assert optimistic() : txEntry;
if (log.isDebugEnabled())
log.debug("Failed to get entry version: [msg=" + e.getMessage() + ']');
ver = null;
}
addInvokeResult(txEntry, txEntry.value(), ret, ver);
}
}
if (!pessimistic()) {
txEntry.markValid();
if (retval && !transform)
ret.set(cacheCtx, v, true, keepBinary);
else
ret.success(true);
}
}
return loadMissed;
}
use of org.apache.ignite.internal.processors.cache.CacheObject in project ignite by apache.
the class GridNearTxRemote method addEntry.
/**
* @param entry Entry to enlist.
* @throws IgniteCheckedException If failed.
* @return {@code True} if entry was enlisted.
*/
private boolean addEntry(IgniteTxEntry entry) throws IgniteCheckedException {
checkInternal(entry.txKey());
GridCacheContext cacheCtx = entry.context();
if (!cacheCtx.isNear())
cacheCtx = cacheCtx.dht().near().context();
GridNearCacheEntry cached = cacheCtx.near().peekExx(entry.key());
if (cached == null) {
evicted.add(entry.txKey());
return false;
} else {
try {
cached.unswap();
CacheObject val = cached.peek(null);
if (val == null && cached.evictInternal(xidVer, null, false)) {
evicted.add(entry.txKey());
return false;
} else {
// Initialize cache entry.
entry.cached(cached);
txState.addWriteEntry(entry.txKey(), entry);
addExplicit(entry);
return true;
}
} catch (GridCacheEntryRemovedException ignore) {
evicted.add(entry.txKey());
if (log.isDebugEnabled())
log.debug("Got removed entry when adding to remote transaction (will ignore): " + cached);
return false;
}
}
}
use of org.apache.ignite.internal.processors.cache.CacheObject in project ignite by apache.
the class GridLocalCacheEntry method removeLock.
/**
* {@inheritDoc}
*/
@Override
public boolean removeLock(GridCacheVersion ver) throws GridCacheEntryRemovedException {
CacheObject val;
CacheLockCandidates prev = null;
CacheLockCandidates owner = null;
GridCacheMvccCandidate doomed;
lockEntry();
try {
GridCacheVersion obsoleteVer = obsoleteVersionExtras();
if (obsoleteVer != null && !obsoleteVer.equals(ver))
checkObsolete();
GridCacheMvcc mvcc = mvccExtras();
doomed = mvcc == null ? null : mvcc.candidate(ver);
if (doomed != null) {
prev = mvcc.allOwners();
mvcc.remove(ver);
if (mvcc.isEmpty())
mvccExtras(null);
else
owner = mvcc.allOwners();
}
val = this.val;
} finally {
unlockEntry();
}
if (doomed != null)
checkThreadChain(doomed);
checkOwnerChanged(prev, owner, val);
return doomed != null;
}
use of org.apache.ignite.internal.processors.cache.CacheObject in project ignite by apache.
the class GridLocalCacheEntry method addLocal.
/**
* Add local candidate.
*
* @param threadId Owning thread ID.
* @param ver Lock version.
* @param serOrder Version for serializable transactions ordering.
* @param serReadVer Optional read entry version for optimistic serializable transaction.
* @param timeout Timeout to acquire lock.
* @param reenter Reentry flag.
* @param tx Transaction flag.
* @param implicitSingle Implicit transaction flag.
* @param read Read lock flag.
* @return New candidate.
* @throws GridCacheEntryRemovedException If entry has been removed.
*/
@Nullable
GridCacheMvccCandidate addLocal(long threadId, GridCacheVersion ver, @Nullable GridCacheVersion serOrder, @Nullable GridCacheVersion serReadVer, long timeout, boolean reenter, boolean tx, boolean implicitSingle, boolean read) throws GridCacheEntryRemovedException {
assert serReadVer == null || serOrder != null;
CacheObject val;
GridCacheMvccCandidate cand;
CacheLockCandidates prev;
CacheLockCandidates owner = null;
lockEntry();
try {
checkObsolete();
if (serReadVer != null) {
if (!checkSerializableReadVersion(serReadVer))
return null;
}
GridCacheMvcc mvcc = mvccExtras();
if (mvcc == null) {
mvcc = new GridCacheMvcc(cctx);
mvccExtras(mvcc);
}
prev = mvcc.localOwners();
cand = mvcc.addLocal(this, /*nearNodeId*/
null, /*nearVer*/
null, threadId, ver, timeout, serOrder, reenter, tx, implicitSingle, /*dht-local*/
false, read);
if (mvcc.isEmpty())
mvccExtras(null);
else
owner = mvcc.localOwners();
val = this.val;
} finally {
unlockEntry();
}
if (cand != null && !cand.reentry())
cctx.mvcc().addNext(cctx, cand);
checkOwnerChanged(prev, owner, val);
return cand;
}
Aggregations