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