use of org.apache.helix.model.InstanceConfig in project ambry by linkedin.
the class HelixParticipantTest method testSetReplicaSealedState.
/**
* Tests setReplicaSealedState method for {@link HelixParticipant}
* @throws IOException
*/
@Test
public void testSetReplicaSealedState() throws IOException {
// setup HelixParticipant and dependencies
String partitionIdStr = "somePartitionId";
String partitionIdStr2 = "someOtherPartitionId";
ReplicaId replicaId = createMockAmbryReplica(partitionIdStr);
ReplicaId replicaId2 = createMockAmbryReplica(partitionIdStr2);
String hostname = "localhost";
int port = 2200;
String instanceName = ClusterMapUtils.getInstanceName(hostname, port);
HelixParticipant helixParticipant = new HelixParticipant(new ClusterMapConfig(new VerifiableProperties(props)), helixManagerFactory);
helixParticipant.initialize(hostname, port, Collections.EMPTY_LIST);
HelixManager helixManager = helixManagerFactory.getZKHelixManager(null, null, null, null);
HelixAdmin helixAdmin = helixManager.getClusterManagmentTool();
InstanceConfig instanceConfig = new InstanceConfig("someInstanceId");
helixAdmin.setInstanceConfig(clusterName, instanceName, instanceConfig);
// Make sure the current sealedReplicas list is null
List<String> sealedReplicas = ClusterMapUtils.getSealedReplicas(instanceConfig);
assertNull("sealedReplicas is not null", sealedReplicas);
String listName = "sealedReplicas";
// Check that invoking setReplicaSealedState with a non-AmbryReplica ReplicaId throws an IllegalArgumentException
ReplicaId notAmbryReplica = createMockNotAmbryReplica(partitionIdStr);
try {
helixParticipant.setReplicaSealedState(notAmbryReplica, true);
fail("Expected an IllegalArgumentException here");
} catch (IllegalArgumentException e) {
// Expected exception
}
// Check that invoking setReplicaSealedState adds the partition to the list of sealed replicas
helixParticipant.setReplicaSealedState(replicaId, true);
sealedReplicas = ClusterMapUtils.getSealedReplicas(helixAdmin.getInstanceConfig(clusterName, instanceName));
listIsExpectedSize(sealedReplicas, 1, listName);
assertTrue(sealedReplicas.contains(partitionIdStr));
// Seal another replicaId
helixParticipant.setReplicaSealedState(replicaId2, true);
sealedReplicas = ClusterMapUtils.getSealedReplicas(helixAdmin.getInstanceConfig(clusterName, instanceName));
listIsExpectedSize(sealedReplicas, 2, listName);
assertTrue(sealedReplicas.contains(partitionIdStr2));
assertTrue(sealedReplicas.contains(partitionIdStr));
// Check that sealed replica list doesn't take duplicates (and that dups are detected by partitionId comparison, not
// replicaId object comparison
ReplicaId dup = createMockAmbryReplica(partitionIdStr);
helixParticipant.setReplicaSealedState(dup, true);
helixParticipant.setReplicaSealedState(replicaId2, true);
sealedReplicas = ClusterMapUtils.getSealedReplicas(helixAdmin.getInstanceConfig(clusterName, instanceName));
listIsExpectedSize(sealedReplicas, 2, listName);
assertTrue(sealedReplicas.contains(partitionIdStr2));
assertTrue(sealedReplicas.contains(partitionIdStr));
// Check that invoking setReplicaSealedState with isSealed == false removes partition from list of sealed replicas
helixParticipant.setReplicaSealedState(replicaId, false);
sealedReplicas = ClusterMapUtils.getSealedReplicas(helixAdmin.getInstanceConfig(clusterName, instanceName));
listIsExpectedSize(sealedReplicas, 1, listName);
assertTrue(sealedReplicas.contains(partitionIdStr2));
assertFalse(sealedReplicas.contains(partitionIdStr));
// Removing a replicaId that's already been removed doesn't hurt anything
helixParticipant.setReplicaSealedState(replicaId, false);
sealedReplicas = ClusterMapUtils.getSealedReplicas(helixAdmin.getInstanceConfig(clusterName, instanceName));
listIsExpectedSize(sealedReplicas, 1, listName);
// Removing all replicas yields expected behavior (and removal works by partitionId, not replicaId itself)
dup = createMockAmbryReplica(partitionIdStr2);
helixParticipant.setReplicaSealedState(dup, false);
sealedReplicas = ClusterMapUtils.getSealedReplicas(helixAdmin.getInstanceConfig(clusterName, instanceName));
listIsExpectedSize(sealedReplicas, 0, listName);
}
use of org.apache.helix.model.InstanceConfig in project helix by apache.
the class ReadClusterDataStage method process.
@Override
public void process(ClusterEvent event) throws Exception {
HelixManager manager = event.getAttribute(AttributeName.helixmanager.name());
if (manager == null) {
throw new StageException("HelixManager attribute value is null");
}
ClusterDataCache cache = event.getAttribute(AttributeName.ClusterDataCache.name());
if (cache == null && _cache == null) {
cache = new ClusterDataCache(event.getClusterName());
}
_cache = cache;
HelixDataAccessor dataAccessor = manager.getHelixDataAccessor();
_cache.refresh(dataAccessor);
final ClusterConfig clusterConfig = cache.getClusterConfig();
if (!_cache.isTaskCache()) {
final ClusterStatusMonitor clusterStatusMonitor = event.getAttribute(AttributeName.clusterStatusMonitor.name());
asyncExecute(_cache.getAsyncTasksThreadPool(), new Callable<Object>() {
@Override
public Object call() {
// Update the cluster status gauges
if (clusterStatusMonitor != null) {
logger.debug("Update cluster status monitors");
Set<String> instanceSet = Sets.newHashSet();
Set<String> liveInstanceSet = Sets.newHashSet();
Set<String> disabledInstanceSet = Sets.newHashSet();
Map<String, Map<String, List<String>>> disabledPartitions = Maps.newHashMap();
Map<String, List<String>> oldDisabledPartitions = Maps.newHashMap();
Map<String, Set<String>> tags = Maps.newHashMap();
Map<String, LiveInstance> liveInstanceMap = _cache.getLiveInstances();
for (Map.Entry<String, InstanceConfig> e : _cache.getInstanceConfigMap().entrySet()) {
String instanceName = e.getKey();
InstanceConfig config = e.getValue();
instanceSet.add(instanceName);
if (liveInstanceMap.containsKey(instanceName)) {
liveInstanceSet.add(instanceName);
}
if (!config.getInstanceEnabled() || (clusterConfig.getDisabledInstances() != null && clusterConfig.getDisabledInstances().containsKey(instanceName))) {
disabledInstanceSet.add(instanceName);
}
// TODO : Get rid of this data structure once the API is removed.
oldDisabledPartitions.put(instanceName, config.getDisabledPartitions());
disabledPartitions.put(instanceName, config.getDisabledPartitionsMap());
Set<String> instanceTags = Sets.newHashSet(config.getTags());
tags.put(instanceName, instanceTags);
}
clusterStatusMonitor.setClusterInstanceStatus(liveInstanceSet, instanceSet, disabledInstanceSet, disabledPartitions, oldDisabledPartitions, tags);
logger.debug("Complete cluster status monitors update.");
}
return null;
}
});
}
event.addAttribute(AttributeName.ClusterDataCache.name(), _cache);
}
use of org.apache.helix.model.InstanceConfig in project helix by apache.
the class ClusterDataCache method setInstanceConfigs.
public synchronized void setInstanceConfigs(List<InstanceConfig> instanceConfigs) {
Map<String, InstanceConfig> instanceConfigMap = new HashMap();
for (InstanceConfig instanceConfig : instanceConfigs) {
instanceConfigMap.put(instanceConfig.getId(), instanceConfig);
}
_instanceConfigCacheMap = instanceConfigMap;
}
use of org.apache.helix.model.InstanceConfig in project helix by apache.
the class Topology method createClusterTreeWithCustomizedTopology.
/**
* Creates a tree representing the cluster structure using default cluster topology definition
* (i,e no topology definition given and no domain id set).
*/
private Node createClusterTreeWithCustomizedTopology() {
// root
Node root = new Node();
root.setName("root");
root.setId(computeId("root"));
root.setType(Types.ROOT.name());
for (String ins : _allInstances) {
InstanceConfig insConfig = _instanceConfigMap.get(ins);
if (insConfig == null) {
throw new HelixException(String.format("Config for instance %s is not found!", ins));
}
String domain = insConfig.getDomain();
if (domain == null) {
if (insConfig.getInstanceEnabled() && (_clusterConfig.getDisabledInstances() == null || !_clusterConfig.getDisabledInstances().containsKey(ins))) {
// if enabled instance missing domain information, fails the rebalance.
throw new HelixException(String.format("Domain for instance %s is not set, failed the topology-aware placement!", ins));
} else {
// if the disabled instance missing domain setting, ignore it should be fine.
logger.warn(String.format("Domain for instance %s is not set, ignore the instance!", ins));
continue;
}
}
String[] pathPairs = domain.trim().split(",");
Map<String, String> pathValueMap = new HashMap<String, String>();
for (String pair : pathPairs) {
String[] values = pair.trim().split("=");
if (values.length != 2 || values[0].isEmpty() || values[1].isEmpty()) {
throw new HelixException(String.format("Domain-Value pair %s for instance %s is not valid, failed the topology-aware placement!", pair, ins));
}
String type = values[0];
String value = values[1];
if (!_types.contains(type)) {
logger.warn(String.format("Path %s defined in domain of instance %s not recognized, ignored!", pair, ins));
continue;
}
pathValueMap.put(type, value);
}
int weight = insConfig.getWeight();
if (weight < 0 || weight == InstanceConfig.WEIGHT_NOT_SET) {
weight = DEFAULT_NODE_WEIGHT;
}
root = addEndNode(root, ins, pathValueMap, weight, _liveInstances);
}
return root;
}
use of org.apache.helix.model.InstanceConfig in project helix by apache.
the class Topology method createClusterTreeWithDefaultTopologyDef.
/**
* Creates a tree representing the cluster structure using default cluster topology definition
* (i,e no topology definition given and no domain id set).
*/
private Node createClusterTreeWithDefaultTopologyDef() {
// root
Node root = new Node();
root.setName("root");
root.setId(computeId("root"));
root.setType(Types.ROOT.name());
for (String ins : _allInstances) {
InstanceConfig config = _instanceConfigMap.get(ins);
if (config == null) {
throw new HelixException(String.format("Config for instance %s is not found!", ins));
}
Map<String, String> pathValueMap = new HashMap<>();
if (_topologyAwareEnabled) {
String zone = config.getZoneId();
if (zone == null) {
// we have the hierarchy style of domain id for instance.
if (config.getInstanceEnabled() && (_clusterConfig.getDisabledInstances() == null || !_clusterConfig.getDisabledInstances().containsKey(ins))) {
// if enabled instance missing ZONE_ID information, fails the rebalance.
throw new HelixException(String.format("ZONE_ID for instance %s is not set, failed the topology-aware placement!", ins));
} else {
// if the disabled instance missing ZONE setting, ignore it should be fine.
logger.warn(String.format("ZONE_ID for instance %s is not set, failed the topology-aware placement!", ins));
continue;
}
}
pathValueMap.put(Types.ZONE.name(), zone);
}
pathValueMap.put(Types.INSTANCE.name(), ins);
int weight = config.getWeight();
if (weight < 0 || weight == InstanceConfig.WEIGHT_NOT_SET) {
weight = DEFAULT_NODE_WEIGHT;
}
root = addEndNode(root, ins, pathValueMap, weight, _liveInstances);
}
return root;
}
Aggregations