Search in sources :

Example 1 with ZKHelixManager

use of org.apache.helix.manager.zk.ZKHelixManager in project helix by apache.

the class HelixAgentMain method main.

public static void main(String[] args) throws Exception {
    CommandLine cmd = processCommandLineArgs(args);
    String zkAddress = cmd.getOptionValue(zkAddr);
    String clusterName = cmd.getOptionValue(cluster);
    String instance = cmd.getOptionValue(instanceName);
    String stateModelName = cmd.getOptionValue(stateModel);
    HelixManager manager = new ZKHelixManager(clusterName, instance, InstanceType.PARTICIPANT, zkAddress);
    StateMachineEngine stateMach = manager.getStateMachineEngine();
    stateMach.registerStateModelFactory(stateModelName, new AgentStateModelFactory());
    Runtime.getRuntime().addShutdownHook(new HelixAgentShutdownHook(manager));
    try {
        manager.connect();
        Thread.currentThread().join();
    } catch (Exception e) {
        LOG.error(e.toString());
    } finally {
        if (manager != null && manager.isConnected()) {
            manager.disconnect();
        }
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) HelixManager(org.apache.helix.HelixManager) ZKHelixManager(org.apache.helix.manager.zk.ZKHelixManager) StateMachineEngine(org.apache.helix.participant.StateMachineEngine) ZKHelixManager(org.apache.helix.manager.zk.ZKHelixManager) ParseException(org.apache.commons.cli.ParseException)

Example 2 with ZKHelixManager

use of org.apache.helix.manager.zk.ZKHelixManager 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<ZNRecord>(_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);
    Assert.assertTrue(mockMessageListener.isSessionSyncMessageSent());
}
Also used : ZkBaseDataAccessor(org.apache.helix.manager.zk.ZkBaseDataAccessor) MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) ZKHelixManager(org.apache.helix.manager.zk.ZKHelixManager) Date(java.util.Date) ClusterControllerManager(org.apache.helix.integration.manager.ClusterControllerManager) Stat(org.apache.zookeeper.data.Stat) PropertyKey(org.apache.helix.PropertyKey) ZNRecord(org.apache.helix.ZNRecord) Test(org.testng.annotations.Test)

Example 3 with ZKHelixManager

use of org.apache.helix.manager.zk.ZKHelixManager in project helix by apache.

the class TestEntropyFreeNodeBounce method createParticipant.

private HelixManager createParticipant(String clusterName, String instanceName) {
    HelixManager participant = new ZKHelixManager(clusterName, instanceName, InstanceType.PARTICIPANT, ZK_ADDR);
    participant.getStateMachineEngine().registerStateModelFactory("OnlineOffline", new MockStateModelFactory());
    return participant;
}
Also used : ZKHelixManager(org.apache.helix.manager.zk.ZKHelixManager) HelixManager(org.apache.helix.HelixManager) ZKHelixManager(org.apache.helix.manager.zk.ZKHelixManager)

Example 4 with ZKHelixManager

use of org.apache.helix.manager.zk.ZKHelixManager in project helix by apache.

the class TestZkReconnect method testZKDisconnectCallback.

@Test
public void testZKDisconnectCallback() throws Exception {
    final int zkPort = TestHelper.getRandomPort();
    final String zkAddr = String.format("localhost:%d", zkPort);
    final ZkServer zkServer = TestHelper.startZkServer(zkAddr);
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    final String clusterName = className + "_" + methodName;
    // Init flag to check if callback is triggered
    final AtomicReference<Boolean> flag = new AtomicReference<Boolean>(false);
    // Setup cluster
    LOG.info("Setup clusters");
    ClusterSetup clusterSetup = new ClusterSetup(zkAddr);
    clusterSetup.addCluster(clusterName, true);
    // For fast test, set short timeout
    System.setProperty("zk.connection.timeout", "2000");
    System.setProperty("zk.connectionReEstablishment.timeout", "1000");
    // Registers and starts controller, register listener for disconnect handling
    LOG.info("Starts controller");
    final ZKHelixManager controller = (ZKHelixManager) HelixManagerFactory.getZKHelixManager(clusterName, null, InstanceType.CONTROLLER, zkAddr, new HelixManagerStateListener() {

        @Override
        public void onConnected(HelixManager helixManager) throws Exception {
            return;
        }

        @Override
        public void onDisconnected(HelixManager helixManager, Throwable error) throws Exception {
            Assert.assertEquals(helixManager.getClusterName(), clusterName);
            flag.getAndSet(true);
        }
    });
    try {
        controller.connect();
        ZkHelixPropertyStore propertyStore = controller.getHelixPropertyStore();
        // 1. shutdown zkServer and check if handler trigger callback
        zkServer.shutdown();
        // Retry will fail, and flag should be set within onDisconnected handler
        controller.handleSessionEstablishmentError(new Exception("For testing"));
        Assert.assertTrue(flag.get());
        try {
            propertyStore.get("/", null, 0);
            Assert.fail("propertyStore should be disconnected.");
        } catch (IllegalStateException e) {
            // Expected exception
            System.out.println(e.getMessage());
        }
        // 2. restart zkServer and check if handler will recover connection
        flag.getAndSet(false);
        zkServer.start();
        // Retry will succeed, and flag should not be set
        controller.handleSessionEstablishmentError(new Exception("For testing"));
        Assert.assertFalse(flag.get());
        // New propertyStore should be in good state
        propertyStore = controller.getHelixPropertyStore();
        propertyStore.get("/", null, 0);
    } finally {
        controller.disconnect();
        zkServer.shutdown();
        System.clearProperty("zk.connection.timeout");
        System.clearProperty("zk.connectionReEstablishment.timeout");
    }
}
Also used : ZKHelixManager(org.apache.helix.manager.zk.ZKHelixManager) HelixManager(org.apache.helix.HelixManager) ZkHelixPropertyStore(org.apache.helix.store.zk.ZkHelixPropertyStore) ZKHelixManager(org.apache.helix.manager.zk.ZKHelixManager) AtomicReference(java.util.concurrent.atomic.AtomicReference) ClusterSetup(org.apache.helix.tools.ClusterSetup) HelixManagerStateListener(org.apache.helix.manager.zk.HelixManagerStateListener) ZkServer(org.I0Itec.zkclient.ZkServer) Test(org.testng.annotations.Test)

Example 5 with ZKHelixManager

use of org.apache.helix.manager.zk.ZKHelixManager in project helix by apache.

the class TestParticipantManager method simpleIntegrationTest.

@Test
public void simpleIntegrationTest() throws Exception {
    // Logger.getRootLogger().setLevel(Level.INFO);
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    int n = 1;
    System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
    // participant port
    TestHelper.setupCluster(// participant port
    clusterName, // participant port
    ZK_ADDR, // participant port
    12918, // participant name prefix
    "localhost", // resource name prefix
    "TestDB", // resources
    1, // partitions per resource
    4, // number of nodes
    n, // replicas
    1, "MasterSlave", // do rebalance
    true);
    HelixManager participant = new ZKHelixManager(clusterName, "localhost_12918", InstanceType.PARTICIPANT, ZK_ADDR);
    participant.getStateMachineEngine().registerStateModelFactory("MasterSlave", new MockMSModelFactory());
    participant.connect();
    HelixManager controller = new ZKHelixManager(clusterName, "controller_0", InstanceType.CONTROLLER, ZK_ADDR);
    controller.connect();
    boolean result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
    // cleanup
    controller.disconnect();
    participant.disconnect();
    // verify all live-instances and leader nodes are gone
    ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
    PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    Assert.assertNull(accessor.getProperty(keyBuilder.liveInstance("localhost_12918")));
    Assert.assertNull(accessor.getProperty(keyBuilder.controllerLeader()));
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : ZKHelixManager(org.apache.helix.manager.zk.ZKHelixManager) HelixManager(org.apache.helix.HelixManager) ZKHelixManager(org.apache.helix.manager.zk.ZKHelixManager) BestPossAndExtViewZkVerifier(org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier) Date(java.util.Date) MockMSModelFactory(org.apache.helix.mock.participant.MockMSModelFactory) ZNRecord(org.apache.helix.ZNRecord) PropertyKey(org.apache.helix.PropertyKey) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) Test(org.testng.annotations.Test)

Aggregations

ZKHelixManager (org.apache.helix.manager.zk.ZKHelixManager)6 HelixManager (org.apache.helix.HelixManager)5 Test (org.testng.annotations.Test)4 Date (java.util.Date)3 PropertyKey (org.apache.helix.PropertyKey)3 ZNRecord (org.apache.helix.ZNRecord)3 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)2 MockMSModelFactory (org.apache.helix.mock.participant.MockMSModelFactory)2 BestPossAndExtViewZkVerifier (org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ZkServer (org.I0Itec.zkclient.ZkServer)1 CommandLine (org.apache.commons.cli.CommandLine)1 ParseException (org.apache.commons.cli.ParseException)1 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)1 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)1 HelixManagerStateListener (org.apache.helix.manager.zk.HelixManagerStateListener)1 ZkBaseDataAccessor (org.apache.helix.manager.zk.ZkBaseDataAccessor)1 LiveInstance (org.apache.helix.model.LiveInstance)1 StateMachineEngine (org.apache.helix.participant.StateMachineEngine)1 ZkHelixPropertyStore (org.apache.helix.store.zk.ZkHelixPropertyStore)1