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;
019
020import static org.apache.hadoop.hbase.ChoreService.CHORE_SERVICE_INITIAL_POOL_SIZE;
021import static org.apache.hadoop.hbase.ChoreService.DEFAULT_CHORE_SERVICE_INITIAL_POOL_SIZE;
022import static org.apache.hadoop.hbase.HConstants.DEFAULT_HBASE_SPLIT_COORDINATED_BY_ZK;
023import static org.apache.hadoop.hbase.HConstants.HBASE_SPLIT_WAL_COORDINATED_BY_ZK;
024
025import com.google.errorprone.annotations.RestrictedApi;
026import io.opentelemetry.api.trace.Span;
027import io.opentelemetry.api.trace.StatusCode;
028import io.opentelemetry.context.Scope;
029import java.io.IOException;
030import java.lang.management.MemoryType;
031import java.net.BindException;
032import java.net.InetAddress;
033import java.net.InetSocketAddress;
034import java.util.concurrent.atomic.AtomicBoolean;
035import java.util.concurrent.atomic.AtomicReference;
036import javax.servlet.http.HttpServlet;
037import org.apache.commons.lang3.StringUtils;
038import org.apache.commons.lang3.SystemUtils;
039import org.apache.hadoop.conf.Configuration;
040import org.apache.hadoop.fs.FileSystem;
041import org.apache.hadoop.fs.Path;
042import org.apache.hadoop.hbase.client.AsyncClusterConnection;
043import org.apache.hadoop.hbase.client.ClusterConnectionFactory;
044import org.apache.hadoop.hbase.client.Connection;
045import org.apache.hadoop.hbase.client.ConnectionFactory;
046import org.apache.hadoop.hbase.client.ConnectionRegistryEndpoint;
047import org.apache.hadoop.hbase.conf.ConfigurationManager;
048import org.apache.hadoop.hbase.conf.ConfigurationObserver;
049import org.apache.hadoop.hbase.coordination.ZkCoordinatedStateManager;
050import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
051import org.apache.hadoop.hbase.executor.ExecutorService;
052import org.apache.hadoop.hbase.fs.HFileSystem;
053import org.apache.hadoop.hbase.http.InfoServer;
054import org.apache.hadoop.hbase.io.MetricsIO;
055import org.apache.hadoop.hbase.io.util.MemorySizeUtil;
056import org.apache.hadoop.hbase.ipc.RpcServerInterface;
057import org.apache.hadoop.hbase.keymeta.KeyManagementService;
058import org.apache.hadoop.hbase.keymeta.KeymetaAdmin;
059import org.apache.hadoop.hbase.keymeta.ManagedKeyDataCache;
060import org.apache.hadoop.hbase.keymeta.SystemKeyCache;
061import org.apache.hadoop.hbase.master.HMaster;
062import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
063import org.apache.hadoop.hbase.namequeues.NamedQueueRecorder;
064import org.apache.hadoop.hbase.regionserver.ChunkCreator;
065import org.apache.hadoop.hbase.regionserver.HeapMemoryManager;
066import org.apache.hadoop.hbase.regionserver.MemStoreLAB;
067import org.apache.hadoop.hbase.regionserver.RegionServerCoprocessorHost;
068import org.apache.hadoop.hbase.regionserver.ShutdownHook;
069import org.apache.hadoop.hbase.security.Superusers;
070import org.apache.hadoop.hbase.security.User;
071import org.apache.hadoop.hbase.security.UserProvider;
072import org.apache.hadoop.hbase.security.access.AccessChecker;
073import org.apache.hadoop.hbase.security.access.ZKPermissionWatcher;
074import org.apache.hadoop.hbase.trace.TraceUtil;
075import org.apache.hadoop.hbase.unsafe.HBasePlatformDependent;
076import org.apache.hadoop.hbase.util.Addressing;
077import org.apache.hadoop.hbase.util.CommonFSUtils;
078import org.apache.hadoop.hbase.util.ConfigurationUtil;
079import org.apache.hadoop.hbase.util.DNS;
080import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
081import org.apache.hadoop.hbase.util.FSTableDescriptors;
082import org.apache.hadoop.hbase.util.NettyEventLoopGroupConfig;
083import org.apache.hadoop.hbase.util.Pair;
084import org.apache.hadoop.hbase.util.Sleeper;
085import org.apache.hadoop.hbase.zookeeper.ClusterStatusTracker;
086import org.apache.hadoop.hbase.zookeeper.ZKAuthentication;
087import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
088import org.apache.yetus.audience.InterfaceAudience;
089import org.slf4j.Logger;
090import org.slf4j.LoggerFactory;
091
092/**
093 * Base class for hbase services, such as master or region server.
094 */
095@InterfaceAudience.Private
096public abstract class HBaseServerBase<R extends HBaseRpcServicesBase<?>> extends Thread
097  implements Server, ConfigurationObserver, ConnectionRegistryEndpoint, KeyManagementService {
098
099  private static final Logger LOG = LoggerFactory.getLogger(HBaseServerBase.class);
100
101  protected final Configuration conf;
102
103  // Go down hard. Used if file system becomes unavailable and also in
104  // debugging and unit tests.
105  protected final AtomicBoolean abortRequested = new AtomicBoolean(false);
106
107  // Set when a report to the master comes back with a message asking us to
108  // shut down. Also set by call to stop when debugging or running unit tests
109  // of HRegionServer in isolation.
110  protected volatile boolean stopped = false;
111
112  // Flag set when a read-only to read-write transition is blocked because another active cluster
113  // exists
114  protected final AtomicBoolean readOnlyTransitionBlocked;
115
116  // Tracks the active cluster in a read-replica setup when a ReadOnlyTransitionException occurs
117  private final AtomicReference<String> blockingActiveClusterId;
118
119  // Only for testing
120  private boolean isShutdownHookInstalled = false;
121
122  /**
123   * This server's startcode.
124   */
125  protected final long startcode;
126
127  protected final UserProvider userProvider;
128
129  // zookeeper connection and watcher
130  protected final ZKWatcher zooKeeper;
131
132  /**
133   * The server name the Master sees us as. Its made from the hostname the master passes us, port,
134   * and server startcode. Gets set after registration against Master.
135   */
136  protected ServerName serverName;
137
138  protected final R rpcServices;
139
140  /**
141   * hostname specified by hostname config
142   */
143  protected final String useThisHostnameInstead;
144
145  /**
146   * Provide online slow log responses from ringbuffer
147   */
148  protected final NamedQueueRecorder namedQueueRecorder;
149
150  /**
151   * Configuration manager is used to register/deregister and notify the configuration observers
152   * when the regionserver is notified that there was a change in the on disk configs.
153   */
154  protected final ConfigurationManager configurationManager;
155
156  /**
157   * ChoreService used to schedule tasks that we want to run periodically
158   */
159  protected final ChoreService choreService;
160
161  // Instance of the hbase executor executorService.
162  protected final ExecutorService executorService;
163
164  // Cluster Status Tracker
165  protected final ClusterStatusTracker clusterStatusTracker;
166
167  protected final CoordinatedStateManager csm;
168
169  // Info server. Default access so can be used by unit tests. REGIONSERVER
170  // is name of the webapp and the attribute name used stuffing this instance
171  // into web context.
172  protected InfoServer infoServer;
173
174  protected HFileSystem dataFs;
175
176  protected HFileSystem walFs;
177
178  protected Path dataRootDir;
179
180  protected Path walRootDir;
181
182  protected final int msgInterval;
183
184  // A sleeper that sleeps for msgInterval.
185  protected final Sleeper sleeper;
186
187  /**
188   * Go here to get table descriptors.
189   */
190  protected TableDescriptors tableDescriptors;
191
192  /**
193   * The asynchronous cluster connection to be shared by services.
194   */
195  protected AsyncClusterConnection asyncClusterConnection;
196
197  /**
198   * Cache for the meta region replica's locations. Also tracks their changes to avoid stale cache
199   * entries. Used for serving ClientMetaService.
200   */
201  protected final MetaRegionLocationCache metaRegionLocationCache;
202
203  protected final NettyEventLoopGroupConfig eventLoopGroupConfig;
204
205  private void setupSignalHandlers() {
206    if (!SystemUtils.IS_OS_WINDOWS) {
207      HBasePlatformDependent.handle("HUP", (number, name) -> {
208        try {
209          updateConfiguration();
210        } catch (IOException e) {
211          LOG.error("Problem while reloading configuration", e);
212        }
213      });
214    }
215  }
216
217  /**
218   * Setup our cluster connection if not already initialized.
219   */
220  protected final synchronized void setupClusterConnection() throws IOException {
221    if (asyncClusterConnection == null) {
222      InetSocketAddress localAddress =
223        new InetSocketAddress(rpcServices.getSocketAddress().getAddress(), 0);
224      User user = userProvider.getCurrent();
225      asyncClusterConnection =
226        ClusterConnectionFactory.createAsyncClusterConnection(this, conf, localAddress, user);
227    }
228  }
229
230  protected final void initializeFileSystem() throws IOException {
231    // Get fs instance used by this RS. Do we use checksum verification in the hbase? If hbase
232    // checksum verification enabled, then automatically switch off hdfs checksum verification.
233    boolean useHBaseChecksum = conf.getBoolean(HConstants.HBASE_CHECKSUM_VERIFICATION, true);
234    String walDirUri = CommonFSUtils.getDirUri(this.conf,
235      new Path(conf.get(CommonFSUtils.HBASE_WAL_DIR, conf.get(HConstants.HBASE_DIR))));
236    // set WAL's uri
237    if (walDirUri != null) {
238      CommonFSUtils.setFsDefault(this.conf, walDirUri);
239    }
240    // init the WALFs
241    this.walFs = new HFileSystem(this.conf, useHBaseChecksum);
242    this.walRootDir = CommonFSUtils.getWALRootDir(this.conf);
243    // Set 'fs.defaultFS' to match the filesystem on hbase.rootdir else
244    // underlying hadoop hdfs accessors will be going against wrong filesystem
245    // (unless all is set to defaults).
246    String rootDirUri =
247      CommonFSUtils.getDirUri(this.conf, new Path(conf.get(HConstants.HBASE_DIR)));
248    if (rootDirUri != null) {
249      CommonFSUtils.setFsDefault(this.conf, rootDirUri);
250    }
251    // init the filesystem
252    this.dataFs = new HFileSystem(this.conf, useHBaseChecksum);
253    this.dataRootDir = CommonFSUtils.getRootDir(this.conf);
254    int tableDescriptorParallelLoadThreads =
255      conf.getInt("hbase.tabledescriptor.parallel.load.threads", 0);
256    this.tableDescriptors = new FSTableDescriptors(this.dataFs, this.dataRootDir,
257      !canUpdateTableDescriptor(), cacheTableDescriptor(), tableDescriptorParallelLoadThreads);
258  }
259
260  public HBaseServerBase(Configuration conf, String name) throws IOException {
261    super(name); // thread name
262    this.readOnlyTransitionBlocked = new AtomicBoolean(false);
263    this.blockingActiveClusterId = new AtomicReference<>(null);
264    final Span span = TraceUtil.createSpan("HBaseServerBase.cxtor");
265    try (Scope ignored = span.makeCurrent()) {
266      this.conf = conf;
267      // Report HFile IO metrics only from server processes, not from clients and tools that also
268      // read or write HFiles. Done here so it covers both HRegionServer and HMaster, which reads
269      // and writes HFiles through its local MasterRegion. See HBASE-30231.
270      MetricsIO.enable();
271      this.eventLoopGroupConfig =
272        NettyEventLoopGroupConfig.setup(conf, getClass().getSimpleName() + "-EventLoopGroup");
273      this.startcode = EnvironmentEdgeManager.currentTime();
274      this.userProvider = UserProvider.instantiate(conf);
275      this.msgInterval = conf.getInt("hbase.regionserver.msginterval", 3 * 1000);
276      this.sleeper = new Sleeper(this.msgInterval, this);
277      this.namedQueueRecorder = createNamedQueueRecord();
278      useThisHostnameInstead = getUseThisHostnameInstead(conf);
279      // Resolve the hostname up-front and log in before creating the RpcServer. The RpcServer
280      // constructor reads UserGroupInformation.getCurrentUser() (HBASE-28321); if the server
281      // has not logged in yet, UGI bootstraps from the ticket cache and spawns a TGT renewer
282      // for whichever principal happens to be there.
283      String hostName = resolveHostName(conf, useThisHostnameInstead);
284      // login the zookeeper client principal (if using security)
285      ZKAuthentication.loginClient(this.conf, HConstants.ZK_CLIENT_KEYTAB_FILE,
286        HConstants.ZK_CLIENT_KERBEROS_PRINCIPAL, hostName);
287      // login the server principal (if using secure Hadoop)
288      login(userProvider, hostName);
289      this.rpcServices = createRpcServices();
290      InetSocketAddress addr = rpcServices.getSocketAddress();
291      serverName = ServerName.valueOf(hostName, addr.getPort(), this.startcode);
292      // init superusers and add the server principal (if using security)
293      // or process owner as default super user.
294      Superusers.initialize(conf);
295      zooKeeper =
296        new ZKWatcher(conf, getProcessName() + ":" + addr.getPort(), this, canCreateBaseZNode());
297
298      this.configurationManager = new ConfigurationManager();
299      setupSignalHandlers();
300
301      initializeFileSystem();
302
303      int choreServiceInitialSize =
304        conf.getInt(CHORE_SERVICE_INITIAL_POOL_SIZE, DEFAULT_CHORE_SERVICE_INITIAL_POOL_SIZE);
305      this.choreService = new ChoreService(getName(), choreServiceInitialSize, true);
306      this.executorService = new ExecutorService(getName());
307
308      this.metaRegionLocationCache = new MetaRegionLocationCache(zooKeeper);
309
310      if (clusterMode()) {
311        if (
312          conf.getBoolean(HBASE_SPLIT_WAL_COORDINATED_BY_ZK, DEFAULT_HBASE_SPLIT_COORDINATED_BY_ZK)
313        ) {
314          csm = new ZkCoordinatedStateManager(this);
315        } else {
316          csm = null;
317        }
318        clusterStatusTracker = new ClusterStatusTracker(zooKeeper, this);
319        clusterStatusTracker.start();
320      } else {
321        csm = null;
322        clusterStatusTracker = null;
323      }
324      putUpWebUI();
325      span.setStatus(StatusCode.OK);
326    } catch (Throwable t) {
327      TraceUtil.setError(span, t);
328      throw t;
329    } finally {
330      span.end();
331    }
332  }
333
334  /**
335   * Puts up the webui.
336   */
337  private void putUpWebUI() throws IOException {
338    int port =
339      this.conf.getInt(HConstants.REGIONSERVER_INFO_PORT, HConstants.DEFAULT_REGIONSERVER_INFOPORT);
340    String addr = this.conf.get("hbase.regionserver.info.bindAddress", "0.0.0.0");
341
342    boolean isMaster = false;
343    if (this instanceof HMaster) {
344      port = conf.getInt(HConstants.MASTER_INFO_PORT, HConstants.DEFAULT_MASTER_INFOPORT);
345      addr = this.conf.get("hbase.master.info.bindAddress", "0.0.0.0");
346      isMaster = true;
347    }
348    // -1 is for disabling info server
349    if (port < 0) {
350      return;
351    }
352
353    if (!Addressing.isLocalAddress(InetAddress.getByName(addr))) {
354      String msg = "Failed to start http info server. Address " + addr
355        + " does not belong to this host. Correct configuration parameter: "
356        + (isMaster ? "hbase.master.info.bindAddress" : "hbase.regionserver.info.bindAddress");
357      LOG.error(msg);
358      throw new IOException(msg);
359    }
360    // check if auto port bind enabled
361    boolean auto = this.conf.getBoolean(HConstants.REGIONSERVER_INFO_PORT_AUTO, false);
362    while (true) {
363      try {
364        this.infoServer = new InfoServer(getProcessName(), addr, port, false, this.conf);
365        infoServer.addPrivilegedServlet("dump", "/dump", getDumpServlet());
366        configureInfoServer(infoServer);
367        this.infoServer.start();
368        break;
369      } catch (BindException e) {
370        if (!auto) {
371          // auto bind disabled throw BindException
372          LOG.error("Failed binding http info server to port: " + port);
373          throw e;
374        }
375        // auto bind enabled, try to use another port
376        LOG.info("Failed binding http info server to port: " + port);
377        port++;
378        LOG.info("Retry starting http info server with port: " + port);
379      }
380    }
381    port = this.infoServer.getPort();
382    conf.setInt(HConstants.REGIONSERVER_INFO_PORT, port);
383    int masterInfoPort =
384      conf.getInt(HConstants.MASTER_INFO_PORT, HConstants.DEFAULT_MASTER_INFOPORT);
385    conf.setInt("hbase.master.info.port.orig", masterInfoPort);
386    conf.setInt(HConstants.MASTER_INFO_PORT, port);
387  }
388
389  /**
390   * Sets the abort state if not already set.
391   * @return True if abortRequested set to True successfully, false if an abort is already in
392   *         progress.
393   */
394  protected final boolean setAbortRequested() {
395    return abortRequested.compareAndSet(false, true);
396  }
397
398  @Override
399  public boolean isStopped() {
400    return stopped;
401  }
402
403  @Override
404  public boolean isAborted() {
405    return abortRequested.get();
406  }
407
408  @Override
409  public Configuration getConfiguration() {
410    return conf;
411  }
412
413  @Override
414  public AsyncClusterConnection getAsyncClusterConnection() {
415    return asyncClusterConnection;
416  }
417
418  @Override
419  public ZKWatcher getZooKeeper() {
420    return zooKeeper;
421  }
422
423  @Override
424  public KeymetaAdmin getKeymetaAdmin() {
425    return null;
426  }
427
428  @Override
429  public ManagedKeyDataCache getManagedKeyDataCache() {
430    return null;
431  }
432
433  @Override
434  public SystemKeyCache getSystemKeyCache() {
435    return null;
436  }
437
438  protected final void shutdownChore(ScheduledChore chore) {
439    if (chore != null) {
440      chore.shutdown();
441    }
442  }
443
444  protected final void initializeMemStoreChunkCreator(HeapMemoryManager hMemManager) {
445    if (MemStoreLAB.isEnabled(conf)) {
446      // MSLAB is enabled. So initialize MemStoreChunkPool
447      // By this time, the MemstoreFlusher is already initialized. We can get the global limits from
448      // it.
449      Pair<Long, MemoryType> pair = MemorySizeUtil.getGlobalMemStoreSize(conf);
450      long globalMemStoreSize = pair.getFirst();
451      boolean offheap = pair.getSecond() == MemoryType.NON_HEAP;
452      // When off heap memstore in use, take full area for chunk pool.
453      float poolSizePercentage = offheap
454        ? 1.0F
455        : conf.getFloat(MemStoreLAB.CHUNK_POOL_MAXSIZE_KEY, MemStoreLAB.POOL_MAX_SIZE_DEFAULT);
456      float initialCountPercentage = conf.getFloat(MemStoreLAB.CHUNK_POOL_INITIALSIZE_KEY,
457        MemStoreLAB.POOL_INITIAL_SIZE_DEFAULT);
458      int chunkSize = conf.getInt(MemStoreLAB.CHUNK_SIZE_KEY, MemStoreLAB.CHUNK_SIZE_DEFAULT);
459      float indexChunkSizePercent = conf.getFloat(MemStoreLAB.INDEX_CHUNK_SIZE_PERCENTAGE_KEY,
460        MemStoreLAB.INDEX_CHUNK_SIZE_PERCENTAGE_DEFAULT);
461      // init the chunkCreator
462      ChunkCreator.initialize(chunkSize, offheap, globalMemStoreSize, poolSizePercentage,
463        initialCountPercentage, hMemManager, indexChunkSizePercent);
464    }
465  }
466
467  protected abstract void stopChores();
468
469  protected final void stopChoreService() {
470    // clean up the scheduled chores
471    if (choreService != null) {
472      LOG.info("Shutdown chores and chore service");
473      stopChores();
474      // cancel the remaining scheduled chores (in case we missed out any)
475      // TODO: cancel will not cleanup the chores, so we need make sure we do not miss any
476      choreService.shutdown();
477    }
478  }
479
480  protected final void stopExecutorService() {
481    if (executorService != null) {
482      LOG.info("Shutdown executor service");
483      executorService.shutdown();
484    }
485  }
486
487  protected final void closeClusterConnection() {
488    if (asyncClusterConnection != null) {
489      LOG.info("Close async cluster connection");
490      try {
491        this.asyncClusterConnection.close();
492      } catch (IOException e) {
493        // Although the {@link Closeable} interface throws an {@link
494        // IOException}, in reality, the implementation would never do that.
495        LOG.warn("Attempt to close server's AsyncClusterConnection failed.", e);
496      }
497    }
498  }
499
500  protected final void stopInfoServer() {
501    if (this.infoServer != null) {
502      LOG.info("Stop info server");
503      try {
504        this.infoServer.stop();
505      } catch (Exception e) {
506        LOG.error("Failed to stop infoServer", e);
507      }
508    }
509  }
510
511  protected final void closeZooKeeper() {
512    if (this.zooKeeper != null) {
513      LOG.info("Close zookeeper");
514      this.zooKeeper.close();
515    }
516  }
517
518  protected final void closeTableDescriptors() {
519    if (this.tableDescriptors != null) {
520      LOG.info("Close table descriptors");
521      try {
522        this.tableDescriptors.close();
523      } catch (IOException e) {
524        LOG.debug("Failed to close table descriptors gracefully", e);
525      }
526    }
527  }
528
529  /**
530   * In order to register ShutdownHook, this method is called when HMaster and HRegionServer are
531   * started. For details, please refer to HBASE-26951
532   */
533  protected final void installShutdownHook() {
534    ShutdownHook.install(conf, dataFs, this, Thread.currentThread());
535    isShutdownHookInstalled = true;
536  }
537
538  @RestrictedApi(explanation = "Should only be called in tests", link = "",
539      allowedOnPath = ".*/src/test/.*")
540  public boolean isShutdownHookInstalled() {
541    return isShutdownHookInstalled;
542  }
543
544  @Override
545  public ServerName getServerName() {
546    return serverName;
547  }
548
549  @Override
550  public ChoreService getChoreService() {
551    return choreService;
552  }
553
554  /** Returns Return table descriptors implementation. */
555  public TableDescriptors getTableDescriptors() {
556    return this.tableDescriptors;
557  }
558
559  public ExecutorService getExecutorService() {
560    return executorService;
561  }
562
563  public AccessChecker getAccessChecker() {
564    return rpcServices.getAccessChecker();
565  }
566
567  public ZKPermissionWatcher getZKPermissionWatcher() {
568    return rpcServices.getZkPermissionWatcher();
569  }
570
571  @Override
572  public CoordinatedStateManager getCoordinatedStateManager() {
573    return csm;
574  }
575
576  @Override
577  public Connection createConnection(Configuration conf) throws IOException {
578    User user = UserProvider.instantiate(conf).getCurrent();
579    return ConnectionFactory.createConnection(conf, null, user);
580  }
581
582  /** Returns Return the rootDir. */
583  public Path getDataRootDir() {
584    return dataRootDir;
585  }
586
587  @Override
588  public FileSystem getFileSystem() {
589    return dataFs;
590  }
591
592  /** Returns Return the walRootDir. */
593  public Path getWALRootDir() {
594    return walRootDir;
595  }
596
597  /** Returns Return the walFs. */
598  public FileSystem getWALFileSystem() {
599    return walFs;
600  }
601
602  /** Returns True if the cluster is up. */
603  public boolean isClusterUp() {
604    return !clusterMode() || this.clusterStatusTracker.isClusterUp();
605  }
606
607  /** Returns time stamp in millis of when this server was started */
608  public long getStartcode() {
609    return this.startcode;
610  }
611
612  public InfoServer getInfoServer() {
613    return infoServer;
614  }
615
616  public int getMsgInterval() {
617    return msgInterval;
618  }
619
620  /**
621   * get NamedQueue Provider to add different logs to ringbuffer
622   */
623  public NamedQueueRecorder getNamedQueueRecorder() {
624    return this.namedQueueRecorder;
625  }
626
627  public RpcServerInterface getRpcServer() {
628    return rpcServices.getRpcServer();
629  }
630
631  public NettyEventLoopGroupConfig getEventLoopGroupConfig() {
632    return eventLoopGroupConfig;
633  }
634
635  public R getRpcServices() {
636    return rpcServices;
637  }
638
639  @RestrictedApi(explanation = "Should only be called in tests", link = "",
640      allowedOnPath = ".*/src/test/.*")
641  public MetaRegionLocationCache getMetaRegionLocationCache() {
642    return this.metaRegionLocationCache;
643  }
644
645  @RestrictedApi(explanation = "Should only be called in tests", link = "",
646      allowedOnPath = ".*/src/test/.*")
647  public ConfigurationManager getConfigurationManager() {
648    return configurationManager;
649  }
650
651  /**
652   * Reload the configuration from disk.
653   */
654  public void updateConfiguration() throws IOException {
655    LOG.info("Reloading the configuration from disk.");
656    // Reload the configuration from disk.
657    preUpdateConfiguration();
658    this.readOnlyTransitionBlocked.set(false);
659    this.blockingActiveClusterId.set(null);
660    conf.reloadConfiguration();
661    configurationManager.notifyAllObservers(conf);
662    this.checkForBlockedReadOnlyTransition();
663    postUpdateConfiguration();
664  }
665
666  protected Configuration blockReadOnlyTransition(Configuration updatedConf,
667    String activeClusterId) {
668    this.blockingActiveClusterId.set(activeClusterId);
669    LOG.error(
670      "Cannot disable read-only mode. The {} file contains a different cluster ID ({}), which means "
671        + "that cluster is already the active cluster. Reverting {} to true",
672      HConstants.ACTIVE_CLUSTER_SUFFIX_FILE_NAME, this.blockingActiveClusterId.get(),
673      HConstants.HBASE_GLOBAL_READONLY_ENABLED_KEY);
674    this.readOnlyTransitionBlocked.set(true);
675    return ConfigurationUtil.copyWithReadOnlyModeEnabled(updatedConf);
676  }
677
678  protected void checkForBlockedReadOnlyTransition() throws ReadOnlyTransitionException {
679    if (this.readOnlyTransitionBlocked.get()) {
680      throw new ReadOnlyTransitionException(
681        "Cannot disable read-only mode because another active cluster already exists on this "
682          + "storage location. The read-only coprocessors have not been removed.",
683        this.blockingActiveClusterId.get());
684    }
685  }
686
687  @Override
688  public KeyManagementService getKeyManagementService() {
689    return this;
690  }
691
692  private void preUpdateConfiguration() throws IOException {
693    CoprocessorHost<?, ?> coprocessorHost = getCoprocessorHost();
694    if (coprocessorHost instanceof RegionServerCoprocessorHost) {
695      ((RegionServerCoprocessorHost) coprocessorHost).preUpdateConfiguration(conf);
696    } else if (coprocessorHost instanceof MasterCoprocessorHost) {
697      ((MasterCoprocessorHost) coprocessorHost).preUpdateConfiguration(conf);
698    }
699  }
700
701  private void postUpdateConfiguration() throws IOException {
702    CoprocessorHost<?, ?> coprocessorHost = getCoprocessorHost();
703    if (coprocessorHost instanceof RegionServerCoprocessorHost) {
704      ((RegionServerCoprocessorHost) coprocessorHost).postUpdateConfiguration(conf);
705    } else if (coprocessorHost instanceof MasterCoprocessorHost) {
706      ((MasterCoprocessorHost) coprocessorHost).postUpdateConfiguration(conf);
707    }
708  }
709
710  @Override
711  public String toString() {
712    return getServerName().toString();
713  }
714
715  protected abstract CoprocessorHost<?, ?> getCoprocessorHost();
716
717  protected abstract boolean canCreateBaseZNode();
718
719  protected abstract String getProcessName();
720
721  protected abstract R createRpcServices() throws IOException;
722
723  protected abstract String getUseThisHostnameInstead(Configuration conf) throws IOException;
724
725  protected abstract void login(UserProvider user, String host) throws IOException;
726
727  protected abstract DNS.ServerType getDNSServerType();
728
729  private String resolveHostName(Configuration conf, String useThisHostnameInstead)
730    throws IOException {
731    if (!StringUtils.isBlank(useThisHostnameInstead)) {
732      return useThisHostnameInstead;
733    }
734    // if use-ip is enabled, we will use ip to expose Master/RS service for client,
735    // see HBASE-27304 for details.
736    boolean useIp = conf.getBoolean(HConstants.HBASE_SERVER_USEIP_ENABLED_KEY,
737      HConstants.HBASE_SERVER_USEIP_ENABLED_DEFAULT);
738    InetAddress addr = InetAddress.getByName(DNS.getHostname(conf, getDNSServerType()));
739    return useIp ? addr.getHostAddress() : addr.getHostName();
740  }
741
742  protected abstract NamedQueueRecorder createNamedQueueRecord();
743
744  protected abstract void configureInfoServer(InfoServer infoServer);
745
746  protected abstract Class<? extends HttpServlet> getDumpServlet();
747
748  protected abstract boolean canUpdateTableDescriptor();
749
750  protected abstract boolean cacheTableDescriptor();
751
752  protected abstract boolean clusterMode();
753}