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
63
64
65 public CompactionRequest() {
66 this.selectionTime = EnvironmentEdgeManager.currentTime();
67 this.timeInNanos = System.nanoTime();
68 }
69
70 public CompactionRequest(Collection<StoreFile> files) {
71 this();
72 Preconditions.checkNotNull(files);
73 this.filesToCompact = files;
74 recalculateSize();
75 }
76
77 public void updateFiles(Collection<StoreFile> files) {
78 this.filesToCompact = files;
79 }
80
81
82
83
84 public void beforeExecute() {}
85
86
87
88
89 public void afterExecute() {}
90
91
92
93
94
95
96
97
98 public CompactionRequest combineWith(CompactionRequest other) {
99 this.filesToCompact = new ArrayList<StoreFile>(other.getFiles());
100 this.isOffPeak = other.isOffPeak;
101 this.isMajor = other.isMajor;
102 this.priority = other.priority;
103 this.selectionTime = other.selectionTime;
104 this.timeInNanos = other.timeInNanos;
105 this.regionName = other.regionName;
106 this.storeName = other.storeName;
107 this.totalSize = other.totalSize;
108 recalculateSize();
109 return this;
110 }
111
112
113
114
115
116
117
118
119
120
121
122 @Override
123 public int compareTo(CompactionRequest request) {
124
125 if (this.equals(request)) {
126 return 0;
127 }
128 int compareVal;
129
130 compareVal = priority - request.priority;
131 if (compareVal != 0) {
132 return compareVal;
133 }
134
135 compareVal = timeInNanos.compareTo(request.timeInNanos);
136 if (compareVal != 0) {
137 return compareVal;
138 }
139
140
141 return this.hashCode() - request.hashCode();
142 }
143
144 @Override
145 public boolean equals(Object obj) {
146 return (this == obj);
147 }
148
149 public Collection<StoreFile> getFiles() {
150 return this.filesToCompact;
151 }
152
153
154
155
156 public void setDescription(String regionName, String storeName) {
157 this.regionName = regionName;
158 this.storeName = storeName;
159 }
160
161
162 public long getSize() {
163 return totalSize;
164 }
165
166 public boolean isAllFiles() {
167 return this.isMajor == DisplayCompactionType.MAJOR
168 || this.isMajor == DisplayCompactionType.ALL_FILES;
169 }
170
171 public boolean isMajor() {
172 return this.isMajor == DisplayCompactionType.MAJOR;
173 }
174
175
176 public int getPriority() {
177 return priority;
178 }
179
180
181 public void setPriority(int p) {
182 this.priority = p;
183 }
184
185 public boolean isOffPeak() {
186 return this.isOffPeak;
187 }
188
189 public void setOffPeak(boolean value) {
190 this.isOffPeak = value;
191 }
192
193 public long getSelectionTime() {
194 return this.selectionTime;
195 }
196
197
198
199
200
201
202 public void setIsMajor(boolean isMajor, boolean isAllFiles) {
203 assert isAllFiles || !isMajor;
204 this.isMajor = !isAllFiles ? DisplayCompactionType.MINOR
205 : (isMajor ? DisplayCompactionType.MAJOR : DisplayCompactionType.ALL_FILES);
206 }
207
208 @Override
209 public String toString() {
210 String fsList = Joiner.on(", ").join(
211 Collections2.transform(Collections2.filter(
212 this.getFiles(),
213 new Predicate<StoreFile>() {
214 @Override
215 public boolean apply(StoreFile sf) {
216 return sf.getReader() != null;
217 }
218 }), new Function<StoreFile, String>() {
219 @Override
220 public String apply(StoreFile sf) {
221 return StringUtils.humanReadableInt(
222 (sf.getReader() == null) ? 0 : sf.getReader().length());
223 }
224 }));
225
226 return "regionName=" + regionName + ", storeName=" + storeName +
227 ", fileCount=" + this.getFiles().size() +
228 ", fileSize=" + StringUtils.humanReadableInt(totalSize) +
229 ((fsList.isEmpty()) ? "" : " (" + fsList + ")") +
230 ", priority=" + priority + ", time=" + timeInNanos;
231 }
232
233
234
235
236
237 private void recalculateSize() {
238 long sz = 0;
239 for (StoreFile sf : this.filesToCompact) {
240 Reader r = sf.getReader();
241 sz += r == null ? 0 : r.length();
242 }
243 this.totalSize = sz;
244 }
245 }
246