use of org.apache.ignite.internal.processors.cache.KeyCacheObject in project ignite by apache.
the class GridCacheStoreManagerAdapter method loadCache.
/**
* {@inheritDoc}
*/
@Override
public final boolean loadCache(final GridInClosure3 vis, Object[] args) throws IgniteCheckedException {
if (store != null) {
if (log.isDebugEnabled())
log.debug("Loading all values from store.");
sessionInit0(null, StoreOperation.READ, false);
boolean threwEx = true;
try {
store.loadCache(new IgniteBiInClosure<Object, Object>() {
@Override
public void apply(Object k, Object o) {
Object v;
GridCacheVersion ver = null;
if (locStore) {
IgniteBiTuple<Object, GridCacheVersion> t = (IgniteBiTuple<Object, GridCacheVersion>) o;
v = t.get1();
ver = t.get2();
} else
v = o;
KeyCacheObject cacheKey = cctx.toCacheKeyObject(k);
vis.apply(cacheKey, v, ver);
}
}, args);
threwEx = false;
} catch (CacheLoaderException e) {
throw new IgniteCheckedException(e);
} catch (Exception e) {
throw new IgniteCheckedException(new CacheLoaderException(e));
} finally {
sessionEnd0(null, threwEx);
}
if (log.isDebugEnabled())
log.debug("Loaded all values from store.");
return true;
}
LT.warn(log, "Calling Cache.loadCache() method will have no effect, " + "CacheConfiguration.getStore() is not defined for cache: " + cctx.name());
return false;
}
use of org.apache.ignite.internal.processors.cache.KeyCacheObject in project ignite by apache.
the class GridCacheStoreManagerAdapter method put.
/**
* {@inheritDoc}
*/
@Override
public final boolean put(@Nullable IgniteInternalTx tx, KeyCacheObject key, CacheObject val, GridCacheVersion ver) throws IgniteCheckedException {
if (store != null) {
// Never persist internal keys.
if (key instanceof GridCacheInternal)
return true;
Object key0 = cctx.unwrapBinaryIfNeeded(key, !convertBinary());
Object val0 = cctx.unwrapBinaryIfNeeded(val, !convertBinary());
if (log.isDebugEnabled()) {
log.debug(S.toString("Storing value in cache store", "key", key0, true, "val", val0, true));
}
sessionInit0(tx, StoreOperation.WRITE, false);
boolean threwEx = true;
try {
store.write(new CacheEntryImpl<>(key0, locStore ? F.t(val0, ver) : val0));
threwEx = false;
} catch (ClassCastException e) {
handleClassCastException(e);
} catch (CacheWriterException e) {
throw new IgniteCheckedException(e);
} catch (Exception e) {
throw new IgniteCheckedException(new CacheWriterException(e));
} finally {
sessionEnd0(tx, threwEx);
}
if (log.isDebugEnabled()) {
log.debug(S.toString("Stored value in cache store", "key", key0, true, "val", val0, true));
}
return true;
}
return false;
}
use of org.apache.ignite.internal.processors.cache.KeyCacheObject in project ignite by apache.
the class IgniteTxLocalAdapter method postLockWrite.
/**
* Post lock processing for put or remove.
*
* @param cacheCtx Context.
* @param keys Keys.
* @param ret Return value.
* @param rmv {@code True} if remove.
* @param retval Flag to return value or not.
* @param read {@code True} if read.
* @param accessTtl TTL for read operation.
* @param filter Filter to check entries.
* @throws IgniteCheckedException If error.
* @param computeInvoke If {@code true} computes return value for invoke operation.
*/
@SuppressWarnings("unchecked")
protected final void postLockWrite(GridCacheContext cacheCtx, Iterable<KeyCacheObject> keys, GridCacheReturn ret, boolean rmv, boolean retval, boolean read, long accessTtl, CacheEntryPredicate[] filter, boolean computeInvoke) throws IgniteCheckedException {
for (KeyCacheObject k : keys) {
IgniteTxEntry txEntry = entry(cacheCtx.txKey(k));
if (txEntry == null)
throw new IgniteCheckedException("Transaction entry is null (most likely collection of keys passed into cache " + "operation was changed before operation completed) [missingKey=" + k + ", tx=" + this + ']');
while (true) {
GridCacheEntryEx cached = txEntry.cached();
try {
assert cached.detached() || cached.lockedByThread(threadId) || isRollbackOnly() : "Transaction lock is not acquired [entry=" + cached + ", tx=" + this + ", nodeId=" + cctx.localNodeId() + ", threadId=" + threadId + ']';
if (log.isDebugEnabled())
log.debug("Post lock write entry: " + cached);
CacheObject v = txEntry.previousValue();
boolean hasPrevVal = txEntry.hasPreviousValue();
if (onePhaseCommit())
filter = txEntry.filters();
// If we have user-passed filter, we must read value into entry for peek().
if (!F.isEmptyOrNulls(filter) && !F.isAlwaysTrue(filter))
retval = true;
boolean invoke = txEntry.op() == TRANSFORM;
if (retval || invoke) {
if (!cacheCtx.isNear()) {
if (!hasPrevVal) {
// For non-local cache should read from store after lock on primary.
boolean readThrough = cacheCtx.isLocal() && (invoke || cacheCtx.loadPreviousValue()) && !txEntry.skipStore();
v = cached.innerGet(null, this, readThrough, /*metrics*/
!invoke, /*event*/
!invoke && !dht(), CU.subjectId(this, cctx), null, resolveTaskName(), null, txEntry.keepBinary());
}
} else {
if (!hasPrevVal)
v = cached.rawGet();
}
if (txEntry.op() == TRANSFORM) {
if (computeInvoke) {
GridCacheVersion ver;
try {
ver = cached.version();
} catch (GridCacheEntryRemovedException e) {
assert optimistic() : txEntry;
if (log.isDebugEnabled())
log.debug("Failed to get entry version: [msg=" + e.getMessage() + ']');
ver = null;
}
addInvokeResult(txEntry, v, ret, ver);
}
} else
ret.value(cacheCtx, v, txEntry.keepBinary());
}
boolean pass = F.isEmpty(filter) || cacheCtx.isAll(cached, filter);
// For remove operation we return true only if we are removing s/t,
// i.e. cached value is not null.
ret.success(pass && (!retval ? !rmv || cached.hasValue() || v != null : !rmv || v != null));
if (onePhaseCommit())
txEntry.filtersPassed(pass);
boolean updateTtl = read;
if (pass) {
txEntry.markValid();
if (log.isDebugEnabled())
log.debug("Filter passed in post lock for key: " + k);
} else {
// Revert operation to previous. (if no - NOOP, so entry will be unlocked).
txEntry.setAndMarkValid(txEntry.previousOperation(), cacheCtx.toCacheObject(ret.value()));
txEntry.filters(CU.empty0());
txEntry.filtersSet(false);
updateTtl = !cacheCtx.putIfAbsentFilter(filter);
}
if (updateTtl) {
if (!read) {
ExpiryPolicy expiryPlc = cacheCtx.expiryForTxEntry(txEntry);
if (expiryPlc != null)
txEntry.ttl(CU.toTtl(expiryPlc.getExpiryForAccess()));
} else
txEntry.ttl(accessTtl);
}
// While.
break;
}// If entry cached within transaction got removed before lock.
catch (GridCacheEntryRemovedException ignore) {
if (log.isDebugEnabled())
log.debug("Got removed entry in putAllAsync method (will retry): " + cached);
txEntry.cached(entryEx(cached.context(), txEntry.txKey(), topologyVersion()));
}
}
}
}
use of org.apache.ignite.internal.processors.cache.KeyCacheObject in project ignite by apache.
the class IgniteTxStateImpl method validateTopology.
/**
* {@inheritDoc}
*/
@Override
public IgniteCheckedException validateTopology(GridCacheSharedContext cctx, boolean read, GridDhtTopologyFuture topFut) {
Map<Integer, Set<KeyCacheObject>> keysByCacheId = new HashMap<>();
for (IgniteTxKey key : txMap.keySet()) {
Set<KeyCacheObject> set = keysByCacheId.get(key.cacheId());
if (set == null)
keysByCacheId.put(key.cacheId(), set = new HashSet<>());
set.add(key.key());
}
StringBuilder invalidCaches = null;
for (Map.Entry<Integer, Set<KeyCacheObject>> e : keysByCacheId.entrySet()) {
int cacheId = e.getKey();
GridCacheContext ctx = cctx.cacheContext(cacheId);
assert ctx != null : cacheId;
Throwable err = topFut.validateCache(ctx, recovery != null && recovery, read, null, e.getValue());
if (err != null) {
if (invalidCaches != null)
invalidCaches.append(", ");
else
invalidCaches = new StringBuilder();
invalidCaches.append(U.maskName(ctx.name()));
}
}
if (invalidCaches != null) {
return new IgniteCheckedException("Failed to perform cache operation (cache topology is not valid): " + invalidCaches);
}
for (int i = 0; i < activeCacheIds.size(); i++) {
int cacheId = activeCacheIds.get(i);
GridCacheContext<?, ?> cacheCtx = cctx.cacheContext(cacheId);
if (CU.affinityNodes(cacheCtx, topFut.topologyVersion()).isEmpty()) {
return new ClusterTopologyServerNotFoundException("Failed to map keys for cache (all " + "partition nodes left the grid): " + cacheCtx.name());
}
}
return null;
}
use of org.apache.ignite.internal.processors.cache.KeyCacheObject in project ignite by apache.
the class GridPartitionedGetFuture method map.
/**
* @param mappings Mappings.
* @param key Key to map.
* @param locVals Local values.
* @param topVer Topology version.
* @param mapped Previously mapped.
* @return {@code True} if has remote nodes.
*/
@SuppressWarnings("ConstantConditions")
private boolean map(KeyCacheObject key, Map<ClusterNode, LinkedHashMap<KeyCacheObject, Boolean>> mappings, Map<K, V> locVals, AffinityTopologyVersion topVer, Map<ClusterNode, LinkedHashMap<KeyCacheObject, Boolean>> mapped) {
int part = cctx.affinity().partition(key);
List<ClusterNode> affNodes = cctx.affinity().nodesByPartition(part, topVer);
if (affNodes.isEmpty()) {
onDone(serverNotFoundError(topVer));
return false;
}
boolean fastLocGet = (!forcePrimary || affNodes.get(0).isLocal()) && cctx.reserveForFastLocalGet(part, topVer);
if (fastLocGet) {
try {
if (localGet(topVer, key, part, locVals))
return false;
} finally {
cctx.releaseForFastLocalGet(part, topVer);
}
}
ClusterNode node = cctx.selectAffinityNodeBalanced(affNodes, canRemap);
if (node == null) {
onDone(serverNotFoundError(topVer));
return false;
}
boolean remote = !node.isLocal();
LinkedHashMap<KeyCacheObject, Boolean> keys = mapped.get(node);
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(node) + ", mappings=" + mapped + ']'));
return false;
}
}
LinkedHashMap<KeyCacheObject, Boolean> old = mappings.get(node);
if (old == null)
mappings.put(node, old = new LinkedHashMap<>(3, 1f));
old.put(key, false);
return remote;
}
Aggregations