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.compactions;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  import org.apache.hadoop.hbase.classification.InterfaceStability;
28  import org.apache.hadoop.hbase.regionserver.Store;
29  import org.apache.hadoop.hbase.regionserver.StoreFile;
30  import org.apache.hadoop.hbase.regionserver.StoreFile.Reader;
31  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
32  import org.apache.hadoop.util.StringUtils;
33  
34  import com.google.common.base.Function;
35  import com.google.common.base.Joiner;
36  import com.google.common.base.Preconditions;
37  import com.google.common.base.Predicate;
38  import com.google.common.collect.Collections2;
39  
40  /**
41   * This class holds all logical details necessary to run a compaction.
42   */
43  @InterfaceAudience.LimitedPrivate({ "coprocessor" })
44  @InterfaceStability.Evolving
45  public class CompactionRequest implements Comparable<CompactionRequest> {
46    private static final Log LOG = LogFactory.getLog(CompactionRequest.class);
47    // was this compaction promoted to an off-peak
48    private boolean isOffPeak = false;
49    private enum DisplayCompactionType { MINOR, ALL_FILES, MAJOR }
50    private DisplayCompactionType isMajor = DisplayCompactionType.MINOR;
51    private int priority = Store.NO_PRIORITY;
52    private Collection<StoreFile> filesToCompact;
53  
54    // CompactRequest object creation time.
55    private long selectionTime;
56    // System time used to compare objects in FIFO order. TODO: maybe use selectionTime?
57    private Long timeInNanos;
58    private String regionName = "";
59    private String storeName = "";
60    private long totalSize = -1L;
61  
62    private Boolean retainDeleteMarkers = null;
63  
64    /**
65     * This ctor should be used by coprocessors that want to subclass CompactionRequest.
66     */
67    public CompactionRequest() {
68      this.selectionTime = EnvironmentEdgeManager.currentTime();
69      this.timeInNanos = System.nanoTime();
70    }
71  
72    public CompactionRequest(Collection<StoreFile> files) {
73      this();
74      Preconditions.checkNotNull(files);
75      this.filesToCompact = files;
76      recalculateSize();
77    }
78  
79    /**
80     * Called before compaction is executed by CompactSplitThread; for use by coproc subclasses.
81     */
82    public void beforeExecute() {}
83  
84    /**
85     * Called after compaction is executed by CompactSplitThread; for use by coproc subclasses.
86     */
87    public void afterExecute() {}
88  
89    /**
90     * Combines the request with other request. Coprocessors subclassing CR may override
91     * this if they want to do clever things based on CompactionPolicy selection that
92     * is passed to this method via "other". The default implementation just does a copy.
93     * @param other Request to combine with.
94     * @return The result (may be "this" or "other").
95     */
96    public CompactionRequest combineWith(CompactionRequest other) {
97      this.filesToCompact = new ArrayList<StoreFile>(other.getFiles());
98      this.isOffPeak = other.isOffPeak;
99      this.isMajor = other.isMajor;
100     this.priority = other.priority;
101     this.selectionTime = other.selectionTime;
102     this.timeInNanos = other.timeInNanos;
103     this.regionName = other.regionName;
104     this.storeName = other.storeName;
105     this.totalSize = other.totalSize;
106     return this;
107   }
108 
109   /**
110    * This function will define where in the priority queue the request will
111    * end up.  Those with the highest priorities will be first.  When the
112    * priorities are the same it will first compare priority then date
113    * to maintain a FIFO functionality.
114    *
115    * <p>Note: The enqueue timestamp is accurate to the nanosecond. if two
116    * requests have same timestamp then this function will break the tie
117    * arbitrarily with hashCode() comparing.
118    */
119   @Override
120   public int compareTo(CompactionRequest request) {
121     //NOTE: The head of the priority queue is the least element
122     if (this.equals(request)) {
123       return 0; //they are the same request
124     }
125     int compareVal;
126 
127     compareVal = priority - request.priority; //compare priority
128     if (compareVal != 0) {
129       return compareVal;
130     }
131 
132     compareVal = timeInNanos.compareTo(request.timeInNanos);
133     if (compareVal != 0) {
134       return compareVal;
135     }
136 
137     // break the tie based on hash code
138     return this.hashCode() - request.hashCode();
139   }
140 
141   @Override
142   public boolean equals(Object obj) {
143     return (this == obj);
144   }
145 
146   public Collection<StoreFile> getFiles() {
147     return this.filesToCompact;
148   }
149 
150   /**
151    * Sets the region/store name, for logging.
152    */
153   public void setDescription(String regionName, String storeName) {
154     this.regionName = regionName;
155     this.storeName = storeName;
156   }
157 
158   /** Gets the total size of all StoreFiles in compaction */
159   public long getSize() {
160     return totalSize;
161   }
162 
163   public boolean isAllFiles() {
164     return this.isMajor == DisplayCompactionType.MAJOR
165         || this.isMajor == DisplayCompactionType.ALL_FILES;
166   }
167 
168   public boolean isMajor() {
169     return this.isMajor == DisplayCompactionType.MAJOR;
170   }
171 
172   /** Gets the priority for the request */
173   public int getPriority() {
174     return priority;
175   }
176 
177   /** Sets the priority for the request */
178   public void setPriority(int p) {
179     this.priority = p;
180   }
181 
182   public boolean isOffPeak() {
183     return this.isOffPeak;
184   }
185 
186   public void setOffPeak(boolean value) {
187     this.isOffPeak = value;
188   }
189 
190   public long getSelectionTime() {
191     return this.selectionTime;
192   }
193 
194   /**
195    * Specify if this compaction should be a major compaction based on the state of the store
196    * @param isMajor <tt>true</tt> if the system determines that this compaction should be a major
197    *          compaction
198    */
199   public void setIsMajor(boolean isMajor, boolean isAllFiles) {
200     assert isAllFiles || !isMajor;
201     this.isMajor = !isAllFiles ? DisplayCompactionType.MINOR
202         : (isMajor ? DisplayCompactionType.MAJOR : DisplayCompactionType.ALL_FILES);
203   }
204 
205   /**
206    * Forcefully setting that this compaction has to retain the delete markers in the new compacted
207    * file, whatever be the type of the compaction.<br>
208    * Note : By default HBase drops delete markers when the compaction is on all files.
209    */
210   public void forceRetainDeleteMarkers() {
211     this.retainDeleteMarkers = Boolean.TRUE;
212   }
213 
214   /**
215    * @return Whether the compaction has to retain the delete markers or not.
216    */
217   public boolean isRetainDeleteMarkers() {
218     return (this.retainDeleteMarkers != null) ? this.retainDeleteMarkers.booleanValue()
219         : !isAllFiles();
220   }
221 
222   @Override
223   public String toString() {
224     String fsList = Joiner.on(", ").join(
225         Collections2.transform(Collections2.filter(
226             this.getFiles(),
227             new Predicate<StoreFile>() {
228               public boolean apply(StoreFile sf) {
229                 return sf.getReader() != null;
230               }
231           }), new Function<StoreFile, String>() {
232             public String apply(StoreFile sf) {
233               return StringUtils.humanReadableInt(
234                 (sf.getReader() == null) ? 0 : sf.getReader().length());
235             }
236           }));
237 
238     return "regionName=" + regionName + ", storeName=" + storeName +
239       ", fileCount=" + this.getFiles().size() +
240       ", fileSize=" + StringUtils.humanReadableInt(totalSize) +
241         ((fsList.isEmpty()) ? "" : " (" + fsList + ")") +
242       ", priority=" + priority + ", time=" + timeInNanos;
243   }
244 
245   /**
246    * Recalculate the size of the compaction based on current files.
247    * @param files files that should be included in the compaction
248    */
249   private void recalculateSize() {
250     long sz = 0;
251     for (StoreFile sf : this.filesToCompact) {
252       Reader r = sf.getReader();
253       sz += r == null ? 0 : r.length();
254     }
255     this.totalSize = sz;
256   }
257 }
258