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