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      // TODO: I can return a null list of StoreFiles? That'll mess up clients. St.Ack 20151111
74      return storefiles;
75    }
76  
77    @Override
78    public void insertNewFiles(Collection<StoreFile> sfs) throws IOException {
79      ArrayList<StoreFile> newFiles = new ArrayList<StoreFile>(storefiles);
80      newFiles.addAll(sfs);
81      sortAndSetStoreFiles(newFiles);
82    }
83  
84    @Override
85    public ImmutableCollection<StoreFile> clearFiles() {
86      ImmutableList<StoreFile> result = storefiles;
87      storefiles = ImmutableList.of();
88      return result;
89    }
90  
91    @Override
92    public final int getStorefileCount() {
93      return storefiles.size();
94    }
95  
96    @Override
97    public void addCompactionResults(
98      Collection<StoreFile> compactedFiles, Collection<StoreFile> results) {
99      ArrayList<StoreFile> newStoreFiles = Lists.newArrayList(storefiles);
100     newStoreFiles.removeAll(compactedFiles);
101     if (!results.isEmpty()) {
102       newStoreFiles.addAll(results);
103     }
104     sortAndSetStoreFiles(newStoreFiles);
105   }
106 
107   @Override
108   public final Iterator<StoreFile> getCandidateFilesForRowKeyBefore(final KeyValue targetKey) {
109     return new ArrayList<StoreFile>(Lists.reverse(this.storefiles)).iterator();
110   }
111 
112   @Override
113   public Iterator<StoreFile> updateCandidateFilesForRowKeyBefore(
114       Iterator<StoreFile> candidateFiles, final KeyValue targetKey, final Cell candidate) {
115     // Default store has nothing useful to do here.
116     // TODO: move this comment when implementing Level:
117     // Level store can trim the list by range, removing all the files which cannot have
118     // any useful candidates less than "candidate".
119     return candidateFiles;
120   }
121 
122   @Override
123   public final byte[] getSplitPoint() throws IOException {
124     if (this.storefiles.isEmpty()) {
125       return null;
126     }
127     return StoreUtils.getLargestFile(this.storefiles).getFileSplitPoint(this.kvComparator);
128   }
129 
130   @Override
131   public final Collection<StoreFile> getFilesForScanOrGet(boolean isGet,
132       byte[] startRow, byte[] stopRow) {
133     // We cannot provide any useful input and already have the files sorted by seqNum.
134     return getStorefiles();
135   }
136 
137   @Override
138   public int getStoreCompactionPriority() {
139     int priority = blockingFileCount - storefiles.size();
140     return (priority == HStore.PRIORITY_USER) ? priority + 1 : priority;
141   }
142 
143   @Override
144   public Collection<StoreFile> getUnneededFiles(long maxTs, List<StoreFile> filesCompacting) {
145     Collection<StoreFile> expiredStoreFiles = null;
146     ImmutableList<StoreFile> files = storefiles;
147     // 1) We can never get rid of the last file which has the maximum seqid.
148     // 2) Files that are not the latest can't become one due to (1), so the rest are fair game.
149     for (int i = 0; i < files.size() - 1; ++i) {
150       StoreFile sf = files.get(i);
151       long fileTs = sf.getReader().getMaxTimestamp();
152       if (fileTs < maxTs && !filesCompacting.contains(sf)) {
153         LOG.info("Found an expired store file: " + sf.getPath()
154             + " whose maxTimeStamp is " + fileTs + ", which is below " + maxTs);
155         if (expiredStoreFiles == null) {
156           expiredStoreFiles = new ArrayList<StoreFile>();
157         }
158         expiredStoreFiles.add(sf);
159       }
160     }
161     return expiredStoreFiles;
162   }
163 
164   private void sortAndSetStoreFiles(List<StoreFile> storeFiles) {
165     Collections.sort(storeFiles, StoreFile.Comparators.SEQ_ID);
166     storefiles = ImmutableList.copyOf(storeFiles);
167   }
168 
169   @Override
170   public double getCompactionPressure() {
171     int storefileCount = getStorefileCount();
172     int minFilesToCompact = comConf.getMinFilesToCompact();
173     if (storefileCount <= minFilesToCompact) {
174       return 0.0;
175     }
176     return (double) (storefileCount - minFilesToCompact) / (blockingFileCount - minFilesToCompact);
177   }
178 }
179