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


##########
modules/index/src/main/java/org/apache/ignite/internal/index/IndexTaskManager.java:
##########
@@ -0,0 +1,207 @@
+/*
+ * 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.failedFuture;
+import static 
org.apache.ignite.internal.index.IndexManagementUtils.isLocalNode;
+import static 
org.apache.ignite.internal.util.CompletableFutures.falseCompletedFuture;
+import static org.apache.ignite.internal.util.IgniteUtils.inBusyLock;
+
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.Consumer;
+import org.apache.ignite.internal.catalog.CatalogManager;
+import org.apache.ignite.internal.catalog.CatalogService;
+import org.apache.ignite.internal.catalog.descriptors.CatalogIndexDescriptor;
+import org.apache.ignite.internal.catalog.descriptors.CatalogIndexStatus;
+import org.apache.ignite.internal.catalog.events.CatalogEvent;
+import org.apache.ignite.internal.catalog.events.CreateIndexEventParameters;
+import org.apache.ignite.internal.catalog.events.RemoveIndexEventParameters;
+import org.apache.ignite.internal.catalog.events.StoppingIndexEventParameters;
+import org.apache.ignite.internal.close.ManuallyCloseable;
+import org.apache.ignite.internal.event.EventListener;
+import org.apache.ignite.internal.event.EventParameters;
+import org.apache.ignite.internal.network.ClusterService;
+import org.apache.ignite.internal.placementdriver.PlacementDriver;
+import org.apache.ignite.internal.placementdriver.ReplicaMeta;
+import org.apache.ignite.internal.placementdriver.event.PrimaryReplicaEvent;
+import 
org.apache.ignite.internal.placementdriver.event.PrimaryReplicaEventParameters;
+import org.apache.ignite.internal.replicator.TablePartitionId;
+import org.apache.ignite.internal.util.IgniteSpinBusyLock;
+
+/**
+ * Component that reacts to certain Catalog changes and starts or stops 
corresponding {@link ChangeIndexStatusTask}s via the
+ * {@link ChangeIndexStatusTaskScheduler}.
+ *
+ * <p>Tasks will be started only if the local node is the primary replica for 
the {@code 0} partition of the table which indexes have been
+ * modified in the Catalog.
+ *
+ * <p>The following events are being monitored:
+ * <ul>
+ *     <li>{@link CatalogEvent#INDEX_CREATE} - when an index is created, a 
task will be started to move the index to the
+ *     {@link CatalogIndexStatus#BUILDING} state; </li>
+ *     <li>{@link CatalogEvent#INDEX_STOPPING} - when an index is dropped, a 
task will be started to remove the index from the Catalog;</li>
+ *     <li>{@link CatalogEvent#INDEX_REMOVED} - when an index is removed from 
the Catalog, all ongoing tasks for the index will be stopped;
+ *     </li>
+ *     <li>{@link PrimaryReplicaEvent#PRIMARY_REPLICA_ELECTED} - if the local 
node has become the primary replica for partition {@code 0} of
+ *     the table, it starts corresponding tasks depending on the current 
status of indices in the Catalog. If the local node stops being the
+ *     primary replica for the partition {@code 0}, it stops all tasks that 
belong to indices of the table that the partition belongs to.
+ *     </li>
+ * </ul>
+ *
+ * <p>On node recovery, tasks will be started on {@link 
PrimaryReplicaEvent#PRIMARY_REPLICA_ELECTED}, which will fire due to a change in
+ * the {@link ReplicaMeta#getLeaseholderId()} on node restart but after {@link 
ReplicaMeta#getExpirationTime()}.
+ */
+class IndexTaskManager implements ManuallyCloseable {
+    private final CatalogService catalogService;
+
+    private final PlacementDriver placementDriver;
+
+    private final ClusterService clusterService;
+
+    private final ChangeIndexStatusTaskScheduler 
changeIndexStatusTaskScheduler;
+
+    /** Tables IDs for which the local node is the primary replica for the 
partition with ID {@code 0}. */
+    private final Set<Integer> localNodeIsPrimaryReplicaForTableIds = 
ConcurrentHashMap.newKeySet();
+
+    private final IgniteSpinBusyLock busyLock = new IgniteSpinBusyLock();
+
+    private final AtomicBoolean closeGuard = new AtomicBoolean();
+
+    IndexTaskManager(
+            CatalogManager catalogManager,
+            PlacementDriver placementDriver,
+            ClusterService clusterService,
+            ChangeIndexStatusTaskScheduler changeIndexStatusTaskScheduler
+    ) {
+        this.catalogService = catalogManager;
+        this.placementDriver = placementDriver;
+        this.clusterService = clusterService;
+        this.changeIndexStatusTaskScheduler = changeIndexStatusTaskScheduler;
+
+        addListeners();
+    }
+
+    @Override
+    public void close() throws Exception {
+        if (!closeGuard.compareAndSet(false, true)) {
+            return;
+        }
+
+        busyLock.block();
+
+        changeIndexStatusTaskScheduler.close();
+    }
+
+    private void addListeners() {
+        catalogService.listen(CatalogEvent.INDEX_CREATE, 
adaptToEventListener(this::onIndexCreated));
+
+        catalogService.listen(CatalogEvent.INDEX_STOPPING, 
adaptToEventListener(this::onIndexDropped));
+
+        catalogService.listen(CatalogEvent.INDEX_REMOVED, 
adaptToEventListener(this::onIndexRemoved));
+
+        placementDriver.listen(PrimaryReplicaEvent.PRIMARY_REPLICA_ELECTED, 
adaptToEventListener(this::onPrimaryReplicaElected));
+    }
+
+    /** Wraps a given callback into an EventListener. */
+    private <T extends EventParameters> EventListener<T> 
adaptToEventListener(Consumer<T> action) {

Review Comment:
   Created a static method in `EventListener`



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