Search in sources :

Example 26 with PropertyKey

use of org.apache.helix.PropertyKey in project helix by apache.

the class HelixTaskExecutor method syncSessionToController.

private void syncSessionToController(HelixManager manager) {
    if (_lastSessionSyncTime == null || System.currentTimeMillis() - _lastSessionSyncTime > SESSION_SYNC_INTERVAL) {
        // > delay since last sync
        HelixDataAccessor accessor = manager.getHelixDataAccessor();
        PropertyKey key = new Builder(manager.getClusterName()).controllerMessage(SESSION_SYNC);
        if (accessor.getProperty(key) == null) {
            LOG.info(String.format("Participant %s syncs session with controller", manager.getInstanceName()));
            Message msg = new Message(MessageType.PARTICIPANT_SESSION_CHANGE, SESSION_SYNC);
            msg.setSrcName(manager.getInstanceName());
            msg.setTgtSessionId("*");
            msg.setMsgState(MessageState.NEW);
            msg.setMsgId(SESSION_SYNC);
            Criteria cr = new Criteria();
            cr.setRecipientInstanceType(InstanceType.CONTROLLER);
            cr.setSessionSpecific(false);
            manager.getMessagingService().send(cr, msg);
            _lastSessionSyncTime = System.currentTimeMillis();
        }
    }
}
Also used : HelixDataAccessor(org.apache.helix.HelixDataAccessor) Message(org.apache.helix.model.Message) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) Builder(org.apache.helix.PropertyKey.Builder) Criteria(org.apache.helix.Criteria) PropertyKey(org.apache.helix.PropertyKey)

Example 27 with PropertyKey

use of org.apache.helix.PropertyKey in project helix by apache.

the class TestSyncSessionToController method testSyncSessionToController.

@Test
public void testSyncSessionToController() throws Exception {
    System.out.println("START testSyncSessionToController at " + new Date(System.currentTimeMillis()));
    String clusterName = getShortClassName();
    MockParticipantManager[] participants = new MockParticipantManager[5];
    int resourceNb = 10;
    // participant port
    TestHelper.setupCluster(// participant port
    clusterName, // participant port
    ZK_ADDR, // participant port
    12918, // participant name prefix
    "localhost", // resource name prefix
    "TestDB", // resources
    resourceNb, // partitions per resource
    1, // number of nodes
    5, // replicas
    1, "MasterSlave", // do rebalance
    true);
    ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller_0");
    controller.syncStart();
    // start participants
    for (int i = 0; i < 5; i++) {
        String instanceName = "localhost_" + (12918 + i);
        participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
        participants[i].syncStart();
    }
    ZKHelixManager zkHelixManager = new ZKHelixManager(clusterName, "controllerMessageListener", InstanceType.CONTROLLER, ZK_ADDR);
    zkHelixManager.connect();
    MockMessageListener mockMessageListener = new MockMessageListener();
    zkHelixManager.addControllerMessageListener(mockMessageListener);
    PropertyKey.Builder keyBuilder = new PropertyKey.Builder(clusterName);
    ZkBaseDataAccessor<ZNRecord> accessor = new ZkBaseDataAccessor<>(_gZkClient);
    String path = keyBuilder.liveInstance("localhost_12918").getPath();
    Stat stat = new Stat();
    ZNRecord data = accessor.get(path, stat, 2);
    data.getSimpleFields().put("SESSION_ID", "invalid-id");
    accessor.set(path, data, 2);
    Thread.sleep(2000);
    // Since we always read the content from ephemeral nodes, sync message won't be sent
    Assert.assertFalse(mockMessageListener.isSessionSyncMessageSent());
    // Even after reconnect, session sync won't happen
    ZkTestHelper.expireSession(participants[0].getZkClient());
    Assert.assertFalse(mockMessageListener.isSessionSyncMessageSent());
    // Inject an invalid session message to trigger sync message
    PropertyKey messageKey = keyBuilder.message("localhost_12918", "Mocked Invalid Message");
    Message msg = new Message(Message.MessageType.STATE_TRANSITION, "Mocked Invalid Message");
    msg.setSrcName(controller.getInstanceName());
    msg.setTgtSessionId("invalid-id");
    msg.setMsgState(Message.MessageState.NEW);
    msg.setMsgId("Mocked Invalid Message");
    msg.setTgtName("localhost_12918");
    msg.setPartitionName("foo");
    msg.setResourceName("bar");
    msg.setFromState("SLAVE");
    msg.setToState("MASTER");
    msg.setSrcSessionId(controller.getSessionId());
    msg.setStateModelDef("MasterSlave");
    msg.setStateModelFactoryName("DEFAULT");
    HelixDataAccessor dataAccessor = new ZKHelixDataAccessor(clusterName, accessor);
    dataAccessor.setProperty(messageKey, msg);
    Assert.assertTrue(TestHelper.verify(() -> mockMessageListener.isSessionSyncMessageSent(), 1500));
    // Cleanup
    controller.syncStop();
    zkHelixManager.disconnect();
    for (int i = 0; i < 5; i++) {
        participants[i].syncStop();
    }
    deleteCluster(clusterName);
}
Also used : ZkBaseDataAccessor(org.apache.helix.manager.zk.ZkBaseDataAccessor) MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) Message(org.apache.helix.model.Message) ZKHelixManager(org.apache.helix.manager.zk.ZKHelixManager) Date(java.util.Date) ClusterControllerManager(org.apache.helix.integration.manager.ClusterControllerManager) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) HelixDataAccessor(org.apache.helix.HelixDataAccessor) Stat(org.apache.zookeeper.data.Stat) PropertyKey(org.apache.helix.PropertyKey) ZNRecord(org.apache.helix.zookeeper.datamodel.ZNRecord) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) Test(org.testng.annotations.Test)

Example 28 with PropertyKey

use of org.apache.helix.PropertyKey in project helix by apache.

the class TestNoThrottleDisabledPartitions method disablePartitionOnInstance.

/**
 * Disable select partitions from the first instance to test that these partitions are not subject
 * to throttling.
 */
private void disablePartitionOnInstance(MockParticipantManager participant, String resourceName, String partitionName) {
    String instanceName = participant.getInstanceName();
    PropertyKey key = _accessor.keyBuilder().instanceConfig(instanceName);
    InstanceConfig instanceConfig = _accessor.getProperty(key);
    instanceConfig.setInstanceEnabledForPartition(resourceName, partitionName, false);
    _accessor.setProperty(key, instanceConfig);
}
Also used : InstanceConfig(org.apache.helix.model.InstanceConfig) PropertyKey(org.apache.helix.PropertyKey)

Example 29 with PropertyKey

use of org.apache.helix.PropertyKey in project helix by apache.

the class TestNoThrottleDisabledPartitions method testDisablingPartitionOnInstance.

/**
 * Given the following setup for a partition:
 * instance 1 : M
 * instance 2 : S
 * instance 3 : S
 * and no throttle config set for recovery balance
 * and throttle config of 1 set for load balance,
 * Instead of disabling the instance, we disable the partition in the instance config.
 * * instance 1 : S (M->S->Offline)
 * * instance 2 : M (S->M because it's in recovery)
 * * instance 3 : S
 * @throws Exception
 */
@Test
public void testDisablingPartitionOnInstance() throws Exception {
    int participantCount = 5;
    setupEnvironment(participantCount);
    // Set the throttling only for load balance
    setThrottleConfigForLoadBalance();
    // In this setup, TestDB0_2 has a MASTER replica on localhost_12918
    disablePartitionOnInstance(_participants[0], _resourceName + "0", "TestDB0_2");
    // Resume the controller
    ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, _clusterName, "controller_0");
    controller.syncStart();
    Thread.sleep(500L);
    // The disabled instance should not hold any top state replicas (MASTER)
    PropertyKey liveInstanceKey = _accessor.keyBuilder().liveInstance(_participants[0].getInstanceName());
    LiveInstance liveInstance = _accessor.getProperty(liveInstanceKey);
    if (liveInstance != null) {
        String sessionId = liveInstance.getEphemeralOwner();
        List<CurrentState> currentStates = _accessor.getChildValues(_accessor.keyBuilder().currentStates(_participants[0].getInstanceName(), sessionId), true);
        for (CurrentState currentState : currentStates) {
            for (Map.Entry<String, String> partitionState : currentState.getPartitionStateMap().entrySet()) {
                if (partitionState.getKey().equals("TestDB0_2")) {
                    Assert.assertFalse(partitionState.getValue().equals("MASTER"));
                }
            }
        }
    }
    // clean up the cluster
    controller.syncStop();
    for (int i = 0; i < participantCount; i++) {
        _participants[i].syncStop();
    }
    deleteCluster(_clusterName);
}
Also used : ClusterControllerManager(org.apache.helix.integration.manager.ClusterControllerManager) LiveInstance(org.apache.helix.model.LiveInstance) CurrentState(org.apache.helix.model.CurrentState) Map(java.util.Map) PropertyKey(org.apache.helix.PropertyKey) Test(org.testng.annotations.Test)

Example 30 with PropertyKey

use of org.apache.helix.PropertyKey in project helix by apache.

the class TestSchemataSM method testSchemataSM.

@Test
public void testSchemataSM() throws Exception {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    int n = 5;
    MockParticipantManager[] participants = new MockParticipantManager[n];
    System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
    // participant start port
    TestHelper.setupCluster(// participant start port
    clusterName, // participant start port
    ZK_ADDR, // participant start port
    12918, // participant name prefix
    "localhost", // resource name prefix
    "TestSchemata", // resources
    1, // partitions per resource
    1, // number of nodes
    n, // replicas
    0, "STORAGE_DEFAULT_SM_SCHEMATA", // don't rebalance
    false);
    // rebalance ideal-state to use ANY_LIVEINSTANCE for preference list
    ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<>(_gZkClient));
    PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    PropertyKey key = keyBuilder.idealStates("TestSchemata0");
    IdealState idealState = accessor.getProperty(key);
    idealState.setReplicas(IdealState.IdealStateConstants.ANY_LIVEINSTANCE.toString());
    idealState.getRecord().setListField("TestSchemata0_0", Collections.singletonList(IdealState.IdealStateConstants.ANY_LIVEINSTANCE.toString()));
    accessor.setProperty(key, idealState);
    ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller");
    controller.syncStart();
    // start n-1 participants
    for (int i = 1; i < n; i++) {
        String instanceName = "localhost_" + (12918 + i);
        participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
        participants[i].syncStart();
    }
    boolean result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
    // start the remaining 1 participant
    participants[0] = new MockParticipantManager(ZK_ADDR, clusterName, "localhost_12918");
    participants[0].syncStart();
    // make sure we have all participants in MASTER state
    result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
    key = keyBuilder.externalView("TestSchemata0");
    ExternalView externalView = accessor.getProperty(key);
    Map<String, String> stateMap = externalView.getStateMap("TestSchemata0_0");
    Assert.assertNotNull(stateMap);
    Assert.assertEquals(stateMap.size(), n, "all " + n + " participants should be in Master state");
    for (int i = 0; i < n; i++) {
        String instanceName = "localhost_" + (12918 + i);
        Assert.assertNotNull(stateMap.get(instanceName));
        Assert.assertEquals(stateMap.get(instanceName), "MASTER");
    }
    // clean up
    controller.syncStop();
    for (int i = 0; i < n; i++) {
        participants[i].syncStop();
    }
    deleteCluster(clusterName);
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : ExternalView(org.apache.helix.model.ExternalView) MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) BestPossAndExtViewZkVerifier(org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier) Date(java.util.Date) IdealState(org.apache.helix.model.IdealState) ClusterControllerManager(org.apache.helix.integration.manager.ClusterControllerManager) PropertyKey(org.apache.helix.PropertyKey) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) Test(org.testng.annotations.Test)

Aggregations

PropertyKey (org.apache.helix.PropertyKey)127 HelixDataAccessor (org.apache.helix.HelixDataAccessor)57 Test (org.testng.annotations.Test)33 ArrayList (java.util.ArrayList)31 CurrentState (org.apache.helix.model.CurrentState)29 Message (org.apache.helix.model.Message)27 ZNRecord (org.apache.helix.zookeeper.datamodel.ZNRecord)27 HelixManager (org.apache.helix.HelixManager)24 Builder (org.apache.helix.PropertyKey.Builder)20 LiveInstance (org.apache.helix.model.LiveInstance)20 Map (java.util.Map)19 HelixException (org.apache.helix.HelixException)19 HashSet (java.util.HashSet)18 HashMap (java.util.HashMap)17 IdealState (org.apache.helix.model.IdealState)16 InstanceConfig (org.apache.helix.model.InstanceConfig)14 ExternalView (org.apache.helix.model.ExternalView)12 HelixProperty (org.apache.helix.HelixProperty)11 List (java.util.List)10 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)10