Search in sources :

Example 46 with InstanceConfig

use of org.apache.helix.model.InstanceConfig in project helix by apache.

the class ZKHelixAdmin method setInstanceConfig.

@Override
public boolean setInstanceConfig(String clusterName, String instanceName, InstanceConfig newInstanceConfig) {
    String instanceConfigPath = PropertyPathBuilder.getPath(PropertyType.CONFIGS, clusterName, HelixConfigScope.ConfigScopeProperty.PARTICIPANT.toString(), instanceName);
    if (!_zkClient.exists(instanceConfigPath)) {
        throw new HelixException("instance" + instanceName + " does not exist in cluster " + clusterName);
    }
    HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_zkClient));
    PropertyKey instanceConfigPropertyKey = accessor.keyBuilder().instanceConfig(instanceName);
    InstanceConfig currentInstanceConfig = accessor.getProperty(instanceConfigPropertyKey);
    if (!newInstanceConfig.getHostName().equals(currentInstanceConfig.getHostName()) || !newInstanceConfig.getPort().equals(currentInstanceConfig.getPort())) {
        throw new HelixException("Hostname and port cannot be changed, current hostname: " + currentInstanceConfig.getHostName() + " and port: " + currentInstanceConfig.getPort() + " is different from new hostname: " + newInstanceConfig.getHostName() + "and new port: " + newInstanceConfig.getPort());
    }
    return accessor.setProperty(instanceConfigPropertyKey, newInstanceConfig);
}
Also used : HelixException(org.apache.helix.HelixException) HelixDataAccessor(org.apache.helix.HelixDataAccessor) InstanceConfig(org.apache.helix.model.InstanceConfig) ZNRecord(org.apache.helix.ZNRecord) PropertyKey(org.apache.helix.PropertyKey)

Example 47 with InstanceConfig

use of org.apache.helix.model.InstanceConfig in project helix by apache.

the class ZKHelixAdmin method addInstanceTag.

@Override
public void addInstanceTag(String clusterName, String instanceName, String tag) {
    if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) {
        throw new HelixException("cluster " + clusterName + " is not setup yet");
    }
    if (!ZKUtil.isInstanceSetup(_zkClient, clusterName, instanceName, InstanceType.PARTICIPANT)) {
        throw new HelixException("cluster " + clusterName + " instance " + instanceName + " is not setup yet");
    }
    HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_zkClient));
    Builder keyBuilder = accessor.keyBuilder();
    InstanceConfig config = accessor.getProperty(keyBuilder.instanceConfig(instanceName));
    config.addTag(tag);
    accessor.setProperty(keyBuilder.instanceConfig(instanceName), config);
}
Also used : HelixException(org.apache.helix.HelixException) HelixDataAccessor(org.apache.helix.HelixDataAccessor) InstanceConfig(org.apache.helix.model.InstanceConfig) Builder(org.apache.helix.PropertyKey.Builder) PropertyPathBuilder(org.apache.helix.PropertyPathBuilder) ZNRecord(org.apache.helix.ZNRecord)

Example 48 with InstanceConfig

use of org.apache.helix.model.InstanceConfig in project helix by apache.

the class CallbackHandler method invoke.

public void invoke(NotificationContext changeContext) throws Exception {
    Type type = changeContext.getType();
    long start = System.currentTimeMillis();
    // This allows the listener to work with one change at a time
    synchronized (_manager) {
        if (logger.isInfoEnabled()) {
            logger.info(Thread.currentThread().getId() + " START:INVOKE " + _path + " listener:" + _listener + " type: " + type);
        }
        if (!_expectTypes.contains(type)) {
            logger.warn("Callback handler received event in wrong order. Listener: " + _listener + ", path: " + _path + ", expected types: " + _expectTypes + " but was " + type);
            return;
        }
        _expectTypes = nextNotificationType.get(type);
        if (type == Type.INIT || type == Type.FINALIZE) {
            subscribeForChanges(changeContext.getType(), _path, _watchChild);
        } else {
            // put SubscribeForChange run in async thread to reduce the latency of zk callback handling.
            subscribeForChangesAsyn(changeContext.getType(), _path, _watchChild);
        }
        _expectTypes = nextNotificationType.get(type);
        if (_changeType == IDEAL_STATE) {
            IdealStateChangeListener idealStateChangeListener = (IdealStateChangeListener) _listener;
            List<IdealState> idealStates = preFetch(_propertyKey);
            idealStateChangeListener.onIdealStateChange(idealStates, changeContext);
        } else if (_changeType == INSTANCE_CONFIG) {
            if (_listener instanceof ConfigChangeListener) {
                ConfigChangeListener configChangeListener = (ConfigChangeListener) _listener;
                List<InstanceConfig> configs = preFetch(_propertyKey);
                configChangeListener.onConfigChange(configs, changeContext);
            } else if (_listener instanceof InstanceConfigChangeListener) {
                InstanceConfigChangeListener listener = (InstanceConfigChangeListener) _listener;
                List<InstanceConfig> configs = preFetch(_propertyKey);
                listener.onInstanceConfigChange(configs, changeContext);
            }
        } else if (_changeType == RESOURCE_CONFIG) {
            ResourceConfigChangeListener listener = (ResourceConfigChangeListener) _listener;
            List<ResourceConfig> configs = preFetch(_propertyKey);
            listener.onResourceConfigChange(configs, changeContext);
        } else if (_changeType == CLUSTER_CONFIG) {
            ClusterConfigChangeListener listener = (ClusterConfigChangeListener) _listener;
            ClusterConfig config = null;
            if (_preFetchEnabled) {
                config = _accessor.getProperty(_propertyKey);
            }
            listener.onClusterConfigChange(config, changeContext);
        } else if (_changeType == CONFIG) {
            ScopedConfigChangeListener listener = (ScopedConfigChangeListener) _listener;
            List<HelixProperty> configs = preFetch(_propertyKey);
            listener.onConfigChange(configs, changeContext);
        } else if (_changeType == LIVE_INSTANCE) {
            LiveInstanceChangeListener liveInstanceChangeListener = (LiveInstanceChangeListener) _listener;
            List<LiveInstance> liveInstances = preFetch(_propertyKey);
            liveInstanceChangeListener.onLiveInstanceChange(liveInstances, changeContext);
        } else if (_changeType == CURRENT_STATE) {
            CurrentStateChangeListener currentStateChangeListener = (CurrentStateChangeListener) _listener;
            String instanceName = PropertyPathConfig.getInstanceNameFromPath(_path);
            List<CurrentState> currentStates = preFetch(_propertyKey);
            currentStateChangeListener.onStateChange(instanceName, currentStates, changeContext);
        } else if (_changeType == MESSAGE) {
            MessageListener messageListener = (MessageListener) _listener;
            String instanceName = PropertyPathConfig.getInstanceNameFromPath(_path);
            List<Message> messages = preFetch(_propertyKey);
            messageListener.onMessage(instanceName, messages, changeContext);
        } else if (_changeType == MESSAGES_CONTROLLER) {
            MessageListener messageListener = (MessageListener) _listener;
            List<Message> messages = preFetch(_propertyKey);
            messageListener.onMessage(_manager.getInstanceName(), messages, changeContext);
        } else if (_changeType == EXTERNAL_VIEW || _changeType == TARGET_EXTERNAL_VIEW) {
            ExternalViewChangeListener externalViewListener = (ExternalViewChangeListener) _listener;
            List<ExternalView> externalViewList = preFetch(_propertyKey);
            externalViewListener.onExternalViewChange(externalViewList, changeContext);
        } else if (_changeType == CONTROLLER) {
            ControllerChangeListener controllerChangelistener = (ControllerChangeListener) _listener;
            controllerChangelistener.onControllerChange(changeContext);
        } else {
            logger.warn("Unknown change type: " + _changeType);
        }
        long end = System.currentTimeMillis();
        if (logger.isInfoEnabled()) {
            logger.info(Thread.currentThread().getId() + " END:INVOKE " + _path + " listener:" + _listener + " type: " + type + " Took: " + (end - start) + "ms");
        }
        if (_monitor != null) {
            _monitor.increaseCallbackCounters(end - start);
        }
    }
}
Also used : Message(org.apache.helix.model.Message) MessageListener(org.apache.helix.api.listeners.MessageListener) IdealState(org.apache.helix.model.IdealState) ScopedConfigChangeListener(org.apache.helix.api.listeners.ScopedConfigChangeListener) InstanceConfig(org.apache.helix.model.InstanceConfig) HelixProperty(org.apache.helix.HelixProperty) CurrentState(org.apache.helix.model.CurrentState) ClusterConfigChangeListener(org.apache.helix.api.listeners.ClusterConfigChangeListener) List(java.util.List) ResourceConfig(org.apache.helix.model.ResourceConfig) ResourceConfigChangeListener(org.apache.helix.api.listeners.ResourceConfigChangeListener) InstanceConfigChangeListener(org.apache.helix.api.listeners.InstanceConfigChangeListener) IdealStateChangeListener(org.apache.helix.api.listeners.IdealStateChangeListener) Type(org.apache.helix.NotificationContext.Type) ChangeType(org.apache.helix.HelixConstants.ChangeType) EventType(org.apache.zookeeper.Watcher.Event.EventType) ClusterConfigChangeListener(org.apache.helix.api.listeners.ClusterConfigChangeListener) InstanceConfigChangeListener(org.apache.helix.api.listeners.InstanceConfigChangeListener) ScopedConfigChangeListener(org.apache.helix.api.listeners.ScopedConfigChangeListener) ResourceConfigChangeListener(org.apache.helix.api.listeners.ResourceConfigChangeListener) ConfigChangeListener(org.apache.helix.api.listeners.ConfigChangeListener) CurrentStateChangeListener(org.apache.helix.api.listeners.CurrentStateChangeListener) ControllerChangeListener(org.apache.helix.api.listeners.ControllerChangeListener) LiveInstanceChangeListener(org.apache.helix.api.listeners.LiveInstanceChangeListener) ExternalViewChangeListener(org.apache.helix.api.listeners.ExternalViewChangeListener) ClusterConfig(org.apache.helix.model.ClusterConfig)

Example 49 with InstanceConfig

use of org.apache.helix.model.InstanceConfig in project helix by apache.

the class ParticipantManager method joinCluster.

private void joinCluster() {
    // Read cluster config and see if instance can auto join the cluster
    boolean autoJoin = false;
    try {
        HelixConfigScope scope = new HelixConfigScopeBuilder(ConfigScopeProperty.CLUSTER).forCluster(_manager.getClusterName()).build();
        autoJoin = Boolean.parseBoolean(_configAccessor.get(scope, ZKHelixManager.ALLOW_PARTICIPANT_AUTO_JOIN));
        LOG.info("instance: " + _instanceName + " auto-joining " + _clusterName + " is " + autoJoin);
    } catch (Exception e) {
    // autoJoin is false
    }
    if (!ZKUtil.isInstanceSetup(_zkclient, _clusterName, _instanceName, _instanceType)) {
        if (!autoJoin) {
            throw new HelixException("Initial cluster structure is not set up for instance: " + _instanceName + ", instanceType: " + _instanceType);
        } else {
            LOG.info(_instanceName + " is auto-joining cluster: " + _clusterName);
            InstanceConfig instanceConfig = new InstanceConfig(_instanceName);
            String hostName = _instanceName;
            String port = "";
            int lastPos = _instanceName.lastIndexOf("_");
            if (lastPos > 0) {
                hostName = _instanceName.substring(0, lastPos);
                port = _instanceName.substring(lastPos + 1);
            }
            instanceConfig.setHostName(hostName);
            instanceConfig.setPort(port);
            instanceConfig.setInstanceEnabled(true);
            _helixAdmin.addInstance(_clusterName, instanceConfig);
        }
    }
}
Also used : HelixException(org.apache.helix.HelixException) InstanceConfig(org.apache.helix.model.InstanceConfig) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) HelixConfigScope(org.apache.helix.model.HelixConfigScope) HelixException(org.apache.helix.HelixException) ZkNodeExistsException(org.I0Itec.zkclient.exception.ZkNodeExistsException)

Example 50 with InstanceConfig

use of org.apache.helix.model.InstanceConfig in project helix by apache.

the class ZkUnitTestBase method setupInstances.

protected void setupInstances(String clusterName, int[] instances) {
    HelixAdmin admin = new ZKHelixAdmin(_gZkClient);
    for (int i = 0; i < instances.length; i++) {
        String instance = "localhost_" + instances[i];
        InstanceConfig instanceConfig = new InstanceConfig(instance);
        instanceConfig.setHostName("localhost");
        instanceConfig.setPort("" + instances[i]);
        instanceConfig.setInstanceEnabled(true);
        admin.addInstance(clusterName, instanceConfig);
    }
}
Also used : ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) InstanceConfig(org.apache.helix.model.InstanceConfig) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin)

Aggregations

InstanceConfig (org.apache.helix.model.InstanceConfig)149 ArrayList (java.util.ArrayList)40 Test (org.testng.annotations.Test)35 HashMap (java.util.HashMap)32 HashSet (java.util.HashSet)28 ZNRecord (org.apache.helix.ZNRecord)26 IdealState (org.apache.helix.model.IdealState)24 ExternalView (org.apache.helix.model.ExternalView)23 Map (java.util.Map)21 HelixException (org.apache.helix.HelixException)21 HelixAdmin (org.apache.helix.HelixAdmin)20 List (java.util.List)19 ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)19 HelixDataAccessor (org.apache.helix.HelixDataAccessor)17 ClusterMapConfig (com.github.ambry.config.ClusterMapConfig)15 Test (org.junit.Test)15 Set (java.util.Set)13 VerifiableProperties (com.github.ambry.config.VerifiableProperties)12 IOException (java.io.IOException)12 ZNRecord (org.apache.helix.zookeeper.datamodel.ZNRecord)12