Search in sources :

Example 1 with CustomizedState

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

the class TestCustomizedStateUpdate method beforeMethod.

@BeforeMethod
public void beforeMethod() {
    _dataAccessor.removeProperty(_propertyKey);
    CustomizedState customizedStates = _dataAccessor.getProperty(_propertyKey);
    Assert.assertNull(customizedStates);
}
Also used : CustomizedState(org.apache.helix.model.CustomizedState) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 2 with CustomizedState

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

the class CustomizedStateProvider method deletePerPartitionCustomizedState.

/**
 * Delete the customized state for a specified resource and a specified partition
 */
public void deletePerPartitionCustomizedState(String customizedStateName, String resourceName, String partitionName) {
    HelixDataAccessor accessor = _helixManager.getHelixDataAccessor();
    PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    PropertyKey propertyKey = keyBuilder.customizedState(_instanceName, customizedStateName, resourceName);
    CustomizedState existingState = getCustomizedState(customizedStateName, resourceName);
    ZNRecord rec = new ZNRecord(existingState.getId());
    rec.getMapFields().put(partitionName, null);
    ZNRecordDelta delta = new ZNRecordDelta(rec, ZNRecordDelta.MergeOperation.SUBTRACT);
    List<ZNRecordDelta> deltaList = new ArrayList<ZNRecordDelta>();
    deltaList.add(delta);
    existingState.setDeltaList(deltaList);
    if (!accessor.updateProperty(propertyKey, existingState)) {
        throw new HelixException(String.format("Failed to delete customized state %s to zk for instance %s, resource %s, " + "partition %s", customizedStateName, _instanceName, resourceName, partitionName));
    }
}
Also used : HelixException(org.apache.helix.HelixException) HelixDataAccessor(org.apache.helix.HelixDataAccessor) CustomizedState(org.apache.helix.model.CustomizedState) ArrayList(java.util.ArrayList) PropertyKey(org.apache.helix.PropertyKey) ZNRecord(org.apache.helix.zookeeper.datamodel.ZNRecord) ZNRecordDelta(org.apache.helix.zookeeper.datamodel.ZNRecordDelta)

Example 3 with CustomizedState

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

the class CustomizedStateProvider method getCustomizedState.

/**
 * Get the customized state for a specified resource
 */
public CustomizedState getCustomizedState(String customizedStateName, String resourceName) {
    HelixDataAccessor accessor = _helixManager.getHelixDataAccessor();
    PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    return (CustomizedState) accessor.getProperty(keyBuilder.customizedState(_instanceName, customizedStateName, resourceName));
}
Also used : HelixDataAccessor(org.apache.helix.HelixDataAccessor) CustomizedState(org.apache.helix.model.CustomizedState) PropertyKey(org.apache.helix.PropertyKey)

Example 4 with CustomizedState

use of org.apache.helix.model.CustomizedState 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();
    if (logger.isInfoEnabled()) {
        logger.info("{} START: CallbackHandler {}, INVOKE {} listener: {} type: {}", Thread.currentThread().getId(), _uid, _path, _listener, type);
    }
    synchronized (this) {
        if (!_expectTypes.contains(type)) {
            logger.warn("Callback handler {} received event in wrong order. Listener: {}, path: {}, " + "expected types: {}, but was {}", _uid, _listener, _path, _expectTypes, type);
            return;
        }
        _expectTypes = nextNotificationType.get(type);
        if (type == Type.INIT || type == Type.FINALIZE || changeContext.getIsChildChange()) {
            subscribeForChanges(changeContext.getType(), _path, _watchChild);
        }
    }
    // potential improvement candidate.
    synchronized (_manager) {
        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 == CUSTOMIZED_STATE_CONFIG) {
            CustomizedStateConfigChangeListener listener = (CustomizedStateConfigChangeListener) _listener;
            CustomizedStateConfig config = null;
            if (_preFetchEnabled) {
                config = _accessor.getProperty(_propertyKey);
            }
            listener.onCustomizedStateConfigChange(config, 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 == TASK_CURRENT_STATE) {
            TaskCurrentStateChangeListener taskCurrentStateChangeListener = (TaskCurrentStateChangeListener) _listener;
            String instanceName = PropertyPathConfig.getInstanceNameFromPath(_path);
            List<CurrentState> currentStates = preFetch(_propertyKey);
            taskCurrentStateChangeListener.onTaskCurrentStateChange(instanceName, currentStates, changeContext);
        } else if (_changeType == CUSTOMIZED_STATE_ROOT) {
            CustomizedStateRootChangeListener customizedStateRootChangeListener = (CustomizedStateRootChangeListener) _listener;
            String instanceName = PropertyPathConfig.getInstanceNameFromPath(_path);
            List<String> customizedStateTypes = new ArrayList<>();
            if (_preFetchEnabled) {
                customizedStateTypes = _accessor.getChildNames(_accessor.keyBuilder().customizedStatesRoot(instanceName));
            }
            customizedStateRootChangeListener.onCustomizedStateRootChange(instanceName, customizedStateTypes, changeContext);
        } else if (_changeType == CUSTOMIZED_STATE) {
            CustomizedStateChangeListener customizedStateChangeListener = (CustomizedStateChangeListener) _listener;
            String instanceName = PropertyPathConfig.getInstanceNameFromPath(_path);
            List<CustomizedState> customizedStates = preFetch(_propertyKey);
            customizedStateChangeListener.onCustomizedStateChange(instanceName, customizedStates, 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 == CUSTOMIZED_VIEW_ROOT) {
            CustomizedViewRootChangeListener customizedViewRootChangeListener = (CustomizedViewRootChangeListener) _listener;
            List<String> customizedViewTypes = new ArrayList<>();
            if (_preFetchEnabled) {
                customizedViewTypes = _accessor.getChildNames(_accessor.keyBuilder().customizedViews());
            }
            customizedViewRootChangeListener.onCustomizedViewRootChange(customizedViewTypes, changeContext);
        } else if (_changeType == CUSTOMIZED_VIEW) {
            CustomizedViewChangeListener customizedViewListener = (CustomizedViewChangeListener) _listener;
            List<CustomizedView> customizedViewListList = preFetch(_propertyKey);
            customizedViewListener.onCustomizedViewChange(customizedViewListList, changeContext);
        } else if (_changeType == CONTROLLER) {
            ControllerChangeListener controllerChangelistener = (ControllerChangeListener) _listener;
            controllerChangelistener.onControllerChange(changeContext);
        } else {
            logger.warn("Callbackhandler {}, Unknown change type: {}", _uid, _changeType);
        }
        long end = System.currentTimeMillis();
        if (logger.isInfoEnabled()) {
            logger.info("{} END:INVOKE CallbackHandler {}, {} listener: {} type: {} Took: {}ms", Thread.currentThread().getId(), _uid, _path, _listener, type, (end - start));
        }
        if (_monitor != null) {
            _monitor.increaseCallbackCounters(end - start);
        }
    }
}
Also used : Message(org.apache.helix.model.Message) CustomizedState(org.apache.helix.model.CustomizedState) MessageListener(org.apache.helix.api.listeners.MessageListener) ArrayList(java.util.ArrayList) IdealState(org.apache.helix.model.IdealState) ScopedConfigChangeListener(org.apache.helix.api.listeners.ScopedConfigChangeListener) InstanceConfig(org.apache.helix.model.InstanceConfig) LiveInstance(org.apache.helix.model.LiveInstance) CurrentState(org.apache.helix.model.CurrentState) CustomizedStateConfig(org.apache.helix.model.CustomizedStateConfig) ClusterConfigChangeListener(org.apache.helix.api.listeners.ClusterConfigChangeListener) List(java.util.List) ArrayList(java.util.ArrayList) CustomizedViewRootChangeListener(org.apache.helix.api.listeners.CustomizedViewRootChangeListener) ResourceConfig(org.apache.helix.model.ResourceConfig) TaskCurrentStateChangeListener(org.apache.helix.api.listeners.TaskCurrentStateChangeListener) ResourceConfigChangeListener(org.apache.helix.api.listeners.ResourceConfigChangeListener) CustomizedStateChangeListener(org.apache.helix.api.listeners.CustomizedStateChangeListener) InstanceConfigChangeListener(org.apache.helix.api.listeners.InstanceConfigChangeListener) CustomizedViewChangeListener(org.apache.helix.api.listeners.CustomizedViewChangeListener) 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) CustomizedStateConfigChangeListener(org.apache.helix.api.listeners.CustomizedStateConfigChangeListener) TaskCurrentStateChangeListener(org.apache.helix.api.listeners.TaskCurrentStateChangeListener) CurrentStateChangeListener(org.apache.helix.api.listeners.CurrentStateChangeListener) ControllerChangeListener(org.apache.helix.api.listeners.ControllerChangeListener) LiveInstanceChangeListener(org.apache.helix.api.listeners.LiveInstanceChangeListener) CustomizedStateRootChangeListener(org.apache.helix.api.listeners.CustomizedStateRootChangeListener) ExternalViewChangeListener(org.apache.helix.api.listeners.ExternalViewChangeListener) CustomizedStateConfigChangeListener(org.apache.helix.api.listeners.CustomizedStateConfigChangeListener) ClusterConfig(org.apache.helix.model.ClusterConfig)

Example 5 with CustomizedState

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

the class TestCustomizedViewStage method testLatencyCalculationWithEmptyTimestamp.

@Test
public void testLatencyCalculationWithEmptyTimestamp() throws Exception {
    String clusterName = "CLUSTER_" + TestHelper.getTestMethodName();
    HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<>(_gZkClient));
    HelixManager manager = new DummyClusterManager(clusterName, accessor);
    setupIdealState(clusterName, new int[] { 0, 1 }, new String[] { "TestDB" }, 1, 2);
    setupLiveInstances(clusterName, new int[] { 0, 1 });
    setupStateModel(clusterName);
    ClusterStatusMonitor clusterStatusMonitor = new ClusterStatusMonitor(clusterName);
    ClusterEvent event = new ClusterEvent(clusterName, ClusterEventType.Unknown);
    ResourceControllerDataProvider cache = new ResourceControllerDataProvider(clusterName);
    ExecutorService executor = Executors.newSingleThreadExecutor();
    cache.setAsyncTasksThreadPool(executor);
    event.addAttribute(AttributeName.helixmanager.name(), manager);
    event.addAttribute(AttributeName.ControllerDataProvider.name(), cache);
    event.addAttribute(AttributeName.clusterStatusMonitor.name(), clusterStatusMonitor);
    CustomizedStateConfig config = new CustomizedStateConfig();
    List<String> aggregationEnabledTypes = new ArrayList<>();
    aggregationEnabledTypes.add(CUSTOMIZED_STATE_NAME);
    config.setAggregationEnabledTypes(aggregationEnabledTypes);
    PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    accessor.setProperty(keyBuilder.customizedStateConfig(), config);
    CustomizedState customizedState = new CustomizedState(RESOURCE_NAME);
    customizedState.setState(PARTITION_NAME, "STATE");
    accessor.setProperty(keyBuilder.customizedState(INSTANCE_NAME, CUSTOMIZED_STATE_NAME, RESOURCE_NAME), customizedState);
    Pipeline dataRefresh = new Pipeline();
    dataRefresh.addStage(new ReadClusterDataStage());
    runPipeline(event, dataRefresh, false);
    runStage(event, new ResourceComputationStage());
    runStage(event, new CustomizedStateComputationStage());
    runStage(event, new CustomizedViewAggregationStage());
    ObjectName objectName = new ObjectName(String.format("%s:%s=%s,%s=%s", MonitorDomainNames.AggregatedView.name(), "Type", "CustomizedView", "Cluster", clusterName));
    Field customizedViewMonitor = ClusterStatusMonitor.class.getDeclaredField("_customizedViewMonitor");
    Assert.assertNotNull(customizedViewMonitor);
    boolean hasLatencyReported = TestHelper.verify(() -> (long) _server.getAttribute(objectName, CustomizedViewMonitor.UPDATE_TO_AGGREGATION_LATENCY_GAUGE + ".Max") != 0, TestHelper.WAIT_DURATION);
    Assert.assertFalse(hasLatencyReported);
    deleteLiveInstances(clusterName);
    deleteCluster(clusterName);
    executor.shutdownNow();
}
Also used : HelixManager(org.apache.helix.HelixManager) CustomizedState(org.apache.helix.model.CustomizedState) ArrayList(java.util.ArrayList) ClusterStatusMonitor(org.apache.helix.monitoring.mbeans.ClusterStatusMonitor) ResourceControllerDataProvider(org.apache.helix.controller.dataproviders.ResourceControllerDataProvider) Pipeline(org.apache.helix.controller.pipeline.Pipeline) ObjectName(javax.management.ObjectName) Field(java.lang.reflect.Field) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) HelixDataAccessor(org.apache.helix.HelixDataAccessor) ExecutorService(java.util.concurrent.ExecutorService) CustomizedStateConfig(org.apache.helix.model.CustomizedStateConfig) PropertyKey(org.apache.helix.PropertyKey) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) Test(org.testng.annotations.Test)

Aggregations

CustomizedState (org.apache.helix.model.CustomizedState)17 Test (org.testng.annotations.Test)10 PropertyKey (org.apache.helix.PropertyKey)8 ArrayList (java.util.ArrayList)7 HelixDataAccessor (org.apache.helix.HelixDataAccessor)6 CustomizedStateConfig (org.apache.helix.model.CustomizedStateConfig)6 ResourceControllerDataProvider (org.apache.helix.controller.dataproviders.ResourceControllerDataProvider)5 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)4 HashMap (java.util.HashMap)3 HelixManager (org.apache.helix.HelixManager)3 Pipeline (org.apache.helix.controller.pipeline.Pipeline)3 ZNRecord (org.apache.helix.zookeeper.datamodel.ZNRecord)3 Field (java.lang.reflect.Field)2 ExecutorService (java.util.concurrent.ExecutorService)2 ObjectName (javax.management.ObjectName)2 HelixException (org.apache.helix.HelixException)2 Resource (org.apache.helix.model.Resource)2 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1