use of org.apache.ignite.internal.processors.cache.version.GridCacheVersion in project ignite by apache.
the class GridNearAtomicCache method processNearAtomicUpdateResponse.
/**
* @param req Update request.
* @param res Update response.
*/
public void processNearAtomicUpdateResponse(GridNearAtomicAbstractUpdateRequest req, GridNearAtomicUpdateResponse res) {
if (F.size(res.failedKeys()) == req.size())
return;
/*
* Choose value to be stored in near cache: first check key is not in failed and not in skipped list,
* then check if value was generated on primary node, if not then use value sent in request.
*/
Collection<KeyCacheObject> failed = res.failedKeys();
List<Integer> nearValsIdxs = res.nearValuesIndexes();
List<Integer> skipped = res.skippedIndexes();
GridCacheVersion ver = res.nearVersion();
assert ver != null : "Failed to find version [req=" + req + ", res=" + res + ']';
int nearValIdx = 0;
String taskName = ctx.kernalContext().task().resolveTaskName(req.taskNameHash());
for (int i = 0; i < req.size(); i++) {
if (F.contains(skipped, i))
continue;
KeyCacheObject key = req.key(i);
if (F.contains(failed, key))
continue;
if (ctx.affinity().partitionBelongs(ctx.localNode(), ctx.affinity().partition(key), req.topologyVersion())) {
// Reader became backup.
GridCacheEntryEx entry = peekEx(key);
if (entry != null && entry.markObsolete(ver))
removeEntry(entry);
continue;
}
CacheObject val = null;
if (F.contains(nearValsIdxs, i)) {
val = res.nearValue(nearValIdx);
nearValIdx++;
} else {
assert req.operation() != TRANSFORM;
if (req.operation() != DELETE)
val = req.value(i);
}
long ttl = res.nearTtl(i);
long expireTime = res.nearExpireTime(i);
if (ttl != CU.TTL_NOT_CHANGED && expireTime == CU.EXPIRE_TIME_CALCULATE)
expireTime = CU.toExpireTime(ttl);
try {
processNearAtomicUpdateResponse(ver, key, val, ttl, expireTime, req.keepBinary(), req.nodeId(), req.subjectId(), taskName);
} catch (IgniteCheckedException e) {
res.addFailedKey(key, new IgniteCheckedException("Failed to update key in near cache: " + key, e));
}
}
}
use of org.apache.ignite.internal.processors.cache.version.GridCacheVersion in project ignite by apache.
the class GridNearGetFuture method loadEntries.
/**
* @param nodeId Node id.
* @param keys Keys.
* @param infos Entry infos.
* @param savedEntries Saved entries.
* @param topVer Topology version
* @return Result map.
*/
private Map<K, V> loadEntries(UUID nodeId, Collection<KeyCacheObject> keys, Collection<GridCacheEntryInfo> infos, Map<KeyCacheObject, GridNearCacheEntry> savedEntries, AffinityTopologyVersion topVer) {
boolean empty = F.isEmpty(keys);
Map<K, V> map = empty ? Collections.<K, V>emptyMap() : new GridLeanMap<K, V>(keys.size());
if (!empty) {
boolean atomic = cctx.atomic();
GridCacheVersion ver = atomic ? null : F.isEmpty(infos) ? null : cctx.versions().next();
for (GridCacheEntryInfo info : infos) {
try {
info.unmarshalValue(cctx, cctx.deploy().globalLoader());
// Entries available locally in DHT should not be loaded into near cache for reading.
if (!cctx.affinity().keyLocalNode(info.key(), cctx.affinity().affinityTopologyVersion())) {
GridNearCacheEntry entry = savedEntries.get(info.key());
if (entry == null)
entry = cache().entryExx(info.key(), topVer);
// Load entry into cache.
entry.loadedValue(tx, nodeId, info.value(), atomic ? info.version() : ver, info.version(), info.ttl(), info.expireTime(), true, !deserializeBinary, topVer, subjId);
}
CacheObject val = info.value();
KeyCacheObject key = info.key();
assert skipVals == (info.value() == null);
cctx.addResult(map, key, val, skipVals, keepCacheObjects, deserializeBinary, false, needVer ? info.version() : null, 0, 0);
} catch (GridCacheEntryRemovedException ignore) {
if (log.isDebugEnabled())
log.debug("Got removed entry while processing get response (will not retry).");
} catch (Exception e) {
// Fail.
onDone(e);
return Collections.emptyMap();
}
}
}
return map;
}
use of org.apache.ignite.internal.processors.cache.version.GridCacheVersion in project ignite by apache.
the class GridNearGetFuture method map.
/**
* @param mappings Mappings.
* @param key Key to map.
* @param topVer Topology version
* @param mapped Previously mapped.
* @param saved Reserved near cache entries.
* @return Map.
*/
@SuppressWarnings("unchecked")
private Map<KeyCacheObject, GridNearCacheEntry> map(KeyCacheObject key, Map<ClusterNode, LinkedHashMap<KeyCacheObject, Boolean>> mappings, AffinityTopologyVersion topVer, Map<ClusterNode, LinkedHashMap<KeyCacheObject, Boolean>> mapped, Map<KeyCacheObject, GridNearCacheEntry> saved) {
int part = cctx.affinity().partition(key);
List<ClusterNode> affNodes = cctx.affinity().nodesByPartition(part, topVer);
if (affNodes.isEmpty()) {
onDone(serverNotFoundError(topVer));
return null;
}
final GridNearCacheAdapter near = cache();
// Allow to get cached value from the local node.
boolean allowLocRead = !forcePrimary || cctx.localNode().equals(affNodes.get(0));
while (true) {
GridNearCacheEntry entry = allowLocRead ? (GridNearCacheEntry) near.peekEx(key) : null;
try {
CacheObject v = null;
GridCacheVersion ver = null;
boolean isNear = entry != null;
// First we peek into near cache.
if (isNear) {
if (needVer) {
EntryGetResult res = entry.innerGetVersioned(null, null, /**update-metrics*/
true, /*event*/
!skipVals, subjId, null, taskName, expiryPlc, !deserializeBinary, null);
if (res != null) {
v = res.value();
ver = res.version();
}
} else {
v = entry.innerGet(null, tx, /*read-through*/
false, /*metrics*/
true, /*events*/
!skipVals, subjId, null, taskName, expiryPlc, !deserializeBinary);
}
}
if (v == null) {
boolean fastLocGet = allowLocRead && cctx.allowFastLocalRead(part, affNodes, topVer);
if (fastLocGet && localDhtGet(key, part, topVer, isNear))
break;
ClusterNode affNode = affinityNode(affNodes);
if (affNode == null) {
onDone(serverNotFoundError(topVer));
return saved;
}
if (cctx.cache().configuration().isStatisticsEnabled() && !skipVals && !affNode.isLocal())
cache().metrics0().onRead(false);
LinkedHashMap<KeyCacheObject, Boolean> keys = mapped.get(affNode);
if (keys != null && keys.containsKey(key)) {
if (REMAP_CNT_UPD.incrementAndGet(this) > MAX_REMAP_CNT) {
onDone(new ClusterTopologyCheckedException("Failed to remap key to a new node after " + MAX_REMAP_CNT + " attempts (key got remapped to the same node) " + "[key=" + key + ", node=" + U.toShortString(affNode) + ", mappings=" + mapped + ']'));
return saved;
}
}
if (!affNodes.contains(cctx.localNode())) {
GridNearCacheEntry nearEntry = entry != null ? entry : near.entryExx(key, topVer);
nearEntry.reserveEviction();
entry = null;
if (saved == null)
saved = U.newHashMap(3);
saved.put(key, nearEntry);
}
// Don't add reader if transaction acquires lock anyway to avoid deadlock.
boolean addRdr = tx == null || tx.optimistic();
if (!addRdr && tx.readCommitted() && !tx.writeSet().contains(cctx.txKey(key)))
addRdr = true;
LinkedHashMap<KeyCacheObject, Boolean> old = mappings.get(affNode);
if (old == null)
mappings.put(affNode, old = new LinkedHashMap<>(3, 1f));
old.put(key, addRdr);
} else
addResult(key, v, ver);
break;
} catch (IgniteCheckedException e) {
onDone(e);
break;
} catch (GridCacheEntryRemovedException ignored) {
// Retry.
} finally {
if (entry != null && tx == null)
cctx.evicts().touch(entry, topVer);
}
}
return saved;
}
use of org.apache.ignite.internal.processors.cache.version.GridCacheVersion in project ignite by apache.
the class GridNearOptimisticSerializableTxPrepareFuture method onOwnerChanged.
/** {@inheritDoc} */
@Override
public boolean onOwnerChanged(GridCacheEntryEx entry, GridCacheMvccCandidate owner) {
if (log.isDebugEnabled())
log.debug("Transaction future received owner changed callback: " + entry);
if ((entry.context().isNear() || entry.context().isLocal()) && owner != null) {
IgniteTxEntry txEntry = tx.entry(entry.txKey());
if (txEntry != null) {
if (entry.context().isLocal()) {
GridCacheVersion serReadVer = txEntry.entryReadVersion();
if (serReadVer != null) {
GridCacheContext ctx = entry.context();
while (true) {
try {
if (!entry.checkSerializableReadVersion(serReadVer)) {
Object key = entry.key().value(ctx.cacheObjectContext(), false);
IgniteTxOptimisticCheckedException err0 = new IgniteTxOptimisticCheckedException(S.toString("Failed to prepare transaction, read/write conflict", "key", key, true, "cache", ctx.name(), false));
ERR_UPD.compareAndSet(this, null, err0);
}
break;
} catch (GridCacheEntryRemovedException ignored) {
entry = ctx.cache().entryEx(entry.key(), tx.topologyVersion());
txEntry.cached(entry);
}
}
}
}
if (keyLockFut != null)
keyLockFut.onKeyLocked(entry.txKey());
return true;
}
}
return false;
}
use of org.apache.ignite.internal.processors.cache.version.GridCacheVersion in project ignite by apache.
the class GridNearTxLocal method checkMissed.
/**
* @param cacheCtx Cache context.
* @param topVer Topology version.
* @param map Return map.
* @param missedMap Missed keys.
* @param deserializeBinary Deserialize binary flag.
* @param skipVals Skip values flag.
* @param keepCacheObjects Keep cache objects flag.
* @param skipStore Skip store flag.
* @param expiryPlc Expiry policy.
* @return Loaded key-value pairs.
*/
private <K, V> IgniteInternalFuture<Map<K, V>> checkMissed(final GridCacheContext cacheCtx, final AffinityTopologyVersion topVer, final Map<K, V> map, final Map<KeyCacheObject, GridCacheVersion> missedMap, final boolean deserializeBinary, final boolean skipVals, final boolean keepCacheObjects, final boolean skipStore, final boolean recovery, final boolean needVer, final ExpiryPolicy expiryPlc) {
if (log.isDebugEnabled())
log.debug("Loading missed values for missed map: " + missedMap);
final boolean needReadVer = (serializable() && optimistic()) || needVer;
return new GridEmbeddedFuture<>(new C2<Void, Exception, Map<K, V>>() {
@Override
public Map<K, V> apply(Void v, Exception e) {
if (e != null) {
setRollbackOnly();
throw new GridClosureException(e);
}
return map;
}
}, loadMissing(cacheCtx, topVer, !skipStore, false, missedMap.keySet(), skipVals, needReadVer, !deserializeBinary, recovery, expiryPlc, new GridInClosure3<KeyCacheObject, Object, GridCacheVersion>() {
@Override
public void apply(KeyCacheObject key, Object val, GridCacheVersion loadVer) {
if (isRollbackOnly()) {
if (log.isDebugEnabled())
log.debug("Ignoring loaded value for read because transaction was rolled back: " + GridNearTxLocal.this);
return;
}
CacheObject cacheVal = cacheCtx.toCacheObject(val);
CacheObject visibleVal = cacheVal;
IgniteTxKey txKey = cacheCtx.txKey(key);
IgniteTxEntry txEntry = entry(txKey);
if (txEntry != null) {
if (!readCommitted())
txEntry.readValue(cacheVal);
if (!F.isEmpty(txEntry.entryProcessors()))
visibleVal = txEntry.applyEntryProcessors(visibleVal);
}
assert txEntry != null || readCommitted() || skipVals;
GridCacheEntryEx e = txEntry == null ? entryEx(cacheCtx, txKey, topVer) : txEntry.cached();
if (readCommitted() || skipVals) {
cacheCtx.evicts().touch(e, topologyVersion());
if (visibleVal != null) {
cacheCtx.addResult(map, key, visibleVal, skipVals, keepCacheObjects, deserializeBinary, false, needVer ? loadVer : null, 0, 0);
}
} else {
assert txEntry != null;
txEntry.setAndMarkValid(cacheVal);
if (needReadVer) {
assert loadVer != null;
txEntry.entryReadVersion(loadVer);
}
if (visibleVal != null) {
cacheCtx.addResult(map, key, visibleVal, skipVals, keepCacheObjects, deserializeBinary, false, needVer ? loadVer : null, 0, 0);
}
}
}
}));
}
Aggregations