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