tkalkirill commented on code in PR #1800: URL: https://github.com/apache/ignite-3/pull/1800#discussion_r1145967690
########## modules/index/src/main/java/org/apache/ignite/internal/index/IndexBuilder.java: ########## @@ -0,0 +1,220 @@ +/* + * 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.index; + +import static java.util.stream.Collectors.toList; +import static org.apache.ignite.internal.util.IgniteUtils.shutdownAndAwaitTermination; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import org.apache.ignite.internal.logger.IgniteLogger; +import org.apache.ignite.internal.logger.Loggers; +import org.apache.ignite.internal.raft.Peer; +import org.apache.ignite.internal.raft.service.RaftGroupService; +import org.apache.ignite.internal.schema.configuration.index.TableIndexView; +import org.apache.ignite.internal.storage.MvPartitionStorage; +import org.apache.ignite.internal.storage.RowId; +import org.apache.ignite.internal.table.InternalTable; +import org.apache.ignite.internal.table.TableImpl; +import org.apache.ignite.internal.table.distributed.TableMessagesFactory; +import org.apache.ignite.internal.table.distributed.command.BuildIndexCommand; +import org.apache.ignite.internal.thread.NamedThreadFactory; +import org.apache.ignite.internal.util.IgniteSpinBusyLock; +import org.apache.ignite.network.ClusterService; + +/** + * Class for managing the index building process. + */ +class IndexBuilder { + private static final IgniteLogger LOG = Loggers.forClass(IndexBuilder.class); + + /** Batch size of row IDs to build the index. */ + private static final int BUILD_INDEX_ROW_ID_BATCH_SIZE = 100; + + /** Message factory to create messages - RAFT commands. */ + private static final TableMessagesFactory TABLE_MESSAGES_FACTORY = new TableMessagesFactory(); + + /** Busy lock to stop synchronously. */ + private final IgniteSpinBusyLock busyLock; + + /** Cluster service. */ + private final ClusterService clusterService; + + /** Index building executor. */ + private final ExecutorService buildIndexExecutor; + + IndexBuilder(String nodeName, IgniteSpinBusyLock busyLock, ClusterService clusterService) { + this.busyLock = busyLock; + this.clusterService = clusterService; + + int cpus = Runtime.getRuntime().availableProcessors(); + + buildIndexExecutor = new ThreadPoolExecutor( + cpus, + cpus, + 30, + TimeUnit.SECONDS, + new LinkedBlockingQueue<>(), + NamedThreadFactory.create(nodeName, "build-index", LOG) + ); + } + + /** + * Stops the index builder. + */ + void stop() { + shutdownAndAwaitTermination(buildIndexExecutor, 10, TimeUnit.SECONDS); + } + + /** + * Initializes the build of the index. + */ + void startIndexBuild(TableIndexView tableIndexView, TableImpl table) { + for (int partitionId = 0; partitionId < table.internalTable().partitions(); partitionId++) { + buildIndexExecutor.submit(new BuildIndexTask(table, tableIndexView, partitionId, true)); + } + } + + /** + * Task of building a table index for a partition. + * + * <p>Only the leader of the raft group will manage the building of the index. Leader sends batches of row IDs via + * {@link BuildIndexCommand}, the next batch will only be send after the previous batch has been processed. + * + * <p>Index building itself occurs locally on each node of the raft group when processing {@link BuildIndexCommand}. This ensures that + * the index build process in the raft group is consistent and that the index build process is restored after restarting the raft group + * (not from the beginning). + */ + private class BuildIndexTask implements Runnable { + private final TableImpl table; + + private final TableIndexView tableIndexView; + + private final int partitionId; + + private final boolean firstBatch; + + private BuildIndexTask(TableImpl table, TableIndexView tableIndexView, int partitionId, boolean firstBatch) { + this.table = table; + this.tableIndexView = tableIndexView; + this.partitionId = partitionId; + this.firstBatch = firstBatch; + } + + @Override + public void run() { + if (!busyLock.enterBusy()) { + return; + } + + try { + InternalTable internalTable = table.internalTable(); + + RaftGroupService raftGroupService = internalTable.partitionRaftGroupService(partitionId); Review Comment: `org.apache.ignite.internal.table.distributed.storage.InternalTableImpl#partitionRaftGroupService` throws an `IgniteInternalException` if not found (**null**). -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
