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 enqueue timestamp is accurate to the nanosecond. if two
112    * requests have same timestamp then this function will break the tie
113    * arbitrarily with hashCode() comparing.
114    */
115   @Override
116   public int compareTo(CompactionRequest request) {
117     //NOTE: The head of the priority queue is the least element
118     if (this.equals(request)) {
119       return 0; //they are the same request
120     }
121     int compareVal;
122 
123     compareVal = priority - request.priority; //compare priority
124     if (compareVal != 0) {
125       return compareVal;
126     }
127 
128     compareVal = timeInNanos.compareTo(request.timeInNanos);
129     if (compareVal != 0) {
130       return compareVal;
131     }
132 
133     // break the tie based on hash code
134     return this.hashCode() - request.hashCode();
135   }
136 
137   @Override
138   public boolean equals(Object obj) {
139     return (this == obj);
140   }
141 
142   public Collection<StoreFile> getFiles() {
143     return this.filesToCompact;
144   }
145 
146   /**
147    * Sets the region/store name, for logging.
148    */
149   public void setDescription(String regionName, String storeName) {
150     this.regionName = regionName;
151     this.storeName = storeName;
152   }
153 
154   /** Gets the total size of all StoreFiles in compaction */
155   public long getSize() {
156     return totalSize;
157   }
158 
159   public boolean isMajor() {
160     return this.isMajor;
161   }
162 
163   /** Gets the priority for the request */
164   public int getPriority() {
165     return priority;
166   }
167 
168   /** Sets the priority for the request */
169   public void setPriority(int p) {
170     this.priority = p;
171   }
172 
173   public boolean isOffPeak() {
174     return this.isOffPeak;
175   }
176 
177   public void setOffPeak(boolean value) {
178     this.isOffPeak = value;
179   }
180 
181   public long getSelectionTime() {
182     return this.selectionTime;
183   }
184 
185   /**
186    * Specify if this compaction should be a major compaction based on the state of the store
187    * @param isMajor <tt>true</tt> if the system determines that this compaction should be a major
188    *          compaction
189    */
190   public void setIsMajor(boolean isMajor) {
191     this.isMajor = isMajor;
192   }
193 
194   @Override
195   public String toString() {
196     String fsList = Joiner.on(", ").join(
197         Collections2.transform(Collections2.filter(
198             this.getFiles(),
199             new Predicate<StoreFile>() {
200               public boolean apply(StoreFile sf) {
201                 return sf.getReader() != null;
202               }
203           }), new Function<StoreFile, String>() {
204             public String apply(StoreFile sf) {
205               return StringUtils.humanReadableInt(sf.getReader().length());
206             }
207           }));
208 
209     return "regionName=" + regionName + ", storeName=" + storeName +
210       ", fileCount=" + this.getFiles().size() +
211       ", fileSize=" + StringUtils.humanReadableInt(totalSize) +
212         ((fsList.isEmpty()) ? "" : " (" + fsList + ")") +
213       ", priority=" + priority + ", time=" + timeInNanos;
214   }
215 
216   /**
217    * Recalculate the size of the compaction based on current files.
218    * @param files files that should be included in the compaction
219    */
220   private void recalculateSize() {
221     long sz = 0;
222     for (StoreFile sf : this.filesToCompact) {
223       sz += sf.getReader().length();
224     }
225     this.totalSize = sz;
226   }
227 }
228