use of org.apache.ignite.internal.processors.cache.GridCacheContext in project ignite by apache.
the class DmlStatementsProcessor method doFastUpdate.
/**
* Perform single cache operation based on given args.
* @param args Query parameters.
* @return 1 if an item was affected, 0 otherwise.
* @throws IgniteCheckedException if failed.
*/
@SuppressWarnings({ "unchecked", "ConstantConditions" })
private static UpdateResult doFastUpdate(UpdatePlan plan, Object[] args) throws IgniteCheckedException {
GridCacheContext cctx = plan.tbl.rowDescriptor().context();
FastUpdateArguments singleUpdate = plan.fastUpdateArgs;
assert singleUpdate != null;
boolean valBounded = (singleUpdate.val != FastUpdateArguments.NULL_ARGUMENT);
if (singleUpdate.newVal != FastUpdateArguments.NULL_ARGUMENT) {
// Single item UPDATE
Object key = singleUpdate.key.apply(args);
Object newVal = singleUpdate.newVal.apply(args);
if (valBounded) {
Object val = singleUpdate.val.apply(args);
return (cctx.cache().replace(key, val, newVal) ? UpdateResult.ONE : UpdateResult.ZERO);
} else
return (cctx.cache().replace(key, newVal) ? UpdateResult.ONE : UpdateResult.ZERO);
} else {
// Single item DELETE
Object key = singleUpdate.key.apply(args);
Object val = singleUpdate.val.apply(args);
if (// No _val bound in source query
singleUpdate.val == FastUpdateArguments.NULL_ARGUMENT)
return cctx.cache().remove(key) ? UpdateResult.ONE : UpdateResult.ZERO;
else
return cctx.cache().remove(key, val) ? UpdateResult.ONE : UpdateResult.ZERO;
}
}
use of org.apache.ignite.internal.processors.cache.GridCacheContext in project ignite by apache.
the class IgniteTxAdapter method isNearLocallyMapped.
/**
* @param e Transaction entry.
* @param primaryOnly Flag to include backups into check or not.
* @return {@code True} if entry is locally mapped as a primary or back up node.
*/
protected boolean isNearLocallyMapped(IgniteTxEntry e, boolean primaryOnly) {
GridCacheContext cacheCtx = e.context();
if (!cacheCtx.isNear())
return false;
// Try to take either entry-recorded primary node ID,
// or transaction node ID from near-local transactions.
UUID nodeId = e.nodeId() == null ? local() ? this.nodeId : null : e.nodeId();
if (nodeId != null && nodeId.equals(cctx.localNodeId()))
return true;
GridCacheEntryEx cached = e.cached();
int part = cached != null ? cached.partition() : cacheCtx.affinity().partition(e.key());
List<ClusterNode> affNodes = cacheCtx.affinity().nodesByPartition(part, topologyVersion());
e.locallyMapped(F.contains(affNodes, cctx.localNode()));
if (primaryOnly) {
ClusterNode primary = F.first(affNodes);
if (primary == null && !cacheCtx.affinityNode())
return false;
assert primary != null : "Primary node is null for affinity nodes: " + affNodes;
return primary.isLocal();
} else
return e.locallyMapped();
}
use of org.apache.ignite.internal.processors.cache.GridCacheContext in project ignite by apache.
the class IgniteTxAdapter method conflictResolve.
/**
* Resolve DR conflict.
*
* @param op Initially proposed operation.
* @param txEntry TX entry being updated.
* @param newVal New value.
* @param newVer New version.
* @param old Old entry.
* @return Tuple with adjusted operation type and conflict context.
* @throws IgniteCheckedException In case of eny exception.
* @throws GridCacheEntryRemovedException If entry got removed.
*/
@SuppressWarnings({ "unchecked", "ConstantConditions" })
protected IgniteBiTuple<GridCacheOperation, GridCacheVersionConflictContext> conflictResolve(GridCacheOperation op, IgniteTxEntry txEntry, CacheObject newVal, GridCacheVersion newVer, GridCacheEntryEx old) throws IgniteCheckedException, GridCacheEntryRemovedException {
assert newVer != null;
// 1. Calculate TTL and expire time.
long newTtl = txEntry.ttl();
long newExpireTime = txEntry.conflictExpireTime();
// 1.1. If TTL is not changed, then calculate it based on expiry.
if (newTtl == CU.TTL_NOT_CHANGED) {
ExpiryPolicy expiry = txEntry.context().expiryForTxEntry(txEntry);
if (expiry != null) {
if (op == CREATE)
newTtl = CU.toTtl(expiry.getExpiryForCreation());
else if (op == UPDATE)
newTtl = CU.toTtl(expiry.getExpiryForUpdate());
}
}
// 1.2. If TTL is set to zero, then mark operation as "DELETE".
if (newTtl == CU.TTL_ZERO) {
op = DELETE;
newTtl = CU.TTL_ETERNAL;
}
// 1.3. If TTL is still not changed, then either use old entry TTL or set it to "ETERNAL".
if (newTtl == CU.TTL_NOT_CHANGED) {
if (old.isNewLocked())
newTtl = CU.TTL_ETERNAL;
else {
newTtl = old.rawTtl();
newExpireTime = old.rawExpireTime();
}
}
// TTL must be resolved at this point.
assert newTtl != CU.TTL_ZERO && newTtl != CU.TTL_NOT_CHANGED;
// 1.4 If expire time was not set explicitly, then calculate it.
if (newExpireTime == CU.EXPIRE_TIME_CALCULATE)
newExpireTime = CU.toExpireTime(newTtl);
// Expire time must be resolved at this point.
assert newExpireTime != CU.EXPIRE_TIME_CALCULATE;
// Construct old entry info.
GridCacheVersionedEntryEx oldEntry = old.versionedEntry(txEntry.keepBinary());
// Construct new entry info.
GridCacheContext entryCtx = txEntry.context();
GridCacheVersionedEntryEx newEntry = new GridCacheLazyPlainVersionedEntry(entryCtx, txEntry.key(), newVal, newTtl, newExpireTime, newVer, false, txEntry.keepBinary());
GridCacheVersionConflictContext ctx = old.context().conflictResolve(oldEntry, newEntry, false);
if (ctx.isMerge()) {
Object resVal = ctx.mergeValue();
if ((op == CREATE || op == UPDATE) && resVal == null)
op = DELETE;
else if (op == DELETE && resVal != null)
op = old.isNewLocked() ? CREATE : UPDATE;
}
return F.t(op, ctx);
}
use of org.apache.ignite.internal.processors.cache.GridCacheContext in project ignite by apache.
the class IgniteTxHandler method needRemap.
/**
* @param expVer Expected topology version.
* @param curVer Current topology version.
* @param req Request.
* @return {@code True} if cache affinity changed and request should be remapped.
*/
private boolean needRemap(AffinityTopologyVersion expVer, AffinityTopologyVersion curVer, GridNearTxPrepareRequest req) {
if (expVer.equals(curVer))
return false;
for (IgniteTxEntry e : F.concat(false, req.reads(), req.writes())) {
GridCacheContext ctx = e.context();
Collection<ClusterNode> cacheNodes0 = ctx.discovery().cacheAffinityNodes(ctx.cacheId(), expVer);
Collection<ClusterNode> cacheNodes1 = ctx.discovery().cacheAffinityNodes(ctx.cacheId(), curVer);
if (!cacheNodes0.equals(cacheNodes1) || ctx.affinity().affinityTopologyVersion().compareTo(curVer) < 0)
return true;
try {
List<List<ClusterNode>> aff1 = ctx.affinity().assignments(expVer);
List<List<ClusterNode>> aff2 = ctx.affinity().assignments(curVer);
if (!aff1.equals(aff2))
return true;
} catch (IllegalStateException ignored) {
return true;
}
}
return false;
}
use of org.apache.ignite.internal.processors.cache.GridCacheContext in project ignite by apache.
the class GridCacheJdbcBlobStoreMultithreadedSelfTest method checkOpenedClosedCount.
/**
*
*/
private void checkOpenedClosedCount() {
assertEquals(GRID_CNT, Ignition.allGrids().size());
for (Ignite ignite : Ignition.allGrids()) {
GridCacheContext cctx = ((IgniteKernal) ignite).internalCache(DEFAULT_CACHE_NAME).context();
CacheStore store = cctx.store().configuredStore();
long opened = ((LongAdder8) U.field(store, "opened")).sum();
long closed = ((LongAdder8) U.field(store, "closed")).sum();
assert opened > 0;
assert closed > 0;
assertEquals(opened, closed);
}
}
Aggregations