Search in sources :

Example 1 with HelixConflictException

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");
    }
}
Also used : HelixException(org.apache.helix.HelixException) HelixDataAccessor(org.apache.helix.HelixDataAccessor) HelixConflictException(org.apache.helix.api.exceptions.HelixConflictException) PauseSignal(org.apache.helix.model.PauseSignal) ZNRecord(org.apache.helix.zookeeper.datamodel.ZNRecord) ClusterConfig(org.apache.helix.model.ClusterConfig)

Example 2 with HelixConflictException

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);
    }
}
Also used : ClusterControllerManager(org.apache.helix.integration.manager.ClusterControllerManager) HelixDataAccessor(org.apache.helix.HelixDataAccessor) ClusterManagementModeRequest(org.apache.helix.api.status.ClusterManagementModeRequest) HelixConflictException(org.apache.helix.api.exceptions.HelixConflictException) PauseSignal(org.apache.helix.model.PauseSignal) Test(org.testng.annotations.Test)

Example 3 with HelixConflictException

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));
}
Also used : HelixException(org.apache.helix.HelixException) ClusterManagementModeRequest(org.apache.helix.api.status.ClusterManagementModeRequest) HelixConflictException(org.apache.helix.api.exceptions.HelixConflictException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Path(javax.ws.rs.Path) ResponseMetered(com.codahale.metrics.annotation.ResponseMetered) ClusterAuth(org.apache.helix.rest.server.filters.ClusterAuth) POST(javax.ws.rs.POST) Timed(com.codahale.metrics.annotation.Timed)

Aggregations

HelixConflictException (org.apache.helix.api.exceptions.HelixConflictException)3 HelixDataAccessor (org.apache.helix.HelixDataAccessor)2 HelixException (org.apache.helix.HelixException)2 ClusterManagementModeRequest (org.apache.helix.api.status.ClusterManagementModeRequest)2 PauseSignal (org.apache.helix.model.PauseSignal)2 ResponseMetered (com.codahale.metrics.annotation.ResponseMetered)1 Timed (com.codahale.metrics.annotation.Timed)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)1 ClusterConfig (org.apache.helix.model.ClusterConfig)1 ClusterAuth (org.apache.helix.rest.server.filters.ClusterAuth)1 ZNRecord (org.apache.helix.zookeeper.datamodel.ZNRecord)1 Test (org.testng.annotations.Test)1