1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
40
41 @InterfaceAudience.LimitedPrivate({ "coprocessor" })
42 @InterfaceStability.Evolving
43 public class CompactionRequest implements Comparable<CompactionRequest> {
44
45
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
53 private long selectionTime;
54
55 private Long timeInNanos;
56 private String regionName = "";
57 private String storeName = "";
58 private long totalSize = -1L;
59
60
61
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
81
82 public void beforeExecute() {}
83
84
85
86
87 public void afterExecute() {}
88
89
90
91
92
93
94
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
112
113
114
115
116
117
118
119
120 @Override
121 public int compareTo(CompactionRequest request) {
122
123 if (this.equals(request)) {
124 return 0;
125 }
126 int compareVal;
127
128 compareVal = priority - request.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
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
222
223 public void setDescription(String regionName, String storeName) {
224 this.regionName = regionName;
225 this.storeName = storeName;
226 }
227
228
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
243 public int getPriority() {
244 return priority;
245 }
246
247
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
266
267
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
302
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