ibessonov commented on a change in pull request #7984: URL: https://github.com/apache/ignite/pull/7984#discussion_r524322515
########## File path: modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/defragmentation/IndexingDefragmentation.java ########## @@ -0,0 +1,454 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.processors.query.h2.defragmentation; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.concurrent.atomic.AtomicLong; +import org.apache.ignite.IgniteCheckedException; +import org.apache.ignite.IgniteLogger; +import org.apache.ignite.internal.pagemem.PageMemory; +import org.apache.ignite.internal.processors.cache.CacheGroupContext; +import org.apache.ignite.internal.processors.cache.GridCacheContext; +import org.apache.ignite.internal.processors.cache.persistence.CacheDataRow; +import org.apache.ignite.internal.processors.cache.persistence.CacheDataRowAdapter; +import org.apache.ignite.internal.processors.cache.persistence.checkpoint.CheckpointTimeoutLock; +import org.apache.ignite.internal.processors.cache.persistence.defragmentation.GridQueryIndexingDefragmentation; +import org.apache.ignite.internal.processors.cache.persistence.defragmentation.LinkMap; +import org.apache.ignite.internal.processors.cache.persistence.defragmentation.TimeTracker; +import org.apache.ignite.internal.processors.cache.persistence.defragmentation.TreeIterator; +import org.apache.ignite.internal.processors.cache.persistence.pagemem.PageMemoryEx; +import org.apache.ignite.internal.processors.cache.persistence.tree.BPlusTree; +import org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusIO; +import org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusInnerIO; +import org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusLeafIO; +import org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusMetaIO; +import org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO; +import org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIoResolver; +import org.apache.ignite.internal.processors.cache.persistence.tree.util.InsertLast; +import org.apache.ignite.internal.processors.cache.tree.mvcc.data.MvccDataRow; +import org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing; +import org.apache.ignite.internal.processors.query.h2.database.H2Tree; +import org.apache.ignite.internal.processors.query.h2.database.H2TreeIndex; +import org.apache.ignite.internal.processors.query.h2.database.InlineIndexColumn; +import org.apache.ignite.internal.processors.query.h2.database.inlinecolumn.AbstractInlineIndexColumn; +import org.apache.ignite.internal.processors.query.h2.database.io.AbstractH2ExtrasInnerIO; +import org.apache.ignite.internal.processors.query.h2.database.io.AbstractH2ExtrasLeafIO; +import org.apache.ignite.internal.processors.query.h2.database.io.AbstractH2InnerIO; +import org.apache.ignite.internal.processors.query.h2.database.io.AbstractH2LeafIO; +import org.apache.ignite.internal.processors.query.h2.database.io.H2RowLinkIO; +import org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor; +import org.apache.ignite.internal.processors.query.h2.opt.GridH2Table; +import org.apache.ignite.internal.processors.query.h2.opt.H2CacheRow; +import org.apache.ignite.internal.processors.query.h2.opt.H2Row; +import org.apache.ignite.internal.util.collection.IntMap; +import org.h2.index.Index; +import org.h2.value.Value; + +import static org.apache.ignite.internal.processors.query.h2.defragmentation.IndexingDefragmentation.IndexStages.CP_LOCK; +import static org.apache.ignite.internal.processors.query.h2.defragmentation.IndexingDefragmentation.IndexStages.INIT_TREE; +import static org.apache.ignite.internal.processors.query.h2.defragmentation.IndexingDefragmentation.IndexStages.INSERT_ROW; +import static org.apache.ignite.internal.processors.query.h2.defragmentation.IndexingDefragmentation.IndexStages.ITERATE; +import static org.apache.ignite.internal.processors.query.h2.defragmentation.IndexingDefragmentation.IndexStages.READ_MAP; +import static org.apache.ignite.internal.processors.query.h2.defragmentation.IndexingDefragmentation.IndexStages.READ_ROW; + +/** + * + */ +public class IndexingDefragmentation implements GridQueryIndexingDefragmentation { + /** Indexing. */ + private final IgniteH2Indexing indexing; + + /** Constructor. */ + public IndexingDefragmentation(IgniteH2Indexing indexing) { + this.indexing = indexing; + } + + /** */ + public enum IndexStages { + START, + CP_LOCK, + INIT_TREE, + ITERATE, + READ_ROW, + READ_MAP, + INSERT_ROW + } + + /** {@inheritDoc} */ + @Override public void defragmentate( + CacheGroupContext grpCtx, + CacheGroupContext newCtx, + PageMemoryEx partPageMem, + IntMap<LinkMap> mappingByPartition, + CheckpointTimeoutLock cpLock, + IgniteLogger log + ) throws IgniteCheckedException { + int pageSize = grpCtx.cacheObjectContext().kernalContext().grid().configuration().getDataStorageConfiguration().getPageSize(); + + TreeIterator treeIterator = new TreeIterator(pageSize); + + PageMemoryEx oldCachePageMem = (PageMemoryEx)grpCtx.dataRegion().pageMemory(); + + PageMemory newCachePageMemory = partPageMem; + + Collection<GridH2Table> tables = indexing.schemaManager().dataTables(); + + long cpLockThreshold = 250L; + + TimeTracker<IndexStages> tracker = new TimeTracker<>(IndexStages.class); + + cpLock.checkpointReadLock(); + tracker.complete(CP_LOCK); + + try { + AtomicLong lastCpLockTs = new AtomicLong(System.currentTimeMillis()); + + for (GridH2Table table : tables) { + GridCacheContext<?, ?> cctx = table.cacheContext(); + + if (cctx.groupId() != grpCtx.groupId()) + continue; // Not our index. + + GridH2RowDescriptor rowDesc = table.rowDescriptor(); + + List<Index> indexes = table.getIndexes(); + H2TreeIndex oldH2Idx = (H2TreeIndex)indexes.get(2); + + int segments = oldH2Idx.segmentsCount(); + + H2Tree firstTree = oldH2Idx.treeForRead(0); + + PageIoResolver pageIoRslvr = pageAddr -> { + PageIO io = PageIoResolver.DEFAULT_PAGE_IO_RESOLVER.resolve(pageAddr); + + if (io instanceof BPlusMetaIO) + return io; + + //noinspection unchecked,rawtypes,rawtypes + return wrap((BPlusIO)io); + }; + + //TODO Create new proper GridCacheContext for it? + H2TreeIndex newIdx = H2TreeIndex.createIndex( + cctx, + null, + table, + oldH2Idx.getName(), + firstTree.getPk(), + firstTree.getAffinityKey(), + Arrays.asList(firstTree.cols()), + Arrays.asList(firstTree.cols()), + oldH2Idx.inlineSize(), + segments, + newCachePageMemory, + newCtx.offheap(), + pageIoRslvr, + log + ); + + tracker.complete(INIT_TREE); + + for (int i = 0; i < segments; i++) { + H2Tree tree = oldH2Idx.treeForRead(i); + + treeIterator.iterate(tree, oldCachePageMem, (theTree, io, pageAddr, idx) -> { + tracker.complete(ITERATE); + + if (System.currentTimeMillis() - lastCpLockTs.get() >= cpLockThreshold) { + cpLock.checkpointReadUnlock(); + + cpLock.checkpointReadLock(); + tracker.complete(CP_LOCK); + + lastCpLockTs.set(System.currentTimeMillis()); + } + + assert 1 == io.getVersion(); + + BPlusIO<H2Row> h2IO = wrap(io); + + H2Row row = theTree.getRow(h2IO, pageAddr, idx); + + tracker.complete(READ_ROW); + + if (row instanceof H2CacheRowWithIndex) { + H2CacheRowWithIndex h2CacheRow = (H2CacheRowWithIndex)row; + + CacheDataRow cacheDataRow = h2CacheRow.getRow(); + + int partition = cacheDataRow.partition(); + + long link = h2CacheRow.link(); + + LinkMap map = mappingByPartition.get(partition); + + long newLink = map.get(link); + + tracker.complete(READ_MAP); + + H2CacheRowWithIndex newRow = H2CacheRowWithIndex.create( + rowDesc, + newLink, + h2CacheRow, + ((H2RowLinkIO)io).storeMvccInfo() + ); + + newIdx.putx(newRow); + + tracker.complete(INSERT_ROW); + } + + return true; + }); + } + } + } + finally { + cpLock.checkpointReadUnlock(); + } + + System.out.println(tracker.toString()); Review comment: Yes, I'll get rid of it. It needs to be replaced with a log message. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
