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.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.classification.InterfaceStability;
27  import org.apache.hadoop.fs.FileSystem;
28  import org.apache.hadoop.fs.Path;
29  import org.apache.hadoop.hbase.Cell;
30  import org.apache.hadoop.hbase.CellComparator;
31  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
32  import org.apache.hadoop.hbase.HColumnDescriptor;
33  import org.apache.hadoop.hbase.HRegionInfo;
34  import org.apache.hadoop.hbase.TableName;
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.compactions.CompactionThroughputController;
46  import org.apache.hadoop.hbase.util.Pair;
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   ScanInfo getScanInfo();
110 
111   /**
112    * Adds or replaces the specified KeyValues.
113    * <p>
114    * For each KeyValue specified, if a cell with the same row, family, and qualifier exists in
115    * MemStore, it will be replaced. Otherwise, it will just be inserted to MemStore.
116    * <p>
117    * This operation is atomic on each KeyValue (row/family/qualifier) but not necessarily atomic
118    * across all of them.
119    * @param cells
120    * @param readpoint readpoint below which we can safely remove duplicate KVs
121    * @return memstore size delta
122    * @throws IOException
123    */
124   long upsert(Iterable<Cell> cells, long readpoint) throws IOException;
125 
126   /**
127    * Adds a value to the memstore
128    * @param cell
129    * @return memstore size delta &amp; newly added KV which maybe different than the passed in KV
130    */
131   Pair<Long, Cell> add(Cell cell);
132 
133   /**
134    * When was the last edit done in the memstore
135    */
136   long timeOfOldestEdit();
137 
138   /**
139    * Removes a Cell from the memstore. The Cell is removed only if its key
140    * &amp; memstoreTS match the key &amp; memstoreTS value of the cell
141    * parameter.
142    * @param cell
143    */
144   void rollback(final Cell cell);
145 
146   /**
147    * Find the key that matches <i>row</i> exactly, or the one that immediately precedes it. WARNING:
148    * Only use this method on a table where writes occur with strictly increasing timestamps. This
149    * method assumes this pattern of writes in order to make it reasonably performant. Also our
150    * search is dependent on the axiom that deletes are for cells that are in the container that
151    * follows whether a memstore snapshot or a storefile, not for the current container: i.e. we'll
152    * see deletes before we come across cells we are to delete. Presumption is that the
153    * memstore#kvset is processed before memstore#snapshot and so on.
154    * @param row The row key of the targeted row.
155    * @return Found Cell or null if none found.
156    * @throws IOException
157    */
158   Cell getRowKeyAtOrBefore(final byte[] row) throws IOException;
159 
160   FileSystem getFileSystem();
161 
162   /*
163    * @param maxKeyCount
164    * @param compression Compression algorithm to use
165    * @param isCompaction whether we are creating a new file in a compaction
166    * @param includeMVCCReadpoint whether we should out the MVCC readpoint
167    * @return Writer for a new StoreFile in the tmp dir.
168    */
169   StoreFile.Writer createWriterInTmp(
170     long maxKeyCount,
171     Compression.Algorithm compression,
172     boolean isCompaction,
173     boolean includeMVCCReadpoint,
174     boolean includesTags
175   ) throws IOException;
176 
177   // Compaction oriented methods
178 
179   boolean throttleCompaction(long compactionSize);
180 
181   /**
182    * getter for CompactionProgress object
183    * @return CompactionProgress object; can be null
184    */
185   CompactionProgress getCompactionProgress();
186 
187   CompactionContext requestCompaction() throws IOException;
188 
189   CompactionContext requestCompaction(int priority, CompactionRequest baseRequest)
190       throws IOException;
191 
192   void cancelRequestedCompaction(CompactionContext compaction);
193 
194   List<StoreFile> compact(CompactionContext compaction,
195       CompactionThroughputController throughputController) throws IOException;
196 
197   /**
198    * @return true if we should run a major compaction.
199    */
200   boolean isMajorCompaction() throws IOException;
201 
202   void triggerMajorCompaction();
203 
204   /**
205    * See if there's too much store files in this store
206    * @return true if number of store files is greater than the number defined in minFilesToCompact
207    */
208   boolean needsCompaction();
209 
210   int getCompactPriority();
211 
212   StoreFlushContext createFlushContext(long cacheFlushId);
213 
214   /**
215    * Call to complete a compaction. Its for the case where we find in the WAL a compaction
216    * that was not finished.  We could find one recovering a WAL after a regionserver crash.
217    * See HBASE-2331.
218    * @param compaction the descriptor for compaction
219    * @param pickCompactionFiles whether or not pick up the new compaction output files and
220    * add it to the store
221    * @param removeFiles whether to remove/archive files from filesystem
222    */
223   void replayCompactionMarker(CompactionDescriptor compaction, boolean pickCompactionFiles,
224       boolean removeFiles)
225       throws IOException;
226 
227   // Split oriented methods
228 
229   boolean canSplit();
230 
231   /**
232    * Determines if Store should be split
233    * @return byte[] if store should be split, null otherwise.
234    */
235   byte[] getSplitPoint();
236 
237   // Bulk Load methods
238 
239   /**
240    * This throws a WrongRegionException if the HFile does not fit in this region, or an
241    * InvalidHFileException if the HFile is not valid.
242    */
243   void assertBulkLoadHFileOk(Path srcPath) throws IOException;
244 
245   /**
246    * This method should only be called from Region. It is assumed that the ranges of values in the
247    * HFile fit within the stores assigned region. (assertBulkLoadHFileOk checks this)
248    *
249    * @param srcPathStr
250    * @param sequenceId sequence Id associated with the HFile
251    */
252   Path bulkLoadHFile(String srcPathStr, long sequenceId) throws IOException;
253 
254   // General accessors into the state of the store
255   // TODO abstract some of this out into a metrics class
256 
257   /**
258    * @return <tt>true</tt> if the store has any underlying reference files to older HFiles
259    */
260   boolean hasReferences();
261 
262   /**
263    * @return The size of this store's memstore, in bytes
264    */
265   long getMemStoreSize();
266 
267   /**
268    * @return The amount of memory we could flush from this memstore; usually this is equal to
269    * {@link #getMemStoreSize()} unless we are carrying snapshots and then it will be the size of
270    * outstanding snapshots.
271    */
272   long getFlushableSize();
273 
274   /**
275    * Returns the memstore snapshot size
276    * @return size of the memstore snapshot
277    */
278   long getSnapshotSize();
279 
280   HColumnDescriptor getFamily();
281 
282   /**
283    * @return The maximum sequence id in all store files.
284    */
285   long getMaxSequenceId();
286 
287   /**
288    * @return The maximum memstoreTS in all store files.
289    */
290   long getMaxMemstoreTS();
291 
292   /**
293    * @return the data block encoder
294    */
295   HFileDataBlockEncoder getDataBlockEncoder();
296 
297   /** @return aggregate size of all HStores used in the last compaction */
298   long getLastCompactSize();
299 
300   /** @return aggregate size of HStore */
301   long getSize();
302 
303   /**
304    * @return Count of store files
305    */
306   int getStorefilesCount();
307 
308   /**
309    * @return The size of the store files, in bytes, uncompressed.
310    */
311   long getStoreSizeUncompressed();
312 
313   /**
314    * @return The size of the store files, in bytes.
315    */
316   long getStorefilesSize();
317 
318   /**
319    * @return The size of the store file indexes, in bytes.
320    */
321   long getStorefilesIndexSize();
322 
323   /**
324    * Returns the total size of all index blocks in the data block indexes, including the root level,
325    * intermediate levels, and the leaf level for multi-level indexes, or just the root level for
326    * single-level indexes.
327    * @return the total size of block indexes in the store
328    */
329   long getTotalStaticIndexSize();
330 
331   /**
332    * Returns the total byte size of all Bloom filter bit arrays. For compound Bloom filters even the
333    * Bloom blocks currently not loaded into the block cache are counted.
334    * @return the total size of all Bloom filters in the store
335    */
336   long getTotalStaticBloomSize();
337 
338   // Test-helper methods
339 
340   /**
341    * Used for tests.
342    * @return cache configuration for this Store.
343    */
344   CacheConfig getCacheConfig();
345 
346   /**
347    * @return the parent region info hosting this store
348    */
349   HRegionInfo getRegionInfo();
350 
351   RegionCoprocessorHost getCoprocessorHost();
352 
353   boolean areWritesEnabled();
354 
355   /**
356    * @return The smallest mvcc readPoint across all the scanners in this
357    * region. Writes older than this readPoint, are included  in every
358    * read operation.
359    */
360   long getSmallestReadPoint();
361 
362   String getColumnFamilyName();
363 
364   TableName getTableName();
365 
366   /**
367    * @return The number of cells flushed to disk
368    */
369   long getFlushedCellsCount();
370 
371   /**
372    * @return The total size of data flushed to disk, in bytes
373    */
374   long getFlushedCellsSize();
375 
376   /**
377    * @return The number of cells processed during minor compactions
378    */
379   long getCompactedCellsCount();
380 
381   /**
382    * @return The total amount of data processed during minor compactions, in bytes
383    */
384   long getCompactedCellsSize();
385 
386   /**
387    * @return The number of cells processed during major compactions
388    */
389   long getMajorCompactedCellsCount();
390 
391   /**
392    * @return The total amount of data processed during major compactions, in bytes
393    */
394   long getMajorCompactedCellsSize();
395 
396   /*
397    * @param o Observer who wants to know about changes in set of Readers
398    */
399   void addChangedReaderObserver(ChangedReadersObserver o);
400 
401   /*
402    * @param o Observer no longer interested in changes in set of Readers.
403    */
404   void deleteChangedReaderObserver(ChangedReadersObserver o);
405 
406   /**
407    * @return Whether this store has too many store files.
408    */
409   boolean hasTooManyStoreFiles();
410 
411   /**
412    * Checks the underlying store files, and opens the files that  have not
413    * been opened, and removes the store file readers for store files no longer
414    * available. Mainly used by secondary region replicas to keep up to date with
415    * the primary region files.
416    * @throws IOException
417    */
418   void refreshStoreFiles() throws IOException;
419 
420   /**
421    * This value can represent the degree of emergency of compaction for this store. It should be
422    * greater than or equal to 0.0, any value greater than 1.0 means we have too many store files.
423    * <ul>
424    * <li>if getStorefilesCount &lt;= getMinFilesToCompact, return 0.0</li>
425    * <li>return (getStorefilesCount - getMinFilesToCompact) / (blockingFileCount -
426    * getMinFilesToCompact)</li>
427    * </ul>
428    * <p>
429    * And for striped stores, we should calculate this value by the files in each stripe separately
430    * and return the maximum value.
431    * <p>
432    * It is similar to {@link #getCompactPriority()} except that it is more suitable to use in a
433    * linear formula.
434    */
435   double getCompactionPressure();
436 
437    /**
438     * Replaces the store files that the store has with the given files. Mainly used by
439     * secondary region replicas to keep up to date with
440     * the primary region files.
441     * @throws IOException
442     */
443   void refreshStoreFiles(Collection<String> newFiles) throws IOException;
444 
445   void bulkLoadHFile(StoreFileInfo fileInfo) throws IOException;
446 }