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.client;
020
021import java.util.Arrays;
022import java.util.Collection;
023import java.util.Comparator;
024import java.util.Iterator;
025import java.util.Map;
026import java.util.Set;
027import java.util.stream.Collectors;
028import java.util.stream.Stream;
029import org.apache.hadoop.hbase.HConstants;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.util.Bytes;
032import org.apache.yetus.audience.InterfaceAudience;
033
034/**
035 * TableDescriptor contains the details about an HBase table such as the descriptors of
036 * all the column families, is the table a catalog table, <code> hbase:meta </code>,
037 * if the table is read only, the maximum size of the memstore,
038 * when the region split should occur, coprocessors associated with it etc...
039 */
040@InterfaceAudience.Public
041public interface TableDescriptor {
042
043  @InterfaceAudience.Private
044  Comparator<TableDescriptor> COMPARATOR = getComparator(ColumnFamilyDescriptor.COMPARATOR);
045
046  @InterfaceAudience.Private
047  Comparator<TableDescriptor> COMPARATOR_IGNORE_REPLICATION =
048      getComparator(ColumnFamilyDescriptor.COMPARATOR_IGNORE_REPLICATION);
049
050  static Comparator<TableDescriptor>
051      getComparator(Comparator<ColumnFamilyDescriptor> cfComparator) {
052    return (TableDescriptor lhs, TableDescriptor rhs) -> {
053      int result = lhs.getTableName().compareTo(rhs.getTableName());
054      if (result != 0) {
055        return result;
056      }
057      Collection<ColumnFamilyDescriptor> lhsFamilies = Arrays.asList(lhs.getColumnFamilies());
058      Collection<ColumnFamilyDescriptor> rhsFamilies = Arrays.asList(rhs.getColumnFamilies());
059      result = Integer.compare(lhsFamilies.size(), rhsFamilies.size());
060      if (result != 0) {
061        return result;
062      }
063
064      for (Iterator<ColumnFamilyDescriptor> it = lhsFamilies.iterator(), it2 =
065          rhsFamilies.iterator(); it.hasNext();) {
066        result = cfComparator.compare(it.next(), it2.next());
067        if (result != 0) {
068          return result;
069        }
070      }
071      // punt on comparison for ordering, just calculate difference
072      return Integer.compare(lhs.getValues().hashCode(), rhs.getValues().hashCode());
073    };
074  }
075
076  /**
077   * Returns the count of the column families of the table.
078   *
079   * @return Count of column families of the table
080   */
081  int getColumnFamilyCount();
082
083  /**
084   * Return the list of attached co-processor represented
085   *
086   * @return The list of CoprocessorDescriptor
087   */
088  Collection<CoprocessorDescriptor> getCoprocessorDescriptors();
089
090  /**
091   * Return the list of attached co-processor represented by their name
092   * className
093   * @return The list of co-processors classNames
094   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0.
095   *                       Use {@link #getCoprocessorDescriptors()} instead
096   */
097  @Deprecated
098  default Collection<String> getCoprocessors() {
099    return getCoprocessorDescriptors().stream()
100      .map(CoprocessorDescriptor::getClassName)
101      .collect(Collectors.toList());
102  }
103
104  /**
105   * Returns the durability setting for the table.
106   *
107   * @return durability setting for the table.
108   */
109  Durability getDurability();
110
111  /**
112   * Returns an unmodifiable collection of all the {@link ColumnFamilyDescriptor} of
113   * all the column families of the table.
114   *
115   * @return An array of {@link ColumnFamilyDescriptor} of all the column
116   * families.
117   */
118  ColumnFamilyDescriptor[] getColumnFamilies();
119
120  /**
121   * Returns all the column family names of the current table. The map of
122   * TableDescriptor contains mapping of family name to ColumnDescriptor.
123   * This returns all the keys of the family map which represents the column
124   * family names of the table.
125   *
126   * @return Immutable sorted set of the keys of the families.
127   */
128  Set<byte[]> getColumnFamilyNames();
129
130  /**
131   * Returns the ColumnDescriptor for a specific column family with name as
132   * specified by the parameter column.
133   *
134   * @param name Column family name
135   * @return Column descriptor for the passed family name or the family on
136   * passed in column.
137   */
138  ColumnFamilyDescriptor getColumnFamily(final byte[] name);
139
140  /**
141   * This gets the class associated with the flush policy which determines the
142   * stores need to be flushed when flushing a region. The class used by default
143   * is defined in org.apache.hadoop.hbase.regionserver.FlushPolicy.
144   *
145   * @return the class name of the flush policy for this table. If this returns
146   * null, the default flush policy is used.
147   */
148  String getFlushPolicyClassName();
149
150  /**
151   * Returns the maximum size upto which a region can grow to after which a
152   * region split is triggered. The region size is represented by the size of
153   * the biggest store file in that region.
154   *
155   * @return max hregion size for table, -1 if not set.
156   */
157  long getMaxFileSize();
158
159  /**
160   * Returns the size of the memstore after which a flush to filesystem is
161   * triggered.
162   *
163   * @return memory cache flush size for each hregion, -1 if not set.
164   */
165  long getMemStoreFlushSize();
166
167  // TODO: Currently this is used RPC scheduling only. Make it more generic than this; allow it
168  // to also be priority when scheduling procedures that pertain to this table scheduling first
169  // those tables with the highest priority (From Yi Liang over on HBASE-18109).
170  int getPriority();
171
172  /**
173   * @return Returns the configured replicas per region
174   */
175  int getRegionReplication();
176
177  /**
178   * This gets the class associated with the region split policy which
179   * determines when a region split should occur. The class used by default is
180   * defined in org.apache.hadoop.hbase.regionserver.RegionSplitPolicy
181   *
182   * @return the class name of the region split policy for this table. If this
183   * returns null, the default split policy is used.
184   */
185  String getRegionSplitPolicyClassName();
186
187  /**
188   * Get the name of the table
189   *
190   * @return TableName
191   */
192  TableName getTableName();
193
194  @Deprecated
195  String getOwnerString();
196
197  /**
198   * Getter for accessing the metadata associated with the key.
199   *
200   * @param key The key.
201   * @return A clone value. Null if no mapping for the key
202   */
203  Bytes getValue(Bytes key);
204
205  /**
206   * Getter for accessing the metadata associated with the key.
207   *
208   * @param key The key.
209   * @return A clone value. Null if no mapping for the key
210   */
211  byte[] getValue(byte[] key);
212
213  /**
214   * Getter for accessing the metadata associated with the key.
215   *
216   * @param key The key.
217   * @return Null if no mapping for the key
218   */
219  String getValue(String key);
220
221  /**
222   * @return Getter for fetching an unmodifiable map.
223   */
224  Map<Bytes, Bytes> getValues();
225
226  /**
227   * Check if the table has an attached co-processor represented by the name
228   * className
229   *
230   * @param classNameToMatch - Class name of the co-processor
231   * @return true of the table has a co-processor className
232   */
233  boolean hasCoprocessor(String classNameToMatch);
234
235  /**
236   * Checks to see if this table contains the given column family
237   *
238   * @param name Family name or column name.
239   * @return true if the table contains the specified family name
240   */
241  boolean hasColumnFamily(final byte[] name);
242
243  /**
244   * @return true if the read-replicas memstore replication is enabled.
245   */
246  boolean hasRegionMemStoreReplication();
247
248  /**
249   * Check if the compaction enable flag of the table is true. If flag is false
250   * then no minor/major compactions will be done in real.
251   *
252   * @return true if table compaction enabled
253   */
254  boolean isCompactionEnabled();
255
256  /**
257   * Check if the split enable flag of the table is true. If flag is false
258   * then no region split will be done.
259   *
260   * @return true if table region split enabled
261   */
262  boolean isSplitEnabled();
263
264  /**
265   * Check if the merge enable flag of the table is true. If flag is false
266   * then no region merge will be done.
267   *
268   * @return true if table region merge enabled
269   */
270  boolean isMergeEnabled();
271
272  /**
273   * Checks if this table is <code> hbase:meta </code> region.
274   *
275   * @return true if this table is <code> hbase:meta </code> region
276   */
277  boolean isMetaRegion();
278
279  /**
280   * Checks if the table is a <code>hbase:meta</code> table
281   *
282   * @return true if table is <code> hbase:meta </code> region.
283   */
284  boolean isMetaTable();
285
286  /**
287   * Check if normalization enable flag of the table is true. If flag is false
288   * then no region normalizer won't attempt to normalize this table.
289   *
290   * @return true if region normalization is enabled for this table
291   */
292  boolean isNormalizationEnabled();
293
294  /**
295   * Check if there is the target region count. If so, the normalize plan will
296   * be calculated based on the target region count.
297   *
298   * @return target region count after normalize done
299   */
300  int getNormalizerTargetRegionCount();
301
302  /**
303   * Check if there is the target region size. If so, the normalize plan will
304   * be calculated based on the target region size.
305   *
306   * @return target region size after normalize done
307   */
308  long getNormalizerTargetRegionSize();
309
310  /**
311   * Check if the readOnly flag of the table is set. If the readOnly flag is set
312   * then the contents of the table can only be read from but not modified.
313   *
314   * @return true if all columns in the table should be read only
315   */
316  boolean isReadOnly();
317
318  /**
319   * @return Name of this table and then a map of all of the column family descriptors (with only
320   *         the non-default column family attributes)
321   */
322  String toStringCustomizedValues();
323
324  /**
325   * Check if any of the table's cfs' replication scope are set to
326   * {@link HConstants#REPLICATION_SCOPE_GLOBAL}.
327   * @return {@code true} if we have, otherwise {@code false}.
328   */
329  default boolean hasGlobalReplicationScope() {
330    return Stream.of(getColumnFamilies())
331      .anyMatch(cf -> cf.getScope() == HConstants.REPLICATION_SCOPE_GLOBAL);
332  }
333
334  /**
335   * Check if the table's cfs' replication scope matched with the replication state
336   * @param enabled replication state
337   * @return true if matched, otherwise false
338   */
339  default boolean matchReplicationScope(boolean enabled) {
340    boolean hasEnabled = false;
341    boolean hasDisabled = false;
342
343    for (ColumnFamilyDescriptor cf : getColumnFamilies()) {
344      if (cf.getScope() != HConstants.REPLICATION_SCOPE_GLOBAL) {
345        hasDisabled = true;
346      } else {
347        hasEnabled = true;
348      }
349    }
350
351    if (hasEnabled && hasDisabled) {
352      return false;
353    }
354    if (hasEnabled) {
355      return enabled;
356    }
357    return !enabled;
358  }
359}