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