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 com.google.common.base.Function;
22  import com.google.common.base.Joiner;
23  import com.google.common.base.Preconditions;
24  import com.google.common.base.Predicate;
25  import com.google.common.collect.Collections2;
26  
27  import java.util.ArrayList;
28  import java.util.Collection;
29  
30  import org.apache.hadoop.hbase.classification.InterfaceAudience;
31  import org.apache.hadoop.hbase.classification.InterfaceStability;
32  import org.apache.hadoop.hbase.regionserver.Store;
33  import org.apache.hadoop.hbase.regionserver.StoreFile;
34  import org.apache.hadoop.hbase.regionserver.StoreFile.Reader;
35  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
36  import org.apache.hadoop.util.StringUtils;
37  
38  /**
39   * This class holds all logical details necessary to run a compaction.
40   */
41  @InterfaceAudience.LimitedPrivate({ "coprocessor" })
42  @InterfaceStability.Evolving
43  public class CompactionRequest implements Comparable<CompactionRequest> {
44  
45    // was this compaction promoted to an off-peak
46    private boolean isOffPeak = false;
47    private enum DisplayCompactionType { MINOR, ALL_FILES, MAJOR }
48    private DisplayCompactionType isMajor = DisplayCompactionType.MINOR;
49    private int priority = Store.NO_PRIORITY;
50    private Collection<StoreFile> filesToCompact;
51  
52    // CompactRequest object creation time.
53    private long selectionTime;
54    // System time used to compare objects in FIFO order. TODO: maybe use selectionTime?
55    private Long timeInNanos;
56    private String regionName = "";
57    private String storeName = "";
58    private long totalSize = -1L;
59  
60    /**
61     * This ctor should be used by coprocessors that want to subclass CompactionRequest.
62     */
63    public CompactionRequest() {
64      this.selectionTime = EnvironmentEdgeManager.currentTime();
65      this.timeInNanos = System.nanoTime();
66    }
67  
68    public CompactionRequest(Collection<StoreFile> files) {
69      this();
70      Preconditions.checkNotNull(files);
71      this.filesToCompact = files;
72      recalculateSize();
73    }
74  
75    public void updateFiles(Collection<StoreFile> files) {
76      this.filesToCompact = files;
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     recalculateSize();
107     return this;
108   }
109 
110   /**
111    * This function will define where in the priority queue the request will
112    * end up.  Those with the highest priorities will be first.  When the
113    * priorities are the same it will first compare priority then date
114    * to maintain a FIFO functionality.
115    *
116    * <p>Note: The enqueue timestamp is accurate to the nanosecond. if two
117    * requests have same timestamp then this function will break the tie
118    * arbitrarily with hashCode() comparing.
119    */
120   @Override
121   public int compareTo(CompactionRequest request) {
122     //NOTE: The head of the priority queue is the least element
123     if (this.equals(request)) {
124       return 0; //they are the same request
125     }
126     int compareVal;
127 
128     compareVal = priority - request.priority; //compare priority
129     if (compareVal != 0) {
130       return compareVal;
131     }
132 
133     compareVal = timeInNanos.compareTo(request.timeInNanos);
134     if (compareVal != 0) {
135       return compareVal;
136     }
137 
138     // break the tie based on hash code
139     return this.hashCode() - request.hashCode();
140   }
141 
142   @Override
143   public int hashCode() {
144     final int prime = 31;
145     int result = 1;
146     result = prime * result + ((filesToCompact == null) ? 0 : filesToCompact.hashCode());
147     result = prime * result + ((isMajor == null) ? 0 : isMajor.hashCode());
148     result = prime * result + (isOffPeak ? 1231 : 1237);
149     result = prime * result + priority;
150     result = prime * result + ((regionName == null) ? 0 : regionName.hashCode());
151     result = prime * result + (int) (selectionTime ^ (selectionTime >>> 32));
152     result = prime * result + ((storeName == null) ? 0 : storeName.hashCode());
153     result = prime * result + ((timeInNanos == null) ? 0 : timeInNanos.hashCode());
154     result = prime * result + (int) (totalSize ^ (totalSize >>> 32));
155     return result;
156   }
157 
158   @Override
159   public boolean equals(Object obj) {
160     if (this == obj) {
161       return true;
162     }
163     if (obj == null) {
164       return false;
165     }
166     if (getClass() != obj.getClass()) {
167       return false;
168     }
169     CompactionRequest other = (CompactionRequest) obj;
170     if (filesToCompact == null) {
171       if (other.filesToCompact != null) {
172         return false;
173       }
174     } else if (!filesToCompact.equals(other.filesToCompact)) {
175       return false;
176     }
177     if (isMajor != other.isMajor) {
178       return false;
179     }
180     if (isOffPeak != other.isOffPeak) {
181       return false;
182     }
183     if (priority != other.priority) {
184       return false;
185     }
186     if (regionName == null) {
187       if (other.regionName != null) {
188         return false;
189       }
190     } else if (!regionName.equals(other.regionName)) {
191       return false;
192     }
193     if (selectionTime != other.selectionTime) {
194       return false;
195     }
196     if (storeName == null) {
197       if (other.storeName != null) {
198         return false;
199       }
200     } else if (!storeName.equals(other.storeName)) {
201       return false;
202     }
203     if (timeInNanos == null) {
204       if (other.timeInNanos != null) {
205         return false;
206       }
207     } else if (!timeInNanos.equals(other.timeInNanos)) {
208       return false;
209     }
210     if (totalSize != other.totalSize) {
211       return false;
212     }
213     return true;
214   }
215 
216   public Collection<StoreFile> getFiles() {
217     return this.filesToCompact;
218   }
219 
220   /**
221    * Sets the region/store name, for logging.
222    */
223   public void setDescription(String regionName, String storeName) {
224     this.regionName = regionName;
225     this.storeName = storeName;
226   }
227 
228   /** Gets the total size of all StoreFiles in compaction */
229   public long getSize() {
230     return totalSize;
231   }
232 
233   public boolean isAllFiles() {
234     return this.isMajor == DisplayCompactionType.MAJOR
235         || this.isMajor == DisplayCompactionType.ALL_FILES;
236   }
237 
238   public boolean isMajor() {
239     return this.isMajor == DisplayCompactionType.MAJOR;
240   }
241 
242   /** Gets the priority for the request */
243   public int getPriority() {
244     return priority;
245   }
246 
247   /** Sets the priority for the request */
248   public void setPriority(int p) {
249     this.priority = p;
250   }
251 
252   public boolean isOffPeak() {
253     return this.isOffPeak;
254   }
255 
256   public void setOffPeak(boolean value) {
257     this.isOffPeak = value;
258   }
259 
260   public long getSelectionTime() {
261     return this.selectionTime;
262   }
263 
264   /**
265    * Specify if this compaction should be a major compaction based on the state of the store
266    * @param isMajor <tt>true</tt> if the system determines that this compaction should be a major
267    *          compaction
268    */
269   public void setIsMajor(boolean isMajor, boolean isAllFiles) {
270     assert isAllFiles || !isMajor;
271     this.isMajor = !isAllFiles ? DisplayCompactionType.MINOR
272         : (isMajor ? DisplayCompactionType.MAJOR : DisplayCompactionType.ALL_FILES);
273   }
274 
275   @Override
276   public String toString() {
277     String fsList = Joiner.on(", ").join(
278         Collections2.transform(Collections2.filter(
279             this.getFiles(),
280             new Predicate<StoreFile>() {
281               @Override
282               public boolean apply(StoreFile sf) {
283                 return sf.getReader() != null;
284               }
285           }), new Function<StoreFile, String>() {
286             @Override
287             public String apply(StoreFile sf) {
288               return StringUtils.humanReadableInt(
289                 (sf.getReader() == null) ? 0 : sf.getReader().length());
290             }
291           }));
292 
293     return "regionName=" + regionName + ", storeName=" + storeName +
294       ", fileCount=" + this.getFiles().size() +
295       ", fileSize=" + StringUtils.humanReadableInt(totalSize) +
296         ((fsList.isEmpty()) ? "" : " (" + fsList + ")") +
297       ", priority=" + priority + ", time=" + timeInNanos;
298   }
299 
300   /**
301    * Recalculate the size of the compaction based on current files.
302    * @param files files that should be included in the compaction
303    */
304   private void recalculateSize() {
305     long sz = 0;
306     for (StoreFile sf : this.filesToCompact) {
307       Reader r = sf.getReader();
308       sz += r == null ? 0 : r.length();
309     }
310     this.totalSize = sz;
311   }
312 }
313