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