Search in sources :

Example 56 with ZKHelixDataAccessor

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;
}
Also used : ExternalView(org.apache.helix.model.ExternalView) ZkBaseDataAccessor(org.apache.helix.manager.zk.ZkBaseDataAccessor) Builder(org.apache.helix.PropertyKey.Builder) ZNRecord(org.apache.helix.ZNRecord) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor)

Example 57 with ZKHelixDataAccessor

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());
        }
    }
}
Also used : Builder(org.apache.helix.PropertyKey.Builder) IdealState(org.apache.helix.model.IdealState) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor)

Example 58 with ZKHelixDataAccessor

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;
}
Also used : Builder(org.apache.helix.PropertyKey.Builder) ArrayList(java.util.ArrayList) IdealState(org.apache.helix.model.IdealState) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor)

Example 59 with ZKHelixDataAccessor

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);
    }
}
Also used : LiveInstance(org.apache.helix.model.LiveInstance) Builder(org.apache.helix.PropertyKey.Builder) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor)

Example 60 with ZKHelixDataAccessor

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();
    }
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) ExternalView(org.apache.helix.model.ExternalView) Builder(org.apache.helix.PropertyKey.Builder) ZNRecordSerializer(org.apache.helix.manager.zk.ZNRecordSerializer) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor)

Aggregations

ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)104 ZNRecord (org.apache.helix.ZNRecord)78 Date (java.util.Date)66 Test (org.testng.annotations.Test)66 Builder (org.apache.helix.PropertyKey.Builder)47 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)41 HelixDataAccessor (org.apache.helix.HelixDataAccessor)38 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)37 IdealState (org.apache.helix.model.IdealState)33 ZkBaseDataAccessor (org.apache.helix.manager.zk.ZkBaseDataAccessor)30 PropertyKey (org.apache.helix.PropertyKey)29 ExternalView (org.apache.helix.model.ExternalView)21 BestPossAndExtViewZkVerifier (org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier)21 LiveInstance (org.apache.helix.model.LiveInstance)20 ClusterStateVerifier (org.apache.helix.tools.ClusterStateVerifier)16 HelixManager (org.apache.helix.HelixManager)15 Message (org.apache.helix.model.Message)12 ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)11 HashMap (java.util.HashMap)10 ZkClient (org.apache.helix.manager.zk.ZkClient)10