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