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 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.hbase.classification.InterfaceAudience;
27 import org.apache.hadoop.hbase.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
42
43 @InterfaceAudience.LimitedPrivate({ "coprocessor" })
44 @InterfaceStability.Evolving
45 public class CompactionRequest implements Comparable<CompactionRequest> {
46 private static final Log LOG = LogFactory.getLog(CompactionRequest.class);
47
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
55 private long selectionTime;
56
57 private Long timeInNanos;
58 private String regionName = "";
59 private String storeName = "";
60 private long totalSize = -1L;
61
62 private Boolean retainDeleteMarkers = null;
63
64
65
66
67 public CompactionRequest() {
68 this.selectionTime = EnvironmentEdgeManager.currentTime();
69 this.timeInNanos = System.nanoTime();
70 }
71
72 public CompactionRequest(Collection<StoreFile> files) {
73 this();
74 Preconditions.checkNotNull(files);
75 this.filesToCompact = files;
76 recalculateSize();
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 return this;
107 }
108
109
110
111
112
113
114
115
116
117
118
119 @Override
120 public int compareTo(CompactionRequest request) {
121
122 if (this.equals(request)) {
123 return 0;
124 }
125 int compareVal;
126
127 compareVal = priority - request.priority;
128 if (compareVal != 0) {
129 return compareVal;
130 }
131
132 compareVal = timeInNanos.compareTo(request.timeInNanos);
133 if (compareVal != 0) {
134 return compareVal;
135 }
136
137
138 return this.hashCode() - request.hashCode();
139 }
140
141 @Override
142 public boolean equals(Object obj) {
143 return (this == obj);
144 }
145
146 public Collection<StoreFile> getFiles() {
147 return this.filesToCompact;
148 }
149
150
151
152
153 public void setDescription(String regionName, String storeName) {
154 this.regionName = regionName;
155 this.storeName = storeName;
156 }
157
158
159 public long getSize() {
160 return totalSize;
161 }
162
163 public boolean isAllFiles() {
164 return this.isMajor == DisplayCompactionType.MAJOR
165 || this.isMajor == DisplayCompactionType.ALL_FILES;
166 }
167
168 public boolean isMajor() {
169 return this.isMajor == DisplayCompactionType.MAJOR;
170 }
171
172
173 public int getPriority() {
174 return priority;
175 }
176
177
178 public void setPriority(int p) {
179 this.priority = p;
180 }
181
182 public boolean isOffPeak() {
183 return this.isOffPeak;
184 }
185
186 public void setOffPeak(boolean value) {
187 this.isOffPeak = value;
188 }
189
190 public long getSelectionTime() {
191 return this.selectionTime;
192 }
193
194
195
196
197
198
199 public void setIsMajor(boolean isMajor, boolean isAllFiles) {
200 assert isAllFiles || !isMajor;
201 this.isMajor = !isAllFiles ? DisplayCompactionType.MINOR
202 : (isMajor ? DisplayCompactionType.MAJOR : DisplayCompactionType.ALL_FILES);
203 }
204
205
206
207
208
209
210 public void forceRetainDeleteMarkers() {
211 this.retainDeleteMarkers = Boolean.TRUE;
212 }
213
214
215
216
217 public boolean isRetainDeleteMarkers() {
218 return (this.retainDeleteMarkers != null) ? this.retainDeleteMarkers.booleanValue()
219 : !isAllFiles();
220 }
221
222 @Override
223 public String toString() {
224 String fsList = Joiner.on(", ").join(
225 Collections2.transform(Collections2.filter(
226 this.getFiles(),
227 new Predicate<StoreFile>() {
228 public boolean apply(StoreFile sf) {
229 return sf.getReader() != null;
230 }
231 }), new Function<StoreFile, String>() {
232 public String apply(StoreFile sf) {
233 return StringUtils.humanReadableInt(
234 (sf.getReader() == null) ? 0 : sf.getReader().length());
235 }
236 }));
237
238 return "regionName=" + regionName + ", storeName=" + storeName +
239 ", fileCount=" + this.getFiles().size() +
240 ", fileSize=" + StringUtils.humanReadableInt(totalSize) +
241 ((fsList.isEmpty()) ? "" : " (" + fsList + ")") +
242 ", priority=" + priority + ", time=" + timeInNanos;
243 }
244
245
246
247
248
249 private void recalculateSize() {
250 long sz = 0;
251 for (StoreFile sf : this.filesToCompact) {
252 Reader r = sf.getReader();
253 sz += r == null ? 0 : r.length();
254 }
255 this.totalSize = sz;
256 }
257 }
258