Search in sources :

Example 1 with ZkClient

use of org.apache.helix.zookeeper.impl.client.ZkClient in project helix by apache.

the class ZkRoutingDataReader method getRawRoutingData.

/**
 * Returns a map form of metadata store routing data.
 * The map fields stand for metadata store realm address (key), and a corresponding list of ZK
 * path sharding keys (key).
 * @param endpoint
 * @return
 */
@Override
public Map<String, List<String>> getRawRoutingData(String endpoint) {
    ZkClient zkClient = new ZkClient.Builder().setZkServer(endpoint).setZkSerializer(new ZNRecordSerializer()).build();
    Map<String, List<String>> routingData = new HashMap<>();
    List<String> allRealmAddresses;
    try {
        allRealmAddresses = zkClient.getChildren(MetadataStoreRoutingConstants.ROUTING_DATA_PATH);
    } catch (ZkNoNodeException e) {
        throw new MultiZkException("Routing data directory ZNode " + MetadataStoreRoutingConstants.ROUTING_DATA_PATH + " does not exist. Routing ZooKeeper address: " + endpoint);
    }
    if (allRealmAddresses != null) {
        for (String realmAddress : allRealmAddresses) {
            ZNRecord record = zkClient.readData(MetadataStoreRoutingConstants.ROUTING_DATA_PATH + "/" + realmAddress, true);
            if (record != null) {
                List<String> shardingKeys = record.getListField(MetadataStoreRoutingConstants.ZNRECORD_LIST_FIELD_KEY);
                routingData.put(realmAddress, shardingKeys != null ? shardingKeys : Collections.emptyList());
            }
        }
    }
    zkClient.close();
    return routingData;
}
Also used : ZkClient(org.apache.helix.zookeeper.impl.client.ZkClient) ZkNoNodeException(org.apache.helix.zookeeper.zkclient.exception.ZkNoNodeException) HashMap(java.util.HashMap) List(java.util.List) MultiZkException(org.apache.helix.zookeeper.exception.MultiZkException) ZNRecord(org.apache.helix.zookeeper.datamodel.ZNRecord) ZNRecordSerializer(org.apache.helix.zookeeper.datamodel.serializer.ZNRecordSerializer)

Example 2 with ZkClient

use of org.apache.helix.zookeeper.impl.client.ZkClient in project helix by apache.

the class ZkTestBase method simulateSessionExpiry.

protected void simulateSessionExpiry(HelixZkClient client) throws IOException, InterruptedException, IOException {
    ZkClient zkClient = (ZkClient) client;
    IZkStateListener listener = new IZkStateListener() {

        @Override
        public void handleStateChanged(Watcher.Event.KeeperState state) throws Exception {
            LOG.info("In Old connection, state changed:" + state);
        }

        @Override
        public void handleNewSession(final String sessionId) throws Exception {
            LOG.info("In Old connection, new session: {}.", sessionId);
        }

        @Override
        public void handleSessionEstablishmentError(Throwable var1) throws Exception {
        }
    };
    zkClient.subscribeStateChanges(listener);
    ZkConnection connection = ((ZkConnection) zkClient.getConnection());
    ZooKeeper oldZookeeper = connection.getZookeeper();
    LOG.info("Old sessionId = " + oldZookeeper.getSessionId());
    Watcher watcher = new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            LOG.info("In New connection, process event:" + event);
        }
    };
    ZooKeeper newZookeeper = new ZooKeeper(connection.getServers(), oldZookeeper.getSessionTimeout(), watcher, oldZookeeper.getSessionId(), oldZookeeper.getSessionPasswd());
    LOG.info("New sessionId = " + newZookeeper.getSessionId());
    // Thread.sleep(3000);
    newZookeeper.close();
    Thread.sleep(10000);
    connection = (ZkConnection) zkClient.getConnection();
    oldZookeeper = connection.getZookeeper();
    LOG.info("After session expiry sessionId = " + oldZookeeper.getSessionId());
}
Also used : ZkClient(org.apache.helix.zookeeper.impl.client.ZkClient) HelixZkClient(org.apache.helix.zookeeper.api.client.HelixZkClient) WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) IZkStateListener(org.apache.helix.zookeeper.zkclient.IZkStateListener) Watcher(org.apache.zookeeper.Watcher) ZkConnection(org.apache.helix.zookeeper.zkclient.ZkConnection)

Example 3 with ZkClient

use of org.apache.helix.zookeeper.impl.client.ZkClient in project helix by apache.

the class TestZkBasis method testCloseZkClient.

@Test
public void testCloseZkClient() {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
    ZkClient client = new ZkClient(ZK_ADDR, HelixZkClient.DEFAULT_SESSION_TIMEOUT, HelixZkClient.DEFAULT_CONNECTION_TIMEOUT, new ZNRecordSerializer());
    String path = String.format("/%s", clusterName);
    client.createEphemeral(path);
    client.close();
    Assert.assertFalse(_gZkClient.exists(path), "Ephemeral node: " + path + " should be removed after ZkClient#close()");
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : ZkClient(org.apache.helix.zookeeper.impl.client.ZkClient) HelixZkClient(org.apache.helix.zookeeper.api.client.HelixZkClient) Date(java.util.Date) ZNRecordSerializer(org.apache.helix.manager.zk.ZNRecordSerializer) Test(org.testng.annotations.Test)

Example 4 with ZkClient

use of org.apache.helix.zookeeper.impl.client.ZkClient in project helix by apache.

the class TestZkBasis method testZkSessionExpiry.

@Test
public void testZkSessionExpiry() throws Exception {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
    ZkClient client = new ZkClient(ZK_ADDR, HelixZkClient.DEFAULT_SESSION_TIMEOUT, HelixZkClient.DEFAULT_CONNECTION_TIMEOUT, new ZNRecordSerializer());
    String path = String.format("/%s", clusterName);
    client.createEphemeral(path);
    String oldSessionId = ZkTestHelper.getSessionId(client);
    ZkTestHelper.expireSession(client);
    String newSessionId = ZkTestHelper.getSessionId(client);
    Assert.assertNotSame(newSessionId, oldSessionId);
    Assert.assertFalse(client.exists(path), "Ephemeral znode should be gone after session expiry");
    client.close();
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : ZkClient(org.apache.helix.zookeeper.impl.client.ZkClient) HelixZkClient(org.apache.helix.zookeeper.api.client.HelixZkClient) Date(java.util.Date) ZNRecordSerializer(org.apache.helix.manager.zk.ZNRecordSerializer) Test(org.testng.annotations.Test)

Example 5 with ZkClient

use of org.apache.helix.zookeeper.impl.client.ZkClient in project helix by apache.

the class TestPrefetchChangedData method testPrefetchChangedDataDisabled.

@Test
public void testPrefetchChangedDataDisabled() throws InterruptedException {
    String path = "/" + TestHelper.getTestMethodName();
    ZkClient zkClient = new ZkClient(ZkTestBase.ZK_ADDR);
    try {
        zkClient.createPersistent(path, "v1");
        CountDownLatch countDownLatch = new CountDownLatch(1);
        PreFetchZkDataListener dataListener = new PreFetchDisabledZkDataListener(countDownLatch);
        zkClient.subscribeDataChanges(path, dataListener);
        zkClient.writeData(path, "v2");
        Assert.assertTrue(countDownLatch.await(3L, TimeUnit.SECONDS));
        Assert.assertFalse(dataListener.isDataPreFetched());
    } finally {
        zkClient.unsubscribeAll();
        zkClient.delete(path);
        zkClient.close();
    }
}
Also used : ZkClient(org.apache.helix.zookeeper.impl.client.ZkClient) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Aggregations

ZkClient (org.apache.helix.zookeeper.impl.client.ZkClient)81 StringRepresentation (org.restlet.representation.StringRepresentation)29 ZNRecord (org.apache.helix.zookeeper.datamodel.ZNRecord)27 HelixException (org.apache.helix.HelixException)22 ClusterSetup (org.apache.helix.tools.ClusterSetup)21 Test (org.testng.annotations.Test)20 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)15 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)15 IOException (java.io.IOException)15 HelixDataAccessor (org.apache.helix.HelixDataAccessor)13 Builder (org.apache.helix.PropertyKey.Builder)11 ZNRecordSerializer (org.apache.helix.manager.zk.ZNRecordSerializer)11 PropertyKey (org.apache.helix.PropertyKey)10 LiveInstance (org.apache.helix.model.LiveInstance)9 Date (java.util.Date)8 HelixZkClient (org.apache.helix.zookeeper.api.client.HelixZkClient)8 JobConfig (org.apache.helix.task.JobConfig)7 CountDownLatch (java.util.concurrent.CountDownLatch)5 JobQueue (org.apache.helix.task.JobQueue)5 Workflow (org.apache.helix.task.Workflow)5