use of org.apache.ignite.internal.processors.cache.CacheObjectContext in project ignite by apache.
the class CacheDataRowAdapter method initFromLink.
/**
* Read row from data pages.
* Can be called with cctx == null, if cache instance is unknown, but its ID is stored in the data row.
*
* @param cctx Cctx.
* @param sharedCtx Shared context.
* @param pageMem Page memory.
* @param rowData Row data.
*/
public final void initFromLink(@Nullable GridCacheContext<?, ?> cctx, GridCacheSharedContext<?, ?> sharedCtx, PageMemory pageMem, RowData rowData) throws IgniteCheckedException {
assert link != 0 : "link";
assert key == null : "key";
CacheObjectContext coctx = null;
if (cctx != null) {
cacheId = cctx.memoryPolicy().config().getPageEvictionMode() == DataPageEvictionMode.DISABLED ? cctx.cacheId() : // Force cacheId reading for evictable memory policies.
0;
coctx = cctx.cacheObjectContext();
}
long nextLink = link;
IncompleteObject<?> incomplete = null;
boolean first = true;
do {
final long pageId = pageId(nextLink);
final long page = pageMem.acquirePage(cacheId, pageId);
try {
// Non-empty data page must not be recycled.
long pageAddr = pageMem.readLock(cacheId, pageId, page);
assert pageAddr != 0L : nextLink;
try {
DataPageIO io = DataPageIO.VERSIONS.forPage(pageAddr);
DataPagePayload data = io.readPayload(pageAddr, itemId(nextLink), pageMem.pageSize());
nextLink = data.nextLink();
if (first) {
if (nextLink == 0) {
// Fast path for a single page row.
readFullRow(sharedCtx, coctx, pageAddr + data.offset(), rowData);
return;
}
first = false;
}
ByteBuffer buf = pageMem.pageBuffer(pageAddr);
buf.position(data.offset());
buf.limit(data.offset() + data.payloadSize());
boolean keyOnly = rowData == RowData.KEY_ONLY;
incomplete = readFragment(sharedCtx, coctx, buf, keyOnly, incomplete);
if (keyOnly && key != null)
return;
} finally {
pageMem.readUnlock(cacheId, pageId, page);
}
} finally {
pageMem.releasePage(cacheId, pageId, page);
}
} while (nextLink != 0);
assert isReady() : "ready";
}
use of org.apache.ignite.internal.processors.cache.CacheObjectContext in project ignite by apache.
the class RecordDataV1Serializer method readDataEntry.
/**
* @param in Input to read from.
* @return Read entry.
*/
DataEntry readDataEntry(ByteBufferBackedDataInput in) throws IOException, IgniteCheckedException {
int cacheId = in.readInt();
int keySize = in.readInt();
byte keyType = in.readByte();
byte[] keyBytes = new byte[keySize];
in.readFully(keyBytes);
int valSize = in.readInt();
byte valType = 0;
byte[] valBytes = null;
if (valSize >= 0) {
valType = in.readByte();
valBytes = new byte[valSize];
in.readFully(valBytes);
}
byte ord = in.readByte();
GridCacheOperation op = GridCacheOperation.fromOrdinal(ord & 0xFF);
GridCacheVersion nearXidVer = readVersion(in, true);
GridCacheVersion writeVer = readVersion(in, false);
int partId = in.readInt();
long partCntr = in.readLong();
long expireTime = in.readLong();
GridCacheContext cacheCtx = cctx.cacheContext(cacheId);
if (cacheCtx != null) {
CacheObjectContext coCtx = cacheCtx.cacheObjectContext();
KeyCacheObject key = co.toKeyCacheObject(coCtx, keyType, keyBytes);
CacheObject val = valBytes != null ? co.toCacheObject(coCtx, valType, valBytes) : null;
return new DataEntry(cacheId, key, val, op, nearXidVer, writeVer, expireTime, partId, partCntr);
} else
return new LazyDataEntry(cctx, cacheId, keyType, keyBytes, valType, valBytes, op, nearXidVer, writeVer, expireTime, partId, partCntr);
}
use of org.apache.ignite.internal.processors.cache.CacheObjectContext in project ignite by apache.
the class QueryUtils method typeForQueryEntity.
/**
* Create type candidate for query entity.
*
* @param cacheName Cache name.
* @param schemaName Schema name.
* @param cctx Cache context.
* @param qryEntity Query entity.
* @param mustDeserializeClss Classes which must be deserialized.
* @param escape Escape flag.
* @return Type candidate.
* @throws IgniteCheckedException If failed.
*/
public static QueryTypeCandidate typeForQueryEntity(String cacheName, String schemaName, GridCacheContext cctx, QueryEntity qryEntity, List<Class<?>> mustDeserializeClss, boolean escape) throws IgniteCheckedException {
GridKernalContext ctx = cctx.kernalContext();
CacheConfiguration<?, ?> ccfg = cctx.config();
boolean binaryEnabled = ctx.cacheObjects().isBinaryEnabled(ccfg);
CacheObjectContext coCtx = binaryEnabled ? ctx.cacheObjects().contextForCache(ccfg) : null;
QueryTypeDescriptorImpl desc = new QueryTypeDescriptorImpl(cacheName);
desc.schemaName(schemaName);
desc.aliases(qryEntity.getAliases());
// Key and value classes still can be available if they are primitive or JDK part.
// We need that to set correct types for _key and _val columns.
// We better box these types - otherwise, if user provides, say, raw 'byte' for
// key or value (which they could), we'll deem key or value as Object which clearly is not right.
Class<?> keyCls = U.box(U.classForName(qryEntity.findKeyType(), null, true));
Class<?> valCls = U.box(U.classForName(qryEntity.findValueType(), null, true));
// If local node has the classes and they are externalizable, we must use reflection properties.
boolean keyMustDeserialize = mustDeserializeBinary(ctx, keyCls);
boolean valMustDeserialize = mustDeserializeBinary(ctx, valCls);
boolean keyOrValMustDeserialize = keyMustDeserialize || valMustDeserialize;
if (keyCls == null)
keyCls = Object.class;
String simpleValType = ((valCls == null) ? typeName(qryEntity.findValueType()) : typeName(valCls));
desc.name(simpleValType);
desc.tableName(qryEntity.getTableName());
if (binaryEnabled && !keyOrValMustDeserialize) {
// Safe to check null.
if (SQL_TYPES.contains(valCls))
desc.valueClass(valCls);
else
desc.valueClass(Object.class);
if (SQL_TYPES.contains(keyCls))
desc.keyClass(keyCls);
else
desc.keyClass(Object.class);
} else {
if (valCls == null)
throw new IgniteCheckedException("Failed to find value class in the node classpath " + "(use default marshaller to enable binary objects) : " + qryEntity.findValueType());
desc.valueClass(valCls);
desc.keyClass(keyCls);
}
desc.keyTypeName(qryEntity.findKeyType());
desc.valueTypeName(qryEntity.findValueType());
desc.keyFieldName(qryEntity.getKeyFieldName());
desc.valueFieldName(qryEntity.getValueFieldName());
if (binaryEnabled && keyOrValMustDeserialize) {
if (keyMustDeserialize)
mustDeserializeClss.add(keyCls);
if (valMustDeserialize)
mustDeserializeClss.add(valCls);
}
QueryTypeIdKey typeId;
QueryTypeIdKey altTypeId = null;
int valTypeId = ctx.cacheObjects().typeId(qryEntity.findValueType());
if (valCls == null || (binaryEnabled && !keyOrValMustDeserialize)) {
processBinaryMeta(ctx, qryEntity, desc);
typeId = new QueryTypeIdKey(cacheName, valTypeId);
if (valCls != null)
altTypeId = new QueryTypeIdKey(cacheName, valCls);
String affField = null;
// Need to setup affinity key for distributed joins.
String keyType = qryEntity.getKeyType();
if (!cctx.customAffinityMapper() && keyType != null) {
if (coCtx != null) {
CacheDefaultBinaryAffinityKeyMapper mapper = (CacheDefaultBinaryAffinityKeyMapper) coCtx.defaultAffMapper();
BinaryField field = mapper.affinityKeyField(keyType);
if (field != null)
affField = field.name();
}
}
if (affField != null) {
if (!escape)
affField = normalizeObjectName(affField, false);
desc.affinityKey(affField);
}
} else {
processClassMeta(qryEntity, desc, coCtx);
AffinityKeyMapper keyMapper = cctx.config().getAffinityMapper();
if (keyMapper instanceof GridCacheDefaultAffinityKeyMapper) {
String affField = ((GridCacheDefaultAffinityKeyMapper) keyMapper).affinityKeyPropertyName(desc.keyClass());
if (affField != null) {
if (!escape)
affField = normalizeObjectName(affField, false);
desc.affinityKey(affField);
}
}
typeId = new QueryTypeIdKey(cacheName, valCls);
altTypeId = new QueryTypeIdKey(cacheName, valTypeId);
}
desc.typeId(valTypeId);
return new QueryTypeCandidate(typeId, altTypeId, desc);
}
use of org.apache.ignite.internal.processors.cache.CacheObjectContext in project ignite by apache.
the class IgniteCacheObjectProcessorImpl method contextForCache.
/**
* {@inheritDoc}
*/
@Override
public CacheObjectContext contextForCache(CacheConfiguration ccfg) throws IgniteCheckedException {
assert ccfg != null;
boolean storeVal = !ccfg.isCopyOnRead() || (!isBinaryEnabled(ccfg) && (QueryUtils.isEnabled(ccfg) || ctx.config().isPeerClassLoadingEnabled()));
CacheObjectContext res = new CacheObjectContext(ctx, ccfg.getName(), ccfg.getAffinityMapper() != null ? ccfg.getAffinityMapper() : new GridCacheDefaultAffinityKeyMapper(), ccfg.isCopyOnRead(), storeVal, ctx.config().isPeerClassLoadingEnabled() && !isBinaryEnabled(ccfg));
ctx.resource().injectGeneric(res.defaultAffMapper());
return res;
}
use of org.apache.ignite.internal.processors.cache.CacheObjectContext in project ignite by apache.
the class GridCacheQueryManager method runQuery.
/**
* Processes cache query request.
*
* @param qryInfo Query info.
*/
@SuppressWarnings("unchecked")
protected void runQuery(GridCacheQueryInfo qryInfo) {
assert qryInfo != null;
assert qryInfo.query().type() != SCAN || !qryInfo.local() : qryInfo;
if (!enterBusy()) {
if (cctx.localNodeId().equals(qryInfo.senderId()))
throw new IllegalStateException("Failed to process query request (grid is stopping).");
// Ignore remote requests when when node is stopping.
return;
}
try {
boolean performanceStatsEnabled = cctx.kernalContext().performanceStatistics().enabled();
if (performanceStatsEnabled)
IoStatisticsQueryHelper.startGatheringQueryStatistics();
boolean loc = qryInfo.local();
QueryResult<K, V> res = null;
if (log.isDebugEnabled())
log.debug("Running query: " + qryInfo);
boolean rmvIter = true;
GridCacheQueryAdapter<?> qry = qryInfo.query();
try {
// Preparing query closures.
IgniteClosure<Cache.Entry<K, V>, Object> trans = (IgniteClosure<Cache.Entry<K, V>, Object>) qryInfo.transformer();
IgniteReducer<Cache.Entry<K, V>, Object> rdc = (IgniteReducer<Cache.Entry<K, V>, Object>) qryInfo.reducer();
injectResources(trans);
injectResources(rdc);
int pageSize = qry.pageSize();
boolean incBackups = qry.includeBackups();
String taskName = cctx.kernalContext().task().resolveTaskName(qry.taskHash());
IgniteSpiCloseableIterator iter;
GridCacheQueryType type;
res = loc ? executeQuery(qry, qryInfo.arguments(), trans, loc, taskName, recipient(qryInfo.senderId(), qryInfo.requestId())) : queryResult(qryInfo, taskName);
if (res == null)
return;
iter = res.iterator(recipient(qryInfo.senderId(), qryInfo.requestId()));
type = res.type();
final GridCacheAdapter<K, V> cache = cctx.cache();
if (log.isDebugEnabled())
log.debug("Received index iterator [iterHasNext=" + iter.hasNext() + ", cacheSize=" + cache.size() + ']');
int cnt = 0;
boolean stop = false;
boolean pageSent = false;
Collection<Object> data = new ArrayList<>(pageSize);
AffinityTopologyVersion topVer = cctx.affinity().affinityTopologyVersion();
final boolean statsEnabled = cctx.statisticsEnabled();
final boolean readEvt = cctx.events().isRecordable(EVT_CACHE_QUERY_OBJECT_READ);
CacheObjectContext objCtx = cctx.cacheObjectContext();
while (!Thread.currentThread().isInterrupted()) {
long start = statsEnabled ? System.nanoTime() : 0L;
// actual row extracting may happen inside this method.
if (!iter.hasNext())
break;
Object row0 = iter.next();
// Query is cancelled.
if (row0 == null) {
onPageReady(loc, qryInfo, null, null, true, null);
break;
}
if (type == SCAN || type == INDEX)
// Scan iterator may return already transformed entry
data.add(row0);
else {
IgniteBiTuple<K, V> row = (IgniteBiTuple<K, V>) row0;
final K key = row.getKey();
final V val = row.getValue();
if (log.isDebugEnabled()) {
ClusterNode primaryNode = cctx.affinity().primaryByKey(key, cctx.affinity().affinityTopologyVersion());
log.debug(S.toString("Record", "key", key, true, "val", val, true, "incBackups", incBackups, false, "priNode", primaryNode != null ? U.id8(primaryNode.id()) : null, false, "node", U.id8(cctx.localNode().id()), false));
}
if (val == null) {
if (log.isDebugEnabled())
log.debug(S.toString("Unsuitable record value", "val", val, true));
continue;
}
if (statsEnabled) {
CacheMetricsImpl metrics = cctx.cache().metrics0();
metrics.onRead(true);
metrics.addGetTimeNanos(System.nanoTime() - start);
}
K key0 = null;
V val0 = null;
if (readEvt && cctx.gridEvents().hasListener(EVT_CACHE_QUERY_OBJECT_READ)) {
key0 = (K) CacheObjectUtils.unwrapBinaryIfNeeded(objCtx, key, qry.keepBinary(), false, null);
val0 = (V) CacheObjectUtils.unwrapBinaryIfNeeded(objCtx, val, qry.keepBinary(), false, null);
switch(type) {
case SQL:
cctx.gridEvents().record(new CacheQueryReadEvent<>(cctx.localNode(), "SQL query entry read.", EVT_CACHE_QUERY_OBJECT_READ, CacheQueryType.SQL.name(), cctx.name(), qry.queryClassName(), qry.clause(), null, null, qryInfo.arguments(), securitySubjectId(cctx), taskName, key0, val0, null, null));
break;
case TEXT:
cctx.gridEvents().record(new CacheQueryReadEvent<>(cctx.localNode(), "Full text query entry read.", EVT_CACHE_QUERY_OBJECT_READ, CacheQueryType.FULL_TEXT.name(), cctx.name(), qry.queryClassName(), qry.clause(), null, null, null, securitySubjectId(cctx), taskName, key0, val0, null, null));
break;
}
}
if (rdc != null) {
if (key0 == null)
key0 = (K) CacheObjectUtils.unwrapBinaryIfNeeded(objCtx, key, qry.keepBinary(), false, null);
if (val0 == null)
val0 = (V) CacheObjectUtils.unwrapBinaryIfNeeded(objCtx, val, qry.keepBinary(), false, null);
Cache.Entry<K, V> entry = new CacheEntryImpl(key0, val0);
// Reduce.
if (!rdc.collect(entry) || !iter.hasNext()) {
onPageReady(loc, qryInfo, null, Collections.singletonList(rdc.reduce()), true, null);
pageSent = true;
break;
} else
continue;
} else {
if (type == TEXT)
// (K, V, score). Value transfers as BinaryObject.
data.add(row0);
else
data.add(new T2<>(key, val));
}
}
if (!loc) {
if (++cnt == pageSize || !iter.hasNext()) {
boolean finished = !iter.hasNext();
onPageReady(loc, qryInfo, res.metadata(), data, finished, null);
pageSent = true;
res.onPageSend();
if (!finished)
rmvIter = false;
if (!qryInfo.allPages())
return;
data = new ArrayList<>(pageSize);
if (stop)
// while
break;
}
}
}
if (!pageSent) {
if (rdc == null)
onPageReady(loc, qryInfo, res.metadata(), data, true, null);
else
onPageReady(loc, qryInfo, res.metadata(), Collections.singletonList(rdc.reduce()), true, null);
res.onPageSend();
}
} catch (Throwable e) {
if (X.hasCause(e, ClassNotFoundException.class) && !qry.keepBinary() && cctx.binaryMarshaller() && !cctx.localNode().isClient() && !log.isQuiet()) {
LT.warn(log, "Suggestion for the cause of ClassNotFoundException");
LT.warn(log, "To disable, set -D" + IGNITE_QUIET + "=true");
LT.warn(log, " ^-- Ignite configured to use BinaryMarshaller but keepBinary is false for " + "request");
LT.warn(log, " ^-- Server node need to load definition of data classes. " + "It can be reason of ClassNotFoundException(consider IgniteCache.withKeepBinary to fix)");
LT.warn(log, "Refer this page for detailed information: " + "https://apacheignite.readme.io/docs/binary-marshaller");
}
if (!X.hasCause(e, GridDhtUnreservedPartitionException.class))
U.error(log, "Failed to run query [qry=" + qryInfo + ", node=" + cctx.nodeId() + "]", e);
onPageReady(loc, qryInfo, null, null, true, e);
if (e instanceof Error)
throw (Error) e;
} finally {
if (loc) {
// Local iterators are always removed.
if (res != null) {
try {
res.closeIfNotShared(recipient(qryInfo.senderId(), qryInfo.requestId()));
} catch (IgniteCheckedException e) {
if (!X.hasCause(e, GridDhtUnreservedPartitionException.class))
U.error(log, "Failed to close local iterator [qry=" + qryInfo + ", node=" + cctx.nodeId() + "]", e);
}
}
} else if (rmvIter)
removeQueryResult(qryInfo.senderId(), qryInfo.requestId());
if (performanceStatsEnabled) {
IoStatisticsHolder stat = IoStatisticsQueryHelper.finishGatheringQueryStatistics();
if (stat.logicalReads() > 0 || stat.physicalReads() > 0) {
cctx.kernalContext().performanceStatistics().queryReads(res.type(), qryInfo.senderId(), qryInfo.requestId(), stat.logicalReads(), stat.physicalReads());
}
}
}
} finally {
leaveBusy();
}
}
Aggregations