Search in sources :

Example 41 with KeeperException

use of org.apache.zookeeper.KeeperException in project hbase by apache.

the class HBaseAdmin method getMasterAddress.

private ServerName getMasterAddress() throws IOException {
    // TODO: Fix!  Reaching into internal implementation!!!!
    ConnectionImplementation connection = (ConnectionImplementation) this.connection;
    ZooKeeperKeepAliveConnection zkw = connection.getKeepAliveZooKeeperWatcher();
    try {
        return MasterAddressTracker.getMasterAddress(zkw);
    } catch (KeeperException e) {
        throw new IOException("Failed to get master server name from MasterAddressTracker", e);
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) TimeoutIOException(org.apache.hadoop.hbase.exceptions.TimeoutIOException) KeeperException(org.apache.zookeeper.KeeperException)

Example 42 with KeeperException

use of org.apache.zookeeper.KeeperException 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 43 with KeeperException

use of org.apache.zookeeper.KeeperException in project hbase by apache.

the class TestSplitLogManager method testLogFilesAreArchived.

@Test(timeout = 60000)
public void testLogFilesAreArchived() throws Exception {
    LOG.info("testLogFilesAreArchived");
    slm = new SplitLogManager(master, conf);
    FileSystem fs = TEST_UTIL.getTestFileSystem();
    Path dir = TEST_UTIL.getDataTestDirOnTestFS("testLogFilesAreArchived");
    conf.set(HConstants.HBASE_DIR, dir.toString());
    String serverName = ServerName.valueOf("foo", 1, 1).toString();
    Path logDirPath = new Path(new Path(dir, HConstants.HREGION_LOGDIR_NAME), serverName);
    fs.mkdirs(logDirPath);
    // create an empty log file
    String logFile = new Path(logDirPath, UUID.randomUUID().toString()).toString();
    fs.create(new Path(logDirPath, logFile)).close();
    // spin up a thread mocking split done.
    new Thread() {

        @Override
        public void run() {
            boolean done = false;
            while (!done) {
                for (Map.Entry<String, Task> entry : slm.getTasks().entrySet()) {
                    final ServerName worker1 = ServerName.valueOf("worker1,1,1");
                    SplitLogTask slt = new SplitLogTask.Done(worker1, RecoveryMode.LOG_SPLITTING);
                    boolean encounteredZKException = false;
                    try {
                        ZKUtil.setData(zkw, entry.getKey(), slt.toByteArray());
                    } catch (KeeperException e) {
                        LOG.warn(e);
                        encounteredZKException = true;
                    }
                    if (!encounteredZKException) {
                        done = true;
                    }
                }
            }
        }

        ;
    }.start();
    slm.splitLogDistributed(logDirPath);
    assertFalse(fs.exists(logDirPath));
}
Also used : Path(org.apache.hadoop.fs.Path) FileSystem(org.apache.hadoop.fs.FileSystem) ServerName(org.apache.hadoop.hbase.ServerName) SplitLogTask(org.apache.hadoop.hbase.SplitLogTask) KeeperException(org.apache.zookeeper.KeeperException) Test(org.junit.Test)

Example 44 with KeeperException

use of org.apache.zookeeper.KeeperException in project hive by apache.

the class ZooKeeperHiveLockManager method setContext.

/**
   * @param ctx  The lock manager context (containing the Hive configuration file)
   * Start the ZooKeeper client based on the zookeeper cluster specified in the conf.
   **/
@Override
public void setContext(HiveLockManagerCtx ctx) throws LockException {
    this.ctx = ctx;
    HiveConf conf = ctx.getConf();
    sleepTime = conf.getTimeVar(HiveConf.ConfVars.HIVE_LOCK_SLEEP_BETWEEN_RETRIES, TimeUnit.MILLISECONDS);
    numRetriesForLock = conf.getIntVar(HiveConf.ConfVars.HIVE_LOCK_NUMRETRIES);
    numRetriesForUnLock = conf.getIntVar(HiveConf.ConfVars.HIVE_UNLOCK_NUMRETRIES);
    try {
        curatorFramework = CuratorFrameworkSingleton.getInstance(conf);
        parent = conf.getVar(HiveConf.ConfVars.HIVE_ZOOKEEPER_NAMESPACE);
        try {
            curatorFramework.create().withMode(CreateMode.PERSISTENT).forPath("/" + parent, new byte[0]);
        } catch (Exception e) {
            // ignore if the parent already exists
            if (!(e instanceof KeeperException) || ((KeeperException) e).code() != KeeperException.Code.NODEEXISTS) {
                LOG.warn("Unexpected ZK exception when creating parent node /" + parent, e);
            }
        }
    } catch (Exception e) {
        LOG.error("Failed to create curatorFramework object: ", e);
        throw new LockException(ErrorMsg.ZOOKEEPER_CLIENT_COULD_NOT_BE_INITIALIZED.getMsg());
    }
}
Also used : HiveConf(org.apache.hadoop.hive.conf.HiveConf) KeeperException(org.apache.zookeeper.KeeperException) KeeperException(org.apache.zookeeper.KeeperException)

Example 45 with KeeperException

use of org.apache.zookeeper.KeeperException in project hive by apache.

the class ZooKeeperHiveLockManager method unlockPrimitive.

/* Remove the lock specified */
@VisibleForTesting
static void unlockPrimitive(HiveLock hiveLock, String parent, CuratorFramework curatorFramework) throws LockException {
    ZooKeeperHiveLock zLock = (ZooKeeperHiveLock) hiveLock;
    HiveLockMode lMode = hiveLock.getHiveLockMode();
    HiveLockObject obj = zLock.getHiveLockObject();
    String name = getLastObjectName(parent, obj);
    try {
        //catch InterruptedException to make sure locks can be released when the query is cancelled.
        try {
            curatorFramework.delete().forPath(zLock.getPath());
        } catch (InterruptedException ie) {
            curatorFramework.delete().forPath(zLock.getPath());
        }
        // Delete the parent node if all the children have been deleted
        List<String> children = null;
        try {
            children = curatorFramework.getChildren().forPath(name);
        } catch (InterruptedException ie) {
            children = curatorFramework.getChildren().forPath(name);
        }
        if (children == null || children.isEmpty()) {
            try {
                curatorFramework.delete().forPath(name);
            } catch (InterruptedException ie) {
                curatorFramework.delete().forPath(name);
            }
        }
        Metrics metrics = MetricsFactory.getInstance();
        if (metrics != null) {
            try {
                switch(lMode) {
                    case EXCLUSIVE:
                        metrics.decrementCounter(MetricsConstant.ZOOKEEPER_HIVE_EXCLUSIVELOCKS);
                        break;
                    case SEMI_SHARED:
                        metrics.decrementCounter(MetricsConstant.ZOOKEEPER_HIVE_SEMISHAREDLOCKS);
                        break;
                    default:
                        metrics.decrementCounter(MetricsConstant.ZOOKEEPER_HIVE_SHAREDLOCKS);
                        break;
                }
            } catch (Exception e) {
                LOG.warn("Error Reporting hive client zookeeper unlock operation to Metrics system", e);
            }
        }
    } catch (KeeperException.NoNodeException nne) {
        //can happen in retrying deleting the zLock after exceptions like InterruptedException
        //or in a race condition where parent has already been deleted by other process when it
        //is to be deleted. Both cases should not raise error
        LOG.debug("Node " + zLock.getPath() + " or its parent has already been deleted.");
    } catch (KeeperException.NotEmptyException nee) {
        //can happen in a race condition where another process adds a zLock under this parent
        //just before it is about to be deleted. It should not be a problem since this parent
        //can eventually be deleted by the process which hold its last child zLock
        LOG.debug("Node " + name + " to be deleted is not empty.");
    } catch (Exception e) {
        //exceptions including InterruptException and other KeeperException
        LOG.error("Failed to release ZooKeeper lock: ", e);
        throw new LockException(e);
    }
}
Also used : Metrics(org.apache.hadoop.hive.common.metrics.common.Metrics) KeeperException(org.apache.zookeeper.KeeperException) KeeperException(org.apache.zookeeper.KeeperException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

KeeperException (org.apache.zookeeper.KeeperException)345 IOException (java.io.IOException)114 Stat (org.apache.zookeeper.data.Stat)79 ZooKeeper (org.apache.zookeeper.ZooKeeper)54 Test (org.junit.Test)37 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)36 ArrayList (java.util.ArrayList)30 SolrException (org.apache.solr.common.SolrException)30 HeliosRuntimeException (com.spotify.helios.common.HeliosRuntimeException)24 HashMap (java.util.HashMap)21 WatchedEvent (org.apache.zookeeper.WatchedEvent)20 Watcher (org.apache.zookeeper.Watcher)20 InterruptedIOException (java.io.InterruptedIOException)19 Map (java.util.Map)19 ZooKeeperClient (com.spotify.helios.servicescommon.coordination.ZooKeeperClient)17 ServerName (org.apache.hadoop.hbase.ServerName)15 ACL (org.apache.zookeeper.data.ACL)15 List (java.util.List)14 CountDownLatch (java.util.concurrent.CountDownLatch)14 RetryCounter (org.apache.hadoop.hbase.util.RetryCounter)13