001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.master.assignment; 019 020import java.util.ArrayList; 021import java.util.Collection; 022import java.util.Collections; 023import java.util.Comparator; 024import java.util.HashMap; 025import java.util.Iterator; 026import java.util.List; 027import java.util.Map; 028import java.util.SortedSet; 029import java.util.TreeSet; 030import java.util.concurrent.ConcurrentHashMap; 031import java.util.concurrent.ConcurrentSkipListMap; 032import java.util.concurrent.atomic.AtomicInteger; 033import java.util.function.Predicate; 034import java.util.stream.Collectors; 035import org.apache.hadoop.hbase.HRegionLocation; 036import org.apache.hadoop.hbase.ServerName; 037import org.apache.hadoop.hbase.TableName; 038import org.apache.hadoop.hbase.client.RegionInfo; 039import org.apache.hadoop.hbase.client.TableState; 040import org.apache.hadoop.hbase.master.RegionState; 041import org.apache.hadoop.hbase.master.RegionState.State; 042import org.apache.hadoop.hbase.master.TableStateManager; 043import org.apache.hadoop.hbase.util.Bytes; 044import org.apache.yetus.audience.InterfaceAudience; 045import org.slf4j.Logger; 046import org.slf4j.LoggerFactory; 047 048/** 049 * RegionStates contains a set of Maps that describes the in-memory state of the AM, with the 050 * regions available in the system, the region in transition, the offline regions and the servers 051 * holding regions. 052 */ 053@InterfaceAudience.Private 054public class RegionStates { 055 private static final Logger LOG = LoggerFactory.getLogger(RegionStates.class); 056 057 // This comparator sorts the RegionStates by time stamp then Region name. 058 // Comparing by timestamp alone can lead us to discard different RegionStates that happen 059 // to share a timestamp. 060 private static class RegionStateStampComparator implements Comparator<RegionState> { 061 @Override 062 public int compare(final RegionState l, final RegionState r) { 063 int stampCmp = Long.compare(l.getStamp(), r.getStamp()); 064 return stampCmp != 0 ? stampCmp : RegionInfo.COMPARATOR.compare(l.getRegion(), r.getRegion()); 065 } 066 } 067 068 public final static RegionStateStampComparator REGION_STATE_STAMP_COMPARATOR = 069 new RegionStateStampComparator(); 070 071 private final Object regionsMapLock = new Object(); 072 073 // TODO: Replace the ConcurrentSkipListMaps 074 /** 075 * RegionName -- i.e. RegionInfo.getRegionName() -- as bytes to {@link RegionStateNode} 076 */ 077 private final ConcurrentSkipListMap<byte[], RegionStateNode> regionsMap = 078 new ConcurrentSkipListMap<>(Bytes.BYTES_COMPARATOR); 079 080 /** 081 * this map is a hack to lookup of region in master by encoded region name is O(n). must put and 082 * remove with regionsMap. 083 */ 084 private final ConcurrentSkipListMap<String, RegionStateNode> encodedRegionsMap = 085 new ConcurrentSkipListMap<>(); 086 087 private final ConcurrentSkipListMap<RegionInfo, RegionStateNode> regionInTransition = 088 new ConcurrentSkipListMap<>(RegionInfo.COMPARATOR); 089 090 /** 091 * Regions marked as offline on a read of hbase:meta. Unused or at least, once offlined, regions 092 * have no means of coming on line again. TODO. 093 */ 094 private final ConcurrentSkipListMap<RegionInfo, RegionStateNode> regionOffline = 095 new ConcurrentSkipListMap<RegionInfo, RegionStateNode>(); 096 097 private final ConcurrentSkipListMap<byte[], RegionFailedOpen> regionFailedOpen = 098 new ConcurrentSkipListMap<byte[], RegionFailedOpen>(Bytes.BYTES_COMPARATOR); 099 100 private final ConcurrentHashMap<ServerName, ServerStateNode> serverMap = 101 new ConcurrentHashMap<ServerName, ServerStateNode>(); 102 103 public RegionStates() { 104 } 105 106 /** 107 * Called on stop of AssignmentManager. 108 */ 109 public void clear() { 110 regionsMap.clear(); 111 encodedRegionsMap.clear(); 112 regionInTransition.clear(); 113 regionOffline.clear(); 114 serverMap.clear(); 115 } 116 117 public boolean isRegionInRegionStates(final RegionInfo hri) { 118 return (regionsMap.containsKey(hri.getRegionName()) || regionInTransition.containsKey(hri) 119 || regionOffline.containsKey(hri)); 120 } 121 122 // ========================================================================== 123 // RegionStateNode helpers 124 // ========================================================================== 125 RegionStateNode createRegionStateNode(RegionInfo regionInfo) { 126 synchronized (regionsMapLock) { 127 RegionStateNode node = regionsMap.computeIfAbsent(regionInfo.getRegionName(), 128 key -> new RegionStateNode(regionInfo, regionInTransition)); 129 130 if (encodedRegionsMap.get(regionInfo.getEncodedName()) != node) { 131 encodedRegionsMap.put(regionInfo.getEncodedName(), node); 132 } 133 134 return node; 135 } 136 } 137 138 public RegionStateNode getOrCreateRegionStateNode(RegionInfo regionInfo) { 139 RegionStateNode node = getRegionStateNodeFromName(regionInfo.getRegionName()); 140 return node != null ? node : createRegionStateNode(regionInfo); 141 } 142 143 public RegionStateNode getRegionStateNodeFromName(byte[] regionName) { 144 return regionsMap.get(regionName); 145 } 146 147 public RegionStateNode getRegionStateNode(RegionInfo regionInfo) { 148 return getRegionStateNodeFromName(regionInfo.getRegionName()); 149 } 150 151 public void deleteRegion(final RegionInfo regionInfo) { 152 synchronized (regionsMapLock) { 153 regionsMap.remove(regionInfo.getRegionName()); 154 encodedRegionsMap.remove(regionInfo.getEncodedName()); 155 } 156 // See HBASE-20860 157 // After master restarts, merged regions' RIT state may not be cleaned, 158 // making sure they are cleaned here 159 if (regionInTransition.containsKey(regionInfo)) { 160 regionInTransition.remove(regionInfo); 161 } 162 // Remove from the offline regions map too if there. 163 if (this.regionOffline.containsKey(regionInfo)) { 164 if (LOG.isTraceEnabled()) LOG.trace("Removing from regionOffline Map: " + regionInfo); 165 this.regionOffline.remove(regionInfo); 166 } 167 } 168 169 public void deleteRegions(final List<RegionInfo> regionInfos) { 170 regionInfos.forEach(this::deleteRegion); 171 } 172 173 List<RegionStateNode> getTableRegionStateNodes(final TableName tableName) { 174 final ArrayList<RegionStateNode> regions = new ArrayList<RegionStateNode>(); 175 for (RegionStateNode node : regionsMap.tailMap(tableName.getName()).values()) { 176 if (!node.getTable().equals(tableName)) break; 177 regions.add(node); 178 } 179 return regions; 180 } 181 182 ArrayList<RegionState> getTableRegionStates(final TableName tableName) { 183 final ArrayList<RegionState> regions = new ArrayList<RegionState>(); 184 for (RegionStateNode node : regionsMap.tailMap(tableName.getName()).values()) { 185 if (!node.getTable().equals(tableName)) break; 186 regions.add(node.toRegionState()); 187 } 188 return regions; 189 } 190 191 ArrayList<RegionInfo> getTableRegionsInfo(final TableName tableName) { 192 final ArrayList<RegionInfo> regions = new ArrayList<RegionInfo>(); 193 for (RegionStateNode node : regionsMap.tailMap(tableName.getName()).values()) { 194 if (!node.getTable().equals(tableName)) break; 195 regions.add(node.getRegionInfo()); 196 } 197 return regions; 198 } 199 200 /** @return A view of region state nodes for all the regions. */ 201 public Collection<RegionStateNode> getRegionStateNodes() { 202 return Collections.unmodifiableCollection(regionsMap.values()); 203 } 204 205 /** @return A snapshot of region state nodes for all the regions. */ 206 public ArrayList<RegionState> getRegionStates() { 207 final ArrayList<RegionState> regions = new ArrayList<>(regionsMap.size()); 208 for (RegionStateNode node : regionsMap.values()) { 209 regions.add(node.toRegionState()); 210 } 211 return regions; 212 } 213 214 // ========================================================================== 215 // RegionState helpers 216 // ========================================================================== 217 public RegionState getRegionState(final RegionInfo regionInfo) { 218 RegionStateNode regionStateNode = getRegionStateNode(regionInfo); 219 return regionStateNode == null ? null : regionStateNode.toRegionState(); 220 } 221 222 public RegionState getRegionState(final String encodedRegionName) { 223 final RegionStateNode node = encodedRegionsMap.get(encodedRegionName); 224 if (node == null) { 225 return null; 226 } 227 return node.toRegionState(); 228 } 229 230 // ============================================================================================ 231 // TODO: helpers 232 // ============================================================================================ 233 public boolean hasTableRegionStates(final TableName tableName) { 234 // TODO 235 return !getTableRegionStates(tableName).isEmpty(); 236 } 237 238 /** 239 * @return Return online regions of table; does not include OFFLINE or SPLITTING regions. 240 */ 241 public List<RegionInfo> getRegionsOfTable(TableName table) { 242 return getRegionsOfTable(table, regionNode -> !regionNode.isInState(State.OFFLINE, State.SPLIT) 243 && !regionNode.getRegionInfo().isSplitParent()); 244 } 245 246 private HRegionLocation createRegionForReopen(RegionStateNode node) { 247 node.lock(); 248 try { 249 if (!include(node, false)) { 250 return null; 251 } 252 if (node.isInState(State.OPEN)) { 253 return new HRegionLocation(node.getRegionInfo(), node.getRegionLocation(), 254 node.getOpenSeqNum()); 255 } else if (node.isInState(State.OPENING)) { 256 return new HRegionLocation(node.getRegionInfo(), node.getRegionLocation(), -1); 257 } else { 258 return null; 259 } 260 } finally { 261 node.unlock(); 262 } 263 } 264 265 /** 266 * Get the regions to be reopened when modifying a table. 267 * <p/> 268 * Notice that the {@code openSeqNum} in the returned HRegionLocation is also used to indicate the 269 * state of this region, positive means the region is in {@link State#OPEN}, -1 means 270 * {@link State#OPENING}. And for regions in other states we do not need reopen them. 271 */ 272 public List<HRegionLocation> getRegionsOfTableForReopen(TableName tableName) { 273 return getTableRegionStateNodes(tableName).stream().map(this::createRegionForReopen) 274 .filter(r -> r != null).collect(Collectors.toList()); 275 } 276 277 /** 278 * Check whether the region has been reopened. The meaning of the {@link HRegionLocation} is the 279 * same with {@link #getRegionsOfTableForReopen(TableName)}. 280 * <p/> 281 * For a region which is in {@link State#OPEN} before, if the region state is changed or the open 282 * seq num is changed, we can confirm that it has been reopened. 283 * <p/> 284 * For a region which is in {@link State#OPENING} before, usually it will be in {@link State#OPEN} 285 * now and we will schedule a MRP to reopen it. But there are several exceptions: 286 * <ul> 287 * <li>The region is in state other than {@link State#OPEN} or {@link State#OPENING}.</li> 288 * <li>The location of the region has been changed</li> 289 * </ul> 290 * Of course the region could still be in {@link State#OPENING} state and still on the same 291 * server, then here we will still return a {@link HRegionLocation} for it, just like 292 * {@link #getRegionsOfTableForReopen(TableName)}. 293 * @param oldLoc the previous state/location of this region 294 * @return null if the region has been reopened, otherwise a new {@link HRegionLocation} which 295 * means we still need to reopen the region. 296 * @see #getRegionsOfTableForReopen(TableName) 297 */ 298 public HRegionLocation checkReopened(HRegionLocation oldLoc) { 299 RegionStateNode node = getRegionStateNode(oldLoc.getRegion()); 300 // HBASE-20921 301 // if the oldLoc's state node does not exist, that means the region is 302 // merged or split, no need to check it 303 if (node == null) { 304 return null; 305 } 306 node.lock(); 307 try { 308 if (oldLoc.getSeqNum() >= 0) { 309 // in OPEN state before 310 if (node.isInState(State.OPEN)) { 311 if (node.getOpenSeqNum() > oldLoc.getSeqNum()) { 312 // normal case, the region has been reopened 313 return null; 314 } else { 315 // the open seq num does not change, need to reopen again 316 return new HRegionLocation(node.getRegionInfo(), node.getRegionLocation(), 317 node.getOpenSeqNum()); 318 } 319 } else { 320 // the state has been changed so we can make sure that the region has been reopened(not 321 // finished maybe, but not a problem). 322 return null; 323 } 324 } else { 325 // in OPENING state before 326 if (!node.isInState(State.OPEN, State.OPENING)) { 327 // not in OPEN or OPENING state, then we can make sure that the region has been 328 // reopened(not finished maybe, but not a problem) 329 return null; 330 } else { 331 if (!node.getRegionLocation().equals(oldLoc.getServerName())) { 332 // the region has been moved, so we can make sure that the region has been reopened. 333 return null; 334 } 335 // normal case, we are still in OPENING state, or the reopen has been opened and the state 336 // is changed to OPEN. 337 long openSeqNum = node.isInState(State.OPEN) ? node.getOpenSeqNum() : -1; 338 return new HRegionLocation(node.getRegionInfo(), node.getRegionLocation(), openSeqNum); 339 } 340 } 341 } finally { 342 node.unlock(); 343 } 344 } 345 346 /** 347 * Get the regions for enabling a table. 348 * <p/> 349 * Here we want the EnableTableProcedure to be more robust and can be used to fix some nasty 350 * states, so the checks in this method will be a bit strange. In general, a region can only be 351 * offline when it is split, for merging we will just delete the parent regions, but with HBCK we 352 * may force update the state of a region to fix some nasty bugs, so in this method we will try to 353 * bring the offline regions back if it is not split. That's why we only check for split state 354 * here. 355 */ 356 public List<RegionInfo> getRegionsOfTableForEnabling(TableName table) { 357 return getRegionsOfTable(table, 358 regionNode -> !regionNode.isInState(State.SPLIT) && !regionNode.getRegionInfo().isSplit()); 359 } 360 361 /** 362 * Get the regions for deleting a table. 363 * <p/> 364 * Here we need to return all the regions irrespective of the states in order to archive them all. 365 * This is because if we don't archive OFFLINE/SPLIT regions and if a snapshot or a cloned table 366 * references to the regions, we will lose the data of the regions. 367 */ 368 public List<RegionInfo> getRegionsOfTableForDeleting(TableName table) { 369 return getTableRegionStateNodes(table).stream().map(RegionStateNode::getRegionInfo) 370 .collect(Collectors.toList()); 371 } 372 373 /** 374 * @return Return the regions of the table and filter them. 375 */ 376 private List<RegionInfo> getRegionsOfTable(TableName table, Predicate<RegionStateNode> filter) { 377 return getTableRegionStateNodes(table).stream().filter(filter).map(n -> n.getRegionInfo()) 378 .collect(Collectors.toList()); 379 } 380 381 /** 382 * Utility. Whether to include region in list of regions. Default is to weed out split and offline 383 * regions. 384 * @return True if we should include the <code>node</code> (do not include if split or offline 385 * unless <code>offline</code> is set to true. 386 */ 387 private boolean include(final RegionStateNode node, final boolean offline) { 388 if (LOG.isTraceEnabled()) { 389 LOG.trace("WORKING ON " + node + " " + node.getRegionInfo()); 390 } 391 final RegionInfo hri = node.getRegionInfo(); 392 if (node.isInState(State.SPLIT) || hri.isSplit()) { 393 return false; 394 } 395 if ((node.isInState(State.OFFLINE) || hri.isOffline()) && !offline) { 396 return false; 397 } 398 return (!hri.isOffline() && !hri.isSplit()) || ((hri.isOffline() || hri.isSplit()) && offline); 399 } 400 401 // ============================================================================================ 402 // Split helpers 403 // These methods will only be called in ServerCrashProcedure, and at the end of SCP we will remove 404 // the ServerStateNode by calling removeServer. 405 // ============================================================================================ 406 407 private void setServerState(ServerName serverName, ServerState state) { 408 ServerStateNode serverNode = getOrCreateServer(serverName); 409 synchronized (serverNode) { 410 serverNode.setState(state); 411 } 412 } 413 414 /** 415 * Call this when we start meta log splitting a crashed Server. 416 * @see #metaLogSplit(ServerName) 417 */ 418 public void metaLogSplitting(ServerName serverName) { 419 setServerState(serverName, ServerState.SPLITTING_META); 420 } 421 422 /** 423 * Called after we've split the meta logs on a crashed Server. 424 * @see #metaLogSplitting(ServerName) 425 */ 426 public void metaLogSplit(ServerName serverName) { 427 setServerState(serverName, ServerState.SPLITTING_META_DONE); 428 } 429 430 /** 431 * Call this when we start log splitting for a crashed Server. 432 * @see #logSplit(ServerName) 433 */ 434 public void logSplitting(final ServerName serverName) { 435 setServerState(serverName, ServerState.SPLITTING); 436 } 437 438 /** 439 * Called after we've split all logs on a crashed Server. 440 * @see #logSplitting(ServerName) 441 */ 442 public void logSplit(final ServerName serverName) { 443 setServerState(serverName, ServerState.OFFLINE); 444 } 445 446 public void updateRegionState(RegionInfo regionInfo, State state) { 447 RegionStateNode regionNode = getOrCreateRegionStateNode(regionInfo); 448 regionNode.lock(); 449 try { 450 regionNode.setState(state); 451 } finally { 452 regionNode.unlock(); 453 } 454 } 455 456 // ============================================================================================ 457 // TODO: 458 // ============================================================================================ 459 public List<RegionInfo> getAssignedRegions() { 460 final List<RegionInfo> result = new ArrayList<RegionInfo>(); 461 for (RegionStateNode node : regionsMap.values()) { 462 if (!node.isInTransition()) { 463 result.add(node.getRegionInfo()); 464 } 465 } 466 return result; 467 } 468 469 public boolean isRegionInState(RegionInfo regionInfo, State... state) { 470 RegionStateNode regionNode = getRegionStateNode(regionInfo); 471 if (regionNode != null) { 472 regionNode.lock(); 473 try { 474 return regionNode.isInState(state); 475 } finally { 476 regionNode.unlock(); 477 } 478 } 479 return false; 480 } 481 482 public boolean isRegionOnline(final RegionInfo regionInfo) { 483 return isRegionInState(regionInfo, State.OPEN); 484 } 485 486 /** 487 * @return True if region is offline (In OFFLINE or CLOSED state). 488 */ 489 public boolean isRegionOffline(final RegionInfo regionInfo) { 490 return isRegionInState(regionInfo, State.OFFLINE, State.CLOSED); 491 } 492 493 public Map<ServerName, List<RegionInfo>> 494 getSnapShotOfAssignment(final Collection<RegionInfo> regions) { 495 final Map<ServerName, List<RegionInfo>> result = new HashMap<ServerName, List<RegionInfo>>(); 496 if (regions != null) { 497 for (RegionInfo hri : regions) { 498 final RegionStateNode node = getRegionStateNode(hri); 499 if (node == null) { 500 continue; 501 } 502 createSnapshot(node, result); 503 } 504 } else { 505 for (RegionStateNode node : regionsMap.values()) { 506 if (node == null) { 507 continue; 508 } 509 createSnapshot(node, result); 510 } 511 } 512 return result; 513 } 514 515 private void createSnapshot(RegionStateNode node, Map<ServerName, List<RegionInfo>> result) { 516 final ServerName serverName = node.getRegionLocation(); 517 if (serverName == null) { 518 return; 519 } 520 521 List<RegionInfo> serverRegions = result.get(serverName); 522 if (serverRegions == null) { 523 serverRegions = new ArrayList<RegionInfo>(); 524 result.put(serverName, serverRegions); 525 } 526 serverRegions.add(node.getRegionInfo()); 527 } 528 529 public Map<RegionInfo, ServerName> getRegionAssignments() { 530 final HashMap<RegionInfo, ServerName> assignments = new HashMap<RegionInfo, ServerName>(); 531 for (RegionStateNode node : regionsMap.values()) { 532 assignments.put(node.getRegionInfo(), node.getRegionLocation()); 533 } 534 return assignments; 535 } 536 537 public Map<RegionState.State, List<RegionInfo>> getRegionByStateOfTable(TableName tableName) { 538 final State[] states = State.values(); 539 final Map<RegionState.State, List<RegionInfo>> tableRegions = 540 new HashMap<State, List<RegionInfo>>(states.length); 541 for (int i = 0; i < states.length; ++i) { 542 tableRegions.put(states[i], new ArrayList<RegionInfo>()); 543 } 544 545 for (RegionStateNode node : regionsMap.values()) { 546 if (node.getTable().equals(tableName)) { 547 tableRegions.get(node.getState()).add(node.getRegionInfo()); 548 } 549 } 550 return tableRegions; 551 } 552 553 public ServerName getRegionServerOfRegion(RegionInfo regionInfo) { 554 RegionStateNode regionNode = getRegionStateNode(regionInfo); 555 if (regionNode != null) { 556 regionNode.lock(); 557 try { 558 ServerName server = regionNode.getRegionLocation(); 559 return server != null ? server : regionNode.getLastHost(); 560 } finally { 561 regionNode.unlock(); 562 } 563 } 564 return null; 565 } 566 567 /** 568 * This is an EXPENSIVE clone. Cloning though is the safest thing to do. Can't let out original 569 * since it can change and at least the load balancer wants to iterate this exported list. We need 570 * to synchronize on regions since all access to this.servers is under a lock on this.regions. 571 * @return A clone of current open or opening assignments. 572 */ 573 public Map<TableName, Map<ServerName, List<RegionInfo>>> 574 getAssignmentsForBalancer(TableStateManager tableStateManager, List<ServerName> onlineServers) { 575 final Map<TableName, Map<ServerName, List<RegionInfo>>> result = new HashMap<>(); 576 for (RegionStateNode node : regionsMap.values()) { 577 // DisableTableProcedure first sets the table state to DISABLED and then force unassigns 578 // the regions in a loop. The balancer should ignore all regions for tables in DISABLED 579 // state because even if still currently open we expect them to be offlined very soon. 580 if (isTableDisabled(tableStateManager, node.getTable())) { 581 if (LOG.isTraceEnabled()) { 582 LOG.trace("Ignoring {} because table is disabled", node); 583 } 584 continue; 585 } 586 // When balancing, we are only interested in OPEN or OPENING regions. These can be 587 // expected to remain online until the next balancer iteration or unless the balancer 588 // decides to move it. Regions in other states are not eligible for balancing, because 589 // they are closing, splitting, merging, or otherwise already in transition. 590 if (!node.isInState(State.OPEN, State.OPENING)) { 591 if (LOG.isTraceEnabled()) { 592 LOG.trace("Ignoring {} because region is not OPEN or OPENING", node); 593 } 594 continue; 595 } 596 Map<ServerName, List<RegionInfo>> tableResult = 597 result.computeIfAbsent(node.getTable(), t -> new HashMap<>()); 598 final ServerName serverName = node.getRegionLocation(); 599 // A region in ONLINE or OPENING state should have a location. 600 if (serverName == null) { 601 LOG.warn("Skipping, no server for {}", node); 602 continue; 603 } 604 List<RegionInfo> serverResult = 605 tableResult.computeIfAbsent(serverName, s -> new ArrayList<>()); 606 serverResult.add(node.getRegionInfo()); 607 } 608 // Add online servers with no assignment for the table. 609 for (Map<ServerName, List<RegionInfo>> table : result.values()) { 610 for (ServerName serverName : onlineServers) { 611 table.computeIfAbsent(serverName, key -> new ArrayList<>()); 612 } 613 } 614 return result; 615 } 616 617 private boolean isTableDisabled(final TableStateManager tableStateManager, 618 final TableName tableName) { 619 return tableStateManager.isTableState(tableName, TableState.State.DISABLED, 620 TableState.State.DISABLING); 621 } 622 623 // ========================================================================== 624 // Region in transition helpers 625 // ========================================================================== 626 public boolean hasRegionsInTransition() { 627 return !regionInTransition.isEmpty(); 628 } 629 630 public boolean isRegionInTransition(final RegionInfo regionInfo) { 631 final RegionStateNode node = regionInTransition.get(regionInfo); 632 return node != null ? node.isInTransition() : false; 633 } 634 635 public RegionState getRegionTransitionState(RegionInfo hri) { 636 RegionStateNode node = regionInTransition.get(hri); 637 if (node == null) { 638 return null; 639 } 640 641 node.lock(); 642 try { 643 return node.isInTransition() ? node.toRegionState() : null; 644 } finally { 645 node.unlock(); 646 } 647 } 648 649 public List<RegionStateNode> getRegionsInTransition() { 650 return new ArrayList<RegionStateNode>(regionInTransition.values()); 651 } 652 653 /** 654 * Get the number of regions in transition. 655 */ 656 public int getRegionsInTransitionCount() { 657 return regionInTransition.size(); 658 } 659 660 public List<RegionState> getRegionsStateInTransition() { 661 final List<RegionState> rit = new ArrayList<RegionState>(regionInTransition.size()); 662 for (RegionStateNode node : regionInTransition.values()) { 663 rit.add(node.toRegionState()); 664 } 665 return rit; 666 } 667 668 public SortedSet<RegionState> getRegionsInTransitionOrderedByTimestamp() { 669 final SortedSet<RegionState> rit = new TreeSet<RegionState>(REGION_STATE_STAMP_COMPARATOR); 670 for (RegionStateNode node : regionInTransition.values()) { 671 rit.add(node.toRegionState()); 672 } 673 return rit; 674 } 675 676 // ========================================================================== 677 // Region offline helpers 678 // ========================================================================== 679 // TODO: Populated when we read meta but regions never make it out of here. 680 public void addToOfflineRegions(final RegionStateNode regionNode) { 681 LOG.info("Added to offline, CURRENTLY NEVER CLEARED!!! " + regionNode); 682 regionOffline.put(regionNode.getRegionInfo(), regionNode); 683 } 684 685 // TODO: Unused. 686 public void removeFromOfflineRegions(final RegionInfo regionInfo) { 687 regionOffline.remove(regionInfo); 688 } 689 690 // ========================================================================== 691 // Region FAIL_OPEN helpers 692 // ========================================================================== 693 public static final class RegionFailedOpen { 694 private final RegionStateNode regionNode; 695 696 private volatile Exception exception = null; 697 private AtomicInteger retries = new AtomicInteger(); 698 699 public RegionFailedOpen(final RegionStateNode regionNode) { 700 this.regionNode = regionNode; 701 } 702 703 public RegionStateNode getRegionStateNode() { 704 return regionNode; 705 } 706 707 public RegionInfo getRegionInfo() { 708 return regionNode.getRegionInfo(); 709 } 710 711 public int incrementAndGetRetries() { 712 return this.retries.incrementAndGet(); 713 } 714 715 public int getRetries() { 716 return retries.get(); 717 } 718 719 public void setException(final Exception exception) { 720 this.exception = exception; 721 } 722 723 public Exception getException() { 724 return this.exception; 725 } 726 } 727 728 public RegionFailedOpen addToFailedOpen(final RegionStateNode regionNode) { 729 final byte[] key = regionNode.getRegionInfo().getRegionName(); 730 return regionFailedOpen.computeIfAbsent(key, (k) -> new RegionFailedOpen(regionNode)); 731 } 732 733 public RegionFailedOpen getFailedOpen(final RegionInfo regionInfo) { 734 return regionFailedOpen.get(regionInfo.getRegionName()); 735 } 736 737 public void removeFromFailedOpen(final RegionInfo regionInfo) { 738 regionFailedOpen.remove(regionInfo.getRegionName()); 739 } 740 741 public List<RegionState> getRegionFailedOpen() { 742 if (regionFailedOpen.isEmpty()) return Collections.emptyList(); 743 744 ArrayList<RegionState> regions = new ArrayList<RegionState>(regionFailedOpen.size()); 745 for (RegionFailedOpen r : regionFailedOpen.values()) { 746 regions.add(r.getRegionStateNode().toRegionState()); 747 } 748 return regions; 749 } 750 751 // ========================================================================== 752 // Servers 753 // ========================================================================== 754 755 /** 756 * Be judicious calling this method. Do it on server register ONLY otherwise you could mess up 757 * online server accounting. TOOD: Review usage and convert to {@link #getServerNode(ServerName)} 758 * where we can. 759 */ 760 public ServerStateNode getOrCreateServer(final ServerName serverName) { 761 return serverMap.computeIfAbsent(serverName, key -> new ServerStateNode(key)); 762 } 763 764 /** 765 * Called by SCP at end of successful processing. 766 */ 767 public void removeServer(final ServerName serverName) { 768 serverMap.remove(serverName); 769 } 770 771 /** 772 * @return Pertinent ServerStateNode or NULL if none found (Do not make modifications). 773 */ 774 public ServerStateNode getServerNode(final ServerName serverName) { 775 return serverMap.get(serverName); 776 } 777 778 public double getAverageLoad() { 779 int numServers = 0; 780 int totalLoad = 0; 781 for (ServerStateNode node : serverMap.values()) { 782 totalLoad += node.getRegionCount(); 783 numServers++; 784 } 785 return numServers == 0 ? 0.0 : (double) totalLoad / (double) numServers; 786 } 787 788 public ServerStateNode addRegionToServer(final RegionStateNode regionNode) { 789 ServerStateNode serverNode = getOrCreateServer(regionNode.getRegionLocation()); 790 serverNode.addRegion(regionNode); 791 return serverNode; 792 } 793 794 public ServerStateNode removeRegionFromServer(final ServerName serverName, 795 final RegionStateNode regionNode) { 796 ServerStateNode serverNode = getOrCreateServer(serverName); 797 serverNode.removeRegion(regionNode); 798 return serverNode; 799 } 800 801 // ========================================================================== 802 // ToString helpers 803 // ========================================================================== 804 public static String regionNamesToString(final Collection<byte[]> regions) { 805 final StringBuilder sb = new StringBuilder(); 806 final Iterator<byte[]> it = regions.iterator(); 807 sb.append("["); 808 if (it.hasNext()) { 809 sb.append(Bytes.toStringBinary(it.next())); 810 while (it.hasNext()) { 811 sb.append(", "); 812 sb.append(Bytes.toStringBinary(it.next())); 813 } 814 } 815 sb.append("]"); 816 return sb.toString(); 817 } 818}