Search in sources :

Example 36 with ZkClient

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

the class AbstractTestClass method beforeSuite.

@BeforeSuite
public void beforeSuite() throws Exception {
    if (!_init) {
        // TODO: use logging.properties file to config java.util.logging.Logger levels
        java.util.logging.Logger topJavaLogger = java.util.logging.Logger.getLogger("");
        topJavaLogger.setLevel(Level.WARNING);
        _gZkClient = new ZkClient(ZK_ADDR, ZkClient.DEFAULT_CONNECTION_TIMEOUT, ZkClient.DEFAULT_SESSION_TIMEOUT, new ZNRecordSerializer());
        _gZkClientTestNS = new ZkClient(_zkAddrTestNS, ZkClient.DEFAULT_CONNECTION_TIMEOUT, ZkClient.DEFAULT_SESSION_TIMEOUT, new ZNRecordSerializer());
        _gSetupTool = new ClusterSetup(_gZkClient);
        _configAccessor = new ConfigAccessor(_gZkClient);
        _baseAccessor = new ZkBaseDataAccessor<>(_gZkClient);
        _baseAccessorTestNS = new ZkBaseDataAccessor<>(_gZkClientTestNS);
        // wait for the web service to start
        Thread.sleep(100);
        setup();
        _init = true;
    }
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) ConfigAccessor(org.apache.helix.ConfigAccessor) ClusterSetup(org.apache.helix.tools.ClusterSetup) ZNRecordSerializer(org.apache.helix.manager.zk.ZNRecordSerializer) BeforeSuite(org.testng.annotations.BeforeSuite)

Example 37 with ZkClient

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

the class TestClusterSetup method beforeClass.

@BeforeClass()
public void beforeClass() throws IOException, Exception {
    System.out.println("START TestClusterSetup.beforeClass() " + new Date(System.currentTimeMillis()));
    _zkClient = new ZkClient(ZK_ADDR);
    _zkClient.setZkSerializer(new ZNRecordSerializer());
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) Date(java.util.Date) ZNRecordSerializer(org.apache.helix.manager.zk.ZNRecordSerializer) BeforeClass(org.testng.annotations.BeforeClass)

Example 38 with ZkClient

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

the class TestZKClientPool method test.

@Test
public void test() throws Exception {
    String testName = "TestZKClientPool";
    System.out.println("START " + testName + " at " + new Date(System.currentTimeMillis()));
    String zkAddr = "localhost:2189";
    ZkServer zkServer = TestHelper.startZkServer(zkAddr);
    ZkClient zkClient = ZKClientPool.getZkClient(zkAddr);
    zkClient.createPersistent("/" + testName, new ZNRecord(testName));
    ZNRecord record = zkClient.readData("/" + testName);
    Assert.assertEquals(record.getId(), testName);
    TestHelper.stopZkServer(zkServer);
    // restart zk
    zkServer = TestHelper.startZkServer(zkAddr);
    try {
        zkClient = ZKClientPool.getZkClient(zkAddr);
        record = zkClient.readData("/" + testName);
        Assert.fail("should fail on zk no node exception");
    } catch (ZkNoNodeException e) {
    // OK
    } catch (Exception e) {
        Assert.fail("should not fail on exception other than ZkNoNodeException");
    }
    zkClient.createPersistent("/" + testName, new ZNRecord(testName));
    record = zkClient.readData("/" + testName);
    Assert.assertEquals(record.getId(), testName);
    zkClient.close();
    TestHelper.stopZkServer(zkServer);
    System.out.println("END " + testName + " at " + new Date(System.currentTimeMillis()));
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) Date(java.util.Date) ZNRecord(org.apache.helix.ZNRecord) ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) ZkServer(org.I0Itec.zkclient.ZkServer) Test(org.testng.annotations.Test)

Example 39 with ZkClient

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

the class ResourceAccessor method getResources.

@GET
public Response getResources(@PathParam("clusterId") String clusterId) {
    ObjectNode root = JsonNodeFactory.instance.objectNode();
    root.put(Properties.id.name(), JsonNodeFactory.instance.textNode(clusterId));
    ZkClient zkClient = getZkClient();
    ArrayNode idealStatesNode = root.putArray(ResourceProperties.idealStates.name());
    ArrayNode externalViewsNode = root.putArray(ResourceProperties.externalViews.name());
    List<String> idealStates = zkClient.getChildren(PropertyPathBuilder.idealState(clusterId));
    List<String> externalViews = zkClient.getChildren(PropertyPathBuilder.externalView(clusterId));
    if (idealStates != null) {
        idealStatesNode.addAll((ArrayNode) OBJECT_MAPPER.valueToTree(idealStates));
    } else {
        return notFound();
    }
    if (externalViews != null) {
        externalViewsNode.addAll((ArrayNode) OBJECT_MAPPER.valueToTree(externalViews));
    }
    return JSONRepresentation(root);
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) ObjectNode(org.codehaus.jackson.node.ObjectNode) ArrayNode(org.codehaus.jackson.node.ArrayNode) GET(javax.ws.rs.GET)

Example 40 with ZkClient

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

the class HelixCustomCodeRunner method start.

/**
 * This method will be invoked when there is a change in any subscribed
 * notificationTypes
 * @throws Exception
 */
public void start() throws Exception {
    if (_callback == null || _notificationTypes == null || _notificationTypes.size() == 0 || _resourceName == null) {
        throw new IllegalArgumentException("Require callback | notificationTypes | resourceName");
    }
    LOG.info("Register participantLeader on " + _notificationTypes + " using " + _resourceName);
    _stateModelFty = new GenericLeaderStandbyStateModelFactory(_callback, _notificationTypes);
    StateMachineEngine stateMach = _manager.getStateMachineEngine();
    stateMach.registerStateModelFactory(LEADER_STANDBY, _stateModelFty, _resourceName);
    ZkClient zkClient = null;
    try {
        // manually add ideal state for participant leader using LeaderStandby
        // model
        zkClient = new ZkClient(_zkAddr, ZkClient.DEFAULT_SESSION_TIMEOUT, ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ZNRecordSerializer());
        HelixDataAccessor accessor = new ZKHelixDataAccessor(_manager.getClusterName(), new ZkBaseDataAccessor<ZNRecord>(zkClient));
        Builder keyBuilder = accessor.keyBuilder();
        IdealState idealState = new IdealState(_resourceName);
        idealState.setRebalanceMode(RebalanceMode.SEMI_AUTO);
        idealState.setReplicas(IdealState.IdealStateConstants.ANY_LIVEINSTANCE.toString());
        idealState.setNumPartitions(1);
        idealState.setStateModelDefRef(LEADER_STANDBY);
        idealState.setStateModelFactoryName(_resourceName);
        List<String> prefList = new ArrayList<String>(Arrays.asList(IdealState.IdealStateConstants.ANY_LIVEINSTANCE.toString()));
        idealState.getRecord().setListField(_resourceName + "_0", prefList);
        List<String> idealStates = accessor.getChildNames(keyBuilder.idealStates());
        while (idealStates == null || !idealStates.contains(_resourceName)) {
            accessor.setProperty(keyBuilder.idealStates(_resourceName), idealState);
            idealStates = accessor.getChildNames(keyBuilder.idealStates());
        }
        LOG.info("Set idealState for participantLeader:" + _resourceName + ", idealState:" + idealState);
    } finally {
        if (zkClient != null && zkClient.getConnection() != null) {
            zkClient.close();
        }
    }
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) Builder(org.apache.helix.PropertyKey.Builder) ArrayList(java.util.ArrayList) IdealState(org.apache.helix.model.IdealState) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) HelixDataAccessor(org.apache.helix.HelixDataAccessor) ZNRecord(org.apache.helix.ZNRecord) ZNRecordSerializer(org.apache.helix.manager.zk.ZNRecordSerializer) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor)

Aggregations

ZkClient (org.apache.helix.manager.zk.ZkClient)109 ZNRecordSerializer (org.apache.helix.manager.zk.ZNRecordSerializer)39 ZNRecord (org.apache.helix.ZNRecord)29 StringRepresentation (org.restlet.representation.StringRepresentation)29 ClusterSetup (org.apache.helix.tools.ClusterSetup)26 HelixException (org.apache.helix.HelixException)23 Builder (org.apache.helix.PropertyKey.Builder)18 IOException (java.io.IOException)17 HelixDataAccessor (org.apache.helix.HelixDataAccessor)15 JsonGenerationException (org.codehaus.jackson.JsonGenerationException)15 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)15 ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)12 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)10 Test (org.testng.annotations.Test)10 Date (java.util.Date)9 BeforeClass (org.testng.annotations.BeforeClass)9 PropertyKey (org.apache.helix.PropertyKey)8 IdealState (org.apache.helix.model.IdealState)8 InstanceConfig (org.apache.helix.model.InstanceConfig)8 StateModelDefinition (org.apache.helix.model.StateModelDefinition)7