You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by "valepakh (via GitHub)" <gi...@apache.org> on 2023/06/26 08:52:33 UTC

[GitHub] [ignite-3] valepakh commented on a diff in pull request #2219: IGNITE-19708 Check refcounter of unit before undeploy

valepakh commented on code in PR #2219:
URL: https://github.com/apache/ignite-3/pull/2219#discussion_r1241835616


##########
modules/code-deployment/src/main/java/org/apache/ignite/internal/deployunit/DefaultNodeCallback.java:
##########
@@ -53,20 +56,23 @@ public class DefaultNodeCallback extends NodeEventCallback {
      *
      * @param deploymentUnitStore Deployment units store.
      * @param messaging Deployment messaging service.
+     * @param undeployer Deployment unit undeployer.
      * @param deployer Deployment unit file system service.
      * @param cmgManager Cluster management group manager.
      * @param nodeName Node consistent ID.
      */
     public DefaultNodeCallback(
             DeploymentUnitStore deploymentUnitStore,
             DeployMessagingService messaging,
+            DeploymentUnitUndeployer undeployer,

Review Comment:
   Could you please move the `undeployer` parameter after the `deployer`?



##########
modules/code-deployment/src/main/java/org/apache/ignite/internal/deployunit/DeploymentUnitUndeployer.java:
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.deployunit;
+
+import java.util.Deque;
+import java.util.concurrent.ConcurrentLinkedDeque;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.function.Consumer;
+import org.apache.ignite.compute.DeploymentUnit;
+import org.apache.ignite.internal.logger.IgniteLogger;
+import org.apache.ignite.internal.logger.Loggers;
+import org.apache.ignite.internal.thread.NamedThreadFactory;
+
+/**
+ * Undeploys deployment units that are not acquired by any class loader. This class is thread-safe.
+ */
+class DeploymentUnitUndeployer {
+    private static final IgniteLogger LOG = Loggers.forClass(DeploymentUnitUndeployer.class);
+
+    /** Deployment units to undeploy. */
+    private final Deque<DeploymentUnit> unitsToUndeploy = new ConcurrentLinkedDeque<>();
+
+    /** Deployment unit accessor. */
+    private final DeploymentUnitAccessor deploymentUnitAccessor;
+
+    /** Executor. */
+    private final ScheduledExecutorService executor;
+
+    /** Undeploy function. */
+    private final Consumer<DeploymentUnit> undeploy;
+
+    /**
+     * Creates undeployer.
+     *
+     * @param nodeName node name.
+     * @param deploymentUnitAccessor deployment unit accessor.
+     * @param undeploy undeploy function.
+     */
+    DeploymentUnitUndeployer(
+            String nodeName,
+            DeploymentUnitAccessor deploymentUnitAccessor,
+            Consumer<DeploymentUnit> undeploy) {
+        this.deploymentUnitAccessor = deploymentUnitAccessor;
+        this.executor = Executors.newScheduledThreadPool(
+                1, NamedThreadFactory.create(nodeName, "deployment-unit-undeployer", LOG));
+        this.undeploy = undeploy;
+    }
+
+    /**
+     * Starts the undeployer.
+     *
+     * @param delay delay between undeploy attempts.
+     * @param unit time unit of the delay.
+     */
+    public void start(long delay, TimeUnit unit) {
+        executor.scheduleWithFixedDelay(this::undeployUnits, 0, delay, unit);
+    }
+
+    /**
+     * Stops the undeployer.
+     */
+    public void stop() {
+        executor.shutdown();
+    }
+
+    /**
+     * Undeploys deployment units that are not acquired by any class loader.
+     * If a deployment unit is acquired, it is put back to the queue.
+     */
+    private void undeployUnits() {
+        int i = 0;
+        while (i < unitsToUndeploy.size()) {
+            DeploymentUnit unit = unitsToUndeploy.poll();
+            if (!deploymentUnitAccessor.isAcquired(unit)) {
+                undeploy.accept(unit);
+            } else {
+                unitsToUndeploy.offer(unit);
+            }
+        }
+    }
+
+    /**
+     * Adds a deployment unit to the queue of units to undeploy.
+     *
+     * @param unit deployment unit to undeploy.
+     */
+    public void undeploy(DeploymentUnit unit) {
+        unitsToUndeploy.offer(unit);

Review Comment:
   Given the chosen time delay of 5 seconds, let's first check that the unit is not used right now and add it to the queue only if it is.



##########
modules/runner/src/integrationTest/java/org/apache/ignite/internal/compute/ItComputeTestStandalone.java:
##########
@@ -115,20 +118,58 @@ void executesJobWithLatestUnitVersion() throws IOException {
         IgniteImpl entryNode = node(0);
 
         DeploymentUnit firstVersion = new DeploymentUnit("latest-unit", Version.parseVersion("1.0.0"));
-        deployJar(entryNode, firstVersion.name(), firstVersion.version(), "unit1-1.0-SNAPSHOT.jar");
+        deployJar(entryNode, firstVersion.name(), firstVersion.version(), "ignite-ut-job1-1.0-SNAPSHOT.jar");
 
         CompletableFuture<Integer> result1 = entryNode.compute()
                 .execute(Set.of(entryNode.node()), jobUnits, "org.my.job.compute.unit.UnitJob");
         assertThat(result1, willBe(1));
 
         DeploymentUnit secondVersion = new DeploymentUnit("latest-unit", Version.parseVersion("1.0.1"));
-        deployJar(entryNode, secondVersion.name(), secondVersion.version(), "unit2-1.0-SNAPSHOT.jar");
+        deployJar(entryNode, secondVersion.name(), secondVersion.version(), "ignite-ut-job2-1.0-SNAPSHOT.jar");
 
         CompletableFuture<String> result2 = entryNode.compute()
                 .execute(Set.of(entryNode.node()), jobUnits, "org.my.job.compute.unit.UnitJob");
         assertThat(result2, willBe("Hello World!"));
     }
 
+    @Test
+    void undeployAcquiredUnit() {
+        IgniteImpl entryNode = node(0);
+        CompletableFuture<Void> job = entryNode.compute().execute(Set.of(entryNode.node()), units, "org.example.SleepJob", 3L);
+
+        assertThat(entryNode.deployment().undeployAsync(unit.name(), unit.version()), willCompleteSuccessfully());
+
+        assertThat(entryNode.deployment().clusterStatusAsync(unit.name(), unit.version()), willBe(OBSOLETE));
+        assertThat(entryNode.deployment().nodeStatusAsync(unit.name(), unit.version()), willBe(OBSOLETE));
+
+        await().failFast("The unit must not be removed until the job is completed", () -> {
+            assertThat(entryNode.deployment().clusterStatusAsync(unit.name(), unit.version()), willBe(OBSOLETE));
+            assertThat(entryNode.deployment().nodeStatusAsync(unit.name(), unit.version()), willBe(OBSOLETE));
+        }).until(() -> job, willCompleteSuccessfully());
+
+        await().until(
+                () -> entryNode.deployment().clusterStatusAsync(unit.name(), unit.version()),
+                willBe(nullValue())
+        );
+    }
+
+    @Test
+    void executeJonWithObsoleteUnit() {

Review Comment:
   ```suggestion
       void executeJobWithObsoleteUnit() {
   ```



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org