/** * Set region as OFFLINED up in zookeeper asynchronously. * @param state * @return True if we succeeded, false otherwise (State was incorrect or failed * updating zk). */ private boolean asyncSetOfflineInZooKeeper(final RegionState state, final AsyncCallback.StringCallback cb, final ServerName destination) { if (!state.isClosed() && !state.isOffline()) { this.server.abort("Unexpected state trying to OFFLINE; " + state, new IllegalStateException()); return false; } regionStates.updateRegionState(state.getRegion(), State.OFFLINE); try { ZKAssign.asyncCreateNodeOffline(watcher, state.getRegion(), destination, cb, state); } catch (KeeperException e) { if (e instanceof NodeExistsException) { LOG.warn("Node for " + state.getRegion() + " already exists"); } else { server.abort("Unexpected ZK exception creating/setting node OFFLINE", e); } return false; } return true; }
private void OpenRegion(Server server, RegionServerServices rss, HTableDescriptor htd, HRegionInfo hri, OpenRegionCoordination coordination) throws IOException, NodeExistsException, KeeperException, DeserializationException { // Create it OFFLINE node, which is what Master set before sending OPEN RPC ZKAssign.createNodeOffline(server.getZooKeeper(), hri, server.getServerName()); OpenRegionCoordination.OpenRegionDetails ord = coordination.getDetailsForNonCoordinatedOpening(); OpenRegionHandler openHandler = new OpenRegionHandler(server, rss, hri, htd, -1, coordination, ord); rss.getRegionsInTransitionInRS().put(hri.getEncodedNameAsBytes(), Boolean.TRUE); openHandler.process(); // This parse is not used? RegionTransition.parseFrom(ZKAssign.getData(server.getZooKeeper(), hri.getEncodedName())); // delete the node, which is what Master do after the region is opened ZKAssign.deleteNode(server.getZooKeeper(), hri.getEncodedName(), EventType.RS_ZK_REGION_OPENED, server.getServerName()); }
public static String createIfAbsent(ZooKeeper zk, String path, byte[] data, CreateMode createMode, boolean gc) throws KeeperException, InterruptedException { String pathCreated = null; try { pathCreated = zk.create(path, data, Ids.OPEN_ACL_UNSAFE, createMode); } catch (NodeExistsException e) { // OK LOG.debug("Create skipped for existing znode: path={}", path); } // reset if what exists is the ephemeral garbage. if (gc && (pathCreated == null) && CreateMode.EPHEMERAL.equals(createMode)) { Stat stat = zk.exists(path, false); if (stat != null && zk.getSessionId() != stat.getEphemeralOwner()) { deleteIfExists(zk, path, -1); pathCreated = zk.create(path, data, Ids.OPEN_ACL_UNSAFE, createMode); } } return pathCreated; }
private void createEphemeralNode(String fullPath, byte[] data) throws KeeperException, InterruptedException { try { zk.create(fullPath, data, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); } catch (NodeExistsException nee) { // this sometimes happens after a ZooKeeper node dies and the ephemeral node // that belonged to the old session was not yet deleted. We need to make our // session the owner of the node so it won't get deleted automatically - // we do this by deleting and recreating it ourselves. LOG.info("node for endpoint already exists, recreating: {}", fullPath); try { zk.delete(fullPath, -1); } catch (NoNodeException nne) { // it's a race condition, but as long as it got deleted - it's ok } zk.create(fullPath, data, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); } }
/** * Set region as OFFLINED up in zookeeper asynchronously. * @param state * @return True if we succeeded, false otherwise (State was incorrect or failed * updating zk). */ boolean asyncSetOfflineInZooKeeper(final RegionState state, final AsyncCallback.StringCallback cb, final Object ctx) { if (!state.isClosed() && !state.isOffline()) { new RuntimeException("Unexpected state trying to OFFLINE; " + state); this.master.abort("Unexpected state trying to OFFLINE; " + state, new IllegalStateException()); return false; } state.update(RegionState.State.OFFLINE); try { ZKAssign.asyncCreateNodeOffline(master.getZooKeeper(), state.getRegion(), this.master.getServerName(), cb, ctx); } catch (KeeperException e) { // TODO: this error handling will never execute, as the callback is async. if (e instanceof NodeExistsException) { LOG.warn("Node for " + state.getRegion() + " already exists"); } else { master.abort("Unexpected ZK exception creating/setting node OFFLINE", e); } return false; } return true; }
private void OpenRegion(Server server, RegionServerServices rss, HTableDescriptor htd, HRegionInfo hri) throws IOException, NodeExistsException, KeeperException { // Create it OFFLINE node, which is what Master set before sending OPEN RPC ZKAssign.createNodeOffline(server.getZooKeeper(), hri, server.getServerName()); int version = ZKAssign.transitionNodeOpening(server.getZooKeeper(), hri, server.getServerName()); OpenRegionHandler openHandler = new OpenRegionHandler(server, rss, hri, htd, version); openHandler.process(); RegionTransitionData data = ZKAssign.getData(server.getZooKeeper(), hri.getEncodedName()); // delete the node, which is what Master do after the region is opened ZKAssign.deleteNode(server.getZooKeeper(), hri.getEncodedName(), EventType.RS_ZK_REGION_OPENED); }
private void OpenRegion(Server server, RegionServerServices rss, HTableDescriptor htd, HRegionInfo hri, OpenRegionCoordination coordination) throws IOException, NodeExistsException, KeeperException, DeserializationException { // Create it OFFLINE node, which is what Master set before sending OPEN RPC ZKAssign.createNodeOffline(server.getZooKeeper(), hri, server.getServerName()); OpenRegionCoordination.OpenRegionDetails ord = coordination.getDetailsForNonCoordinatedOpening(); OpenRegionHandler openHandler = new OpenRegionHandler(server, rss, hri, htd, coordination, ord); rss.getRegionsInTransitionInRS().put(hri.getEncodedNameAsBytes(), Boolean.TRUE); openHandler.process(); // This parse is not used? RegionTransition.parseFrom(ZKAssign.getData(server.getZooKeeper(), hri.getEncodedName())); // delete the node, which is what Master do after the region is opened ZKAssign.deleteNode(server.getZooKeeper(), hri.getEncodedName(), EventType.RS_ZK_REGION_OPENED, server.getServerName()); }
protected void createOrUpdatePath(final String pPath, final byte[] pData) { //CuratorFramework namespacedClient = client.usingNamespace(NAMESPACE); String path = pPath; if (!StringUtils.startsWith(path, "/")) { path = "/" + path; } try { try { client.create().creatingParentsIfNeeded().forPath(path, pData); } catch (NodeExistsException nee) { client.setData().forPath(path, pData); } } catch (Exception e) { e.printStackTrace(); } }
/** * Set region as OFFLINED up in zookeeper asynchronously. * @param state * @return True if we succeeded, false otherwise (State was incorrect or failed * updating zk). */ private boolean asyncSetOfflineInZooKeeper(final RegionState state, final AsyncCallback.StringCallback cb, final ServerName destination) { if (!state.isClosed() && !state.isOffline()) { this.server.abort("Unexpected state trying to OFFLINE; " + state, new IllegalStateException()); return false; } regionStates.updateRegionState( state.getRegion(), RegionState.State.OFFLINE); try { ZKAssign.asyncCreateNodeOffline(watcher, state.getRegion(), destination, cb, state); } catch (KeeperException e) { if (e instanceof NodeExistsException) { LOG.warn("Node for " + state.getRegion() + " already exists"); } else { server.abort("Unexpected ZK exception creating/setting node OFFLINE", e); } return false; } return true; }
private void verifyZooKeeperStructure(ZooKeeper zk) throws KeeperException, InterruptedException { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Verifying ZooKeeper structure..."); } for (String path : new String[] { rootNode, groupNode, dataNode }) { try { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Ensuring {} exists", path); } if (zk.exists(path, true) == null) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("{} doesn't exist, creating", path); } zk.create(path, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } } catch (NodeExistsException ex) { // do nothing } } }
private void createEphemeralNode(long session, final DataTree dataTree, int count) throws NoNodeException, NodeExistsException { for (int i = 0; i < count; i++) { dataTree.createNode("/test" + i, new byte[0], null, session + i, dataTree.getNode("/").stat.getCversion() + 1, 1, 1); } }
private String startInternal(ClusteredTask clusteredTask, String taskId) { ClusteredTaskWithArgs withArgs; if( clusteredTask.isTransient() ) { String argsId = UUID.randomUUID().toString(); Serializable[] args = clusteredTask.getArgs(); taskArgs.put(argsId, args); withArgs = new ClusteredTaskWithArgs(clusteredTask, null, argsId, ourNodeId); } else { withArgs = new ClusteredTaskWithArgs(clusteredTask, clusteredTask.getArgs(), null, null); } byte[] taskBytes = PluginAwareObjectOutputStream.toBytes(withArgs); try { if( LOGGER.isTraceEnabled() ) { LOGGER.trace("Serialized ClusteredTask size is " + taskBytes.length); } curator.create().creatingParentsIfNeeded().forPath(zookeeperService.getFullPath(ZK_TASKPATH, taskId), taskBytes); } catch( NodeExistsException nee ) { LOGGER.debug("Task " + taskId + " already exists in ZK."); } catch( Exception e ) { throw Throwables.propagate(e); } return taskId; }
@Override public GlobalTaskStartInfo getGlobalTask(ClusteredTask globalTask, long millis) { if( !globalTask.isGlobal() ) throw new Error("Isn't a global task"); String globalId = globalTask.getGlobalId(); String existingTaskId = getRunningGlobalTask(globalId); if( existingTaskId != null ) { ensureGlobalTaskRunner(existingTaskId, globalTask); return new GlobalTaskStartInfo(existingTaskId, true); } String globalPath = zookeeperService.getFullPath(ZK_GLOBALTASKPATH, globalId); try { String taskId = UUID.randomUUID().toString(); try { curator.create().creatingParentsIfNeeded().forPath(globalPath, taskId.getBytes()); return new GlobalTaskStartInfo(startInternal(globalTask, taskId), false); } catch( NodeExistsException nee ) { final String taskFromZk = new String(curator.getData().forPath(globalPath)); //ensure there *is* a task for this ensureGlobalTaskRunner(taskFromZk, globalTask); return new GlobalTaskStartInfo(taskFromZk, false); } } catch( Exception e ) { throw Throwables.propagate(e); } }
/** * Creates a znode with OPENED state. * @param TEST_UTIL * @param region * @param serverName * @return * @throws IOException * @throws org.apache.hadoop.hbase.ZooKeeperConnectionException * @throws KeeperException * @throws NodeExistsException */ public static ZooKeeperWatcher createAndForceNodeToOpenedState( HBaseTestingUtility TEST_UTIL, HRegion region, ServerName serverName) throws ZooKeeperConnectionException, IOException, KeeperException, NodeExistsException { ZooKeeperWatcher zkw = getZooKeeperWatcher(TEST_UTIL); ZKAssign.createNodeOffline(zkw, region.getRegionInfo(), serverName); int version = ZKAssign.transitionNodeOpening(zkw, region .getRegionInfo(), serverName); ZKAssign.transitionNodeOpened(zkw, region.getRegionInfo(), serverName, version); return zkw; }
public static void stopMasterAndAssignMeta(HBaseTestingUtility HTU) throws NodeExistsException, KeeperException, IOException, InterruptedException { // Stop master HMaster master = HTU.getHBaseCluster().getMaster(); Thread masterThread = HTU.getHBaseCluster().getMasterThread(); ServerName masterAddr = master.getServerName(); master.stopMaster(); LOG.info("Waiting until master thread exits"); while (masterThread != null && masterThread.isAlive()) { Threads.sleep(100); } }
/** * Test if close region can handle ZK closing node version mismatch * @throws IOException * @throws NodeExistsException * @throws KeeperException * @throws DeserializationException */ @Test public void testZKClosingNodeVersionMismatch() throws IOException, NodeExistsException, KeeperException, DeserializationException { final Server server = new MockServer(HTU); final RegionServerServices rss = HTU.createMockRegionServerService(); HTableDescriptor htd = TEST_HTD; final HRegionInfo hri = TEST_HRI; ZkCoordinatedStateManager coordinationProvider = new ZkCoordinatedStateManager(); coordinationProvider.initialize(server); coordinationProvider.start(); // open a region first so that it can be closed later OpenRegion(server, rss, htd, hri, coordinationProvider.getOpenRegionCoordination()); // close the region // Create it CLOSING, which is what Master set before sending CLOSE RPC int versionOfClosingNode = ZKAssign.createNodeClosing(server.getZooKeeper(), hri, server.getServerName()); // The CloseRegionHandler will validate the expected version // Given it is set to invalid versionOfClosingNode+1, // CloseRegionHandler should be M_ZK_REGION_CLOSING ZkCloseRegionCoordination.ZkCloseRegionDetails zkCrd = new ZkCloseRegionCoordination.ZkCloseRegionDetails(); zkCrd.setPublishStatusInZk(true); zkCrd.setExpectedVersion(versionOfClosingNode+1); CloseRegionHandler handler = new CloseRegionHandler(server, rss, hri, false, coordinationProvider.getCloseRegionCoordination(), zkCrd); handler.process(); // Handler should remain in M_ZK_REGION_CLOSING RegionTransition rt = RegionTransition.parseFrom(ZKAssign.getData(server.getZooKeeper(), hri.getEncodedName())); assertTrue(rt.getEventType().equals(EventType.M_ZK_REGION_CLOSING )); }
/** * Test if the region can be closed properly * @throws IOException * @throws NodeExistsException * @throws KeeperException * @throws org.apache.hadoop.hbase.exceptions.DeserializationException */ @Test public void testCloseRegion() throws IOException, NodeExistsException, KeeperException, DeserializationException { final Server server = new MockServer(HTU); final RegionServerServices rss = HTU.createMockRegionServerService(); HTableDescriptor htd = TEST_HTD; HRegionInfo hri = TEST_HRI; ZkCoordinatedStateManager coordinationProvider = new ZkCoordinatedStateManager(); coordinationProvider.initialize(server); coordinationProvider.start(); // open a region first so that it can be closed later OpenRegion(server, rss, htd, hri, coordinationProvider.getOpenRegionCoordination()); // close the region // Create it CLOSING, which is what Master set before sending CLOSE RPC int versionOfClosingNode = ZKAssign.createNodeClosing(server.getZooKeeper(), hri, server.getServerName()); // The CloseRegionHandler will validate the expected version // Given it is set to correct versionOfClosingNode, // CloseRegionHandlerit should be RS_ZK_REGION_CLOSED ZkCloseRegionCoordination.ZkCloseRegionDetails zkCrd = new ZkCloseRegionCoordination.ZkCloseRegionDetails(); zkCrd.setPublishStatusInZk(true); zkCrd.setExpectedVersion(versionOfClosingNode); CloseRegionHandler handler = new CloseRegionHandler(server, rss, hri, false, coordinationProvider.getCloseRegionCoordination(), zkCrd); handler.process(); // Handler should have transitioned it to RS_ZK_REGION_CLOSED RegionTransition rt = RegionTransition.parseFrom( ZKAssign.getData(server.getZooKeeper(), hri.getEncodedName())); assertTrue(rt.getEventType().equals(EventType.RS_ZK_REGION_CLOSED)); }
/** * Utility method to ensure an ENABLED znode is in place; if not present, we create it. * @param zookeeper * @param path Path to znode to check * @return True if we created the znode. * @throws NodeExistsException * @throws KeeperException */ private static boolean ensurePeerEnabled(final ZooKeeperWatcher zookeeper, final String path) throws NodeExistsException, KeeperException { if (ZKUtil.checkExists(zookeeper, path) == -1) { // There is a race b/w PeerWatcher and ReplicationZookeeper#add method to create the // peer-state znode. This happens while adding a peer. // The peer state data is set as "ENABLED" by default. ZKUtil.createNodeIfNotExistsAndWatch(zookeeper, path, ReplicationStateZKBase.ENABLED_ZNODE_BYTES); return true; } return false; }
protected SingularityCreateResult create(String path, Optional<byte[]> data) { try { privateCreate(path, data); return SingularityCreateResult.CREATED; } catch (NodeExistsException nee) { return SingularityCreateResult.EXISTED; } catch (Throwable t) { throw Throwables.propagate(t); } }
protected SingularityCreateResult save(String path, Optional<byte[]> data) { try { privateCreate(path, data); return SingularityCreateResult.CREATED; } catch (NodeExistsException nee) { return set(path, data); } catch (Throwable t) { throw Throwables.propagate(t); } }
private void createTaskAndDeletePendingTaskPrivate(SingularityTask task) throws Exception { delete(getPendingPath(task.getTaskRequest().getPendingTask().getPendingTaskId())); final long now = System.currentTimeMillis(); String msg = String.format("Task launched because of %s", task.getTaskRequest().getPendingTask().getPendingTaskId().getPendingType().name()); if (task.getTaskRequest().getPendingTask().getUser().isPresent()) { msg = String.format("%s by %s", msg, task.getTaskRequest().getPendingTask().getUser().get()); } if (task.getTaskRequest().getPendingTask().getMessage().isPresent()) { msg = String.format("%s (%s)", msg, task.getTaskRequest().getPendingTask().getMessage().get()); } saveTaskHistoryUpdate(new SingularityTaskHistoryUpdate(task.getTaskId(), now, ExtendedTaskState.TASK_LAUNCHED, Optional.of(msg), Optional.<String>absent())); saveLastActiveTaskStatus(new SingularityTaskStatusHolder(task.getTaskId(), Optional.<TaskStatus>absent(), now, serverId, Optional.of(task.getOffer().getSlaveId().getValue()))); try { final String path = getTaskPath(task.getTaskId()); CuratorTransactionFinal transaction = curator.inTransaction().create().forPath(path, taskTranscoder.toBytes(task)).and(); transaction.create().forPath(getActivePath(task.getTaskId().getId())).and().commit(); taskCache.set(path, task); } catch (KeeperException.NodeExistsException nee) { LOG.error("Task or active path already existed for {}", task.getTaskId()); } }
public void createPersistent(String path) { try { client.create().forPath(path); } catch (NodeExistsException ignore) { } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } }
public void createEphemeral(String path) { try { client.create().withMode(CreateMode.EPHEMERAL).forPath(path); } catch (NodeExistsException ignore) { } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } }