use of org.apache.helix.model.ClusterConfig 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);
}
}
}
use of org.apache.helix.model.ClusterConfig in project helix by apache.
the class BaseStageTest method setup.
public void setup() {
_clusterName = "testCluster-" + UUID.randomUUID().toString();
manager = new MockManager(_clusterName);
accessor = manager.getHelixDataAccessor();
ClusterConfig clusterConfig = new ClusterConfig(_clusterName);
setClusterConfig(clusterConfig);
admin = new MockHelixAdmin(manager);
event = new ClusterEvent(ClusterEventType.Unknown);
admin.addCluster(_clusterName);
}
use of org.apache.helix.model.ClusterConfig in project helix by apache.
the class TestResourceValidationStage method testMissingStateModel.
@Test
public void testMissingStateModel() throws Exception {
MockAccessor accessor = new MockAccessor();
accessor.setProperty(accessor.keyBuilder().clusterConfig(), new ClusterConfig("TestCluster"));
// create an ideal state and no spec
String masterSlaveCustomResource = "masterSlaveCustomResource";
String leaderStandbyCustomResource = "leaderStandbyCustomResource";
createIS(accessor, masterSlaveCustomResource, "MasterSlave", RebalanceMode.CUSTOMIZED);
createIS(accessor, leaderStandbyCustomResource, "LeaderStandby", RebalanceMode.CUSTOMIZED);
// add some state models (but not leader standby)
addStateModels(accessor);
// refresh the cache
ClusterEvent event = new ClusterEvent(ClusterEventType.Unknown);
ClusterDataCache cache = new ClusterDataCache();
cache.refresh(accessor);
event.addAttribute(AttributeName.ClusterDataCache.name(), cache);
// run resource computation
new ResourceComputationStage().process(event);
Map<String, Resource> resourceMap = event.getAttribute(AttributeName.RESOURCES.name());
Assert.assertTrue(resourceMap.containsKey(masterSlaveCustomResource));
Assert.assertTrue(resourceMap.containsKey(leaderStandbyCustomResource));
// run resource validation
new ResourceValidationStage().process(event);
Map<String, Resource> finalResourceMap = event.getAttribute(AttributeName.RESOURCES.name());
Assert.assertTrue(finalResourceMap.containsKey(masterSlaveCustomResource));
Assert.assertFalse(finalResourceMap.containsKey(leaderStandbyCustomResource));
}
use of org.apache.helix.model.ClusterConfig in project helix by apache.
the class TestResourceValidationStage method testNoSpec.
@Test
public void testNoSpec() throws Exception {
MockAccessor accessor = new MockAccessor();
accessor.setProperty(accessor.keyBuilder().clusterConfig(), new ClusterConfig("TestCluster"));
// create an ideal state and no spec
String masterSlaveCustomResource = "masterSlaveCustomResource";
createIS(accessor, masterSlaveCustomResource, "MasterSlave", RebalanceMode.CUSTOMIZED);
// add some state models
addStateModels(accessor);
// refresh the cache
ClusterEvent event = new ClusterEvent(ClusterEventType.Unknown);
ClusterDataCache cache = new ClusterDataCache();
cache.refresh(accessor);
event.addAttribute(AttributeName.ClusterDataCache.name(), cache);
// run resource computation
new ResourceComputationStage().process(event);
Map<String, Resource> resourceMap = event.getAttribute(AttributeName.RESOURCES.name());
Assert.assertTrue(resourceMap.containsKey(masterSlaveCustomResource));
// run resource validation
new ResourceValidationStage().process(event);
Map<String, Resource> finalResourceMap = event.getAttribute(AttributeName.RESOURCES.name());
Assert.assertTrue(finalResourceMap.containsKey(masterSlaveCustomResource));
}
use of org.apache.helix.model.ClusterConfig in project helix by apache.
the class TestTopology method testCreateClusterTopologyWithDefaultTopology.
@Test
public void testCreateClusterTopologyWithDefaultTopology() {
ClusterConfig clusterConfig = new ClusterConfig("Test_Cluster");
clusterConfig.setTopologyAwareEnabled(true);
List<String> allNodes = new ArrayList<String>();
List<String> liveNodes = new ArrayList<String>();
Map<String, InstanceConfig> instanceConfigMap = new HashMap<String, InstanceConfig>();
Map<String, Integer> nodeToWeightMap = new HashMap<String, Integer>();
for (int i = 0; i < 100; i++) {
String instance = "localhost_" + i;
InstanceConfig config = new InstanceConfig(instance);
String zoneId = "rack_" + i / 10;
config.setZoneId(zoneId);
config.setHostName(instance);
config.setPort("9000");
allNodes.add(instance);
int weight = 0;
if (i % 10 != 0) {
liveNodes.add(instance);
weight = 1000;
if (i % 3 == 0) {
// set random instance weight.
weight = (i + 1) * 100;
config.setWeight(weight);
}
}
instanceConfigMap.put(instance, config);
if (!nodeToWeightMap.containsKey(zoneId)) {
nodeToWeightMap.put(zoneId, 0);
}
nodeToWeightMap.put(zoneId, nodeToWeightMap.get(zoneId) + weight);
}
Topology topo = new Topology(allNodes, liveNodes, instanceConfigMap, clusterConfig);
Assert.assertTrue(topo.getEndNodeType().equals(Topology.Types.INSTANCE.name()));
Assert.assertTrue(topo.getFaultZoneType().equals(Topology.Types.ZONE.name()));
List<Node> faultZones = topo.getFaultZones();
Assert.assertEquals(faultZones.size(), 10);
Node root = topo.getRootNode();
Assert.assertEquals(root.getChildrenCount(Topology.Types.ZONE.name()), 10);
Assert.assertEquals(root.getChildrenCount(topo.getEndNodeType()), 100);
// validate weights.
for (Node rack : root.getChildren()) {
Assert.assertEquals(rack.getWeight(), (long) nodeToWeightMap.get(rack.getName()));
}
}
Aggregations