Search in sources :

Example 1 with ClusterManagementModeRequest

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);
}
Also used : HelixDataAccessor(org.apache.helix.HelixDataAccessor) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) ClusterManagementModeRequest(org.apache.helix.api.status.ClusterManagementModeRequest) PauseSignal(org.apache.helix.model.PauseSignal) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) Test(org.testng.annotations.Test)

Example 2 with ClusterManagementModeRequest

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));
}
Also used : Message(org.apache.helix.model.Message) ClusterManagementModeRequest(org.apache.helix.api.status.ClusterManagementModeRequest) Resource(org.apache.helix.model.Resource) ControllerHistory(org.apache.helix.model.ControllerHistory) CountDownLatch(java.util.concurrent.CountDownLatch) PauseSignal(org.apache.helix.model.PauseSignal) List(java.util.List) PropertyKey(org.apache.helix.PropertyKey) ClusterStatus(org.apache.helix.model.ClusterStatus) Test(org.testng.annotations.Test)

Example 3 with ClusterManagementModeRequest

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)));
}
Also used : ClusterManagementModeRequest(org.apache.helix.api.status.ClusterManagementModeRequest) ControllerHistory(org.apache.helix.model.ControllerHistory) List(java.util.List) ClusterStatus(org.apache.helix.model.ClusterStatus) Test(org.testng.annotations.Test)

Example 4 with ClusterManagementModeRequest

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);
    }
}
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 5 with ClusterManagementModeRequest

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));
}
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

ClusterManagementModeRequest (org.apache.helix.api.status.ClusterManagementModeRequest)6 Test (org.testng.annotations.Test)5 ClusterStatus (org.apache.helix.model.ClusterStatus)3 ControllerHistory (org.apache.helix.model.ControllerHistory)3 PauseSignal (org.apache.helix.model.PauseSignal)3 List (java.util.List)2 HelixDataAccessor (org.apache.helix.HelixDataAccessor)2 HelixException (org.apache.helix.HelixException)2 PropertyKey (org.apache.helix.PropertyKey)2 HelixConflictException (org.apache.helix.api.exceptions.HelixConflictException)2 ResponseMetered (com.codahale.metrics.annotation.ResponseMetered)1 Timed (com.codahale.metrics.annotation.Timed)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 ManagementControllerDataProvider (org.apache.helix.controller.dataproviders.ManagementControllerDataProvider)1