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