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 * @return memstore size delta
141 * @throws IOException
142 */
143 long upsert(Iterable<Cell> cells, long readpoint) throws IOException;
144
145 /**
146 * Adds a value to the memstore
147 * @param cell
148 * @return memstore size delta
149 */
150 long add(Cell cell);
151
152 /**
153 * When was the last edit done in the memstore
154 */
155 long timeOfOldestEdit();
156
157 /**
158 * Removes a Cell from the memstore. The Cell is removed only if its key
159 * & memstoreTS match the key & memstoreTS value of the cell
160 * parameter.
161 * @param cell
162 */
163 void rollback(final Cell cell);
164
165 /**
166 * Find the key that matches <i>row</i> exactly, or the one that immediately precedes it. WARNING:
167 * Only use this method on a table where writes occur with strictly increasing timestamps. This
168 * method assumes this pattern of writes in order to make it reasonably performant. Also our
169 * search is dependent on the axiom that deletes are for cells that are in the container that
170 * follows whether a memstore snapshot or a storefile, not for the current container: i.e. we'll
171 * see deletes before we come across cells we are to delete. Presumption is that the
172 * memstore#kvset is processed before memstore#snapshot and so on.
173 * @param row The row key of the targeted row.
174 * @return Found Cell or null if none found.
175 * @throws IOException
176 */
177 Cell getRowKeyAtOrBefore(final byte[] row) throws IOException;
178
179 FileSystem getFileSystem();
180
181
182 /**
183 * @param maxKeyCount
184 * @param compression Compression algorithm to use
185 * @param isCompaction whether we are creating a new file in a compaction
186 * @param includeMVCCReadpoint whether we should out the MVCC readpoint
187 * @return Writer for a new StoreFile in the tmp dir.
188 */
189 StoreFile.Writer createWriterInTmp(
190 long maxKeyCount,
191 Compression.Algorithm compression,
192 boolean isCompaction,
193 boolean includeMVCCReadpoint,
194 boolean includesTags
195 ) throws IOException;
196
197 /**
198 * @param maxKeyCount
199 * @param compression Compression algorithm to use
200 * @param isCompaction whether we are creating a new file in a compaction
201 * @param includeMVCCReadpoint whether we should out the MVCC readpoint
202 * @param shouldDropBehind should the writer drop caches behind writes
203 * @return Writer for a new StoreFile in the tmp dir.
204 */
205 StoreFile.Writer createWriterInTmp(
206 long maxKeyCount,
207 Compression.Algorithm compression,
208 boolean isCompaction,
209 boolean includeMVCCReadpoint,
210 boolean includesTags,
211 boolean shouldDropBehind
212 ) throws IOException;
213
214 /**
215 * @param maxKeyCount
216 * @param compression Compression algorithm to use
217 * @param isCompaction whether we are creating a new file in a compaction
218 * @param includeMVCCReadpoint whether we should out the MVCC readpoint
219 * @param shouldDropBehind should the writer drop caches behind writes
220 * @param trt Ready-made timetracker to use.
221 * @return Writer for a new StoreFile in the tmp dir.
222 */
223 StoreFile.Writer createWriterInTmp(
224 long maxKeyCount,
225 Compression.Algorithm compression,
226 boolean isCompaction,
227 boolean includeMVCCReadpoint,
228 boolean includesTags,
229 boolean shouldDropBehind,
230 final TimeRangeTracker trt
231 ) throws IOException;
232
233 // Compaction oriented methods
234
235 boolean throttleCompaction(long compactionSize);
236
237 /**
238 * getter for CompactionProgress object
239 * @return CompactionProgress object; can be null
240 */
241 CompactionProgress getCompactionProgress();
242
243 CompactionContext requestCompaction() throws IOException;
244
245 /**
246 * @deprecated see requestCompaction(int, CompactionRequest, User)
247 */
248 @Deprecated
249 CompactionContext requestCompaction(int priority, CompactionRequest baseRequest)
250 throws IOException;
251
252 CompactionContext requestCompaction(int priority, CompactionRequest baseRequest, User user)
253 throws IOException;
254
255 void cancelRequestedCompaction(CompactionContext compaction);
256
257 /**
258 * @deprecated see compact(CompactionContext, ThroughputController, User)
259 */
260 @Deprecated
261 List<StoreFile> compact(CompactionContext compaction,
262 ThroughputController throughputController) throws IOException;
263
264 List<StoreFile> compact(CompactionContext compaction,
265 ThroughputController throughputController, User user) throws IOException;
266
267 /**
268 * @return true if we should run a major compaction.
269 */
270 boolean isMajorCompaction() throws IOException;
271
272 void triggerMajorCompaction();
273
274 /**
275 * See if there's too much store files in this store
276 * @return true if number of store files is greater than the number defined in minFilesToCompact
277 */
278 boolean needsCompaction();
279
280 int getCompactPriority();
281
282 StoreFlushContext createFlushContext(long cacheFlushId);
283
284 /**
285 * Call to complete a compaction. Its for the case where we find in the WAL a compaction
286 * that was not finished. We could find one recovering a WAL after a regionserver crash.
287 * See HBASE-2331.
288 * @param compaction the descriptor for compaction
289 * @param pickCompactionFiles whether or not pick up the new compaction output files and
290 * add it to the store
291 * @param removeFiles whether to remove/archive files from filesystem
292 */
293 void replayCompactionMarker(CompactionDescriptor compaction, boolean pickCompactionFiles,
294 boolean removeFiles)
295 throws IOException;
296
297 // Split oriented methods
298
299 boolean canSplit();
300
301 /**
302 * Determines if Store should be split
303 * @return byte[] if store should be split, null otherwise.
304 */
305 byte[] getSplitPoint();
306
307 // Bulk Load methods
308
309 /**
310 * This throws a WrongRegionException if the HFile does not fit in this region, or an
311 * InvalidHFileException if the HFile is not valid.
312 */
313 void assertBulkLoadHFileOk(Path srcPath) throws IOException;
314
315 /**
316 * This method should only be called from Region. It is assumed that the ranges of values in the
317 * HFile fit within the stores assigned region. (assertBulkLoadHFileOk checks this)
318 *
319 * @param srcPathStr
320 * @param sequenceId sequence Id associated with the HFile
321 */
322 Path bulkLoadHFile(String srcPathStr, long sequenceId) throws IOException;
323
324 // General accessors into the state of the store
325 // TODO abstract some of this out into a metrics class
326
327 /**
328 * @return <tt>true</tt> if the store has any underlying reference files to older HFiles
329 */
330 boolean hasReferences();
331
332 /**
333 * @return The size of this store's memstore, in bytes
334 */
335 long getMemStoreSize();
336
337 /**
338 * @return The amount of memory we could flush from this memstore; usually this is equal to
339 * {@link #getMemStoreSize()} unless we are carrying snapshots and then it will be the size of
340 * outstanding snapshots.
341 */
342 long getFlushableSize();
343
344 /**
345 * Returns the memstore snapshot size
346 * @return size of the memstore snapshot
347 */
348 long getSnapshotSize();
349
350 HColumnDescriptor getFamily();
351
352 /**
353 * @return The maximum sequence id in all store files.
354 */
355 long getMaxSequenceId();
356
357 /**
358 * @return The maximum memstoreTS in all store files.
359 */
360 long getMaxMemstoreTS();
361
362 /**
363 * @return the data block encoder
364 */
365 HFileDataBlockEncoder getDataBlockEncoder();
366
367 /** @return aggregate size of all HStores used in the last compaction */
368 long getLastCompactSize();
369
370 /** @return aggregate size of HStore */
371 long getSize();
372
373 /**
374 * @return Count of store files
375 */
376 int getStorefilesCount();
377
378 /**
379 * @return Max age of store files in this store
380 */
381 long getMaxStoreFileAge();
382
383 /**
384 * @return Min age of store files in this store
385 */
386 long getMinStoreFileAge();
387
388 /**
389 * @return Average age of store files in this store, 0 if no store files
390 */
391 long getAvgStoreFileAge();
392
393 /**
394 * @return Number of reference files in this store
395 */
396 long getNumReferenceFiles();
397
398 /**
399 * @return Number of HFiles in this store
400 */
401 long getNumHFiles();
402
403 /**
404 * @return The size of the store files, in bytes, uncompressed.
405 */
406 long getStoreSizeUncompressed();
407
408 /**
409 * @return The size of the store files, in bytes.
410 */
411 long getStorefilesSize();
412
413 /**
414 * @return The size of the store file indexes, in bytes.
415 */
416 long getStorefilesIndexSize();
417
418 /**
419 * Returns the total size of all index blocks in the data block indexes, including the root level,
420 * intermediate levels, and the leaf level for multi-level indexes, or just the root level for
421 * single-level indexes.
422 * @return the total size of block indexes in the store
423 */
424 long getTotalStaticIndexSize();
425
426 /**
427 * Returns the total byte size of all Bloom filter bit arrays. For compound Bloom filters even the
428 * Bloom blocks currently not loaded into the block cache are counted.
429 * @return the total size of all Bloom filters in the store
430 */
431 long getTotalStaticBloomSize();
432
433 // Test-helper methods
434
435 /**
436 * Used for tests.
437 * @return cache configuration for this Store.
438 */
439 CacheConfig getCacheConfig();
440
441 /**
442 * @return the parent region info hosting this store
443 */
444 HRegionInfo getRegionInfo();
445
446 RegionCoprocessorHost getCoprocessorHost();
447
448 boolean areWritesEnabled();
449
450 /**
451 * @return The smallest mvcc readPoint across all the scanners in this
452 * region. Writes older than this readPoint, are included in every
453 * read operation.
454 */
455 long getSmallestReadPoint();
456
457 String getColumnFamilyName();
458
459 TableName getTableName();
460
461 /**
462 * @return The number of cells flushed to disk
463 */
464 long getFlushedCellsCount();
465
466 /**
467 * @return The total size of data flushed to disk, in bytes
468 */
469 long getFlushedCellsSize();
470
471 /**
472 * @return The total size of out output files on disk, in bytes
473 */
474 long getFlushedOutputFileSize();
475
476 /**
477 * @return The number of cells processed during minor compactions
478 */
479 long getCompactedCellsCount();
480
481 /**
482 * @return The total amount of data processed during minor compactions, in bytes
483 */
484 long getCompactedCellsSize();
485
486 /**
487 * @return The number of cells processed during major compactions
488 */
489 long getMajorCompactedCellsCount();
490
491 /**
492 * @return The total amount of data processed during major compactions, in bytes
493 */
494 long getMajorCompactedCellsSize();
495
496 /*
497 * @param o Observer who wants to know about changes in set of Readers
498 */
499 void addChangedReaderObserver(ChangedReadersObserver o);
500
501 /*
502 * @param o Observer no longer interested in changes in set of Readers.
503 */
504 void deleteChangedReaderObserver(ChangedReadersObserver o);
505
506 /**
507 * @return Whether this store has too many store files.
508 */
509 boolean hasTooManyStoreFiles();
510
511 /**
512 * Checks the underlying store files, and opens the files that have not
513 * been opened, and removes the store file readers for store files no longer
514 * available. Mainly used by secondary region replicas to keep up to date with
515 * the primary region files.
516 * @throws IOException
517 */
518 void refreshStoreFiles() throws IOException;
519
520 /**
521 * This value can represent the degree of emergency of compaction for this store. It should be
522 * greater than or equal to 0.0, any value greater than 1.0 means we have too many store files.
523 * <ul>
524 * <li>if getStorefilesCount <= getMinFilesToCompact, return 0.0</li>
525 * <li>return (getStorefilesCount - getMinFilesToCompact) / (blockingFileCount -
526 * getMinFilesToCompact)</li>
527 * </ul>
528 * <p>
529 * And for striped stores, we should calculate this value by the files in each stripe separately
530 * and return the maximum value.
531 * <p>
532 * It is similar to {@link #getCompactPriority()} except that it is more suitable to use in a
533 * linear formula.
534 */
535 double getCompactionPressure();
536
537 /**
538 * Replaces the store files that the store has with the given files. Mainly used by
539 * secondary region replicas to keep up to date with
540 * the primary region files.
541 * @throws IOException
542 */
543 void refreshStoreFiles(Collection<String> newFiles) throws IOException;
544
545 void bulkLoadHFile(StoreFileInfo fileInfo) throws IOException;
546
547 boolean isPrimaryReplicaStore();
548
549 /**
550 * Closes and archives the compacted files under this store
551 */
552 void closeAndArchiveCompactedFiles() throws IOException;
553 }