Search in sources :

Example 1 with H2Row

use of org.apache.ignite.internal.processors.query.h2.opt.H2Row 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 H2Row

use of org.apache.ignite.internal.processors.query.h2.opt.H2Row 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)

Example 3 with H2Row

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

the class IgniteH2Indexing method dynamicIndexCreate.

/**
 * {@inheritDoc}
 */
@Override
public void dynamicIndexCreate(final String schemaName, final String tblName, final QueryIndexDescriptorImpl idxDesc, boolean ifNotExists, SchemaIndexCacheVisitor cacheVisitor) throws IgniteCheckedException {
    // Locate table.
    H2Schema schema = schemas.get(schemaName);
    H2TableDescriptor desc = (schema != null ? schema.tableByName(tblName) : null);
    if (desc == null)
        throw new IgniteCheckedException("Table not found in internal H2 database [schemaName=" + schemaName + ", tblName=" + tblName + ']');
    GridH2Table h2Tbl = desc.table();
    // Create index.
    final GridH2IndexBase h2Idx = desc.createUserIndex(idxDesc);
    h2Tbl.proposeUserIndex(h2Idx);
    try {
        // Populate index with existing cache data.
        final GridH2RowDescriptor rowDesc = h2Tbl.rowDescriptor();
        SchemaIndexCacheVisitorClosure clo = new SchemaIndexCacheVisitorClosure() {

            @Override
            public void apply(CacheDataRow row) throws IgniteCheckedException {
                GridH2Row h2Row = rowDesc.createRow(row);
                h2Idx.putx(h2Row);
            }
        };
        cacheVisitor.visit(clo);
        // At this point index is in consistent state, promote it through H2 SQL statement, so that cached
        // prepared statements are re-built.
        String sql = H2Utils.indexCreateSql(desc.fullTableName(), h2Idx, ifNotExists);
        executeSql(schemaName, sql);
    } catch (Exception e) {
        // Rollback and re-throw.
        h2Tbl.rollbackUserIndex(h2Idx.getName());
        throw e;
    }
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) GridH2IndexBase(org.apache.ignite.internal.processors.query.h2.opt.GridH2IndexBase) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) GridH2Row(org.apache.ignite.internal.processors.query.h2.opt.GridH2Row) SchemaIndexCacheVisitorClosure(org.apache.ignite.internal.processors.query.schema.SchemaIndexCacheVisitorClosure) IgniteSystemProperties.getString(org.apache.ignite.IgniteSystemProperties.getString) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SqlParseException(org.apache.ignite.internal.sql.SqlParseException) SQLException(java.sql.SQLException) IgniteException(org.apache.ignite.IgniteException) CacheException(javax.cache.CacheException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException)

Example 4 with H2Row

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

the class GridH2SpatialIndex method findByGeometry.

/**
 * {@inheritDoc}
 */
@Override
public Cursor findByGeometry(TableFilter filter, SearchRow first, SearchRow last, SearchRow intersection) {
    Value v = intersection.getValue(columnIds[0]);
    Geometry g = ((ValueGeometry) v.convertTo(Value.GEOMETRY)).getGeometry();
    int seg = segmentsCount() == 1 ? 0 : H2Utils.context(filter.getSession()).segment();
    GridCursor<IndexRow> cursor = delegate.findByGeometry(seg, filter, g);
    GridCursor<H2Row> h2cursor = new IndexValueCursor<>(cursor, this::mapIndexRow);
    return new H2Cursor(h2cursor);
}
Also used : ValueGeometry(org.h2.value.ValueGeometry) Geometry(org.locationtech.jts.geom.Geometry) ValueGeometry(org.h2.value.ValueGeometry) IndexRow(org.apache.ignite.internal.cache.query.index.sorted.IndexRow) H2Cursor(org.apache.ignite.internal.processors.query.h2.H2Cursor) Value(org.h2.value.Value) IndexValueCursor(org.apache.ignite.internal.cache.query.index.sorted.IndexValueCursor)

Example 5 with H2Row

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

the class H2TreeIndex method findForSegment.

/**
 * Find rows for the segments (distributed joins).
 *
 * @param bounds Bounds.
 * @param segment Segment.
 * @param qryCtx Index query context.
 * @return Iterator.
 */
public Iterator<H2Row> findForSegment(GridH2RowRangeBounds bounds, int segment, IndexQueryContext qryCtx) {
    SearchRow lower = toSearchRow(bounds.first());
    SearchRow upper = toSearchRow(bounds.last());
    T2<IndexRow, IndexRow> key = prepareIndexKeys(lower, upper);
    try {
        GridCursor<IndexRow> range = queryIndex.find(key.get1(), key.get2(), true, true, segment, qryCtx);
        if (range == null)
            range = IndexValueCursor.EMPTY;
        GridCursor<H2Row> h2cursor = new IndexValueCursor<>(range, this::mapIndexRow);
        H2Cursor cur = new H2Cursor(h2cursor);
        return new CursorIteratorWrapper(cur);
    } catch (IgniteCheckedException e) {
        throw DbException.convert(e);
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IndexRow(org.apache.ignite.internal.cache.query.index.sorted.IndexRow) H2Cursor(org.apache.ignite.internal.processors.query.h2.H2Cursor) CursorIteratorWrapper(org.apache.ignite.internal.processors.query.h2.opt.join.CursorIteratorWrapper) SearchRow(org.h2.result.SearchRow) H2Row(org.apache.ignite.internal.processors.query.h2.opt.H2Row) IndexValueCursor(org.apache.ignite.internal.cache.query.index.sorted.IndexValueCursor)

Aggregations

IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 CacheDataRow (org.apache.ignite.internal.processors.cache.persistence.CacheDataRow)4 IgniteException (org.apache.ignite.IgniteException)3 IndexRow (org.apache.ignite.internal.cache.query.index.sorted.IndexRow)3 IndexValueCursor (org.apache.ignite.internal.cache.query.index.sorted.IndexValueCursor)3 H2Cursor (org.apache.ignite.internal.processors.query.h2.H2Cursor)3 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)3 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)3 H2Row (org.apache.ignite.internal.processors.query.h2.opt.H2Row)3 CacheGroupContext (org.apache.ignite.internal.processors.cache.CacheGroupContext)2 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)2 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)2 H2CacheRow (org.apache.ignite.internal.processors.query.h2.opt.H2CacheRow)2 Cursor (org.h2.index.Cursor)2 Index (org.h2.index.Index)2 SQLException (java.sql.SQLException)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1