You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airavata.apache.org by ma...@apache.org on 2020/03/25 19:46:43 UTC

[airavata] 02/04: AIRAVATA-3126 Update logic to get active reservation

This is an automated email from the ASF dual-hosted git repository.

machristie pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/airavata.git

commit d698f9b57efa7aad900bca7deb1a6fad648d7202
Author: Marcus Christie <ma...@apache.org>
AuthorDate: Sun Mar 8 16:32:32 2020 -0400

    AIRAVATA-3126 Update logic to get active reservation
---
 airavata-api/airavata-model-utils/pom.xml          |   6 ++
 .../util/GroupComputeResourcePreferenceUtil.java   |  44 ++++++++
 .../GroupComputeResourcePreferenceUtilTest.java    | 116 +++++++++++++++++++++
 .../airavata/helix/impl/task/TaskContext.java      |   9 ++
 4 files changed, 175 insertions(+)

diff --git a/airavata-api/airavata-model-utils/pom.xml b/airavata-api/airavata-model-utils/pom.xml
index 67eeaed..51bace8 100644
--- a/airavata-api/airavata-model-utils/pom.xml
+++ b/airavata-api/airavata-model-utils/pom.xml
@@ -43,6 +43,12 @@
             <artifactId>airavata-data-models</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>${junit.version}</version>
+            <scope>test</scope>
+        </dependency>
 
     </dependencies>
 
diff --git a/airavata-api/airavata-model-utils/src/main/java/org/apache/airavata/model/util/GroupComputeResourcePreferenceUtil.java b/airavata-api/airavata-model-utils/src/main/java/org/apache/airavata/model/util/GroupComputeResourcePreferenceUtil.java
new file mode 100644
index 0000000..58731e9
--- /dev/null
+++ b/airavata-api/airavata-model-utils/src/main/java/org/apache/airavata/model/util/GroupComputeResourcePreferenceUtil.java
@@ -0,0 +1,44 @@
+/*
+ *
+ * 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.airavata.model.util;
+
+import org.apache.airavata.model.appcatalog.groupresourceprofile.ComputeResourceReservation;
+import org.apache.airavata.model.appcatalog.groupresourceprofile.GroupComputeResourcePreference;
+
+public class GroupComputeResourcePreferenceUtil {
+
+    public static ComputeResourceReservation getActiveReservationForQueue(
+            GroupComputeResourcePreference groupComputeResourcePreference, String queueName) {
+
+        long now = System.currentTimeMillis();
+        if (groupComputeResourcePreference.getReservationsSize() > 0) {
+            for (ComputeResourceReservation reservation : groupComputeResourcePreference.getReservations()) {
+
+                if (reservation.getQueueNames().contains(queueName) && now > reservation.getStartTime()
+                        && now < reservation.getEndTime()) {
+                    return reservation;
+                }
+            }
+        }
+        return null;
+    }
+}
diff --git a/airavata-api/airavata-model-utils/src/test/java/org/apache/airavata/model/util/GroupComputeResourcePreferenceUtilTest.java b/airavata-api/airavata-model-utils/src/test/java/org/apache/airavata/model/util/GroupComputeResourcePreferenceUtilTest.java
new file mode 100644
index 0000000..7f9bedf
--- /dev/null
+++ b/airavata-api/airavata-model-utils/src/test/java/org/apache/airavata/model/util/GroupComputeResourcePreferenceUtilTest.java
@@ -0,0 +1,116 @@
+/*
+ *
+ * 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.airavata.model.util;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.airavata.model.appcatalog.groupresourceprofile.ComputeResourceReservation;
+import org.apache.airavata.model.appcatalog.groupresourceprofile.GroupComputeResourcePreference;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * GroupComputeResourcePreferenceUtilTest
+ */
+public class GroupComputeResourcePreferenceUtilTest {
+
+    @Test
+    public void testGetActiveReservationForQueue() {
+
+        final GroupComputeResourcePreference pref = new GroupComputeResourcePreference();
+        final ComputeResourceReservation res1 = new ComputeResourceReservation("id1", "res1",
+                Arrays.asList("cpu", "gpu"), System.currentTimeMillis() - 10000, System.currentTimeMillis() + 10000);
+        pref.addToReservations(res1);
+
+        final ComputeResourceReservation result = GroupComputeResourcePreferenceUtil.getActiveReservationForQueue(pref,
+                "cpu");
+
+        Assert.assertSame(res1, result);
+    }
+
+    @Test
+    public void testGetActiveReservationForQueueWhenNoReservations() {
+
+        final GroupComputeResourcePreference pref = new GroupComputeResourcePreference();
+
+        final ComputeResourceReservation result = GroupComputeResourcePreferenceUtil.getActiveReservationForQueue(pref,
+                "cpu");
+
+        Assert.assertNull(result);
+    }
+
+    @Test
+    public void testGetActiveReservationForQueueWhenReservationIsExpired() {
+
+        final GroupComputeResourcePreference pref = new GroupComputeResourcePreference();
+        final ComputeResourceReservation res1 = new ComputeResourceReservation("id1", "res1",
+                Arrays.asList("cpu", "gpu"), System.currentTimeMillis() - 20000, System.currentTimeMillis() - 10000);
+        pref.addToReservations(res1);
+
+        final ComputeResourceReservation result = GroupComputeResourcePreferenceUtil.getActiveReservationForQueue(pref,
+                "cpu");
+
+        Assert.assertNull(result);
+    }
+
+    @Test
+    public void testGetActiveReservationForQueueWhenReservationActiveButWrongQueue() {
+
+        final GroupComputeResourcePreference pref = new GroupComputeResourcePreference();
+        final ComputeResourceReservation res1 = new ComputeResourceReservation("id1", "res1",
+                Arrays.asList("cpu", "gpu"), System.currentTimeMillis() - 10000, System.currentTimeMillis() + 10000);
+        pref.addToReservations(res1);
+
+        final ComputeResourceReservation result = GroupComputeResourcePreferenceUtil.getActiveReservationForQueue(pref,
+                "thirdqueue");
+
+        Assert.assertNull(result);
+    }
+
+    @Test
+    public void testGetActiveReservationWithRandomOrder() {
+
+        final GroupComputeResourcePreference pref = new GroupComputeResourcePreference();
+        final ComputeResourceReservation res1 = new ComputeResourceReservation("id1", "res1",
+                Arrays.asList("cpu", "gpu"), System.currentTimeMillis() - 10000, System.currentTimeMillis() + 10000);
+        // expired
+        final ComputeResourceReservation res2 = new ComputeResourceReservation("id2", "res2",
+                Arrays.asList("cpu", "gpu"), System.currentTimeMillis() - 20000, System.currentTimeMillis() - 10000);
+        // future
+        final ComputeResourceReservation res3 = new ComputeResourceReservation("id3", "res3",
+                Arrays.asList("cpu", "gpu"), System.currentTimeMillis() + 10000, System.currentTimeMillis() + 20000);
+        // wrong queue
+        final ComputeResourceReservation res4 = new ComputeResourceReservation("id3", "res3",
+                Arrays.asList("shared", "compute"), System.currentTimeMillis() + 10000, System.currentTimeMillis() + 20000);
+        final List<ComputeResourceReservation> reservations = Arrays.asList(res1, res2, res3, res4);
+
+        Collections.shuffle(reservations);
+        pref.setReservations(reservations);
+
+        final ComputeResourceReservation result = GroupComputeResourcePreferenceUtil.getActiveReservationForQueue(pref,
+                "cpu");
+
+        Assert.assertSame(res1, result);
+    }
+}
diff --git a/modules/airavata-helix/helix-spectator/src/main/java/org/apache/airavata/helix/impl/task/TaskContext.java b/modules/airavata-helix/helix-spectator/src/main/java/org/apache/airavata/helix/impl/task/TaskContext.java
index ab323b0..e57fa1e 100644
--- a/modules/airavata-helix/helix-spectator/src/main/java/org/apache/airavata/helix/impl/task/TaskContext.java
+++ b/modules/airavata-helix/helix-spectator/src/main/java/org/apache/airavata/helix/impl/task/TaskContext.java
@@ -33,6 +33,7 @@ import org.apache.airavata.model.appcatalog.computeresource.ResourceJobManager;
 import org.apache.airavata.model.appcatalog.computeresource.SSHJobSubmission;
 import org.apache.airavata.model.appcatalog.gatewayprofile.GatewayResourceProfile;
 import org.apache.airavata.model.appcatalog.gatewayprofile.StoragePreference;
+import org.apache.airavata.model.appcatalog.groupresourceprofile.ComputeResourceReservation;
 import org.apache.airavata.model.appcatalog.groupresourceprofile.GroupComputeResourcePreference;
 import org.apache.airavata.model.appcatalog.groupresourceprofile.GroupResourceProfile;
 import org.apache.airavata.model.appcatalog.storageresource.StorageResourceDescription;
@@ -53,6 +54,7 @@ import org.apache.airavata.model.status.TaskState;
 import org.apache.airavata.model.status.TaskStatus;
 import org.apache.airavata.model.task.TaskModel;
 import org.apache.airavata.model.user.UserProfile;
+import org.apache.airavata.model.util.GroupComputeResourcePreferenceUtil;
 import org.apache.airavata.registry.api.RegistryService;
 import org.apache.airavata.service.profile.user.cpi.UserProfileService;
 import org.apache.airavata.service.security.AiravataSecurityManager;
@@ -646,6 +648,7 @@ public class TaskContext {
             start = userComputeResourcePreference.getReservationStartTime();
             end = userComputeResourcePreference.getReservationEndTime();
         } else {
+            // TODO: remove this once we've migrated to groupComputeResourcePreference.getReservations()
             reservation = groupComputeResourcePreference.getReservation();
             start = groupComputeResourcePreference.getReservationStartTime();
             end = groupComputeResourcePreference.getReservationEndTime();
@@ -656,6 +659,12 @@ public class TaskContext {
                 return reservation;
             }
         }
+        String queueName = getQueueName();
+        ComputeResourceReservation computeResourceReservation = GroupComputeResourcePreferenceUtil
+                .getActiveReservationForQueue(groupComputeResourcePreference, queueName);
+        if (computeResourceReservation != null) {
+            return computeResourceReservation.getReservationName();
+        }
         return null;
     }