sashapolo commented on code in PR #2030:
URL: https://github.com/apache/ignite-3/pull/2030#discussion_r1197681502


##########
modules/replicator/src/main/java/org/apache/ignite/internal/replicator/ReplicaManager.java:
##########
@@ -382,8 +382,17 @@ public boolean stopReplica(ReplicationGroupId 
replicaGrpId) throws NodeStoppingE
      * @param replicaGrpId Replication group id.
      * @return True if the replica is found and closed, false otherwise.
      */
+    // TODO: IGNITE-19494 We need to correctly stop the replica

Review Comment:
   To quote the ticket:
   
   ```
   I think that this is not right, and we should wait for it or something and 
only after that stop it.
   Maybe I don't understand something and then we'll just close the ticket.
   ```
   
   Please ask @sanpwc or someone from his team what is the correct course of 
action, I would like to see a more detailed description of the problem. We 
didn't wait for this future before and everything was ok.



##########
modules/table/src/main/java/org/apache/ignite/internal/table/distributed/index/IndexBuilder.java:
##########
@@ -0,0 +1,186 @@
+/*
+ * 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.table.distributed.index;
+
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import org.apache.ignite.internal.close.ManuallyCloseable;
+import org.apache.ignite.internal.logger.IgniteLogger;
+import org.apache.ignite.internal.logger.Loggers;
+import org.apache.ignite.internal.raft.service.RaftGroupService;
+import org.apache.ignite.internal.storage.MvPartitionStorage;
+import org.apache.ignite.internal.storage.index.IndexStorage;
+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.internal.util.IgniteUtils;
+
+/**
+ * Class for managing the building of table indexes.
+ */
+public class IndexBuilder implements ManuallyCloseable {
+    private static final IgniteLogger LOG = 
Loggers.forClass(IndexBuilder.class);
+
+    private static final int BATCH_SIZE = 100;
+
+    private final ExecutorService executor;
+
+    private final Map<IndexBuildTaskId, IndexBuildTask> indexBuildTaskById = 
new ConcurrentHashMap<>();
+
+    private final IgniteSpinBusyLock busyLock = new IgniteSpinBusyLock();
+
+    private final AtomicBoolean closeGuard = new AtomicBoolean();
+
+    /**
+     * Constructor.
+     *
+     * @param nodeName Node name.
+     * @param threadCount Number of threads to build indexes.
+     */
+    public IndexBuilder(String nodeName, int threadCount) {
+        executor = new ThreadPoolExecutor(
+                threadCount,
+                threadCount,
+                30,
+                TimeUnit.SECONDS,
+                new LinkedBlockingQueue<>(),
+                NamedThreadFactory.create(nodeName, "build-index", LOG)
+        );
+    }
+
+    /**
+     * Starts building the index if it is not already built or is not yet in 
progress.
+     *
+     * <p>Index is built in batches using {@link BuildIndexCommand} (via 
raft), batches are sent sequentially.
+     *
+     * <p>It is expected that the index building is triggered by the leader of 
the raft group.
+     *
+     * @param tableId Table ID.
+     * @param partitionId Partition ID.
+     * @param indexId Index ID.
+     * @param indexStorage Index storage to build.
+     * @param partitionStorage Multi-versioned partition storage.
+     * @param raftClient Raft client.
+     */
+    // TODO: IGNITE-19498 Perhaps we need to start building the index only once
+    public void startBuildIndex(
+            UUID tableId,
+            int partitionId,
+            UUID indexId,
+            IndexStorage indexStorage,
+            MvPartitionStorage partitionStorage,
+            RaftGroupService raftClient
+    ) {
+        inBusyLock(() -> {
+            if (indexStorage.getNextRowIdToBuild() == null) {
+                return;
+            }
+
+            IndexBuildTaskId taskId = new IndexBuildTaskId(tableId, 
partitionId, indexId);
+
+            IndexBuildTask newTask = new IndexBuildTask(taskId, indexStorage, 
partitionStorage, raftClient, executor, busyLock, BATCH_SIZE);
+
+            IndexBuildTask previousTask = 
indexBuildTaskById.putIfAbsent(taskId, newTask);
+
+            if (previousTask != null) {
+                // Index building is already in progress.
+                return;
+            }
+
+            newTask.start();
+
+            newTask.getTaskFuture().whenComplete((unused, throwable) -> 
indexBuildTaskById.remove(taskId));
+        });
+    }
+
+    /**
+     * Stops index building if it is in progress.
+     *
+     * @param tableId Table ID.
+     * @param partitionId Partition ID.
+     * @param indexId Index ID.
+     */
+    public void stopBuildIndex(UUID tableId, int partitionId, UUID indexId) {
+        inBusyLock(() -> {
+            IndexBuildTask removed = indexBuildTaskById.remove(new 
IndexBuildTaskId(tableId, partitionId, indexId));
+
+            if (removed != null) {
+                removed.stop();
+            }
+        });
+    }
+
+    /**
+     * Stops building all indexes (for a table partition) if they are in 
progress.
+     *
+     * @param tableId Table ID.
+     * @param partitionId Partition ID.
+     */
+    public void stopBuildIndexes(UUID tableId, int partitionId) {
+        for (Iterator<Entry<IndexBuildTaskId, IndexBuildTask>> it = 
indexBuildTaskById.entrySet().iterator(); it.hasNext(); ) {
+            if (!busyLock.enterBusy()) {
+                return;
+            }
+
+            try {
+                Entry<IndexBuildTaskId, IndexBuildTask> entry = it.next();
+
+                IndexBuildTaskId taskId = entry.getKey();
+
+                if (tableId.equals(taskId.getTableId()) && partitionId == 
taskId.getPartitionId()) {
+                    it.remove();
+
+                    entry.getValue().stop();
+                }
+            } finally {
+                busyLock.leaveBusy();
+            }
+        }
+    }
+
+    @Override
+    public void close() {
+        if (!closeGuard.compareAndSet(false, true)) {
+            return;
+        }
+
+        busyLock.block();

Review Comment:
   Do you mean that `stopBuildIndex` will be called externally before stopping 
the `IndexBuilder` itself? I would suggest to add an assertion then



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