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