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