You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@helix.apache.org by GitBox <gi...@apache.org> on 2019/07/23 20:49:20 UTC

[GitHub] [helix] dasahcc commented on a change in pull request #339: Implementation of stateModelDef modification in REST 2.0

dasahcc commented on a change in pull request #339: Implementation of stateModelDef modification in REST 2.0
URL: https://github.com/apache/helix/pull/339#discussion_r306525969
 
 

 ##########
 File path: helix-rest/src/main/java/org/apache/helix/rest/server/resources/helix/ClusterAccessor.java
 ##########
 @@ -416,24 +418,99 @@ public Response getClusterStateModelDefinitions(@PathParam("clusterId") String c
     return JSONRepresentation(clusterStateModelDefs);
   }
 
-  @GET
-  @Path("{clusterId}/maintenance")
-  public Response getClusterMaintenanceMode(@PathParam("clusterId") String clusterId) {
-    return JSONRepresentation(
-        ImmutableMap.of(ClusterProperties.maintenance.name(), getHelixAdmin().isInMaintenanceMode(clusterId)));
-  }
-
   @GET
   @Path("{clusterId}/statemodeldefs/{statemodel}")
   public Response getClusterStateModelDefinition(@PathParam("clusterId") String clusterId,
       @PathParam("statemodel") String statemodel) {
     HelixDataAccessor dataAccessor = getDataAccssor(clusterId);
-    StateModelDefinition stateModelDef =
-        dataAccessor.getProperty(dataAccessor.keyBuilder().stateModelDef(statemodel));
+    StateModelDefinition stateModelDef = dataAccessor.getProperty(dataAccessor.keyBuilder().stateModelDef(statemodel));
 
+    if (stateModelDef == null) {
+      return notFound();
+    }
     return JSONRepresentation(stateModelDef.getRecord());
   }
 
+  @PUT
+  @Path("{clusterId}/statemodeldefs/{statemodel}")
+  public Response createClusterStateModelDefinition(@PathParam("clusterId") String clusterId,
+      @PathParam("statemodel") String statemodel, String content) {
+    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 valid ZNRecord!");
+    }
+    HelixZkClient zkClient = getHelixZkClient();
+    String path = PropertyPathBuilder.stateModelDef(clusterId);
+    try {
+      ZKUtil.createChildren(zkClient, path, record);
+    } catch (Exception e) {
+      _logger.error("Failed to create zk node with path " + path + ", Exception:" + e);
+      return badRequest("Failed to create a Znode for stateModel!");
+    }
+
+    return OK();
+  }
+
+  @POST
+  @Path("{clusterId}/statemodeldefs/{statemodel}")
+  public Response setClusterStateModelDefinition(@PathParam("clusterId") String clusterId,
+      @PathParam("statemodel") String statemodel, String content) {
+    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 valid ZNRecord!");
+    }
+
+    StateModelDefinition stateModelDefinition = new StateModelDefinition(record);
+    HelixDataAccessor dataAccessor = getDataAccssor(clusterId);
+
+    PropertyKey key = dataAccessor.keyBuilder().stateModelDef(stateModelDefinition.getId());
+    boolean retcode = true;
+    try {
+      retcode = dataAccessor.setProperty(key, stateModelDefinition);
+    } catch (Exception e) {
+      _logger.error("Failed to set StateModelDefinition key:" + key + ", Exception: " + e);
+      return badRequest("Failed to set the content " + content);
+    }
+
+    return OK();
+  }
+
+  @DELETE
+  @Path("{clusterId}/statemodeldefs/{statemodel}")
+  public Response removeClusterStateModelDefinition(@PathParam("clusterId") String clusterId,
+      @PathParam("statemodel") String statemodel) {
+    //Shall we validate the statemodel string not having special character such as ../ etc?
+    if (!StringUtils.isAlphanumeric(statemodel)) {
+      return badRequest("Invalid statemodel name!");
+    }
+
+    HelixDataAccessor dataAccessor = getDataAccssor(clusterId);
+    PropertyKey key = dataAccessor.keyBuilder().stateModelDef(statemodel);
+    boolean retcode = true;
+    try {
+      retcode = dataAccessor.removeProperty(key);
+    } catch (Exception e) {
+      _logger.error("Failed to remove StateModelDefinition key:" + key + ", Exception: " + e);
+      retcode = false;
+    }
+    if (!retcode) {
+      return badRequest("Failed to remove!");
+    }
+    return OK();
+  }
+
+  @GET
+  @Path("{clusterId}/maintenance")
+  public Response getClusterMaintenanceMode(@PathParam("clusterId") String clusterId) {
+    return JSONRepresentation(
+        ImmutableMap.of(ClusterProperties.maintenance.name(), getHelixAdmin().isInMaintenanceMode(clusterId)));
 
 Review comment:
   Was this line reformatted?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services