aurora-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wfar...@apache.org
Subject git commit: Add an UpdateStrategy abstraction, with two implementations.
Date Mon, 04 Aug 2014 20:46:22 GMT
Repository: incubator-aurora
Updated Branches:
  refs/heads/master f2ac7fb98 -> ffa786211


Add an UpdateStrategy abstraction, with two implementations.

Bugs closed: AURORA-613

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


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

Branch: refs/heads/master
Commit: ffa786211fd6699def0795e725d3b88e82323fc0
Parents: f2ac7fb
Author: Bill Farner <wfarner@apache.org>
Authored: Mon Aug 4 13:41:33 2014 -0700
Committer: Bill Farner <wfarner@apache.org>
Committed: Mon Aug 4 13:41:33 2014 -0700

----------------------------------------------------------------------
 .../updater/strategy/ActiveLimitedStrategy.java | 58 ++++++++++++++++
 .../updater/strategy/BatchStrategy.java         | 40 +++++++++++
 .../updater/strategy/QueueStrategy.java         | 33 +++++++++
 .../updater/strategy/UpdateStrategy.java        | 31 +++++++++
 .../updater/strategy/BatchStrategyTest.java     | 71 ++++++++++++++++++++
 .../updater/strategy/QueueStrategyTest.java     | 67 ++++++++++++++++++
 6 files changed, 300 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/ffa78621/src/main/java/org/apache/aurora/scheduler/updater/strategy/ActiveLimitedStrategy.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/aurora/scheduler/updater/strategy/ActiveLimitedStrategy.java
b/src/main/java/org/apache/aurora/scheduler/updater/strategy/ActiveLimitedStrategy.java
new file mode 100644
index 0000000..8298ea2
--- /dev/null
+++ b/src/main/java/org/apache/aurora/scheduler/updater/strategy/ActiveLimitedStrategy.java
@@ -0,0 +1,58 @@
+/**
+ * Licensed 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.aurora.scheduler.updater.strategy;
+
+import java.util.Set;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.FluentIterable;
+import com.google.common.collect.Ordering;
+
+/**
+ * A strategy that limits the number of instances selected by the subclass.
+ *
+ * @param <T> Instance type.
+ */
+abstract class ActiveLimitedStrategy<T extends Comparable<T>> implements UpdateStrategy<T>
{
+  protected final int maxActive;
+
+  /**
+   * Creates an active-limited strategy that applies an upper bound to all results.
+   *
+   * @param maxActive Maximum number of values to return from
+   * {@link #getNextGroup(java.util.Set, java.util.Set)}.
+   */
+  protected ActiveLimitedStrategy(int maxActive) {
+    Preconditions.checkArgument(maxActive > 0);
+    this.maxActive = maxActive;
+  }
+
+  @Override
+  public final Set<T> getNextGroup(Set<T> idle, Set<T> active) {
+    return FluentIterable
+        .from(Ordering.natural().sortedCopy(doGetNextGroup(idle, active)))
+        .limit(Math.max(0, maxActive - active.size()))
+        .toSet();
+  }
+
+  /**
+   * Delegate function for the subclass. The implementation may return as many or few results
+   * as they wish. If the result is larger than {@link #maxActive}, it will be truncated.
+   *
+   * @param idle Idle instances, candidate for being updated.
+   * @param active Instances currently being updated.
+   * @return A subset of {@code idle}, instances to start updating.
+   */
+  abstract Set<T> doGetNextGroup(Set<T> idle, Set<T> active);
+}

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/ffa78621/src/main/java/org/apache/aurora/scheduler/updater/strategy/BatchStrategy.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/aurora/scheduler/updater/strategy/BatchStrategy.java
b/src/main/java/org/apache/aurora/scheduler/updater/strategy/BatchStrategy.java
new file mode 100644
index 0000000..a3e666e
--- /dev/null
+++ b/src/main/java/org/apache/aurora/scheduler/updater/strategy/BatchStrategy.java
@@ -0,0 +1,40 @@
+/**
+ * Licensed 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.aurora.scheduler.updater.strategy;
+
+import java.util.Set;
+
+import com.google.common.collect.ImmutableSet;
+
+/**
+ * An update strategy that will only add more work when the current active group is empty.
+ *
+ * @param <T> Instance type.
+ */
+public class BatchStrategy<T extends Comparable<T>> extends ActiveLimitedStrategy<T>
{
+
+  /**
+   * Creates a batch strategy that will allow a limited number of active entries.
+   *
+   * @param maxActive The maximum number of active entries.
+   */
+  public BatchStrategy(int maxActive) {
+    super(maxActive);
+  }
+
+  @Override
+  Set<T> doGetNextGroup(Set<T> idle, Set<T> active) {
+    return active.isEmpty() ? idle : ImmutableSet.<T>of();
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/ffa78621/src/main/java/org/apache/aurora/scheduler/updater/strategy/QueueStrategy.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/aurora/scheduler/updater/strategy/QueueStrategy.java
b/src/main/java/org/apache/aurora/scheduler/updater/strategy/QueueStrategy.java
new file mode 100644
index 0000000..0cf3683
--- /dev/null
+++ b/src/main/java/org/apache/aurora/scheduler/updater/strategy/QueueStrategy.java
@@ -0,0 +1,33 @@
+/**
+ * Licensed 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.aurora.scheduler.updater.strategy;
+
+import java.util.Set;
+
+/**
+ * An update strategy that attempts to keep the in-progress queue full at all times, with
an upper
+ * bound.
+ *
+ * @param <T> Instance type.
+ */
+public class QueueStrategy<T extends Comparable<T>> extends ActiveLimitedStrategy<T>
{
+  public QueueStrategy(int maxActive) {
+    super(maxActive);
+  }
+
+  @Override
+  Set<T> doGetNextGroup(Set<T> idle, Set<T> active) {
+    return idle;
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/ffa78621/src/main/java/org/apache/aurora/scheduler/updater/strategy/UpdateStrategy.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/aurora/scheduler/updater/strategy/UpdateStrategy.java
b/src/main/java/org/apache/aurora/scheduler/updater/strategy/UpdateStrategy.java
new file mode 100644
index 0000000..c2a2ee8
--- /dev/null
+++ b/src/main/java/org/apache/aurora/scheduler/updater/strategy/UpdateStrategy.java
@@ -0,0 +1,31 @@
+/**
+ * Licensed 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.aurora.scheduler.updater.strategy;
+
+import java.util.Set;
+
+/**
+ * A strategy that chooses which instances in a group to update next.
+ */
+public interface UpdateStrategy<T> {
+
+  /**
+   * Gets the next group to update.
+   *
+   * @param idle Idle instances, candidate for being updated.
+   * @param active Instances currently being updated.
+   * @return A subset of {@code idle}, instances to start updating.
+   */
+  Set<T> getNextGroup(Set<T> idle, Set<T> active);
+}

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/ffa78621/src/test/java/org/apache/aurora/scheduler/updater/strategy/BatchStrategyTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/aurora/scheduler/updater/strategy/BatchStrategyTest.java
b/src/test/java/org/apache/aurora/scheduler/updater/strategy/BatchStrategyTest.java
new file mode 100644
index 0000000..e6742dc
--- /dev/null
+++ b/src/test/java/org/apache/aurora/scheduler/updater/strategy/BatchStrategyTest.java
@@ -0,0 +1,71 @@
+/**
+ * Licensed 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.aurora.scheduler.updater.strategy;
+
+import java.util.Set;
+
+import org.junit.Test;
+
+import static com.google.common.collect.ImmutableSet.of;
+
+import static org.junit.Assert.assertEquals;
+
+public class BatchStrategyTest {
+
+  private static final Set<Integer> EMPTY = of();
+
+  @Test(expected = IllegalArgumentException.class)
+  public void testBadParameter() {
+    new BatchStrategy<Integer>(0);
+  }
+
+  @Test
+  public void testNoWorkToDo() {
+    UpdateStrategy<Integer> strategy = new BatchStrategy<>(2);
+    assertEquals(EMPTY, strategy.getNextGroup(EMPTY, of(0, 1)));
+    assertEquals(EMPTY, strategy.getNextGroup(EMPTY, EMPTY));
+  }
+
+  @Test
+  public void testWaitForBatchCompletion() {
+    UpdateStrategy<Integer> strategy = new BatchStrategy<>(2);
+    assertEquals(EMPTY, strategy.getNextGroup(of(2, 3), of(0, 1)));
+    assertEquals(EMPTY, strategy.getNextGroup(of(2, 3), of(1)));
+    assertEquals(of(2, 3), strategy.getNextGroup(of(2, 3), EMPTY));
+  }
+
+  @Test
+  public void testBatchesIgnoreInstanceValues() {
+    // Batches are defined as groups of instances, not partitioned based on the instance
ID values.
+    UpdateStrategy<Integer> strategy = new BatchStrategy<>(2);
+    assertEquals(of(0, 1), strategy.getNextGroup(of(0, 1, 2, 3), EMPTY));
+    assertEquals(of(1, 2), strategy.getNextGroup(of(1, 2, 3), EMPTY));
+    assertEquals(of(2, 3), strategy.getNextGroup(of(2, 3), EMPTY));
+    assertEquals(of(3, 8), strategy.getNextGroup(of(3, 8), EMPTY));
+  }
+
+  @Test
+  public void testExhausted() {
+    UpdateStrategy<Integer> strategy = new BatchStrategy<>(3);
+    assertEquals(of(0, 1, 2), strategy.getNextGroup(of(0, 1, 2), EMPTY));
+    assertEquals(of(0, 1), strategy.getNextGroup(of(0, 1), EMPTY));
+    assertEquals(of(1), strategy.getNextGroup(of(1), EMPTY));
+  }
+
+  @Test
+  public void testActiveTooLarge() {
+    UpdateStrategy<Integer> strategy = new BatchStrategy<>(2);
+    assertEquals(EMPTY, strategy.getNextGroup(of(0, 1, 2), of(3, 4, 5)));
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/ffa78621/src/test/java/org/apache/aurora/scheduler/updater/strategy/QueueStrategyTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/aurora/scheduler/updater/strategy/QueueStrategyTest.java
b/src/test/java/org/apache/aurora/scheduler/updater/strategy/QueueStrategyTest.java
new file mode 100644
index 0000000..f9a2806
--- /dev/null
+++ b/src/test/java/org/apache/aurora/scheduler/updater/strategy/QueueStrategyTest.java
@@ -0,0 +1,67 @@
+/**
+ * Licensed 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.aurora.scheduler.updater.strategy;
+
+import java.util.Set;
+
+import org.junit.Test;
+
+import static com.google.common.collect.ImmutableSet.of;
+
+import static org.junit.Assert.assertEquals;
+
+public class QueueStrategyTest {
+
+  private static final Set<Integer> EMPTY = of();
+
+  @Test(expected = IllegalArgumentException.class)
+  public void testBadParameter() {
+    new QueueStrategy<Integer>(0);
+  }
+
+  @Test
+  public void testNoWorkToDO() {
+    UpdateStrategy<Integer> strategy = new QueueStrategy<>(2);
+    assertEquals(EMPTY, strategy.getNextGroup(EMPTY, of(0, 1)));
+    assertEquals(EMPTY, strategy.getNextGroup(EMPTY, EMPTY));
+  }
+
+  @Test
+  public void testFillsQueue() {
+    UpdateStrategy<Integer> strategy = new QueueStrategy<>(3);
+    assertEquals(of(0, 1, 2), strategy.getNextGroup(of(0, 1, 2, 3, 4, 5), EMPTY));
+    assertEquals(of(2), strategy.getNextGroup(of(2, 3, 4), of(0, 1)));
+  }
+
+  @Test
+  public void testNonContiguous() {
+    UpdateStrategy<Integer> strategy = new QueueStrategy<>(3);
+    assertEquals(of(0, 1, 3), strategy.getNextGroup(of(0, 1, 3, 5, 8), EMPTY));
+    assertEquals(of(5, 8), strategy.getNextGroup(of(5, 8), of(3)));
+  }
+
+  @Test
+  public void testExhausted() {
+    UpdateStrategy<Integer> strategy = new QueueStrategy<>(3);
+    assertEquals(of(0, 1, 2), strategy.getNextGroup(of(0, 1, 2), EMPTY));
+    assertEquals(of(0, 1), strategy.getNextGroup(of(0, 1), EMPTY));
+    assertEquals(of(1), strategy.getNextGroup(of(1), EMPTY));
+  }
+
+  @Test
+  public void testActiveTooLarge() {
+    UpdateStrategy<Integer> strategy = new QueueStrategy<>(2);
+    assertEquals(EMPTY, strategy.getNextGroup(of(0, 1, 2), of(3, 4, 5)));
+  }
+}


Mime
View raw message