This is an automated email from the ASF dual-hosted git repository.

jxue pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/helix.git

commit db33ae5dbe5c50c0db62d3abc828530bc114b0ea
Author: narendly <[email protected]>
AuthorDate: Tue Feb 26 17:19:14 2019 -0800

    [HELIX-806] HELIX: Modify endpoints for instrumenting maintenance with 
custom fields
    
    We want to use content for users to input their KV mappings as a JSON 
string.
        Changelist:
        1. Modify enable/disableMaintenanceMode endpoint logic
        2. Modify tests
---
 .../server/resources/helix/ClusterAccessor.java    | 28 +++++++++++-----------
 .../helix/rest/server/TestClusterAccessor.java     | 15 ++++--------
 2 files changed, 19 insertions(+), 24 deletions(-)

diff --git 
a/helix-rest/src/main/java/org/apache/helix/rest/server/resources/helix/ClusterAccessor.java
 
b/helix-rest/src/main/java/org/apache/helix/rest/server/resources/helix/ClusterAccessor.java
index 51dde95..c81f467 100644
--- 
a/helix-rest/src/main/java/org/apache/helix/rest/server/resources/helix/ClusterAccessor.java
+++ 
b/helix-rest/src/main/java/org/apache/helix/rest/server/resources/helix/ClusterAccessor.java
@@ -158,7 +158,7 @@ public class ClusterAccessor extends AbstractHelixResource {
   @Path("{clusterId}")
   public Response updateCluster(@PathParam("clusterId") String clusterId,
       @QueryParam("command") String commandStr, @QueryParam("superCluster") 
String superCluster,
-      @QueryParam("customFields") String customFields, String content) {
+      String content) {
     Command command;
     try {
       command = getCommand(commandStr);
@@ -211,20 +211,21 @@ public class ClusterAccessor extends 
AbstractHelixResource {
 
     case enableMaintenanceMode:
     case disableMaintenanceMode:
+      // Try to parse the content string. If parseable, use it as a KV 
mapping. Otherwise, treat it
+      // as a REASON String
+      Map<String, String> customFieldsMap = null;
       try {
-        Map<String, String> customFieldsMap = new HashMap<>();
-        if (customFields != null && !customFields.isEmpty()) {
-          customFieldsMap = OBJECT_MAPPER.readValue(customFields,
-              new TypeReference<HashMap<String, String>>() {
-              });
-        }
-        helixAdmin.manuallyEnableMaintenanceMode(clusterId,
-            command == Command.enableMaintenanceMode, content, 
customFieldsMap);
-      } catch (Exception ex) {
-        _logger.error("Failed to disable maintenance mode for cluster {}. 
Exception: {}", clusterId,
-            ex);
-        return serverError(ex);
+        // Try to parse content
+        customFieldsMap =
+            OBJECT_MAPPER.readValue(content, new TypeReference<HashMap<String, 
String>>() {
+            });
+        // content is given as a KV mapping. Nullify content
+        content = null;
+      } catch (Exception e) {
+        // NOP
       }
+      helixAdmin.manuallyEnableMaintenanceMode(clusterId, command == 
Command.enableMaintenanceMode,
+          content, customFieldsMap);
       break;
 
     default:
@@ -234,7 +235,6 @@ public class ClusterAccessor extends AbstractHelixResource {
     return OK();
   }
 
-
   @GET
   @Path("{clusterId}/configs")
   public Response getClusterConfig(@PathParam("clusterId") String clusterId) {
diff --git 
a/helix-rest/src/test/java/org/apache/helix/rest/server/TestClusterAccessor.java
 
b/helix-rest/src/test/java/org/apache/helix/rest/server/TestClusterAccessor.java
index 604c682..5506509 100644
--- 
a/helix-rest/src/test/java/org/apache/helix/rest/server/TestClusterAccessor.java
+++ 
b/helix-rest/src/test/java/org/apache/helix/rest/server/TestClusterAccessor.java
@@ -20,7 +20,6 @@ package org.apache.helix.rest.server;
  */
 
 import java.io.IOException;
-import java.net.URLEncoder;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -337,20 +336,16 @@ public class TestClusterAccessor extends 
AbstractTestClass {
   public void testEnableDisableMaintenanceModeWithCustomFields() {
     System.out.println("Start test :" + TestHelper.getTestMethodName());
     String cluster = _clusters.iterator().next();
-    String reason = "Test reason";
     HelixDataAccessor accessor = new ZKHelixDataAccessor(cluster, 
_baseAccessor);
 
-    String customFields = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
-    // Note that URLEncoder.encode has to be used due to a Jersey bug
-    // See https://github.com/Mercateo/rest-schemagen/issues/51
-    post("clusters/" + cluster,
-        ImmutableMap.of("command", "enableMaintenanceMode", "customFields",
-            URLEncoder.encode(customFields)),
-        Entity.entity(reason, MediaType.APPLICATION_JSON_TYPE), 
Response.Status.OK.getStatusCode());
+    String content = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
+    post("clusters/" + cluster, ImmutableMap.of("command", 
"enableMaintenanceMode"),
+        Entity.entity(content, MediaType.APPLICATION_JSON_TYPE),
+        Response.Status.OK.getStatusCode());
 
     MaintenanceSignal signal = 
accessor.getProperty(accessor.keyBuilder().maintenance());
     Assert.assertNotNull(signal);
-    Assert.assertEquals(reason, signal.getReason());
+    Assert.assertNull(signal.getReason());
     Assert.assertEquals(signal.getTriggeringEntity(), 
MaintenanceSignal.TriggeringEntity.USER);
     Map<String, String> simpleFields = signal.getRecord().getSimpleFields();
     Assert.assertEquals(simpleFields.get("key1"), "value1");

Reply via email to