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