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.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.hadoop.hbase.classification.InterfaceAudience;
33  import org.apache.hadoop.hbase.classification.InterfaceStability;
34  import org.apache.hadoop.hbase.regionserver.Store;
35  import org.apache.hadoop.hbase.regionserver.StoreFile;
36  import org.apache.hadoop.hbase.regionserver.StoreFile.Reader;
37  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
38  import org.apache.hadoop.util.StringUtils;
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    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    /**
63     * This ctor should be used by coprocessors that want to subclass CompactionRequest.
64     */
65    public CompactionRequest() {
66      this.selectionTime = EnvironmentEdgeManager.currentTime();
67      this.timeInNanos = System.nanoTime();
68    }
69  
70    public CompactionRequest(Collection<StoreFile> files) {
71      this();
72      Preconditions.checkNotNull(files);
73      this.filesToCompact = files;
74      recalculateSize();
75    }
76  
77    public void updateFiles(Collection<StoreFile> files) {
78      this.filesToCompact = files;
79    }
80  
81    /**
82     * Called before compaction is executed by CompactSplitThread; for use by coproc subclasses.
83     */
84    public void beforeExecute() {}
85  
86    /**
87     * Called after compaction is executed by CompactSplitThread; for use by coproc subclasses.
88     */
89    public void afterExecute() {}
90  
91    /**
92     * Combines the request with other request. Coprocessors subclassing CR may override
93     * this if they want to do clever things based on CompactionPolicy selection that
94     * is passed to this method via "other". The default implementation just does a copy.
95     * @param other Request to combine with.
96     * @return The result (may be "this" or "other").
97     */
98    public CompactionRequest combineWith(CompactionRequest other) {
99      this.filesToCompact = new ArrayList<StoreFile>(other.getFiles());
100     this.isOffPeak = other.isOffPeak;
101     this.isMajor = other.isMajor;
102     this.priority = other.priority;
103     this.selectionTime = other.selectionTime;
104     this.timeInNanos = other.timeInNanos;
105     this.regionName = other.regionName;
106     this.storeName = other.storeName;
107     this.totalSize = other.totalSize;
108     recalculateSize();
109     return this;
110   }
111 
112   /**
113    * This function will define where in the priority queue the request will
114    * end up.  Those with the highest priorities will be first.  When the
115    * priorities are the same it will first compare priority then date
116    * to maintain a FIFO functionality.
117    *
118    * <p>Note: The enqueue timestamp is accurate to the nanosecond. if two
119    * requests have same timestamp then this function will break the tie
120    * arbitrarily with hashCode() comparing.
121    */
122   @Override
123   public int compareTo(CompactionRequest request) {
124     //NOTE: The head of the priority queue is the least element
125     if (this.equals(request)) {
126       return 0; //they are the same request
127     }
128     int compareVal;
129 
130     compareVal = priority - request.priority; //compare priority
131     if (compareVal != 0) {
132       return compareVal;
133     }
134 
135     compareVal = timeInNanos.compareTo(request.timeInNanos);
136     if (compareVal != 0) {
137       return compareVal;
138     }
139 
140     // break the tie based on hash code
141     return this.hashCode() - request.hashCode();
142   }
143 
144   @Override
145   public boolean equals(Object obj) {
146     return (this == obj);
147   }
148 
149   public Collection<StoreFile> getFiles() {
150     return this.filesToCompact;
151   }
152 
153   /**
154    * Sets the region/store name, for logging.
155    */
156   public void setDescription(String regionName, String storeName) {
157     this.regionName = regionName;
158     this.storeName = storeName;
159   }
160 
161   /** Gets the total size of all StoreFiles in compaction */
162   public long getSize() {
163     return totalSize;
164   }
165 
166   public boolean isAllFiles() {
167     return this.isMajor == DisplayCompactionType.MAJOR
168         || this.isMajor == DisplayCompactionType.ALL_FILES;
169   }
170 
171   public boolean isMajor() {
172     return this.isMajor == DisplayCompactionType.MAJOR;
173   }
174 
175   /** Gets the priority for the request */
176   public int getPriority() {
177     return priority;
178   }
179 
180   /** Sets the priority for the request */
181   public void setPriority(int p) {
182     this.priority = p;
183   }
184 
185   public boolean isOffPeak() {
186     return this.isOffPeak;
187   }
188 
189   public void setOffPeak(boolean value) {
190     this.isOffPeak = value;
191   }
192 
193   public long getSelectionTime() {
194     return this.selectionTime;
195   }
196 
197   /**
198    * Specify if this compaction should be a major compaction based on the state of the store
199    * @param isMajor <tt>true</tt> if the system determines that this compaction should be a major
200    *          compaction
201    */
202   public void setIsMajor(boolean isMajor, boolean isAllFiles) {
203     assert isAllFiles || !isMajor;
204     this.isMajor = !isAllFiles ? DisplayCompactionType.MINOR
205         : (isMajor ? DisplayCompactionType.MAJOR : DisplayCompactionType.ALL_FILES);
206   }
207 
208   @Override
209   public String toString() {
210     String fsList = Joiner.on(", ").join(
211         Collections2.transform(Collections2.filter(
212             this.getFiles(),
213             new Predicate<StoreFile>() {
214               @Override
215               public boolean apply(StoreFile sf) {
216                 return sf.getReader() != null;
217               }
218           }), new Function<StoreFile, String>() {
219             @Override
220             public String apply(StoreFile sf) {
221               return StringUtils.humanReadableInt(
222                 (sf.getReader() == null) ? 0 : sf.getReader().length());
223             }
224           }));
225 
226     return "regionName=" + regionName + ", storeName=" + storeName +
227       ", fileCount=" + this.getFiles().size() +
228       ", fileSize=" + StringUtils.humanReadableInt(totalSize) +
229         ((fsList.isEmpty()) ? "" : " (" + fsList + ")") +
230       ", priority=" + priority + ", time=" + timeInNanos;
231   }
232 
233   /**
234    * Recalculate the size of the compaction based on current files.
235    * @param files files that should be included in the compaction
236    */
237   private void recalculateSize() {
238     long sz = 0;
239     for (StoreFile sf : this.filesToCompact) {
240       Reader r = sf.getReader();
241       sz += r == null ? 0 : r.length();
242     }
243     this.totalSize = sz;
244   }
245 }
246