Search in sources :

Example 1 with InlineIndex

use of org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndex in project ignite by apache.

the class IndexingDefragmentation method defragmentTable.

/**
 * Defragment one given table.
 */
private boolean defragmentTable(CacheGroupContext newCtx, IntMap<LinkMap> mappingByPartition, CheckpointTimeoutLock cpLock, Runnable cancellationChecker, int pageSize, PageMemoryEx oldCachePageMem, PageMemory newCachePageMemory, long cpLockThreshold, AtomicLong lastCpLockTs, TableIndexes indexes) throws IgniteCheckedException {
    cpLock.checkpointReadLock();
    try {
        TreeIterator treeIterator = new TreeIterator(pageSize);
        GridCacheContext<?, ?> cctx = indexes.cctx;
        cancellationChecker.run();
        for (InlineIndex oldIdx : indexes.idxs) {
            InlineIndexRowHandler oldRowHnd = oldIdx.segment(0).rowHandler();
            SortedIndexDefinition idxDef = (SortedIndexDefinition) indexing.indexDefinition(oldIdx.id());
            InlineIndexImpl newIdx = new DefragIndexFactory(newCtx.offheap(), newCachePageMemory, oldIdx).createIndex(cctx, idxDef).unwrap(InlineIndexImpl.class);
            int segments = oldIdx.segmentsCount();
            for (int i = 0; i < segments; ++i) {
                treeIterator.iterate(oldIdx.segment(i), oldCachePageMem, (theTree, io, pageAddr, idx) -> {
                    cancellationChecker.run();
                    if (System.currentTimeMillis() - lastCpLockTs.get() >= cpLockThreshold) {
                        cpLock.checkpointReadUnlock();
                        cpLock.checkpointReadLock();
                        lastCpLockTs.set(System.currentTimeMillis());
                    }
                    assert 1 == io.getVersion() : "IO version " + io.getVersion() + " is not supported by current defragmentation algorithm." + " Please implement copying of tree in a new format.";
                    BPlusIO<IndexRow> h2IO = DefragIndexFactory.wrap(io, oldRowHnd);
                    IndexRow row = theTree.getRow(h2IO, pageAddr, idx);
                    if (row instanceof DefragIndexRowImpl) {
                        DefragIndexRowImpl r = (DefragIndexRowImpl) row;
                        CacheDataRow cacheDataRow = r.cacheDataRow();
                        int partition = cacheDataRow.partition();
                        long link = r.link();
                        LinkMap map = mappingByPartition.get(partition);
                        long newLink = map.get(link);
                        // Use old row handler, as MetaInfo is copied from old tree.
                        DefragIndexRowImpl newRow = DefragIndexRowImpl.create(oldRowHnd, newLink, r, ((MvccIO) io).storeMvccInfo());
                        newIdx.putIndexRow(newRow);
                    }
                    return true;
                });
            }
        }
        return true;
    } catch (Throwable t) {
        newCtx.cacheObjectContext().kernalContext().failure().process(new FailureContext(CRITICAL_ERROR, t));
        throw t;
    } finally {
        cpLock.checkpointReadUnlock();
    }
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) LinkMap(org.apache.ignite.internal.processors.cache.persistence.defragmentation.LinkMap) SortedIndexDefinition(org.apache.ignite.internal.cache.query.index.sorted.SortedIndexDefinition) IndexRow(org.apache.ignite.internal.cache.query.index.sorted.IndexRow) InlineIndex(org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndex) DefragIndexRowImpl(org.apache.ignite.internal.cache.query.index.sorted.defragmentation.DefragIndexFactory.DefragIndexRowImpl) InlineIndexRowHandler(org.apache.ignite.internal.cache.query.index.sorted.InlineIndexRowHandler) InlineIndexImpl(org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndexImpl) FailureContext(org.apache.ignite.failure.FailureContext) TreeIterator(org.apache.ignite.internal.processors.cache.persistence.defragmentation.TreeIterator)

Example 2 with InlineIndex

use of org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndex in project ignite by apache.

the class IndexProcessor method treeIndexes.

/**
 * Collect indexes for rebuild.
 *
 * @param cctx Cache context.
 * @param createdOnly Get only created indexes (not restored from dick).
 */
public List<InlineIndex> treeIndexes(GridCacheContext cctx, boolean createdOnly) {
    Collection<Index> idxs = indexes(cctx);
    List<InlineIndex> treeIdxs = new ArrayList<>();
    for (Index idx : idxs) {
        if (idx instanceof InlineIndex) {
            InlineIndex idx0 = (InlineIndex) idx;
            if (!createdOnly || idx0.created())
                treeIdxs.add(idx0);
        }
    }
    return treeIdxs;
}
Also used : InlineIndex(org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndex) ArrayList(java.util.ArrayList) InlineIndex(org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndex)

Example 3 with InlineIndex

use of org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndex in project ignite by apache.

the class IgniteH2Indexing method createSortedIndex.

/**
 * Create sorted index.
 *
 * @param name Index name,
 * @param tbl Table.
 * @param pk Primary key flag.
 * @param affinityKey Affinity key flag.
 * @param unwrappedCols Unwrapped index columns for complex types.
 * @param wrappedCols Index columns as is complex types.
 * @param inlineSize Index inline size.
 * @param cacheVisitor whether index created with new cache or on existing one.
 * @return Index.
 */
@SuppressWarnings("ConstantConditions")
GridH2IndexBase createSortedIndex(String name, GridH2Table tbl, boolean pk, boolean affinityKey, List<IndexColumn> unwrappedCols, List<IndexColumn> wrappedCols, int inlineSize, @Nullable SchemaIndexCacheVisitor cacheVisitor) {
    GridCacheContextInfo cacheInfo = tbl.cacheInfo();
    if (log.isDebugEnabled())
        log.debug("Creating cache index [cacheId=" + cacheInfo.cacheId() + ", idxName=" + name + ']');
    if (cacheInfo.affinityNode()) {
        QueryIndexDefinition idxDef = new QueryIndexDefinition(tbl, name, ctx.indexProcessor().rowCacheCleaner(cacheInfo.groupId()), pk, affinityKey, unwrappedCols, wrappedCols, inlineSize);
        org.apache.ignite.internal.cache.query.index.Index index;
        if (cacheVisitor != null)
            index = ctx.indexProcessor().createIndexDynamically(tbl.cacheContext(), idxFactory, idxDef, cacheVisitor);
        else
            index = ctx.indexProcessor().createIndex(tbl.cacheContext(), idxFactory, idxDef);
        InlineIndexImpl queryIndex = index.unwrap(InlineIndexImpl.class);
        return new H2TreeIndex(queryIndex, tbl, unwrappedCols.toArray(new IndexColumn[0]), pk, log);
    } else {
        ClientIndexDefinition d = new ClientIndexDefinition(tbl, new IndexName(tbl.cacheName(), tbl.getSchema().getName(), tbl.getName(), name), unwrappedCols, inlineSize, tbl.cacheInfo().config().getSqlIndexMaxInlineSize());
        org.apache.ignite.internal.cache.query.index.Index index = ctx.indexProcessor().createIndex(tbl.cacheContext(), ClientIndexFactory.INSTANCE, d);
        InlineIndex idx = index.unwrap(InlineIndex.class);
        IndexType idxType = pk ? IndexType.createPrimaryKey(false, false) : IndexType.createNonUnique(false, false, false);
        return new H2TreeClientIndex(idx, tbl, name, unwrappedCols.toArray(new IndexColumn[0]), idxType);
    }
}
Also used : H2TreeIndex(org.apache.ignite.internal.processors.query.h2.database.H2TreeIndex) InlineIndex(org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndex) IndexColumn(org.h2.table.IndexColumn) IndexName(org.apache.ignite.internal.cache.query.index.IndexName) InlineIndexImpl(org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndexImpl) QueryIndexDefinition(org.apache.ignite.internal.processors.query.h2.index.QueryIndexDefinition) GridCacheContextInfo(org.apache.ignite.internal.processors.cache.GridCacheContextInfo) H2TreeClientIndex(org.apache.ignite.internal.processors.query.h2.database.H2TreeClientIndex) ClientIndexDefinition(org.apache.ignite.internal.processors.query.h2.index.client.ClientIndexDefinition) IndexType(org.h2.index.IndexType)

Example 4 with InlineIndex

use of org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndex in project ignite by apache.

the class IndexesRebuildTask method rebuild.

/**
 * Start to rebuild.
 *
 * @param cctx Cache context.
 * @param force Force rebuild indexes.
 * @return A future of rebuilding cache indexes.
 */
@Nullable
public IgniteInternalFuture<?> rebuild(GridCacheContext<?, ?> cctx, boolean force, IndexRebuildCancelToken cancelTok) {
    assert cctx != null;
    if (!CU.affinityNode(cctx.localNode(), cctx.config().getNodeFilter()))
        return null;
    IgnitePageStoreManager pageStore = cctx.shared().pageStore();
    SchemaIndexCacheVisitorClosure clo;
    String cacheName = cctx.name();
    if (pageStore == null || !pageStore.hasIndexStore(cctx.groupId())) {
        boolean mvccEnabled = cctx.mvccEnabled();
        // If there are no index store, rebuild all indexes.
        clo = row -> cctx.queries().store(row, null, mvccEnabled);
    } else {
        Collection<InlineIndex> toRebuild = cctx.kernalContext().indexProcessor().treeIndexes(cctx, !force);
        if (F.isEmpty(toRebuild))
            return null;
        clo = row -> cctx.kernalContext().indexProcessor().store(toRebuild, row, null, false);
    }
    // Closure prepared, do rebuild.
    cctx.kernalContext().query().markAsRebuildNeeded(cctx, true);
    GridFutureAdapter<Void> rebuildCacheIdxFut = new GridFutureAdapter<>();
    // To avoid possible data race.
    GridFutureAdapter<Void> outRebuildCacheIdxFut = new GridFutureAdapter<>();
    IgniteLogger log = cctx.kernalContext().grid().log();
    // An internal future for the ability to cancel index rebuilding.
    SchemaIndexCacheFuture intRebFut = new SchemaIndexCacheFuture(cancelTok);
    SchemaIndexCacheFuture prevIntRebFut = idxRebuildFuts.put(cctx.cacheId(), intRebFut);
    // Check that the previous rebuild is completed.
    assert prevIntRebFut == null;
    cctx.kernalContext().query().onStartRebuildIndexes(cctx);
    rebuildCacheIdxFut.listen(fut -> {
        Throwable err = fut.error();
        if (err == null) {
            try {
                cctx.kernalContext().query().markAsRebuildNeeded(cctx, false);
            } catch (Throwable t) {
                err = t;
            }
        }
        if (err != null)
            U.error(log, "Failed to rebuild indexes for cache: " + cacheName, err);
        else
            cctx.kernalContext().query().onFinishRebuildIndexes(cctx);
        idxRebuildFuts.remove(cctx.cacheId(), intRebFut);
        intRebFut.onDone(err);
        outRebuildCacheIdxFut.onDone(err);
    });
    startRebuild(cctx, rebuildCacheIdxFut, clo, intRebFut.cancelToken());
    return outRebuildCacheIdxFut;
}
Also used : IgnitePageStoreManager(org.apache.ignite.internal.pagemem.store.IgnitePageStoreManager) InlineIndex(org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndex) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) SchemaIndexCacheFuture(org.apache.ignite.internal.processors.query.schema.SchemaIndexCacheFuture) SchemaIndexCacheVisitorClosure(org.apache.ignite.internal.processors.query.schema.SchemaIndexCacheVisitorClosure) IgniteLogger(org.apache.ignite.IgniteLogger) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with InlineIndex

use of org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndex in project ignite by apache.

the class IndexingDefragmentation method tables.

/**
 * Returns collection of table indexes.
 */
private Collection<TableIndexes> tables(CacheGroupContext gctx) {
    Collection<TableIndexes> tables = new ArrayList<>();
    for (GridCacheContext<?, ?> cctx : gctx.caches()) {
        Map<String, TableIndexes> idxs = new HashMap<>();
        List<InlineIndex> indexes = indexing.treeIndexes(cctx, false);
        for (InlineIndex idx : indexes) {
            String table = indexing.indexDefinition(idx.id()).idxName().tableName();
            idxs.putIfAbsent(table, new TableIndexes(cctx, table));
            idxs.get(table).addIndex(idx);
        }
        tables.addAll(idxs.values());
    }
    return tables;
}
Also used : HashMap(java.util.HashMap) InlineIndex(org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndex) ArrayList(java.util.ArrayList)

Aggregations

InlineIndex (org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndex)6 ArrayList (java.util.ArrayList)2 IndexName (org.apache.ignite.internal.cache.query.index.IndexName)2 InlineIndexImpl (org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndexImpl)2 HashMap (java.util.HashMap)1 IgniteLogger (org.apache.ignite.IgniteLogger)1 FailureContext (org.apache.ignite.failure.FailureContext)1 IndexRow (org.apache.ignite.internal.cache.query.index.sorted.IndexRow)1 InlineIndexRowHandler (org.apache.ignite.internal.cache.query.index.sorted.InlineIndexRowHandler)1 SortedIndexDefinition (org.apache.ignite.internal.cache.query.index.sorted.SortedIndexDefinition)1 DefragIndexRowImpl (org.apache.ignite.internal.cache.query.index.sorted.defragmentation.DefragIndexFactory.DefragIndexRowImpl)1 IgnitePageStoreManager (org.apache.ignite.internal.pagemem.store.IgnitePageStoreManager)1 GridCacheContextInfo (org.apache.ignite.internal.processors.cache.GridCacheContextInfo)1 CacheDataRow (org.apache.ignite.internal.processors.cache.persistence.CacheDataRow)1 LinkMap (org.apache.ignite.internal.processors.cache.persistence.defragmentation.LinkMap)1 TreeIterator (org.apache.ignite.internal.processors.cache.persistence.defragmentation.TreeIterator)1 H2TreeClientIndex (org.apache.ignite.internal.processors.query.h2.database.H2TreeClientIndex)1 H2TreeIndex (org.apache.ignite.internal.processors.query.h2.database.H2TreeIndex)1 QueryIndexDefinition (org.apache.ignite.internal.processors.query.h2.index.QueryIndexDefinition)1 ClientIndexDefinition (org.apache.ignite.internal.processors.query.h2.index.client.ClientIndexDefinition)1