Search in sources :

Example 1 with H2CacheRow

use of org.apache.ignite.internal.processors.query.h2.opt.H2CacheRow in project ignite by apache.

the class ValidateIndexesClosure method processIndex.

/**
 * @param cacheCtxWithIdx Cache context and appropriate index.
 * @param idleChecker Idle check closure.
 */
private Map<String, ValidateIndexesPartitionResult> processIndex(T2<GridCacheContext, Index> cacheCtxWithIdx, IgniteInClosure<Integer> idleChecker) {
    if (validateCtx.isCancelled())
        return emptyMap();
    GridCacheContext ctx = cacheCtxWithIdx.get1();
    Index idx = cacheCtxWithIdx.get2();
    ValidateIndexesPartitionResult idxValidationRes = new ValidateIndexesPartitionResult();
    boolean enoughIssues = false;
    Cursor cursor = null;
    try (Session session = mvccSession(cacheCtxWithIdx.get1())) {
        cursor = idx.find(session, null, null);
        if (cursor == null)
            throw new IgniteCheckedException("Can't iterate through index: " + idx);
    } catch (Throwable t) {
        IndexValidationIssue is = new IndexValidationIssue(null, ctx.name(), idx.getName(), t);
        log.error("Find in index failed: " + is.toString());
        idxValidationRes.reportIssue(is);
        enoughIssues = true;
    }
    final boolean skipConditions = checkFirst > 0 || checkThrough > 0;
    final boolean bothSkipConditions = checkFirst > 0 && checkThrough > 0;
    long current = 0;
    long processedNumber = 0;
    KeyCacheObject previousKey = null;
    while (!enoughIssues && !validateCtx.isCancelled()) {
        KeyCacheObject h2key = null;
        try {
            try {
                if (!cursor.next())
                    break;
            } catch (DbException e) {
                if (X.hasCause(e, CorruptedTreeException.class))
                    throw new IgniteCheckedException("Key is present in SQL index, but is missing in corresponding " + "data page. Previous successfully read key: " + CacheObjectUtils.unwrapBinaryIfNeeded(ctx.cacheObjectContext(), previousKey, true, true), X.cause(e, CorruptedTreeException.class));
                throw e;
            }
            H2CacheRow h2Row = (H2CacheRow) cursor.get();
            if (skipConditions) {
                if (bothSkipConditions) {
                    if (processedNumber > checkFirst)
                        break;
                    else if (current++ % checkThrough > 0)
                        continue;
                    else
                        processedNumber++;
                } else {
                    if (checkFirst > 0) {
                        if (current++ > checkFirst)
                            break;
                    } else {
                        if (current++ % checkThrough > 0)
                            continue;
                    }
                }
            }
            h2key = h2Row.key();
            if (h2Row.link() != 0L) {
                CacheDataRow cacheDataStoreRow = ctx.group().offheap().read(ctx, h2key);
                if (cacheDataStoreRow == null)
                    throw new IgniteCheckedException("Key is present in SQL index, but can't be found in CacheDataTree.");
            } else
                throw new IgniteCheckedException("Invalid index row, possibly deleted " + h2Row);
        } catch (Throwable t) {
            Object o = CacheObjectUtils.unwrapBinaryIfNeeded(ctx.cacheObjectContext(), h2key, true, true);
            IndexValidationIssue is = new IndexValidationIssue(String.valueOf(o), ctx.name(), idx.getName(), t);
            log.error("Failed to lookup key: " + is.toString());
            enoughIssues |= idxValidationRes.reportIssue(is);
        } finally {
            previousKey = h2key;
        }
    }
    CacheGroupContext group = ctx.group();
    String uniqueIdxName = String.format("[cacheGroup=%s, cacheGroupId=%s, cache=%s, cacheId=%s, idx=%s]", group.name(), group.groupId(), ctx.name(), ctx.cacheId(), idx.getName());
    idleChecker.apply(group.groupId());
    processedIndexes.incrementAndGet();
    printProgressOfIndexValidationIfNeeded();
    return Collections.singletonMap(uniqueIdxName, idxValidationRes);
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) Index(org.h2.index.Index) H2CacheRow(org.apache.ignite.internal.processors.query.h2.opt.H2CacheRow) Cursor(org.h2.index.Cursor) DbException(org.h2.message.DbException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CorruptedTreeException(org.apache.ignite.internal.processors.cache.persistence.tree.CorruptedTreeException) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) Session(org.h2.engine.Session) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 2 with H2CacheRow

use of org.apache.ignite.internal.processors.query.h2.opt.H2CacheRow in project ignite by apache.

the class ValidateIndexesClosure method processPartIterator.

/**
 * Process partition iterator.
 *
 * @param grpCtx Cache group context.
 * @param partRes Result object.
 * @param session H2 session.
 * @param it Partition iterator.
 * @throws IgniteCheckedException
 */
private void processPartIterator(CacheGroupContext grpCtx, ValidateIndexesPartitionResult partRes, Session session, GridIterator<CacheDataRow> it) throws IgniteCheckedException {
    boolean enoughIssues = false;
    GridQueryProcessor qryProcessor = ignite.context().query();
    final boolean skipConditions = checkFirst > 0 || checkThrough > 0;
    final boolean bothSkipConditions = checkFirst > 0 && checkThrough > 0;
    long current = 0;
    long processedNumber = 0;
    while (it.hasNextX() && !validateCtx.isCancelled()) {
        if (enoughIssues)
            break;
        CacheDataRow row = it.nextX();
        if (skipConditions) {
            if (bothSkipConditions) {
                if (processedNumber > checkFirst)
                    break;
                else if (current++ % checkThrough > 0)
                    continue;
                else
                    processedNumber++;
            } else {
                if (checkFirst > 0) {
                    if (current++ > checkFirst)
                        break;
                } else {
                    if (current++ % checkThrough > 0)
                        continue;
                }
            }
        }
        int cacheId = row.cacheId() == 0 ? grpCtx.groupId() : row.cacheId();
        GridCacheContext<?, ?> cacheCtx = row.cacheId() == 0 ? grpCtx.singleCacheContext() : grpCtx.shared().cacheContext(row.cacheId());
        if (cacheCtx == null)
            throw new IgniteException("Unknown cacheId of CacheDataRow: " + cacheId);
        if (row.link() == 0L) {
            String errMsg = "Invalid partition row, possibly deleted";
            log.error(errMsg);
            IndexValidationIssue is = new IndexValidationIssue(null, cacheCtx.name(), null, new IgniteCheckedException(errMsg));
            enoughIssues |= partRes.reportIssue(is);
            continue;
        }
        QueryTypeDescriptorImpl res = qryProcessor.typeByValue(cacheCtx.name(), cacheCtx.cacheObjectContext(), row.key(), row.value(), true);
        if (res == null)
            // Tolerate - (k, v) is just not indexed.
            continue;
        IgniteH2Indexing indexing = (IgniteH2Indexing) qryProcessor.getIndexing();
        GridH2Table gridH2Tbl = indexing.schemaManager().dataTable(cacheCtx.name(), res.tableName());
        if (gridH2Tbl == null)
            // Tolerate - (k, v) is just not indexed.
            continue;
        GridH2RowDescriptor gridH2RowDesc = gridH2Tbl.rowDescriptor();
        H2CacheRow h2Row = gridH2RowDesc.createRow(row);
        ArrayList<Index> indexes = gridH2Tbl.getIndexes();
        for (Index idx : indexes) {
            if (validateCtx.isCancelled())
                break;
            if (!(idx instanceof H2TreeIndexBase))
                continue;
            try {
                Cursor cursor = idx.find(session, h2Row, h2Row);
                if (cursor == null || !cursor.next())
                    throw new IgniteCheckedException("Key is present in CacheDataTree, but can't be found in SQL index.");
            } catch (Throwable t) {
                Object o = CacheObjectUtils.unwrapBinaryIfNeeded(grpCtx.cacheObjectContext(), row.key(), true, true);
                IndexValidationIssue is = new IndexValidationIssue(o.toString(), cacheCtx.name(), idx.getName(), t);
                log.error("Failed to lookup key: " + is.toString(), t);
                enoughIssues |= partRes.reportIssue(is);
            }
        }
    }
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) QueryTypeDescriptorImpl(org.apache.ignite.internal.processors.query.QueryTypeDescriptorImpl) H2TreeIndexBase(org.apache.ignite.internal.processors.query.h2.database.H2TreeIndexBase) Index(org.h2.index.Index) H2CacheRow(org.apache.ignite.internal.processors.query.h2.opt.H2CacheRow) Cursor(org.h2.index.Cursor) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) IgniteException(org.apache.ignite.IgniteException) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) GridQueryProcessor(org.apache.ignite.internal.processors.query.GridQueryProcessor) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) IgniteH2Indexing(org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing)

Aggregations

IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)2 CacheDataRow (org.apache.ignite.internal.processors.cache.persistence.CacheDataRow)2 H2CacheRow (org.apache.ignite.internal.processors.query.h2.opt.H2CacheRow)2 Cursor (org.h2.index.Cursor)2 Index (org.h2.index.Index)2 IgniteException (org.apache.ignite.IgniteException)1 CacheGroupContext (org.apache.ignite.internal.processors.cache.CacheGroupContext)1 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)1 CorruptedTreeException (org.apache.ignite.internal.processors.cache.persistence.tree.CorruptedTreeException)1 GridQueryProcessor (org.apache.ignite.internal.processors.query.GridQueryProcessor)1 QueryTypeDescriptorImpl (org.apache.ignite.internal.processors.query.QueryTypeDescriptorImpl)1 IgniteH2Indexing (org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing)1 H2TreeIndexBase (org.apache.ignite.internal.processors.query.h2.database.H2TreeIndexBase)1 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)1 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)1 Session (org.h2.engine.Session)1 DbException (org.h2.message.DbException)1