ibessonov commented on code in PR #1800:
URL: https://github.com/apache/ignite-3/pull/1800#discussion_r1154104429


##########
modules/index/src/main/java/org/apache/ignite/internal/index/IndexBuilder.java:
##########
@@ -0,0 +1,273 @@
+/*
+ * 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.concurrent.CompletableFuture.completedFuture;
+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.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;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * 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, null));
+        }
+    }
+
+    /**
+     * 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;
+
+        /**
+         * ID of the next row to build the index from the previous batch, 
{@code null} if it is the first row after the index was crated
+         * (both on a live node and after a restore).
+         */
+        private final @Nullable RowId nextRowIdToBuiltFromPreviousBatch;
+
+        private BuildIndexTask(
+                TableImpl table,
+                TableIndexView tableIndexView,
+                int partitionId,
+                @Nullable RowId nextRowIdToBuiltFromPreviousBatch
+        ) {
+            this.table = table;
+            this.tableIndexView = tableIndexView;
+            this.partitionId = partitionId;
+            this.nextRowIdToBuiltFromPreviousBatch = 
nextRowIdToBuiltFromPreviousBatch;
+        }
+
+        @Override
+        public void run() {
+            if (!busyLock.enterBusy()) {
+                return;
+            }
+
+            try {
+                // At the time of creating the index, we should have already 
waited for the table to be created and its raft of clients
+                // (services) to start for all partitions, so there should be 
no errors.
+                RaftGroupService raftGroupService = 
table.internalTable().partitionRaftGroupService(partitionId);
+
+                raftGroupService
+                        // We do not check the presence of nodes in the 
topology on purpose, so as not to get into races on
+                        // rebalancing, it will be more convenient and 
reliable for us to wait for a stable topology with a chosen
+                        // leader.
+                        .refreshAndGetLeaderWithTerm()
+                        .thenComposeAsync(leaderWithTerm -> {
+                            if (!busyLock.enterBusy()) {
+                                return completedFuture(null);
+                            }
+
+                            try {
+                                // At this point, we have a stable topology, 
each node of which has already applied all local updates.
+                                if 
(!localNodeConsistentId().equals(leaderWithTerm.leader().consistentId())) {
+                                    // TODO: IGNITE-19053 Must handle the 
change of leader
+                                    // TODO: IGNITE-19053 Add a test to change 
the leader even at the start of the task
+                                    return completedFuture(null);
+                                }
+
+                                List<RowId> batchRowIds = collectRowIdBatch();
+
+                                RowId nextRowId = 
getNextRowIdForNextBatch(batchRowIds);
+
+                                boolean finish = batchRowIds.size() < 
BUILD_INDEX_ROW_ID_BATCH_SIZE || nextRowId == null;
+
+                                // TODO: IGNITE-19053 Must handle the change 
of leader
+                                return 
raftGroupService.run(createBuildIndexCommand(batchRowIds, finish))
+                                        .thenRun(() -> {
+                                            if (!finish) {
+                                                assert nextRowId != null : 
createCommonTableIndexInfo();
+
+                                                buildIndexExecutor.submit(
+                                                        new 
BuildIndexTask(table, tableIndexView, partitionId, nextRowId)

Review Comment:
   Because I don't understand the need to create a new object, if we already 
have one



-- 
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]

Reply via email to