Search in sources :

Example 61 with ZkClient

use of org.apache.helix.manager.zk.ZkClient in project helix by apache.

the class ZkUnitTestBase method beforeSuite.

@BeforeSuite(alwaysRun = true)
public void beforeSuite() throws Exception {
    // Due to ZOOKEEPER-2693 fix, we need to specify whitelist for execute zk commends
    System.setProperty("zookeeper.4lw.commands.whitelist", "*");
    _zkServer = TestHelper.startZkServer(ZK_ADDR);
    AssertJUnit.assertTrue(_zkServer != null);
    ZKClientPool.reset();
    // System.out.println("Number of open zkClient before ZkUnitTests: "
    // + ZkClient.getNumberOfConnections());
    _gZkClient = new ZkClient(ZK_ADDR);
    _gZkClient.setZkSerializer(new ZNRecordSerializer());
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) ZNRecordSerializer(org.apache.helix.manager.zk.ZNRecordSerializer) BeforeSuite(org.testng.annotations.BeforeSuite)

Example 62 with ZkClient

use of org.apache.helix.manager.zk.ZkClient in project helix by apache.

the class TestExecutor method executeTestHelper.

private static Map<TestCommand, Boolean> executeTestHelper(List<TestCommand> commandList, String zkAddr, CountDownLatch countDown) {
    final Map<TestCommand, Boolean> testResults = new ConcurrentHashMap<TestCommand, Boolean>();
    ZkClient zkClient = null;
    zkClient = new ZkClient(zkAddr, ZkClient.DEFAULT_CONNECTION_TIMEOUT);
    zkClient.setZkSerializer(new ZNRecordSerializer());
    // sort on trigger's start time, stable sort
    Collections.sort(commandList, new Comparator<TestCommand>() {

        @Override
        public int compare(TestCommand o1, TestCommand o2) {
            return (int) (o1._trigger._startTime - o2._trigger._startTime);
        }
    });
    for (TestCommand command : commandList) {
        testResults.put(command, new Boolean(false));
        TestTrigger trigger = command._trigger;
        command._startTimestamp = System.currentTimeMillis() + trigger._startTime;
        new Thread(new ExecuteCommand(command._startTimestamp, command, countDown, zkClient, testResults)).start();
    }
    return testResults;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ZNRecordSerializer(org.apache.helix.manager.zk.ZNRecordSerializer)

Example 63 with ZkClient

use of org.apache.helix.manager.zk.ZkClient in project helix by apache.

the class ZkCopy method main.

public static void main(String[] args) throws Exception {
    CommandLineParser cliParser = new GnuParser();
    Options cliOptions = constructCmdLineOpt();
    CommandLine cmd = null;
    try {
        cmd = cliParser.parse(cliOptions, args);
    } catch (ParseException pe) {
        System.err.println("CommandLineClient: failed to parse command-line options: " + pe.toString());
        printUsage(cliOptions);
        System.exit(1);
    }
    URI srcUri = new URI(cmd.getOptionValue(src));
    URI dstUri = new URI(cmd.getOptionValue(dst));
    ZkCopyScheme srcScheme = ZkCopyScheme.valueOf(srcUri.getScheme());
    ZkCopyScheme dstScheme = ZkCopyScheme.valueOf(dstUri.getScheme());
    if (srcScheme == ZkCopyScheme.zk && dstScheme == ZkCopyScheme.zk) {
        String srcZkAddr = srcUri.getAuthority();
        String dstZkAddr = dstUri.getAuthority();
        ZkClient srcClient = null;
        ZkClient dstClient = null;
        try {
            if (srcZkAddr.equals(dstZkAddr)) {
                srcClient = dstClient = new ZkClient(srcZkAddr, ZkClient.DEFAULT_SESSION_TIMEOUT, ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ByteArraySerializer());
            } else {
                srcClient = new ZkClient(srcZkAddr, ZkClient.DEFAULT_SESSION_TIMEOUT, ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ByteArraySerializer());
                dstClient = new ZkClient(dstZkAddr, ZkClient.DEFAULT_SESSION_TIMEOUT, ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ByteArraySerializer());
            }
            String srcPath = srcUri.getPath();
            String dstPath = dstUri.getPath();
            zkCopy(srcClient, srcPath, dstClient, dstPath);
        } finally {
            if (srcClient != null) {
                srcClient.close();
            }
            if (dstClient != null) {
                dstClient.close();
            }
        }
    } else {
        System.err.println("Unsupported scheme. srcScheme: " + srcScheme + ", dstScheme: " + dstScheme);
        System.exit(1);
    }
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) GnuParser(org.apache.commons.cli.GnuParser) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) URI(java.net.URI) ByteArraySerializer(org.apache.helix.manager.zk.ByteArraySerializer)

Example 64 with ZkClient

use of org.apache.helix.manager.zk.ZkClient 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)

Example 65 with ZkClient

use of org.apache.helix.manager.zk.ZkClient in project helix by apache.

the class TestHelper method verifyEmptyCurStateAndExtView.

public static boolean verifyEmptyCurStateAndExtView(String clusterName, String resourceName, Set<String> instanceNames, String zkAddr) {
    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 instanceName : instanceNames) {
            List<String> sessionIds = accessor.getChildNames(keyBuilder.sessions(instanceName));
            for (String sessionId : sessionIds) {
                CurrentState curState = accessor.getProperty(keyBuilder.currentState(instanceName, sessionId, resourceName));
                if (curState != null && curState.getRecord().getMapFields().size() != 0) {
                    return false;
                }
            }
            ExternalView extView = accessor.getProperty(keyBuilder.externalView(resourceName));
            if (extView != null && extView.getRecord().getMapFields().size() != 0) {
                return false;
            }
        }
        return true;
    } finally {
        zkClient.close();
    }
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) ExternalView(org.apache.helix.model.ExternalView) Builder(org.apache.helix.PropertyKey.Builder) CurrentState(org.apache.helix.model.CurrentState) ZNRecordSerializer(org.apache.helix.manager.zk.ZNRecordSerializer) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor)

Aggregations

ZkClient (org.apache.helix.manager.zk.ZkClient)109 ZNRecordSerializer (org.apache.helix.manager.zk.ZNRecordSerializer)39 ZNRecord (org.apache.helix.ZNRecord)29 StringRepresentation (org.restlet.representation.StringRepresentation)29 ClusterSetup (org.apache.helix.tools.ClusterSetup)26 HelixException (org.apache.helix.HelixException)23 Builder (org.apache.helix.PropertyKey.Builder)18 IOException (java.io.IOException)17 HelixDataAccessor (org.apache.helix.HelixDataAccessor)15 JsonGenerationException (org.codehaus.jackson.JsonGenerationException)15 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)15 ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)12 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)10 Test (org.testng.annotations.Test)10 Date (java.util.Date)9 BeforeClass (org.testng.annotations.BeforeClass)9 PropertyKey (org.apache.helix.PropertyKey)8 IdealState (org.apache.helix.model.IdealState)8 InstanceConfig (org.apache.helix.model.InstanceConfig)8 StateModelDefinition (org.apache.helix.model.StateModelDefinition)7