View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import java.io.IOException;
22  import java.io.InterruptedIOException;
23  import java.net.InetSocketAddress;
24  import java.security.Key;
25  import java.security.KeyException;
26  import java.security.PrivilegedExceptionAction;
27  import java.util.ArrayList;
28  import java.util.Collection;
29  import java.util.Collections;
30  import java.util.HashMap;
31  import java.util.HashSet;
32  import java.util.Iterator;
33  import java.util.List;
34  import java.util.NavigableSet;
35  import java.util.Set;
36  import java.util.concurrent.Callable;
37  import java.util.concurrent.CompletionService;
38  import java.util.concurrent.ConcurrentHashMap;
39  import java.util.concurrent.ExecutionException;
40  import java.util.concurrent.ExecutorCompletionService;
41  import java.util.concurrent.Future;
42  import java.util.concurrent.ThreadPoolExecutor;
43  import java.util.concurrent.atomic.AtomicBoolean;
44  import java.util.concurrent.locks.ReentrantReadWriteLock;
45  
46  import org.apache.commons.logging.Log;
47  import org.apache.commons.logging.LogFactory;
48  import org.apache.hadoop.conf.Configuration;
49  import org.apache.hadoop.fs.FileSystem;
50  import org.apache.hadoop.fs.Path;
51  import org.apache.hadoop.hbase.Cell;
52  import org.apache.hadoop.hbase.CellComparator;
53  import org.apache.hadoop.hbase.CellUtil;
54  import org.apache.hadoop.hbase.CompoundConfiguration;
55  import org.apache.hadoop.hbase.HColumnDescriptor;
56  import org.apache.hadoop.hbase.HConstants;
57  import org.apache.hadoop.hbase.HRegionInfo;
58  import org.apache.hadoop.hbase.KeyValue;
59  import org.apache.hadoop.hbase.RemoteExceptionHandler;
60  import org.apache.hadoop.hbase.TableName;
61  import org.apache.hadoop.hbase.Tag;
62  import org.apache.hadoop.hbase.TagType;
63  import org.apache.hadoop.hbase.classification.InterfaceAudience;
64  import org.apache.hadoop.hbase.client.Scan;
65  import org.apache.hadoop.hbase.conf.ConfigurationManager;
66  import org.apache.hadoop.hbase.io.compress.Compression;
67  import org.apache.hadoop.hbase.io.crypto.Cipher;
68  import org.apache.hadoop.hbase.io.crypto.Encryption;
69  import org.apache.hadoop.hbase.io.hfile.CacheConfig;
70  import org.apache.hadoop.hbase.io.hfile.HFile;
71  import org.apache.hadoop.hbase.io.hfile.HFileContext;
72  import org.apache.hadoop.hbase.io.hfile.HFileContextBuilder;
73  import org.apache.hadoop.hbase.io.hfile.HFileDataBlockEncoder;
74  import org.apache.hadoop.hbase.io.hfile.HFileDataBlockEncoderImpl;
75  import org.apache.hadoop.hbase.io.hfile.HFileScanner;
76  import org.apache.hadoop.hbase.io.hfile.InvalidHFileException;
77  import org.apache.hadoop.hbase.monitoring.MonitoredTask;
78  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
79  import org.apache.hadoop.hbase.protobuf.generated.WALProtos.CompactionDescriptor;
80  import org.apache.hadoop.hbase.regionserver.compactions.CompactionContext;
81  import org.apache.hadoop.hbase.regionserver.compactions.CompactionProgress;
82  import org.apache.hadoop.hbase.regionserver.compactions.CompactionRequest;
83  import org.apache.hadoop.hbase.regionserver.compactions.DefaultCompactor;
84  import org.apache.hadoop.hbase.regionserver.compactions.OffPeakHours;
85  import org.apache.hadoop.hbase.regionserver.compactions.CompactionThroughputController;
86  import org.apache.hadoop.hbase.regionserver.wal.WALUtil;
87  import org.apache.hadoop.hbase.security.EncryptionUtil;
88  import org.apache.hadoop.hbase.security.User;
89  import org.apache.hadoop.hbase.util.Bytes;
90  import org.apache.hadoop.hbase.util.ChecksumType;
91  import org.apache.hadoop.hbase.util.ClassSize;
92  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
93  import org.apache.hadoop.hbase.util.Pair;
94  import org.apache.hadoop.hbase.util.ReflectionUtils;
95  import org.apache.hadoop.util.StringUtils;
96  import org.apache.hadoop.util.StringUtils.TraditionalBinaryPrefix;
97  
98  import com.google.common.annotations.VisibleForTesting;
99  import com.google.common.base.Preconditions;
100 import com.google.common.collect.ImmutableCollection;
101 import com.google.common.collect.ImmutableList;
102 import com.google.common.collect.Lists;
103 import com.google.common.collect.Sets;
104 
105 /**
106  * A Store holds a column family in a Region.  Its a memstore and a set of zero
107  * or more StoreFiles, which stretch backwards over time.
108  *
109  * <p>There's no reason to consider append-logging at this level; all logging
110  * and locking is handled at the HRegion level.  Store just provides
111  * services to manage sets of StoreFiles.  One of the most important of those
112  * services is compaction services where files are aggregated once they pass
113  * a configurable threshold.
114  *
115  * <p>The only thing having to do with logs that Store needs to deal with is
116  * the reconstructionLog.  This is a segment of an HRegion's log that might
117  * NOT be present upon startup.  If the param is NULL, there's nothing to do.
118  * If the param is non-NULL, we need to process the log to reconstruct
119  * a TreeMap that might not have been written to disk before the process
120  * died.
121  *
122  * <p>It's assumed that after this constructor returns, the reconstructionLog
123  * file will be deleted (by whoever has instantiated the Store).
124  *
125  * <p>Locking and transactions are handled at a higher level.  This API should
126  * not be called directly but by an HRegion manager.
127  */
128 @InterfaceAudience.Private
129 public class HStore implements Store {
130   private static final String MEMSTORE_CLASS_NAME = "hbase.regionserver.memstore.class";
131   public static final String COMPACTCHECKER_INTERVAL_MULTIPLIER_KEY =
132       "hbase.server.compactchecker.interval.multiplier";
133   public static final String BLOCKING_STOREFILES_KEY = "hbase.hstore.blockingStoreFiles";
134   public static final int DEFAULT_COMPACTCHECKER_INTERVAL_MULTIPLIER = 1000;
135   public static final int DEFAULT_BLOCKING_STOREFILE_COUNT = 7;
136 
137   static final Log LOG = LogFactory.getLog(HStore.class);
138 
139   protected final MemStore memstore;
140   // This stores directory in the filesystem.
141   private final HRegion region;
142   private final HColumnDescriptor family;
143   private final HRegionFileSystem fs;
144   private Configuration conf;
145   private final CacheConfig cacheConf;
146   private long lastCompactSize = 0;
147   volatile boolean forceMajor = false;
148   /* how many bytes to write between status checks */
149   static int closeCheckInterval = 0;
150   private volatile long storeSize = 0L;
151   private volatile long totalUncompressedBytes = 0L;
152 
153   /**
154    * RWLock for store operations.
155    * Locked in shared mode when the list of component stores is looked at:
156    *   - all reads/writes to table data
157    *   - checking for split
158    * Locked in exclusive mode when the list of component stores is modified:
159    *   - closing
160    *   - completing a compaction
161    */
162   final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
163   private final boolean verifyBulkLoads;
164 
165   private ScanInfo scanInfo;
166 
167   // TODO: ideally, this should be part of storeFileManager, as we keep passing this to it.
168   final List<StoreFile> filesCompacting = Lists.newArrayList();
169 
170   // All access must be synchronized.
171   private final Set<ChangedReadersObserver> changedReaderObservers =
172     Collections.newSetFromMap(new ConcurrentHashMap<ChangedReadersObserver, Boolean>());
173 
174   private final int blocksize;
175   private HFileDataBlockEncoder dataBlockEncoder;
176 
177   /** Checksum configuration */
178   private ChecksumType checksumType;
179   private int bytesPerChecksum;
180 
181   // Comparing KeyValues
182   private final KeyValue.KVComparator comparator;
183 
184   final StoreEngine<?, ?, ?, ?> storeEngine;
185 
186   private static final AtomicBoolean offPeakCompactionTracker = new AtomicBoolean();
187   private volatile OffPeakHours offPeakHours;
188 
189   private static final int DEFAULT_FLUSH_RETRIES_NUMBER = 10;
190   private int flushRetriesNumber;
191   private int pauseTime;
192 
193   private long blockingFileCount;
194   private int compactionCheckMultiplier;
195 
196   private Encryption.Context cryptoContext = Encryption.Context.NONE;
197 
198   private volatile long flushedCellsCount = 0;
199   private volatile long compactedCellsCount = 0;
200   private volatile long majorCompactedCellsCount = 0;
201   private volatile long flushedCellsSize = 0;
202   private volatile long compactedCellsSize = 0;
203   private volatile long majorCompactedCellsSize = 0;
204 
205   /**
206    * Constructor
207    * @param region
208    * @param family HColumnDescriptor for this column
209    * @param confParam configuration object
210    * failed.  Can be null.
211    * @throws IOException
212    */
213   protected HStore(final HRegion region, final HColumnDescriptor family,
214       final Configuration confParam) throws IOException {
215 
216     HRegionInfo info = region.getRegionInfo();
217     this.fs = region.getRegionFileSystem();
218 
219     // Assemble the store's home directory and Ensure it exists.
220     fs.createStoreDir(family.getNameAsString());
221     this.region = region;
222     this.family = family;
223     // 'conf' renamed to 'confParam' b/c we use this.conf in the constructor
224     // CompoundConfiguration will look for keys in reverse order of addition, so we'd
225     // add global config first, then table and cf overrides, then cf metadata.
226     this.conf = new CompoundConfiguration()
227       .add(confParam)
228       .addStringMap(region.getTableDesc().getConfiguration())
229       .addStringMap(family.getConfiguration())
230       .addWritableMap(family.getValues());
231     this.blocksize = family.getBlocksize();
232 
233     this.dataBlockEncoder =
234         new HFileDataBlockEncoderImpl(family.getDataBlockEncoding());
235 
236     this.comparator = info.getComparator();
237     // used by ScanQueryMatcher
238     long timeToPurgeDeletes =
239         Math.max(conf.getLong("hbase.hstore.time.to.purge.deletes", 0), 0);
240     LOG.trace("Time to purge deletes set to " + timeToPurgeDeletes +
241         "ms in store " + this);
242     // Get TTL
243     long ttl = determineTTLFromFamily(family);
244     // Why not just pass a HColumnDescriptor in here altogether?  Even if have
245     // to clone it?
246     scanInfo = new ScanInfo(family, ttl, timeToPurgeDeletes, this.comparator);
247     String className = conf.get(MEMSTORE_CLASS_NAME, DefaultMemStore.class.getName());
248     this.memstore = ReflectionUtils.instantiateWithCustomCtor(className, new Class[] {
249         Configuration.class, KeyValue.KVComparator.class }, new Object[] { conf, this.comparator });
250     this.offPeakHours = OffPeakHours.getInstance(conf);
251 
252     // Setting up cache configuration for this family
253     this.cacheConf = new CacheConfig(conf, family);
254 
255     this.verifyBulkLoads = conf.getBoolean("hbase.hstore.bulkload.verify", false);
256 
257     this.blockingFileCount =
258         conf.getInt(BLOCKING_STOREFILES_KEY, DEFAULT_BLOCKING_STOREFILE_COUNT);
259     this.compactionCheckMultiplier = conf.getInt(
260         COMPACTCHECKER_INTERVAL_MULTIPLIER_KEY, DEFAULT_COMPACTCHECKER_INTERVAL_MULTIPLIER);
261     if (this.compactionCheckMultiplier <= 0) {
262       LOG.error("Compaction check period multiplier must be positive, setting default: "
263           + DEFAULT_COMPACTCHECKER_INTERVAL_MULTIPLIER);
264       this.compactionCheckMultiplier = DEFAULT_COMPACTCHECKER_INTERVAL_MULTIPLIER;
265     }
266 
267     if (HStore.closeCheckInterval == 0) {
268       HStore.closeCheckInterval = conf.getInt(
269           "hbase.hstore.close.check.interval", 10*1000*1000 /* 10 MB */);
270     }
271 
272     this.storeEngine = StoreEngine.create(this, this.conf, this.comparator);
273     this.storeEngine.getStoreFileManager().loadFiles(loadStoreFiles());
274 
275     // Initialize checksum type from name. The names are CRC32, CRC32C, etc.
276     this.checksumType = getChecksumType(conf);
277     // initilize bytes per checksum
278     this.bytesPerChecksum = getBytesPerChecksum(conf);
279     flushRetriesNumber = conf.getInt(
280         "hbase.hstore.flush.retries.number", DEFAULT_FLUSH_RETRIES_NUMBER);
281     pauseTime = conf.getInt(HConstants.HBASE_SERVER_PAUSE, HConstants.DEFAULT_HBASE_SERVER_PAUSE);
282     if (flushRetriesNumber <= 0) {
283       throw new IllegalArgumentException(
284           "hbase.hstore.flush.retries.number must be > 0, not "
285               + flushRetriesNumber);
286     }
287 
288     // Crypto context for new store files
289     String cipherName = family.getEncryptionType();
290     if (cipherName != null) {
291       Cipher cipher;
292       Key key;
293       byte[] keyBytes = family.getEncryptionKey();
294       if (keyBytes != null) {
295         // Family provides specific key material
296         String masterKeyName = conf.get(HConstants.CRYPTO_MASTERKEY_NAME_CONF_KEY,
297           User.getCurrent().getShortName());
298         try {
299           // First try the master key
300           key = EncryptionUtil.unwrapKey(conf, masterKeyName, keyBytes);
301         } catch (KeyException e) {
302           // If the current master key fails to unwrap, try the alternate, if
303           // one is configured
304           if (LOG.isDebugEnabled()) {
305             LOG.debug("Unable to unwrap key with current master key '" + masterKeyName + "'");
306           }
307           String alternateKeyName =
308             conf.get(HConstants.CRYPTO_MASTERKEY_ALTERNATE_NAME_CONF_KEY);
309           if (alternateKeyName != null) {
310             try {
311               key = EncryptionUtil.unwrapKey(conf, alternateKeyName, keyBytes);
312             } catch (KeyException ex) {
313               throw new IOException(ex);
314             }
315           } else {
316             throw new IOException(e);
317           }
318         }
319         // Use the algorithm the key wants
320         cipher = Encryption.getCipher(conf, key.getAlgorithm());
321         if (cipher == null) {
322           throw new RuntimeException("Cipher '" + key.getAlgorithm() + "' is not available");
323         }
324         // Fail if misconfigured
325         // We use the encryption type specified in the column schema as a sanity check on
326         // what the wrapped key is telling us
327         if (!cipher.getName().equalsIgnoreCase(cipherName)) {
328           throw new RuntimeException("Encryption for family '" + family.getNameAsString() +
329             "' configured with type '" + cipherName +
330             "' but key specifies algorithm '" + cipher.getName() + "'");
331         }
332       } else {
333         // Family does not provide key material, create a random key
334         cipher = Encryption.getCipher(conf, cipherName);
335         if (cipher == null) {
336           throw new RuntimeException("Cipher '" + cipherName + "' is not available");
337         }
338         key = cipher.getRandomKey();
339       }
340       cryptoContext = Encryption.newContext(conf);
341       cryptoContext.setCipher(cipher);
342       cryptoContext.setKey(key);
343     }
344   }
345 
346   /**
347    * @param family
348    * @return TTL in seconds of the specified family
349    */
350   private static long determineTTLFromFamily(final HColumnDescriptor family) {
351     // HCD.getTimeToLive returns ttl in seconds.  Convert to milliseconds.
352     long ttl = family.getTimeToLive();
353     if (ttl == HConstants.FOREVER) {
354       // Default is unlimited ttl.
355       ttl = Long.MAX_VALUE;
356     } else if (ttl == -1) {
357       ttl = Long.MAX_VALUE;
358     } else {
359       // Second -> ms adjust for user data
360       ttl *= 1000;
361     }
362     return ttl;
363   }
364 
365   @Override
366   public String getColumnFamilyName() {
367     return this.family.getNameAsString();
368   }
369 
370   @Override
371   public TableName getTableName() {
372     return this.getRegionInfo().getTable();
373   }
374 
375   @Override
376   public FileSystem getFileSystem() {
377     return this.fs.getFileSystem();
378   }
379 
380   public HRegionFileSystem getRegionFileSystem() {
381     return this.fs;
382   }
383 
384   /* Implementation of StoreConfigInformation */
385   @Override
386   public long getStoreFileTtl() {
387     // TTL only applies if there's no MIN_VERSIONs setting on the column.
388     return (this.scanInfo.getMinVersions() == 0) ? this.scanInfo.getTtl() : Long.MAX_VALUE;
389   }
390 
391   @Override
392   public long getMemstoreFlushSize() {
393     // TODO: Why is this in here?  The flushsize of the region rather than the store?  St.Ack
394     return this.region.memstoreFlushSize;
395   }
396 
397   @Override
398   public long getFlushableSize() {
399     return this.memstore.getFlushableSize();
400   }
401 
402   @Override
403   public long getSnapshotSize() {
404     return this.memstore.getSnapshotSize();
405   }
406 
407   @Override
408   public long getCompactionCheckMultiplier() {
409     return this.compactionCheckMultiplier;
410   }
411 
412   @Override
413   public long getBlockingFileCount() {
414     return blockingFileCount;
415   }
416   /* End implementation of StoreConfigInformation */
417 
418   /**
419    * Returns the configured bytesPerChecksum value.
420    * @param conf The configuration
421    * @return The bytesPerChecksum that is set in the configuration
422    */
423   public static int getBytesPerChecksum(Configuration conf) {
424     return conf.getInt(HConstants.BYTES_PER_CHECKSUM,
425                        HFile.DEFAULT_BYTES_PER_CHECKSUM);
426   }
427 
428   /**
429    * Returns the configured checksum algorithm.
430    * @param conf The configuration
431    * @return The checksum algorithm that is set in the configuration
432    */
433   public static ChecksumType getChecksumType(Configuration conf) {
434     String checksumName = conf.get(HConstants.CHECKSUM_TYPE_NAME);
435     if (checksumName == null) {
436       return HFile.DEFAULT_CHECKSUM_TYPE;
437     } else {
438       return ChecksumType.nameToType(checksumName);
439     }
440   }
441 
442   /**
443    * @return how many bytes to write between status checks
444    */
445   public static int getCloseCheckInterval() {
446     return closeCheckInterval;
447   }
448 
449   @Override
450   public HColumnDescriptor getFamily() {
451     return this.family;
452   }
453 
454   /**
455    * @return The maximum sequence id in all store files. Used for log replay.
456    */
457   @Override
458   public long getMaxSequenceId() {
459     return StoreFile.getMaxSequenceIdInList(this.getStorefiles());
460   }
461 
462   @Override
463   public long getMaxMemstoreTS() {
464     return StoreFile.getMaxMemstoreTSInList(this.getStorefiles());
465   }
466 
467   /**
468    * @param tabledir {@link Path} to where the table is being stored
469    * @param hri {@link HRegionInfo} for the region.
470    * @param family {@link HColumnDescriptor} describing the column family
471    * @return Path to family/Store home directory.
472    */
473   @Deprecated
474   public static Path getStoreHomedir(final Path tabledir,
475       final HRegionInfo hri, final byte[] family) {
476     return getStoreHomedir(tabledir, hri.getEncodedName(), family);
477   }
478 
479   /**
480    * @param tabledir {@link Path} to where the table is being stored
481    * @param encodedName Encoded region name.
482    * @param family {@link HColumnDescriptor} describing the column family
483    * @return Path to family/Store home directory.
484    */
485   @Deprecated
486   public static Path getStoreHomedir(final Path tabledir,
487       final String encodedName, final byte[] family) {
488     return new Path(tabledir, new Path(encodedName, Bytes.toString(family)));
489   }
490 
491   @Override
492   public HFileDataBlockEncoder getDataBlockEncoder() {
493     return dataBlockEncoder;
494   }
495 
496   /**
497    * Should be used only in tests.
498    * @param blockEncoder the block delta encoder to use
499    */
500   void setDataBlockEncoderInTest(HFileDataBlockEncoder blockEncoder) {
501     this.dataBlockEncoder = blockEncoder;
502   }
503 
504   /**
505    * Creates an unsorted list of StoreFile loaded in parallel
506    * from the given directory.
507    * @throws IOException
508    */
509   private List<StoreFile> loadStoreFiles() throws IOException {
510     Collection<StoreFileInfo> files = fs.getStoreFiles(getColumnFamilyName());
511     return openStoreFiles(files);
512   }
513 
514   private List<StoreFile> openStoreFiles(Collection<StoreFileInfo> files) throws IOException {
515     if (files == null || files.size() == 0) {
516       return new ArrayList<StoreFile>();
517     }
518     // initialize the thread pool for opening store files in parallel..
519     ThreadPoolExecutor storeFileOpenerThreadPool =
520       this.region.getStoreFileOpenAndCloseThreadPool("StoreFileOpenerThread-" +
521           this.getColumnFamilyName());
522     CompletionService<StoreFile> completionService =
523       new ExecutorCompletionService<StoreFile>(storeFileOpenerThreadPool);
524 
525     int totalValidStoreFile = 0;
526     for (final StoreFileInfo storeFileInfo: files) {
527       // open each store file in parallel
528       completionService.submit(new Callable<StoreFile>() {
529         @Override
530         public StoreFile call() throws IOException {
531           StoreFile storeFile = createStoreFileAndReader(storeFileInfo);
532           return storeFile;
533         }
534       });
535       totalValidStoreFile++;
536     }
537 
538     ArrayList<StoreFile> results = new ArrayList<StoreFile>(files.size());
539     IOException ioe = null;
540     try {
541       for (int i = 0; i < totalValidStoreFile; i++) {
542         try {
543           Future<StoreFile> future = completionService.take();
544           StoreFile storeFile = future.get();
545           long length = storeFile.getReader().length();
546           this.storeSize += length;
547           this.totalUncompressedBytes +=
548               storeFile.getReader().getTotalUncompressedBytes();
549           if (LOG.isDebugEnabled()) {
550             LOG.debug("loaded " + storeFile.toStringDetailed());
551           }
552           results.add(storeFile);
553         } catch (InterruptedException e) {
554           if (ioe == null) ioe = new InterruptedIOException(e.getMessage());
555         } catch (ExecutionException e) {
556           if (ioe == null) ioe = new IOException(e.getCause());
557         }
558       }
559     } finally {
560       storeFileOpenerThreadPool.shutdownNow();
561     }
562     if (ioe != null) {
563       // close StoreFile readers
564       for (StoreFile file : results) {
565         try {
566           if (file != null) file.closeReader(true);
567         } catch (IOException e) {
568           LOG.warn(e.getMessage());
569         }
570       }
571       throw ioe;
572     }
573 
574     return results;
575   }
576 
577   /**
578    * Checks the underlying store files, and opens the files that  have not
579    * been opened, and removes the store file readers for store files no longer
580    * available. Mainly used by secondary region replicas to keep up to date with
581    * the primary region files.
582    * @throws IOException
583    */
584   @Override
585   public void refreshStoreFiles() throws IOException {
586     Collection<StoreFileInfo> newFiles = fs.getStoreFiles(getColumnFamilyName());
587     refreshStoreFilesInternal(newFiles);
588   }
589 
590   @Override
591   public void refreshStoreFiles(Collection<String> newFiles) throws IOException {
592     List<StoreFileInfo> storeFiles = new ArrayList<StoreFileInfo>(newFiles.size());
593     for (String file : newFiles) {
594       storeFiles.add(fs.getStoreFileInfo(getColumnFamilyName(), file));
595     }
596     refreshStoreFilesInternal(storeFiles);
597   }
598 
599   /**
600    * Checks the underlying store files, and opens the files that  have not
601    * been opened, and removes the store file readers for store files no longer
602    * available. Mainly used by secondary region replicas to keep up to date with
603    * the primary region files.
604    * @throws IOException
605    */
606   private void refreshStoreFilesInternal(Collection<StoreFileInfo> newFiles) throws IOException {
607     StoreFileManager sfm = storeEngine.getStoreFileManager();
608     Collection<StoreFile> currentFiles = sfm.getStorefiles();
609     if (currentFiles == null) currentFiles = new ArrayList<StoreFile>(0);
610 
611     if (newFiles == null) newFiles = new ArrayList<StoreFileInfo>(0);
612 
613     HashMap<StoreFileInfo, StoreFile> currentFilesSet = new HashMap<StoreFileInfo, StoreFile>(currentFiles.size());
614     for (StoreFile sf : currentFiles) {
615       currentFilesSet.put(sf.getFileInfo(), sf);
616     }
617     HashSet<StoreFileInfo> newFilesSet = new HashSet<StoreFileInfo>(newFiles);
618 
619     Set<StoreFileInfo> toBeAddedFiles = Sets.difference(newFilesSet, currentFilesSet.keySet());
620     Set<StoreFileInfo> toBeRemovedFiles = Sets.difference(currentFilesSet.keySet(), newFilesSet);
621 
622     if (toBeAddedFiles.isEmpty() && toBeRemovedFiles.isEmpty()) {
623       return;
624     }
625 
626     LOG.info("Refreshing store files for region " + this.getRegionInfo().getRegionNameAsString()
627       + " files to add: " + toBeAddedFiles + " files to remove: " + toBeRemovedFiles);
628 
629     Set<StoreFile> toBeRemovedStoreFiles = new HashSet<StoreFile>(toBeRemovedFiles.size());
630     for (StoreFileInfo sfi : toBeRemovedFiles) {
631       toBeRemovedStoreFiles.add(currentFilesSet.get(sfi));
632     }
633 
634     // try to open the files
635     List<StoreFile> openedFiles = openStoreFiles(toBeAddedFiles);
636 
637     // propogate the file changes to the underlying store file manager
638     replaceStoreFiles(toBeRemovedStoreFiles, openedFiles); //won't throw an exception
639 
640     // Advance the memstore read point to be at least the new store files seqIds so that
641     // readers might pick it up. This assumes that the store is not getting any writes (otherwise
642     // in-flight transactions might be made visible)
643     if (!toBeAddedFiles.isEmpty()) {
644       region.getMVCC().advanceMemstoreReadPointIfNeeded(this.getMaxSequenceId());
645     }
646 
647     // notify scanners, close file readers, and recompute store size
648     completeCompaction(toBeRemovedStoreFiles, false);
649   }
650 
651   private StoreFile createStoreFileAndReader(final Path p) throws IOException {
652     StoreFileInfo info = new StoreFileInfo(conf, this.getFileSystem(), p);
653     return createStoreFileAndReader(info);
654   }
655 
656   private StoreFile createStoreFileAndReader(final StoreFileInfo info)
657       throws IOException {
658     info.setRegionCoprocessorHost(this.region.getCoprocessorHost());
659     StoreFile storeFile = new StoreFile(this.getFileSystem(), info, this.conf, this.cacheConf,
660       this.family.getBloomFilterType());
661     storeFile.createReader();
662     return storeFile;
663   }
664 
665   @Override
666   public Pair<Long, Cell> add(final Cell cell) {
667     lock.readLock().lock();
668     try {
669        return this.memstore.add(cell);
670     } finally {
671       lock.readLock().unlock();
672     }
673   }
674 
675   @Override
676   public long timeOfOldestEdit() {
677     return memstore.timeOfOldestEdit();
678   }
679 
680   /**
681    * Adds a value to the memstore
682    *
683    * @param kv
684    * @return memstore size delta
685    */
686   protected long delete(final KeyValue kv) {
687     lock.readLock().lock();
688     try {
689       return this.memstore.delete(kv);
690     } finally {
691       lock.readLock().unlock();
692     }
693   }
694 
695   @Override
696   public void rollback(final Cell cell) {
697     lock.readLock().lock();
698     try {
699       this.memstore.rollback(cell);
700     } finally {
701       lock.readLock().unlock();
702     }
703   }
704 
705   /**
706    * @return All store files.
707    */
708   @Override
709   public Collection<StoreFile> getStorefiles() {
710     return this.storeEngine.getStoreFileManager().getStorefiles();
711   }
712 
713   @Override
714   public void assertBulkLoadHFileOk(Path srcPath) throws IOException {
715     HFile.Reader reader  = null;
716     try {
717       LOG.info("Validating hfile at " + srcPath + " for inclusion in "
718           + "store " + this + " region " + this.getRegionInfo().getRegionNameAsString());
719       reader = HFile.createReader(srcPath.getFileSystem(conf),
720           srcPath, cacheConf, conf);
721       reader.loadFileInfo();
722 
723       byte[] firstKey = reader.getFirstRowKey();
724       Preconditions.checkState(firstKey != null, "First key can not be null");
725       byte[] lk = reader.getLastKey();
726       Preconditions.checkState(lk != null, "Last key can not be null");
727       byte[] lastKey =  KeyValue.createKeyValueFromKey(lk).getRow();
728 
729       LOG.debug("HFile bounds: first=" + Bytes.toStringBinary(firstKey) +
730           " last=" + Bytes.toStringBinary(lastKey));
731       LOG.debug("Region bounds: first=" +
732           Bytes.toStringBinary(getRegionInfo().getStartKey()) +
733           " last=" + Bytes.toStringBinary(getRegionInfo().getEndKey()));
734 
735       if (!this.getRegionInfo().containsRange(firstKey, lastKey)) {
736         throw new WrongRegionException(
737             "Bulk load file " + srcPath.toString() + " does not fit inside region "
738             + this.getRegionInfo().getRegionNameAsString());
739       }
740 
741       if(reader.length() > conf.getLong(HConstants.HREGION_MAX_FILESIZE,
742           HConstants.DEFAULT_MAX_FILE_SIZE)) {
743         LOG.warn("Trying to bulk load hfile " + srcPath.toString() + " with size: " +
744             reader.length() + " bytes can be problematic as it may lead to oversplitting.");
745       }
746 
747       if (verifyBulkLoads) {
748         long verificationStartTime = EnvironmentEdgeManager.currentTime();
749         LOG.info("Full verification started for bulk load hfile: " + srcPath.toString());
750         Cell prevCell = null;
751         HFileScanner scanner = reader.getScanner(false, false, false);
752         scanner.seekTo();
753         do {
754           Cell cell = scanner.getKeyValue();
755           if (prevCell != null) {
756             if (CellComparator.compareRows(prevCell, cell) > 0) {
757               throw new InvalidHFileException("Previous row is greater than"
758                   + " current row: path=" + srcPath + " previous="
759                   + CellUtil.getCellKeyAsString(prevCell) + " current="
760                   + CellUtil.getCellKeyAsString(cell));
761             }
762             if (CellComparator.compareFamilies(prevCell, cell) != 0) {
763               throw new InvalidHFileException("Previous key had different"
764                   + " family compared to current key: path=" + srcPath
765                   + " previous="
766                   + Bytes.toStringBinary(prevCell.getFamilyArray(), prevCell.getFamilyOffset(),
767                       prevCell.getFamilyLength())
768                   + " current="
769                   + Bytes.toStringBinary(cell.getFamilyArray(), cell.getFamilyOffset(),
770                       cell.getFamilyLength()));
771             }
772           }
773           prevCell = cell;
774         } while (scanner.next());
775       LOG.info("Full verification complete for bulk load hfile: " + srcPath.toString()
776          + " took " + (EnvironmentEdgeManager.currentTime() - verificationStartTime)
777          + " ms");
778       }
779     } finally {
780       if (reader != null) reader.close();
781     }
782   }
783 
784   @Override
785   public Path bulkLoadHFile(String srcPathStr, long seqNum) throws IOException {
786     Path srcPath = new Path(srcPathStr);
787     Path dstPath = fs.bulkLoadStoreFile(getColumnFamilyName(), srcPath, seqNum);
788 
789     LOG.info("Loaded HFile " + srcPath + " into store '" + getColumnFamilyName() + "' as "
790         + dstPath + " - updating store file list.");
791 
792     StoreFile sf = createStoreFileAndReader(dstPath);
793     bulkLoadHFile(sf);
794 
795     LOG.info("Successfully loaded store file " + srcPath + " into store " + this
796         + " (new location: " + dstPath + ")");
797 
798     return dstPath;
799   }
800 
801   @Override
802   public void bulkLoadHFile(StoreFileInfo fileInfo) throws IOException {
803     StoreFile sf = createStoreFileAndReader(fileInfo);
804     bulkLoadHFile(sf);
805   }
806 
807   private void bulkLoadHFile(StoreFile sf) throws IOException {
808     StoreFile.Reader r = sf.getReader();
809     this.storeSize += r.length();
810     this.totalUncompressedBytes += r.getTotalUncompressedBytes();
811 
812     // Append the new storefile into the list
813     this.lock.writeLock().lock();
814     try {
815       this.storeEngine.getStoreFileManager().insertNewFiles(Lists.newArrayList(sf));
816     } finally {
817       // We need the lock, as long as we are updating the storeFiles
818       // or changing the memstore. Let us release it before calling
819       // notifyChangeReadersObservers. See HBASE-4485 for a possible
820       // deadlock scenario that could have happened if continue to hold
821       // the lock.
822       this.lock.writeLock().unlock();
823     }
824     notifyChangedReadersObservers();
825     LOG.info("Loaded HFile " + sf.getFileInfo() + " into store '" + getColumnFamilyName());
826     if (LOG.isTraceEnabled()) {
827       String traceMessage = "BULK LOAD time,size,store size,store files ["
828           + EnvironmentEdgeManager.currentTime() + "," + r.length() + "," + storeSize
829           + "," + storeEngine.getStoreFileManager().getStorefileCount() + "]";
830       LOG.trace(traceMessage);
831     }
832   }
833 
834   @Override
835   public ImmutableCollection<StoreFile> close() throws IOException {
836     this.lock.writeLock().lock();
837     try {
838       // Clear so metrics doesn't find them.
839       ImmutableCollection<StoreFile> result = storeEngine.getStoreFileManager().clearFiles();
840 
841       if (!result.isEmpty()) {
842         // initialize the thread pool for closing store files in parallel.
843         ThreadPoolExecutor storeFileCloserThreadPool = this.region
844             .getStoreFileOpenAndCloseThreadPool("StoreFileCloserThread-"
845                 + this.getColumnFamilyName());
846 
847         // close each store file in parallel
848         CompletionService<Void> completionService =
849           new ExecutorCompletionService<Void>(storeFileCloserThreadPool);
850         for (final StoreFile f : result) {
851           completionService.submit(new Callable<Void>() {
852             @Override
853             public Void call() throws IOException {
854               boolean evictOnClose = 
855                   cacheConf != null? cacheConf.shouldEvictOnClose(): true; 
856               f.closeReader(evictOnClose);
857               return null;
858             }
859           });
860         }
861 
862         IOException ioe = null;
863         try {
864           for (int i = 0; i < result.size(); i++) {
865             try {
866               Future<Void> future = completionService.take();
867               future.get();
868             } catch (InterruptedException e) {
869               if (ioe == null) {
870                 ioe = new InterruptedIOException();
871                 ioe.initCause(e);
872               }
873             } catch (ExecutionException e) {
874               if (ioe == null) ioe = new IOException(e.getCause());
875             }
876           }
877         } finally {
878           storeFileCloserThreadPool.shutdownNow();
879         }
880         if (ioe != null) throw ioe;
881       }
882       LOG.info("Closed " + this);
883       return result;
884     } finally {
885       this.lock.writeLock().unlock();
886     }
887   }
888 
889   /**
890    * Snapshot this stores memstore. Call before running
891    * {@link #flushCache(long, MemStoreSnapshot, MonitoredTask)}
892    *  so it has some work to do.
893    */
894   void snapshot() {
895     this.lock.writeLock().lock();
896     try {
897       this.memstore.snapshot();
898     } finally {
899       this.lock.writeLock().unlock();
900     }
901   }
902 
903   /**
904    * Write out current snapshot.  Presumes {@link #snapshot()} has been called
905    * previously.
906    * @param logCacheFlushId flush sequence number
907    * @param snapshot
908    * @param status
909    * @return The path name of the tmp file to which the store was flushed
910    * @throws IOException
911    */
912   protected List<Path> flushCache(final long logCacheFlushId, MemStoreSnapshot snapshot,
913       MonitoredTask status) throws IOException {
914     // If an exception happens flushing, we let it out without clearing
915     // the memstore snapshot.  The old snapshot will be returned when we say
916     // 'snapshot', the next time flush comes around.
917     // Retry after catching exception when flushing, otherwise server will abort
918     // itself
919     StoreFlusher flusher = storeEngine.getStoreFlusher();
920     IOException lastException = null;
921     for (int i = 0; i < flushRetriesNumber; i++) {
922       try {
923         List<Path> pathNames = flusher.flushSnapshot(snapshot, logCacheFlushId, status);
924         Path lastPathName = null;
925         try {
926           for (Path pathName : pathNames) {
927             lastPathName = pathName;
928             validateStoreFile(pathName);
929           }
930           return pathNames;
931         } catch (Exception e) {
932           LOG.warn("Failed validating store file " + lastPathName + ", retrying num=" + i, e);
933           if (e instanceof IOException) {
934             lastException = (IOException) e;
935           } else {
936             lastException = new IOException(e);
937           }
938         }
939       } catch (IOException e) {
940         LOG.warn("Failed flushing store file, retrying num=" + i, e);
941         lastException = e;
942       }
943       if (lastException != null && i < (flushRetriesNumber - 1)) {
944         try {
945           Thread.sleep(pauseTime);
946         } catch (InterruptedException e) {
947           IOException iie = new InterruptedIOException();
948           iie.initCause(e);
949           throw iie;
950         }
951       }
952     }
953     throw lastException;
954   }
955 
956   /*
957    * @param path The pathname of the tmp file into which the store was flushed
958    * @param logCacheFlushId
959    * @param status
960    * @return StoreFile created.
961    * @throws IOException
962    */
963   private StoreFile commitFile(final Path path, final long logCacheFlushId, MonitoredTask status)
964       throws IOException {
965     // Write-out finished successfully, move into the right spot
966     Path dstPath = fs.commitStoreFile(getColumnFamilyName(), path);
967 
968     status.setStatus("Flushing " + this + ": reopening flushed file");
969     StoreFile sf = createStoreFileAndReader(dstPath);
970 
971     StoreFile.Reader r = sf.getReader();
972     this.storeSize += r.length();
973     this.totalUncompressedBytes += r.getTotalUncompressedBytes();
974 
975     if (LOG.isInfoEnabled()) {
976       LOG.info("Added " + sf + ", entries=" + r.getEntries() +
977         ", sequenceid=" + logCacheFlushId +
978         ", filesize=" + TraditionalBinaryPrefix.long2String(r.length(), "", 1));
979     }
980     return sf;
981   }
982 
983   /*
984    * @param maxKeyCount
985    * @param compression Compression algorithm to use
986    * @param isCompaction whether we are creating a new file in a compaction
987    * @param includesMVCCReadPoint - whether to include MVCC or not
988    * @param includesTag - includesTag or not
989    * @return Writer for a new StoreFile in the tmp dir.
990    */
991   @Override
992   public StoreFile.Writer createWriterInTmp(long maxKeyCount, Compression.Algorithm compression,
993       boolean isCompaction, boolean includeMVCCReadpoint, boolean includesTag)
994   throws IOException {
995     final CacheConfig writerCacheConf;
996     if (isCompaction) {
997       // Don't cache data on write on compactions.
998       writerCacheConf = new CacheConfig(cacheConf);
999       writerCacheConf.setCacheDataOnWrite(false);
1000     } else {
1001       writerCacheConf = cacheConf;
1002     }
1003     InetSocketAddress[] favoredNodes = null;
1004     if (region.getRegionServerServices() != null) {
1005       favoredNodes = region.getRegionServerServices().getFavoredNodesForRegion(
1006           region.getRegionInfo().getEncodedName());
1007     }
1008     HFileContext hFileContext = createFileContext(compression, includeMVCCReadpoint, includesTag,
1009       cryptoContext);
1010     StoreFile.Writer w = new StoreFile.WriterBuilder(conf, writerCacheConf,
1011         this.getFileSystem())
1012             .withFilePath(fs.createTempName())
1013             .withComparator(comparator)
1014             .withBloomType(family.getBloomFilterType())
1015             .withMaxKeyCount(maxKeyCount)
1016             .withFavoredNodes(favoredNodes)
1017             .withFileContext(hFileContext)
1018             .build();
1019     return w;
1020   }
1021 
1022   private HFileContext createFileContext(Compression.Algorithm compression,
1023       boolean includeMVCCReadpoint, boolean includesTag, Encryption.Context cryptoContext) {
1024     if (compression == null) {
1025       compression = HFile.DEFAULT_COMPRESSION_ALGORITHM;
1026     }
1027     HFileContext hFileContext = new HFileContextBuilder()
1028                                 .withIncludesMvcc(includeMVCCReadpoint)
1029                                 .withIncludesTags(includesTag)
1030                                 .withCompression(compression)
1031                                 .withCompressTags(family.isCompressTags())
1032                                 .withChecksumType(checksumType)
1033                                 .withBytesPerCheckSum(bytesPerChecksum)
1034                                 .withBlockSize(blocksize)
1035                                 .withHBaseCheckSum(true)
1036                                 .withDataBlockEncoding(family.getDataBlockEncoding())
1037                                 .withEncryptionContext(cryptoContext)
1038                                 .withCreateTime(EnvironmentEdgeManager.currentTime())
1039                                 .build();
1040     return hFileContext;
1041   }
1042 
1043 
1044   /*
1045    * Change storeFiles adding into place the Reader produced by this new flush.
1046    * @param sfs Store files
1047    * @param snapshotId
1048    * @throws IOException
1049    * @return Whether compaction is required.
1050    */
1051   private boolean updateStorefiles(final List<StoreFile> sfs, final long snapshotId)
1052       throws IOException {
1053     this.lock.writeLock().lock();
1054     try {
1055       this.storeEngine.getStoreFileManager().insertNewFiles(sfs);
1056       if (snapshotId > 0) {
1057         this.memstore.clearSnapshot(snapshotId);
1058       }
1059     } finally {
1060       // We need the lock, as long as we are updating the storeFiles
1061       // or changing the memstore. Let us release it before calling
1062       // notifyChangeReadersObservers. See HBASE-4485 for a possible
1063       // deadlock scenario that could have happened if continue to hold
1064       // the lock.
1065       this.lock.writeLock().unlock();
1066     }
1067 
1068     // Tell listeners of the change in readers.
1069     notifyChangedReadersObservers();
1070 
1071     if (LOG.isTraceEnabled()) {
1072       long totalSize = 0;
1073       for (StoreFile sf : sfs) {
1074         totalSize += sf.getReader().length();
1075       }
1076       String traceMessage = "FLUSH time,count,size,store size,store files ["
1077           + EnvironmentEdgeManager.currentTime() + "," + sfs.size() + "," + totalSize
1078           + "," + storeSize + "," + storeEngine.getStoreFileManager().getStorefileCount() + "]";
1079       LOG.trace(traceMessage);
1080     }
1081     return needsCompaction();
1082   }
1083 
1084   /*
1085    * Notify all observers that set of Readers has changed.
1086    * @throws IOException
1087    */
1088   private void notifyChangedReadersObservers() throws IOException {
1089     for (ChangedReadersObserver o: this.changedReaderObservers) {
1090       o.updateReaders();
1091     }
1092   }
1093 
1094   /**
1095    * Get all scanners with no filtering based on TTL (that happens further down
1096    * the line).
1097    * @return all scanners for this store
1098    */
1099   @Override
1100   public List<KeyValueScanner> getScanners(boolean cacheBlocks, boolean isGet,
1101       boolean usePread, boolean isCompaction, ScanQueryMatcher matcher, byte[] startRow,
1102       byte[] stopRow, long readPt) throws IOException {
1103     Collection<StoreFile> storeFilesToScan;
1104     List<KeyValueScanner> memStoreScanners;
1105     this.lock.readLock().lock();
1106     try {
1107       storeFilesToScan =
1108           this.storeEngine.getStoreFileManager().getFilesForScanOrGet(isGet, startRow, stopRow);
1109       memStoreScanners = this.memstore.getScanners(readPt);
1110     } finally {
1111       this.lock.readLock().unlock();
1112     }
1113 
1114     // First the store file scanners
1115 
1116     // TODO this used to get the store files in descending order,
1117     // but now we get them in ascending order, which I think is
1118     // actually more correct, since memstore get put at the end.
1119     List<StoreFileScanner> sfScanners = StoreFileScanner
1120       .getScannersForStoreFiles(storeFilesToScan, cacheBlocks, usePread, isCompaction, matcher,
1121         readPt);
1122     List<KeyValueScanner> scanners =
1123       new ArrayList<KeyValueScanner>(sfScanners.size()+1);
1124     scanners.addAll(sfScanners);
1125     // Then the memstore scanners
1126     scanners.addAll(memStoreScanners);
1127     return scanners;
1128   }
1129 
1130   @Override
1131   public void addChangedReaderObserver(ChangedReadersObserver o) {
1132     this.changedReaderObservers.add(o);
1133   }
1134 
1135   @Override
1136   public void deleteChangedReaderObserver(ChangedReadersObserver o) {
1137     // We don't check if observer present; it may not be (legitimately)
1138     this.changedReaderObservers.remove(o);
1139   }
1140 
1141   //////////////////////////////////////////////////////////////////////////////
1142   // Compaction
1143   //////////////////////////////////////////////////////////////////////////////
1144 
1145   /**
1146    * Compact the StoreFiles.  This method may take some time, so the calling
1147    * thread must be able to block for long periods.
1148    *
1149    * <p>During this time, the Store can work as usual, getting values from
1150    * StoreFiles and writing new StoreFiles from the memstore.
1151    *
1152    * Existing StoreFiles are not destroyed until the new compacted StoreFile is
1153    * completely written-out to disk.
1154    *
1155    * <p>The compactLock prevents multiple simultaneous compactions.
1156    * The structureLock prevents us from interfering with other write operations.
1157    *
1158    * <p>We don't want to hold the structureLock for the whole time, as a compact()
1159    * can be lengthy and we want to allow cache-flushes during this period.
1160    *
1161    * <p> Compaction event should be idempotent, since there is no IO Fencing for
1162    * the region directory in hdfs. A region server might still try to complete the
1163    * compaction after it lost the region. That is why the following events are carefully
1164    * ordered for a compaction:
1165    *  1. Compaction writes new files under region/.tmp directory (compaction output)
1166    *  2. Compaction atomically moves the temporary file under region directory
1167    *  3. Compaction appends a WAL edit containing the compaction input and output files.
1168    *  Forces sync on WAL.
1169    *  4. Compaction deletes the input files from the region directory.
1170    *
1171    * Failure conditions are handled like this:
1172    *  - If RS fails before 2, compaction wont complete. Even if RS lives on and finishes
1173    *  the compaction later, it will only write the new data file to the region directory.
1174    *  Since we already have this data, this will be idempotent but we will have a redundant
1175    *  copy of the data.
1176    *  - If RS fails between 2 and 3, the region will have a redundant copy of the data. The
1177    *  RS that failed won't be able to finish snyc() for WAL because of lease recovery in WAL.
1178    *  - If RS fails after 3, the region region server who opens the region will pick up the
1179    *  the compaction marker from the WAL and replay it by removing the compaction input files.
1180    *  Failed RS can also attempt to delete those files, but the operation will be idempotent
1181    *
1182    * See HBASE-2231 for details.
1183    *
1184    * @param compaction compaction details obtained from requestCompaction()
1185    * @throws IOException
1186    * @return Storefile we compacted into or null if we failed or opted out early.
1187    */
1188   @Override
1189   public List<StoreFile> compact(CompactionContext compaction,
1190       CompactionThroughputController throughputController) throws IOException {
1191     return compact(compaction, throughputController, null);
1192   }
1193 
1194   @Override
1195   public List<StoreFile> compact(CompactionContext compaction,
1196     CompactionThroughputController throughputController, User user) throws IOException {
1197     assert compaction != null;
1198     List<StoreFile> sfs = null;
1199     CompactionRequest cr = compaction.getRequest();;
1200     try {
1201       // Do all sanity checking in here if we have a valid CompactionRequest
1202       // because we need to clean up after it on the way out in a finally
1203       // block below
1204       long compactionStartTime = EnvironmentEdgeManager.currentTime();
1205       assert compaction.hasSelection();
1206       Collection<StoreFile> filesToCompact = cr.getFiles();
1207       assert !filesToCompact.isEmpty();
1208       synchronized (filesCompacting) {
1209         // sanity check: we're compacting files that this store knows about
1210         // TODO: change this to LOG.error() after more debugging
1211         Preconditions.checkArgument(filesCompacting.containsAll(filesToCompact));
1212       }
1213 
1214       // Ready to go. Have list of files to compact.
1215       LOG.info("Starting compaction of " + filesToCompact.size() + " file(s) in "
1216           + this + " of " + this.getRegionInfo().getRegionNameAsString()
1217           + " into tmpdir=" + fs.getTempDir() + ", totalSize="
1218           + TraditionalBinaryPrefix.long2String(cr.getSize(), "", 1));
1219 
1220       // Commence the compaction.
1221       List<Path> newFiles = compaction.compact(throughputController, user);
1222 
1223       // TODO: get rid of this!
1224       if (!this.conf.getBoolean("hbase.hstore.compaction.complete", true)) {
1225         LOG.warn("hbase.hstore.compaction.complete is set to false");
1226         sfs = new ArrayList<StoreFile>(newFiles.size());
1227         for (Path newFile : newFiles) {
1228           // Create storefile around what we wrote with a reader on it.
1229           StoreFile sf = createStoreFileAndReader(newFile);
1230           sf.closeReader(true);
1231           sfs.add(sf);
1232         }
1233         return sfs;
1234       }
1235       // Do the steps necessary to complete the compaction.
1236       sfs = moveCompatedFilesIntoPlace(cr, newFiles, user);
1237       writeCompactionWalRecord(filesToCompact, sfs);
1238       replaceStoreFiles(filesToCompact, sfs);
1239       if (cr.isMajor()) {
1240         majorCompactedCellsCount += getCompactionProgress().totalCompactingKVs;
1241         majorCompactedCellsSize += getCompactionProgress().totalCompactedSize;
1242       } else {
1243         compactedCellsCount += getCompactionProgress().totalCompactingKVs;
1244         compactedCellsSize += getCompactionProgress().totalCompactedSize;
1245       }
1246       // At this point the store will use new files for all new scanners.
1247       completeCompaction(filesToCompact, true); // Archive old files & update store size.
1248 
1249       logCompactionEndMessage(cr, sfs, compactionStartTime);
1250       return sfs;
1251     } finally {
1252       finishCompactionRequest(cr);
1253     }
1254   }
1255 
1256   private List<StoreFile> moveCompatedFilesIntoPlace(
1257       final CompactionRequest cr, List<Path> newFiles, User user) throws IOException {
1258     List<StoreFile> sfs = new ArrayList<StoreFile>(newFiles.size());
1259     for (Path newFile : newFiles) {
1260       assert newFile != null;
1261       final StoreFile sf = moveFileIntoPlace(newFile);
1262       if (this.getCoprocessorHost() != null) {
1263         final Store thisStore = this;
1264         if (user == null) {
1265           getCoprocessorHost().postCompact(thisStore, sf, cr);
1266         } else {
1267           try {
1268             user.getUGI().doAs(new PrivilegedExceptionAction<Void>() {
1269               @Override
1270               public Void run() throws Exception {
1271                 getCoprocessorHost().postCompact(thisStore, sf, cr);
1272                 return null;
1273               }
1274             });
1275           } catch (InterruptedException ie) {
1276             InterruptedIOException iioe = new InterruptedIOException();
1277             iioe.initCause(ie);
1278             throw iioe;
1279           }
1280         }
1281       }
1282       assert sf != null;
1283       sfs.add(sf);
1284     }
1285     return sfs;
1286   }
1287 
1288   // Package-visible for tests
1289   StoreFile moveFileIntoPlace(final Path newFile) throws IOException {
1290     validateStoreFile(newFile);
1291     // Move the file into the right spot
1292     Path destPath = fs.commitStoreFile(getColumnFamilyName(), newFile);
1293     return createStoreFileAndReader(destPath);
1294   }
1295 
1296   /**
1297    * Writes the compaction WAL record.
1298    * @param filesCompacted Files compacted (input).
1299    * @param newFiles Files from compaction.
1300    */
1301   private void writeCompactionWalRecord(Collection<StoreFile> filesCompacted,
1302       Collection<StoreFile> newFiles) throws IOException {
1303     if (region.getWAL() == null) return;
1304     List<Path> inputPaths = new ArrayList<Path>(filesCompacted.size());
1305     for (StoreFile f : filesCompacted) {
1306       inputPaths.add(f.getPath());
1307     }
1308     List<Path> outputPaths = new ArrayList<Path>(newFiles.size());
1309     for (StoreFile f : newFiles) {
1310       outputPaths.add(f.getPath());
1311     }
1312     HRegionInfo info = this.region.getRegionInfo();
1313     CompactionDescriptor compactionDescriptor = ProtobufUtil.toCompactionDescriptor(info,
1314         family.getName(), inputPaths, outputPaths, fs.getStoreDir(getFamily().getNameAsString()));
1315     WALUtil.writeCompactionMarker(region.getWAL(), this.region.getTableDesc(),
1316         this.region.getRegionInfo(), compactionDescriptor, this.region.getSequenceId());
1317   }
1318 
1319   @VisibleForTesting
1320   void replaceStoreFiles(final Collection<StoreFile> compactedFiles,
1321       final Collection<StoreFile> result) throws IOException {
1322     this.lock.writeLock().lock();
1323     try {
1324       this.storeEngine.getStoreFileManager().addCompactionResults(compactedFiles, result);
1325       filesCompacting.removeAll(compactedFiles); // safe bc: lock.writeLock();
1326     } finally {
1327       this.lock.writeLock().unlock();
1328     }
1329   }
1330 
1331   /**
1332    * Log a very elaborate compaction completion message.
1333    * @param cr Request.
1334    * @param sfs Resulting files.
1335    * @param compactionStartTime Start time.
1336    */
1337   private void logCompactionEndMessage(
1338       CompactionRequest cr, List<StoreFile> sfs, long compactionStartTime) {
1339     long now = EnvironmentEdgeManager.currentTime();
1340     StringBuilder message = new StringBuilder(
1341       "Completed" + (cr.isMajor() ? " major" : "") + " compaction of "
1342       + cr.getFiles().size() + (cr.isAllFiles() ? " (all)" : "") + " file(s) in "
1343       + this + " of " + this.getRegionInfo().getRegionNameAsString() + " into ");
1344     if (sfs.isEmpty()) {
1345       message.append("none, ");
1346     } else {
1347       for (StoreFile sf: sfs) {
1348         message.append(sf.getPath().getName());
1349         message.append("(size=");
1350         message.append(TraditionalBinaryPrefix.long2String(sf.getReader().length(), "", 1));
1351         message.append("), ");
1352       }
1353     }
1354     message.append("total size for store is ")
1355       .append(StringUtils.TraditionalBinaryPrefix.long2String(storeSize, "", 1))
1356       .append(". This selection was in queue for ")
1357       .append(StringUtils.formatTimeDiff(compactionStartTime, cr.getSelectionTime()))
1358       .append(", and took ").append(StringUtils.formatTimeDiff(now, compactionStartTime))
1359       .append(" to execute.");
1360     LOG.info(message.toString());
1361     if (LOG.isTraceEnabled()) {
1362       int fileCount = storeEngine.getStoreFileManager().getStorefileCount();
1363       long resultSize = 0;
1364       for (StoreFile sf : sfs) {
1365         resultSize += sf.getReader().length();
1366       }
1367       String traceMessage = "COMPACTION start,end,size out,files in,files out,store size,"
1368         + "store files [" + compactionStartTime + "," + now + "," + resultSize + ","
1369           + cr.getFiles().size() + "," + sfs.size() + "," +  storeSize + "," + fileCount + "]";
1370       LOG.trace(traceMessage);
1371     }
1372   }
1373 
1374   /**
1375    * Call to complete a compaction. Its for the case where we find in the WAL a compaction
1376    * that was not finished.  We could find one recovering a WAL after a regionserver crash.
1377    * See HBASE-2231.
1378    * @param compaction
1379    */
1380   @Override
1381   public void replayCompactionMarker(CompactionDescriptor compaction,
1382       boolean pickCompactionFiles, boolean removeFiles)
1383       throws IOException {
1384     LOG.debug("Completing compaction from the WAL marker");
1385     List<String> compactionInputs = compaction.getCompactionInputList();
1386     List<String> compactionOutputs = Lists.newArrayList(compaction.getCompactionOutputList());
1387 
1388     // The Compaction Marker is written after the compaction is completed,
1389     // and the files moved into the region/family folder.
1390     //
1391     // If we crash after the entry is written, we may not have removed the
1392     // input files, but the output file is present.
1393     // (The unremoved input files will be removed by this function)
1394     //
1395     // If we scan the directory and the file is not present, it can mean that:
1396     //   - The file was manually removed by the user
1397     //   - The file was removed as consequence of subsequent compaction
1398     // so, we can't do anything with the "compaction output list" because those
1399     // files have already been loaded when opening the region (by virtue of
1400     // being in the store's folder) or they may be missing due to a compaction.
1401 
1402     String familyName = this.getColumnFamilyName();
1403     List<String> inputFiles = new ArrayList<String>(compactionInputs.size());
1404     for (String compactionInput : compactionInputs) {
1405       Path inputPath = fs.getStoreFilePath(familyName, compactionInput);
1406       inputFiles.add(inputPath.getName());
1407     }
1408 
1409     //some of the input files might already be deleted
1410     List<StoreFile> inputStoreFiles = new ArrayList<StoreFile>(compactionInputs.size());
1411     for (StoreFile sf : this.getStorefiles()) {
1412       if (inputFiles.contains(sf.getPath().getName())) {
1413         inputStoreFiles.add(sf);
1414       }
1415     }
1416 
1417     // check whether we need to pick up the new files
1418     List<StoreFile> outputStoreFiles = new ArrayList<StoreFile>(compactionOutputs.size());
1419 
1420     if (pickCompactionFiles) {
1421       for (StoreFile sf : this.getStorefiles()) {
1422         compactionOutputs.remove(sf.getPath().getName());
1423       }
1424       for (String compactionOutput : compactionOutputs) {
1425         StoreFileInfo storeFileInfo = fs.getStoreFileInfo(getColumnFamilyName(), compactionOutput);
1426         StoreFile storeFile = createStoreFileAndReader(storeFileInfo);
1427         outputStoreFiles.add(storeFile);
1428       }
1429     }
1430 
1431     if (!inputStoreFiles.isEmpty() || !outputStoreFiles.isEmpty()) {
1432       LOG.info("Replaying compaction marker, replacing input files: " +
1433           inputStoreFiles + " with output files : " + outputStoreFiles);
1434       this.replaceStoreFiles(inputStoreFiles, outputStoreFiles);
1435       this.completeCompaction(inputStoreFiles, removeFiles);
1436     }
1437   }
1438 
1439   /**
1440    * This method tries to compact N recent files for testing.
1441    * Note that because compacting "recent" files only makes sense for some policies,
1442    * e.g. the default one, it assumes default policy is used. It doesn't use policy,
1443    * but instead makes a compaction candidate list by itself.
1444    * @param N Number of files.
1445    */
1446   public void compactRecentForTestingAssumingDefaultPolicy(int N) throws IOException {
1447     List<StoreFile> filesToCompact;
1448     boolean isMajor;
1449 
1450     this.lock.readLock().lock();
1451     try {
1452       synchronized (filesCompacting) {
1453         filesToCompact = Lists.newArrayList(storeEngine.getStoreFileManager().getStorefiles());
1454         if (!filesCompacting.isEmpty()) {
1455           // exclude all files older than the newest file we're currently
1456           // compacting. this allows us to preserve contiguity (HBASE-2856)
1457           StoreFile last = filesCompacting.get(filesCompacting.size() - 1);
1458           int idx = filesToCompact.indexOf(last);
1459           Preconditions.checkArgument(idx != -1);
1460           filesToCompact.subList(0, idx + 1).clear();
1461         }
1462         int count = filesToCompact.size();
1463         if (N > count) {
1464           throw new RuntimeException("Not enough files");
1465         }
1466 
1467         filesToCompact = filesToCompact.subList(count - N, count);
1468         isMajor = (filesToCompact.size() == storeEngine.getStoreFileManager().getStorefileCount());
1469         filesCompacting.addAll(filesToCompact);
1470         Collections.sort(filesCompacting, StoreFile.Comparators.SEQ_ID);
1471       }
1472     } finally {
1473       this.lock.readLock().unlock();
1474     }
1475 
1476     try {
1477       // Ready to go. Have list of files to compact.
1478       List<Path> newFiles = ((DefaultCompactor)this.storeEngine.getCompactor())
1479           .compactForTesting(filesToCompact, isMajor);
1480       for (Path newFile: newFiles) {
1481         // Move the compaction into place.
1482         StoreFile sf = moveFileIntoPlace(newFile);
1483         if (this.getCoprocessorHost() != null) {
1484           this.getCoprocessorHost().postCompact(this, sf, null);
1485         }
1486         replaceStoreFiles(filesToCompact, Lists.newArrayList(sf));
1487         completeCompaction(filesToCompact, true);
1488       }
1489     } finally {
1490       synchronized (filesCompacting) {
1491         filesCompacting.removeAll(filesToCompact);
1492       }
1493     }
1494   }
1495 
1496   @Override
1497   public boolean hasReferences() {
1498     return StoreUtils.hasReferences(this.storeEngine.getStoreFileManager().getStorefiles());
1499   }
1500 
1501   @Override
1502   public CompactionProgress getCompactionProgress() {
1503     return this.storeEngine.getCompactor().getProgress();
1504   }
1505 
1506   @Override
1507   public boolean isMajorCompaction() throws IOException {
1508     for (StoreFile sf : this.storeEngine.getStoreFileManager().getStorefiles()) {
1509       // TODO: what are these reader checks all over the place?
1510       if (sf.getReader() == null) {
1511         LOG.debug("StoreFile " + sf + " has null Reader");
1512         return false;
1513       }
1514     }
1515     return storeEngine.getCompactionPolicy().isMajorCompaction(
1516         this.storeEngine.getStoreFileManager().getStorefiles());
1517   }
1518 
1519   @Override
1520   public CompactionContext requestCompaction() throws IOException {
1521     return requestCompaction(Store.NO_PRIORITY, null);
1522   }
1523 
1524   @Override
1525   public CompactionContext requestCompaction(int priority, CompactionRequest baseRequest)
1526       throws IOException {
1527     return requestCompaction(priority, baseRequest, null);
1528   }
1529   @Override
1530   public CompactionContext requestCompaction(int priority, final CompactionRequest baseRequest,
1531       User user) throws IOException {
1532     // don't even select for compaction if writes are disabled
1533     if (!this.areWritesEnabled()) {
1534       return null;
1535     }
1536 
1537     // Before we do compaction, try to get rid of unneeded files to simplify things.
1538     removeUnneededFiles();
1539 
1540     final CompactionContext compaction = storeEngine.createCompaction();
1541     CompactionRequest request = null;
1542     this.lock.readLock().lock();
1543     try {
1544       synchronized (filesCompacting) {
1545         final Store thisStore = this;
1546         // First, see if coprocessor would want to override selection.
1547         if (this.getCoprocessorHost() != null) {
1548           final List<StoreFile> candidatesForCoproc = compaction.preSelect(this.filesCompacting);
1549           boolean override = false;
1550           if (user == null) {
1551             override = getCoprocessorHost().preCompactSelection(this, candidatesForCoproc,
1552               baseRequest);
1553           } else {
1554             try {
1555               override = user.getUGI().doAs(new PrivilegedExceptionAction<Boolean>() {
1556                 @Override
1557                 public Boolean run() throws Exception {
1558                   return getCoprocessorHost().preCompactSelection(thisStore, candidatesForCoproc,
1559                     baseRequest);
1560                 }
1561               });
1562             } catch (InterruptedException ie) {
1563               InterruptedIOException iioe = new InterruptedIOException();
1564               iioe.initCause(ie);
1565               throw iioe;
1566             }
1567           }
1568           if (override) {
1569             // Coprocessor is overriding normal file selection.
1570             compaction.forceSelect(new CompactionRequest(candidatesForCoproc));
1571           }
1572         }
1573 
1574         // Normal case - coprocessor is not overriding file selection.
1575         if (!compaction.hasSelection()) {
1576           boolean isUserCompaction = priority == Store.PRIORITY_USER;
1577           boolean mayUseOffPeak = offPeakHours.isOffPeakHour() &&
1578               offPeakCompactionTracker.compareAndSet(false, true);
1579           try {
1580             compaction.select(this.filesCompacting, isUserCompaction,
1581               mayUseOffPeak, forceMajor && filesCompacting.isEmpty());
1582           } catch (IOException e) {
1583             if (mayUseOffPeak) {
1584               offPeakCompactionTracker.set(false);
1585             }
1586             throw e;
1587           }
1588           assert compaction.hasSelection();
1589           if (mayUseOffPeak && !compaction.getRequest().isOffPeak()) {
1590             // Compaction policy doesn't want to take advantage of off-peak.
1591             offPeakCompactionTracker.set(false);
1592           }
1593         }
1594         if (this.getCoprocessorHost() != null) {
1595           if (user == null) {
1596             this.getCoprocessorHost().postCompactSelection(
1597               this, ImmutableList.copyOf(compaction.getRequest().getFiles()), baseRequest);
1598           } else {
1599             try {
1600               user.getUGI().doAs(new PrivilegedExceptionAction<Void>() {
1601                 @Override
1602                 public Void run() throws Exception {
1603                   getCoprocessorHost().postCompactSelection(
1604                     thisStore,ImmutableList.copyOf(compaction.getRequest().getFiles()),baseRequest);
1605                   return null;
1606                 }
1607               });
1608             } catch (InterruptedException ie) {
1609               InterruptedIOException iioe = new InterruptedIOException();
1610               iioe.initCause(ie);
1611               throw iioe;
1612             }
1613           }
1614         }
1615 
1616         // Selected files; see if we have a compaction with some custom base request.
1617         if (baseRequest != null) {
1618           // Update the request with what the system thinks the request should be;
1619           // its up to the request if it wants to listen.
1620           compaction.forceSelect(
1621               baseRequest.combineWith(compaction.getRequest()));
1622         }
1623         // Finally, we have the resulting files list. Check if we have any files at all.
1624         request = compaction.getRequest();
1625         final Collection<StoreFile> selectedFiles = request.getFiles();
1626         if (selectedFiles.isEmpty()) {
1627           return null;
1628         }
1629 
1630         addToCompactingFiles(selectedFiles);
1631 
1632         // If we're enqueuing a major, clear the force flag.
1633         this.forceMajor = this.forceMajor && !request.isMajor();
1634 
1635         // Set common request properties.
1636         // Set priority, either override value supplied by caller or from store.
1637         request.setPriority((priority != Store.NO_PRIORITY) ? priority : getCompactPriority());
1638         request.setDescription(getRegionInfo().getRegionNameAsString(), getColumnFamilyName());
1639       }
1640     } finally {
1641       this.lock.readLock().unlock();
1642     }
1643 
1644     LOG.debug(getRegionInfo().getEncodedName() + " - "  + getColumnFamilyName()
1645         + ": Initiating " + (request.isMajor() ? "major" : "minor") + " compaction"
1646         + (request.isAllFiles() ? " (all files)" : ""));
1647     this.region.reportCompactionRequestStart(request.isMajor());
1648     return compaction;
1649   }
1650 
1651   /** Adds the files to compacting files. filesCompacting must be locked. */
1652   private void addToCompactingFiles(final Collection<StoreFile> filesToAdd) {
1653     if (filesToAdd == null) return;
1654     // Check that we do not try to compact the same StoreFile twice.
1655     if (!Collections.disjoint(filesCompacting, filesToAdd)) {
1656       Preconditions.checkArgument(false, "%s overlaps with %s", filesToAdd, filesCompacting);
1657     }
1658     filesCompacting.addAll(filesToAdd);
1659     Collections.sort(filesCompacting, StoreFile.Comparators.SEQ_ID);
1660   }
1661 
1662   private void removeUnneededFiles() throws IOException {
1663     if (!conf.getBoolean("hbase.store.delete.expired.storefile", true)) return;
1664     if (getFamily().getMinVersions() > 0) {
1665       LOG.debug("Skipping expired store file removal due to min version being " +
1666           getFamily().getMinVersions());
1667       return;
1668     }
1669     this.lock.readLock().lock();
1670     Collection<StoreFile> delSfs = null;
1671     try {
1672       synchronized (filesCompacting) {
1673         long cfTtl = getStoreFileTtl();
1674         if (cfTtl != Long.MAX_VALUE) {
1675           delSfs = storeEngine.getStoreFileManager().getUnneededFiles(
1676               EnvironmentEdgeManager.currentTime() - cfTtl, filesCompacting);
1677           addToCompactingFiles(delSfs);
1678         }
1679       }
1680     } finally {
1681       this.lock.readLock().unlock();
1682     }
1683     if (delSfs == null || delSfs.isEmpty()) return;
1684 
1685     Collection<StoreFile> newFiles = new ArrayList<StoreFile>(); // No new files.
1686     writeCompactionWalRecord(delSfs, newFiles);
1687     replaceStoreFiles(delSfs, newFiles);
1688     completeCompaction(delSfs);
1689     LOG.info("Completed removal of " + delSfs.size() + " unnecessary (expired) file(s) in "
1690         + this + " of " + this.getRegionInfo().getRegionNameAsString()
1691         + "; total size for store is " + TraditionalBinaryPrefix.long2String(storeSize, "", 1));
1692   }
1693 
1694   @Override
1695   public void cancelRequestedCompaction(CompactionContext compaction) {
1696     finishCompactionRequest(compaction.getRequest());
1697   }
1698 
1699   private void finishCompactionRequest(CompactionRequest cr) {
1700     this.region.reportCompactionRequestEnd(cr.isMajor(), cr.getFiles().size(), cr.getSize());
1701     if (cr.isOffPeak()) {
1702       offPeakCompactionTracker.set(false);
1703       cr.setOffPeak(false);
1704     }
1705     synchronized (filesCompacting) {
1706       filesCompacting.removeAll(cr.getFiles());
1707     }
1708   }
1709 
1710   /**
1711    * Validates a store file by opening and closing it. In HFileV2 this should
1712    * not be an expensive operation.
1713    *
1714    * @param path the path to the store file
1715    */
1716   private void validateStoreFile(Path path)
1717       throws IOException {
1718     StoreFile storeFile = null;
1719     try {
1720       storeFile = createStoreFileAndReader(path);
1721     } catch (IOException e) {
1722       LOG.error("Failed to open store file : " + path
1723           + ", keeping it in tmp location", e);
1724       throw e;
1725     } finally {
1726       if (storeFile != null) {
1727         storeFile.closeReader(false);
1728       }
1729     }
1730   }
1731 
1732   /**
1733    * <p>It works by processing a compaction that's been written to disk.
1734    *
1735    * <p>It is usually invoked at the end of a compaction, but might also be
1736    * invoked at HStore startup, if the prior execution died midway through.
1737    *
1738    * <p>Moving the compacted TreeMap into place means:
1739    * <pre>
1740    * 1) Unload all replaced StoreFile, close and collect list to delete.
1741    * 2) Compute new store size
1742    * </pre>
1743    *
1744    * @param compactedFiles list of files that were compacted
1745    */
1746   @VisibleForTesting
1747   protected void completeCompaction(final Collection<StoreFile> compactedFiles)
1748     throws IOException {
1749     completeCompaction(compactedFiles, true);
1750   }
1751 
1752 
1753   /**
1754    * <p>It works by processing a compaction that's been written to disk.
1755    *
1756    * <p>It is usually invoked at the end of a compaction, but might also be
1757    * invoked at HStore startup, if the prior execution died midway through.
1758    *
1759    * <p>Moving the compacted TreeMap into place means:
1760    * <pre>
1761    * 1) Unload all replaced StoreFile, close and collect list to delete.
1762    * 2) Compute new store size
1763    * </pre>
1764    *
1765    * @param compactedFiles list of files that were compacted
1766    */
1767   @VisibleForTesting
1768   protected void completeCompaction(final Collection<StoreFile> compactedFiles, boolean removeFiles)
1769       throws IOException {
1770     try {
1771       // Do not delete old store files until we have sent out notification of
1772       // change in case old files are still being accessed by outstanding scanners.
1773       // Don't do this under writeLock; see HBASE-4485 for a possible deadlock
1774       // scenario that could have happened if continue to hold the lock.
1775       notifyChangedReadersObservers();
1776       // At this point the store will use new files for all scanners.
1777 
1778       // let the archive util decide if we should archive or delete the files
1779       LOG.debug("Removing store files after compaction...");
1780       for (StoreFile compactedFile : compactedFiles) {
1781         compactedFile.closeReader(true);
1782       }
1783       if (removeFiles) {
1784         this.fs.removeStoreFiles(this.getColumnFamilyName(), compactedFiles);
1785       }
1786     } catch (IOException e) {
1787       e = RemoteExceptionHandler.checkIOException(e);
1788       LOG.error("Failed removing compacted files in " + this +
1789         ". Files we were trying to remove are " + compactedFiles.toString() +
1790         "; some of them may have been already removed", e);
1791     }
1792 
1793     // 4. Compute new store size
1794     this.storeSize = 0L;
1795     this.totalUncompressedBytes = 0L;
1796     for (StoreFile hsf : this.storeEngine.getStoreFileManager().getStorefiles()) {
1797       StoreFile.Reader r = hsf.getReader();
1798       if (r == null) {
1799         LOG.warn("StoreFile " + hsf + " has a null Reader");
1800         continue;
1801       }
1802       this.storeSize += r.length();
1803       this.totalUncompressedBytes += r.getTotalUncompressedBytes();
1804     }
1805   }
1806 
1807   /*
1808    * @param wantedVersions How many versions were asked for.
1809    * @return wantedVersions or this families' {@link HConstants#VERSIONS}.
1810    */
1811   int versionsToReturn(final int wantedVersions) {
1812     if (wantedVersions <= 0) {
1813       throw new IllegalArgumentException("Number of versions must be > 0");
1814     }
1815     // Make sure we do not return more than maximum versions for this store.
1816     int maxVersions = this.family.getMaxVersions();
1817     return wantedVersions > maxVersions ? maxVersions: wantedVersions;
1818   }
1819 
1820   /**
1821    * @param cell
1822    * @param oldestTimestamp
1823    * @return true if the cell is expired
1824    */
1825   static boolean isCellTTLExpired(final Cell cell, final long oldestTimestamp, final long now) {
1826     // Do not create an Iterator or Tag objects unless the cell actually has tags.
1827     // TODO: This check for tags is really expensive. We decode an int for key and value. Costs.
1828     if (cell.getTagsLength() > 0) {
1829       // Look for a TTL tag first. Use it instead of the family setting if
1830       // found. If a cell has multiple TTLs, resolve the conflict by using the
1831       // first tag encountered.
1832       Iterator<Tag> i = CellUtil.tagsIterator(cell.getTagsArray(), cell.getTagsOffset(),
1833         cell.getTagsLength());
1834       while (i.hasNext()) {
1835         Tag t = i.next();
1836         if (TagType.TTL_TAG_TYPE == t.getType()) {
1837           // Unlike in schema cell TTLs are stored in milliseconds, no need
1838           // to convert
1839           long ts = cell.getTimestamp();
1840           assert t.getTagLength() == Bytes.SIZEOF_LONG;
1841           long ttl = Bytes.toLong(t.getBuffer(), t.getTagOffset(), t.getTagLength());
1842           if (ts + ttl < now) {
1843             return true;
1844           }
1845           // Per cell TTLs cannot extend lifetime beyond family settings, so
1846           // fall through to check that
1847           break;
1848         }
1849       }
1850     }
1851     return false;
1852   }
1853 
1854   @Override
1855   public Cell getRowKeyAtOrBefore(final byte[] row) throws IOException {
1856     // If minVersions is set, we will not ignore expired KVs.
1857     // As we're only looking for the latest matches, that should be OK.
1858     // With minVersions > 0 we guarantee that any KV that has any version
1859     // at all (expired or not) has at least one version that will not expire.
1860     // Note that this method used to take a KeyValue as arguments. KeyValue
1861     // can be back-dated, a row key cannot.
1862     long ttlToUse = scanInfo.getMinVersions() > 0 ? Long.MAX_VALUE : this.scanInfo.getTtl();
1863 
1864     KeyValue kv = new KeyValue(row, HConstants.LATEST_TIMESTAMP);
1865 
1866     GetClosestRowBeforeTracker state = new GetClosestRowBeforeTracker(
1867       this.comparator, kv, ttlToUse, this.getRegionInfo().isMetaRegion());
1868     this.lock.readLock().lock();
1869     try {
1870       // First go to the memstore.  Pick up deletes and candidates.
1871       this.memstore.getRowKeyAtOrBefore(state);
1872       // Check if match, if we got a candidate on the asked for 'kv' row.
1873       // Process each relevant store file. Run through from newest to oldest.
1874       Iterator<StoreFile> sfIterator = this.storeEngine.getStoreFileManager()
1875           .getCandidateFilesForRowKeyBefore(state.getTargetKey());
1876       while (sfIterator.hasNext()) {
1877         StoreFile sf = sfIterator.next();
1878         sfIterator.remove(); // Remove sf from iterator.
1879         boolean haveNewCandidate = rowAtOrBeforeFromStoreFile(sf, state);
1880         Cell candidate = state.getCandidate();
1881         // we have an optimization here which stops the search if we find exact match.
1882         if (candidate != null && CellUtil.matchingRow(candidate, row)) {
1883           return candidate;
1884         }
1885         if (haveNewCandidate) {
1886           sfIterator = this.storeEngine.getStoreFileManager().updateCandidateFilesForRowKeyBefore(
1887               sfIterator, state.getTargetKey(), candidate);
1888         }
1889       }
1890       return state.getCandidate();
1891     } finally {
1892       this.lock.readLock().unlock();
1893     }
1894   }
1895 
1896   /*
1897    * Check an individual MapFile for the row at or before a given row.
1898    * @param f
1899    * @param state
1900    * @throws IOException
1901    * @return True iff the candidate has been updated in the state.
1902    */
1903   private boolean rowAtOrBeforeFromStoreFile(final StoreFile f,
1904                                           final GetClosestRowBeforeTracker state)
1905       throws IOException {
1906     StoreFile.Reader r = f.getReader();
1907     if (r == null) {
1908       LOG.warn("StoreFile " + f + " has a null Reader");
1909       return false;
1910     }
1911     if (r.getEntries() == 0) {
1912       LOG.warn("StoreFile " + f + " is a empty store file");
1913       return false;
1914     }
1915     // TODO: Cache these keys rather than make each time?
1916     byte [] fk = r.getFirstKey();
1917     if (fk == null) return false;
1918     KeyValue firstKV = KeyValue.createKeyValueFromKey(fk, 0, fk.length);
1919     byte [] lk = r.getLastKey();
1920     KeyValue lastKV = KeyValue.createKeyValueFromKey(lk, 0, lk.length);
1921     KeyValue firstOnRow = state.getTargetKey();
1922     if (this.comparator.compareRows(lastKV, firstOnRow) < 0) {
1923       // If last key in file is not of the target table, no candidates in this
1924       // file.  Return.
1925       if (!state.isTargetTable(lastKV)) return false;
1926       // If the row we're looking for is past the end of file, set search key to
1927       // last key. TODO: Cache last and first key rather than make each time.
1928       firstOnRow = new KeyValue(lastKV.getRow(), HConstants.LATEST_TIMESTAMP);
1929     }
1930     // Get a scanner that caches blocks and that uses pread.
1931     HFileScanner scanner = r.getScanner(true, true, false);
1932     // Seek scanner.  If can't seek it, return.
1933     if (!seekToScanner(scanner, firstOnRow, firstKV)) return false;
1934     // If we found candidate on firstOnRow, just return. THIS WILL NEVER HAPPEN!
1935     // Unlikely that there'll be an instance of actual first row in table.
1936     if (walkForwardInSingleRow(scanner, firstOnRow, state)) return true;
1937     // If here, need to start backing up.
1938     while (scanner.seekBefore(firstOnRow.getBuffer(), firstOnRow.getKeyOffset(),
1939        firstOnRow.getKeyLength())) {
1940       Cell kv = scanner.getKeyValue();
1941       if (!state.isTargetTable(kv)) break;
1942       if (!state.isBetterCandidate(kv)) break;
1943       // Make new first on row.
1944       firstOnRow = new KeyValue(kv.getRow(), HConstants.LATEST_TIMESTAMP);
1945       // Seek scanner.  If can't seek it, break.
1946       if (!seekToScanner(scanner, firstOnRow, firstKV)) return false;
1947       // If we find something, break;
1948       if (walkForwardInSingleRow(scanner, firstOnRow, state)) return true;
1949     }
1950     return false;
1951   }
1952 
1953   /*
1954    * Seek the file scanner to firstOnRow or first entry in file.
1955    * @param scanner
1956    * @param firstOnRow
1957    * @param firstKV
1958    * @return True if we successfully seeked scanner.
1959    * @throws IOException
1960    */
1961   private boolean seekToScanner(final HFileScanner scanner,
1962                                 final KeyValue firstOnRow,
1963                                 final KeyValue firstKV)
1964       throws IOException {
1965     KeyValue kv = firstOnRow;
1966     // If firstOnRow < firstKV, set to firstKV
1967     if (this.comparator.compareRows(firstKV, firstOnRow) == 0) kv = firstKV;
1968     int result = scanner.seekTo(kv);
1969     return result != -1;
1970   }
1971 
1972   /*
1973    * When we come in here, we are probably at the kv just before we break into
1974    * the row that firstOnRow is on.  Usually need to increment one time to get
1975    * on to the row we are interested in.
1976    * @param scanner
1977    * @param firstOnRow
1978    * @param state
1979    * @return True we found a candidate.
1980    * @throws IOException
1981    */
1982   private boolean walkForwardInSingleRow(final HFileScanner scanner,
1983                                          final KeyValue firstOnRow,
1984                                          final GetClosestRowBeforeTracker state)
1985       throws IOException {
1986     boolean foundCandidate = false;
1987     do {
1988       Cell kv = scanner.getKeyValue();
1989       // If we are not in the row, skip.
1990       if (this.comparator.compareRows(kv, firstOnRow) < 0) continue;
1991       // Did we go beyond the target row? If so break.
1992       if (state.isTooFar(kv, firstOnRow)) break;
1993       if (state.isExpired(kv)) {
1994         continue;
1995       }
1996       // If we added something, this row is a contender. break.
1997       if (state.handle(kv)) {
1998         foundCandidate = true;
1999         break;
2000       }
2001     } while(scanner.next());
2002     return foundCandidate;
2003   }
2004 
2005   @Override
2006   public boolean canSplit() {
2007     this.lock.readLock().lock();
2008     try {
2009       // Not split-able if we find a reference store file present in the store.
2010       boolean result = !hasReferences();
2011       if (!result && LOG.isDebugEnabled()) {
2012         LOG.debug("Cannot split region due to reference files being there");
2013       }
2014       return result;
2015     } finally {
2016       this.lock.readLock().unlock();
2017     }
2018   }
2019 
2020   @Override
2021   public byte[] getSplitPoint() {
2022     this.lock.readLock().lock();
2023     try {
2024       // Should already be enforced by the split policy!
2025       assert !this.getRegionInfo().isMetaRegion();
2026       // Not split-able if we find a reference store file present in the store.
2027       if (hasReferences()) {
2028         return null;
2029       }
2030       return this.storeEngine.getStoreFileManager().getSplitPoint();
2031     } catch(IOException e) {
2032       LOG.warn("Failed getting store size for " + this, e);
2033     } finally {
2034       this.lock.readLock().unlock();
2035     }
2036     return null;
2037   }
2038 
2039   @Override
2040   public long getLastCompactSize() {
2041     return this.lastCompactSize;
2042   }
2043 
2044   @Override
2045   public long getSize() {
2046     return storeSize;
2047   }
2048 
2049   @Override
2050   public void triggerMajorCompaction() {
2051     this.forceMajor = true;
2052   }
2053 
2054 
2055   //////////////////////////////////////////////////////////////////////////////
2056   // File administration
2057   //////////////////////////////////////////////////////////////////////////////
2058 
2059   @Override
2060   public KeyValueScanner getScanner(Scan scan,
2061       final NavigableSet<byte []> targetCols, long readPt) throws IOException {
2062     lock.readLock().lock();
2063     try {
2064       KeyValueScanner scanner = null;
2065       if (this.getCoprocessorHost() != null) {
2066         scanner = this.getCoprocessorHost().preStoreScannerOpen(this, scan, targetCols);
2067       }
2068       if (scanner == null) {
2069         scanner = scan.isReversed() ? new ReversedStoreScanner(this,
2070             getScanInfo(), scan, targetCols, readPt) : new StoreScanner(this,
2071             getScanInfo(), scan, targetCols, readPt);
2072       }
2073       return scanner;
2074     } finally {
2075       lock.readLock().unlock();
2076     }
2077   }
2078 
2079   @Override
2080   public String toString() {
2081     return this.getColumnFamilyName();
2082   }
2083 
2084   @Override
2085   public int getStorefilesCount() {
2086     return this.storeEngine.getStoreFileManager().getStorefileCount();
2087   }
2088 
2089   @Override
2090   public long getStoreSizeUncompressed() {
2091     return this.totalUncompressedBytes;
2092   }
2093 
2094   @Override
2095   public long getStorefilesSize() {
2096     long size = 0;
2097     for (StoreFile s: this.storeEngine.getStoreFileManager().getStorefiles()) {
2098       StoreFile.Reader r = s.getReader();
2099       if (r == null) {
2100         LOG.warn("StoreFile " + s + " has a null Reader");
2101         continue;
2102       }
2103       size += r.length();
2104     }
2105     return size;
2106   }
2107 
2108   @Override
2109   public long getStorefilesIndexSize() {
2110     long size = 0;
2111     for (StoreFile s: this.storeEngine.getStoreFileManager().getStorefiles()) {
2112       StoreFile.Reader r = s.getReader();
2113       if (r == null) {
2114         LOG.warn("StoreFile " + s + " has a null Reader");
2115         continue;
2116       }
2117       size += r.indexSize();
2118     }
2119     return size;
2120   }
2121 
2122   @Override
2123   public long getTotalStaticIndexSize() {
2124     long size = 0;
2125     for (StoreFile s : this.storeEngine.getStoreFileManager().getStorefiles()) {
2126       StoreFile.Reader r = s.getReader();
2127       if (r == null) {
2128         continue;
2129       }
2130       size += r.getUncompressedDataIndexSize();
2131     }
2132     return size;
2133   }
2134 
2135   @Override
2136   public long getTotalStaticBloomSize() {
2137     long size = 0;
2138     for (StoreFile s : this.storeEngine.getStoreFileManager().getStorefiles()) {
2139       StoreFile.Reader r = s.getReader();
2140       if (r == null) {
2141         continue;
2142       }
2143       size += r.getTotalBloomSize();
2144     }
2145     return size;
2146   }
2147 
2148   @Override
2149   public long getMemStoreSize() {
2150     return this.memstore.size();
2151   }
2152 
2153   @Override
2154   public int getCompactPriority() {
2155     int priority = this.storeEngine.getStoreFileManager().getStoreCompactionPriority();
2156     if (priority == PRIORITY_USER) {
2157       LOG.warn("Compaction priority is USER despite there being no user compaction");
2158     }
2159     return priority;
2160   }
2161 
2162   @Override
2163   public boolean throttleCompaction(long compactionSize) {
2164     return storeEngine.getCompactionPolicy().throttleCompaction(compactionSize);
2165   }
2166 
2167   public HRegion getHRegion() {
2168     return this.region;
2169   }
2170 
2171   @Override
2172   public RegionCoprocessorHost getCoprocessorHost() {
2173     return this.region.getCoprocessorHost();
2174   }
2175 
2176   @Override
2177   public HRegionInfo getRegionInfo() {
2178     return this.fs.getRegionInfo();
2179   }
2180 
2181   @Override
2182   public boolean areWritesEnabled() {
2183     return this.region.areWritesEnabled();
2184   }
2185 
2186   @Override
2187   public long getSmallestReadPoint() {
2188     return this.region.getSmallestReadPoint();
2189   }
2190 
2191   /**
2192    * Used in tests. TODO: Remove
2193    *
2194    * Updates the value for the given row/family/qualifier. This function will always be seen as
2195    * atomic by other readers because it only puts a single KV to memstore. Thus no read/write
2196    * control necessary.
2197    * @param row row to update
2198    * @param f family to update
2199    * @param qualifier qualifier to update
2200    * @param newValue the new value to set into memstore
2201    * @return memstore size delta
2202    * @throws IOException
2203    */
2204   public long updateColumnValue(byte [] row, byte [] f,
2205                                 byte [] qualifier, long newValue)
2206       throws IOException {
2207 
2208     this.lock.readLock().lock();
2209     try {
2210       long now = EnvironmentEdgeManager.currentTime();
2211 
2212       return this.memstore.updateColumnValue(row,
2213           f,
2214           qualifier,
2215           newValue,
2216           now);
2217 
2218     } finally {
2219       this.lock.readLock().unlock();
2220     }
2221   }
2222 
2223   @Override
2224   public long upsert(Iterable<Cell> cells, long readpoint) throws IOException {
2225     this.lock.readLock().lock();
2226     try {
2227       return this.memstore.upsert(cells, readpoint);
2228     } finally {
2229       this.lock.readLock().unlock();
2230     }
2231   }
2232 
2233   @Override
2234   public StoreFlushContext createFlushContext(long cacheFlushId) {
2235     return new StoreFlusherImpl(cacheFlushId);
2236   }
2237 
2238   private class StoreFlusherImpl implements StoreFlushContext {
2239 
2240     private long cacheFlushSeqNum;
2241     private MemStoreSnapshot snapshot;
2242     private List<Path> tempFiles;
2243     private List<Path> committedFiles;
2244     private long cacheFlushCount;
2245     private long cacheFlushSize;
2246 
2247     private StoreFlusherImpl(long cacheFlushSeqNum) {
2248       this.cacheFlushSeqNum = cacheFlushSeqNum;
2249     }
2250 
2251     /**
2252      * This is not thread safe. The caller should have a lock on the region or the store.
2253      * If necessary, the lock can be added with the patch provided in HBASE-10087
2254      */
2255     @Override
2256     public void prepare() {
2257       this.snapshot = memstore.snapshot();
2258       this.cacheFlushCount = snapshot.getCellsCount();
2259       this.cacheFlushSize = snapshot.getSize();
2260       committedFiles = new ArrayList<Path>(1);
2261     }
2262 
2263     @Override
2264     public void flushCache(MonitoredTask status) throws IOException {
2265       tempFiles = HStore.this.flushCache(cacheFlushSeqNum, snapshot, status);
2266     }
2267 
2268     @Override
2269     public boolean commit(MonitoredTask status) throws IOException {
2270       if (this.tempFiles == null || this.tempFiles.isEmpty()) {
2271         return false;
2272       }
2273       List<StoreFile> storeFiles = new ArrayList<StoreFile>(this.tempFiles.size());
2274       for (Path storeFilePath : tempFiles) {
2275         try {
2276           storeFiles.add(HStore.this.commitFile(storeFilePath, cacheFlushSeqNum, status));
2277         } catch (IOException ex) {
2278           LOG.error("Failed to commit store file " + storeFilePath, ex);
2279           // Try to delete the files we have committed before.
2280           for (StoreFile sf : storeFiles) {
2281             Path pathToDelete = sf.getPath();
2282             try {
2283               sf.deleteReader();
2284             } catch (IOException deleteEx) {
2285               LOG.fatal("Failed to delete store file we committed, halting " + pathToDelete, ex);
2286               Runtime.getRuntime().halt(1);
2287             }
2288           }
2289           throw new IOException("Failed to commit the flush", ex);
2290         }
2291       }
2292 
2293       for (StoreFile sf : storeFiles) {
2294         if (HStore.this.getCoprocessorHost() != null) {
2295           HStore.this.getCoprocessorHost().postFlush(HStore.this, sf);
2296         }
2297         committedFiles.add(sf.getPath());
2298       }
2299 
2300       HStore.this.flushedCellsCount += cacheFlushCount;
2301       HStore.this.flushedCellsSize += cacheFlushSize;
2302 
2303       // Add new file to store files.  Clear snapshot too while we have the Store write lock.
2304       return HStore.this.updateStorefiles(storeFiles, snapshot.getId());
2305     }
2306 
2307     @Override
2308     public List<Path> getCommittedFiles() {
2309       return committedFiles;
2310     }
2311 
2312     /**
2313      * Similar to commit, but called in secondary region replicas for replaying the
2314      * flush cache from primary region. Adds the new files to the store, and drops the
2315      * snapshot depending on dropMemstoreSnapshot argument.
2316      * @param fileNames names of the flushed files
2317      * @param dropMemstoreSnapshot whether to drop the prepared memstore snapshot
2318      * @throws IOException
2319      */
2320     @Override
2321     public void replayFlush(List<String> fileNames, boolean dropMemstoreSnapshot)
2322         throws IOException {
2323       List<StoreFile> storeFiles = new ArrayList<StoreFile>(fileNames.size());
2324       for (String file : fileNames) {
2325         // open the file as a store file (hfile link, etc)
2326         StoreFileInfo storeFileInfo = fs.getStoreFileInfo(getColumnFamilyName(), file);
2327         StoreFile storeFile = createStoreFileAndReader(storeFileInfo);
2328         storeFiles.add(storeFile);
2329         HStore.this.storeSize += storeFile.getReader().length();
2330         HStore.this.totalUncompressedBytes += storeFile.getReader().getTotalUncompressedBytes();
2331         if (LOG.isInfoEnabled()) {
2332           LOG.info("Region: " + HStore.this.getRegionInfo().getEncodedName() +
2333             " added " + storeFile + ", entries=" + storeFile.getReader().getEntries() +
2334             ", sequenceid=" +  + storeFile.getReader().getSequenceID() +
2335             ", filesize=" + StringUtils.humanReadableInt(storeFile.getReader().length()));
2336         }
2337       }
2338 
2339       long snapshotId = -1; // -1 means do not drop
2340       if (dropMemstoreSnapshot && snapshot != null) {
2341         snapshotId = snapshot.getId();
2342       }
2343       HStore.this.updateStorefiles(storeFiles, snapshotId);
2344     }
2345 
2346     /**
2347      * Abort the snapshot preparation. Drops the snapshot if any.
2348      * @throws IOException
2349      */
2350     @Override
2351     public void abort() throws IOException {
2352       if (snapshot == null) {
2353         return;
2354       }
2355       HStore.this.updateStorefiles(new ArrayList<StoreFile>(0), snapshot.getId());
2356     }
2357   }
2358 
2359   @Override
2360   public boolean needsCompaction() {
2361     return this.storeEngine.needsCompaction(this.filesCompacting);
2362   }
2363 
2364   @Override
2365   public CacheConfig getCacheConfig() {
2366     return this.cacheConf;
2367   }
2368 
2369   public static final long FIXED_OVERHEAD =
2370       ClassSize.align(ClassSize.OBJECT + (16 * ClassSize.REFERENCE) + (10 * Bytes.SIZEOF_LONG)
2371               + (5 * Bytes.SIZEOF_INT) + (2 * Bytes.SIZEOF_BOOLEAN));
2372 
2373   public static final long DEEP_OVERHEAD = ClassSize.align(FIXED_OVERHEAD
2374       + ClassSize.OBJECT + ClassSize.REENTRANT_LOCK
2375       + ClassSize.CONCURRENT_SKIPLISTMAP
2376       + ClassSize.CONCURRENT_SKIPLISTMAP_ENTRY + ClassSize.OBJECT
2377       + ScanInfo.FIXED_OVERHEAD);
2378 
2379   @Override
2380   public long heapSize() {
2381     return DEEP_OVERHEAD + this.memstore.heapSize();
2382   }
2383 
2384   @Override
2385   public KeyValue.KVComparator getComparator() {
2386     return comparator;
2387   }
2388 
2389   @Override
2390   public ScanInfo getScanInfo() {
2391     return scanInfo;
2392   }
2393 
2394   /**
2395    * Set scan info, used by test
2396    * @param scanInfo new scan info to use for test
2397    */
2398   void setScanInfo(ScanInfo scanInfo) {
2399     this.scanInfo = scanInfo;
2400   }
2401 
2402   @Override
2403   public boolean hasTooManyStoreFiles() {
2404     return getStorefilesCount() > this.blockingFileCount;
2405   }
2406 
2407   @Override
2408   public long getFlushedCellsCount() {
2409     return flushedCellsCount;
2410   }
2411 
2412   @Override
2413   public long getFlushedCellsSize() {
2414     return flushedCellsSize;
2415   }
2416 
2417   @Override
2418   public long getCompactedCellsCount() {
2419     return compactedCellsCount;
2420   }
2421 
2422   @Override
2423   public long getCompactedCellsSize() {
2424     return compactedCellsSize;
2425   }
2426 
2427   @Override
2428   public long getMajorCompactedCellsCount() {
2429     return majorCompactedCellsCount;
2430   }
2431 
2432   @Override
2433   public long getMajorCompactedCellsSize() {
2434     return majorCompactedCellsSize;
2435   }
2436 
2437   /**
2438    * Returns the StoreEngine that is backing this concrete implementation of Store.
2439    * @return Returns the {@link StoreEngine} object used internally inside this HStore object.
2440    */
2441   @VisibleForTesting
2442   public StoreEngine<?, ?, ?, ?> getStoreEngine() {
2443     return this.storeEngine;
2444   }
2445 
2446   protected OffPeakHours getOffPeakHours() {
2447     return this.offPeakHours;
2448   }
2449 
2450   /**
2451    * {@inheritDoc}
2452    */
2453   @Override
2454   public void onConfigurationChange(Configuration conf) {
2455     this.conf = new CompoundConfiguration()
2456             .add(conf)
2457             .addWritableMap(family.getValues());
2458     this.storeEngine.compactionPolicy.setConf(conf);
2459     this.offPeakHours = OffPeakHours.getInstance(conf);
2460   }
2461 
2462   /**
2463    * {@inheritDoc}
2464    */
2465   @Override
2466   public void registerChildren(ConfigurationManager manager) {
2467     // No children to register
2468   }
2469 
2470   /**
2471    * {@inheritDoc}
2472    */
2473   @Override
2474   public void deregisterChildren(ConfigurationManager manager) {
2475     // No children to deregister
2476   }
2477 
2478   @Override
2479   public double getCompactionPressure() {
2480     return storeEngine.getStoreFileManager().getCompactionPressure();
2481   }
2482 }