Search in sources :

Example 1 with PersistentEphemeralNode

use of org.apache.curator.framework.recipes.nodes.PersistentEphemeralNode in project hive by apache.

the class HiveServer2 method addServerInstanceToZooKeeper.

/**
   * Adds a server instance to ZooKeeper as a znode.
   *
   * @param hiveConf
   * @throws Exception
   */
private void addServerInstanceToZooKeeper(HiveConf hiveConf) throws Exception {
    String zooKeeperEnsemble = ZooKeeperHiveHelper.getQuorumServers(hiveConf);
    String rootNamespace = hiveConf.getVar(HiveConf.ConfVars.HIVE_SERVER2_ZOOKEEPER_NAMESPACE);
    String instanceURI = getServerInstanceURI();
    setUpZooKeeperAuth(hiveConf);
    int sessionTimeout = (int) hiveConf.getTimeVar(HiveConf.ConfVars.HIVE_ZOOKEEPER_SESSION_TIMEOUT, TimeUnit.MILLISECONDS);
    int baseSleepTime = (int) hiveConf.getTimeVar(HiveConf.ConfVars.HIVE_ZOOKEEPER_CONNECTION_BASESLEEPTIME, TimeUnit.MILLISECONDS);
    int maxRetries = hiveConf.getIntVar(HiveConf.ConfVars.HIVE_ZOOKEEPER_CONNECTION_MAX_RETRIES);
    // Create a CuratorFramework instance to be used as the ZooKeeper client
    // Use the zooKeeperAclProvider to create appropriate ACLs
    zooKeeperClient = CuratorFrameworkFactory.builder().connectString(zooKeeperEnsemble).sessionTimeoutMs(sessionTimeout).aclProvider(zooKeeperAclProvider).retryPolicy(new ExponentialBackoffRetry(baseSleepTime, maxRetries)).build();
    zooKeeperClient.start();
    // Create the parent znodes recursively; ignore if the parent already exists.
    try {
        zooKeeperClient.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(ZooKeeperHiveHelper.ZOOKEEPER_PATH_SEPARATOR + rootNamespace);
        LOG.info("Created the root name space: " + rootNamespace + " on ZooKeeper for HiveServer2");
    } catch (KeeperException e) {
        if (e.code() != KeeperException.Code.NODEEXISTS) {
            LOG.error("Unable to create HiveServer2 namespace: " + rootNamespace + " on ZooKeeper", e);
            throw e;
        }
    }
    // Znode name: serverUri=host:port;version=versionInfo;sequence=sequenceNumber
    try {
        String pathPrefix = ZooKeeperHiveHelper.ZOOKEEPER_PATH_SEPARATOR + rootNamespace + ZooKeeperHiveHelper.ZOOKEEPER_PATH_SEPARATOR + "serverUri=" + instanceURI + ";" + "version=" + HiveVersionInfo.getVersion() + ";" + "sequence=";
        String znodeData = "";
        if (hiveConf.getBoolVar(HiveConf.ConfVars.HIVE_SERVER2_ZOOKEEPER_PUBLISH_CONFIGS)) {
            // HiveServer2 configs that this instance will publish to ZooKeeper,
            // so that the clients can read these and configure themselves properly.
            Map<String, String> confsToPublish = new HashMap<String, String>();
            addConfsToPublish(hiveConf, confsToPublish);
            // Publish configs for this instance as the data on the node
            znodeData = Joiner.on(';').withKeyValueSeparator("=").join(confsToPublish);
        } else {
            znodeData = instanceURI;
        }
        byte[] znodeDataUTF8 = znodeData.getBytes(Charset.forName("UTF-8"));
        znode = new PersistentEphemeralNode(zooKeeperClient, PersistentEphemeralNode.Mode.EPHEMERAL_SEQUENTIAL, pathPrefix, znodeDataUTF8);
        znode.start();
        // We'll wait for 120s for node creation
        long znodeCreationTimeout = 120;
        if (!znode.waitForInitialCreate(znodeCreationTimeout, TimeUnit.SECONDS)) {
            throw new Exception("Max znode creation wait time: " + znodeCreationTimeout + "s exhausted");
        }
        setDeregisteredWithZooKeeper(false);
        znodePath = znode.getActualPath();
        // Set a watch on the znode
        if (zooKeeperClient.checkExists().usingWatcher(new DeRegisterWatcher()).forPath(znodePath) == null) {
            // No node exists, throw exception
            throw new Exception("Unable to create znode for this HiveServer2 instance on ZooKeeper.");
        }
        LOG.info("Created a znode on ZooKeeper for HiveServer2 uri: " + instanceURI);
    } catch (Exception e) {
        LOG.error("Unable to create a znode for this server instance", e);
        if (znode != null) {
            znode.close();
        }
        throw (e);
    }
}
Also used : HashMap(java.util.HashMap) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) PersistentEphemeralNode(org.apache.curator.framework.recipes.nodes.PersistentEphemeralNode) KeeperException(org.apache.zookeeper.KeeperException) ServiceException(org.apache.hive.service.ServiceException) ParseException(org.apache.commons.cli.ParseException) HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) LogInitializationException(org.apache.hadoop.hive.common.LogUtils.LogInitializationException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException)

Example 2 with PersistentEphemeralNode

use of org.apache.curator.framework.recipes.nodes.PersistentEphemeralNode in project hive by apache.

the class HiveServer2 method addServerInstanceToZooKeeper.

/**
 * Adds a server instance to ZooKeeper as a znode.
 *
 * @param hiveConf
 * @throws Exception
 */
private void addServerInstanceToZooKeeper(HiveConf hiveConf, Map<String, String> confsToPublish) throws Exception {
    String zooKeeperEnsemble = ZooKeeperHiveHelper.getQuorumServers(hiveConf);
    String rootNamespace = hiveConf.getVar(HiveConf.ConfVars.HIVE_SERVER2_ZOOKEEPER_NAMESPACE);
    String instanceURI = getServerInstanceURI();
    setUpZooKeeperAuth(hiveConf);
    int sessionTimeout = (int) hiveConf.getTimeVar(HiveConf.ConfVars.HIVE_ZOOKEEPER_SESSION_TIMEOUT, TimeUnit.MILLISECONDS);
    int baseSleepTime = (int) hiveConf.getTimeVar(HiveConf.ConfVars.HIVE_ZOOKEEPER_CONNECTION_BASESLEEPTIME, TimeUnit.MILLISECONDS);
    int maxRetries = hiveConf.getIntVar(HiveConf.ConfVars.HIVE_ZOOKEEPER_CONNECTION_MAX_RETRIES);
    // Create a CuratorFramework instance to be used as the ZooKeeper client
    // Use the zooKeeperAclProvider to create appropriate ACLs
    zooKeeperClient = CuratorFrameworkFactory.builder().connectString(zooKeeperEnsemble).sessionTimeoutMs(sessionTimeout).aclProvider(zooKeeperAclProvider).retryPolicy(new ExponentialBackoffRetry(baseSleepTime, maxRetries)).build();
    zooKeeperClient.start();
    // Create the parent znodes recursively; ignore if the parent already exists.
    try {
        zooKeeperClient.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(ZooKeeperHiveHelper.ZOOKEEPER_PATH_SEPARATOR + rootNamespace);
        LOG.info("Created the root name space: " + rootNamespace + " on ZooKeeper for HiveServer2");
    } catch (KeeperException e) {
        if (e.code() != KeeperException.Code.NODEEXISTS) {
            LOG.error("Unable to create HiveServer2 namespace: " + rootNamespace + " on ZooKeeper", e);
            throw e;
        }
    }
    // Znode name: serverUri=host:port;version=versionInfo;sequence=sequenceNumber
    try {
        String pathPrefix = ZooKeeperHiveHelper.ZOOKEEPER_PATH_SEPARATOR + rootNamespace + ZooKeeperHiveHelper.ZOOKEEPER_PATH_SEPARATOR + "serverUri=" + instanceURI + ";" + "version=" + HiveVersionInfo.getVersion() + ";" + "sequence=";
        String znodeData = "";
        if (hiveConf.getBoolVar(HiveConf.ConfVars.HIVE_SERVER2_ZOOKEEPER_PUBLISH_CONFIGS)) {
            // HiveServer2 configs that this instance will publish to ZooKeeper,
            // so that the clients can read these and configure themselves properly.
            addConfsToPublish(hiveConf, confsToPublish, instanceURI);
            // Publish configs for this instance as the data on the node
            znodeData = Joiner.on(';').withKeyValueSeparator("=").join(confsToPublish);
        } else {
            znodeData = instanceURI;
        }
        byte[] znodeDataUTF8 = znodeData.getBytes(Charset.forName("UTF-8"));
        znode = new PersistentEphemeralNode(zooKeeperClient, PersistentEphemeralNode.Mode.EPHEMERAL_SEQUENTIAL, pathPrefix, znodeDataUTF8);
        znode.start();
        // We'll wait for 120s for node creation
        long znodeCreationTimeout = 120;
        if (!znode.waitForInitialCreate(znodeCreationTimeout, TimeUnit.SECONDS)) {
            throw new Exception("Max znode creation wait time: " + znodeCreationTimeout + "s exhausted");
        }
        setDeregisteredWithZooKeeper(false);
        final String znodePath = znode.getActualPath();
        // Set a watch on the znode
        if (zooKeeperClient.checkExists().usingWatcher(new DeRegisterWatcher()).forPath(znodePath) == null) {
            // No node exists, throw exception
            throw new Exception("Unable to create znode for this HiveServer2 instance on ZooKeeper.");
        }
        LOG.info("Created a znode on ZooKeeper for HiveServer2 uri: " + instanceURI);
    } catch (Exception e) {
        LOG.error("Unable to create a znode for this server instance", e);
        if (znode != null) {
            znode.close();
        }
        throw (e);
    }
}
Also used : ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) PersistentEphemeralNode(org.apache.curator.framework.recipes.nodes.PersistentEphemeralNode) KeeperException(org.apache.zookeeper.KeeperException) ServiceException(org.apache.hive.service.ServiceException) LogInitializationException(org.apache.hadoop.hive.common.LogUtils.LogInitializationException) IOException(java.io.IOException) ParseException(org.apache.commons.cli.ParseException) HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) KeeperException(org.apache.zookeeper.KeeperException)

Example 3 with PersistentEphemeralNode

use of org.apache.curator.framework.recipes.nodes.PersistentEphemeralNode in project xian by happyyangyuan.

the class TestBackgroundStates method testListenersReconnectedIsOK.

@Test
public void testListenersReconnectedIsOK() throws Exception {
    server.close();
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    PersistentEphemeralNode node = null;
    try {
        client.start();
        node = new PersistentEphemeralNode(client, PersistentEphemeralNode.Mode.EPHEMERAL, "/abc/node", "hello".getBytes());
        node.start();
        final CountDownLatch connectedLatch = new CountDownLatch(1);
        final CountDownLatch reconnectedLatch = new CountDownLatch(1);
        final AtomicReference<ConnectionState> lastState = new AtomicReference<ConnectionState>();
        ConnectionStateListener listener = new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                lastState.set(newState);
                if (newState == ConnectionState.CONNECTED) {
                    connectedLatch.countDown();
                }
                if (newState == ConnectionState.RECONNECTED) {
                    reconnectedLatch.countDown();
                }
            }
        };
        client.getConnectionStateListenable().addListener(listener);
        timing.sleepABit();
        server = new TestingServer(server.getPort());
        Assert.assertTrue(timing.awaitLatch(connectedLatch));
        timing.sleepABit();
        Assert.assertTrue(node.waitForInitialCreate(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS));
        server.restart();
        timing.sleepABit();
        Assert.assertTrue(timing.awaitLatch(reconnectedLatch));
        timing.sleepABit();
        Assert.assertEquals(lastState.get(), ConnectionState.RECONNECTED);
    } finally {
        CloseableUtils.closeQuietly(client);
        CloseableUtils.closeQuietly(node);
    }
}
Also used : TestingServer(org.apache.curator.test.TestingServer) CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) PersistentEphemeralNode(org.apache.curator.framework.recipes.nodes.PersistentEphemeralNode) AtomicReference(java.util.concurrent.atomic.AtomicReference) Timing(org.apache.curator.test.Timing) ConnectionState(org.apache.curator.framework.state.ConnectionState) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) Test(org.testng.annotations.Test)

Example 4 with PersistentEphemeralNode

use of org.apache.curator.framework.recipes.nodes.PersistentEphemeralNode in project helios by spotify.

the class AgentZooKeeperRegistrarTest method setUp.

@Before
public void setUp() throws Exception {
    registrar.startUp();
    final PersistentEphemeralNode ephemeralNode = mock(PersistentEphemeralNode.class);
    when(client.persistentEphemeralNode(upPath, PersistentEphemeralNode.Mode.EPHEMERAL, new byte[] {})).thenReturn(ephemeralNode);
    // default behavior for checking ID of the host registered in zookeeper - it is this host
    when(client.exists(idPath)).thenReturn(new Stat());
    when(client.getData(idPath)).thenReturn(hostId.getBytes());
}
Also used : Stat(org.apache.zookeeper.data.Stat) PersistentEphemeralNode(org.apache.curator.framework.recipes.nodes.PersistentEphemeralNode) Before(org.junit.Before)

Example 5 with PersistentEphemeralNode

use of org.apache.curator.framework.recipes.nodes.PersistentEphemeralNode in project helios by spotify.

the class DefaultZooKeeperClient method persistentEphemeralNode.

@Override
public PersistentEphemeralNode persistentEphemeralNode(final String path, final PersistentEphemeralNode.Mode mode, final byte[] data) {
    assertClusterIdFlagTrue();
    final PersistentEphemeralNode node = new PersistentEphemeralNode(client, mode, path, data);
    // all parent paths if they don't exist but applies the ACL for its own path to its parents
    try {
        final Field field = node.getClass().getDeclaredField("createMethod");
        field.setAccessible(true);
        field.set(node, client.create());
    } catch (NoSuchFieldException | IllegalAccessException e) {
        throw new RuntimeException(e);
    }
    return node;
}
Also used : Field(java.lang.reflect.Field) PersistentEphemeralNode(org.apache.curator.framework.recipes.nodes.PersistentEphemeralNode)

Aggregations

PersistentEphemeralNode (org.apache.curator.framework.recipes.nodes.PersistentEphemeralNode)5 IOException (java.io.IOException)2 ParseException (org.apache.commons.cli.ParseException)2 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)2 LogInitializationException (org.apache.hadoop.hive.common.LogUtils.LogInitializationException)2 HiveException (org.apache.hadoop.hive.ql.metadata.HiveException)2 ServiceException (org.apache.hive.service.ServiceException)2 KeeperException (org.apache.zookeeper.KeeperException)2 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1 ConnectionState (org.apache.curator.framework.state.ConnectionState)1 ConnectionStateListener (org.apache.curator.framework.state.ConnectionStateListener)1 RetryOneTime (org.apache.curator.retry.RetryOneTime)1 TestingServer (org.apache.curator.test.TestingServer)1 Timing (org.apache.curator.test.Timing)1 Stat (org.apache.zookeeper.data.Stat)1 Before (org.junit.Before)1