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