View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.regionserver;
19  
20  import java.io.IOException;
21  import java.util.Collection;
22  import java.util.List;
23  import java.util.NavigableSet;
24  
25  import org.apache.hadoop.fs.FileSystem;
26  import org.apache.hadoop.fs.Path;
27  import org.apache.hadoop.hbase.Cell;
28  import org.apache.hadoop.hbase.CellComparator;
29  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
30  import org.apache.hadoop.hbase.HColumnDescriptor;
31  import org.apache.hadoop.hbase.HRegionInfo;
32  import org.apache.hadoop.hbase.TableName;
33  import org.apache.hadoop.hbase.classification.InterfaceAudience;
34  import org.apache.hadoop.hbase.classification.InterfaceStability;
35  import org.apache.hadoop.hbase.client.Scan;
36  import org.apache.hadoop.hbase.conf.PropagatingConfigurationObserver;
37  import org.apache.hadoop.hbase.io.HeapSize;
38  import org.apache.hadoop.hbase.io.compress.Compression;
39  import org.apache.hadoop.hbase.io.hfile.CacheConfig;
40  import org.apache.hadoop.hbase.io.hfile.HFileDataBlockEncoder;
41  import org.apache.hadoop.hbase.protobuf.generated.WALProtos.CompactionDescriptor;
42  import org.apache.hadoop.hbase.regionserver.compactions.CompactionContext;
43  import org.apache.hadoop.hbase.regionserver.compactions.CompactionProgress;
44  import org.apache.hadoop.hbase.regionserver.compactions.CompactionRequest;
45  import org.apache.hadoop.hbase.regionserver.throttle.ThroughputController;
46  import org.apache.hadoop.hbase.security.User;
47  
48  /**
49   * Interface for objects that hold a column family in a Region. Its a memstore and a set of zero or
50   * more StoreFiles, which stretch backwards over time.
51   */
52  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.COPROC)
53  @InterfaceStability.Evolving
54  public interface Store extends HeapSize, StoreConfigInformation, PropagatingConfigurationObserver {
55  
56    /* The default priority for user-specified compaction requests.
57     * The user gets top priority unless we have blocking compactions. (Pri <= 0)
58     */ int PRIORITY_USER = 1;
59    int NO_PRIORITY = Integer.MIN_VALUE;
60  
61    // General Accessors
62    CellComparator getComparator();
63  
64    Collection<StoreFile> getStorefiles();
65  
66    /**
67     * Close all the readers We don't need to worry about subsequent requests because the Region
68     * holds a write lock that will prevent any more reads or writes.
69     * @return the {@link StoreFile StoreFiles} that were previously being used.
70     * @throws IOException on failure
71     */
72    Collection<StoreFile> close() throws IOException;
73  
74    /**
75     * Return a scanner for both the memstore and the HStore files. Assumes we are not in a
76     * compaction.
77     * @param scan Scan to apply when scanning the stores
78     * @param targetCols columns to scan
79     * @return a scanner over the current key values
80     * @throws IOException on failure
81     */
82    KeyValueScanner getScanner(Scan scan, final NavigableSet<byte[]> targetCols, long readPt)
83        throws IOException;
84  
85    /**
86     * Get all scanners with no filtering based on TTL (that happens further down
87     * the line).
88     * @param cacheBlocks
89     * @param isGet
90     * @param usePread
91     * @param isCompaction
92     * @param matcher
93     * @param startRow
94     * @param stopRow
95     * @param readPt
96     * @return all scanners for this store
97     */
98    List<KeyValueScanner> getScanners(
99      boolean cacheBlocks,
100     boolean isGet,
101     boolean usePread,
102     boolean isCompaction,
103     ScanQueryMatcher matcher,
104     byte[] startRow,
105     byte[] stopRow,
106     long readPt
107   ) throws IOException;
108 
109   /**
110    * Create scanners on the given files and if needed on the memstore with no filtering based on TTL
111    * (that happens further down the line).
112    * @param files the list of files on which the scanners has to be created
113    * @param cacheBlocks cache the blocks or not
114    * @param isGet true if it is get, false if not
115    * @param usePread true to use pread, false if not
116    * @param isCompaction true if the scanner is created for compaction
117    * @param matcher the scan query matcher
118    * @param startRow the start row
119    * @param stopRow the stop row
120    * @param readPt the read point of the current scan
121    * @param includeMemstoreScanner true if memstore has to be included
122    * @return scanners on the given files and on the memstore if specified
123    */
124    List<KeyValueScanner> getScanners(List<StoreFile> files, boolean cacheBlocks, boolean isGet,
125           boolean usePread, boolean isCompaction, ScanQueryMatcher matcher, byte[] startRow,
126           byte[] stopRow, long readPt, boolean includeMemstoreScanner) throws IOException;
127   
128   ScanInfo getScanInfo();
129 
130   /**
131    * Adds or replaces the specified KeyValues.
132    * <p>
133    * For each KeyValue specified, if a cell with the same row, family, and qualifier exists in
134    * MemStore, it will be replaced. Otherwise, it will just be inserted to MemStore.
135    * <p>
136    * This operation is atomic on each KeyValue (row/family/qualifier) but not necessarily atomic
137    * across all of them.
138    * @param cells
139    * @param readpoint readpoint below which we can safely remove duplicate KVs
140    * @return memstore size delta
141    * @throws IOException
142    */
143   long upsert(Iterable<Cell> cells, long readpoint) throws IOException;
144 
145   /**
146    * Adds a value to the memstore
147    * @param cell
148    * @return memstore size delta
149    */
150   long add(Cell cell);
151 
152   /**
153    * When was the last edit done in the memstore
154    */
155   long timeOfOldestEdit();
156 
157   /**
158    * Removes a Cell from the memstore. The Cell is removed only if its key
159    * &amp; memstoreTS match the key &amp; memstoreTS value of the cell
160    * parameter.
161    * @param cell
162    */
163   void rollback(final Cell cell);
164 
165   FileSystem getFileSystem();
166 
167 
168   /**
169    * @param maxKeyCount
170    * @param compression Compression algorithm to use
171    * @param isCompaction whether we are creating a new file in a compaction
172    * @param includeMVCCReadpoint whether we should out the MVCC readpoint
173    * @return Writer for a new StoreFile in the tmp dir.
174    */
175   StoreFile.Writer createWriterInTmp(
176       long maxKeyCount,
177       Compression.Algorithm compression,
178       boolean isCompaction,
179       boolean includeMVCCReadpoint,
180       boolean includesTags
181   ) throws IOException;
182 
183   /**
184    * @param maxKeyCount
185    * @param compression Compression algorithm to use
186    * @param isCompaction whether we are creating a new file in a compaction
187    * @param includeMVCCReadpoint whether we should out the MVCC readpoint
188    * @param shouldDropBehind should the writer drop caches behind writes
189    * @return Writer for a new StoreFile in the tmp dir.
190    */
191   StoreFile.Writer createWriterInTmp(
192     long maxKeyCount,
193     Compression.Algorithm compression,
194     boolean isCompaction,
195     boolean includeMVCCReadpoint,
196     boolean includesTags,
197     boolean shouldDropBehind
198   ) throws IOException;
199 
200 
201 
202 
203   // Compaction oriented methods
204 
205   boolean throttleCompaction(long compactionSize);
206 
207   /**
208    * getter for CompactionProgress object
209    * @return CompactionProgress object; can be null
210    */
211   CompactionProgress getCompactionProgress();
212 
213   CompactionContext requestCompaction() throws IOException;
214 
215   /**
216    * @deprecated see requestCompaction(int, CompactionRequest, User)
217    */
218   @Deprecated
219   CompactionContext requestCompaction(int priority, CompactionRequest baseRequest)
220       throws IOException;
221 
222   CompactionContext requestCompaction(int priority, CompactionRequest baseRequest, User user)
223       throws IOException;
224 
225   void cancelRequestedCompaction(CompactionContext compaction);
226 
227   /**
228    * @deprecated see compact(CompactionContext, ThroughputController, User)
229    */
230   @Deprecated
231   List<StoreFile> compact(CompactionContext compaction,
232       ThroughputController throughputController) throws IOException;
233 
234   List<StoreFile> compact(CompactionContext compaction,
235     ThroughputController throughputController, User user) throws IOException;
236 
237   /**
238    * @return true if we should run a major compaction.
239    */
240   boolean isMajorCompaction() throws IOException;
241 
242   void triggerMajorCompaction();
243 
244   /**
245    * See if there's too much store files in this store
246    * @return true if number of store files is greater than the number defined in minFilesToCompact
247    */
248   boolean needsCompaction();
249 
250   int getCompactPriority();
251 
252   StoreFlushContext createFlushContext(long cacheFlushId);
253 
254   /**
255    * Call to complete a compaction. Its for the case where we find in the WAL a compaction
256    * that was not finished.  We could find one recovering a WAL after a regionserver crash.
257    * See HBASE-2331.
258    * @param compaction the descriptor for compaction
259    * @param pickCompactionFiles whether or not pick up the new compaction output files and
260    * add it to the store
261    * @param removeFiles whether to remove/archive files from filesystem
262    */
263   void replayCompactionMarker(CompactionDescriptor compaction, boolean pickCompactionFiles,
264       boolean removeFiles)
265       throws IOException;
266 
267   // Split oriented methods
268 
269   boolean canSplit();
270 
271   /**
272    * Determines if Store should be split
273    * @return byte[] if store should be split, null otherwise.
274    */
275   byte[] getSplitPoint();
276 
277   // Bulk Load methods
278 
279   /**
280    * This throws a WrongRegionException if the HFile does not fit in this region, or an
281    * InvalidHFileException if the HFile is not valid.
282    */
283   void assertBulkLoadHFileOk(Path srcPath) throws IOException;
284 
285   /**
286    * This method should only be called from Region. It is assumed that the ranges of values in the
287    * HFile fit within the stores assigned region. (assertBulkLoadHFileOk checks this)
288    *
289    * @param srcPathStr
290    * @param sequenceId sequence Id associated with the HFile
291    */
292   Path bulkLoadHFile(String srcPathStr, long sequenceId) throws IOException;
293 
294   // General accessors into the state of the store
295   // TODO abstract some of this out into a metrics class
296 
297   /**
298    * @return <tt>true</tt> if the store has any underlying reference files to older HFiles
299    */
300   boolean hasReferences();
301 
302   /**
303    * @return The size of this store's memstore, in bytes
304    */
305   long getMemStoreSize();
306 
307   /**
308    * @return The amount of memory we could flush from this memstore; usually this is equal to
309    * {@link #getMemStoreSize()} unless we are carrying snapshots and then it will be the size of
310    * outstanding snapshots.
311    */
312   long getFlushableSize();
313 
314   /**
315    * Returns the memstore snapshot size
316    * @return size of the memstore snapshot
317    */
318   long getSnapshotSize();
319 
320   HColumnDescriptor getFamily();
321 
322   /**
323    * @return The maximum sequence id in all store files.
324    */
325   long getMaxSequenceId();
326 
327   /**
328    * @return The maximum memstoreTS in all store files.
329    */
330   long getMaxMemstoreTS();
331 
332   /**
333    * @return the data block encoder
334    */
335   HFileDataBlockEncoder getDataBlockEncoder();
336 
337   /** @return aggregate size of all HStores used in the last compaction */
338   long getLastCompactSize();
339 
340   /** @return aggregate size of HStore */
341   long getSize();
342 
343   /**
344    * @return Count of store files
345    */
346   int getStorefilesCount();
347 
348   /**
349    * @return The size of the store files, in bytes, uncompressed.
350    */
351   long getStoreSizeUncompressed();
352 
353   /**
354    * @return The size of the store files, in bytes.
355    */
356   long getStorefilesSize();
357 
358   /**
359    * @return The size of the store file indexes, in bytes.
360    */
361   long getStorefilesIndexSize();
362 
363   /**
364    * Returns the total size of all index blocks in the data block indexes, including the root level,
365    * intermediate levels, and the leaf level for multi-level indexes, or just the root level for
366    * single-level indexes.
367    * @return the total size of block indexes in the store
368    */
369   long getTotalStaticIndexSize();
370 
371   /**
372    * Returns the total byte size of all Bloom filter bit arrays. For compound Bloom filters even the
373    * Bloom blocks currently not loaded into the block cache are counted.
374    * @return the total size of all Bloom filters in the store
375    */
376   long getTotalStaticBloomSize();
377 
378   // Test-helper methods
379 
380   /**
381    * Used for tests.
382    * @return cache configuration for this Store.
383    */
384   CacheConfig getCacheConfig();
385 
386   /**
387    * @return the parent region info hosting this store
388    */
389   HRegionInfo getRegionInfo();
390 
391   RegionCoprocessorHost getCoprocessorHost();
392 
393   boolean areWritesEnabled();
394 
395   /**
396    * @return The smallest mvcc readPoint across all the scanners in this
397    * region. Writes older than this readPoint, are included  in every
398    * read operation.
399    */
400   long getSmallestReadPoint();
401 
402   String getColumnFamilyName();
403 
404   TableName getTableName();
405 
406   /**
407    * @return The number of cells flushed to disk
408    */
409   long getFlushedCellsCount();
410 
411   /**
412    * @return The total size of data flushed to disk, in bytes
413    */
414   long getFlushedCellsSize();
415 
416   /**
417    * @return The number of cells processed during minor compactions
418    */
419   long getCompactedCellsCount();
420 
421   /**
422    * @return The total amount of data processed during minor compactions, in bytes
423    */
424   long getCompactedCellsSize();
425 
426   /**
427    * @return The number of cells processed during major compactions
428    */
429   long getMajorCompactedCellsCount();
430 
431   /**
432    * @return The total amount of data processed during major compactions, in bytes
433    */
434   long getMajorCompactedCellsSize();
435 
436   /*
437    * @param o Observer who wants to know about changes in set of Readers
438    */
439   void addChangedReaderObserver(ChangedReadersObserver o);
440 
441   /*
442    * @param o Observer no longer interested in changes in set of Readers.
443    */
444   void deleteChangedReaderObserver(ChangedReadersObserver o);
445 
446   /**
447    * @return Whether this store has too many store files.
448    */
449   boolean hasTooManyStoreFiles();
450 
451   /**
452    * Checks the underlying store files, and opens the files that  have not
453    * been opened, and removes the store file readers for store files no longer
454    * available. Mainly used by secondary region replicas to keep up to date with
455    * the primary region files.
456    * @throws IOException
457    */
458   void refreshStoreFiles() throws IOException;
459 
460   /**
461    * This value can represent the degree of emergency of compaction for this store. It should be
462    * greater than or equal to 0.0, any value greater than 1.0 means we have too many store files.
463    * <ul>
464    * <li>if getStorefilesCount &lt;= getMinFilesToCompact, return 0.0</li>
465    * <li>return (getStorefilesCount - getMinFilesToCompact) / (blockingFileCount -
466    * getMinFilesToCompact)</li>
467    * </ul>
468    * <p>
469    * And for striped stores, we should calculate this value by the files in each stripe separately
470    * and return the maximum value.
471    * <p>
472    * It is similar to {@link #getCompactPriority()} except that it is more suitable to use in a
473    * linear formula.
474    */
475   double getCompactionPressure();
476 
477    /**
478     * Replaces the store files that the store has with the given files. Mainly used by
479     * secondary region replicas to keep up to date with
480     * the primary region files.
481     * @throws IOException
482     */
483   void refreshStoreFiles(Collection<String> newFiles) throws IOException;
484 
485   void bulkLoadHFile(StoreFileInfo fileInfo) throws IOException;
486 
487   boolean isPrimaryReplicaStore();
488 
489   /**
490    * Closes and archives the compacted files under this store
491    */
492   void closeAndArchiveCompactedFiles() throws IOException;
493 }