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