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