You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@helix.apache.org by jx...@apache.org on 2018/01/25 21:49:08 UTC

[17/50] [abbrv] helix git commit: Support update complete instance config in REST

Support update complete instance config in REST


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

Branch: refs/heads/master
Commit: ae537054b61e4801c55bee246622f67c748c53a4
Parents: d03dca1
Author: Junkai Xue <jx...@linkedin.com>
Authored: Wed Nov 15 17:54:28 2017 -0800
Committer: Junkai Xue <jx...@linkedin.com>
Committed: Wed Jan 24 18:31:33 2018 -0800

----------------------------------------------------------------------
 .../rest/server/resources/InstanceAccessor.java | 23 +++++++++++++++++++
 .../helix/rest/server/TestInstanceAccessor.java | 24 ++++++++++++++++++++
 2 files changed, 47 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/helix/blob/ae537054/helix-rest/src/main/java/org/apache/helix/rest/server/resources/InstanceAccessor.java
----------------------------------------------------------------------
diff --git a/helix-rest/src/main/java/org/apache/helix/rest/server/resources/InstanceAccessor.java b/helix-rest/src/main/java/org/apache/helix/rest/server/resources/InstanceAccessor.java
index 0099097..72ffc19 100644
--- a/helix-rest/src/main/java/org/apache/helix/rest/server/resources/InstanceAccessor.java
+++ b/helix-rest/src/main/java/org/apache/helix/rest/server/resources/InstanceAccessor.java
@@ -300,6 +300,29 @@ public class InstanceAccessor extends AbstractResource {
     return notFound();
   }
 
+  @PUT
+  @Path("{instanceName}/configs")
+  public Response updateInstanceConfig(@PathParam("clusterId") String clusterId,
+      @PathParam("instanceName") String instanceName, String content) throws IOException {
+    HelixAdmin admin = getHelixAdmin();
+    ZNRecord record;
+    try {
+      record = toZNRecord(content);
+    } catch (IOException e) {
+      _logger.error("Failed to deserialize user's input " + content + ", Exception: " + e);
+      return badRequest("Input is not a vaild ZNRecord!");
+    }
+
+    try {
+      admin.setInstanceConfig(clusterId, instanceName, new InstanceConfig(record));
+    } catch (Exception ex) {
+      _logger.error("Error in update instance config: " + instanceName, ex);
+      return serverError(ex);
+    }
+
+    return OK();
+  }
+
   @GET
   @Path("{instanceName}/resources")
   public Response getResourcesOnInstance(@PathParam("clusterId") String clusterId,

http://git-wip-us.apache.org/repos/asf/helix/blob/ae537054/helix-rest/src/test/java/org/apache/helix/rest/server/TestInstanceAccessor.java
----------------------------------------------------------------------
diff --git a/helix-rest/src/test/java/org/apache/helix/rest/server/TestInstanceAccessor.java b/helix-rest/src/test/java/org/apache/helix/rest/server/TestInstanceAccessor.java
index 7e1b4cb..8db348e 100644
--- a/helix-rest/src/test/java/org/apache/helix/rest/server/TestInstanceAccessor.java
+++ b/helix-rest/src/test/java/org/apache/helix/rest/server/TestInstanceAccessor.java
@@ -32,6 +32,7 @@ import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import org.apache.helix.HelixException;
 import org.apache.helix.TestHelper;
+import org.apache.helix.ZNRecord;
 import org.apache.helix.model.ClusterConfig;
 import org.apache.helix.model.InstanceConfig;
 import org.apache.helix.rest.server.resources.AbstractResource;
@@ -163,4 +164,27 @@ public class TestInstanceAccessor extends AbstractTestClass {
     Assert.assertEquals(clusterConfig.getDisabledInstances().keySet(),
         new HashSet<>(Arrays.asList(CLUSTER_NAME + "localhost_12919")));
   }
+
+  @Test(dependsOnMethods = "updateInstance")
+  public void updateInstanceConfig() throws IOException {
+    System.out.println("Start test :" + TestHelper.getTestMethodName());
+    String instanceName = CLUSTER_NAME + "localhost_12918";
+    InstanceConfig instanceConfig = _configAccessor.getInstanceConfig(CLUSTER_NAME, instanceName);
+    ZNRecord record = instanceConfig.getRecord();
+    record.getSimpleFields().put("TestSimple", "value");
+    record.getMapFields().put("TestMap", ImmutableMap.of("key", "value"));
+    record.getListFields().put("TestList", Arrays.asList("e1", "e2", "e3"));
+
+    Entity entity =
+        Entity.entity(OBJECT_MAPPER.writeValueAsString(record), MediaType.APPLICATION_JSON_TYPE);
+    put("clusters/" + CLUSTER_NAME + "/instances/" + instanceName + "/configs", null, entity,
+        Response.Status.OK.getStatusCode());
+    Assert.assertEquals(record.getSimpleFields(),
+        _configAccessor.getInstanceConfig(CLUSTER_NAME, instanceName).getRecord()
+            .getSimpleFields());
+    Assert.assertEquals(record.getListFields(),
+        _configAccessor.getInstanceConfig(CLUSTER_NAME, instanceName).getRecord().getListFields());
+    Assert.assertEquals(record.getMapFields(),
+        _configAccessor.getInstanceConfig(CLUSTER_NAME, instanceName).getRecord().getMapFields());
+  }
 }