use of org.apache.helix.manager.zk.ZKHelixDataAccessor in project helix by apache.
the class ZkTestHelper method verifyState.
/*
* stateMap: partition->instance->state
*/
public static boolean verifyState(ZkClient zkclient, String clusterName, String resourceName, Map<String, Map<String, String>> expectStateMap, String op) {
boolean result = true;
ZkBaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(zkclient);
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor);
Builder keyBuilder = accessor.keyBuilder();
ExternalView extView = accessor.getProperty(keyBuilder.externalView(resourceName));
Map<String, Map<String, String>> actualStateMap = extView.getRecord().getMapFields();
for (String partition : actualStateMap.keySet()) {
for (String expectPartiton : expectStateMap.keySet()) {
if (!partition.matches(expectPartiton)) {
continue;
}
Map<String, String> actualInstanceStateMap = actualStateMap.get(partition);
Map<String, String> expectInstanceStateMap = expectStateMap.get(expectPartiton);
for (String instance : actualInstanceStateMap.keySet()) {
for (String expectInstance : expectStateMap.get(expectPartiton).keySet()) {
if (!instance.matches(expectInstance)) {
continue;
}
String actualState = actualInstanceStateMap.get(instance);
String expectState = expectInstanceStateMap.get(expectInstance);
boolean equals = expectState.equals(actualState);
if (op.equals("==") && !equals || op.equals("!=") && equals) {
System.out.println(partition + "/" + instance + " state mismatch. actual state: " + actualState + ", but expect: " + expectState + ", op: " + op);
result = false;
}
}
}
}
}
return result;
}
use of org.apache.helix.manager.zk.ZKHelixDataAccessor in project helix by apache.
the class ZkUnitTestBase method verifyReplication.
public void verifyReplication(ZkClient zkClient, String clusterName, String resource, int repl) {
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(zkClient));
Builder keyBuilder = accessor.keyBuilder();
IdealState idealState = accessor.getProperty(keyBuilder.idealStates(resource));
for (String partitionName : idealState.getPartitionSet()) {
if (idealState.getRebalanceMode() == RebalanceMode.SEMI_AUTO) {
AssertJUnit.assertEquals(repl, idealState.getPreferenceList(partitionName).size());
} else if (idealState.getRebalanceMode() == RebalanceMode.CUSTOMIZED) {
AssertJUnit.assertEquals(repl, idealState.getInstanceStateMap(partitionName).size());
}
}
}
use of org.apache.helix.manager.zk.ZKHelixDataAccessor in project helix by apache.
the class ZkUnitTestBase method setupIdealState.
protected List<IdealState> setupIdealState(String clusterName, int[] nodes, String[] resources, int partitions, int replicas) {
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
Builder keyBuilder = accessor.keyBuilder();
List<IdealState> idealStates = new ArrayList<IdealState>();
List<String> instances = new ArrayList<String>();
for (int i : nodes) {
instances.add("localhost_" + i);
}
for (String resourceName : resources) {
IdealState idealState = new IdealState(resourceName);
for (int p = 0; p < partitions; p++) {
List<String> value = new ArrayList<String>();
for (int r = 0; r < replicas; r++) {
int n = nodes[(p + r) % nodes.length];
value.add("localhost_" + n);
}
idealState.getRecord().setListField(resourceName + "_" + p, value);
}
idealState.setReplicas(Integer.toString(replicas));
idealState.setStateModelDefRef("MasterSlave");
idealState.setRebalanceMode(RebalanceMode.SEMI_AUTO);
idealState.setNumPartitions(partitions);
idealStates.add(idealState);
// System.out.println(idealState);
accessor.setProperty(keyBuilder.idealStates(resourceName), idealState);
}
return idealStates;
}
use of org.apache.helix.manager.zk.ZKHelixDataAccessor in project helix by apache.
the class ZkUnitTestBase method setupLiveInstances.
protected void setupLiveInstances(String clusterName, int[] liveInstances) {
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
Builder keyBuilder = accessor.keyBuilder();
for (int i = 0; i < liveInstances.length; i++) {
String instance = "localhost_" + liveInstances[i];
LiveInstance liveInstance = new LiveInstance(instance);
liveInstance.setSessionId("session_" + liveInstances[i]);
liveInstance.setHelixVersion("0.0.0");
accessor.setProperty(keyBuilder.liveInstance(instance), liveInstance);
}
}
use of org.apache.helix.manager.zk.ZKHelixDataAccessor in project helix by apache.
the class TestHelper method verifyState.
/**
* @param stateMap
* : "ResourceName/partitionKey" -> setOf(instances)
* @param state
* : MASTER|SLAVE|ERROR...
*/
public static void verifyState(String clusterName, String zkAddr, Map<String, Set<String>> stateMap, String state) {
ZkClient zkClient = new ZkClient(zkAddr);
zkClient.setZkSerializer(new ZNRecordSerializer());
try {
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(zkClient));
Builder keyBuilder = accessor.keyBuilder();
for (String resGroupPartitionKey : stateMap.keySet()) {
Map<String, String> retMap = getResourceAndPartitionKey(resGroupPartitionKey);
String resGroup = retMap.get("RESOURCE");
String partitionKey = retMap.get("PARTITION");
ExternalView extView = accessor.getProperty(keyBuilder.externalView(resGroup));
for (String instance : stateMap.get(resGroupPartitionKey)) {
String actualState = extView.getStateMap(partitionKey).get(instance);
Assert.assertNotNull(actualState, "externalView doesn't contain state for " + resGroup + "/" + partitionKey + " on " + instance + " (expect " + state + ")");
Assert.assertEquals(actualState, state, "externalView for " + resGroup + "/" + partitionKey + " on " + instance + " is " + actualState + " (expect " + state + ")");
}
}
} finally {
zkClient.close();
}
}
Aggregations