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