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


The following commit(s) were added to refs/heads/master by this push:
     new 0635ed4  Check maitenanceMode rest api implementation - Refactor the 
getClusterInfo method in ClusterAccessor rest api - Changes in paused and 
maintenance mode
0635ed4 is described below

commit 0635ed4f996bab8115fe9915f19abcc77f80879b
Author: ywang4 <[email protected]>
AuthorDate: Mon Feb 4 15:44:33 2019 -0800

    Check maitenanceMode rest api implementation - Refactor the getClusterInfo 
method in ClusterAccessor rest api - Changes in paused and maintenance mode
---
 .../org/apache/helix/manager/zk/ZKHelixAdmin.java  |   1 +
 .../server/resources/helix/ClusterAccessor.java    |  27 +++---
 .../helix/rest/server/TestClusterAccessor.java     | 101 ++++++++++++---------
 3 files changed, 76 insertions(+), 53 deletions(-)

diff --git 
a/helix-core/src/main/java/org/apache/helix/manager/zk/ZKHelixAdmin.java 
b/helix-core/src/main/java/org/apache/helix/manager/zk/ZKHelixAdmin.java
index 511ac8c..3db3849 100644
--- a/helix-core/src/main/java/org/apache/helix/manager/zk/ZKHelixAdmin.java
+++ b/helix-core/src/main/java/org/apache/helix/manager/zk/ZKHelixAdmin.java
@@ -397,6 +397,7 @@ public class ZKHelixAdmin implements HelixAdmin {
   }
 
   @Override
+  @Deprecated
   public void enableMaintenanceMode(String clusterName, boolean enabled, 
String reason) {
     manuallyEnableMaintenanceMode(clusterName, enabled, reason, null);
   }
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 f24076a..51dde95 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
@@ -19,20 +19,22 @@ package org.apache.helix.rest.server.resources.helix;
  * under the License.
  */
 
+import com.google.common.collect.ImmutableMap;
 import java.io.IOException;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import javax.ws.rs.DELETE;
+import javax.ws.rs.DefaultValue;
 import javax.ws.rs.GET;
 import javax.ws.rs.POST;
-import javax.ws.rs.DefaultValue;
 import javax.ws.rs.PUT;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.Response;
+import org.apache.helix.AccessOption;
 import org.apache.helix.ConfigAccessor;
 import org.apache.helix.HelixAdmin;
 import org.apache.helix.HelixDataAccessor;
@@ -42,8 +44,8 @@ import org.apache.helix.ZNRecord;
 import org.apache.helix.manager.zk.ZKUtil;
 import org.apache.helix.manager.zk.client.HelixZkClient;
 import org.apache.helix.model.ClusterConfig;
-import org.apache.helix.model.HelixConfigScope;
 import org.apache.helix.model.ControllerHistory;
+import org.apache.helix.model.HelixConfigScope;
 import org.apache.helix.model.LiveInstance;
 import org.apache.helix.model.Message;
 import org.apache.helix.model.StateModelDefinition;
@@ -83,7 +85,7 @@ public class ClusterAccessor extends AbstractHelixResource {
   @GET
   @Path("{clusterId}")
   public Response getClusterInfo(@PathParam("clusterId") String clusterId) {
-    if (!isClusterExist(clusterId)) {
+    if (!doesClusterExist(clusterId)) {
       return notFound();
     }
 
@@ -100,10 +102,9 @@ public class ClusterAccessor extends AbstractHelixResource 
{
       clusterInfo.put(ClusterProperties.controller.name(), "No Lead 
Controller!");
     }
 
-    boolean paused = (dataAccessor.getProperty(keyBuilder.pause()) == null ? 
false : true);
+    boolean paused = 
dataAccessor.getBaseDataAccessor().exists(keyBuilder.pause().getPath(), 
AccessOption.PERSISTENT);
     clusterInfo.put(ClusterProperties.paused.name(), paused);
-    boolean maintenance =
-        (dataAccessor.getProperty(keyBuilder.maintenance()) == null ? false : 
true);
+    boolean maintenance = getHelixAdmin().isInMaintenanceMode(clusterId);
     clusterInfo.put(ClusterProperties.maintenance.name(), maintenance);
 
     List<String> idealStates = 
dataAccessor.getChildNames(keyBuilder.idealStates());
@@ -380,6 +381,13 @@ public class ClusterAccessor extends AbstractHelixResource 
{
   }
 
   @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) {
@@ -390,12 +398,9 @@ public class ClusterAccessor extends AbstractHelixResource 
{
     return JSONRepresentation(stateModelDef.getRecord());
   }
 
-  private boolean isClusterExist(String cluster) {
+  private boolean doesClusterExist(String cluster) {
     HelixZkClient zkClient = getHelixZkClient();
-    if (ZKUtil.isClusterSetup(cluster, zkClient)) {
-      return true;
-    }
-    return false;
+    return ZKUtil.isClusterSetup(cluster, zkClient);
   }
 
   /**
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 b2a14cf..604c682 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
@@ -19,8 +19,6 @@ package org.apache.helix.rest.server;
  * under the License.
  */
 
-import com.google.common.collect.ImmutableMap;
-import com.sun.research.ws.wadl.HTTPMethods;
 import java.io.IOException;
 import java.net.URLEncoder;
 import java.util.Arrays;
@@ -29,9 +27,11 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+
 import javax.ws.rs.client.Entity;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
+
 import org.apache.helix.HelixDataAccessor;
 import org.apache.helix.PropertyKey;
 import org.apache.helix.TestHelper;
@@ -52,6 +52,9 @@ import org.testng.Assert;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
+import com.google.common.collect.ImmutableMap;
+import com.sun.research.ws.wadl.HTTPMethods;
+
 public class TestClusterAccessor extends AbstractTestClass {
 
   @BeforeClass
@@ -92,10 +95,12 @@ public class TestClusterAccessor extends AbstractTestClass {
     ClusterConfig configDelta = new ClusterConfig(cluster);
     configDelta.getRecord().setSimpleField("newField", "newValue");
     configDelta.getRecord().setListField("newList", Arrays.asList("newValue1", 
"newValue2"));
-    configDelta.getRecord().setMapField("newMap", new HashMap<String, 
String>() {{
-      put("newkey1", "newvalue1");
-      put("newkey2", "newvalue2");
-    }});
+    configDelta.getRecord().setMapField("newMap", new HashMap<String, 
String>() {
+      {
+        put("newkey1", "newvalue1");
+        put("newkey2", "newvalue2");
+      }
+    });
 
     updateClusterConfigFromRest(cluster, configDelta, Command.update);
 
@@ -139,13 +144,12 @@ public class TestClusterAccessor extends 
AbstractTestClass {
     ClusterConfig newConfig = getClusterConfigFromRest(cluster);
 
     prevConfig.getRecord().update(config.getRecord());
-    Assert.assertEquals(newConfig, prevConfig,
-        "cluster config from response: " + newConfig + " vs cluster config 
actually: " + prevConfig);
+    Assert.assertEquals(newConfig, prevConfig, "cluster config from response: 
" + newConfig
+        + " vs cluster config actually: " + prevConfig);
   }
 
-  @Test (dependsOnMethods = "testUpdateConfigFields")
-  public void testDeleteConfigFields()
-      throws IOException {
+  @Test(dependsOnMethods = "testUpdateConfigFields")
+  public void testDeleteConfigFields() throws IOException {
     System.out.println("Start test :" + TestHelper.getTestMethodName());
     String cluster = _clusters.iterator().next();
     ClusterConfig config = getClusterConfigFromRest(cluster);
@@ -179,9 +183,8 @@ public class TestClusterAccessor extends AbstractTestClass {
         "Failed to delete key " + mapKey + " from cluster config");
 
     prevConfig.getRecord().subtract(config.getRecord());
-    Assert.assertEquals(newConfig, prevConfig,
-        "cluster config from response: " + newConfig + " vs cluster config 
actually: "
-            + prevConfig);
+    Assert.assertEquals(newConfig, prevConfig, "cluster config from response: 
" + newConfig
+        + " vs cluster config actually: " + prevConfig);
   }
 
   @Test(dependsOnMethods = "testDeleteConfigFields")
@@ -243,21 +246,32 @@ public class TestClusterAccessor extends 
AbstractTestClass {
   }
 
   @Test(dependsOnMethods = "testGetClusterConfig")
-  public void testEnableDisableMaintenanceMode() {
+  public void testEnableDisableMaintenanceMode() throws IOException {
     System.out.println("Start test :" + TestHelper.getTestMethodName());
     String cluster = _clusters.iterator().next();
     String reason = "Test reason";
-    HelixDataAccessor accessor = new ZKHelixDataAccessor(cluster, 
_baseAccessor);
+    // enable maintenance mode
     post("clusters/" + cluster, ImmutableMap.of("command", 
"enableMaintenanceMode"),
-        Entity.entity(reason, MediaType.APPLICATION_JSON_TYPE),
-        Response.Status.OK.getStatusCode());
-    MaintenanceSignal signal = 
accessor.getProperty(accessor.keyBuilder().maintenance());
-    Assert.assertNotNull(signal);
-    Assert.assertEquals(reason, signal.getReason());
+        Entity.entity(reason, MediaType.APPLICATION_JSON_TYPE), 
Response.Status.OK.getStatusCode());
+
+    // verify is in maintenance mode
+    String body =
+        get("clusters/" + cluster + "/maintenance", 
Response.Status.OK.getStatusCode(), true);
+    JsonNode node = OBJECT_MAPPER.readTree(body);
+    boolean maintenance =
+        
node.get(ClusterAccessor.ClusterProperties.maintenance.name()).getBooleanValue();
+    Assert.assertTrue(maintenance);
+
+    // disable maintenance mode
     post("clusters/" + cluster, ImmutableMap.of("command", 
"disableMaintenanceMode"),
         Entity.entity("", MediaType.APPLICATION_JSON_TYPE),
         Response.Status.OK.getStatusCode());
-    
Assert.assertNull(accessor.getProperty(accessor.keyBuilder().maintenance()));
+
+    // verify no longer in maintenance mode
+    body = get("clusters/" + cluster + "/maintenance", 
Response.Status.OK.getStatusCode(), true);
+    node = OBJECT_MAPPER.readTree(body);
+    Assert.assertFalse(
+        
node.get(ClusterAccessor.ClusterProperties.maintenance.name()).getBooleanValue());
   }
 
   @Test
@@ -354,18 +368,17 @@ public class TestClusterAccessor extends 
AbstractTestClass {
     ZNRecord record = new 
ObjectMapper().reader(ZNRecord.class).readValue(body);
     ClusterConfig clusterConfigRest = new ClusterConfig(record);
     ClusterConfig clusterConfigZk = _configAccessor.getClusterConfig(cluster);
-    Assert.assertEquals(clusterConfigZk, clusterConfigRest,
-        "cluster config from response: " + clusterConfigRest + " vs cluster 
config actually: "
-            + clusterConfigZk);
+    Assert.assertEquals(clusterConfigZk, clusterConfigRest, "cluster config 
from response: "
+        + clusterConfigRest + " vs cluster config actually: " + 
clusterConfigZk);
 
     return clusterConfigRest;
   }
 
-  private void updateClusterConfigFromRest(String cluster, ClusterConfig 
newConfig,
-      Command command) throws IOException {
+  private void updateClusterConfigFromRest(String cluster, ClusterConfig 
newConfig, Command command)
+      throws IOException {
     _auditLogger.clearupLogs();
-    Entity entity = Entity
-        .entity(OBJECT_MAPPER.writeValueAsString(newConfig.getRecord()), 
MediaType.APPLICATION_JSON_TYPE);
+    Entity entity = 
Entity.entity(OBJECT_MAPPER.writeValueAsString(newConfig.getRecord()),
+        MediaType.APPLICATION_JSON_TYPE);
     post("clusters/" + cluster + "/configs", ImmutableMap.of("command", 
command.name()), entity,
         Response.Status.OK.getStatusCode());
 
@@ -382,19 +395,23 @@ public class TestClusterAccessor extends 
AbstractTestClass {
     clusterConfig.getRecord().setSimpleField("SimpleField1", "Value1");
     clusterConfig.getRecord().setSimpleField("SimpleField2", "Value2");
 
-    clusterConfig.getRecord()
-        .setListField("ListField1", Arrays.asList("Value1", "Value2", 
"Value3"));
-    clusterConfig.getRecord()
-        .setListField("ListField2", Arrays.asList("Value2", "Value1", 
"Value3"));
-
-    clusterConfig.getRecord().setMapField("MapField1", new HashMap<String, 
String>() {{
-      put("key1", "value1");
-      put("key2", "value2");
-    }});
-    clusterConfig.getRecord().setMapField("MapField2", new HashMap<String, 
String>() {{
-      put("key3", "value1");
-      put("key4", "value2");
-    }});
+    clusterConfig.getRecord().setListField("ListField1",
+        Arrays.asList("Value1", "Value2", "Value3"));
+    clusterConfig.getRecord().setListField("ListField2",
+        Arrays.asList("Value2", "Value1", "Value3"));
+
+    clusterConfig.getRecord().setMapField("MapField1", new HashMap<String, 
String>() {
+      {
+        put("key1", "value1");
+        put("key2", "value2");
+      }
+    });
+    clusterConfig.getRecord().setMapField("MapField2", new HashMap<String, 
String>() {
+      {
+        put("key3", "value1");
+        put("key4", "value2");
+      }
+    });
 
     return clusterConfig;
   }

Reply via email to