use of org.apache.helix.model.IdealState in project helix by apache.
the class TestIdealStateBuilder method testSemiAutoISBuilder.
@Test
public void testSemiAutoISBuilder() {
SemiAutoModeISBuilder builder = new SemiAutoModeISBuilder("test-db");
builder.setStateModel("MasterSlave").setNumPartitions(2).setNumReplica(2);
builder.assignPreferenceList("test-db_0", "node_0", "node_1").assignPreferenceList("test-db_1", "node_1", "node_0");
IdealState idealState = null;
try {
idealState = builder.build();
} catch (Exception e) {
Assert.fail("fail to build an auto mode ideal-state.", e);
}
// System.out.println("ideal-state: " + idealState);
Assert.assertEquals(idealState.getRebalanceMode(), IdealState.RebalanceMode.SEMI_AUTO, "rebalancer mode should be semi-auto");
}
use of org.apache.helix.model.IdealState in project helix by apache.
the class ClusterSetup method expandResource.
public void expandResource(String clusterName, String resourceName) {
IdealState idealState = _admin.getResourceIdealState(clusterName, resourceName);
if (idealState.getRebalanceMode() == RebalanceMode.FULL_AUTO || idealState.getRebalanceMode() == RebalanceMode.CUSTOMIZED) {
_logger.info("Skipping idealState " + idealState.getResourceName() + " " + idealState.getRebalanceMode());
return;
}
boolean anyLiveInstance = false;
for (List<String> list : idealState.getRecord().getListFields().values()) {
if (list.contains(IdealState.IdealStateConstants.ANY_LIVEINSTANCE.toString())) {
_logger.info("Skipping idealState " + idealState.getResourceName() + " with ANY_LIVEINSTANCE");
anyLiveInstance = true;
continue;
}
}
if (anyLiveInstance) {
return;
}
try {
int replica = Integer.parseInt(idealState.getReplicas());
} catch (Exception e) {
_logger.error("", e);
return;
}
if (idealState.getRecord().getListFields().size() == 0) {
_logger.warn("Resource " + resourceName + " not balanced, skip");
return;
}
balanceIdealState(clusterName, idealState);
}
use of org.apache.helix.model.IdealState in project helix by apache.
the class ClusterSetup method addResourceProperty.
public void addResourceProperty(String clusterName, String resourceName, String propertyKey, String propertyVal) {
IdealState idealState = _admin.getResourceIdealState(clusterName, resourceName);
if (idealState == null) {
throw new HelixException("Resource: " + resourceName + " has NOT been added yet");
}
idealState.getRecord().setSimpleField(propertyKey, propertyVal);
_admin.setResourceIdealState(clusterName, resourceName, idealState);
}
use of org.apache.helix.model.IdealState in project helix by apache.
the class ClusterSetup method createIdealStateForResourceGroup.
/**
* Create an IdealState for a resource that belongs to a resource group We use
* "resourceGroupName$resourceInstanceTag" as the IdealState znode name to differetiate different
* resources from the same resourceGroup.
*/
public IdealState createIdealStateForResourceGroup(String resourceGroupName, String resourceTag, int numPartition, int replica, String rebalanceMode, String stateModelDefName) {
String idealStateId = genIdealStateNameWithResourceTag(resourceGroupName, resourceTag);
IdealState idealState = new IdealState(idealStateId);
idealState.setNumPartitions(numPartition);
idealState.setStateModelDefRef(stateModelDefName);
IdealState.RebalanceMode mode = idealState.rebalanceModeFromString(rebalanceMode, IdealState.RebalanceMode.SEMI_AUTO);
idealState.setRebalanceMode(mode);
idealState.setReplicas("" + replica);
idealState.setStateModelFactoryName(HelixConstants.DEFAULT_STATE_MODEL_FACTORY);
idealState.setResourceGroupName(resourceGroupName);
idealState.setInstanceGroupTag(resourceTag);
idealState.enableGroupRouting(true);
return idealState;
}
use of org.apache.helix.model.IdealState in project helix by apache.
the class StrictMatchExternalViewVerifier method verifyState.
@Override
protected boolean verifyState() {
try {
PropertyKey.Builder keyBuilder = _accessor.keyBuilder();
// read cluster once and do verification
ClusterDataCache cache = new ClusterDataCache();
cache.refresh(_accessor);
Map<String, IdealState> idealStates = cache.getIdealStates();
if (idealStates == null) {
// ideal state is null because ideal state is dropped
idealStates = Collections.emptyMap();
}
// filter out all resources that use Task state model
Iterator<Map.Entry<String, IdealState>> it = idealStates.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, IdealState> pair = it.next();
if (pair.getValue().getStateModelDefRef().equals(TaskConstants.STATE_MODEL_NAME)) {
it.remove();
}
}
// verify live instances.
if (_expectLiveInstances != null && !_expectLiveInstances.isEmpty()) {
Set<String> actualLiveNodes = cache.getLiveInstances().keySet();
if (!_expectLiveInstances.equals(actualLiveNodes)) {
return false;
}
}
Map<String, ExternalView> extViews = _accessor.getChildValuesMap(keyBuilder.externalViews());
if (extViews == null) {
extViews = Collections.emptyMap();
}
// Filter resources if requested
if (_resources != null && !_resources.isEmpty()) {
idealStates.keySet().retainAll(_resources);
extViews.keySet().retainAll(_resources);
}
// add empty idealState for the resource
for (String resource : extViews.keySet()) {
if (!idealStates.containsKey(resource)) {
idealStates.put(resource, new IdealState(resource));
}
}
for (String resourceName : idealStates.keySet()) {
ExternalView extView = extViews.get(resourceName);
IdealState idealState = idealStates.get(resourceName);
if (extView == null) {
if (idealState.isExternalViewDisabled()) {
continue;
} else {
LOG.debug("externalView for " + resourceName + " is not available");
return false;
}
}
boolean result = verifyExternalView(cache, extView, idealState);
if (!result) {
return false;
}
}
return true;
} catch (Exception e) {
LOG.error("exception in verification", e);
return false;
}
}
Aggregations