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.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.hadoop.hbase.classification.InterfaceAudience;
33 import org.apache.hadoop.hbase.classification.InterfaceStability;
34 import org.apache.hadoop.hbase.regionserver.Store;
35 import org.apache.hadoop.hbase.regionserver.StoreFile;
36 import org.apache.hadoop.hbase.regionserver.StoreFile.Reader;
37 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
38 import org.apache.hadoop.util.StringUtils;
39
40
41
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
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 public void updateFiles(Collection<StoreFile> files) {
80 this.filesToCompact = files;
81 }
82
83
84
85
86 public void beforeExecute() {}
87
88
89
90
91 public void afterExecute() {}
92
93
94
95
96
97
98
99
100 public CompactionRequest combineWith(CompactionRequest other) {
101 this.filesToCompact = new ArrayList<StoreFile>(other.getFiles());
102 this.isOffPeak = other.isOffPeak;
103 this.isMajor = other.isMajor;
104 this.priority = other.priority;
105 this.selectionTime = other.selectionTime;
106 this.timeInNanos = other.timeInNanos;
107 this.regionName = other.regionName;
108 this.storeName = other.storeName;
109 this.totalSize = other.totalSize;
110 recalculateSize();
111 return this;
112 }
113
114
115
116
117
118
119
120
121
122
123
124 @Override
125 public int compareTo(CompactionRequest request) {
126
127 if (this.equals(request)) {
128 return 0;
129 }
130 int compareVal;
131
132 compareVal = priority - request.priority;
133 if (compareVal != 0) {
134 return compareVal;
135 }
136
137 compareVal = timeInNanos.compareTo(request.timeInNanos);
138 if (compareVal != 0) {
139 return compareVal;
140 }
141
142
143 return this.hashCode() - request.hashCode();
144 }
145
146 @Override
147 public boolean equals(Object obj) {
148 return (this == obj);
149 }
150
151 public Collection<StoreFile> getFiles() {
152 return this.filesToCompact;
153 }
154
155
156
157
158 public void setDescription(String regionName, String storeName) {
159 this.regionName = regionName;
160 this.storeName = storeName;
161 }
162
163
164 public long getSize() {
165 return totalSize;
166 }
167
168 public boolean isAllFiles() {
169 return this.isMajor == DisplayCompactionType.MAJOR
170 || this.isMajor == DisplayCompactionType.ALL_FILES;
171 }
172
173 public boolean isMajor() {
174 return this.isMajor == DisplayCompactionType.MAJOR;
175 }
176
177
178 public int getPriority() {
179 return priority;
180 }
181
182
183 public void setPriority(int p) {
184 this.priority = p;
185 }
186
187 public boolean isOffPeak() {
188 return this.isOffPeak;
189 }
190
191 public void setOffPeak(boolean value) {
192 this.isOffPeak = value;
193 }
194
195 public long getSelectionTime() {
196 return this.selectionTime;
197 }
198
199
200
201
202
203
204 public void setIsMajor(boolean isMajor, boolean isAllFiles) {
205 assert isAllFiles || !isMajor;
206 this.isMajor = !isAllFiles ? DisplayCompactionType.MINOR
207 : (isMajor ? DisplayCompactionType.MAJOR : DisplayCompactionType.ALL_FILES);
208 }
209
210
211
212
213
214
215 public void forceRetainDeleteMarkers() {
216 this.retainDeleteMarkers = Boolean.TRUE;
217 }
218
219
220
221
222 public boolean isRetainDeleteMarkers() {
223 return (this.retainDeleteMarkers != null) ? this.retainDeleteMarkers.booleanValue()
224 : !isAllFiles();
225 }
226
227 @Override
228 public String toString() {
229 String fsList = Joiner.on(", ").join(
230 Collections2.transform(Collections2.filter(
231 this.getFiles(),
232 new Predicate<StoreFile>() {
233 @Override
234 public boolean apply(StoreFile sf) {
235 return sf.getReader() != null;
236 }
237 }), new Function<StoreFile, String>() {
238 @Override
239 public String apply(StoreFile sf) {
240 return StringUtils.humanReadableInt(
241 (sf.getReader() == null) ? 0 : sf.getReader().length());
242 }
243 }));
244
245 return "regionName=" + regionName + ", storeName=" + storeName +
246 ", fileCount=" + this.getFiles().size() +
247 ", fileSize=" + StringUtils.humanReadableInt(totalSize) +
248 ((fsList.isEmpty()) ? "" : " (" + fsList + ")") +
249 ", priority=" + priority + ", time=" + timeInNanos;
250 }
251
252
253
254
255
256 private void recalculateSize() {
257 long sz = 0;
258 for (StoreFile sf : this.filesToCompact) {
259 Reader r = sf.getReader();
260 sz += r == null ? 0 : r.length();
261 }
262 this.totalSize = sz;
263 }
264 }
265