aurora-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wfar...@apache.org
Subject incubator-aurora git commit: Simplify port name association.
Date Mon, 23 Mar 2015 18:52:39 GMT
Repository: incubator-aurora
Updated Branches:
  refs/heads/master a3a35e99f -> 5602448d0


Simplify port name association.

Reviewed at https://reviews.apache.org/r/32369/


Project: http://git-wip-us.apache.org/repos/asf/incubator-aurora/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-aurora/commit/5602448d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-aurora/tree/5602448d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-aurora/diff/5602448d

Branch: refs/heads/master
Commit: 5602448d0194507cc11ec9ba124ee0c838314299
Parents: a3a35e9
Author: Bill Farner <wfarner@apache.org>
Authored: Mon Mar 23 11:52:14 2015 -0700
Committer: Bill Farner <wfarner@apache.org>
Committed: Mon Mar 23 11:52:14 2015 -0700

----------------------------------------------------------------------
 .../scheduler/filter/SchedulingFilter.java      |  4 +--
 .../aurora/scheduler/state/StateManager.java    |  3 +-
 .../scheduler/state/StateManagerImpl.java       | 35 +++-----------------
 .../aurora/scheduler/state/TaskAssigner.java    | 24 +++++++++++---
 .../scheduler/state/StateManagerImplTest.java   | 15 ++++++---
 .../scheduler/state/TaskAssignerImplTest.java   |  6 ++--
 6 files changed, 42 insertions(+), 45 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/5602448d/src/main/java/org/apache/aurora/scheduler/filter/SchedulingFilter.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/aurora/scheduler/filter/SchedulingFilter.java b/src/main/java/org/apache/aurora/scheduler/filter/SchedulingFilter.java
index 93fc05e..dc1ad6e 100644
--- a/src/main/java/org/apache/aurora/scheduler/filter/SchedulingFilter.java
+++ b/src/main/java/org/apache/aurora/scheduler/filter/SchedulingFilter.java
@@ -307,8 +307,8 @@ public interface SchedulingFilter {
       return taskId;
     }
 
-    public int getNumRequestedPorts() {
-      return task.getRequestedPorts().size();
+    public Set<String> getRequestedPorts() {
+      return task.getRequestedPorts();
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/5602448d/src/main/java/org/apache/aurora/scheduler/state/StateManager.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/aurora/scheduler/state/StateManager.java b/src/main/java/org/apache/aurora/scheduler/state/StateManager.java
index 50ff4ec..71bfefb 100644
--- a/src/main/java/org/apache/aurora/scheduler/state/StateManager.java
+++ b/src/main/java/org/apache/aurora/scheduler/state/StateManager.java
@@ -13,6 +13,7 @@
  */
 package org.apache.aurora.scheduler.state;
 
+import java.util.Map;
 import java.util.Set;
 
 import com.google.common.base.Optional;
@@ -71,7 +72,7 @@ public interface StateManager {
       String taskId,
       String slaveHost,
       SlaveID slaveId,
-      Set<Integer> assignedPorts);
+      Map<String, Integer> assignedPorts);
 
   /**
    * Inserts pending instances using {@code task} as their configuration. Tasks will immediately

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/5602448d/src/main/java/org/apache/aurora/scheduler/state/StateManagerImpl.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/aurora/scheduler/state/StateManagerImpl.java b/src/main/java/org/apache/aurora/scheduler/state/StateManagerImpl.java
index 8721466..b6a7b4a 100644
--- a/src/main/java/org/apache/aurora/scheduler/state/StateManagerImpl.java
+++ b/src/main/java/org/apache/aurora/scheduler/state/StateManagerImpl.java
@@ -15,7 +15,6 @@ package org.apache.aurora.scheduler.state;
 
 import java.net.InetAddress;
 import java.net.UnknownHostException;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -171,7 +170,7 @@ public class StateManagerImpl implements StateManager {
       String taskId,
       final String slaveHost,
       final SlaveID slaveId,
-      final Set<Integer> assignedPorts) {
+      final Map<String, Integer> assignedPorts) {
 
     checkNotBlank(taskId);
     checkNotBlank(slaveHost);
@@ -185,10 +184,9 @@ public class StateManagerImpl implements StateManager {
           @Override
           public IScheduledTask apply(IScheduledTask task) {
             ScheduledTask builder = task.newBuilder();
-            AssignedTask assigned = builder.getAssignedTask();
-            assigned.setAssignedPorts(
-                getNameMappedPorts(assigned.getTask().getRequestedPorts(), assignedPorts));
-            assigned.setSlaveHost(slaveHost)
+            builder.getAssignedTask()
+                .setAssignedPorts(assignedPorts)
+                .setSlaveHost(slaveHost)
                 .setSlaveId(slaveId.getValue());
             return IScheduledTask.build(builder);
           }
@@ -211,31 +209,6 @@ public class StateManagerImpl implements StateManager {
             Tasks.SCHEDULED_TO_ASSIGNED));
   }
 
-  private static Map<String, Integer> getNameMappedPorts(
-      Set<String> portNames,
-      Set<Integer> allocatedPorts) {
-
-    requireNonNull(portNames);
-
-    // Expand ports.
-    Map<String, Integer> ports = Maps.newHashMap();
-    Set<Integer> portsRemaining = Sets.newHashSet(allocatedPorts);
-    Iterator<Integer> portConsumer = Iterables.consumingIterable(portsRemaining).iterator();
-
-    for (String portName : portNames) {
-      Preconditions.checkArgument(portConsumer.hasNext(),
-          "Allocated ports %s were not sufficient to expand task.", allocatedPorts);
-      int portNumber = portConsumer.next();
-      ports.put(portName, portNumber);
-    }
-
-    if (!portsRemaining.isEmpty()) {
-      LOG.warning("Not all allocated ports were used to map ports!");
-    }
-
-    return ports;
-  }
-
   @VisibleForTesting
   static final Supplier<String> LOCAL_HOST_SUPPLIER = Suppliers.memoize(
       new Supplier<String>() {

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/5602448d/src/main/java/org/apache/aurora/scheduler/state/TaskAssigner.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/aurora/scheduler/state/TaskAssigner.java b/src/main/java/org/apache/aurora/scheduler/state/TaskAssigner.java
index c44ff33..1cef4e1 100644
--- a/src/main/java/org/apache/aurora/scheduler/state/TaskAssigner.java
+++ b/src/main/java/org/apache/aurora/scheduler/state/TaskAssigner.java
@@ -13,13 +13,18 @@
  */
 package org.apache.aurora.scheduler.state;
 
+import java.util.Iterator;
+import java.util.Map;
 import java.util.Set;
 import java.util.logging.Logger;
 
 import javax.inject.Inject;
 
+import com.google.common.base.Function;
 import com.google.common.base.Objects;
 import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.FluentIterable;
 import com.google.common.collect.ImmutableSet;
 import com.twitter.common.base.MorePreconditions;
 
@@ -190,17 +195,28 @@ public interface TaskAssigner {
     private TaskInfo assign(
         MutableStoreProvider storeProvider,
         Offer offer,
-        int numRequestedPorts,
+        Set<String> requestedPorts,
         String taskId) {
 
       String host = offer.getHostname();
-      Set<Integer> selectedPorts = Resources.getPorts(offer, numRequestedPorts);
+      Set<Integer> selectedPorts = Resources.getPorts(offer, requestedPorts.size());
+      Preconditions.checkState(selectedPorts.size() == requestedPorts.size());
+
+      final Iterator<String> names = requestedPorts.iterator();
+      Map<String, Integer> portsByName = FluentIterable.from(selectedPorts)
+          .uniqueIndex(new Function<Object, String>() {
+            @Override
+            public String apply(Object input) {
+              return names.next();
+            }
+          });
+
       IAssignedTask assigned = stateManager.assignTask(
           storeProvider,
           taskId,
           host,
           offer.getSlaveId(),
-          selectedPorts);
+          portsByName);
       LOG.info(String.format("Offer on slave %s (id %s) is being assigned task for %s.",
           host, offer.getSlaveId().getValue(), taskId));
       return taskFactory.createFrom(assigned, offer.getSlaveId());
@@ -219,7 +235,7 @@ public interface TaskAssigner {
         return Assignment.success(assign(
             storeProvider,
             offer.getOffer(),
-            resourceRequest.getNumRequestedPorts(),
+            resourceRequest.getRequestedPorts(),
             resourceRequest.getTaskId()));
       } else {
         LOG.fine("Slave " + offer.getOffer().getHostname()

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/5602448d/src/test/java/org/apache/aurora/scheduler/state/StateManagerImplTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/aurora/scheduler/state/StateManagerImplTest.java b/src/test/java/org/apache/aurora/scheduler/state/StateManagerImplTest.java
index 17b1702..7234fd3 100644
--- a/src/test/java/org/apache/aurora/scheduler/state/StateManagerImplTest.java
+++ b/src/test/java/org/apache/aurora/scheduler/state/StateManagerImplTest.java
@@ -14,6 +14,7 @@
 package org.apache.aurora.scheduler.state;
 
 import java.util.List;
+import java.util.Map;
 import java.util.NoSuchElementException;
 import java.util.Set;
 
@@ -409,7 +410,7 @@ public class StateManagerImplTest extends EasyMockTest {
     control.replay();
 
     insertTask(task, 0);
-    assignTask(taskId, HOST_A, ImmutableSet.of(80, 81, 82));
+    assignTask(taskId, HOST_A, ImmutableMap.of("one", 80, "two", 81, "three", 82));
 
     IScheduledTask actual = Iterables.getOnlyElement(
         Storage.Util.fetchTasks(storage, Query.taskScoped(taskId)));
@@ -437,11 +438,11 @@ public class StateManagerImplTest extends EasyMockTest {
     control.replay();
 
     insertTask(task, 0);
-    assignTask(taskId, HOST_A, ImmutableSet.of(80));
+    assignTask(taskId, HOST_A, ImmutableMap.of("one", 80));
     changeState(taskId, RUNNING);
     changeState(taskId, LOST);
 
-    assignTask(newTaskId, HOST_A, ImmutableSet.of(86));
+    assignTask(newTaskId, HOST_A, ImmutableMap.of("one", 86));
 
     IScheduledTask actual = Iterables.getOnlyElement(
         Storage.Util.fetchTasks(storage, Query.taskScoped(newTaskId)));
@@ -570,10 +571,14 @@ public class StateManagerImplTest extends EasyMockTest {
   }
 
   private void assignTask(String taskId, String host) {
-    assignTask(taskId, host, ImmutableSet.<Integer>of());
+    assignTask(taskId, host, ImmutableMap.<String, Integer>of());
   }
 
-  private void assignTask(final String taskId, final String host, final Set<Integer>
ports) {
+  private void assignTask(
+      final String taskId,
+      final String host,
+      final Map<String, Integer> ports) {
+
     storage.write(new Storage.MutateWork.NoResult.Quiet() {
       @Override
       protected void execute(Storage.MutableStoreProvider storeProvider) {

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/5602448d/src/test/java/org/apache/aurora/scheduler/state/TaskAssignerImplTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/aurora/scheduler/state/TaskAssignerImplTest.java b/src/test/java/org/apache/aurora/scheduler/state/TaskAssignerImplTest.java
index 06a1903..aca0234 100644
--- a/src/test/java/org/apache/aurora/scheduler/state/TaskAssignerImplTest.java
+++ b/src/test/java/org/apache/aurora/scheduler/state/TaskAssignerImplTest.java
@@ -14,6 +14,7 @@
 package org.apache.aurora.scheduler.state;
 
 import com.google.common.base.Suppliers;
+import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
 import com.twitter.common.testing.easymock.EasyMockTest;
 
@@ -69,13 +70,14 @@ public class TaskAssignerImplTest extends EasyMockTest {
       .build();
   private static final HostOffer OFFER =
       new HostOffer(MESOS_OFFER, IHostAttributes.build(new HostAttributes()));
+  private static final String PORT_NAME = "http";
   private static final IScheduledTask TASK = IScheduledTask.build(
       new ScheduledTask()
           .setAssignedTask(new AssignedTask()
               .setTaskId("taskId")
               .setTask(new TaskConfig()
                   .setExecutorConfig(new ExecutorConfig().setData("opaque data"))
-                  .setRequestedPorts(ImmutableSet.of("http")))));
+                  .setRequestedPorts(ImmutableSet.of(PORT_NAME)))));
   private static final TaskInfo TASK_INFO = TaskInfo.newBuilder()
       .setName("taskName")
       .setTaskId(TaskID.newBuilder().setValue(Tasks.id(TASK)))
@@ -112,7 +114,7 @@ public class TaskAssignerImplTest extends EasyMockTest {
         Tasks.id(TASK),
         MESOS_OFFER.getHostname(),
         MESOS_OFFER.getSlaveId(),
-        ImmutableSet.of(PORT)))
+        ImmutableMap.of(PORT_NAME, PORT)))
         .andReturn(TASK.getAssignedTask());
     expect(taskFactory.createFrom(TASK.getAssignedTask(), MESOS_OFFER.getSlaveId()))
         .andReturn(TASK_INFO);


Mime
View raw message