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;
019
020import java.io.IOException;
021import java.util.List;
022import java.util.concurrent.Semaphore;
023import org.apache.hadoop.hbase.Server;
024import org.apache.hadoop.hbase.ServerName;
025import org.apache.hadoop.hbase.TableDescriptors;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.TableNotDisabledException;
028import org.apache.hadoop.hbase.TableNotFoundException;
029import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
030import org.apache.hadoop.hbase.client.MasterSwitchType;
031import org.apache.hadoop.hbase.client.NormalizeTableFilterParams;
032import org.apache.hadoop.hbase.client.RegionInfo;
033import org.apache.hadoop.hbase.client.TableDescriptor;
034import org.apache.hadoop.hbase.executor.ExecutorService;
035import org.apache.hadoop.hbase.favored.FavoredNodesManager;
036import org.apache.hadoop.hbase.keymeta.KeyManagementService;
037import org.apache.hadoop.hbase.master.assignment.AssignmentManager;
038import org.apache.hadoop.hbase.master.hbck.HbckChore;
039import org.apache.hadoop.hbase.master.janitor.CatalogJanitor;
040import org.apache.hadoop.hbase.master.locking.LockManager;
041import org.apache.hadoop.hbase.master.normalizer.RegionNormalizerManager;
042import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
043import org.apache.hadoop.hbase.master.replication.ReplicationPeerManager;
044import org.apache.hadoop.hbase.master.replication.SyncReplicationReplayWALManager;
045import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
046import org.apache.hadoop.hbase.master.zksyncer.MetaLocationSyncer;
047import org.apache.hadoop.hbase.procedure.MasterProcedureManagerHost;
048import org.apache.hadoop.hbase.procedure2.LockedResource;
049import org.apache.hadoop.hbase.procedure2.Procedure;
050import org.apache.hadoop.hbase.procedure2.ProcedureEvent;
051import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
052import org.apache.hadoop.hbase.quotas.MasterQuotaManager;
053import org.apache.hadoop.hbase.replication.ReplicationException;
054import org.apache.hadoop.hbase.replication.ReplicationPeerConfig;
055import org.apache.hadoop.hbase.replication.ReplicationPeerDescription;
056import org.apache.hadoop.hbase.replication.SyncReplicationState;
057import org.apache.hadoop.hbase.replication.master.ReplicationLogCleanerBarrier;
058import org.apache.hadoop.hbase.rsgroup.RSGroupInfoManager;
059import org.apache.hadoop.hbase.security.access.AccessChecker;
060import org.apache.hadoop.hbase.security.access.ZKPermissionWatcher;
061import org.apache.yetus.audience.InterfaceAudience;
062
063import org.apache.hbase.thirdparty.com.google.protobuf.Service;
064
065/**
066 * A curated subset of services provided by {@link HMaster}. For use internally only. Passed to
067 * Managers, Services and Chores so can pass less-than-a full-on HMaster at test-time. Be judicious
068 * adding API. Changes cause ripples through the code base.
069 */
070@InterfaceAudience.Private
071public interface MasterServices extends Server, KeyManagementService {
072  /** Returns the underlying snapshot manager */
073  SnapshotManager getSnapshotManager();
074
075  /** Returns the underlying MasterProcedureManagerHost */
076  MasterProcedureManagerHost getMasterProcedureManagerHost();
077
078  /** Returns Master's instance of {@link ClusterSchema} */
079  ClusterSchema getClusterSchema();
080
081  /** Returns Master's instance of the {@link AssignmentManager} */
082  AssignmentManager getAssignmentManager();
083
084  /** Returns Master's filesystem {@link MasterFileSystem} utility class. */
085  MasterFileSystem getMasterFileSystem();
086
087  /** Returns Master's WALs {@link MasterWalManager} utility class. */
088  MasterWalManager getMasterWalManager();
089
090  /** Rotates the system key if changed, returns true if a new key was detected and rotated */
091  boolean rotateSystemKeyIfChanged() throws IOException;
092
093  /** Returns Master's {@link ServerManager} instance. */
094  ServerManager getServerManager();
095
096  /** Returns Master's instance of {@link ExecutorService} */
097  ExecutorService getExecutorService();
098
099  /** Returns Master's instance of {@link TableStateManager} */
100  TableStateManager getTableStateManager();
101
102  /** Returns Master's instance of {@link MasterCoprocessorHost} */
103  MasterCoprocessorHost getMasterCoprocessorHost();
104
105  /** Returns Master's instance of {@link MasterQuotaManager} */
106  MasterQuotaManager getMasterQuotaManager();
107
108  /** Returns Master's instance of {@link RegionNormalizerManager} */
109  RegionNormalizerManager getRegionNormalizerManager();
110
111  /** Returns Master's instance of {@link CatalogJanitor} */
112  CatalogJanitor getCatalogJanitor();
113
114  /** Returns Master's instance of {@link HbckChore} */
115  HbckChore getHbckChore();
116
117  /** Returns Master's instance of {@link ProcedureExecutor} */
118  ProcedureExecutor<MasterProcedureEnv> getMasterProcedureExecutor();
119
120  /** Returns Tripped when Master has finished initialization. */
121  public ProcedureEvent<?> getInitializedEvent();
122
123  /** Returns Master's instance of {@link MetricsMaster} */
124  MetricsMaster getMasterMetrics();
125
126  /**
127   * Check table is modifiable; i.e. exists and is offline.
128   * @param tableName Name of table to check.
129   */
130  // We actually throw the exceptions mentioned in the
131  void checkTableModifiable(final TableName tableName)
132    throws IOException, TableNotFoundException, TableNotDisabledException;
133
134  /**
135   * Create a table using the given table definition.
136   * @param desc      The table definition
137   * @param splitKeys Starting row keys for the initial table regions. If null a single region is
138   *                  created.
139   */
140  long createTable(final TableDescriptor desc, final byte[][] splitKeys, final long nonceGroup,
141    final long nonce) throws IOException;
142
143  /**
144   * Create a system table using the given table definition.
145   * @param tableDescriptor The system table definition a single region is created.
146   */
147  long createSystemTable(final TableDescriptor tableDescriptor) throws IOException;
148
149  /**
150   * Delete a table
151   * @param tableName The table name
152   */
153  long deleteTable(final TableName tableName, final long nonceGroup, final long nonce)
154    throws IOException;
155
156  /**
157   * Truncate a table
158   * @param tableName      The table name
159   * @param preserveSplits True if the splits should be preserved
160   */
161  public long truncateTable(final TableName tableName, final boolean preserveSplits,
162    final long nonceGroup, final long nonce) throws IOException;
163
164  /**
165   * Modify the descriptor of an existing table
166   * @param tableName  The table name
167   * @param descriptor The updated table descriptor
168   */
169  default long modifyTable(final TableName tableName, final TableDescriptor descriptor,
170    final long nonceGroup, final long nonce) throws IOException {
171    return modifyTable(tableName, descriptor, nonceGroup, nonce, true);
172  }
173
174  /**
175   * Modify the descriptor of an existing table
176   * @param tableName     The table name
177   * @param descriptor    The updated table descriptor
178   * @param reopenRegions Whether to reopen regions after modifying the table descriptor
179   */
180  long modifyTable(final TableName tableName, final TableDescriptor descriptor,
181    final long nonceGroup, final long nonce, final boolean reopenRegions) throws IOException;
182
183  /**
184   * Modify the store file tracker of an existing table
185   */
186  long modifyTableStoreFileTracker(final TableName tableName, final String dstSFT,
187    final long nonceGroup, final long nonce) throws IOException;
188
189  /**
190   * Enable an existing table
191   * @param tableName The table name
192   */
193  long enableTable(final TableName tableName, final long nonceGroup, final long nonce)
194    throws IOException;
195
196  /**
197   * Disable an existing table
198   * @param tableName The table name
199   */
200  long disableTable(final TableName tableName, final long nonceGroup, final long nonce)
201    throws IOException;
202
203  /**
204   * Add a new column to an existing table
205   * @param tableName The table name
206   * @param column    The column definition
207   */
208  long addColumn(final TableName tableName, final ColumnFamilyDescriptor column,
209    final long nonceGroup, final long nonce) throws IOException;
210
211  /**
212   * Modify the column descriptor of an existing column in an existing table
213   * @param tableName  The table name
214   * @param descriptor The updated column definition
215   */
216  long modifyColumn(final TableName tableName, final ColumnFamilyDescriptor descriptor,
217    final long nonceGroup, final long nonce) throws IOException;
218
219  /**
220   * Modify the store file tracker of an existing column in an existing table
221   */
222  long modifyColumnStoreFileTracker(final TableName tableName, final byte[] family,
223    final String dstSFT, final long nonceGroup, final long nonce) throws IOException;
224
225  /**
226   * Delete a column from an existing table
227   * @param tableName  The table name
228   * @param columnName The column name
229   */
230  long deleteColumn(final TableName tableName, final byte[] columnName, final long nonceGroup,
231    final long nonce) throws IOException;
232
233  /**
234   * Merge regions in a table.
235   * @param regionsToMerge daughter regions to merge
236   * @param forcible       whether to force to merge even two regions are not adjacent
237   * @param nonceGroup     used to detect duplicate
238   * @param nonce          used to detect duplicate
239   * @return procedure Id
240   */
241  long mergeRegions(final RegionInfo[] regionsToMerge, final boolean forcible,
242    final long nonceGroup, final long nonce) throws IOException;
243
244  /**
245   * Split a region.
246   * @param regionInfo region to split
247   * @param splitRow   split point
248   * @param nonceGroup used to detect duplicate
249   * @param nonce      used to detect duplicate
250   * @return procedure Id
251   */
252  long splitRegion(final RegionInfo regionInfo, final byte[] splitRow, final long nonceGroup,
253    final long nonce) throws IOException;
254
255  /** Returns Return table descriptors implementation. */
256  TableDescriptors getTableDescriptors();
257
258  /**
259   * Registers a new protocol buffer {@link Service} subclass as a master coprocessor endpoint.
260   * <p/>
261   * Only a single instance may be registered for a given {@link Service} subclass (the instances
262   * are keyed on
263   * {@link org.apache.hbase.thirdparty.com.google.protobuf.Descriptors.ServiceDescriptor#getFullName()}.
264   * After the first registration, subsequent calls with the same service name will fail with a
265   * return value of {@code false}.
266   * @param instance the {@code Service} subclass instance to expose as a coprocessor endpoint
267   * @return {@code true} if the registration was successful, {@code false} otherwise
268   */
269  boolean registerService(Service instance);
270
271  /** Returns true if master is the active one */
272  boolean isActiveMaster();
273
274  /** Returns timestamp in millis when this master became the active one. */
275  long getMasterActiveTime();
276
277  /** Returns true if master is initialized */
278  boolean isInitialized();
279
280  /**
281   * @return true if master is in maintanceMode
282   * @throws IOException if the inquiry failed due to an IO problem
283   */
284  boolean isInMaintenanceMode();
285
286  /**
287   * Checks master state before initiating action over region topology.
288   * @param action the name of the action under consideration, for logging.
289   * @return {@code true} when the caller should exit early, {@code false} otherwise.
290   */
291  boolean skipRegionManagementAction(final String action);
292
293  /**
294   * Abort a procedure.
295   * @param procId                ID of the procedure
296   * @param mayInterruptIfRunning if the proc completed at least one step, should it be aborted?
297   * @return true if aborted, false if procedure already completed or does not exist
298   */
299  public boolean abortProcedure(final long procId, final boolean mayInterruptIfRunning)
300    throws IOException;
301
302  /**
303   * Get procedures
304   * @return procedure list
305   */
306  public List<Procedure<?>> getProcedures() throws IOException;
307
308  /**
309   * Get locks
310   * @return lock list
311   */
312  public List<LockedResource> getLocks() throws IOException;
313
314  /**
315   * Get list of table descriptors by namespace
316   * @param name namespace name
317   */
318  public List<TableDescriptor> listTableDescriptorsByNamespace(String name) throws IOException;
319
320  /**
321   * Get list of table names by namespace
322   * @param name namespace name
323   * @return table names
324   */
325  public List<TableName> listTableNamesByNamespace(String name) throws IOException;
326
327  /**
328   * @param table the table for which last successful major compaction time is queried
329   * @return the timestamp of the last successful major compaction for the passed table, or 0 if no
330   *         HFile resulting from a major compaction exists
331   */
332  public long getLastMajorCompactionTimestamp(TableName table) throws IOException;
333
334  /**
335   * Returns the timestamp of the last successful major compaction for the passed region or 0 if no
336   * HFile resulting from a major compaction exists
337   */
338  public long getLastMajorCompactionTimestampForRegion(byte[] regionName) throws IOException;
339
340  /** Returns load balancer */
341  public LoadBalancer getLoadBalancer();
342
343  boolean isSplitOrMergeEnabled(MasterSwitchType switchType);
344
345  /** Returns Favored Nodes Manager */
346  public FavoredNodesManager getFavoredNodesManager();
347
348  /**
349   * Add a new replication peer for replicating data to slave cluster
350   * @param peerId     a short name that identifies the peer
351   * @param peerConfig configuration for the replication slave cluster
352   * @param enabled    peer state, true if ENABLED and false if DISABLED
353   */
354  long addReplicationPeer(String peerId, ReplicationPeerConfig peerConfig, boolean enabled)
355    throws ReplicationException, IOException;
356
357  /**
358   * Removes a peer and stops the replication
359   * @param peerId a short name that identifies the peer
360   */
361  long removeReplicationPeer(String peerId) throws ReplicationException, IOException;
362
363  /**
364   * Restart the replication stream to the specified peer
365   * @param peerId a short name that identifies the peer
366   */
367  long enableReplicationPeer(String peerId) throws ReplicationException, IOException;
368
369  /**
370   * Stop the replication stream to the specified peer
371   * @param peerId a short name that identifies the peer
372   */
373  long disableReplicationPeer(String peerId) throws ReplicationException, IOException;
374
375  /**
376   * Returns the configured ReplicationPeerConfig for the specified peer
377   * @param peerId a short name that identifies the peer
378   * @return ReplicationPeerConfig for the peer
379   */
380  ReplicationPeerConfig getReplicationPeerConfig(String peerId)
381    throws ReplicationException, IOException;
382
383  /**
384   * Returns the {@link ReplicationPeerManager}.
385   */
386  ReplicationPeerManager getReplicationPeerManager();
387
388  /**
389   * Returns the {@link ReplicationLogCleanerBarrier}. It will be used at multiple places so we put
390   * it in MasterServices directly.
391   */
392  ReplicationLogCleanerBarrier getReplicationLogCleanerBarrier();
393
394  /**
395   * Returns the SyncReplicationPeerLock.
396   */
397  Semaphore getSyncReplicationPeerLock();
398
399  /**
400   * Returns the {@link SyncReplicationReplayWALManager}.
401   */
402  SyncReplicationReplayWALManager getSyncReplicationReplayWALManager();
403
404  /**
405   * Update the peerConfig for the specified peer
406   * @param peerId     a short name that identifies the peer
407   * @param peerConfig new config for the peer
408   */
409  long updateReplicationPeerConfig(String peerId, ReplicationPeerConfig peerConfig)
410    throws ReplicationException, IOException;
411
412  /**
413   * Return a list of replication peers.
414   * @param regex The regular expression to match peer id
415   * @return a list of replication peers description
416   */
417  List<ReplicationPeerDescription> listReplicationPeers(String regex)
418    throws ReplicationException, IOException;
419
420  /**
421   * Set current cluster state for a synchronous replication peer.
422   * @param peerId       a short name that identifies the peer
423   * @param clusterState state of current cluster
424   */
425  long transitReplicationPeerSyncReplicationState(String peerId, SyncReplicationState clusterState)
426    throws ReplicationException, IOException;
427
428  boolean replicationPeerModificationSwitch(boolean on) throws IOException;
429
430  boolean isReplicationPeerModificationEnabled();
431
432  /** Returns {@link LockManager} to lock namespaces/tables/regions. */
433  LockManager getLockManager();
434
435  public String getRegionServerVersion(final ServerName sn);
436
437  /**
438   * Called when a new RegionServer is added to the cluster. Checks if new server has a newer
439   * version than any existing server and will move system tables there if so.
440   */
441  public void checkIfShouldMoveSystemRegionAsync();
442
443  String getClientIdAuditPrefix();
444
445  /** Returns True if cluster is up; false if cluster is not up (we are shutting down). */
446  boolean isClusterUp();
447
448  /** Returns return null if current is zk-based WAL splitting */
449  default SplitWALManager getSplitWALManager() {
450    return null;
451  }
452
453  /** Returns the {@link AccessChecker} */
454  AccessChecker getAccessChecker();
455
456  /** Returns the {@link ZKPermissionWatcher} */
457  ZKPermissionWatcher getZKPermissionWatcher();
458
459  /**
460   * Execute region plans with throttling
461   * @param plans to execute
462   * @return succeeded plans
463   */
464  List<RegionPlan> executeRegionPlansWithThrottling(List<RegionPlan> plans);
465
466  /**
467   * Run the ReplicationBarrierChore.
468   */
469  void runReplicationBarrierCleaner();
470
471  /** Returns the {@link RSGroupInfoManager} */
472  RSGroupInfoManager getRSGroupInfoManager();
473
474  /**
475   * Queries the state of the {@code LoadBalancerStateStore}. If the balancer is not initialized,
476   * false is returned.
477   * @return The state of the load balancer, or false if the load balancer isn't defined.
478   */
479  boolean isBalancerOn();
480
481  /**
482   * Perform normalization of cluster.
483   * @param ntfp           Selection criteria for identifying which tables to normalize.
484   * @param isHighPriority {@code true} when these requested tables should skip to the front of the
485   *                       queue.
486   * @return {@code true} when the request was submitted, {@code false} otherwise.
487   */
488  boolean normalizeRegions(final NormalizeTableFilterParams ntfp, final boolean isHighPriority)
489    throws IOException;
490
491  /**
492   * Get the meta location syncer.
493   * <p/>
494   * We need to get this in MTP to tell the syncer the new meta replica count.
495   */
496  MetaLocationSyncer getMetaLocationSyncer();
497
498  /**
499   * Flush master local region
500   */
501  void flushMasterStore() throws IOException;
502
503  /**
504   * Flush an existing table
505   * @param tableName      The table name
506   * @param columnFamilies The column families to flush
507   * @param nonceGroup     the nonce group
508   * @param nonce          the nonce
509   * @return the flush procedure id
510   */
511  long flushTable(final TableName tableName, final List<byte[]> columnFamilies,
512    final long nonceGroup, final long nonce) throws IOException;
513
514  /**
515   * Truncate region
516   * @param regionInfo region to be truncated
517   * @param nonceGroup the nonce group
518   * @param nonce      the nonce
519   * @return procedure Id
520   */
521  long truncateRegion(RegionInfo regionInfo, long nonceGroup, long nonce) throws IOException;
522
523  /**
524   * Roll WAL writer for all RegionServers
525   * @return procedure id
526   */
527  long rollAllWALWriters(long nonceGroup, long nonce) throws IOException;
528}