aurora-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject git commit: Adding /locks endpoint to expose update locks.
Date Mon, 16 Jun 2014 21:16:38 GMT
Repository: incubator-aurora
Updated Branches:
  refs/heads/master 5241cb949 -> d97c3e623


Adding /locks endpoint to expose update locks.

Bugs closed: AURORA-346

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


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

Branch: refs/heads/master
Commit: d97c3e62371704f960f51b5da78bcea73f5a75ef
Parents: 5241cb9
Author: Maxim Khutornenko <maxim@apache.org>
Authored: Mon Jun 16 14:16:12 2014 -0700
Committer: Maxim Khutornenko <maxim@apache.org>
Committed: Mon Jun 16 14:16:12 2014 -0700

----------------------------------------------------------------------
 .../org/apache/aurora/scheduler/http/Locks.java | 101 +++++++++++++++++++
 .../apache/aurora/scheduler/http/Quotas.java    |   2 +-
 .../aurora/scheduler/http/ServletModule.java    |   1 +
 .../aurora/scheduler/state/LockManager.java     |   7 ++
 .../aurora/scheduler/state/LockManagerImpl.java |  11 ++
 .../thrift/SchedulerThriftInterface.java        |   7 ++
 .../python/apache/aurora/client/cli/client.py   |   1 +
 .../thrift/org/apache/aurora/gen/api.thrift     |   8 ++
 .../apache/aurora/scheduler/http/LocksTest.java |  85 ++++++++++++++++
 .../scheduler/state/LockManagerImplTest.java    |   8 ++
 .../thrift/SchedulerThriftInterfaceTest.java    |  12 +++
 .../scheduler/thrift/aop/ForwardingThrift.java  |   5 +
 .../org/apache/aurora/gen/api.thrift.md5        |   2 +-
 13 files changed, 248 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/d97c3e62/src/main/java/org/apache/aurora/scheduler/http/Locks.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/aurora/scheduler/http/Locks.java b/src/main/java/org/apache/aurora/scheduler/http/Locks.java
new file mode 100644
index 0000000..dce1891
--- /dev/null
+++ b/src/main/java/org/apache/aurora/scheduler/http/Locks.java
@@ -0,0 +1,101 @@
+/**
+ * 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.http;
+
+import javax.inject.Inject;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import com.google.common.base.Function;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Maps;
+
+import org.apache.aurora.gen.LockKey;
+import org.apache.aurora.scheduler.base.JobKeys;
+import org.apache.aurora.scheduler.state.LockManager;
+import org.apache.aurora.scheduler.storage.entities.ILock;
+import org.codehaus.jackson.annotate.JsonProperty;
+
+/**
+ * Servlet that exposes existing resource/operation locks.
+ */
+@Path("/locks")
+public class Locks {
+
+  private final LockManager lockManager;
+
+  @Inject
+  Locks(LockManager lockManager) {
+    this.lockManager = Preconditions.checkNotNull(lockManager);
+  }
+
+  /**
+   * Dumps existing locks.
+   *
+   * @return HTTP response.
+   */
+  @GET
+  @Produces(MediaType.APPLICATION_JSON)
+  public Response getLocks() {
+    return Response.ok(Maps.transformValues(
+        Maps.uniqueIndex(lockManager.getLocks(), TO_LOCK_KEY), TO_BEAN)).build();
+  }
+
+  private static final Function<ILock, String> TO_LOCK_KEY = new Function<ILock,
String>() {
+    @Override
+    public String apply(ILock lock) {
+      return lock.getKey().getSetField() == LockKey._Fields.JOB
+          ? JobKeys.canonicalString(lock.getKey().getJob())
+          : "Unknown lock key type: " + lock.getKey().getSetField();
+    }
+  };
+
+  private static final Function<ILock, LockBean> TO_BEAN = new Function<ILock, LockBean>()
{
+    @Override
+    public LockBean apply(ILock lock) {
+      return new LockBean(lock);
+    }
+  };
+
+  private static final class LockBean {
+    private final ILock lock;
+
+    LockBean(ILock lock) {
+      this.lock = lock;
+    }
+
+    @JsonProperty("token")
+    public String getToken() {
+      return lock.getToken();
+    }
+
+    @JsonProperty("user")
+    public String getUser() {
+      return lock.getUser();
+    }
+
+    @JsonProperty("timestampMs")
+    public long getTimestampMs() {
+      return lock.getTimestampMs();
+    }
+
+    @JsonProperty("message")
+    public String getMessage() {
+      return lock.getMessage();
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/d97c3e62/src/main/java/org/apache/aurora/scheduler/http/Quotas.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/aurora/scheduler/http/Quotas.java b/src/main/java/org/apache/aurora/scheduler/http/Quotas.java
index 438d127..f23739a 100644
--- a/src/main/java/org/apache/aurora/scheduler/http/Quotas.java
+++ b/src/main/java/org/apache/aurora/scheduler/http/Quotas.java
@@ -55,7 +55,7 @@ public class Quotas {
    */
   @GET
   @Produces(MediaType.APPLICATION_JSON)
-  public Response getOffers(@QueryParam("role") final String role) {
+  public Response getQuotas(@QueryParam("role") final String role) {
     return storage.weaklyConsistentRead(new Work.Quiet<Response>() {
       @Override
       public Response apply(StoreProvider storeProvider) {

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/d97c3e62/src/main/java/org/apache/aurora/scheduler/http/ServletModule.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/aurora/scheduler/http/ServletModule.java b/src/main/java/org/apache/aurora/scheduler/http/ServletModule.java
index d104eab..fb225f1 100644
--- a/src/main/java/org/apache/aurora/scheduler/http/ServletModule.java
+++ b/src/main/java/org/apache/aurora/scheduler/http/ServletModule.java
@@ -109,6 +109,7 @@ public class ServletModule extends AbstractModule {
         }
 
         registerJerseyEndpoint("/cron", Cron.class);
+        registerJerseyEndpoint("/locks", Locks.class);
         registerJerseyEndpoint("/maintenance", Maintenance.class);
         registerJerseyEndpoint("/mname", Mname.class);
         registerJerseyEndpoint("/offers", Offers.class);

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/d97c3e62/src/main/java/org/apache/aurora/scheduler/state/LockManager.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/aurora/scheduler/state/LockManager.java b/src/main/java/org/apache/aurora/scheduler/state/LockManager.java
index 8b3b174..e8303f9 100644
--- a/src/main/java/org/apache/aurora/scheduler/state/LockManager.java
+++ b/src/main/java/org/apache/aurora/scheduler/state/LockManager.java
@@ -54,6 +54,13 @@ public interface LockManager {
   void validateIfLocked(ILockKey context, Optional<ILock> heldLock) throws LockException;
 
   /**
+   * Returns all available locks stored.
+   *
+   * @return Set of {@link ILock} instances.
+   */
+  Iterable<ILock> getLocks();
+
+  /**
    * Thrown when {@link ILock} related operation failed.
    */
   class LockException extends Exception {

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/d97c3e62/src/main/java/org/apache/aurora/scheduler/state/LockManagerImpl.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/aurora/scheduler/state/LockManagerImpl.java b/src/main/java/org/apache/aurora/scheduler/state/LockManagerImpl.java
index 0568529..df2bc5a 100644
--- a/src/main/java/org/apache/aurora/scheduler/state/LockManagerImpl.java
+++ b/src/main/java/org/apache/aurora/scheduler/state/LockManagerImpl.java
@@ -18,6 +18,7 @@ import java.util.Date;
 import javax.inject.Inject;
 
 import com.google.common.base.Optional;
+
 import com.twitter.common.util.Clock;
 
 import org.apache.aurora.gen.Lock;
@@ -121,6 +122,16 @@ class LockManagerImpl implements LockManager {
     }
   }
 
+  @Override
+  public Iterable<ILock> getLocks() {
+    return storage.weaklyConsistentRead(new Work.Quiet<Iterable<ILock>>() {
+      @Override
+      public Iterable<ILock> apply(StoreProvider storeProvider) {
+        return storeProvider.getLockStore().fetchLocks();
+      }
+    });
+  }
+
   private static String formatLockKey(ILockKey lockKey) {
     return lockKey.getSetField() == _Fields.JOB
         ? JobKeys.canonicalString(lockKey.getJob())

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/d97c3e62/src/main/java/org/apache/aurora/scheduler/thrift/SchedulerThriftInterface.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/aurora/scheduler/thrift/SchedulerThriftInterface.java
b/src/main/java/org/apache/aurora/scheduler/thrift/SchedulerThriftInterface.java
index 485b5e6..94072cf 100644
--- a/src/main/java/org/apache/aurora/scheduler/thrift/SchedulerThriftInterface.java
+++ b/src/main/java/org/apache/aurora/scheduler/thrift/SchedulerThriftInterface.java
@@ -53,6 +53,7 @@ import org.apache.aurora.gen.ConfigRewrite;
 import org.apache.aurora.gen.DrainHostsResult;
 import org.apache.aurora.gen.EndMaintenanceResult;
 import org.apache.aurora.gen.GetJobsResult;
+import org.apache.aurora.gen.GetLocksResult;
 import org.apache.aurora.gen.GetQuotaResult;
 import org.apache.aurora.gen.Hosts;
 import org.apache.aurora.gen.InstanceConfigRewrite;
@@ -1067,6 +1068,12 @@ class SchedulerThriftInterface implements AuroraAdmin.Iface {
     }
   }
 
+  @Override
+  public Response getLocks() {
+    return okResponse(Result.getLocksResult(
+        new GetLocksResult().setLocks(ILock.toBuildersSet(lockManager.getLocks()))));
+  }
+
   @VisibleForTesting
   static Optional<String> transitionMessage(String user) {
     return Optional.of("Transition forced by " + user);

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/d97c3e62/src/main/python/apache/aurora/client/cli/client.py
----------------------------------------------------------------------
diff --git a/src/main/python/apache/aurora/client/cli/client.py b/src/main/python/apache/aurora/client/cli/client.py
index 2a06cdd..0cb6944 100644
--- a/src/main/python/apache/aurora/client/cli/client.py
+++ b/src/main/python/apache/aurora/client/cli/client.py
@@ -18,6 +18,7 @@ from apache.aurora.client.cli.bridge import Bridge, CommandProcessor
 from apache.aurora.client.cli.logsetup import setup_default_log_handlers
 from apache.aurora.client.cli.standalone_client import AuroraCommandLine
 
+
 # TODO(mchucarroll): the entire bridged executable mechanism here is
 # intended to be deprecated once clientv2 has proven to be stable
 # and adopted by most users. Once we reach that point, this should

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/d97c3e62/src/main/thrift/org/apache/aurora/gen/api.thrift
----------------------------------------------------------------------
diff --git a/src/main/thrift/org/apache/aurora/gen/api.thrift b/src/main/thrift/org/apache/aurora/gen/api.thrift
index 8865d8f..d115c74 100644
--- a/src/main/thrift/org/apache/aurora/gen/api.thrift
+++ b/src/main/thrift/org/apache/aurora/gen/api.thrift
@@ -432,6 +432,10 @@ struct JobSummaryResult {
   1: set<JobSummary> summaries
 }
 
+struct GetLocksResult {
+  1: set<Lock> locks
+}
+
 // meta-data about the thrift server that is wrapped around every thrift response
 struct ServerInfo {
   1: string clusterName
@@ -454,6 +458,7 @@ union Result {
   16: AcquireLockResult acquireLockResult
   17: RoleSummaryResult roleSummaryResult
   18: JobSummaryResult jobSummaryResult
+  19: GetLocksResult getLocksResult
 }
 
 struct ResponseDetail {
@@ -497,6 +502,9 @@ service ReadOnlyScheduler {
   // Populates fields in a job configuration as though it were about to be run.
   // This can be used to diff a configuration running tasks.
   Response populateJobConfig(1: JobConfiguration description)
+
+  // Returns all stored context specific resource/operation locks.
+  Response getLocks()
 }
 
 // Due to assumptions in the client all authenticated RPCs must have a SessionKey as their

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/d97c3e62/src/test/java/org/apache/aurora/scheduler/http/LocksTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/aurora/scheduler/http/LocksTest.java b/src/test/java/org/apache/aurora/scheduler/http/LocksTest.java
new file mode 100644
index 0000000..6038514
--- /dev/null
+++ b/src/test/java/org/apache/aurora/scheduler/http/LocksTest.java
@@ -0,0 +1,85 @@
+/**
+ * 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.http;
+
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.core.Response;
+
+import com.google.common.collect.ImmutableSet;
+
+import com.twitter.common.testing.easymock.EasyMockTest;
+
+import org.apache.aurora.gen.Lock;
+import org.apache.aurora.gen.LockKey;
+import org.apache.aurora.scheduler.base.JobKeys;
+import org.apache.aurora.scheduler.state.LockManager;
+import org.apache.aurora.scheduler.storage.entities.IJobKey;
+import org.apache.aurora.scheduler.storage.entities.ILock;
+import org.apache.aurora.scheduler.storage.entities.ILockKey;
+import org.codehaus.jackson.map.ObjectMapper;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.easymock.EasyMock.expect;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class LocksTest extends EasyMockTest {
+  private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+  private static final IJobKey JOB_KEY = JobKeys.from("role", "env", "job");
+  private static final ILockKey LOCK_KEY = ILockKey.build(LockKey.job(JOB_KEY.newBuilder()));
+
+  private Locks locks;
+  private LockManager lockManager;
+
+  @Before
+  public void setUp() {
+    lockManager = createMock(LockManager.class);
+    locks = new Locks(lockManager);
+  }
+
+  @Test
+  public void testDumpContents() throws Exception {
+    ILock lock = ILock.build(new Lock()
+        .setKey(LOCK_KEY.newBuilder())
+        .setToken("test token")
+        .setMessage("test msg")
+        .setUser("test usr")
+        .setTimestampMs(325));
+    expect(lockManager.getLocks()).andReturn(ImmutableSet.of(lock));
+
+    control.replay();
+
+    Response response = locks.getLocks();
+    assertEquals(HttpServletResponse.SC_OK, response.getStatus());
+
+    String result = OBJECT_MAPPER.writeValueAsString(response.getEntity());
+    assertTrue(result.contains("role/env/job"));
+    assertTrue(result.contains("test token"));
+    assertTrue(result.contains("test msg"));
+    assertTrue(result.contains("test usr"));
+    assertTrue(result.contains("325"));
+  }
+
+  @Test
+  public void testDumpEmptyContents() throws Exception {
+    expect(lockManager.getLocks()).andReturn(ImmutableSet.<ILock>of());
+
+    control.replay();
+
+    Response response = locks.getLocks();
+    assertEquals(HttpServletResponse.SC_OK, response.getStatus());
+    assertEquals("{}", OBJECT_MAPPER.writeValueAsString(response.getEntity()));
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/d97c3e62/src/test/java/org/apache/aurora/scheduler/state/LockManagerImplTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/aurora/scheduler/state/LockManagerImplTest.java b/src/test/java/org/apache/aurora/scheduler/state/LockManagerImplTest.java
index dd36027..f1892bd 100644
--- a/src/test/java/org/apache/aurora/scheduler/state/LockManagerImplTest.java
+++ b/src/test/java/org/apache/aurora/scheduler/state/LockManagerImplTest.java
@@ -16,6 +16,8 @@ package org.apache.aurora.scheduler.state;
 import java.util.UUID;
 
 import com.google.common.base.Optional;
+import com.google.common.collect.Iterables;
+
 import com.twitter.common.quantity.Amount;
 import com.twitter.common.quantity.Time;
 import com.twitter.common.testing.easymock.EasyMockTest;
@@ -129,6 +131,12 @@ public class LockManagerImplTest extends EasyMockTest {
     lockManager.validateIfLocked(key, Optional.of(lock));
   }
 
+  @Test
+  public void testGetLocks() throws Exception {
+    ILock lock = lockManager.acquireLock(LOCK_KEY, USER);
+    assertEquals(lock, Iterables.getOnlyElement(lockManager.getLocks()));
+  }
+
   private void expectLockException(IJobKey key) {
     expectedException.expect(LockException.class);
     expectedException.expectMessage(JobKeys.canonicalString(key));

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/d97c3e62/src/test/java/org/apache/aurora/scheduler/thrift/SchedulerThriftInterfaceTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/aurora/scheduler/thrift/SchedulerThriftInterfaceTest.java
b/src/test/java/org/apache/aurora/scheduler/thrift/SchedulerThriftInterfaceTest.java
index 1742171..bab8b9f 100644
--- a/src/test/java/org/apache/aurora/scheduler/thrift/SchedulerThriftInterfaceTest.java
+++ b/src/test/java/org/apache/aurora/scheduler/thrift/SchedulerThriftInterfaceTest.java
@@ -1579,6 +1579,18 @@ public class SchedulerThriftInterfaceTest extends EasyMockTest {
   }
 
   @Test
+  public void testGetLocks() throws Exception {
+    expect(lockManager.getLocks()).andReturn(ImmutableSet.of(LOCK));
+
+    control.replay();
+
+    Response response = thrift.getLocks();
+    assertEquals(
+        LOCK.newBuilder(),
+        Iterables.getOnlyElement(response.getResult().getGetLocksResult().getLocks()));
+  }
+
+  @Test
   public void testGetQuota() throws Exception {
     QuotaInfo infoMock = createMock(QuotaInfo.class);
     expect(quotaManager.getQuotaInfo(ROLE)).andReturn(infoMock);

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/d97c3e62/src/test/java/org/apache/aurora/scheduler/thrift/aop/ForwardingThrift.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/aurora/scheduler/thrift/aop/ForwardingThrift.java b/src/test/java/org/apache/aurora/scheduler/thrift/aop/ForwardingThrift.java
index cfe45d1..1f1f05e 100644
--- a/src/test/java/org/apache/aurora/scheduler/thrift/aop/ForwardingThrift.java
+++ b/src/test/java/org/apache/aurora/scheduler/thrift/aop/ForwardingThrift.java
@@ -235,6 +235,11 @@ abstract class ForwardingThrift implements AuroraAdmin.Iface {
   }
 
   @Override
+  public Response getLocks() throws TException {
+    return delegate.getLocks();
+  }
+
+  @Override
   public Response addInstances(
       AddInstancesConfig config,
       Lock lock,

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/d97c3e62/src/test/resources/org/apache/aurora/gen/api.thrift.md5
----------------------------------------------------------------------
diff --git a/src/test/resources/org/apache/aurora/gen/api.thrift.md5 b/src/test/resources/org/apache/aurora/gen/api.thrift.md5
index 244ab50..255b3d9 100644
--- a/src/test/resources/org/apache/aurora/gen/api.thrift.md5
+++ b/src/test/resources/org/apache/aurora/gen/api.thrift.md5
@@ -1 +1 @@
-ab3862f4cce0d5dfd2484d7f943ac457
+34d9e4cafe845ccff03ef21ced50f7f9


Mime
View raw message