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 edu.umd.cs.findbugs.annotations.NonNull;
021import java.io.IOException;
022import java.util.ArrayList;
023import java.util.Collection;
024import java.util.Collections;
025import java.util.Comparator;
026import java.util.HashMap;
027import java.util.HashSet;
028import java.util.List;
029import java.util.Map;
030import java.util.Set;
031import java.util.SortedSet;
032import java.util.TreeSet;
033import java.util.concurrent.CompletableFuture;
034import java.util.concurrent.Future;
035import java.util.concurrent.TimeUnit;
036import java.util.concurrent.atomic.AtomicBoolean;
037import java.util.concurrent.locks.Condition;
038import java.util.concurrent.locks.ReentrantLock;
039import java.util.function.Consumer;
040import java.util.function.Function;
041import java.util.stream.Collectors;
042import java.util.stream.Stream;
043import org.apache.hadoop.conf.Configuration;
044import org.apache.hadoop.hbase.CatalogFamilyFormat;
045import org.apache.hadoop.hbase.DoNotRetryIOException;
046import org.apache.hadoop.hbase.HBaseIOException;
047import org.apache.hadoop.hbase.HConstants;
048import org.apache.hadoop.hbase.PleaseHoldException;
049import org.apache.hadoop.hbase.ServerName;
050import org.apache.hadoop.hbase.TableName;
051import org.apache.hadoop.hbase.UnknownRegionException;
052import org.apache.hadoop.hbase.client.DoNotRetryRegionException;
053import org.apache.hadoop.hbase.client.MasterSwitchType;
054import org.apache.hadoop.hbase.client.RegionInfo;
055import org.apache.hadoop.hbase.client.RegionInfoBuilder;
056import org.apache.hadoop.hbase.client.RegionReplicaUtil;
057import org.apache.hadoop.hbase.client.RegionStatesCount;
058import org.apache.hadoop.hbase.client.Result;
059import org.apache.hadoop.hbase.client.ResultScanner;
060import org.apache.hadoop.hbase.client.Scan;
061import org.apache.hadoop.hbase.client.TableDescriptor;
062import org.apache.hadoop.hbase.client.TableState;
063import org.apache.hadoop.hbase.exceptions.UnexpectedStateException;
064import org.apache.hadoop.hbase.favored.FavoredNodesManager;
065import org.apache.hadoop.hbase.favored.FavoredNodesPromoter;
066import org.apache.hadoop.hbase.master.LoadBalancer;
067import org.apache.hadoop.hbase.master.MasterServices;
068import org.apache.hadoop.hbase.master.MetricsAssignmentManager;
069import org.apache.hadoop.hbase.master.RegionPlan;
070import org.apache.hadoop.hbase.master.RegionState;
071import org.apache.hadoop.hbase.master.RegionState.State;
072import org.apache.hadoop.hbase.master.ServerManager;
073import org.apache.hadoop.hbase.master.TableStateManager;
074import org.apache.hadoop.hbase.master.balancer.FavoredStochasticBalancer;
075import org.apache.hadoop.hbase.master.procedure.HBCKServerCrashProcedure;
076import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
077import org.apache.hadoop.hbase.master.procedure.MasterProcedureScheduler;
078import org.apache.hadoop.hbase.master.procedure.ProcedureSyncWait;
079import org.apache.hadoop.hbase.master.procedure.ServerCrashProcedure;
080import org.apache.hadoop.hbase.master.procedure.TruncateRegionProcedure;
081import org.apache.hadoop.hbase.master.region.MasterRegion;
082import org.apache.hadoop.hbase.procedure2.Procedure;
083import org.apache.hadoop.hbase.procedure2.ProcedureEvent;
084import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
085import org.apache.hadoop.hbase.procedure2.ProcedureInMemoryChore;
086import org.apache.hadoop.hbase.procedure2.util.StringUtils;
087import org.apache.hadoop.hbase.regionserver.SequenceId;
088import org.apache.hadoop.hbase.rsgroup.RSGroupBasedLoadBalancer;
089import org.apache.hadoop.hbase.util.Bytes;
090import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
091import org.apache.hadoop.hbase.util.FutureUtils;
092import org.apache.hadoop.hbase.util.Pair;
093import org.apache.hadoop.hbase.util.Threads;
094import org.apache.hadoop.hbase.util.VersionInfo;
095import org.apache.hadoop.hbase.zookeeper.MetaTableLocator;
096import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
097import org.apache.yetus.audience.InterfaceAudience;
098import org.apache.zookeeper.KeeperException;
099import org.slf4j.Logger;
100import org.slf4j.LoggerFactory;
101
102import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
103import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.RegionStateTransition;
104import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.RegionStateTransition.TransitionCode;
105import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.ReportRegionStateTransitionRequest;
106import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.ReportRegionStateTransitionResponse;
107
108/**
109 * The AssignmentManager is the coordinator for region assign/unassign operations.
110 * <ul>
111 * <li>In-memory states of regions and servers are stored in {@link RegionStates}.</li>
112 * <li>hbase:meta state updates are handled by {@link RegionStateStore}.</li>
113 * </ul>
114 * Regions are created by CreateTable, Split, Merge. Regions are deleted by DeleteTable, Split,
115 * Merge. Assigns are triggered by CreateTable, EnableTable, Split, Merge, ServerCrash. Unassigns
116 * are triggered by DisableTable, Split, Merge
117 */
118@InterfaceAudience.Private
119public class AssignmentManager {
120  private static final Logger LOG = LoggerFactory.getLogger(AssignmentManager.class);
121
122  // TODO: AMv2
123  // - handle region migration from hbase1 to hbase2.
124  // - handle sys table assignment first (e.g. acl, namespace)
125  // - handle table priorities
126  // - If ServerBusyException trying to update hbase:meta, we abort the Master
127  // See updateRegionLocation in RegionStateStore.
128  //
129  // See also
130  // https://docs.google.com/document/d/1eVKa7FHdeoJ1-9o8yZcOTAQbv0u0bblBlCCzVSIn69g/edit#heading=h.ystjyrkbtoq5
131  // for other TODOs.
132
133  public static final String BOOTSTRAP_THREAD_POOL_SIZE_CONF_KEY =
134    "hbase.assignment.bootstrap.thread.pool.size";
135
136  public static final String ASSIGN_DISPATCH_WAIT_MSEC_CONF_KEY =
137    "hbase.assignment.dispatch.wait.msec";
138  private static final int DEFAULT_ASSIGN_DISPATCH_WAIT_MSEC = 150;
139
140  public static final String ASSIGN_DISPATCH_WAITQ_MAX_CONF_KEY =
141    "hbase.assignment.dispatch.wait.queue.max.size";
142  private static final int DEFAULT_ASSIGN_DISPATCH_WAITQ_MAX = 100;
143
144  public static final String RIT_CHORE_INTERVAL_MSEC_CONF_KEY =
145    "hbase.assignment.rit.chore.interval.msec";
146  private static final int DEFAULT_RIT_CHORE_INTERVAL_MSEC = 60 * 1000;
147
148  public static final String DEAD_REGION_METRIC_CHORE_INTERVAL_MSEC_CONF_KEY =
149    "hbase.assignment.dead.region.metric.chore.interval.msec";
150  private static final int DEFAULT_DEAD_REGION_METRIC_CHORE_INTERVAL_MSEC = 120 * 1000;
151
152  public static final String ASSIGN_MAX_ATTEMPTS = "hbase.assignment.maximum.attempts";
153  private static final int DEFAULT_ASSIGN_MAX_ATTEMPTS = Integer.MAX_VALUE;
154
155  public static final String ASSIGN_RETRY_IMMEDIATELY_MAX_ATTEMPTS =
156    "hbase.assignment.retry.immediately.maximum.attempts";
157  private static final int DEFAULT_ASSIGN_RETRY_IMMEDIATELY_MAX_ATTEMPTS = 3;
158
159  /** Region in Transition metrics threshold time */
160  public static final String METRICS_RIT_STUCK_WARNING_THRESHOLD =
161    "hbase.metrics.rit.stuck.warning.threshold";
162  private static final int DEFAULT_RIT_STUCK_WARNING_THRESHOLD = 60 * 1000;
163  public static final String UNEXPECTED_STATE_REGION = "Unexpected state for ";
164
165  public static final String FORCE_REGION_RETAINMENT = "hbase.master.scp.retain.assignment.force";
166
167  public static final boolean DEFAULT_FORCE_REGION_RETAINMENT = false;
168
169  /** The wait time in millis before checking again if the region's previous RS is back online */
170  public static final String FORCE_REGION_RETAINMENT_WAIT_INTERVAL =
171    "hbase.master.scp.retain.assignment.force.wait-interval";
172
173  public static final long DEFAULT_FORCE_REGION_RETAINMENT_WAIT_INTERVAL = 50;
174
175  /**
176   * The number of times to check if the region's previous RS is back online, before giving up and
177   * proceeding with assignment on a new RS
178   */
179  public static final String FORCE_REGION_RETAINMENT_RETRIES =
180    "hbase.master.scp.retain.assignment.force.retries";
181
182  public static final int DEFAULT_FORCE_REGION_RETAINMENT_RETRIES = 600;
183
184  private final ProcedureEvent<?> metaAssignEvent = new ProcedureEvent<>("meta assign");
185  private final ProcedureEvent<?> metaLoadEvent = new ProcedureEvent<>("meta load");
186
187  private final MetricsAssignmentManager metrics;
188  private final RegionInTransitionChore ritChore;
189  private final DeadServerMetricRegionChore deadMetricChore;
190  private final MasterServices master;
191
192  private final AtomicBoolean running = new AtomicBoolean(false);
193  private final RegionStates regionStates = new RegionStates();
194  private final RegionStateStore regionStateStore;
195
196  /**
197   * When the operator uses this configuration option, any version between the current cluster
198   * version and the value of "hbase.min.version.move.system.tables" does not trigger any
199   * auto-region movement. Auto-region movement here refers to auto-migration of system table
200   * regions to newer server versions. It is assumed that the configured range of versions does not
201   * require special handling of moving system table regions to higher versioned RegionServer. This
202   * auto-migration is done by {@link #checkIfShouldMoveSystemRegionAsync()}. Example: Let's assume
203   * the cluster is on version 1.4.0 and we have set "hbase.min.version.move.system.tables" as
204   * "2.0.0". Now if we upgrade one RegionServer on 1.4.0 cluster to 1.6.0 (< 2.0.0), then
205   * AssignmentManager will not move hbase:meta, hbase:namespace and other system table regions to
206   * newly brought up RegionServer 1.6.0 as part of auto-migration. However, if we upgrade one
207   * RegionServer on 1.4.0 cluster to 2.2.0 (> 2.0.0), then AssignmentManager will move all system
208   * table regions to newly brought up RegionServer 2.2.0 as part of auto-migration done by
209   * {@link #checkIfShouldMoveSystemRegionAsync()}. "hbase.min.version.move.system.tables" is
210   * introduced as part of HBASE-22923.
211   */
212  private final String minVersionToMoveSysTables;
213
214  private static final String MIN_VERSION_MOVE_SYS_TABLES_CONFIG =
215    "hbase.min.version.move.system.tables";
216  private static final String DEFAULT_MIN_VERSION_MOVE_SYS_TABLES_CONFIG = "";
217
218  private final Map<ServerName, Set<byte[]>> rsReports = new HashMap<>();
219
220  private final boolean shouldAssignRegionsWithFavoredNodes;
221  private final int assignDispatchWaitQueueMaxSize;
222  private final int assignDispatchWaitMillis;
223  private final int assignMaxAttempts;
224  private final int assignRetryImmediatelyMaxAttempts;
225
226  private final MasterRegion masterRegion;
227
228  private final Object checkIfShouldMoveSystemRegionLock = new Object();
229
230  private Thread assignThread;
231
232  private final boolean forceRegionRetainment;
233
234  private final long forceRegionRetainmentWaitInterval;
235
236  private final int forceRegionRetainmentRetries;
237
238  private final RegionInTransitionTracker regionInTransitionTracker;
239
240  public AssignmentManager(MasterServices master, MasterRegion masterRegion) {
241    this(master, masterRegion, new RegionStateStore(master, masterRegion));
242  }
243
244  AssignmentManager(MasterServices master, MasterRegion masterRegion, RegionStateStore stateStore) {
245    this.master = master;
246    this.regionStateStore = stateStore;
247    this.metrics = new MetricsAssignmentManager();
248    this.regionInTransitionTracker = new RegionInTransitionTracker(metrics::updateRitDuration);
249    this.masterRegion = masterRegion;
250
251    final Configuration conf = master.getConfiguration();
252
253    // Only read favored nodes if using the favored nodes load balancer.
254    this.shouldAssignRegionsWithFavoredNodes = FavoredStochasticBalancer.class
255      .isAssignableFrom(conf.getClass(HConstants.HBASE_MASTER_LOADBALANCER_CLASS, Object.class));
256
257    this.assignDispatchWaitMillis =
258      conf.getInt(ASSIGN_DISPATCH_WAIT_MSEC_CONF_KEY, DEFAULT_ASSIGN_DISPATCH_WAIT_MSEC);
259    this.assignDispatchWaitQueueMaxSize =
260      conf.getInt(ASSIGN_DISPATCH_WAITQ_MAX_CONF_KEY, DEFAULT_ASSIGN_DISPATCH_WAITQ_MAX);
261
262    this.assignMaxAttempts =
263      Math.max(1, conf.getInt(ASSIGN_MAX_ATTEMPTS, DEFAULT_ASSIGN_MAX_ATTEMPTS));
264    this.assignRetryImmediatelyMaxAttempts = conf.getInt(ASSIGN_RETRY_IMMEDIATELY_MAX_ATTEMPTS,
265      DEFAULT_ASSIGN_RETRY_IMMEDIATELY_MAX_ATTEMPTS);
266
267    int ritChoreInterval =
268      conf.getInt(RIT_CHORE_INTERVAL_MSEC_CONF_KEY, DEFAULT_RIT_CHORE_INTERVAL_MSEC);
269    this.ritChore = new RegionInTransitionChore(ritChoreInterval);
270
271    int deadRegionChoreInterval = conf.getInt(DEAD_REGION_METRIC_CHORE_INTERVAL_MSEC_CONF_KEY,
272      DEFAULT_DEAD_REGION_METRIC_CHORE_INTERVAL_MSEC);
273    if (deadRegionChoreInterval > 0) {
274      this.deadMetricChore = new DeadServerMetricRegionChore(deadRegionChoreInterval);
275    } else {
276      this.deadMetricChore = null;
277    }
278    minVersionToMoveSysTables =
279      conf.get(MIN_VERSION_MOVE_SYS_TABLES_CONFIG, DEFAULT_MIN_VERSION_MOVE_SYS_TABLES_CONFIG);
280
281    forceRegionRetainment =
282      conf.getBoolean(FORCE_REGION_RETAINMENT, DEFAULT_FORCE_REGION_RETAINMENT);
283    forceRegionRetainmentWaitInterval = conf.getLong(FORCE_REGION_RETAINMENT_WAIT_INTERVAL,
284      DEFAULT_FORCE_REGION_RETAINMENT_WAIT_INTERVAL);
285    forceRegionRetainmentRetries =
286      conf.getInt(FORCE_REGION_RETAINMENT_RETRIES, DEFAULT_FORCE_REGION_RETAINMENT_RETRIES);
287  }
288
289  private void mirrorMetaLocations() throws IOException, KeeperException {
290    // For compatibility, mirror the meta region state to zookeeper
291    // And we still need to use zookeeper to publish the meta region locations to region
292    // server, so they can serve as ClientMetaService
293    ZKWatcher zk = master.getZooKeeper();
294    if (zk == null || !zk.getRecoverableZooKeeper().getState().isAlive()) {
295      // this is possible in tests, we do not provide a zk watcher or the zk watcher has been closed
296      return;
297    }
298    Collection<RegionStateNode> metaStates = regionStates.getRegionStateNodes();
299    for (RegionStateNode metaState : metaStates) {
300      MetaTableLocator.setMetaLocation(zk, metaState.getRegionLocation(),
301        metaState.getRegionInfo().getReplicaId(), metaState.getState());
302    }
303    int replicaCount = metaStates.size();
304    // remove extra mirror locations
305    for (String znode : zk.getMetaReplicaNodes()) {
306      int replicaId = zk.getZNodePaths().getMetaReplicaIdFromZNode(znode);
307      if (replicaId >= replicaCount) {
308        MetaTableLocator.deleteMetaLocation(zk, replicaId);
309      }
310    }
311  }
312
313  public void start() throws IOException, KeeperException {
314    if (!running.compareAndSet(false, true)) {
315      return;
316    }
317
318    LOG.trace("Starting assignment manager");
319
320    // Start the Assignment Thread
321    startAssignmentThread();
322    // load meta region states.
323    // here we are still in the early steps of active master startup. There is only one thread(us)
324    // can access AssignmentManager and create region node, so here we do not need to lock the
325    // region node.
326    try (ResultScanner scanner =
327      masterRegion.getScanner(new Scan().addFamily(HConstants.CATALOG_FAMILY))) {
328      for (;;) {
329        Result result = scanner.next();
330        if (result == null) {
331          break;
332        }
333        RegionStateStore
334          .visitMetaEntry((r, regionInfo, state, regionLocation, lastHost, openSeqNum) -> {
335            RegionStateNode regionNode = regionStates.getOrCreateRegionStateNode(regionInfo);
336            regionNode.setState(state);
337            regionNode.setLastHost(lastHost);
338            regionNode.setRegionLocation(regionLocation);
339            regionNode.setOpenSeqNum(openSeqNum);
340            regionInTransitionTracker.handleRegionStateNodeOperation(regionNode);
341
342            if (regionNode.getProcedure() != null) {
343              regionNode.getProcedure().stateLoaded(this, regionNode);
344            }
345            if (regionLocation != null) {
346              // TODO: this could lead to some orphan server state nodes, as it is possible that the
347              // region server is already dead and its SCP has already finished but we have
348              // persisted an opening state on this region server. Finally the TRSP will assign the
349              // region to another region server, so it will not cause critical problems, just waste
350              // some memory as no one will try to cleanup these orphan server state nodes.
351              regionStates.createServer(regionLocation);
352              regionStates.addRegionToServer(regionNode);
353            }
354            if (RegionReplicaUtil.isDefaultReplica(regionInfo.getReplicaId())) {
355              setMetaAssigned(regionInfo, state == State.OPEN);
356            }
357            LOG.debug("Loaded {} {}", TableName.META_TABLE_NAME, regionNode);
358          }, result);
359      }
360    }
361    mirrorMetaLocations();
362  }
363
364  /**
365   * Create RegionStateNode based on the TRSP list, and attach the TRSP to the RegionStateNode.
366   * <p>
367   * This is used to restore the RIT region list, so we do not need to restore it in the loadingMeta
368   * method below. And it is also very important as now before submitting a TRSP, we need to attach
369   * it to the RegionStateNode, which acts like a guard, so we need to restore this information at
370   * the very beginning, before we start processing any procedures.
371   */
372  public void setupRIT(List<TransitRegionStateProcedure> procs) {
373    procs.forEach(proc -> {
374      RegionInfo regionInfo = proc.getRegion();
375      RegionStateNode regionNode = regionStates.getOrCreateRegionStateNode(regionInfo);
376      TransitRegionStateProcedure existingProc = regionNode.getProcedure();
377      if (existingProc != null) {
378        // This is possible, as we will detach the procedure from the RSN before we
379        // actually finish the procedure. This is because that, we will detach the TRSP from the RSN
380        // during execution, at that time, the procedure has not been marked as done in the pv2
381        // framework yet, so it is possible that we schedule a new TRSP immediately and when
382        // arriving here, we will find out that there are multiple TRSPs for the region. But we can
383        // make sure that, only the last one can take the charge, the previous ones should have all
384        // been finished already. So here we will compare the proc id, the greater one will win.
385        if (existingProc.getProcId() < proc.getProcId()) {
386          // the new one wins, unset and set it to the new one below
387          regionNode.unsetProcedure(existingProc);
388        } else {
389          // the old one wins, skip
390          return;
391        }
392      }
393      LOG.info("Attach {} to {}", proc, regionNode);
394      regionNode.setProcedure(proc);
395    });
396  }
397
398  public void initializationPostMetaOnline() {
399    // now that we are sure that meta is online, we can set TableStateManger in
400    // regionInTransitionTracker
401    regionInTransitionTracker.setTableStateManager(master.getTableStateManager());
402  }
403
404  public void stop() {
405    if (!running.compareAndSet(true, false)) {
406      return;
407    }
408
409    LOG.info("Stopping assignment manager");
410
411    // The AM is started before the procedure executor,
412    // but the actual work will be loaded/submitted only once we have the executor
413    final boolean hasProcExecutor = master.getMasterProcedureExecutor() != null;
414
415    // Remove the RIT chore
416    if (hasProcExecutor) {
417      master.getMasterProcedureExecutor().removeChore(this.ritChore);
418      if (this.deadMetricChore != null) {
419        master.getMasterProcedureExecutor().removeChore(this.deadMetricChore);
420      }
421    }
422
423    // Stop the Assignment Thread
424    stopAssignmentThread();
425
426    // Stop the RegionStateStore
427    regionStates.clear();
428    regionInTransitionTracker.stop();
429
430    // Update meta events (for testing)
431    if (hasProcExecutor) {
432      metaLoadEvent.suspend();
433      for (RegionInfo hri : getMetaRegionSet()) {
434        setMetaAssigned(hri, false);
435      }
436    }
437  }
438
439  public boolean isRunning() {
440    return running.get();
441  }
442
443  public Configuration getConfiguration() {
444    return master.getConfiguration();
445  }
446
447  public MetricsAssignmentManager getAssignmentManagerMetrics() {
448    return metrics;
449  }
450
451  private LoadBalancer getBalancer() {
452    return master.getLoadBalancer();
453  }
454
455  private FavoredNodesPromoter getFavoredNodePromoter() {
456    return (FavoredNodesPromoter) ((RSGroupBasedLoadBalancer) master.getLoadBalancer())
457      .getInternalBalancer();
458  }
459
460  private MasterProcedureEnv getProcedureEnvironment() {
461    return master.getMasterProcedureExecutor().getEnvironment();
462  }
463
464  private MasterProcedureScheduler getProcedureScheduler() {
465    return getProcedureEnvironment().getProcedureScheduler();
466  }
467
468  int getAssignMaxAttempts() {
469    return assignMaxAttempts;
470  }
471
472  public boolean isForceRegionRetainment() {
473    return forceRegionRetainment;
474  }
475
476  public long getForceRegionRetainmentWaitInterval() {
477    return forceRegionRetainmentWaitInterval;
478  }
479
480  public int getForceRegionRetainmentRetries() {
481    return forceRegionRetainmentRetries;
482  }
483
484  int getAssignRetryImmediatelyMaxAttempts() {
485    return assignRetryImmediatelyMaxAttempts;
486  }
487
488  public RegionStates getRegionStates() {
489    return regionStates;
490  }
491
492  /**
493   * Returns the regions hosted by the specified server.
494   * <p/>
495   * Notice that, for SCP, after we submit the SCP, no one can change the region list for the
496   * ServerStateNode so we do not need any locks here. And for other usage, this can only give you a
497   * snapshot of the current region list for this server, which means, right after you get the
498   * region list, new regions may be moved to this server or some regions may be moved out from this
499   * server, so you should not use it critically if you need strong consistency.
500   */
501  public List<RegionInfo> getRegionsOnServer(ServerName serverName) {
502    ServerStateNode serverInfo = regionStates.getServerNode(serverName);
503    if (serverInfo == null) {
504      return Collections.emptyList();
505    }
506    return serverInfo.getRegionInfoList();
507  }
508
509  private RegionInfo getRegionInfo(RegionStateNode rsn) {
510    if (rsn.isSplit() && !rsn.getRegionInfo().isSplit()) {
511      // see the comments in markRegionAsSplit on why we need to do this converting.
512      return RegionInfoBuilder.newBuilder(rsn.getRegionInfo()).setSplit(true).setOffline(true)
513        .build();
514    } else {
515      return rsn.getRegionInfo();
516    }
517  }
518
519  private Stream<RegionStateNode> getRegionStateNodes(TableName tableName,
520    boolean excludeOfflinedSplitParents) {
521    Stream<RegionStateNode> stream = regionStates.getTableRegionStateNodes(tableName).stream();
522    if (excludeOfflinedSplitParents) {
523      return stream.filter(rsn -> !rsn.isSplit());
524    } else {
525      return stream;
526    }
527  }
528
529  public List<RegionInfo> getTableRegions(TableName tableName,
530    boolean excludeOfflinedSplitParents) {
531    return getRegionStateNodes(tableName, excludeOfflinedSplitParents).map(this::getRegionInfo)
532      .collect(Collectors.toList());
533  }
534
535  public List<Pair<RegionInfo, ServerName>> getTableRegionsAndLocations(TableName tableName,
536    boolean excludeOfflinedSplitParents) {
537    return getRegionStateNodes(tableName, excludeOfflinedSplitParents)
538      .map(rsn -> Pair.newPair(getRegionInfo(rsn), rsn.getRegionLocation()))
539      .collect(Collectors.toList());
540  }
541
542  public RegionStateStore getRegionStateStore() {
543    return regionStateStore;
544  }
545
546  public List<ServerName> getFavoredNodes(final RegionInfo regionInfo) {
547    return this.shouldAssignRegionsWithFavoredNodes
548      ? getFavoredNodePromoter().getFavoredNodes(regionInfo)
549      : ServerName.EMPTY_SERVER_LIST;
550  }
551
552  // ============================================================================================
553  // Table State Manager helpers
554  // ============================================================================================
555  private TableStateManager getTableStateManager() {
556    return master.getTableStateManager();
557  }
558
559  private boolean isTableEnabled(final TableName tableName) {
560    return getTableStateManager().isTableState(tableName, TableState.State.ENABLED);
561  }
562
563  private boolean isTableDisabled(final TableName tableName) {
564    return getTableStateManager().isTableState(tableName, TableState.State.DISABLED,
565      TableState.State.DISABLING);
566  }
567
568  // ============================================================================================
569  // META Helpers
570  // ============================================================================================
571  private boolean isMetaRegion(final RegionInfo regionInfo) {
572    return regionInfo.isMetaRegion();
573  }
574
575  public boolean isMetaRegion(final byte[] regionName) {
576    return getMetaRegionFromName(regionName) != null;
577  }
578
579  public RegionInfo getMetaRegionFromName(final byte[] regionName) {
580    for (RegionInfo hri : getMetaRegionSet()) {
581      if (Bytes.equals(hri.getRegionName(), regionName)) {
582        return hri;
583      }
584    }
585    return null;
586  }
587
588  public boolean isCarryingMeta(final ServerName serverName) {
589    // TODO: handle multiple meta
590    return isCarryingRegion(serverName, RegionInfoBuilder.FIRST_META_REGIONINFO);
591  }
592
593  private boolean isCarryingRegion(final ServerName serverName, final RegionInfo regionInfo) {
594    // TODO: check for state?
595    final RegionStateNode node = regionStates.getRegionStateNode(regionInfo);
596    return (node != null && serverName.equals(node.getRegionLocation()));
597  }
598
599  private RegionInfo getMetaForRegion(final RegionInfo regionInfo) {
600    // if (regionInfo.isMetaRegion()) return regionInfo;
601    // TODO: handle multiple meta. if the region provided is not meta lookup
602    // which meta the region belongs to.
603    return RegionInfoBuilder.FIRST_META_REGIONINFO;
604  }
605
606  // TODO: handle multiple meta.
607  private static final Set<RegionInfo> META_REGION_SET =
608    Collections.singleton(RegionInfoBuilder.FIRST_META_REGIONINFO);
609
610  public Set<RegionInfo> getMetaRegionSet() {
611    return META_REGION_SET;
612  }
613
614  // ============================================================================================
615  // META Event(s) helpers
616  // ============================================================================================
617  /**
618   * Notice that, this only means the meta region is available on a RS, but the AM may still be
619   * loading the region states from meta, so usually you need to check {@link #isMetaLoaded()} first
620   * before checking this method, unless you can make sure that your piece of code can only be
621   * executed after AM builds the region states.
622   * @see #isMetaLoaded()
623   */
624  public boolean isMetaAssigned() {
625    return metaAssignEvent.isReady();
626  }
627
628  public boolean isMetaRegionInTransition() {
629    return !isMetaAssigned();
630  }
631
632  /**
633   * Notice that this event does not mean the AM has already finished region state rebuilding. See
634   * the comment of {@link #isMetaAssigned()} for more details.
635   * @see #isMetaAssigned()
636   */
637  public boolean waitMetaAssigned(Procedure<?> proc, RegionInfo regionInfo) {
638    return getMetaAssignEvent(getMetaForRegion(regionInfo)).suspendIfNotReady(proc);
639  }
640
641  private void setMetaAssigned(RegionInfo metaRegionInfo, boolean assigned) {
642    assert isMetaRegion(metaRegionInfo) : "unexpected non-meta region " + metaRegionInfo;
643    ProcedureEvent<?> metaAssignEvent = getMetaAssignEvent(metaRegionInfo);
644    if (assigned) {
645      metaAssignEvent.wake(getProcedureScheduler());
646    } else {
647      metaAssignEvent.suspend();
648    }
649  }
650
651  private ProcedureEvent<?> getMetaAssignEvent(RegionInfo metaRegionInfo) {
652    assert isMetaRegion(metaRegionInfo) : "unexpected non-meta region " + metaRegionInfo;
653    // TODO: handle multiple meta.
654    return metaAssignEvent;
655  }
656
657  /**
658   * Wait until AM finishes the meta loading, i.e, the region states rebuilding.
659   * @see #isMetaLoaded()
660   * @see #waitMetaAssigned(Procedure, RegionInfo)
661   */
662  public boolean waitMetaLoaded(Procedure<?> proc) {
663    return metaLoadEvent.suspendIfNotReady(proc);
664  }
665
666  /**
667   * This method will be called in master initialization method after calling
668   * {@link #processOfflineRegions()}, as in processOfflineRegions we will generate assign
669   * procedures for offline regions, which may be conflict with creating table.
670   * <p/>
671   * This is a bit dirty, should be reconsidered after we decide whether to keep the
672   * {@link #processOfflineRegions()} method.
673   */
674  public void wakeMetaLoadedEvent() {
675    metaLoadEvent.wake(getProcedureScheduler());
676    assert isMetaLoaded() : "expected meta to be loaded";
677  }
678
679  /**
680   * Return whether AM finishes the meta loading, i.e, the region states rebuilding.
681   * @see #isMetaAssigned()
682   * @see #waitMetaLoaded(Procedure)
683   */
684  public boolean isMetaLoaded() {
685    return metaLoadEvent.isReady();
686  }
687
688  /**
689   * Start a new thread to check if there are region servers whose versions are higher than others.
690   * If so, move all system table regions to RS with the highest version to keep compatibility. The
691   * reason is, RS in new version may not be able to access RS in old version when there are some
692   * incompatible changes.
693   * <p>
694   * This method is called when a new RegionServer is added to cluster only.
695   * </p>
696   */
697  public void checkIfShouldMoveSystemRegionAsync() {
698    // TODO: Fix this thread. If a server is killed and a new one started, this thread thinks that
699    // it should 'move' the system tables from the old server to the new server but
700    // ServerCrashProcedure is on it; and it will take care of the assign without dataloss.
701    if (this.master.getServerManager().countOfRegionServers() <= 1) {
702      return;
703    }
704    // This thread used to run whenever there was a change in the cluster. The ZooKeeper
705    // childrenChanged notification came in before the nodeDeleted message and so this method
706    // cold run before a ServerCrashProcedure could run. That meant that this thread could see
707    // a Crashed Server before ServerCrashProcedure and it could find system regions on the
708    // crashed server and go move them before ServerCrashProcedure had a chance; could be
709    // dataloss too if WALs were not recovered.
710    new Thread(() -> {
711      try {
712        synchronized (checkIfShouldMoveSystemRegionLock) {
713          List<RegionPlan> plans = new ArrayList<>();
714          // TODO: I don't think this code does a good job if all servers in cluster have same
715          // version. It looks like it will schedule unnecessary moves.
716          for (ServerName server : getExcludedServersForSystemTable()) {
717            if (master.getServerManager().isServerDead(server)) {
718              // TODO: See HBASE-18494 and HBASE-18495. Though getExcludedServersForSystemTable()
719              // considers only online servers, the server could be queued for dead server
720              // processing. As region assignments for crashed server is handled by
721              // ServerCrashProcedure, do NOT handle them here. The goal is to handle this through
722              // regular flow of LoadBalancer as a favored node and not to have this special
723              // handling.
724              continue;
725            }
726            List<RegionInfo> regionsShouldMove = getSystemTables(server);
727            if (!regionsShouldMove.isEmpty()) {
728              for (RegionInfo regionInfo : regionsShouldMove) {
729                // null value for dest forces destination server to be selected by balancer
730                RegionPlan plan = new RegionPlan(regionInfo, server, null);
731                if (regionInfo.isMetaRegion()) {
732                  // Must move meta region first.
733                  LOG.info("Async MOVE of {} to newer Server={}", regionInfo.getEncodedName(),
734                    server);
735                  moveAsync(plan);
736                } else {
737                  plans.add(plan);
738                }
739              }
740            }
741            for (RegionPlan plan : plans) {
742              LOG.info("Async MOVE of {} to newer Server={}", plan.getRegionInfo().getEncodedName(),
743                server);
744              moveAsync(plan);
745            }
746          }
747        }
748      } catch (Throwable t) {
749        LOG.error(t.toString(), t);
750      }
751    }).start();
752  }
753
754  private List<RegionInfo> getSystemTables(ServerName serverName) {
755    ServerStateNode serverNode = regionStates.getServerNode(serverName);
756    if (serverNode == null) {
757      return Collections.emptyList();
758    }
759    return serverNode.getSystemRegionInfoList();
760  }
761
762  private void preTransitCheck(RegionStateNode regionNode, RegionState.State[] expectedStates)
763    throws HBaseIOException {
764    if (regionNode.getProcedure() != null) {
765      throw new HBaseIOException(
766        regionNode + " is currently in transition; pid=" + regionNode.getProcedure().getProcId());
767    }
768    if (!regionNode.isInState(expectedStates)) {
769      throw new DoNotRetryRegionException(UNEXPECTED_STATE_REGION + regionNode);
770    }
771    if (isTableDisabled(regionNode.getTable())) {
772      throw new DoNotRetryIOException(regionNode.getTable() + " is disabled for " + regionNode);
773    }
774  }
775
776  /**
777   * Create an assign TransitRegionStateProcedure. Makes sure of RegionState. Throws exception if
778   * not appropriate UNLESS override is set. Used by hbck2 but also by straightline
779   * {@link #assign(RegionInfo, ServerName)} and {@link #assignAsync(RegionInfo, ServerName)}.
780   * @see #createAssignProcedure(RegionStateNode, ServerName) for a version that does NO checking
781   *      used when only when no checking needed.
782   * @param override If false, check RegionState is appropriate for assign; if not throw exception.
783   */
784  private TransitRegionStateProcedure createAssignProcedure(RegionInfo regionInfo, ServerName sn,
785    boolean override, boolean force) throws IOException {
786    RegionStateNode regionNode = regionStates.getOrCreateRegionStateNode(regionInfo);
787    regionNode.lock();
788    try {
789      if (override) {
790        if (!force) {
791          preTransitCheck(regionNode, STATES_EXPECTED_ON_ASSIGN);
792        }
793        if (regionNode.getProcedure() != null) {
794          regionNode.unsetProcedure(regionNode.getProcedure());
795        }
796      } else {
797        preTransitCheck(regionNode, STATES_EXPECTED_ON_ASSIGN);
798      }
799      assert regionNode.getProcedure() == null;
800      TransitRegionStateProcedure proc = regionNode.setProcedure(
801        TransitRegionStateProcedure.assign(getProcedureEnvironment(), regionInfo, sn));
802      regionInTransitionTracker.handleRegionStateNodeOperation(regionNode);
803      return proc;
804    } finally {
805      regionNode.unlock();
806    }
807  }
808
809  /**
810   * Create an assign TransitRegionStateProcedure. Does NO checking of RegionState. Presumes
811   * appriopriate state ripe for assign.
812   * @see #createAssignProcedure(RegionInfo, ServerName, boolean, boolean)
813   */
814  private TransitRegionStateProcedure createAssignProcedure(RegionStateNode regionNode,
815    ServerName targetServer) {
816    regionNode.lock();
817    try {
818      TransitRegionStateProcedure proc = regionNode.setProcedure(TransitRegionStateProcedure
819        .assign(getProcedureEnvironment(), regionNode.getRegionInfo(), targetServer));
820      regionInTransitionTracker.handleRegionStateNodeOperation(regionNode);
821      return proc;
822    } finally {
823      regionNode.unlock();
824    }
825  }
826
827  public long assign(RegionInfo regionInfo, ServerName sn) throws IOException {
828    TransitRegionStateProcedure proc = createAssignProcedure(regionInfo, sn, false, false);
829    ProcedureSyncWait.submitAndWaitProcedure(master.getMasterProcedureExecutor(), proc);
830    return proc.getProcId();
831  }
832
833  public long assign(RegionInfo regionInfo) throws IOException {
834    return assign(regionInfo, null);
835  }
836
837  /**
838   * Submits a procedure that assigns a region to a target server without waiting for it to finish
839   * @param regionInfo the region we would like to assign
840   * @param sn         target server name
841   */
842  public Future<byte[]> assignAsync(RegionInfo regionInfo, ServerName sn) throws IOException {
843    return ProcedureSyncWait.submitProcedure(master.getMasterProcedureExecutor(),
844      createAssignProcedure(regionInfo, sn, false, false));
845  }
846
847  /**
848   * Submits a procedure that assigns a region without waiting for it to finish
849   * @param regionInfo the region we would like to assign
850   */
851  public Future<byte[]> assignAsync(RegionInfo regionInfo) throws IOException {
852    return assignAsync(regionInfo, null);
853  }
854
855  public long unassign(RegionInfo regionInfo) throws IOException {
856    RegionStateNode regionNode = regionStates.getRegionStateNode(regionInfo);
857    if (regionNode == null) {
858      throw new UnknownRegionException("No RegionState found for " + regionInfo.getEncodedName());
859    }
860    TransitRegionStateProcedure proc;
861    regionNode.lock();
862    try {
863      preTransitCheck(regionNode, STATES_EXPECTED_ON_UNASSIGN_OR_MOVE);
864      proc = TransitRegionStateProcedure.unassign(getProcedureEnvironment(), regionInfo);
865      regionNode.setProcedure(proc);
866    } finally {
867      regionNode.unlock();
868    }
869    ProcedureSyncWait.submitAndWaitProcedure(master.getMasterProcedureExecutor(), proc);
870    return proc.getProcId();
871  }
872
873  public TransitRegionStateProcedure createMoveRegionProcedure(RegionInfo regionInfo,
874    ServerName targetServer) throws HBaseIOException {
875    RegionStateNode regionNode = this.regionStates.getRegionStateNode(regionInfo);
876    if (regionNode == null) {
877      throw new UnknownRegionException(
878        "No RegionStateNode found for " + regionInfo.getEncodedName() + "(Closed/Deleted?)");
879    }
880    TransitRegionStateProcedure proc;
881    regionNode.lock();
882    try {
883      preTransitCheck(regionNode, STATES_EXPECTED_ON_UNASSIGN_OR_MOVE);
884      regionNode.checkOnline();
885      proc = TransitRegionStateProcedure.move(getProcedureEnvironment(), regionInfo, targetServer);
886      regionNode.setProcedure(proc);
887    } finally {
888      regionNode.unlock();
889    }
890    return proc;
891  }
892
893  public void move(RegionInfo regionInfo) throws IOException {
894    TransitRegionStateProcedure proc = createMoveRegionProcedure(regionInfo, null);
895    ProcedureSyncWait.submitAndWaitProcedure(master.getMasterProcedureExecutor(), proc);
896  }
897
898  public Future<byte[]> moveAsync(RegionPlan regionPlan) throws HBaseIOException {
899    TransitRegionStateProcedure proc =
900      createMoveRegionProcedure(regionPlan.getRegionInfo(), regionPlan.getDestination());
901    return ProcedureSyncWait.submitProcedure(master.getMasterProcedureExecutor(), proc);
902  }
903
904  public Future<byte[]> balance(RegionPlan regionPlan) throws HBaseIOException {
905    ServerName current =
906      this.getRegionStates().getRegionAssignments().get(regionPlan.getRegionInfo());
907    if (current == null || !current.equals(regionPlan.getSource())) {
908      LOG.debug("Skip region plan {}, source server not match, current region location is {}",
909        regionPlan, current == null ? "(null)" : current);
910      return null;
911    }
912    return moveAsync(regionPlan);
913  }
914
915  // ============================================================================================
916  // RegionTransition procedures helpers
917  // ============================================================================================
918
919  /**
920   * Create round-robin assigns. Use on table creation to distribute out regions across cluster.
921   * @return AssignProcedures made out of the passed in <code>hris</code> and a call to the balancer
922   *         to populate the assigns with targets chosen using round-robin (default balancer
923   *         scheme). If at assign-time, the target chosen is no longer up, thats fine, the
924   *         AssignProcedure will ask the balancer for a new target, and so on.
925   */
926  public TransitRegionStateProcedure[] createRoundRobinAssignProcedures(List<RegionInfo> hris,
927    List<ServerName> serversToExclude) {
928    if (hris.isEmpty()) {
929      return new TransitRegionStateProcedure[0];
930    }
931
932    if (
933      serversToExclude != null && this.master.getServerManager().getOnlineServersList().size() == 1
934    ) {
935      LOG.debug("Only one region server found and hence going ahead with the assignment");
936      serversToExclude = null;
937    }
938    try {
939      // Ask the balancer to assign our regions. Pass the regions en masse. The balancer can do
940      // a better job if it has all the assignments in the one lump.
941      Map<ServerName, List<RegionInfo>> assignments = getBalancer().roundRobinAssignment(hris,
942        this.master.getServerManager().createDestinationServersList(serversToExclude));
943      // Return mid-method!
944      return createAssignProcedures(assignments);
945    } catch (IOException hioe) {
946      LOG.warn("Failed roundRobinAssignment", hioe);
947    }
948    // If an error above, fall-through to this simpler assign. Last resort.
949    return createAssignProcedures(hris);
950  }
951
952  /**
953   * Create round-robin assigns. Use on table creation to distribute out regions across cluster.
954   * @return AssignProcedures made out of the passed in <code>hris</code> and a call to the balancer
955   *         to populate the assigns with targets chosen using round-robin (default balancer
956   *         scheme). If at assign-time, the target chosen is no longer up, thats fine, the
957   *         AssignProcedure will ask the balancer for a new target, and so on.
958   */
959  public TransitRegionStateProcedure[] createRoundRobinAssignProcedures(List<RegionInfo> hris) {
960    return createRoundRobinAssignProcedures(hris, null);
961  }
962
963  static int compare(TransitRegionStateProcedure left, TransitRegionStateProcedure right) {
964    if (left.getRegion().isMetaRegion()) {
965      if (right.getRegion().isMetaRegion()) {
966        return RegionInfo.COMPARATOR.compare(left.getRegion(), right.getRegion());
967      }
968      return -1;
969    } else if (right.getRegion().isMetaRegion()) {
970      return +1;
971    }
972    if (left.getRegion().getTable().isSystemTable()) {
973      if (right.getRegion().getTable().isSystemTable()) {
974        return RegionInfo.COMPARATOR.compare(left.getRegion(), right.getRegion());
975      }
976      return -1;
977    } else if (right.getRegion().getTable().isSystemTable()) {
978      return +1;
979    }
980    return RegionInfo.COMPARATOR.compare(left.getRegion(), right.getRegion());
981  }
982
983  /**
984   * Create one TransitRegionStateProcedure to assign a region w/o specifying a target server. This
985   * method is called from HBCK2.
986   * @return an assign or null
987   */
988  public TransitRegionStateProcedure createOneAssignProcedure(RegionInfo ri, boolean override,
989    boolean force) {
990    TransitRegionStateProcedure trsp = null;
991    try {
992      trsp = createAssignProcedure(ri, null, override, force);
993    } catch (IOException ioe) {
994      LOG.info(
995        "Failed {} assign, override={}"
996          + (override ? "" : "; set override to by-pass state checks."),
997        ri.getEncodedName(), override, ioe);
998    }
999    return trsp;
1000  }
1001
1002  /**
1003   * Create one TransitRegionStateProcedure to unassign a region. This method is called from HBCK2.
1004   * @return an unassign or null
1005   */
1006  public TransitRegionStateProcedure createOneUnassignProcedure(RegionInfo ri, boolean override,
1007    boolean force) {
1008    RegionStateNode regionNode = regionStates.getOrCreateRegionStateNode(ri);
1009    TransitRegionStateProcedure trsp = null;
1010    regionNode.lock();
1011    try {
1012      if (override) {
1013        if (!force) {
1014          preTransitCheck(regionNode, STATES_EXPECTED_ON_UNASSIGN_OR_MOVE);
1015        }
1016        if (regionNode.getProcedure() != null) {
1017          regionNode.unsetProcedure(regionNode.getProcedure());
1018        }
1019      } else {
1020        // This is where we could throw an exception; i.e. override is false.
1021        preTransitCheck(regionNode, STATES_EXPECTED_ON_UNASSIGN_OR_MOVE);
1022      }
1023      assert regionNode.getProcedure() == null;
1024      trsp =
1025        TransitRegionStateProcedure.unassign(getProcedureEnvironment(), regionNode.getRegionInfo());
1026      regionNode.setProcedure(trsp);
1027    } catch (IOException ioe) {
1028      // 'override' must be false here.
1029      LOG.info("Failed {} unassign, override=false; set override to by-pass state checks.",
1030        ri.getEncodedName(), ioe);
1031    } finally {
1032      regionNode.unlock();
1033    }
1034    return trsp;
1035  }
1036
1037  /**
1038   * Create an array of TransitRegionStateProcedure w/o specifying a target server. Used as fallback
1039   * of caller is unable to do {@link #createAssignProcedures(Map)}.
1040   * <p/>
1041   * If no target server, at assign time, we will try to use the former location of the region if
1042   * one exists. This is how we 'retain' the old location across a server restart.
1043   * <p/>
1044   * Should only be called when you can make sure that no one can touch these regions other than
1045   * you. For example, when you are creating or enabling table. Presumes all Regions are in
1046   * appropriate state ripe for assign; no checking of Region state is done in here.
1047   * @see #createAssignProcedures(Map)
1048   */
1049  public TransitRegionStateProcedure[] createAssignProcedures(List<RegionInfo> hris) {
1050    return hris.stream().map(hri -> regionStates.getOrCreateRegionStateNode(hri))
1051      .map(regionNode -> createAssignProcedure(regionNode, null)).sorted(AssignmentManager::compare)
1052      .toArray(TransitRegionStateProcedure[]::new);
1053  }
1054
1055  /**
1056   * Tied to {@link #createAssignProcedures(List)} in that it is called if caller is unable to run
1057   * this method. Presumes all Regions are in appropriate state ripe for assign; no checking of
1058   * Region state is done in here.
1059   * @param assignments Map of assignments from which we produce an array of AssignProcedures.
1060   * @return Assignments made from the passed in <code>assignments</code>
1061   * @see #createAssignProcedures(List)
1062   */
1063  private TransitRegionStateProcedure[]
1064    createAssignProcedures(Map<ServerName, List<RegionInfo>> assignments) {
1065    return assignments.entrySet().stream()
1066      .flatMap(e -> e.getValue().stream().map(hri -> regionStates.getOrCreateRegionStateNode(hri))
1067        .map(regionNode -> createAssignProcedure(regionNode, e.getKey())))
1068      .sorted(AssignmentManager::compare).toArray(TransitRegionStateProcedure[]::new);
1069  }
1070
1071  // for creating unassign TRSP when disabling a table or closing excess region replicas
1072  private TransitRegionStateProcedure forceCreateUnssignProcedure(RegionStateNode regionNode) {
1073    regionNode.lock();
1074    try {
1075      if (regionNode.isInState(State.OFFLINE, State.CLOSED, State.SPLIT)) {
1076        return null;
1077      }
1078      // in general, a split parent should be in CLOSED or SPLIT state, but anyway, let's check it
1079      // here for safety
1080      if (regionNode.getRegionInfo().isSplit()) {
1081        LOG.warn("{} is a split parent but not in CLOSED or SPLIT state", regionNode);
1082        return null;
1083      }
1084      // As in DisableTableProcedure or ModifyTableProcedure, we will hold the xlock for table, so
1085      // we can make sure that this procedure has not been executed yet, as TRSP will hold the
1086      // shared lock for table all the time. So here we will unset it and when it is actually
1087      // executed, it will find that the attach procedure is not itself and quit immediately.
1088      if (regionNode.getProcedure() != null) {
1089        regionNode.unsetProcedure(regionNode.getProcedure());
1090      }
1091      return regionNode.setProcedure(TransitRegionStateProcedure.unassign(getProcedureEnvironment(),
1092        regionNode.getRegionInfo()));
1093    } finally {
1094      regionNode.unlock();
1095    }
1096  }
1097
1098  /**
1099   * Called by DisableTableProcedure to unassign all the regions for a table.
1100   */
1101  public TransitRegionStateProcedure[] createUnassignProceduresForDisabling(TableName tableName) {
1102    return regionStates.getTableRegionStateNodes(tableName).stream()
1103      .map(this::forceCreateUnssignProcedure).filter(p -> p != null)
1104      .toArray(TransitRegionStateProcedure[]::new);
1105  }
1106
1107  private int submitUnassignProcedure(TableName tableName,
1108    Function<RegionStateNode, Boolean> shouldSubmit, Consumer<RegionStateNode> logRIT,
1109    Consumer<TransitRegionStateProcedure> submit) {
1110    int inTransitionCount = 0;
1111    for (RegionStateNode regionNode : regionStates.getTableRegionStateNodes(tableName)) {
1112      regionNode.lock();
1113      try {
1114        if (shouldSubmit.apply(regionNode)) {
1115          if (regionNode.isTransitionScheduled()) {
1116            logRIT.accept(regionNode);
1117            inTransitionCount++;
1118            continue;
1119          }
1120          if (regionNode.isInState(State.OFFLINE, State.CLOSED, State.SPLIT)) {
1121            continue;
1122          }
1123          submit.accept(regionNode.setProcedure(TransitRegionStateProcedure
1124            .unassign(getProcedureEnvironment(), regionNode.getRegionInfo())));
1125        }
1126      } finally {
1127        regionNode.unlock();
1128      }
1129    }
1130    return inTransitionCount;
1131  }
1132
1133  /**
1134   * Called by DisableTableProcedure to unassign all regions for a table. Will skip submit unassign
1135   * procedure if the region is in transition, so you may need to call this method multiple times.
1136   * @param tableName the table for closing excess region replicas
1137   * @param submit    for submitting procedure
1138   * @return the number of regions in transition that we can not schedule unassign procedures
1139   */
1140  public int submitUnassignProcedureForDisablingTable(TableName tableName,
1141    Consumer<TransitRegionStateProcedure> submit) {
1142    return submitUnassignProcedure(tableName, rn -> true,
1143      rn -> LOG.debug("skip scheduling unassign procedure for {} when closing table regions "
1144        + "for disabling since it is in transition", rn),
1145      submit);
1146  }
1147
1148  /**
1149   * Called by ModifyTableProcedure to unassign all the excess region replicas for a table. Will
1150   * skip submit unassign procedure if the region is in transition, so you may need to call this
1151   * method multiple times.
1152   * @param tableName       the table for closing excess region replicas
1153   * @param newReplicaCount the new replica count, should be less than current replica count
1154   * @param submit          for submitting procedure
1155   * @return the number of regions in transition that we can not schedule unassign procedures
1156   */
1157  public int submitUnassignProcedureForClosingExcessRegionReplicas(TableName tableName,
1158    int newReplicaCount, Consumer<TransitRegionStateProcedure> submit) {
1159    return submitUnassignProcedure(tableName,
1160      rn -> rn.getRegionInfo().getReplicaId() >= newReplicaCount,
1161      rn -> LOG.debug("skip scheduling unassign procedure for {} when closing excess region "
1162        + "replicas since it is in transition", rn),
1163      submit);
1164  }
1165
1166  private int numberOfUnclosedRegions(TableName tableName,
1167    Function<RegionStateNode, Boolean> shouldSubmit) {
1168    int unclosed = 0;
1169    for (RegionStateNode regionNode : regionStates.getTableRegionStateNodes(tableName)) {
1170      regionNode.lock();
1171      try {
1172        if (shouldSubmit.apply(regionNode)) {
1173          if (!regionNode.isInState(State.OFFLINE, State.CLOSED, State.SPLIT)) {
1174            unclosed++;
1175          }
1176        }
1177      } finally {
1178        regionNode.unlock();
1179      }
1180    }
1181    return unclosed;
1182  }
1183
1184  public int numberOfUnclosedRegionsForDisabling(TableName tableName) {
1185    return numberOfUnclosedRegions(tableName, rn -> true);
1186  }
1187
1188  public int numberOfUnclosedExcessRegionReplicas(TableName tableName, int newReplicaCount) {
1189    return numberOfUnclosedRegions(tableName,
1190      rn -> rn.getRegionInfo().getReplicaId() >= newReplicaCount);
1191  }
1192
1193  public SplitTableRegionProcedure createSplitProcedure(final RegionInfo regionToSplit,
1194    final byte[] splitKey) throws IOException {
1195    return new SplitTableRegionProcedure(getProcedureEnvironment(), regionToSplit, splitKey);
1196  }
1197
1198  public TruncateRegionProcedure createTruncateRegionProcedure(final RegionInfo regionToTruncate)
1199    throws IOException {
1200    return new TruncateRegionProcedure(getProcedureEnvironment(), regionToTruncate);
1201  }
1202
1203  public MergeTableRegionsProcedure createMergeProcedure(RegionInfo... ris) throws IOException {
1204    return new MergeTableRegionsProcedure(getProcedureEnvironment(), ris, false);
1205  }
1206
1207  /**
1208   * Delete the region states. This is called by "DeleteTable"
1209   */
1210  public void deleteTable(final TableName tableName) throws IOException {
1211    final ArrayList<RegionInfo> regions = regionStates.getTableRegionsInfo(tableName);
1212    regionStateStore.deleteRegions(regions);
1213    for (int i = 0; i < regions.size(); ++i) {
1214      final RegionInfo regionInfo = regions.get(i);
1215      regionStates.deleteRegion(regionInfo);
1216    }
1217  }
1218
1219  // ============================================================================================
1220  // RS Region Transition Report helpers
1221  // ============================================================================================
1222  private void reportRegionStateTransition(ReportRegionStateTransitionResponse.Builder builder,
1223    ServerStateNode serverNode, List<RegionStateTransition> transitionList) throws IOException {
1224    for (RegionStateTransition transition : transitionList) {
1225      switch (transition.getTransitionCode()) {
1226        case OPENED:
1227        case FAILED_OPEN:
1228        case CLOSED:
1229          assert transition.getRegionInfoCount() == 1 : transition;
1230          final RegionInfo hri = ProtobufUtil.toRegionInfo(transition.getRegionInfo(0));
1231          long procId =
1232            transition.getProcIdCount() > 0 ? transition.getProcId(0) : Procedure.NO_PROC_ID;
1233          long seqId =
1234            transition.hasOpenSeqNum() ? transition.getOpenSeqNum() : HConstants.NO_SEQNUM;
1235          // On CLOSE, seed the master's flushed-seqid watermark with the region's final flushed
1236          // seqid (reported by the RS). This complements the OPEN-time seed (HBASE-30335) and
1237          // narrows the graceful-close window where a WAL split of a crashed source RS could
1238          // write orphaned recovered.edits for already-durable edits. Uses the same monotonic
1239          // merge(Math::max) seed, so it never regresses a higher value.
1240          if (transition.getTransitionCode() == TransitionCode.CLOSED && seqId >= 0) {
1241            master.getServerManager().advanceRegionFlushedSeq(hri, seqId);
1242          }
1243          updateRegionTransition(serverNode, transition.getTransitionCode(), hri, seqId, procId);
1244          break;
1245        case READY_TO_SPLIT:
1246        case SPLIT:
1247        case SPLIT_REVERTED:
1248          assert transition.getRegionInfoCount() == 3 : transition;
1249          final RegionInfo parent = ProtobufUtil.toRegionInfo(transition.getRegionInfo(0));
1250          final RegionInfo splitA = ProtobufUtil.toRegionInfo(transition.getRegionInfo(1));
1251          final RegionInfo splitB = ProtobufUtil.toRegionInfo(transition.getRegionInfo(2));
1252          updateRegionSplitTransition(serverNode, transition.getTransitionCode(), parent, splitA,
1253            splitB);
1254          break;
1255        case READY_TO_MERGE:
1256        case MERGED:
1257        case MERGE_REVERTED:
1258          assert transition.getRegionInfoCount() == 3 : transition;
1259          final RegionInfo merged = ProtobufUtil.toRegionInfo(transition.getRegionInfo(0));
1260          final RegionInfo mergeA = ProtobufUtil.toRegionInfo(transition.getRegionInfo(1));
1261          final RegionInfo mergeB = ProtobufUtil.toRegionInfo(transition.getRegionInfo(2));
1262          updateRegionMergeTransition(serverNode, transition.getTransitionCode(), merged, mergeA,
1263            mergeB);
1264          break;
1265      }
1266    }
1267  }
1268
1269  public ReportRegionStateTransitionResponse reportRegionStateTransition(
1270    final ReportRegionStateTransitionRequest req) throws PleaseHoldException {
1271    ReportRegionStateTransitionResponse.Builder builder =
1272      ReportRegionStateTransitionResponse.newBuilder();
1273    ServerName serverName = ProtobufUtil.toServerName(req.getServer());
1274    ServerStateNode serverNode = regionStates.getServerNode(serverName);
1275    if (serverNode == null) {
1276      LOG.warn("No server node for {}", serverName);
1277      builder.setErrorMessage("No server node for " + serverName);
1278      return builder.build();
1279    }
1280    // here we have to acquire a read lock instead of a simple exclusive lock. This is because that
1281    // we should not block other reportRegionStateTransition call from the same region server. This
1282    // is not only about performance, but also to prevent dead lock. Think of the meta region is
1283    // also on the same region server and you hold the lock which blocks the
1284    // reportRegionStateTransition for meta, and since meta is not online, you will block inside the
1285    // lock protection to wait for meta online...
1286    serverNode.readLock().lock();
1287    try {
1288      // we only accept reportRegionStateTransition if the region server is online, see the comment
1289      // above in submitServerCrash method and HBASE-21508 for more details.
1290      if (serverNode.isInState(ServerState.ONLINE)) {
1291        try {
1292          reportRegionStateTransition(builder, serverNode, req.getTransitionList());
1293        } catch (PleaseHoldException e) {
1294          LOG.trace("Failed transition ", e);
1295          throw e;
1296        } catch (UnsupportedOperationException | IOException e) {
1297          // TODO: at the moment we have a single error message and the RS will abort
1298          // if the master says that one of the region transitions failed.
1299          LOG.warn("Failed transition", e);
1300          builder.setErrorMessage("Failed transition " + e.getMessage());
1301        }
1302      } else {
1303        LOG.warn("The region server {} is already dead, skip reportRegionStateTransition call",
1304          serverName);
1305        builder.setErrorMessage("You are dead");
1306      }
1307    } finally {
1308      serverNode.readLock().unlock();
1309    }
1310
1311    return builder.build();
1312  }
1313
1314  private void updateRegionTransition(ServerStateNode serverNode, TransitionCode state,
1315    RegionInfo regionInfo, long seqId, long procId) throws IOException {
1316    checkMetaLoaded(regionInfo, procId);
1317
1318    RegionStateNode regionNode = regionStates.getRegionStateNode(regionInfo);
1319    if (regionNode == null) {
1320      // the table/region is gone. maybe a delete, split, merge
1321      throw new UnexpectedStateException(String.format(
1322        "Server %s was trying to transition region %s to %s. but Region is not known.",
1323        serverNode.getServerName(), regionInfo, state));
1324    }
1325    LOG.trace("Update region transition serverName={} region={} regionState={}",
1326      serverNode.getServerName(), regionNode, state);
1327
1328    regionNode.lock();
1329    try {
1330      if (!reportTransition(regionNode, serverNode, state, seqId, procId)) {
1331        // Don't log WARN if shutting down cluster; during shutdown. Avoid the below messages:
1332        // 2018-08-13 10:45:10,551 WARN ...AssignmentManager: No matching procedure found for
1333        // rit=OPEN, location=ve0538.halxg.cloudera.com,16020,1533493000958,
1334        // table=IntegrationTestBigLinkedList, region=65ab289e2fc1530df65f6c3d7cde7aa5 transition
1335        // to CLOSED
1336        // These happen because on cluster shutdown, we currently let the RegionServers close
1337        // regions. This is the only time that region close is not run by the Master (so cluster
1338        // goes down fast). Consider changing it so Master runs all shutdowns.
1339        if (
1340          this.master.getServerManager().isClusterShutdown() && state.equals(TransitionCode.CLOSED)
1341        ) {
1342          LOG.info("RegionServer {} {}", state, regionNode.getRegionInfo().getEncodedName());
1343        } else {
1344          LOG.warn("No matching procedure found for {} transition on {} to {}",
1345            serverNode.getServerName(), regionNode, state);
1346        }
1347      }
1348    } finally {
1349      regionNode.unlock();
1350    }
1351  }
1352
1353  private boolean reportTransition(RegionStateNode regionNode, ServerStateNode serverNode,
1354    TransitionCode state, long seqId, long procId) throws IOException {
1355    ServerName serverName = serverNode.getServerName();
1356    TransitRegionStateProcedure proc = regionNode.getProcedure();
1357    if (proc == null) {
1358      return false;
1359    }
1360    proc.reportTransition(master.getMasterProcedureExecutor().getEnvironment(), regionNode,
1361      serverName, state, seqId, procId);
1362    return true;
1363  }
1364
1365  private void updateRegionSplitTransition(final ServerStateNode serverNode,
1366    final TransitionCode state, final RegionInfo parent, final RegionInfo hriA,
1367    final RegionInfo hriB) throws IOException {
1368    checkMetaLoaded(parent, Procedure.NO_PROC_ID);
1369
1370    if (state != TransitionCode.READY_TO_SPLIT) {
1371      throw new UnexpectedStateException(
1372        "unsupported split regionState=" + state + " for parent region " + parent
1373          + " maybe an old RS (< 2.0) had the operation in progress");
1374    }
1375
1376    // sanity check on the request
1377    if (!Bytes.equals(hriA.getEndKey(), hriB.getStartKey())) {
1378      throw new UnsupportedOperationException("unsupported split request with bad keys: parent="
1379        + parent + " hriA=" + hriA + " hriB=" + hriB);
1380    }
1381
1382    if (!master.isSplitOrMergeEnabled(MasterSwitchType.SPLIT)) {
1383      LOG.warn("Split switch is off! skip split of " + parent);
1384      throw new DoNotRetryIOException(
1385        "Split region " + parent.getRegionNameAsString() + " failed due to split switch off");
1386    }
1387
1388    // Submit the Split procedure
1389    final byte[] splitKey = hriB.getStartKey();
1390    if (LOG.isDebugEnabled()) {
1391      LOG.debug("Split request from {}, parent={}, splitKey={}", serverNode.getServerName(), parent,
1392        Bytes.toStringBinary(splitKey));
1393    }
1394    // Processing this report happens asynchronously from other activities which can mutate
1395    // the region state. For example, a split procedure may already be running for this parent.
1396    // A split procedure cannot succeed if the parent region is no longer open, so we can
1397    // ignore it in that case.
1398    // Note that submitting more than one split procedure for a given region is
1399    // harmless -- the split is fenced in the procedure handling -- but it would be noisy in
1400    // the logs. Only one procedure can succeed. The other procedure(s) would abort during
1401    // initialization and report failure with WARN level logging.
1402    RegionState parentState = regionStates.getRegionState(parent);
1403    if (parentState != null && parentState.isOpened()) {
1404      master.getMasterProcedureExecutor().submitProcedure(createSplitProcedure(parent, splitKey));
1405    } else {
1406      LOG.info("Ignoring split request from {}, parent={} because parent is unknown or not open",
1407        serverNode.getServerName(), parent);
1408      return;
1409    }
1410
1411    // If the RS is < 2.0 throw an exception to abort the operation, we are handling the split
1412    if (master.getServerManager().getVersionNumber(serverNode.getServerName()) < 0x0200000) {
1413      throw new UnsupportedOperationException(
1414        String.format("Split handled by the master: " + "parent=%s hriA=%s hriB=%s",
1415          parent.getShortNameToLog(), hriA, hriB));
1416    }
1417  }
1418
1419  private void updateRegionMergeTransition(final ServerStateNode serverNode,
1420    final TransitionCode state, final RegionInfo merged, final RegionInfo hriA,
1421    final RegionInfo hriB) throws IOException {
1422    checkMetaLoaded(merged, Procedure.NO_PROC_ID);
1423
1424    if (state != TransitionCode.READY_TO_MERGE) {
1425      throw new UnexpectedStateException(
1426        "Unsupported merge regionState=" + state + " for regionA=" + hriA + " regionB=" + hriB
1427          + " merged=" + merged + " maybe an old RS (< 2.0) had the operation in progress");
1428    }
1429
1430    if (!master.isSplitOrMergeEnabled(MasterSwitchType.MERGE)) {
1431      LOG.warn("Merge switch is off! skip merge of regionA=" + hriA + " regionB=" + hriB);
1432      throw new DoNotRetryIOException(
1433        "Merge of regionA=" + hriA + " regionB=" + hriB + " failed because merge switch is off");
1434    }
1435
1436    // Submit the Merge procedure
1437    if (LOG.isDebugEnabled()) {
1438      LOG.debug("Handling merge request from RS=" + merged + ", merged=" + merged);
1439    }
1440    master.getMasterProcedureExecutor().submitProcedure(createMergeProcedure(hriA, hriB));
1441
1442    // If the RS is < 2.0 throw an exception to abort the operation, we are handling the merge
1443    if (master.getServerManager().getVersionNumber(serverNode.getServerName()) < 0x0200000) {
1444      throw new UnsupportedOperationException(
1445        String.format("Merge not handled yet: regionState=%s merged=%s hriA=%s hriB=%s", state,
1446          merged, hriA, hriB));
1447    }
1448  }
1449
1450  // ============================================================================================
1451  // RS Status update (report online regions) helpers
1452  // ============================================================================================
1453  /**
1454   * The master will call this method when the RS send the regionServerReport(). The report will
1455   * contains the "online regions". This method will check the the online regions against the
1456   * in-memory state of the AM, and we will log a warn message if there is a mismatch. This is
1457   * because that there is no fencing between the reportRegionStateTransition method and
1458   * regionServerReport method, so there could be race and introduce inconsistency here, but
1459   * actually there is no problem.
1460   * <p/>
1461   * Please see HBASE-21421 and HBASE-21463 for more details.
1462   */
1463  public void reportOnlineRegions(ServerName serverName, Set<byte[]> regionNames) {
1464    if (!isRunning()) {
1465      return;
1466    }
1467    if (LOG.isTraceEnabled()) {
1468      LOG.trace("ReportOnlineRegions {} regionCount={}, metaLoaded={} {}", serverName,
1469        regionNames.size(), isMetaLoaded(),
1470        regionNames.stream().map(Bytes::toStringBinary).collect(Collectors.toList()));
1471    }
1472
1473    ServerStateNode serverNode = regionStates.getServerNode(serverName);
1474    if (serverNode == null) {
1475      LOG.warn("Got a report from server {} where its server node is null", serverName);
1476      return;
1477    }
1478    serverNode.readLock().lock();
1479    try {
1480      if (!serverNode.isInState(ServerState.ONLINE)) {
1481        LOG.warn("Got a report from a server result in state {}", serverNode);
1482        return;
1483      }
1484    } finally {
1485      serverNode.readLock().unlock();
1486    }
1487
1488    // Track the regionserver reported online regions in memory.
1489    synchronized (rsReports) {
1490      rsReports.put(serverName, regionNames);
1491    }
1492
1493    if (regionNames.isEmpty()) {
1494      // nothing to do if we don't have regions
1495      LOG.trace("no online region found on {}", serverName);
1496      return;
1497    }
1498    if (!isMetaLoaded()) {
1499      // we are still on startup, skip checking
1500      return;
1501    }
1502    // The Heartbeat tells us of what regions are on the region serve, check the state.
1503    checkOnlineRegionsReport(serverNode, regionNames);
1504  }
1505
1506  /**
1507   * Close <code>regionName</code> on <code>sn</code> silently and immediately without using a
1508   * Procedure or going via hbase:meta. For case where a RegionServer's hosting of a Region is not
1509   * aligned w/ the Master's accounting of Region state. This is for cleaning up an error in
1510   * accounting.
1511   */
1512  private void closeRegionSilently(ServerName sn, byte[] regionName) {
1513    try {
1514      RegionInfo ri = CatalogFamilyFormat.parseRegionInfoFromRegionName(regionName);
1515      // Pass -1 for timeout. Means do not wait.
1516      ServerManager.closeRegionSilentlyAndWait(this.master.getAsyncClusterConnection(), sn, ri, -1);
1517    } catch (Exception e) {
1518      LOG.error("Failed trying to close {} on {}", Bytes.toStringBinary(regionName), sn, e);
1519    }
1520  }
1521
1522  /**
1523   * Check that what the RegionServer reports aligns with the Master's image. If disagreement, we
1524   * will tell the RegionServer to expediently close a Region we do not think it should have.
1525   */
1526  private void checkOnlineRegionsReport(ServerStateNode serverNode, Set<byte[]> regionNames) {
1527    ServerName serverName = serverNode.getServerName();
1528    for (byte[] regionName : regionNames) {
1529      if (!isRunning()) {
1530        return;
1531      }
1532      RegionStateNode regionNode = regionStates.getRegionStateNodeFromName(regionName);
1533      if (regionNode == null) {
1534        String regionNameAsStr = Bytes.toStringBinary(regionName);
1535        LOG.warn("No RegionStateNode for {} but reported as up on {}; closing...", regionNameAsStr,
1536          serverName);
1537        closeRegionSilently(serverNode.getServerName(), regionName);
1538        continue;
1539      }
1540      final long lag = 1000;
1541      // This is just a fallback check designed to identify unexpected data inconsistencies, so we
1542      // use tryLock to attempt to acquire the lock, and if the lock cannot be acquired, we skip the
1543      // check. This will not cause any additional problems and also prevents the regionServerReport
1544      // call from being stuck for too long which may cause deadlock on region assignment.
1545      if (regionNode.tryLock()) {
1546        try {
1547          long diff = EnvironmentEdgeManager.currentTime() - regionNode.getLastUpdate();
1548          if (regionNode.isInState(State.OPENING, State.OPEN)) {
1549            // This is possible as a region server has just closed a region but the region server
1550            // report is generated before the closing, but arrive after the closing. Make sure
1551            // there
1552            // is some elapsed time so less false alarms.
1553            if (!regionNode.getRegionLocation().equals(serverName) && diff > lag) {
1554              LOG.warn("Reporting {} server does not match {} (time since last "
1555                + "update={}ms); closing...", serverName, regionNode, diff);
1556              closeRegionSilently(serverNode.getServerName(), regionName);
1557            }
1558          } else if (!regionNode.isInState(State.CLOSING, State.SPLITTING)) {
1559            // So, we can get report that a region is CLOSED or SPLIT because a heartbeat
1560            // came in at about same time as a region transition. Make sure there is some
1561            // elapsed time so less false alarms.
1562            if (diff > lag) {
1563              LOG.warn("Reporting {} state does not match {} (time since last update={}ms)",
1564                serverName, regionNode, diff);
1565            }
1566          }
1567        } finally {
1568          regionNode.unlock();
1569        }
1570      } else {
1571        LOG.warn(
1572          "Unable to acquire lock for regionNode {}. It is likely that another thread is currently holding the lock. To avoid deadlock, skip execution for now.",
1573          regionNode);
1574      }
1575    }
1576  }
1577
1578  // ============================================================================================
1579  // RIT chore
1580  // ============================================================================================
1581  private static class RegionInTransitionChore extends ProcedureInMemoryChore<MasterProcedureEnv> {
1582    public RegionInTransitionChore(final int timeoutMsec) {
1583      super(timeoutMsec);
1584    }
1585
1586    @Override
1587    protected void periodicExecute(final MasterProcedureEnv env) {
1588      final AssignmentManager am = env.getAssignmentManager();
1589
1590      final RegionInTransitionStat ritStat = am.computeRegionInTransitionStat();
1591      if (ritStat.hasRegionsOverThreshold()) {
1592        for (RegionState hri : ritStat.getRegionOverThreshold()) {
1593          am.handleRegionOverStuckWarningThreshold(hri.getRegion());
1594        }
1595      }
1596
1597      // update metrics
1598      am.updateRegionsInTransitionMetrics(ritStat);
1599    }
1600  }
1601
1602  private static class DeadServerMetricRegionChore
1603    extends ProcedureInMemoryChore<MasterProcedureEnv> {
1604    public DeadServerMetricRegionChore(final int timeoutMsec) {
1605      super(timeoutMsec);
1606    }
1607
1608    @Override
1609    protected void periodicExecute(final MasterProcedureEnv env) {
1610      final ServerManager sm = env.getMasterServices().getServerManager();
1611      final AssignmentManager am = env.getAssignmentManager();
1612      // To minimize inconsistencies we are not going to snapshot live servers in advance in case
1613      // new servers are added; OTOH we don't want to add heavy sync for a consistent view since
1614      // this is for metrics. Instead, we're going to check each regions as we go; to avoid making
1615      // too many checks, we maintain a local lists of server, limiting us to false negatives. If
1616      // we miss some recently-dead server, we'll just see it next time.
1617      Set<ServerName> recentlyLiveServers = new HashSet<>();
1618      int deadRegions = 0, unknownRegions = 0;
1619      for (RegionStateNode rsn : am.getRegionStates().getRegionStateNodes()) {
1620        if (rsn.getState() != State.OPEN) {
1621          continue; // Opportunistic check, should quickly skip RITs, offline tables, etc.
1622        }
1623        // Do not need to acquire region state lock as this is only for showing metrics.
1624        ServerName sn = rsn.getRegionLocation();
1625        State state = rsn.getState();
1626        if (state != State.OPEN) {
1627          continue; // Mostly skipping RITs that are already being take care of.
1628        }
1629        if (sn == null) {
1630          ++unknownRegions; // Opened on null?
1631          continue;
1632        }
1633        if (recentlyLiveServers.contains(sn)) {
1634          continue;
1635        }
1636        ServerManager.ServerLiveState sls = sm.isServerKnownAndOnline(sn);
1637        switch (sls) {
1638          case LIVE:
1639            recentlyLiveServers.add(sn);
1640            break;
1641          case DEAD:
1642            ++deadRegions;
1643            break;
1644          case UNKNOWN:
1645            ++unknownRegions;
1646            break;
1647          default:
1648            throw new AssertionError("Unexpected " + sls);
1649        }
1650      }
1651      if (deadRegions > 0 || unknownRegions > 0) {
1652        LOG.info("Found {} OPEN regions on dead servers and {} OPEN regions on unknown servers",
1653          deadRegions, unknownRegions);
1654      }
1655
1656      am.updateDeadServerRegionMetrics(deadRegions, unknownRegions);
1657    }
1658  }
1659
1660  public RegionInTransitionStat computeRegionInTransitionStat() {
1661    final RegionInTransitionStat rit = new RegionInTransitionStat(getConfiguration());
1662    rit.update(this);
1663    return rit;
1664  }
1665
1666  public static class RegionInTransitionStat {
1667    private final int ritThreshold;
1668
1669    private HashMap<String, RegionState> ritsOverThreshold = null;
1670    private long statTimestamp;
1671    private long oldestRITTime = 0;
1672    private int totalRITsTwiceThreshold = 0;
1673    private int totalRITs = 0;
1674
1675    public RegionInTransitionStat(final Configuration conf) {
1676      this.ritThreshold =
1677        conf.getInt(METRICS_RIT_STUCK_WARNING_THRESHOLD, DEFAULT_RIT_STUCK_WARNING_THRESHOLD);
1678    }
1679
1680    public int getRITThreshold() {
1681      return ritThreshold;
1682    }
1683
1684    public long getTimestamp() {
1685      return statTimestamp;
1686    }
1687
1688    public int getTotalRITs() {
1689      return totalRITs;
1690    }
1691
1692    public long getOldestRITTime() {
1693      return oldestRITTime;
1694    }
1695
1696    public int getTotalRITsOverThreshold() {
1697      Map<String, RegionState> m = this.ritsOverThreshold;
1698      return m != null ? m.size() : 0;
1699    }
1700
1701    public boolean hasRegionsTwiceOverThreshold() {
1702      return totalRITsTwiceThreshold > 0;
1703    }
1704
1705    public boolean hasRegionsOverThreshold() {
1706      Map<String, RegionState> m = this.ritsOverThreshold;
1707      return m != null && !m.isEmpty();
1708    }
1709
1710    public Collection<RegionState> getRegionOverThreshold() {
1711      Map<String, RegionState> m = this.ritsOverThreshold;
1712      return m != null ? m.values() : Collections.emptySet();
1713    }
1714
1715    public boolean isRegionOverThreshold(final RegionInfo regionInfo) {
1716      Map<String, RegionState> m = this.ritsOverThreshold;
1717      return m != null && m.containsKey(regionInfo.getEncodedName());
1718    }
1719
1720    public boolean isRegionTwiceOverThreshold(final RegionInfo regionInfo) {
1721      Map<String, RegionState> m = this.ritsOverThreshold;
1722      if (m == null) {
1723        return false;
1724      }
1725      final RegionState state = m.get(regionInfo.getEncodedName());
1726      if (state == null) {
1727        return false;
1728      }
1729      return (statTimestamp - state.getStamp()) > (ritThreshold * 2L);
1730    }
1731
1732    protected void update(final AssignmentManager am) {
1733      this.statTimestamp = EnvironmentEdgeManager.currentTime();
1734      update(am.getRegionsStateInTransition(), statTimestamp);
1735
1736      if (LOG.isDebugEnabled() && ritsOverThreshold != null && !ritsOverThreshold.isEmpty()) {
1737        LOG.debug("RITs over threshold: {}",
1738          ritsOverThreshold.entrySet().stream()
1739            .map(e -> e.getKey() + ":" + e.getValue().getState().name())
1740            .collect(Collectors.joining("\n")));
1741      }
1742    }
1743
1744    private void update(final Collection<RegionState> regions, final long currentTime) {
1745      for (RegionState state : regions) {
1746        totalRITs++;
1747        final long ritStartedMs = state.getStamp();
1748        if (ritStartedMs == 0) {
1749          // Don't output bogus values to metrics if they accidentally make it here.
1750          LOG.warn("The RIT {} has no start time", state.getRegion());
1751          continue;
1752        }
1753        final long ritTime = currentTime - ritStartedMs;
1754        if (ritTime > ritThreshold) {
1755          if (ritsOverThreshold == null) {
1756            ritsOverThreshold = new HashMap<String, RegionState>();
1757          }
1758          ritsOverThreshold.put(state.getRegion().getEncodedName(), state);
1759          totalRITsTwiceThreshold += (ritTime > (ritThreshold * 2L)) ? 1 : 0;
1760        }
1761        if (oldestRITTime < ritTime) {
1762          oldestRITTime = ritTime;
1763        }
1764      }
1765    }
1766  }
1767
1768  private void updateRegionsInTransitionMetrics(final RegionInTransitionStat ritStat) {
1769    metrics.updateRITOldestAge(ritStat.getOldestRITTime());
1770    metrics.updateRITCount(ritStat.getTotalRITs());
1771    metrics.updateRITCountOverThreshold(ritStat.getTotalRITsOverThreshold());
1772  }
1773
1774  private void updateDeadServerRegionMetrics(int deadRegions, int unknownRegions) {
1775    metrics.updateDeadServerOpenRegions(deadRegions);
1776    metrics.updateUnknownServerOpenRegions(unknownRegions);
1777  }
1778
1779  private void handleRegionOverStuckWarningThreshold(final RegionInfo regionInfo) {
1780    final RegionStateNode regionNode = regionStates.getRegionStateNode(regionInfo);
1781    // if (regionNode.isStuck()) {
1782    LOG.warn("STUCK Region-In-Transition {}", regionNode);
1783  }
1784
1785  // ============================================================================================
1786  // TODO: Master load/bootstrap
1787  // ============================================================================================
1788  public void joinCluster() throws IOException {
1789    long startTime = System.nanoTime();
1790    LOG.debug("Joining cluster...");
1791
1792    // Scan hbase:meta to build list of existing regions, servers, and assignment.
1793    // hbase:meta is online now or will be. Inside loadMeta, we keep trying. Can't make progress
1794    // w/o meta.
1795    loadMeta();
1796
1797    while (master.getServerManager().countOfRegionServers() < 1) {
1798      LOG.info("Waiting for RegionServers to join; current count={}",
1799        master.getServerManager().countOfRegionServers());
1800      Threads.sleep(250);
1801    }
1802    LOG.info("Number of RegionServers={}", master.getServerManager().countOfRegionServers());
1803
1804    // Start the chores
1805    master.getMasterProcedureExecutor().addChore(this.ritChore);
1806    master.getMasterProcedureExecutor().addChore(this.deadMetricChore);
1807
1808    long costMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime);
1809    LOG.info("Joined the cluster in {}", StringUtils.humanTimeDiff(costMs));
1810  }
1811
1812  /**
1813   * Create assign procedure for offline regions. Just follow the old
1814   * processofflineServersWithOnlineRegions method. Since now we do not need to deal with dead
1815   * server any more, we only deal with the regions in OFFLINE state in this method. And this is a
1816   * bit strange, that for new regions, we will add it in CLOSED state instead of OFFLINE state, and
1817   * usually there will be a procedure to track them. The processofflineServersWithOnlineRegions is
1818   * a legacy from long ago, as things are going really different now, maybe we do not need this
1819   * method any more. Need to revisit later.
1820   */
1821  // Public so can be run by the Master as part of the startup. Needs hbase:meta to be online.
1822  // Needs to be done after the table state manager has been started.
1823  public void processOfflineRegions() {
1824    TransitRegionStateProcedure[] procs =
1825      regionStates.getRegionStateNodes().stream().filter(rsn -> rsn.isInState(State.OFFLINE))
1826        .filter(rsn -> isTableEnabled(rsn.getRegionInfo().getTable())).map(rsn -> {
1827          rsn.lock();
1828          try {
1829            if (rsn.getProcedure() != null) {
1830              return null;
1831            } else {
1832              return rsn.setProcedure(TransitRegionStateProcedure.assign(getProcedureEnvironment(),
1833                rsn.getRegionInfo(), null));
1834            }
1835          } finally {
1836            rsn.unlock();
1837          }
1838        }).filter(p -> p != null).toArray(TransitRegionStateProcedure[]::new);
1839    if (procs.length > 0) {
1840      master.getMasterProcedureExecutor().submitProcedures(procs);
1841    }
1842  }
1843
1844  /*
1845   * AM internal RegionStateStore.RegionStateVisitor implementation. To be used when scanning META
1846   * table for region rows, using RegionStateStore utility methods. RegionStateStore methods will
1847   * convert Result into proper RegionInfo instances, but those would still need to be added into
1848   * AssignmentManager.regionStates in-memory cache. RegionMetaLoadingVisitor.visitRegionState
1849   * method provides the logic for adding RegionInfo instances as loaded from latest META scan into
1850   * AssignmentManager.regionStates.
1851   */
1852  private class RegionMetaLoadingVisitor implements RegionStateStore.RegionStateVisitor {
1853
1854    @Override
1855    public void visitRegionState(Result result, final RegionInfo regionInfo, final State state,
1856      final ServerName regionLocation, final ServerName lastHost, final long openSeqNum) {
1857      if (
1858        state == null && regionLocation == null && lastHost == null
1859          && openSeqNum == SequenceId.NO_SEQUENCE_ID
1860      ) {
1861        // This is a row with nothing in it.
1862        LOG.warn("Skipping empty row={}", result);
1863        return;
1864      }
1865      State localState = state;
1866      if (localState == null) {
1867        // No region state column data in hbase:meta table! Are I doing a rolling upgrade from
1868        // hbase1 to hbase2? Am I restoring a SNAPSHOT or otherwise adding a region to hbase:meta?
1869        // In any of these cases, state is empty. For now, presume OFFLINE but there are probably
1870        // cases where we need to probe more to be sure this correct; TODO informed by experience.
1871        LOG.info(regionInfo.getEncodedName() + " regionState=null; presuming " + State.OFFLINE);
1872        localState = State.OFFLINE;
1873      }
1874      RegionStateNode regionNode = regionStates.getOrCreateRegionStateNode(regionInfo);
1875      // Do not need to lock on regionNode, as we can make sure that before we finish loading
1876      // meta, all the related procedures can not be executed. The only exception is for meta
1877      // region related operations, but here we do not load the informations for meta region.
1878      regionNode.setState(localState);
1879      regionNode.setLastHost(lastHost);
1880      regionNode.setRegionLocation(regionLocation);
1881      regionNode.setOpenSeqNum(openSeqNum);
1882
1883      // Note: keep consistent with other methods, see region(Opening|Opened|Closing)
1884      // RIT/ServerCrash handling should take care of the transiting regions.
1885      if (
1886        localState.matches(State.OPEN, State.OPENING, State.CLOSING, State.SPLITTING, State.MERGING)
1887      ) {
1888        assert regionLocation != null : "found null region location for " + regionNode;
1889        // TODO: this could lead to some orphan server state nodes, as it is possible that the
1890        // region server is already dead and its SCP has already finished but we have
1891        // persisted an opening state on this region server. Finally the TRSP will assign the
1892        // region to another region server, so it will not cause critical problems, just waste
1893        // some memory as no one will try to cleanup these orphan server state nodes.
1894        regionStates.createServer(regionLocation);
1895        regionStates.addRegionToServer(regionNode);
1896      } else if (localState == State.OFFLINE || regionInfo.isOffline()) {
1897        regionStates.addToOfflineRegions(regionNode);
1898      }
1899      if (regionNode.getProcedure() != null) {
1900        regionNode.getProcedure().stateLoaded(AssignmentManager.this, regionNode);
1901      }
1902      // add regions to RIT while visiting the meta
1903      regionInTransitionTracker.handleRegionStateNodeOperation(regionNode);
1904      // If region is supposed to be serve traffic (NOT split and merged) and location of region
1905      // belongs to a dead server mark the region crashed
1906      if (
1907        regionNode.getRegionLocation() != null && !AssignmentManagerUtil.isSplitOrMerged(regionNode)
1908          && master.getServerManager().isServerDead(regionNode.getRegionLocation())
1909      ) {
1910        long timeOfCrash = master.getServerManager().getDeadServers()
1911          .getDeathTimestamp(regionNode.getRegionLocation());
1912        if (timeOfCrash != 0) {
1913          regionNode.crashed(timeOfCrash);
1914        }
1915        regionInTransitionTracker.regionCrashed(regionNode, timeOfCrash);
1916      }
1917    }
1918  };
1919
1920  /**
1921   * Attempt to load {@code regionInfo} from META, adding any results to the
1922   * {@link #regionStateStore} Is NOT aware of replica regions.
1923   * @param regionInfo the region to be loaded from META.
1924   * @throws IOException If some error occurs while querying META or parsing results.
1925   */
1926  public void populateRegionStatesFromMeta(@NonNull final RegionInfo regionInfo)
1927    throws IOException {
1928    final String regionEncodedName = RegionInfo.DEFAULT_REPLICA_ID == regionInfo.getReplicaId()
1929      ? regionInfo.getEncodedName()
1930      : RegionInfoBuilder.newBuilder(regionInfo).setReplicaId(RegionInfo.DEFAULT_REPLICA_ID).build()
1931        .getEncodedName();
1932    populateRegionStatesFromMeta(regionEncodedName);
1933  }
1934
1935  /**
1936   * Attempt to load {@code regionEncodedName} from META, adding any results to the
1937   * {@link #regionStateStore} Is NOT aware of replica regions.
1938   * @param regionEncodedName encoded name for the region to be loaded from META.
1939   * @throws IOException If some error occurs while querying META or parsing results.
1940   */
1941  public void populateRegionStatesFromMeta(@NonNull String regionEncodedName) throws IOException {
1942    final RegionMetaLoadingVisitor visitor = new RegionMetaLoadingVisitor();
1943    regionStateStore.visitMetaForRegion(regionEncodedName, visitor);
1944  }
1945
1946  private void loadMeta() throws IOException {
1947    // TODO: use a thread pool
1948    regionStateStore.visitMeta(new RegionMetaLoadingVisitor());
1949  }
1950
1951  /**
1952   * Used to check if the meta loading is done.
1953   * <p/>
1954   * if not we throw PleaseHoldException since we are rebuilding the RegionStates
1955   * @param hri    region to check if it is already rebuild
1956   * @param procId the procedure id for this region operation, or NO_PROC_ID if not available
1957   * @throws PleaseHoldException if meta has not been loaded yet
1958   */
1959  private void checkMetaLoaded(RegionInfo hri, long procId) throws PleaseHoldException {
1960    if (!isRunning()) {
1961      throw new PleaseHoldException("AssignmentManager not running");
1962    }
1963
1964    // Check if the procedure is for a critical system table
1965    // Critical system tables can proceed even if meta is not loaded yet
1966    // We are currently making procId available only for the code path which can execute during the
1967    // cluster boot up. In the future, if additional code paths execute during cluster boot up, we
1968    // will need to make procId available for all those code paths.
1969    if (procId != Procedure.NO_PROC_ID) {
1970      Procedure<?> proc = master.getMasterProcedureExecutor().getProcedure(procId);
1971      if (proc != null && proc.isCriticalSystemTable()) {
1972        return;
1973      }
1974    }
1975
1976    boolean meta = isMetaRegion(hri);
1977    boolean metaLoaded = isMetaLoaded();
1978    if (!meta && !metaLoaded) {
1979      throw new PleaseHoldException("Master not fully online; " + TableName.META_TABLE_NAME + "="
1980        + meta + ", metaLoaded=" + metaLoaded);
1981    }
1982  }
1983
1984  // ============================================================================================
1985  // TODO: Metrics
1986  // ============================================================================================
1987  public int getNumRegionsOpened() {
1988    // TODO: Used by TestRegionPlacement.java and assume monotonically increasing value
1989    return 0;
1990  }
1991
1992  /**
1993   * Usually run by the Master in reaction to server crash during normal processing. Can also be
1994   * invoked via external RPC to effect repair; in the latter case, the 'force' flag is set so we
1995   * push through the SCP though context may indicate already-running-SCP (An old SCP may have
1996   * exited abnormally, or damaged cluster may still have references in hbase:meta to 'Unknown
1997   * Servers' -- servers that are not online or in dead servers list, etc.)
1998   * @param force Set if the request came in externally over RPC (via hbck2). Force means run the
1999   *              SCP even if it seems as though there might be an outstanding SCP running.
2000   * @return pid of scheduled SCP or {@link Procedure#NO_PROC_ID} if none scheduled.
2001   */
2002  public long submitServerCrash(ServerName serverName, boolean shouldSplitWal, boolean force) {
2003    // May be an 'Unknown Server' so handle case where serverNode is null.
2004    ServerStateNode serverNode = regionStates.getServerNode(serverName);
2005    // Remove the in-memory rsReports result
2006    synchronized (rsReports) {
2007      rsReports.remove(serverName);
2008    }
2009    if (serverNode == null) {
2010      if (force) {
2011        LOG.info("Force adding ServerCrashProcedure for {} when server node is null", serverName);
2012      } else {
2013        // for normal case, do not schedule SCP if ServerStateNode is null
2014        LOG.warn("Skip adding ServerCrashProcedure for {} because server node is null", serverName);
2015        return Procedure.NO_PROC_ID;
2016      }
2017    }
2018
2019    ProcedureExecutor<MasterProcedureEnv> procExec = this.master.getMasterProcedureExecutor();
2020    // We hold the write lock here for fencing on reportRegionStateTransition. Once we set the
2021    // server state to CRASHED, we will no longer accept the reportRegionStateTransition call from
2022    // this server. This is used to simplify the implementation for TRSP and SCP, where we can make
2023    // sure that, the region list fetched by SCP will not be changed any more.
2024    if (serverNode != null) {
2025      serverNode.writeLock().lock();
2026    }
2027    try {
2028
2029      boolean carryingMeta = isCarryingMeta(serverName);
2030      if (serverNode != null && !serverNode.isInState(ServerState.ONLINE)) {
2031        if (force) {
2032          LOG.info("Force adding ServerCrashProcedure for {} (meta={}) when state is not {}",
2033            serverNode, carryingMeta, ServerState.ONLINE);
2034        } else {
2035          LOG.info("Skip adding ServerCrashProcedure for {} (meta={}) when state is not {}",
2036            serverNode, carryingMeta, ServerState.ONLINE);
2037          return Procedure.NO_PROC_ID;
2038        }
2039      }
2040      MasterProcedureEnv mpe = procExec.getEnvironment();
2041      // If serverNode == null, then 'Unknown Server'. Schedule HBCKSCP instead.
2042      // HBCKSCP scours Master in-memory state AND hbase;meta for references to
2043      // serverName just-in-case. An SCP that is scheduled when the server is
2044      // 'Unknown' probably originated externally with HBCK2 fix-it tool.
2045      ServerState oldState = null;
2046      if (serverNode != null) {
2047        oldState = serverNode.getState();
2048        serverNode.setState(ServerState.CRASHED);
2049      }
2050      ServerCrashProcedure scp = force
2051        ? new HBCKServerCrashProcedure(mpe, serverName, shouldSplitWal, carryingMeta)
2052        : new ServerCrashProcedure(mpe, serverName, shouldSplitWal, carryingMeta);
2053      long pid = procExec.submitProcedure(scp);
2054      LOG.info("Scheduled ServerCrashProcedure pid={} for {} (carryingMeta={}){}.", pid, serverName,
2055        carryingMeta,
2056        serverNode == null ? "" : " " + serverNode.toString() + ", oldState=" + oldState);
2057      return pid;
2058    } finally {
2059      if (serverNode != null) {
2060        serverNode.writeLock().unlock();
2061      }
2062    }
2063  }
2064
2065  public void offlineRegion(final RegionInfo regionInfo) {
2066    // TODO used by MasterRpcServices
2067    RegionStateNode node = regionStates.getRegionStateNode(regionInfo);
2068    if (node != null) {
2069      node.offline();
2070    }
2071  }
2072
2073  public void onlineRegion(final RegionInfo regionInfo, final ServerName serverName) {
2074    // TODO used by TestSplitTransactionOnCluster.java
2075  }
2076
2077  public Map<ServerName, List<RegionInfo>>
2078    getSnapShotOfAssignment(final Collection<RegionInfo> regions) {
2079    return regionStates.getSnapShotOfAssignment(regions);
2080  }
2081
2082  // ============================================================================================
2083  // TODO: UTILS/HELPERS?
2084  // ============================================================================================
2085  /**
2086   * Used by the client (via master) to identify if all regions have the schema updates
2087   * @return Pair indicating the status of the alter command (pending/total)
2088   */
2089  public Pair<Integer, Integer> getReopenStatus(TableName tableName) {
2090    if (isTableDisabled(tableName)) {
2091      return new Pair<Integer, Integer>(0, 0);
2092    }
2093
2094    final List<RegionState> states = regionStates.getTableRegionStates(tableName);
2095    int ritCount = 0;
2096    for (RegionState regionState : states) {
2097      if (!regionState.isOpened() && !regionState.isSplit()) {
2098        ritCount++;
2099      }
2100    }
2101    return new Pair<Integer, Integer>(ritCount, states.size());
2102  }
2103
2104  // This comparator sorts the RegionStates by time stamp then Region name.
2105  // Comparing by timestamp alone can lead us to discard different RegionStates that happen
2106  // to share a timestamp.
2107  private final static class RegionStateStampComparator implements Comparator<RegionState> {
2108    @Override
2109    public int compare(final RegionState l, final RegionState r) {
2110      int stampCmp = Long.compare(l.getStamp(), r.getStamp());
2111      return stampCmp != 0 ? stampCmp : RegionInfo.COMPARATOR.compare(l.getRegion(), r.getRegion());
2112    }
2113  }
2114
2115  public final static RegionStateStampComparator REGION_STATE_STAMP_COMPARATOR =
2116    new RegionStateStampComparator();
2117
2118  // ============================================================================================
2119  // TODO: Region State In Transition
2120  // ============================================================================================
2121  public boolean hasRegionsInTransition() {
2122    return regionInTransitionTracker.hasRegionsInTransition();
2123  }
2124
2125  public List<RegionStateNode> getRegionsInTransition() {
2126    return regionInTransitionTracker.getRegionsInTransition();
2127  }
2128
2129  public int getRegionTransitScheduledCount() {
2130    return regionStates.getRegionTransitScheduledCount();
2131  }
2132
2133  /**
2134   * Get the number of regions in transition.
2135   */
2136  public int getRegionsInTransitionCount() {
2137    return regionInTransitionTracker.getRegionsInTransitionCount();
2138  }
2139
2140  public SortedSet<RegionState> getRegionsStateInTransition() {
2141    final SortedSet<RegionState> rit = new TreeSet<RegionState>(REGION_STATE_STAMP_COMPARATOR);
2142    for (RegionStateNode node : getRegionsInTransition()) {
2143      rit.add(node.toRegionState());
2144    }
2145    return rit;
2146  }
2147
2148  public List<RegionInfo> getAssignedRegions() {
2149    return regionStates.getAssignedRegions();
2150  }
2151
2152  /**
2153   * Resolve a cached {@link RegionInfo} from the region name as a {@code byte[]}.
2154   */
2155  public RegionInfo getRegionInfo(final byte[] regionName) {
2156    final RegionStateNode regionState = regionStates.getRegionStateNodeFromName(regionName);
2157    return regionState != null ? regionState.getRegionInfo() : null;
2158  }
2159
2160  /**
2161   * Resolve a cached {@link RegionInfo} from the encoded region name as a {@code String}.
2162   */
2163  public RegionInfo getRegionInfo(final String encodedRegionName) {
2164    final RegionStateNode regionState =
2165      regionStates.getRegionStateNodeFromEncodedRegionName(encodedRegionName);
2166    return regionState != null ? regionState.getRegionInfo() : null;
2167  }
2168
2169  // ============================================================================================
2170  // Expected states on region state transition.
2171  // Notice that there is expected states for transiting to OPENING state, this is because SCP.
2172  // See the comments in regionOpening method for more details.
2173  // ============================================================================================
2174  private static final State[] STATES_EXPECTED_ON_OPEN = { State.OPENING, // Normal case
2175    State.OPEN // Retrying
2176  };
2177
2178  private static final State[] STATES_EXPECTED_ON_CLOSING = { State.OPEN, // Normal case
2179    State.CLOSING, // Retrying
2180    State.SPLITTING, // Offline the split parent
2181    State.MERGING // Offline the merge parents
2182  };
2183
2184  private static final State[] STATES_EXPECTED_ON_CLOSED = { State.CLOSING, // Normal case
2185    State.CLOSED // Retrying
2186  };
2187
2188  // This is for manually scheduled region assign, can add other states later if we find out other
2189  // usages
2190  private static final State[] STATES_EXPECTED_ON_ASSIGN = { State.CLOSED, State.OFFLINE };
2191
2192  // We only allow unassign or move a region which is in OPEN state.
2193  private static final State[] STATES_EXPECTED_ON_UNASSIGN_OR_MOVE = { State.OPEN };
2194
2195  // ============================================================================================
2196  // Region Status update
2197  // Should only be called in TransitRegionStateProcedure(and related procedures), as the locking
2198  // and pre-assumptions are very tricky.
2199  // ============================================================================================
2200  private CompletableFuture<Void> transitStateAndUpdate(RegionStateNode regionNode,
2201    RegionState.State newState, RegionState.State... expectedStates) {
2202    RegionState.State state = regionNode.getState();
2203    try {
2204      regionNode.transitionState(newState, expectedStates);
2205    } catch (UnexpectedStateException e) {
2206      return FutureUtils.failedFuture(e);
2207    }
2208    CompletableFuture<Void> future = regionStateStore.updateRegionLocation(regionNode);
2209    FutureUtils.addListener(future, (r, e) -> {
2210      if (e != null) {
2211        // revert
2212        regionNode.setState(state);
2213      } else {
2214        regionInTransitionTracker.handleRegionStateNodeOperation(regionNode);
2215      }
2216    });
2217    return future;
2218  }
2219
2220  // should be called within the synchronized block of RegionStateNode
2221  CompletableFuture<Void> regionOpening(RegionStateNode regionNode) {
2222    // As in SCP, for performance reason, there is no TRSP attached with this region, we will not
2223    // update the region state, which means that the region could be in any state when we want to
2224    // assign it after a RS crash. So here we do not pass the expectedStates parameter.
2225    return transitStateAndUpdate(regionNode, State.OPENING).thenAccept(r -> {
2226      ServerStateNode serverNode = regionStates.getServerNode(regionNode.getRegionLocation());
2227      // Here the server node could be null. For example, we want to assign the region to a given
2228      // region server and it crashes, and it is the region server which holds hbase:meta, then the
2229      // above transitStateAndUpdate call will never succeed until we finishes the SCP for it. But
2230      // after the SCP finishes, the server node will be removed, so when we arrive there, the
2231      // server
2232      // node will be null. This is not a big problem if we skip adding it, as later we will fail to
2233      // execute the remote procedure on the region server and then try to assign to another region
2234      // server
2235      if (serverNode != null) {
2236        serverNode.addRegion(regionNode);
2237      }
2238      // update the operation count metrics
2239      metrics.incrementOperationCounter();
2240    });
2241  }
2242
2243  // should be called under the RegionStateNode lock
2244  // Called when a region open attempt has failed. The 'giveUp' parameter says whether we will try
2245  // to open the region again. If true, this is a terminal failure with no more retries: we set
2246  // RegionState.State to FAILED_OPEN and persist it to hbase:meta, and only remove the region from
2247  // its old target server's bookkeeping once that persist succeeds. If false, we are about to
2248  // retry the open with a new plan, so RegionState.State is intentionally left untouched (e.g.
2249  // still OPENING), meta is not touched, and we remove the region from its old target server's
2250  // bookkeeping immediately. In both cases the removal only happens if there was an old location.
2251  CompletableFuture<Void> regionFailedOpen(RegionStateNode regionNode, boolean giveUp) {
2252    RegionState.State state = regionNode.getState();
2253    ServerName regionLocation = regionNode.getRegionLocation();
2254    if (!giveUp) {
2255      if (regionLocation != null) {
2256        regionStates.removeRegionFromServer(regionLocation, regionNode);
2257      }
2258      return CompletableFuture.completedFuture(null);
2259    }
2260    regionNode.setState(State.FAILED_OPEN);
2261    regionNode.setRegionLocation(null);
2262    CompletableFuture<Void> future = regionStateStore.updateRegionLocation(regionNode);
2263    FutureUtils.addListener(future, (r, e) -> {
2264      if (e == null) {
2265        if (regionLocation != null) {
2266          regionStates.removeRegionFromServer(regionLocation, regionNode);
2267        }
2268        regionInTransitionTracker.handleRegionStateNodeOperation(regionNode);
2269      } else {
2270        // revert
2271        regionNode.setState(state);
2272        regionNode.setRegionLocation(regionLocation);
2273      }
2274    });
2275    return future;
2276  }
2277
2278  // should be called under the RegionStateNode lock
2279  CompletableFuture<Void> regionClosing(RegionStateNode regionNode) {
2280    return transitStateAndUpdate(regionNode, State.CLOSING, STATES_EXPECTED_ON_CLOSING)
2281      .thenAccept(r -> {
2282        RegionInfo hri = regionNode.getRegionInfo();
2283        // Set meta has not initialized early. so people trying to create/edit tables will wait
2284        if (isMetaRegion(hri)) {
2285          setMetaAssigned(hri, false);
2286        }
2287        // update the operation count metrics
2288        metrics.incrementOperationCounter();
2289      });
2290  }
2291
2292  // for open and close, they will first be persist to the procedure store in
2293  // RegionRemoteProcedureBase. So here we will first change the in memory state as it is considered
2294  // as succeeded if the persistence to procedure store is succeeded, and then when the
2295  // RegionRemoteProcedureBase is woken up, we will persist the RegionStateNode to hbase:meta.
2296
2297  // should be called under the RegionStateNode lock
2298  void regionOpenedWithoutPersistingToMeta(RegionStateNode regionNode)
2299    throws UnexpectedStateException {
2300    regionNode.transitionState(State.OPEN, STATES_EXPECTED_ON_OPEN);
2301    RegionInfo regionInfo = regionNode.getRegionInfo();
2302    regionStates.addRegionToServer(regionNode);
2303    regionStates.removeFromFailedOpen(regionInfo);
2304    // HBASE-30335: seed the master's flushed sequence cache with openSeqNum so a subsequent
2305    // WAL split (e.g. source RS crashes after drain-move) recognizes already-durable edits
2306    // instead of writing orphaned recovered.edits.
2307    master.getServerManager().advanceRegionFlushedSeq(regionInfo, regionNode.getOpenSeqNum());
2308  }
2309
2310  // should be called under the RegionStateNode lock
2311  void regionClosedWithoutPersistingToMeta(RegionStateNode regionNode)
2312    throws UnexpectedStateException {
2313    ServerName regionLocation = regionNode.getRegionLocation();
2314    regionNode.transitionState(State.CLOSED, STATES_EXPECTED_ON_CLOSED);
2315    regionNode.setRegionLocation(null);
2316    if (regionLocation != null) {
2317      regionNode.setLastHost(regionLocation);
2318      regionStates.removeRegionFromServer(regionLocation, regionNode);
2319    }
2320  }
2321
2322  // should be called under the RegionStateNode lock
2323  CompletableFuture<Void> persistToMeta(RegionStateNode regionNode) {
2324    return regionStateStore.updateRegionLocation(regionNode).thenAccept(r -> {
2325      RegionInfo regionInfo = regionNode.getRegionInfo();
2326      if (isMetaRegion(regionInfo) && regionNode.getState() == State.OPEN) {
2327        // Usually we'd set a table ENABLED at this stage but hbase:meta is ALWAYs enabled, it
2328        // can't be disabled -- so skip the RPC (besides... enabled is managed by TableStateManager
2329        // which is backed by hbase:meta... Avoid setting ENABLED to avoid having to update state
2330        // on table that contains state.
2331        setMetaAssigned(regionInfo, true);
2332      }
2333      regionInTransitionTracker.handleRegionStateNodeOperation(regionNode);
2334    });
2335  }
2336
2337  // should be called under the RegionStateNode lock
2338  // for SCP
2339  public CompletableFuture<Void> regionClosedAbnormally(RegionStateNode regionNode) {
2340    RegionState.State state = regionNode.getState();
2341    ServerName regionLocation = regionNode.getRegionLocation();
2342    regionNode.setState(State.ABNORMALLY_CLOSED);
2343    regionNode.setRegionLocation(null);
2344    CompletableFuture<Void> future = regionStateStore.updateRegionLocation(regionNode);
2345    FutureUtils.addListener(future, (r, e) -> {
2346      if (e == null) {
2347        if (regionLocation != null) {
2348          regionNode.setLastHost(regionLocation);
2349          regionStates.removeRegionFromServer(regionLocation, regionNode);
2350        }
2351        regionInTransitionTracker.handleRegionStateNodeOperation(regionNode);
2352      } else {
2353        // revert
2354        regionNode.setState(state);
2355        regionNode.setRegionLocation(regionLocation);
2356      }
2357    });
2358    return future;
2359  }
2360
2361  // ============================================================================================
2362  // The above methods can only be called in TransitRegionStateProcedure(and related procedures)
2363  // ============================================================================================
2364
2365  // As soon as a server a crashed, region hosting on that are un-available, this method helps to
2366  // track those un-available regions. This method can only be called from ServerCrashProcedure.
2367  public void markRegionsAsCrashed(List<RegionInfo> regionsOnCrashedServer,
2368    ServerCrashProcedure scp) {
2369    ServerName crashedServerName = scp.getServerName();
2370    assert crashedServerName != null;
2371    for (RegionInfo regionInfo : regionsOnCrashedServer) {
2372      RegionStateNode node = regionStates.getOrCreateRegionStateNode(regionInfo);
2373      if (crashedServerName.equals(node.getRegionLocation())) {
2374        node.crashed(scp.getSubmittedTime());
2375        regionInTransitionTracker.regionCrashed(node, scp.getSubmittedTime());
2376      } else {
2377        LOG.warn("Region {} should be on crashed region server {} but is recorded on {}",
2378          regionInfo, crashedServerName, node.getRegionLocation());
2379      }
2380    }
2381  }
2382
2383  public void markRegionAsSplit(final RegionInfo parent, final ServerName serverName,
2384    final RegionInfo daughterA, final RegionInfo daughterB) throws IOException {
2385    // Update hbase:meta. Parent will be marked offline and split up in hbase:meta.
2386    // The parent stays in regionStates until cleared when removed by CatalogJanitor.
2387    // Update its state in regionStates to it shows as offline and split when read
2388    // later figuring what regions are in a table and what are not: see
2389    // regionStates#getRegionsOfTable
2390    final RegionStateNode node = regionStates.getOrCreateRegionStateNode(parent);
2391    node.setState(State.SPLIT);
2392    final RegionStateNode nodeA = regionStates.getOrCreateRegionStateNode(daughterA);
2393    nodeA.setState(State.SPLITTING_NEW);
2394    final RegionStateNode nodeB = regionStates.getOrCreateRegionStateNode(daughterB);
2395    nodeB.setState(State.SPLITTING_NEW);
2396
2397    TableDescriptor td = master.getTableDescriptors().get(parent.getTable());
2398    // TODO: here we just update the parent region info in meta, to set split and offline to true,
2399    // without changing the one in the region node. This is a bit confusing but the region info
2400    // field in RegionStateNode is not expected to be changed in the current design. Need to find a
2401    // possible way to address this problem, or at least adding more comments about the trick to
2402    // deal with this problem, that when you want to filter out split parent, you need to check both
2403    // the RegionState on whether it is split, and also the region info. If one of them matches then
2404    // it is a split parent. And usually only one of them can match, as after restart, the region
2405    // state will be changed from SPLIT to CLOSED.
2406    regionStateStore.splitRegion(parent, daughterA, daughterB, serverName, td);
2407    regionInTransitionTracker.handleRegionStateNodeOperation(node);
2408    regionInTransitionTracker.handleRegionStateNodeOperation(nodeA);
2409    regionInTransitionTracker.handleRegionStateNodeOperation(nodeB);
2410    if (shouldAssignFavoredNodes(parent)) {
2411      List<ServerName> onlineServers = this.master.getServerManager().getOnlineServersList();
2412      getFavoredNodePromoter().generateFavoredNodesForDaughter(onlineServers, parent, daughterA,
2413        daughterB);
2414    }
2415  }
2416
2417  /**
2418   * When called here, the merge has happened. The merged regions have been unassigned and the above
2419   * markRegionClosed has been called on each so they have been disassociated from a hosting Server.
2420   * The merged region will be open after this call. The merged regions are removed from hbase:meta
2421   * below. Later they are deleted from the filesystem by the catalog janitor running against
2422   * hbase:meta. It notices when the merged region no longer holds references to the old regions
2423   * (References are deleted after a compaction rewrites what the Reference points at but not until
2424   * the archiver chore runs, are the References removed).
2425   */
2426  public void markRegionAsMerged(final RegionInfo child, final ServerName serverName,
2427    RegionInfo[] mergeParents) throws IOException {
2428    final RegionStateNode node = regionStates.getOrCreateRegionStateNode(child);
2429    for (RegionInfo ri : mergeParents) {
2430      regionStates.deleteRegion(ri);
2431      regionInTransitionTracker.handleRegionDelete(ri);
2432    }
2433
2434    TableDescriptor td = master.getTableDescriptors().get(child.getTable());
2435    regionStateStore.mergeRegions(child, mergeParents, serverName, td);
2436    regionInTransitionTracker.handleRegionStateNodeOperation(node);
2437    if (shouldAssignFavoredNodes(child)) {
2438      getFavoredNodePromoter().generateFavoredNodesForMergedRegion(child, mergeParents);
2439    }
2440  }
2441
2442  /*
2443   * Favored nodes should be applied only when FavoredNodes balancer is configured and the region
2444   * belongs to a non-system table.
2445   */
2446  private boolean shouldAssignFavoredNodes(RegionInfo region) {
2447    return this.shouldAssignRegionsWithFavoredNodes
2448      && FavoredNodesManager.isFavoredNodeApplicable(region);
2449  }
2450
2451  // ============================================================================================
2452  // Assign Queue (Assign/Balance)
2453  // ============================================================================================
2454  private final ArrayList<RegionStateNode> pendingAssignQueue = new ArrayList<RegionStateNode>();
2455  private final ReentrantLock assignQueueLock = new ReentrantLock();
2456  private final Condition assignQueueFullCond = assignQueueLock.newCondition();
2457
2458  /**
2459   * Add the assign operation to the assignment queue. The pending assignment operation will be
2460   * processed, and each region will be assigned by a server using the balancer.
2461   */
2462  protected void queueAssign(final RegionStateNode regionNode) {
2463    regionNode.getProcedureEvent().suspend();
2464
2465    // TODO: quick-start for meta and the other sys-tables?
2466    assignQueueLock.lock();
2467    try {
2468      pendingAssignQueue.add(regionNode);
2469      if (
2470        regionNode.isSystemTable() || pendingAssignQueue.size() == 1
2471          || pendingAssignQueue.size() >= assignDispatchWaitQueueMaxSize
2472      ) {
2473        assignQueueFullCond.signal();
2474      }
2475    } finally {
2476      assignQueueLock.unlock();
2477    }
2478  }
2479
2480  private void startAssignmentThread() {
2481    assignThread = new Thread(master.getServerName().toShortString()) {
2482      @Override
2483      public void run() {
2484        while (isRunning()) {
2485          processAssignQueue();
2486        }
2487        pendingAssignQueue.clear();
2488      }
2489    };
2490    assignThread.setDaemon(true);
2491    assignThread.start();
2492  }
2493
2494  private void stopAssignmentThread() {
2495    assignQueueSignal();
2496    try {
2497      while (assignThread.isAlive()) {
2498        assignQueueSignal();
2499        assignThread.join(250);
2500      }
2501    } catch (InterruptedException e) {
2502      LOG.warn("join interrupted", e);
2503      Thread.currentThread().interrupt();
2504    }
2505  }
2506
2507  private void assignQueueSignal() {
2508    assignQueueLock.lock();
2509    try {
2510      assignQueueFullCond.signal();
2511    } finally {
2512      assignQueueLock.unlock();
2513    }
2514  }
2515
2516  @edu.umd.cs.findbugs.annotations.SuppressWarnings("WA_AWAIT_NOT_IN_LOOP")
2517  private HashMap<RegionInfo, RegionStateNode> waitOnAssignQueue() {
2518    HashMap<RegionInfo, RegionStateNode> regions = null;
2519
2520    assignQueueLock.lock();
2521    try {
2522      if (pendingAssignQueue.isEmpty() && isRunning()) {
2523        assignQueueFullCond.await();
2524      }
2525
2526      if (!isRunning()) {
2527        return null;
2528      }
2529      assignQueueFullCond.await(assignDispatchWaitMillis, TimeUnit.MILLISECONDS);
2530      regions = new HashMap<RegionInfo, RegionStateNode>(pendingAssignQueue.size());
2531      for (RegionStateNode regionNode : pendingAssignQueue) {
2532        regions.put(regionNode.getRegionInfo(), regionNode);
2533      }
2534      pendingAssignQueue.clear();
2535    } catch (InterruptedException e) {
2536      LOG.warn("got interrupted ", e);
2537      Thread.currentThread().interrupt();
2538    } finally {
2539      assignQueueLock.unlock();
2540    }
2541    return regions;
2542  }
2543
2544  private void processAssignQueue() {
2545    final HashMap<RegionInfo, RegionStateNode> regions = waitOnAssignQueue();
2546    if (regions == null || regions.size() == 0 || !isRunning()) {
2547      return;
2548    }
2549
2550    if (LOG.isTraceEnabled()) {
2551      LOG.trace("PROCESS ASSIGN QUEUE regionCount=" + regions.size());
2552    }
2553
2554    // TODO: Optimize balancer. pass a RegionPlan?
2555    final HashMap<RegionInfo, ServerName> retainMap = new HashMap<>();
2556    final List<RegionInfo> userHRIs = new ArrayList<>(regions.size());
2557    // Regions for system tables requiring reassignment
2558    final List<RegionInfo> systemHRIs = new ArrayList<>();
2559    for (RegionStateNode regionStateNode : regions.values()) {
2560      boolean sysTable = regionStateNode.isSystemTable();
2561      final List<RegionInfo> hris = sysTable ? systemHRIs : userHRIs;
2562      if (regionStateNode.getRegionLocation() != null) {
2563        retainMap.put(regionStateNode.getRegionInfo(), regionStateNode.getRegionLocation());
2564      } else {
2565        hris.add(regionStateNode.getRegionInfo());
2566      }
2567    }
2568
2569    // TODO: connect with the listener to invalidate the cache
2570
2571    // TODO use events
2572    List<ServerName> servers = master.getServerManager().createDestinationServersList();
2573    for (int i = 0; servers.size() < 1; ++i) {
2574      // Report every fourth time around this loop; try not to flood log.
2575      if (i % 4 == 0) {
2576        LOG.warn("No servers available; cannot place " + regions.size() + " unassigned regions.");
2577      }
2578
2579      if (!isRunning()) {
2580        LOG.debug("Stopped! Dropping assign of " + regions.size() + " queued regions.");
2581        return;
2582      }
2583      Threads.sleep(250);
2584      servers = master.getServerManager().createDestinationServersList();
2585    }
2586
2587    if (!systemHRIs.isEmpty()) {
2588      // System table regions requiring reassignment are present, get region servers
2589      // not available for system table regions
2590      final List<ServerName> excludeServers = getExcludedServersForSystemTable();
2591      List<ServerName> serversForSysTables =
2592        servers.stream().filter(s -> !excludeServers.contains(s)).collect(Collectors.toList());
2593      if (serversForSysTables.isEmpty()) {
2594        LOG.warn("Filtering old server versions and the excluded produced an empty set; "
2595          + "instead considering all candidate servers!");
2596      }
2597      LOG.debug("Processing assignQueue; systemServersCount=" + serversForSysTables.size()
2598        + ", allServersCount=" + servers.size());
2599      processAssignmentPlans(regions, null, systemHRIs,
2600        serversForSysTables.isEmpty() && !containsBogusAssignments(regions, systemHRIs)
2601          ? servers
2602          : serversForSysTables);
2603    }
2604
2605    processAssignmentPlans(regions, retainMap, userHRIs, servers);
2606  }
2607
2608  private boolean containsBogusAssignments(Map<RegionInfo, RegionStateNode> regions,
2609    List<RegionInfo> hirs) {
2610    for (RegionInfo ri : hirs) {
2611      if (
2612        regions.get(ri).getRegionLocation() != null
2613          && regions.get(ri).getRegionLocation().equals(LoadBalancer.BOGUS_SERVER_NAME)
2614      ) {
2615        return true;
2616      }
2617    }
2618    return false;
2619  }
2620
2621  private void processAssignmentPlans(final HashMap<RegionInfo, RegionStateNode> regions,
2622    final HashMap<RegionInfo, ServerName> retainMap, final List<RegionInfo> hris,
2623    final List<ServerName> servers) {
2624    boolean isTraceEnabled = LOG.isTraceEnabled();
2625    if (isTraceEnabled) {
2626      LOG.trace("Available servers count=" + servers.size() + ": " + servers);
2627    }
2628
2629    final LoadBalancer balancer = getBalancer();
2630    // ask the balancer where to place regions
2631    if (retainMap != null && !retainMap.isEmpty()) {
2632      if (isTraceEnabled) {
2633        LOG.trace("retain assign regions=" + retainMap);
2634      }
2635      try {
2636        acceptPlan(regions, balancer.retainAssignment(retainMap, servers));
2637      } catch (IOException e) {
2638        LOG.warn("unable to retain assignment", e);
2639        addToPendingAssignment(regions, retainMap.keySet());
2640      }
2641    }
2642
2643    // TODO: Do we need to split retain and round-robin?
2644    // the retain seems to fallback to round-robin/random if the region is not in the map.
2645    if (!hris.isEmpty()) {
2646      Collections.sort(hris, RegionInfo.COMPARATOR);
2647      if (isTraceEnabled) {
2648        LOG.trace("round robin regions=" + hris);
2649      }
2650      try {
2651        acceptPlan(regions, balancer.roundRobinAssignment(hris, servers));
2652      } catch (IOException e) {
2653        LOG.warn("unable to round-robin assignment", e);
2654        addToPendingAssignment(regions, hris);
2655      }
2656    }
2657  }
2658
2659  private void acceptPlan(final HashMap<RegionInfo, RegionStateNode> regions,
2660    final Map<ServerName, List<RegionInfo>> plan) throws HBaseIOException {
2661    final ProcedureEvent<?>[] events = new ProcedureEvent[regions.size()];
2662    final long st = EnvironmentEdgeManager.currentTime();
2663
2664    if (plan.isEmpty()) {
2665      throw new HBaseIOException("unable to compute plans for regions=" + regions.size());
2666    }
2667
2668    int evcount = 0;
2669    for (Map.Entry<ServerName, List<RegionInfo>> entry : plan.entrySet()) {
2670      final ServerName server = entry.getKey();
2671      for (RegionInfo hri : entry.getValue()) {
2672        final RegionStateNode regionNode = regions.get(hri);
2673        regionNode.setRegionLocation(server);
2674        if (server.equals(LoadBalancer.BOGUS_SERVER_NAME) && regionNode.isSystemTable()) {
2675          assignQueueLock.lock();
2676          try {
2677            pendingAssignQueue.add(regionNode);
2678          } finally {
2679            assignQueueLock.unlock();
2680          }
2681        } else {
2682          events[evcount++] = regionNode.getProcedureEvent();
2683        }
2684      }
2685    }
2686    ProcedureEvent.wakeEvents(getProcedureScheduler(), events);
2687
2688    final long et = EnvironmentEdgeManager.currentTime();
2689    if (LOG.isTraceEnabled()) {
2690      LOG.trace("ASSIGN ACCEPT " + events.length + " -> " + StringUtils.humanTimeDiff(et - st));
2691    }
2692  }
2693
2694  private void addToPendingAssignment(final HashMap<RegionInfo, RegionStateNode> regions,
2695    final Collection<RegionInfo> pendingRegions) {
2696    assignQueueLock.lock();
2697    try {
2698      for (RegionInfo hri : pendingRegions) {
2699        pendingAssignQueue.add(regions.get(hri));
2700      }
2701    } finally {
2702      assignQueueLock.unlock();
2703    }
2704  }
2705
2706  /**
2707   * For a given cluster with mixed versions of servers, get a list of servers with lower versions,
2708   * where system table regions should not be assigned to. For system table, we must assign regions
2709   * to a server with highest version. However, we can disable this exclusion using config:
2710   * "hbase.min.version.move.system.tables" if checkForMinVersion is true. Detailed explanation
2711   * available with definition of minVersionToMoveSysTables.
2712   * @return List of Excluded servers for System table regions.
2713   */
2714  public List<ServerName> getExcludedServersForSystemTable() {
2715    // TODO: This should be a cached list kept by the ServerManager rather than calculated on each
2716    // move or system region assign. The RegionServerTracker keeps list of online Servers with
2717    // RegionServerInfo that includes Version.
2718    List<Pair<ServerName, String>> serverList =
2719      master.getServerManager().getOnlineServersList().stream()
2720        .map(s -> new Pair<>(s, master.getRegionServerVersion(s))).collect(Collectors.toList());
2721    if (serverList.isEmpty()) {
2722      return new ArrayList<>();
2723    }
2724    String highestVersion = Collections
2725      .max(serverList, (o1, o2) -> VersionInfo.compareVersion(o1.getSecond(), o2.getSecond()))
2726      .getSecond();
2727    if (!DEFAULT_MIN_VERSION_MOVE_SYS_TABLES_CONFIG.equals(minVersionToMoveSysTables)) {
2728      int comparedValue = VersionInfo.compareVersion(minVersionToMoveSysTables, highestVersion);
2729      if (comparedValue > 0) {
2730        return new ArrayList<>();
2731      }
2732    }
2733    return serverList.stream().filter(pair -> !pair.getSecond().equals(highestVersion))
2734      .map(Pair::getFirst).collect(Collectors.toList());
2735  }
2736
2737  MasterServices getMaster() {
2738    return master;
2739  }
2740
2741  /** Returns a snapshot of rsReports */
2742  public Map<ServerName, Set<byte[]>> getRSReports() {
2743    Map<ServerName, Set<byte[]>> rsReportsSnapshot = new HashMap<>();
2744    synchronized (rsReports) {
2745      rsReports.entrySet().forEach(e -> rsReportsSnapshot.put(e.getKey(), e.getValue()));
2746    }
2747    return rsReportsSnapshot;
2748  }
2749
2750  /**
2751   * Provide regions state count for given table. e.g howmany regions of give table are
2752   * opened/closed/rit etc
2753   * @param tableName TableName
2754   * @return region states count
2755   */
2756  public RegionStatesCount getRegionStatesCount(TableName tableName) {
2757    int openRegionsCount = 0;
2758    int closedRegionCount = 0;
2759    int ritCount = 0;
2760    int splitRegionCount = 0;
2761    int totalRegionCount = 0;
2762    if (!isTableDisabled(tableName)) {
2763      final List<RegionState> states = regionStates.getTableRegionStates(tableName);
2764      for (RegionState regionState : states) {
2765        if (regionState.isOpened()) {
2766          openRegionsCount++;
2767        } else if (regionState.isClosed()) {
2768          closedRegionCount++;
2769        } else if (regionState.isSplit()) {
2770          splitRegionCount++;
2771        }
2772      }
2773      totalRegionCount = states.size();
2774      ritCount = totalRegionCount - openRegionsCount - splitRegionCount;
2775    }
2776    return new RegionStatesCount.RegionStatesCountBuilder().setOpenRegions(openRegionsCount)
2777      .setClosedRegions(closedRegionCount).setSplitRegions(splitRegionCount)
2778      .setRegionsInTransition(ritCount).setTotalRegions(totalRegionCount).build();
2779  }
2780
2781}