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.classification.InterfaceAudience;
27 import org.apache.hadoop.classification.InterfaceStability;
28 import org.apache.hadoop.hbase.regionserver.Store;
29 import org.apache.hadoop.hbase.regionserver.StoreFile;
30 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
31 import org.apache.hadoop.util.StringUtils;
32
33 import com.google.common.base.Function;
34 import com.google.common.base.Joiner;
35 import com.google.common.base.Preconditions;
36 import com.google.common.base.Predicate;
37 import com.google.common.collect.Collections2;
38
39
40
41
42 @InterfaceAudience.LimitedPrivate({ "coprocessor" })
43 @InterfaceStability.Evolving
44 public class CompactionRequest implements Comparable<CompactionRequest> {
45 static final Log LOG = LogFactory.getLog(CompactionRequest.class);
46
47 private boolean isOffPeak = false;
48 private boolean isMajor = false;
49 private int priority = Store.NO_PRIORITY;
50 private Collection<StoreFile> filesToCompact;
51
52
53 private long selectionTime;
54
55 private Long timeInNanos;
56 private String regionName = "";
57 private String storeName = "";
58 private long totalSize = -1L;
59
60
61
62
63 public CompactionRequest() {
64 this.selectionTime = EnvironmentEdgeManager.currentTimeMillis();
65 this.timeInNanos = System.nanoTime();
66 }
67
68 public CompactionRequest(Collection<StoreFile> files) {
69 this();
70 Preconditions.checkNotNull(files);
71 this.filesToCompact = files;
72 recalculateSize();
73 }
74
75
76
77
78 public void beforeExecute() {}
79
80
81
82
83 public void afterExecute() {}
84
85
86
87
88
89
90
91
92 public CompactionRequest combineWith(CompactionRequest other) {
93 this.filesToCompact = new ArrayList<StoreFile>(other.getFiles());
94 this.isOffPeak = other.isOffPeak;
95 this.isMajor = other.isMajor;
96 this.priority = other.priority;
97 this.selectionTime = other.selectionTime;
98 this.timeInNanos = other.timeInNanos;
99 this.regionName = other.regionName;
100 this.storeName = other.storeName;
101 this.totalSize = other.totalSize;
102 return this;
103 }
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(sf.getReader().length());
207 }
208 }));
209
210 return "regionName=" + regionName + ", storeName=" + storeName +
211 ", fileCount=" + this.getFiles().size() +
212 ", fileSize=" + StringUtils.humanReadableInt(totalSize) +
213 ((fsList.isEmpty()) ? "" : " (" + fsList + ")") +
214 ", priority=" + priority + ", time=" + timeInNanos;
215 }
216
217
218
219
220
221 private void recalculateSize() {
222 long sz = 0;
223 for (StoreFile sf : this.filesToCompact) {
224 sz += sf.getReader().length();
225 }
226 this.totalSize = sz;
227 }
228 }
229