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