Search in sources :

Example 1 with ConfigChangeListener

use of org.apache.helix.api.listeners.ConfigChangeListener 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 2 with ConfigChangeListener

use of org.apache.helix.api.listeners.ConfigChangeListener in project helix by apache.

the class CallbackHandler method parseListenerProperties.

private void parseListenerProperties() {
    BatchMode batchMode = _listener.getClass().getAnnotation(BatchMode.class);
    PreFetch preFetch = _listener.getClass().getAnnotation(PreFetch.class);
    String asyncBatchModeEnabled = System.getProperty("helix.callbackhandler.isAsyncBatchModeEnabled");
    if (asyncBatchModeEnabled == null) {
        // for backcompatible, the old property name is deprecated.
        asyncBatchModeEnabled = System.getProperty("isAsyncBatchModeEnabled");
    }
    if (asyncBatchModeEnabled != null) {
        _batchModeEnabled = Boolean.parseBoolean(asyncBatchModeEnabled);
        logger.info("isAsyncBatchModeEnabled by default: " + _batchModeEnabled);
    }
    if (batchMode != null) {
        _batchModeEnabled = batchMode.enabled();
    }
    if (preFetch != null) {
        _preFetchEnabled = preFetch.enabled();
    }
    Class listenerClass = null;
    switch(_changeType) {
        case IDEAL_STATE:
            listenerClass = IdealStateChangeListener.class;
            break;
        case INSTANCE_CONFIG:
            if (_listener instanceof ConfigChangeListener) {
                listenerClass = ConfigChangeListener.class;
            } else if (_listener instanceof InstanceConfigChangeListener) {
                listenerClass = InstanceConfigChangeListener.class;
            }
            break;
        case CLUSTER_CONFIG:
            listenerClass = ClusterConfigChangeListener.class;
            break;
        case RESOURCE_CONFIG:
            listenerClass = ResourceConfigChangeListener.class;
            break;
        case CONFIG:
            listenerClass = ConfigChangeListener.class;
            break;
        case LIVE_INSTANCE:
            listenerClass = LiveInstanceChangeListener.class;
            break;
        case CURRENT_STATE:
            listenerClass = CurrentStateChangeListener.class;
            ;
            break;
        case MESSAGE:
        case MESSAGES_CONTROLLER:
            listenerClass = MessageListener.class;
            break;
        case EXTERNAL_VIEW:
        case TARGET_EXTERNAL_VIEW:
            listenerClass = ExternalViewChangeListener.class;
            break;
        case CONTROLLER:
            listenerClass = ControllerChangeListener.class;
    }
    Method callbackMethod = listenerClass.getMethods()[0];
    try {
        Method method = _listener.getClass().getMethod(callbackMethod.getName(), callbackMethod.getParameterTypes());
        BatchMode batchModeInMethod = method.getAnnotation(BatchMode.class);
        PreFetch preFetchInMethod = method.getAnnotation(PreFetch.class);
        if (batchModeInMethod != null) {
            _batchModeEnabled = batchModeInMethod.enabled();
        }
        if (preFetchInMethod != null) {
            _preFetchEnabled = preFetchInMethod.enabled();
        }
    } catch (NoSuchMethodException e) {
        logger.warn("No method " + callbackMethod.getName() + " defined in listener " + _listener.getClass().getCanonicalName());
    }
}
Also used : PreFetch(org.apache.helix.api.listeners.PreFetch) 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) InstanceConfigChangeListener(org.apache.helix.api.listeners.InstanceConfigChangeListener) BatchMode(org.apache.helix.api.listeners.BatchMode) Method(java.lang.reflect.Method)

Aggregations

ClusterConfigChangeListener (org.apache.helix.api.listeners.ClusterConfigChangeListener)2 ConfigChangeListener (org.apache.helix.api.listeners.ConfigChangeListener)2 InstanceConfigChangeListener (org.apache.helix.api.listeners.InstanceConfigChangeListener)2 ResourceConfigChangeListener (org.apache.helix.api.listeners.ResourceConfigChangeListener)2 ScopedConfigChangeListener (org.apache.helix.api.listeners.ScopedConfigChangeListener)2 Method (java.lang.reflect.Method)1 List (java.util.List)1 ChangeType (org.apache.helix.HelixConstants.ChangeType)1 HelixProperty (org.apache.helix.HelixProperty)1 Type (org.apache.helix.NotificationContext.Type)1 BatchMode (org.apache.helix.api.listeners.BatchMode)1 ControllerChangeListener (org.apache.helix.api.listeners.ControllerChangeListener)1 CurrentStateChangeListener (org.apache.helix.api.listeners.CurrentStateChangeListener)1 ExternalViewChangeListener (org.apache.helix.api.listeners.ExternalViewChangeListener)1 IdealStateChangeListener (org.apache.helix.api.listeners.IdealStateChangeListener)1 LiveInstanceChangeListener (org.apache.helix.api.listeners.LiveInstanceChangeListener)1 MessageListener (org.apache.helix.api.listeners.MessageListener)1 PreFetch (org.apache.helix.api.listeners.PreFetch)1 ClusterConfig (org.apache.helix.model.ClusterConfig)1 CurrentState (org.apache.helix.model.CurrentState)1