public static void createNodeOffline(ZooKeeperWatcher zkw, HRegionInfo region, ServerName serverName, final EventType event) throws KeeperException, KeeperException.NodeExistsException { LOG.debug(zkw.prefix("Creating unassigned node for " + region.getEncodedName() + " in OFFLINE state")); RegionTransitionData data = new RegionTransitionData(event, region.getRegionName(), serverName); String node = getNodeName(zkw, region.getEncodedName()); ZKUtil.createAndWatch(zkw, node, data.getBytes()); }
private static void logRetrievedMsg(final ZooKeeperWatcher zkw, final String znode, final byte [] data, final boolean watcherSet) { if (!LOG.isDebugEnabled()) return; LOG.debug(zkw.prefix("Retrieved " + ((data == null)? 0: data.length) + " byte(s) of data from znode " + znode + (watcherSet? " and set watcher; ": "; data=") + (data == null? "null": data.length == 0? "empty": ( znode.startsWith(zkw.assignmentZNode) ? RegionTransitionData.fromBytes(data).toString() : StringUtils.abbreviate(Bytes.toStringBinary(data), 32))))); }
String encodedName = HRegionInfo.encodeRegionName(data.getRegionName()); LOG.info("Handling HBCK triggered transition=" + data.getEventType() + ", server=" + data.getOrigin() + ", region=" + HRegionInfo.prettyPrint(encodedName)); RegionState regionState = regionsInTransition.get(encodedName); switch (data.getEventType()) { case M_ZK_REGION_OFFLINE: HRegionInfo regionInfo = null; } else { try { byte[] name = data.getRegionName(); Pair<HRegionInfo, ServerName> p = MetaReader.getRegion(catalogTracker, name); regionInfo = p.getFirst(); data.getEventType() + ")"); break;
/** * Add to the in-memory copy of regions in transition and then call close * handler on passed region <code>hri</code> * @param hri * @param state * @param oldData */ private void addToRITandCallClose(final HRegionInfo hri, final RegionState.State state, final RegionTransitionData oldData) { this.regionsInTransition.put(hri.getEncodedName(), new RegionState(hri, state, oldData.getStamp(), oldData.getOrigin())); new ClosedRegionHandler(this.master, this, hri).process(); }
synchronized(regionsInTransition) { HRegionInfo hri = null; if (data == null || data.getOrigin() == null) { LOG.warn("Unexpected NULL input " + data); return; ServerName sn = data.getOrigin(); String encodedName = HRegionInfo.encodeRegionName(data.getRegionName()); String prettyPrintedRegionName = HRegionInfo.prettyPrint(encodedName); && !ignoreStatesRSOffline.contains(data.getEventType())) { LOG.warn("Attempted to handle region transition for server but " + "server is not online: " + prettyPrintedRegionName); boolean lateEvent = data.getStamp() < (System.currentTimeMillis() - 15000); LOG.debug("Handling transition=" + data.getEventType() + ", server=" + data.getOrigin() + ", region=" + (prettyPrintedRegionName == null? "null": prettyPrintedRegionName) + (lateEvent? ", which is more than 15 seconds late" : "")); RegionState regionState = regionsInTransition.get(encodedName); switch (data.getEventType()) { case M_ZK_REGION_OFFLINE: byte [] payload = data.getPayload(); List<HRegionInfo> daughters = null; try {
RegionTransitionData.fromBytes(existingBytes); if(!existingData.getEventType().equals(beginState)) { LOG.warn(zkw.prefix("Attempt to transition the " + "unassigned node for " + encoded + " from " + beginState + " to " + endState + " failed, " + "the node existed but was in the state " + existingData.getEventType() + " set by the server " + serverName)); return -1; RegionTransitionData data = new RegionTransitionData(endState, region.getRegionName(), serverName, payload); if(!ZKUtil.setData(zkw, node, data.getBytes(), stat.getVersion())) { LOG.warn(zkw.prefix("Attempt to transition the " + "unassigned node for " + encoded +
String encodedRegionName = regionInfo.getEncodedName(); LOG.info("Processing region " + regionInfo.getRegionNameAsString() + " in state " + data.getEventType()); List<HRegionInfo> hris = this.enablingTables.get(regionInfo.getTableNameAsString()); if (hris != null && !hris.isEmpty()) { switch (data.getEventType()) { case M_ZK_REGION_CLOSING: (data.getOrigin() == null || !serverManager.isServerOnline(data.getOrigin()))) { data.getStamp(), data.getOrigin())); (data.getOrigin() == null || !serverManager.isServerOnline(data.getOrigin()))) { } else if (data.getOrigin() != null && !serverManager.isServerOnline(data.getOrigin())) { } else { regionsInTransition.put(encodedRegionName, new RegionState( regionInfo, RegionState.State.PENDING_OPEN, data.getStamp(), data .getOrigin())); regionInfo, RegionState.State.OPENING, data.getStamp(), data .getOrigin())); RegionState.State.OPENING, data.getStamp(), data.getOrigin())); failoverProcessedRegions.put(encodedRegionName, regionInfo);
LOG.debug(zkw.prefix("Creating (or updating) unassigned node for " + region.getEncodedName() + " with OFFLINE state")); RegionTransitionData data = new RegionTransitionData( EventType.M_ZK_REGION_OFFLINE, region.getRegionName(), serverName); String node = getNodeName(zkw, region.getEncodedName()); return -1; return ZKUtil.createAndWatch(zkw, node, data.getBytes()); } else { RegionTransitionData curDataInZNode = ZKAssign.getDataNoWatch(zkw, region EventType eventType = curDataInZNode.getEventType(); if (eventType.equals(EventType.M_ZK_REGION_CLOSING) || eventType.equals(EventType.RS_ZK_REGION_CLOSED) setData = ZKUtil.setData(zkw, node, data.getBytes(), version); if (curData.getEventType() != data.getEventType()) {
RegionTransitionData.fromBytes(existingBytes); if (existingData.getEventType() == expectedState){ return true;
@Override public String toString() { return node + " (" + RegionTransitionData.fromBytes(data) + ")"; } public boolean isEmpty() {
return; if (dataInZNode.getEventType() == EventType.RS_ZK_REGION_OPENED) { LOG.debug("Region has transitioned to OPENED, allowing " + "watched event handlers to process"); return; } else if (dataInZNode.getEventType() != EventType.RS_ZK_REGION_OPENING && dataInZNode.getEventType() != EventType.RS_ZK_REGION_FAILED_OPEN ) { LOG.warn("While timing out a region in state OPENING, " + "found ZK node in unexpected state: " + dataInZNode.getEventType()); return;
/** * Get an instance from bytes. Throws a {@link RuntimeException} if * there is an error serializing this instance from bytes because it * represents a code bug. * @param bytes binary representation of this instance * @return instance of this class */ public static RegionTransitionData fromBytes(byte [] bytes) { try { RegionTransitionData data = new RegionTransitionData(); Writables.getWritable(bytes, data); return data; } catch(IOException e) { throw new RuntimeException(e); } }
/** * Gets the HRegionInfo from the META table * @param data * @return HRegionInfo hri for the region */ private HRegionInfo getHRegionInfo(RegionTransitionData data) { Pair<HRegionInfo, ServerName> p = null; try { p = MetaReader.getRegion(catalogTracker, data.getRegionName()); if (p == null) return null; return p.getFirst(); } catch (IOException e) { master.abort("Aborting because error occoured while reading " + data.getRegionName() + " from .META.", e); return null; } }
RegionTransitionData data = RegionTransitionData.fromBytes(bytes); if (!data.getEventType().equals(expectedState)) { LOG.warn(zkw.prefix("Attempting to delete unassigned " + "node " + regionName + " in " + expectedState + " state but node is in " + data.getEventType() + " state")); return false;
/** * Gets the current data in the unassigned node for the specified region name * or fully-qualified path. * * <p>Returns null if the region does not currently have a node. * * <p>Does not set a watch. * * @param zkw zk reference * @param pathOrRegionName fully-specified path or region name * @param stat object to store node info into on getData call * @return data for the unassigned node or null if node does not exist * @throws KeeperException if unexpected zookeeper exception */ public static RegionTransitionData getDataNoWatch(ZooKeeperWatcher zkw, String pathOrRegionName, Stat stat) throws KeeperException { String node = pathOrRegionName.startsWith("/") ? pathOrRegionName : getNodeName(zkw, pathOrRegionName); byte [] data = ZKUtil.getDataNoWatch(zkw, node, stat); if (data == null) { return null; } return RegionTransitionData.fromBytes(data); }
/** * @param path * @return True if znode is in SPLIT or SPLITTING state. * @throws KeeperException Can happen if the znode went away in meantime. */ private boolean isSplitOrSplitting(final String path) throws KeeperException { boolean result = false; // This may fail if the SPLIT or SPLITTING znode gets cleaned up before we // can get data from it. RegionTransitionData data = ZKAssign.getData(master.getZooKeeper(), path); EventType evt = data.getEventType(); switch (evt) { case RS_ZK_REGION_SPLIT: case RS_ZK_REGION_SPLITTING: result = true; break; default: break; } return result; }
/** * Creates a new ephemeral node in the SPLITTING state for the specified region. * Create it ephemeral in case regionserver dies mid-split. * * <p>Does not transition nodes from other states. If a node already exists * for this region, a {@link NodeExistsException} will be thrown. * * @param zkw zk reference * @param region region to be created as offline * @param serverName server event originates from * @return Version of znode created. * @throws KeeperException * @throws IOException */ void createNodeSplitting(final ZooKeeperWatcher zkw, final HRegionInfo region, final ServerName serverName) throws KeeperException, IOException { LOG.debug(zkw.prefix("Creating ephemeral node for " + region.getEncodedName() + " in SPLITTING state")); RegionTransitionData data = new RegionTransitionData(EventType.RS_ZK_REGION_SPLITTING, region.getRegionName(), serverName); String node = ZKAssign.getNodeName(zkw, region.getEncodedName()); if (!ZKUtil.createEphemeralNodeAndWatch(zkw, node, data.getBytes())) { throw new IOException("Failed create of ephemeral " + node); } }
/** * Gets the current data in the unassigned node for the specified region name * or fully-qualified path. * * <p>Returns null if the region does not currently have a node. * * <p>Sets a watch on the node if the node exists. * * @param zkw zk reference * @param pathOrRegionName fully-specified path or region name * @return data for the unassigned node * @throws KeeperException if unexpected zookeeper exception */ public static RegionTransitionData getData(ZooKeeperWatcher zkw, String pathOrRegionName) throws KeeperException { String node = pathOrRegionName.startsWith("/") ? pathOrRegionName : getNodeName(zkw, pathOrRegionName); byte [] data = ZKUtil.getDataAndWatch(zkw, node); if(data == null) { return null; } return RegionTransitionData.fromBytes(data); }