use of org.apache.helix.api.status.ClusterManagementModeRequest in project helix by apache.
the class TestClusterAccessor method testClusterFreezeMode.
@Test
public void testClusterFreezeMode() throws Exception {
String cluster = "TestCluster_0";
HelixDataAccessor dataAccessor = new ZKHelixDataAccessor(cluster, new ZkBaseDataAccessor<>(_gZkClient));
// Pause not existed
Assert.assertNull(dataAccessor.getProperty(dataAccessor.keyBuilder().pause()));
String endpoint = "clusters/" + cluster + "/management-mode";
// Set cluster pause mode
ClusterManagementModeRequest request = ClusterManagementModeRequest.newBuilder().withMode(ClusterManagementMode.Type.CLUSTER_FREEZE).withClusterName(cluster).build();
String payload = OBJECT_MAPPER.writeValueAsString(request);
post(endpoint, null, Entity.entity(payload, MediaType.APPLICATION_JSON_TYPE), Response.Status.OK.getStatusCode());
PauseSignal pauseSignal = dataAccessor.getProperty(dataAccessor.keyBuilder().pause());
Assert.assertNotNull(pauseSignal);
Assert.assertTrue(pauseSignal.isClusterPause());
Assert.assertFalse(pauseSignal.getCancelPendingST());
// Wait until cluster status is persisted
TestHelper.verify(() -> dataAccessor.getBaseDataAccessor().exists(dataAccessor.keyBuilder().clusterStatus().getPath(), AccessOption.PERSISTENT), TestHelper.WAIT_DURATION);
// Verify get cluster status
String body = get(endpoint, null, Response.Status.OK.getStatusCode(), true);
Map<String, Object> responseMap = OBJECT_MAPPER.readerFor(Map.class).readValue(body);
Assert.assertEquals(responseMap.get("mode"), ClusterManagementMode.Type.CLUSTER_FREEZE.name());
// Depending on timing, it could IN_PROGRESS or COMPLETED.
// It's just to verify the rest response format is correct
String status = (String) responseMap.get("status");
Assert.assertTrue(ClusterManagementMode.Status.IN_PROGRESS.name().equals(status) || ClusterManagementMode.Status.COMPLETED.name().equals(status));
body = get(endpoint, ImmutableMap.of("showDetails", "true"), Response.Status.OK.getStatusCode(), true);
responseMap = OBJECT_MAPPER.readerFor(Map.class).readValue(body);
Map<String, Object> detailsMap = (Map<String, Object>) responseMap.get("details");
status = (String) responseMap.get("status");
Assert.assertEquals(responseMap.get("cluster"), cluster);
Assert.assertEquals(responseMap.get("mode"), ClusterManagementMode.Type.CLUSTER_FREEZE.name());
Assert.assertEquals(responseMap.get("status"), status);
Assert.assertTrue(responseMap.containsKey("details"));
Assert.assertTrue(detailsMap.containsKey("cluster"));
Assert.assertTrue(detailsMap.containsKey("liveInstances"));
// set normal mode
request = ClusterManagementModeRequest.newBuilder().withMode(ClusterManagementMode.Type.NORMAL).withClusterName(cluster).build();
payload = OBJECT_MAPPER.writeValueAsString(request);
post(endpoint, null, Entity.entity(payload, MediaType.APPLICATION_JSON_TYPE), Response.Status.OK.getStatusCode());
// Pause signal is deleted
pauseSignal = dataAccessor.getProperty(dataAccessor.keyBuilder().pause());
Assert.assertNull(pauseSignal);
}
use of org.apache.helix.api.status.ClusterManagementModeRequest in project helix by apache.
the class TestClusterFreezeMode method testEnableFreezeMode.
/*
* Tests below scenarios:
* 1. cluster is in progress to freeze mode if there is a pending state transition message;
* 2. after state transition is completed, cluster freeze mode is completed
*
* Also tests cluster status and management mode history recording.
*/
@Test
public void testEnableFreezeMode() throws Exception {
String methodName = TestHelper.getTestMethodName();
// Not in freeze mode
PropertyKey.Builder keyBuilder = _accessor.keyBuilder();
PauseSignal pauseSignal = _accessor.getProperty(keyBuilder.pause());
Assert.assertNull(pauseSignal);
// Block state transition for participants[1]
CountDownLatch latch = new CountDownLatch(1);
_participants[1].setTransition(new BlockingTransition(latch));
// Send a state transition message to participants[1]
Resource resource = new Resource("TestDB0");
resource.setStateModelFactoryName(HelixConstants.DEFAULT_STATE_MODEL_FACTORY);
Message message = MessageUtil.createStateTransitionMessage(_manager.getInstanceName(), _manager.getSessionId(), resource, "TestDB0_1", _participants[1].getInstanceName(), "SLAVE", "OFFLINE", _participants[1].getSessionId(), "MasterSlave");
Assert.assertTrue(_accessor.updateProperty(keyBuilder.message(message.getTgtName(), message.getMsgId()), message));
// Freeze cluster
ClusterManagementModeRequest request = ClusterManagementModeRequest.newBuilder().withClusterName(_clusterName).withMode(ClusterManagementMode.Type.CLUSTER_FREEZE).withReason(methodName).build();
_gSetupTool.getClusterManagementTool().setClusterManagementMode(request);
// Wait for all live instances are marked as frozen
verifyLiveInstanceStatus(_participants, LiveInstance.LiveInstanceStatus.FROZEN);
// Pending ST message exists
Assert.assertTrue(_gZkClient.exists(keyBuilder.message(message.getTgtName(), message.getMsgId()).getPath()));
// Even live instance status is marked as frozen, Cluster is in progress to cluster freeze
// because there is a pending state transition message
ClusterStatus expectedClusterStatus = new ClusterStatus();
expectedClusterStatus.setManagementMode(ClusterManagementMode.Type.CLUSTER_FREEZE);
expectedClusterStatus.setManagementModeStatus(ClusterManagementMode.Status.IN_PROGRESS);
verifyClusterStatus(expectedClusterStatus);
// Verify management mode history is empty
ControllerHistory controllerHistory = _accessor.getProperty(_accessor.keyBuilder().controllerLeaderHistory());
List<String> managementHistory = controllerHistory.getManagementModeHistory();
Assert.assertTrue(managementHistory.isEmpty());
// Unblock to finish state transition and delete the ST message
latch.countDown();
// Verify live instance status and cluster status
verifyLiveInstanceStatus(_participants, LiveInstance.LiveInstanceStatus.FROZEN);
expectedClusterStatus = new ClusterStatus();
expectedClusterStatus.setManagementMode(ClusterManagementMode.Type.CLUSTER_FREEZE);
expectedClusterStatus.setManagementModeStatus(ClusterManagementMode.Status.COMPLETED);
verifyClusterStatus(expectedClusterStatus);
// Verify management mode history
Assert.assertTrue(TestHelper.verify(() -> {
ControllerHistory tmpControllerHistory = _accessor.getProperty(keyBuilder.controllerLeaderHistory());
List<String> tmpManagementHistory = tmpControllerHistory.getManagementModeHistory();
if (tmpManagementHistory == null || tmpManagementHistory.isEmpty()) {
return false;
}
// Should not have duplicate entries
if (tmpManagementHistory.size() > 1) {
return false;
}
String lastHistory = tmpManagementHistory.get(0);
return lastHistory.contains("MODE=" + ClusterManagementMode.Type.CLUSTER_FREEZE) && lastHistory.contains("STATUS=" + ClusterManagementMode.Status.COMPLETED) && lastHistory.contains("REASON=" + methodName);
}, TestHelper.WAIT_DURATION));
}
use of org.apache.helix.api.status.ClusterManagementModeRequest in project helix by apache.
the class TestClusterFreezeMode method testUnfreezeCluster.
@Test(dependsOnMethods = "testCreateResourceWhenFrozen")
public void testUnfreezeCluster() throws Exception {
String methodName = TestHelper.getTestMethodName();
// Unfreeze cluster
ClusterManagementModeRequest request = ClusterManagementModeRequest.newBuilder().withClusterName(_clusterName).withMode(ClusterManagementMode.Type.NORMAL).withReason(methodName).build();
_gSetupTool.getClusterManagementTool().setClusterManagementMode(request);
verifyLiveInstanceStatus(_participants, LiveInstance.LiveInstanceStatus.NORMAL);
ClusterStatus expectedClusterStatus = new ClusterStatus();
expectedClusterStatus.setManagementMode(ClusterManagementMode.Type.NORMAL);
expectedClusterStatus.setManagementModeStatus(ClusterManagementMode.Status.COMPLETED);
verifyClusterStatus(expectedClusterStatus);
// Verify management mode history: NORMAL + COMPLETED
Assert.assertTrue(TestHelper.verify(() -> {
ControllerHistory history = _accessor.getProperty(_accessor.keyBuilder().controllerLeaderHistory());
List<String> managementHistory = history.getManagementModeHistory();
if (managementHistory == null || managementHistory.isEmpty()) {
return false;
}
String lastHistory = managementHistory.get(managementHistory.size() - 1);
return lastHistory.contains("MODE=" + ClusterManagementMode.Type.NORMAL) && lastHistory.contains("STATUS=" + ClusterManagementMode.Status.COMPLETED);
}, TestHelper.WAIT_DURATION));
// Verify cluster's normal rebalance ability after unfrozen.
Assert.assertTrue(ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, _clusterName)));
}
use of org.apache.helix.api.status.ClusterManagementModeRequest in project helix by apache.
the class TestZkHelixAdmin method testEnableDisableClusterPauseMode.
/*
* Tests 2 APIs: enable and disable cluster pause mode.
*/
@Test
public void testEnableDisableClusterPauseMode() {
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
_gSetupTool.setupTestCluster(clusterName);
ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller_0");
controller.syncStart();
_gSetupTool.activateCluster(clusterName, controller.getClusterName(), true);
try {
// Should not create pause with pending cancel ST enabled because cancellation is not enabled
try {
ClusterManagementModeRequest request = ClusterManagementModeRequest.newBuilder().withClusterName(clusterName).withMode(ClusterManagementMode.Type.CLUSTER_FREEZE).withCancelPendingST(true).withReason(methodName).build();
_gSetupTool.getClusterManagementTool().setClusterManagementMode(request);
Assert.fail("Should not create pause with pending cancel ST enabled because " + "cancellation is not enabled");
} catch (HelixConflictException e) {
Assert.assertTrue(e.getMessage().startsWith("State transition cancellation not enabled"));
}
ClusterManagementModeRequest request = ClusterManagementModeRequest.newBuilder().withClusterName(clusterName).withMode(ClusterManagementMode.Type.CLUSTER_FREEZE).withReason(methodName).build();
_gSetupTool.getClusterManagementTool().setClusterManagementMode(request);
HelixDataAccessor dataAccessor = new ZKHelixDataAccessor(clusterName, _baseAccessor);
PauseSignal pauseSignal = dataAccessor.getProperty(dataAccessor.keyBuilder().pause());
// Verify pause signal is correctly written
Assert.assertNotNull(pauseSignal);
Assert.assertTrue(pauseSignal.isClusterPause());
Assert.assertFalse(pauseSignal.getCancelPendingST());
Assert.assertEquals(pauseSignal.getFromHost(), NetworkUtil.getLocalhostName());
Assert.assertEquals(pauseSignal.getReason(), methodName);
// Disable pause mode
request = ClusterManagementModeRequest.newBuilder().withClusterName(clusterName).withMode(ClusterManagementMode.Type.NORMAL).build();
_gSetupTool.getClusterManagementTool().setClusterManagementMode(request);
pauseSignal = dataAccessor.getProperty(dataAccessor.keyBuilder().pause());
// Verify pause signal has been deleted.
Assert.assertNull(pauseSignal);
} finally {
_gSetupTool.activateCluster(clusterName, controller.getClusterName(), false);
controller.syncStop();
_gSetupTool.deleteCluster(clusterName);
}
}
use of org.apache.helix.api.status.ClusterManagementModeRequest in project helix by apache.
the class ClusterAccessor method updateClusterManagementMode.
@ClusterAuth
@ResponseMetered(name = HttpConstants.WRITE_REQUEST)
@Timed(name = HttpConstants.WRITE_REQUEST)
@POST
@Path("{clusterId}/management-mode")
public Response updateClusterManagementMode(@PathParam("clusterId") String clusterId, @DefaultValue("{}") String content) {
ClusterManagementModeRequest request;
try {
request = OBJECT_MAPPER.readerFor(ClusterManagementModeRequest.class).readValue(content);
} catch (JsonProcessingException e) {
LOG.warn("Failed to parse json string: {}", content, e);
return badRequest("Invalid payload json body: " + content);
}
// Need to add cluster name
request = ClusterManagementModeRequest.newBuilder().withClusterName(clusterId).withMode(request.getMode()).withCancelPendingST(request.isCancelPendingST()).withReason(request.getReason()).build();
try {
getHelixAdmin().setClusterManagementMode(request);
} catch (HelixConflictException e) {
return Response.status(Response.Status.CONFLICT).entity(e.getMessage()).build();
} catch (HelixException e) {
return serverError(e.getMessage());
}
return JSONRepresentation(ImmutableMap.of("acknowledged", true));
}
Aggregations