Search in sources :

Example 1 with FederatedZkClient

use of org.apache.helix.zookeeper.impl.client.FederatedZkClient in project helix by apache.

the class TestMultiZkConectionConfig method beforeClass.

@BeforeClass
public void beforeClass() throws Exception {
    // Create 3 in-memory zookeepers and routing mapping
    for (int i = 0; i < NUM_ZK; i++) {
        String zkAddress = ZK_PREFIX + (ZK_START_PORT + i);
        ZK_SERVER_MAP.put(zkAddress, TestHelper.startZkServer(zkAddress));
        ZK_CLIENT_MAP.put(zkAddress, DedicatedZkClientFactory.getInstance().buildZkClient(new HelixZkClient.ZkConnectionConfig(zkAddress), new HelixZkClient.ZkClientConfig().setZkSerializer(new ZNRecordSerializer())));
        // One cluster per ZkServer created
        _rawRoutingData.put(zkAddress, Collections.singletonList("/" + CLUSTER_LIST.get(i)));
    }
    // Create a Mock MSDS
    final String msdsHostName = "localhost";
    final int msdsPort = 11117;
    final String msdsNamespace = "multiZkTest";
    _msdsEndpoint = "http://" + msdsHostName + ":" + msdsPort + "/admin/v2/namespaces/" + msdsNamespace;
    _msds = new MockMetadataStoreDirectoryServer(msdsHostName, msdsPort, msdsNamespace, _rawRoutingData);
    _msds.startServer();
    // Save previously-set system configs
    String prevMultiZkEnabled = System.getProperty(SystemPropertyKeys.MULTI_ZK_ENABLED);
    String prevMsdsServerEndpoint = System.getProperty(MetadataStoreRoutingConstants.MSDS_SERVER_ENDPOINT_KEY);
    if (prevMultiZkEnabled != null) {
        _configStore.put(SystemPropertyKeys.MULTI_ZK_ENABLED, prevMultiZkEnabled);
    }
    if (prevMsdsServerEndpoint != null) {
        _configStore.put(MetadataStoreRoutingConstants.MSDS_SERVER_ENDPOINT_KEY, prevMsdsServerEndpoint);
    }
    // Turn on multiZk mode in System config
    System.setProperty(SystemPropertyKeys.MULTI_ZK_ENABLED, "true");
    // MSDS endpoint: http://localhost:11117/admin/v2/namespaces/multiZkTest
    // We are not setting routing ZK in system property.
    // System.setProperty(MetadataStoreRoutingConstants.MSDS_SERVER_ENDPOINT_KEY, _msdsEndpoint);
    // Routing data may be set by other tests using the same endpoint; reset() for good measure
    RoutingDataManager.getInstance().reset();
    try {
        _zkClient = new FederatedZkClient(new RealmAwareZkClient.RealmAwareZkConnectionConfig.Builder().setRoutingDataSourceEndpoint(_msdsEndpoint + "," + ZK_PREFIX + ZK_START_PORT).setRoutingDataSourceType(RoutingDataReaderType.HTTP_ZK_FALLBACK.name()).build(), new RealmAwareZkClient.RealmAwareZkClientConfig());
        _zkClient.setZkSerializer(new ZNRecordSerializer());
    } catch (Exception ex) {
        for (StackTraceElement elm : ex.getStackTrace()) {
            System.out.println(elm);
        }
    }
    System.out.println("end start");
}
Also used : HelixZkClient(org.apache.helix.zookeeper.api.client.HelixZkClient) MockMetadataStoreDirectoryServer(org.apache.helix.msdcommon.mock.MockMetadataStoreDirectoryServer) HelixException(org.apache.helix.HelixException) FederatedZkClient(org.apache.helix.zookeeper.impl.client.FederatedZkClient) ZNRecordSerializer(org.apache.helix.zookeeper.datamodel.serializer.ZNRecordSerializer) BeforeClass(org.testng.annotations.BeforeClass)

Example 2 with FederatedZkClient

use of org.apache.helix.zookeeper.impl.client.FederatedZkClient in project helix by apache.

the class TestMultiZkHelixJavaApis method testDifferentMsdsEndpointConfigs.

/**
 * Tests Helix Java APIs which use different MSDS endpoint configs. Java API should
 * only connect to the configured MSDS but not the others. The APIs are explicitly tested are:
 * - ClusterSetup
 * - HelixAdmin
 * - ZkUtil
 * - HelixManager
 * - BaseDataAccessor
 * - ConfigAccessor
 */
@Test(dependsOnMethods = "testGenericBaseDataAccessorBuilder")
public void testDifferentMsdsEndpointConfigs() throws IOException, InvalidRoutingDataException {
    String methodName = TestHelper.getTestMethodName();
    System.out.println("Start " + methodName);
    final String zkAddress = ZK_SERVER_MAP.keySet().iterator().next();
    final Map<String, Collection<String>> secondRoutingData = ImmutableMap.of(zkAddress, Collections.singletonList(formPath(CLUSTER_FOUR)));
    MockMetadataStoreDirectoryServer secondMsds = new MockMetadataStoreDirectoryServer("localhost", 11118, "multiZkTest", secondRoutingData);
    final RealmAwareZkClient.RealmAwareZkConnectionConfig connectionConfig = new RealmAwareZkClient.RealmAwareZkConnectionConfig.Builder().setRoutingDataSourceType(RoutingDataReaderType.HTTP.name()).setRoutingDataSourceEndpoint(secondMsds.getEndpoint()).build();
    secondMsds.startServer();
    try {
        // Verify ClusterSetup
        verifyClusterSetupMsdsEndpoint(connectionConfig);
        // Verify HelixAdmin
        verifyHelixAdminMsdsEndpoint(connectionConfig);
        // Verify ZKUtil
        verifyZkUtilMsdsEndpoint();
        // Verify HelixManager
        verifyHelixManagerMsdsEndpoint();
        // Verify BaseDataAccessor
        verifyBaseDataAccessorMsdsEndpoint(connectionConfig);
        // Verify ConfigAccessor
        verifyConfigAccessorMsdsEndpoint(connectionConfig);
    } finally {
        RealmAwareZkClient zkClient = new FederatedZkClient(connectionConfig, new RealmAwareZkClient.RealmAwareZkClientConfig());
        TestHelper.dropCluster(CLUSTER_FOUR, zkClient);
        zkClient.close();
        secondMsds.stopServer();
    }
    System.out.println("End " + methodName);
}
Also used : FederatedZkClient(org.apache.helix.zookeeper.impl.client.FederatedZkClient) Collection(java.util.Collection) MockMetadataStoreDirectoryServer(org.apache.helix.msdcommon.mock.MockMetadataStoreDirectoryServer) RealmAwareZkClient(org.apache.helix.zookeeper.api.client.RealmAwareZkClient) Test(org.testng.annotations.Test)

Example 3 with FederatedZkClient

use of org.apache.helix.zookeeper.impl.client.FederatedZkClient in project helix by apache.

the class ZKUtil method getHelixZkClient.

/**
 * Returns a dedicated ZkClient. A federatedZkClient will be used on multi-zk mode.
 * WARNING: ephemeral operations will not be supported on multi-zk mode!
 * @return
 */
private static RealmAwareZkClient getHelixZkClient(String zkAddr) {
    if (Boolean.getBoolean(SystemPropertyKeys.MULTI_ZK_ENABLED) || zkAddr == null) {
        try {
            // Create realm-aware ZkClient.
            RealmAwareZkClient.RealmAwareZkConnectionConfig connectionConfig = new RealmAwareZkClient.RealmAwareZkConnectionConfig.Builder().build();
            RealmAwareZkClient.RealmAwareZkClientConfig clientConfig = new RealmAwareZkClient.RealmAwareZkClientConfig();
            return new FederatedZkClient(connectionConfig, clientConfig);
        } catch (IllegalArgumentException | InvalidRoutingDataException e) {
            throw new HelixException("Not able to connect on realm-aware mode", e);
        }
    }
    if (zkAddr.isEmpty()) {
        throw new HelixException("ZK Address given is empty!");
    }
    HelixZkClient.ZkClientConfig clientConfig = new HelixZkClient.ZkClientConfig();
    clientConfig.setZkSerializer(new ZNRecordSerializer());
    return DedicatedZkClientFactory.getInstance().buildZkClient(new HelixZkClient.ZkConnectionConfig(zkAddr), clientConfig);
}
Also used : HelixZkClient(org.apache.helix.zookeeper.api.client.HelixZkClient) FederatedZkClient(org.apache.helix.zookeeper.impl.client.FederatedZkClient) HelixException(org.apache.helix.HelixException) InvalidRoutingDataException(org.apache.helix.msdcommon.exception.InvalidRoutingDataException) RealmAwareZkClient(org.apache.helix.zookeeper.api.client.RealmAwareZkClient)

Example 4 with FederatedZkClient

use of org.apache.helix.zookeeper.impl.client.FederatedZkClient in project helix by apache.

the class ZkBucketDataAccessor method createRealmAwareZkClient.

private static RealmAwareZkClient createRealmAwareZkClient(String zkAddr) {
    RealmAwareZkClient zkClient;
    if (Boolean.getBoolean(SystemPropertyKeys.MULTI_ZK_ENABLED) || zkAddr == null) {
        LOG.warn("ZkBucketDataAccessor: either multi-zk enabled or zkAddr is null - " + "starting ZkBucketDataAccessor in multi-zk mode!");
        try {
            // Create realm-aware ZkClient.
            RealmAwareZkClient.RealmAwareZkConnectionConfig connectionConfig = new RealmAwareZkClient.RealmAwareZkConnectionConfig.Builder().build();
            RealmAwareZkClient.RealmAwareZkClientConfig clientConfig = new RealmAwareZkClient.RealmAwareZkClientConfig();
            zkClient = new FederatedZkClient(connectionConfig, clientConfig);
        } catch (IllegalArgumentException | InvalidRoutingDataException e) {
            throw new HelixException("Not able to connect on realm-aware mode", e);
        }
    } else {
        zkClient = DedicatedZkClientFactory.getInstance().buildZkClient(new HelixZkClient.ZkConnectionConfig(zkAddr));
    }
    return zkClient;
}
Also used : FederatedZkClient(org.apache.helix.zookeeper.impl.client.FederatedZkClient) HelixException(org.apache.helix.HelixException) InvalidRoutingDataException(org.apache.helix.msdcommon.exception.InvalidRoutingDataException) RealmAwareZkClient(org.apache.helix.zookeeper.api.client.RealmAwareZkClient)

Example 5 with FederatedZkClient

use of org.apache.helix.zookeeper.impl.client.FederatedZkClient 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);
    RealmAwareZkClient zkClient = null;
    try {
        // model
        if (Boolean.getBoolean(SystemPropertyKeys.MULTI_ZK_ENABLED) || _zkAddr == null) {
            // Use multi-zk mode (FederatedZkClient)
            RealmAwareZkClient.RealmAwareZkClientConfig clientConfig = new RealmAwareZkClient.RealmAwareZkClientConfig();
            clientConfig.setZkSerializer(new ZNRecordSerializer());
            zkClient = new FederatedZkClient(_connectionConfig, clientConfig);
        } else {
            // Use single-zk mode using the ZkAddr given
            HelixZkClient.ZkClientConfig clientConfig = new HelixZkClient.ZkClientConfig();
            clientConfig.setZkSerializer(new ZNRecordSerializer());
            zkClient = SharedZkClientFactory.getInstance().buildZkClient(new HelixZkClient.ZkConnectionConfig(_zkAddr), clientConfig);
        }
        HelixDataAccessor accessor = new ZKHelixDataAccessor(_manager.getClusterName(), new ZkBaseDataAccessor<>(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.isClosed()) {
            zkClient.close();
        }
    }
}
Also used : HelixZkClient(org.apache.helix.zookeeper.api.client.HelixZkClient) Builder(org.apache.helix.PropertyKey.Builder) ArrayList(java.util.ArrayList) IdealState(org.apache.helix.model.IdealState) FederatedZkClient(org.apache.helix.zookeeper.impl.client.FederatedZkClient) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) HelixDataAccessor(org.apache.helix.HelixDataAccessor) RealmAwareZkClient(org.apache.helix.zookeeper.api.client.RealmAwareZkClient) ZNRecordSerializer(org.apache.helix.zookeeper.datamodel.serializer.ZNRecordSerializer) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor)

Aggregations

FederatedZkClient (org.apache.helix.zookeeper.impl.client.FederatedZkClient)10 RealmAwareZkClient (org.apache.helix.zookeeper.api.client.RealmAwareZkClient)8 HelixException (org.apache.helix.HelixException)7 HelixZkClient (org.apache.helix.zookeeper.api.client.HelixZkClient)7 InvalidRoutingDataException (org.apache.helix.msdcommon.exception.InvalidRoutingDataException)6 ZNRecordSerializer (org.apache.helix.zookeeper.datamodel.serializer.ZNRecordSerializer)4 MockMetadataStoreDirectoryServer (org.apache.helix.msdcommon.mock.MockMetadataStoreDirectoryServer)3 BeforeClass (org.testng.annotations.BeforeClass)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HelixDataAccessor (org.apache.helix.HelixDataAccessor)1 Builder (org.apache.helix.PropertyKey.Builder)1 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)1 ZKHelixManager (org.apache.helix.manager.zk.ZKHelixManager)1 IdealState (org.apache.helix.model.IdealState)1 Test (org.testng.annotations.Test)1