use of org.apache.helix.api.exceptions.HelixConflictException in project helix by apache.
the class ZKHelixAdmin method enableClusterPauseMode.
private void enableClusterPauseMode(String clusterName, boolean cancelPendingST, String reason) {
String hostname = NetworkUtil.getLocalhostName();
logger.info("Enable cluster pause mode for cluster: {}. CancelPendingST: {}. Reason: {}. From Host: {}", clusterName, cancelPendingST, reason, hostname);
BaseDataAccessor<ZNRecord> baseDataAccessor = new ZkBaseDataAccessor<>(_zkClient);
HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseDataAccessor);
if (baseDataAccessor.exists(accessor.keyBuilder().pause().getPath(), AccessOption.PERSISTENT)) {
throw new HelixConflictException(clusterName + " pause signal already exists");
}
// check whether cancellation is enabled
ClusterConfig config = accessor.getProperty(accessor.keyBuilder().clusterConfig());
if (cancelPendingST && !config.isStateTransitionCancelEnabled()) {
throw new HelixConflictException("State transition cancellation not enabled in " + clusterName);
}
PauseSignal pauseSignal = new PauseSignal();
pauseSignal.setClusterPause(true);
pauseSignal.setCancelPendingST(cancelPendingST);
pauseSignal.setFromHost(hostname);
pauseSignal.setTriggerTime(Instant.now().toEpochMilli());
if (reason != null && !reason.isEmpty()) {
pauseSignal.setReason(reason);
}
// TODO: merge management status signal into one znode to avoid race condition
if (!accessor.createPause(pauseSignal)) {
throw new HelixException("Failed to create pause signal");
}
}
use of org.apache.helix.api.exceptions.HelixConflictException 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.exceptions.HelixConflictException 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