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();
}
}
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;
}
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);
}
}
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;
}
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;
}
Aggregations