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   FileSystem getFileSystem();
158 
159 
160   /**
161    * @param maxKeyCount
162    * @param compression Compression algorithm to use
163    * @param isCompaction whether we are creating a new file in a compaction
164    * @param includeMVCCReadpoint whether we should out the MVCC readpoint
165    * @return Writer for a new StoreFile in the tmp dir.
166    */
167   StoreFileWriter createWriterInTmp(
168       long maxKeyCount,
169       Compression.Algorithm compression,
170       boolean isCompaction,
171       boolean includeMVCCReadpoint,
172       boolean includesTags
173   ) throws IOException;
174 
175   /**
176    * @param maxKeyCount
177    * @param compression Compression algorithm to use
178    * @param isCompaction whether we are creating a new file in a compaction
179    * @param includeMVCCReadpoint whether we should out the MVCC readpoint
180    * @param shouldDropBehind should the writer drop caches behind writes
181    * @return Writer for a new StoreFile in the tmp dir.
182    */
183   StoreFileWriter createWriterInTmp(
184     long maxKeyCount,
185     Compression.Algorithm compression,
186     boolean isCompaction,
187     boolean includeMVCCReadpoint,
188     boolean includesTags,
189     boolean shouldDropBehind
190   ) throws IOException;
191 
192 
193 
194 
195   // Compaction oriented methods
196 
197   boolean throttleCompaction(long compactionSize);
198 
199   /**
200    * getter for CompactionProgress object
201    * @return CompactionProgress object; can be null
202    */
203   CompactionProgress getCompactionProgress();
204 
205   CompactionContext requestCompaction() throws IOException;
206 
207   /**
208    * @deprecated see requestCompaction(int, CompactionRequest, User)
209    */
210   @Deprecated
211   CompactionContext requestCompaction(int priority, CompactionRequest baseRequest)
212       throws IOException;
213 
214   CompactionContext requestCompaction(int priority, CompactionRequest baseRequest, User user)
215       throws IOException;
216 
217   void cancelRequestedCompaction(CompactionContext compaction);
218 
219   /**
220    * @deprecated see compact(CompactionContext, ThroughputController, User)
221    */
222   @Deprecated
223   List<StoreFile> compact(CompactionContext compaction,
224       ThroughputController throughputController) throws IOException;
225 
226   List<StoreFile> compact(CompactionContext compaction,
227     ThroughputController throughputController, User user) throws IOException;
228 
229   /**
230    * @return true if we should run a major compaction.
231    */
232   boolean isMajorCompaction() throws IOException;
233 
234   void triggerMajorCompaction();
235 
236   /**
237    * See if there's too much store files in this store
238    * @return true if number of store files is greater than the number defined in minFilesToCompact
239    */
240   boolean needsCompaction();
241 
242   int getCompactPriority();
243 
244   StoreFlushContext createFlushContext(long cacheFlushId);
245 
246   /**
247    * Call to complete a compaction. Its for the case where we find in the WAL a compaction
248    * that was not finished.  We could find one recovering a WAL after a regionserver crash.
249    * See HBASE-2331.
250    * @param compaction the descriptor for compaction
251    * @param pickCompactionFiles whether or not pick up the new compaction output files and
252    * add it to the store
253    * @param removeFiles whether to remove/archive files from filesystem
254    */
255   void replayCompactionMarker(CompactionDescriptor compaction, boolean pickCompactionFiles,
256       boolean removeFiles)
257       throws IOException;
258 
259   // Split oriented methods
260 
261   boolean canSplit();
262 
263   /**
264    * Determines if Store should be split
265    * @return byte[] if store should be split, null otherwise.
266    */
267   byte[] getSplitPoint();
268 
269   // Bulk Load methods
270 
271   /**
272    * This throws a WrongRegionException if the HFile does not fit in this region, or an
273    * InvalidHFileException if the HFile is not valid.
274    */
275   void assertBulkLoadHFileOk(Path srcPath) throws IOException;
276 
277   /**
278    * This method should only be called from Region. It is assumed that the ranges of values in the
279    * HFile fit within the stores assigned region. (assertBulkLoadHFileOk checks this)
280    *
281    * @param srcPathStr
282    * @param sequenceId sequence Id associated with the HFile
283    */
284   Path bulkLoadHFile(String srcPathStr, long sequenceId) throws IOException;
285 
286   // General accessors into the state of the store
287   // TODO abstract some of this out into a metrics class
288 
289   /**
290    * @return <tt>true</tt> if the store has any underlying reference files to older HFiles
291    */
292   boolean hasReferences();
293 
294   /**
295    * @return The size of this store's memstore, in bytes
296    */
297   long getMemStoreSize();
298 
299   /**
300    * @return The amount of memory we could flush from this memstore; usually this is equal to
301    * {@link #getMemStoreSize()} unless we are carrying snapshots and then it will be the size of
302    * outstanding snapshots.
303    */
304   long getFlushableSize();
305 
306   /**
307    * Returns the memstore snapshot size
308    * @return size of the memstore snapshot
309    */
310   long getSnapshotSize();
311 
312   HColumnDescriptor getFamily();
313 
314   /**
315    * @return The maximum sequence id in all store files.
316    */
317   long getMaxSequenceId();
318 
319   /**
320    * @return The maximum memstoreTS in all store files.
321    */
322   long getMaxMemstoreTS();
323 
324   /**
325    * @return the data block encoder
326    */
327   HFileDataBlockEncoder getDataBlockEncoder();
328 
329   /** @return aggregate size of all HStores used in the last compaction */
330   long getLastCompactSize();
331 
332   /** @return aggregate size of HStore */
333   long getSize();
334 
335   /**
336    * @return Count of store files
337    */
338   int getStorefilesCount();
339 
340   /**
341    * @return Max age of store files in this store
342    */
343   long getMaxStoreFileAge();
344 
345   /**
346    * @return Min age of store files in this store
347    */
348   long getMinStoreFileAge();
349 
350   /**
351    *  @return Average age of store files in this store, 0 if no store files
352    */
353   long getAvgStoreFileAge();
354 
355   /**
356    *  @return Number of reference files in this store
357    */
358   long getNumReferenceFiles();
359 
360   /**
361    *  @return Number of HFiles in this store
362    */
363   long getNumHFiles();
364 
365   /**
366    * @return The size of the store files, in bytes, uncompressed.
367    */
368   long getStoreSizeUncompressed();
369 
370   /**
371    * @return The size of the store files, in bytes.
372    */
373   long getStorefilesSize();
374 
375   /**
376    * @return The size of the store file indexes, in bytes.
377    */
378   long getStorefilesIndexSize();
379 
380   /**
381    * Returns the total size of all index blocks in the data block indexes, including the root level,
382    * intermediate levels, and the leaf level for multi-level indexes, or just the root level for
383    * single-level indexes.
384    * @return the total size of block indexes in the store
385    */
386   long getTotalStaticIndexSize();
387 
388   /**
389    * Returns the total byte size of all Bloom filter bit arrays. For compound Bloom filters even the
390    * Bloom blocks currently not loaded into the block cache are counted.
391    * @return the total size of all Bloom filters in the store
392    */
393   long getTotalStaticBloomSize();
394 
395   // Test-helper methods
396 
397   /**
398    * Used for tests.
399    * @return cache configuration for this Store.
400    */
401   CacheConfig getCacheConfig();
402 
403   /**
404    * @return the parent region info hosting this store
405    */
406   HRegionInfo getRegionInfo();
407 
408   RegionCoprocessorHost getCoprocessorHost();
409 
410   boolean areWritesEnabled();
411 
412   /**
413    * @return The smallest mvcc readPoint across all the scanners in this
414    * region. Writes older than this readPoint, are included  in every
415    * read operation.
416    */
417   long getSmallestReadPoint();
418 
419   String getColumnFamilyName();
420 
421   TableName getTableName();
422 
423   /**
424    * @return The number of cells flushed to disk
425    */
426   long getFlushedCellsCount();
427 
428   /**
429    * @return The total size of data flushed to disk, in bytes
430    */
431   long getFlushedCellsSize();
432 
433   /**
434    * @return The total size of out output files on disk, in bytes
435    */
436   long getFlushedOutputFileSize();
437 
438   /**
439    * @return The number of cells processed during minor compactions
440    */
441   long getCompactedCellsCount();
442 
443   /**
444    * @return The total amount of data processed during minor compactions, in bytes
445    */
446   long getCompactedCellsSize();
447 
448   /**
449    * @return The number of cells processed during major compactions
450    */
451   long getMajorCompactedCellsCount();
452 
453   /**
454    * @return The total amount of data processed during major compactions, in bytes
455    */
456   long getMajorCompactedCellsSize();
457 
458   /*
459    * @param o Observer who wants to know about changes in set of Readers
460    */
461   void addChangedReaderObserver(ChangedReadersObserver o);
462 
463   /*
464    * @param o Observer no longer interested in changes in set of Readers.
465    */
466   void deleteChangedReaderObserver(ChangedReadersObserver o);
467 
468   /**
469    * @return Whether this store has too many store files.
470    */
471   boolean hasTooManyStoreFiles();
472 
473   /**
474    * Checks the underlying store files, and opens the files that  have not
475    * been opened, and removes the store file readers for store files no longer
476    * available. Mainly used by secondary region replicas to keep up to date with
477    * the primary region files.
478    * @throws IOException
479    */
480   void refreshStoreFiles() throws IOException;
481 
482   /**
483    * This value can represent the degree of emergency of compaction for this store. It should be
484    * greater than or equal to 0.0, any value greater than 1.0 means we have too many store files.
485    * <ul>
486    * <li>if getStorefilesCount &lt;= getMinFilesToCompact, return 0.0</li>
487    * <li>return (getStorefilesCount - getMinFilesToCompact) / (blockingFileCount -
488    * getMinFilesToCompact)</li>
489    * </ul>
490    * <p>
491    * And for striped stores, we should calculate this value by the files in each stripe separately
492    * and return the maximum value.
493    * <p>
494    * It is similar to {@link #getCompactPriority()} except that it is more suitable to use in a
495    * linear formula.
496    */
497   double getCompactionPressure();
498 
499    /**
500     * Replaces the store files that the store has with the given files. Mainly used by
501     * secondary region replicas to keep up to date with
502     * the primary region files.
503     * @throws IOException
504     */
505   void refreshStoreFiles(Collection<String> newFiles) throws IOException;
506 
507   void bulkLoadHFile(StoreFileInfo fileInfo) throws IOException;
508 
509   boolean isPrimaryReplicaStore();
510 
511   /**
512    * Closes and archives the compacted files under this store
513    */
514   void closeAndArchiveCompactedFiles() throws IOException;
515 
516   /**
517    * This method is called when it is clear that the flush to disk is completed.
518    * The store may do any post-flush actions at this point.
519    * One example is to update the wal with sequence number that is known only at the store level.
520    */
521   void finalizeFlush();
522 
523   MemStore getMemStore();
524 }