Search in sources :

Example 6 with InvalidRoutingDataException

use of org.apache.helix.msdcommon.exception.InvalidRoutingDataException in project helix by apache.

the class ZkMetadataStoreDirectory method refreshRoutingData.

/**
 * Callback for updating the cached routing data.
 * Note: this method should not synchronize on the class or the map. We do not want namespaces
 * blocking each other.
 * Threadsafe map is used for _realmToShardingKeysMap.
 * The global consistency of the in-memory routing data is not a requirement (eventual consistency
 * is enough).
 * @param namespace
 */
@Override
public void refreshRoutingData(String namespace) {
    // Check if namespace exists; otherwise, return as a NOP and log it
    if (!_routingZkAddressMap.containsKey(namespace)) {
        LOG.error("Failed to refresh internally-cached routing data! Namespace not found: " + namespace);
        return;
    }
    Map<String, List<String>> rawRoutingData;
    try {
        rawRoutingData = _routingDataReaderMap.get(namespace).getRoutingData();
    } catch (InvalidRoutingDataException e) {
        LOG.error("Failed to refresh cached routing data for namespace {}", namespace, e);
        _realmToShardingKeysMap.put(namespace, Collections.emptyMap());
        _routingDataMap.remove(namespace);
        return;
    }
    _realmToShardingKeysMap.put(namespace, rawRoutingData);
    TrieRoutingData trieRoutingData;
    try {
        trieRoutingData = new TrieRoutingData(rawRoutingData);
    } catch (InvalidRoutingDataException e) {
        LOG.warn("TrieRoutingData is not created for namespace {}", namespace, e);
        _routingDataMap.remove(namespace);
        return;
    }
    _routingDataMap.put(namespace, trieRoutingData);
}
Also used : TrieRoutingData(org.apache.helix.msdcommon.datamodel.TrieRoutingData) List(java.util.List) InvalidRoutingDataException(org.apache.helix.msdcommon.exception.InvalidRoutingDataException)

Example 7 with InvalidRoutingDataException

use of org.apache.helix.msdcommon.exception.InvalidRoutingDataException in project helix by apache.

the class ServerContext method createRealmAwareZkClient.

/**
 * Main creation logic for RealmAwareZkClient.
 * @param zkSerializer the type of ZkSerializer to use
 * @return
 */
private RealmAwareZkClient createRealmAwareZkClient(ZkSerializer zkSerializer) {
    // If the multi ZK config is enabled, use FederatedZkClient on multi-realm mode
    RealmAwareZkClient realmAwareZkClient;
    if (_isMultiZkEnabled || Boolean.parseBoolean(System.getProperty(SystemPropertyKeys.MULTI_ZK_ENABLED))) {
        try {
            initializeZkClientForRoutingData();
            RealmAwareZkClient.RealmAwareZkConnectionConfig.Builder connectionConfigBuilder = new RealmAwareZkClient.RealmAwareZkConnectionConfig.Builder();
            // If MSDS endpoint is set for this namespace, use that instead.
            if (_msdsEndpoint != null && !_msdsEndpoint.isEmpty()) {
                connectionConfigBuilder.setRoutingDataSourceEndpoint(_msdsEndpoint).setRoutingDataSourceType(RoutingDataReaderType.HTTP.name());
            }
            realmAwareZkClient = new FederatedZkClient(connectionConfigBuilder.build(), new RealmAwareZkClient.RealmAwareZkClientConfig().setZkSerializer(zkSerializer));
            LOG.info("ServerContext: FederatedZkClient created successfully!");
        } catch (InvalidRoutingDataException | IllegalStateException e) {
            throw new HelixException("Failed to create FederatedZkClient!", e);
        }
    } else {
        // If multi ZK config is not set, just connect to the ZK address given
        HelixZkClient.ZkClientConfig clientConfig = new HelixZkClient.ZkClientConfig();
        clientConfig.setZkSerializer(zkSerializer);
        realmAwareZkClient = SharedZkClientFactory.getInstance().buildZkClient(new HelixZkClient.ZkConnectionConfig(_zkAddr), clientConfig);
    }
    return realmAwareZkClient;
}
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 8 with InvalidRoutingDataException

use of org.apache.helix.msdcommon.exception.InvalidRoutingDataException in project helix by apache.

the class HelixPropertyFactory method getCloudConfig.

public static CloudConfig getCloudConfig(String zkAddress, String clusterName, RealmAwareZkClient.RealmAwareZkConnectionConfig realmAwareZkConnectionConfig) {
    CloudConfig cloudConfig;
    RealmAwareZkClient dedicatedZkClient = null;
    try {
        if (Boolean.getBoolean(SystemPropertyKeys.MULTI_ZK_ENABLED) || zkAddress == null) {
            // DedicatedZkClient
            try {
                if (realmAwareZkConnectionConfig == null) {
                    realmAwareZkConnectionConfig = new RealmAwareZkClient.RealmAwareZkConnectionConfig.Builder().setRealmMode(RealmAwareZkClient.RealmMode.SINGLE_REALM).setZkRealmShardingKey("/" + clusterName).build();
                }
                dedicatedZkClient = DedicatedZkClientFactory.getInstance().buildZkClient(realmAwareZkConnectionConfig);
            } catch (IOException | InvalidRoutingDataException e) {
                throw new HelixException("Not able to connect on multi-ZK mode!", e);
            }
        } else {
            // Use a dedicated ZK client single-ZK mode
            HelixZkClient.ZkConnectionConfig connectionConfig = new HelixZkClient.ZkConnectionConfig(zkAddress);
            dedicatedZkClient = DedicatedZkClientFactory.getInstance().buildZkClient(connectionConfig);
        }
        dedicatedZkClient.setZkSerializer(new ZNRecordSerializer());
        ConfigAccessor configAccessor = new ConfigAccessor(dedicatedZkClient);
        // up yet, constructing a new ZKHelixManager should not throw an exception
        try {
            cloudConfig = configAccessor.getCloudConfig(clusterName);
            if (cloudConfig == null) {
                cloudConfig = new CloudConfig();
            }
        } catch (HelixException e) {
            cloudConfig = new CloudConfig();
        }
    } finally {
        // Use a try-finally to make sure zkclient connection is closed properly
        if (dedicatedZkClient != null && !dedicatedZkClient.isClosed()) {
            dedicatedZkClient.close();
        }
    }
    return cloudConfig;
}
Also used : HelixZkClient(org.apache.helix.zookeeper.api.client.HelixZkClient) CloudConfig(org.apache.helix.model.CloudConfig) IOException(java.io.IOException) InvalidRoutingDataException(org.apache.helix.msdcommon.exception.InvalidRoutingDataException) RealmAwareZkClient(org.apache.helix.zookeeper.api.client.RealmAwareZkClient) ZNRecordSerializer(org.apache.helix.zookeeper.datamodel.serializer.ZNRecordSerializer)

Example 9 with InvalidRoutingDataException

use of org.apache.helix.msdcommon.exception.InvalidRoutingDataException in project helix by apache.

the class ZkMetadataStoreDirectory method init.

private void init(String namespace, String zkAddress) throws InvalidRoutingDataException {
    if (!_routingZkAddressMap.containsKey(namespace)) {
        synchronized (_routingZkAddressMap) {
            if (!_routingZkAddressMap.containsKey(namespace)) {
                HelixZkClient zkClient = null;
                try {
                    // Ensure that ROUTING_DATA_PATH exists in ZK.
                    zkClient = DedicatedZkClientFactory.getInstance().buildZkClient(new HelixZkClient.ZkConnectionConfig(zkAddress), new HelixZkClient.ZkClientConfig().setZkSerializer(new ZNRecordSerializer()));
                    createRoutingDataPath(zkClient, zkAddress);
                } finally {
                    if (zkClient != null && !zkClient.isClosed()) {
                        zkClient.close();
                    }
                }
                try {
                    _routingZkAddressMap.put(namespace, zkAddress);
                    _routingDataReaderMap.put(namespace, new ZkRoutingDataReader(namespace, zkAddress, this));
                    _routingDataWriterMap.put(namespace, new ZkRoutingDataWriter(namespace, zkAddress));
                } catch (IllegalArgumentException | IllegalStateException e) {
                    LOG.error("ZkMetadataStoreDirectory: initializing ZkRoutingDataReader/Writer failed!", e);
                }
                // Populate realmToShardingKeys with ZkRoutingDataReader
                Map<String, List<String>> rawRoutingData = _routingDataReaderMap.get(namespace).getRoutingData();
                _realmToShardingKeysMap.put(namespace, rawRoutingData);
                try {
                    _routingDataMap.put(namespace, new TrieRoutingData(rawRoutingData));
                } catch (InvalidRoutingDataException e) {
                    LOG.warn("ZkMetadataStoreDirectory: TrieRoutingData is not created for namespace {}", namespace, e);
                }
            }
        }
    }
}
Also used : HelixZkClient(org.apache.helix.zookeeper.api.client.HelixZkClient) ZkRoutingDataReader(org.apache.helix.rest.metadatastore.accessor.ZkRoutingDataReader) TrieRoutingData(org.apache.helix.msdcommon.datamodel.TrieRoutingData) ZkRoutingDataWriter(org.apache.helix.rest.metadatastore.accessor.ZkRoutingDataWriter) List(java.util.List) InvalidRoutingDataException(org.apache.helix.msdcommon.exception.InvalidRoutingDataException) ZNRecordSerializer(org.apache.helix.zookeeper.datamodel.serializer.ZNRecordSerializer)

Example 10 with InvalidRoutingDataException

use of org.apache.helix.msdcommon.exception.InvalidRoutingDataException in project helix by apache.

the class GenericBaseDataAccessorBuilder method createZkClient.

/**
 * This method contains construction logic for ZK-based BaseDataAccessor
 * implementations.
 * It uses an implementation of GenericBaseDataAccessorBuilder to construct the right
 * RealmAwareZkClient based on a host of configs provided in the Builder.
 * @return RealmAwareZkClient
 */
@Override
protected RealmAwareZkClient createZkClient(RealmAwareZkClient.RealmMode realmMode, RealmAwareZkClient.RealmAwareZkConnectionConfig connectionConfig, RealmAwareZkClient.RealmAwareZkClientConfig clientConfig, String zkAddress) {
    RealmAwareZkClient zkClient;
    switch(realmMode) {
        case MULTI_REALM:
            try {
                zkClient = new FederatedZkClient(connectionConfig, clientConfig);
            } catch (InvalidRoutingDataException e) {
                throw new HelixException("Not able to connect on multi-realm mode.", e);
            }
            break;
        case SINGLE_REALM:
            HelixZkClient.ZkConnectionConfig helixZkConnectionConfig = new HelixZkClient.ZkConnectionConfig(zkAddress).setSessionTimeout(connectionConfig.getSessionTimeout());
            if (_zkClientType == ZkClientType.DEDICATED) {
                // If DEDICATED, then we use a dedicated HelixZkClient because we must support ephemeral
                // operations
                zkClient = DedicatedZkClientFactory.getInstance().buildZkClient(helixZkConnectionConfig, clientConfig.createHelixZkClientConfig());
            } else {
                // if SHARED or null: Use a SharedZkClient because ZkBaseDataAccessor does not need to
                // do ephemeral operations.
                zkClient = SharedZkClientFactory.getInstance().buildZkClient(helixZkConnectionConfig, clientConfig.createHelixZkClientConfig());
            }
            zkClient.waitUntilConnected(HelixZkClient.DEFAULT_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
            break;
        default:
            throw new HelixException("Invalid RealmMode given: " + realmMode);
    }
    return zkClient;
}
Also used : FederatedZkClient(org.apache.helix.zookeeper.impl.client.FederatedZkClient) HelixException(org.apache.helix.HelixException) HelixZkClient(org.apache.helix.zookeeper.api.client.HelixZkClient) InvalidRoutingDataException(org.apache.helix.msdcommon.exception.InvalidRoutingDataException) RealmAwareZkClient(org.apache.helix.zookeeper.api.client.RealmAwareZkClient)

Aggregations

InvalidRoutingDataException (org.apache.helix.msdcommon.exception.InvalidRoutingDataException)15 HelixException (org.apache.helix.HelixException)8 HelixZkClient (org.apache.helix.zookeeper.api.client.HelixZkClient)8 RealmAwareZkClient (org.apache.helix.zookeeper.api.client.RealmAwareZkClient)8 FederatedZkClient (org.apache.helix.zookeeper.impl.client.FederatedZkClient)6 List (java.util.List)5 ZNRecordSerializer (org.apache.helix.zookeeper.datamodel.serializer.ZNRecordSerializer)4 IOException (java.io.IOException)3 ZNRecord (org.apache.helix.zookeeper.datamodel.ZNRecord)3 CloudConfig (org.apache.helix.model.CloudConfig)2 TrieRoutingData (org.apache.helix.msdcommon.datamodel.TrieRoutingData)2 Test (org.testng.annotations.Test)2 HashMap (java.util.HashMap)1 ZKHelixManager (org.apache.helix.manager.zk.ZKHelixManager)1 ZkRoutingDataReader (org.apache.helix.rest.metadatastore.accessor.ZkRoutingDataReader)1 ZkRoutingDataWriter (org.apache.helix.rest.metadatastore.accessor.ZkRoutingDataWriter)1 ZkNoNodeException (org.apache.helix.zookeeper.zkclient.exception.ZkNoNodeException)1