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.util.ArrayList;
23  import java.util.Collection;
24  import java.util.Collections;
25  import java.util.Iterator;
26  import java.util.List;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.hadoop.conf.Configuration;
31  import org.apache.hadoop.hbase.Cell;
32  import org.apache.hadoop.hbase.KeyValue;
33  import org.apache.hadoop.hbase.KeyValue.KVComparator;
34  import org.apache.hadoop.hbase.classification.InterfaceAudience;
35  import org.apache.hadoop.hbase.regionserver.compactions.CompactionConfiguration;
36  
37  import com.google.common.collect.ImmutableCollection;
38  import com.google.common.collect.ImmutableList;
39  import com.google.common.collect.Lists;
40  
41  /**
42   * Default implementation of StoreFileManager. Not thread-safe.
43   */
44  @InterfaceAudience.Private
45  class DefaultStoreFileManager implements StoreFileManager {
46    static final Log LOG = LogFactory.getLog(DefaultStoreFileManager.class);
47  
48    private final KVComparator kvComparator;
49    private final CompactionConfiguration comConf;
50    private final int blockingFileCount;
51  
52    /**
53     * List of store files inside this store. This is an immutable list that
54     * is atomically replaced when its contents change.
55     */
56    private volatile ImmutableList<StoreFile> storefiles = null;
57  
58    public DefaultStoreFileManager(KVComparator kvComparator, Configuration conf,
59        CompactionConfiguration comConf) {
60      this.kvComparator = kvComparator;
61      this.comConf = comConf;
62      this.blockingFileCount =
63          conf.getInt(HStore.BLOCKING_STOREFILES_KEY, HStore.DEFAULT_BLOCKING_STOREFILE_COUNT);
64    }
65  
66    @Override
67    public void loadFiles(List<StoreFile> storeFiles) {
68      sortAndSetStoreFiles(storeFiles);
69    }
70  
71    @Override
72    public final Collection<StoreFile> getStorefiles() {
73      return storefiles;
74    }
75  
76    @Override
77    public void insertNewFiles(Collection<StoreFile> sfs) throws IOException {
78      ArrayList<StoreFile> newFiles = new ArrayList<StoreFile>(storefiles);
79      newFiles.addAll(sfs);
80      sortAndSetStoreFiles(newFiles);
81    }
82  
83    @Override
84    public ImmutableCollection<StoreFile> clearFiles() {
85      ImmutableList<StoreFile> result = storefiles;
86      storefiles = ImmutableList.of();
87      return result;
88    }
89  
90    @Override
91    public final int getStorefileCount() {
92      return storefiles.size();
93    }
94  
95    @Override
96    public void addCompactionResults(
97      Collection<StoreFile> compactedFiles, Collection<StoreFile> results) {
98      ArrayList<StoreFile> newStoreFiles = Lists.newArrayList(storefiles);
99      newStoreFiles.removeAll(compactedFiles);
100     if (!results.isEmpty()) {
101       newStoreFiles.addAll(results);
102     }
103     sortAndSetStoreFiles(newStoreFiles);
104   }
105 
106   @Override
107   public final Iterator<StoreFile> getCandidateFilesForRowKeyBefore(final KeyValue targetKey) {
108     return new ArrayList<StoreFile>(Lists.reverse(this.storefiles)).iterator();
109   }
110 
111   @Override
112   public Iterator<StoreFile> updateCandidateFilesForRowKeyBefore(
113       Iterator<StoreFile> candidateFiles, final KeyValue targetKey, final Cell candidate) {
114     // Default store has nothing useful to do here.
115     // TODO: move this comment when implementing Level:
116     // Level store can trim the list by range, removing all the files which cannot have
117     // any useful candidates less than "candidate".
118     return candidateFiles;
119   }
120 
121   @Override
122   public final byte[] getSplitPoint() throws IOException {
123     if (this.storefiles.isEmpty()) {
124       return null;
125     }
126     return StoreUtils.getLargestFile(this.storefiles).getFileSplitPoint(this.kvComparator);
127   }
128 
129   @Override
130   public final Collection<StoreFile> getFilesForScanOrGet(boolean isGet,
131       byte[] startRow, byte[] stopRow) {
132     // We cannot provide any useful input and already have the files sorted by seqNum.
133     return getStorefiles();
134   }
135 
136   @Override
137   public int getStoreCompactionPriority() {
138     int priority = blockingFileCount - storefiles.size();
139     return (priority == HStore.PRIORITY_USER) ? priority + 1 : priority;
140   }
141 
142   @Override
143   public Collection<StoreFile> getUnneededFiles(long maxTs, List<StoreFile> filesCompacting) {
144     Collection<StoreFile> expiredStoreFiles = null;
145     ImmutableList<StoreFile> files = storefiles;
146     // 1) We can never get rid of the last file which has the maximum seqid.
147     // 2) Files that are not the latest can't become one due to (1), so the rest are fair game.
148     for (int i = 0; i < files.size() - 1; ++i) {
149       StoreFile sf = files.get(i);
150       long fileTs = sf.getReader().getMaxTimestamp();
151       if (fileTs < maxTs && !filesCompacting.contains(sf)) {
152         LOG.info("Found an expired store file: " + sf.getPath()
153             + " whose maxTimeStamp is " + fileTs + ", which is below " + maxTs);
154         if (expiredStoreFiles == null) {
155           expiredStoreFiles = new ArrayList<StoreFile>();
156         }
157         expiredStoreFiles.add(sf);
158       }
159     }
160     return expiredStoreFiles;
161   }
162 
163   private void sortAndSetStoreFiles(List<StoreFile> storeFiles) {
164     Collections.sort(storeFiles, StoreFile.Comparators.SEQ_ID);
165     storefiles = ImmutableList.copyOf(storeFiles);
166   }
167 
168   @Override
169   public double getCompactionPressure() {
170     int storefileCount = getStorefileCount();
171     int minFilesToCompact = comConf.getMinFilesToCompact();
172     if (storefileCount <= minFilesToCompact) {
173       return 0.0;
174     }
175     return (double) (storefileCount - minFilesToCompact) / (blockingFileCount - minFilesToCompact);
176   }
177 }
178