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 @Override
116 public int compareTo(CompactionRequest request) {
117
118 if (this.equals(request)) {
119 return 0;
120 }
121 int compareVal;
122
123 compareVal = priority - request.priority;
124 if (compareVal != 0) {
125 return compareVal;
126 }
127
128 compareVal = timeInNanos.compareTo(request.timeInNanos);
129 if (compareVal != 0) {
130 return compareVal;
131 }
132
133
134 return this.hashCode() - request.hashCode();
135 }
136
137 @Override
138 public boolean equals(Object obj) {
139 return (this == obj);
140 }
141
142 public Collection<StoreFile> getFiles() {
143 return this.filesToCompact;
144 }
145
146
147
148
149 public void setDescription(String regionName, String storeName) {
150 this.regionName = regionName;
151 this.storeName = storeName;
152 }
153
154
155 public long getSize() {
156 return totalSize;
157 }
158
159 public boolean isMajor() {
160 return this.isMajor;
161 }
162
163
164 public int getPriority() {
165 return priority;
166 }
167
168
169 public void setPriority(int p) {
170 this.priority = p;
171 }
172
173 public boolean isOffPeak() {
174 return this.isOffPeak;
175 }
176
177 public void setOffPeak(boolean value) {
178 this.isOffPeak = value;
179 }
180
181 public long getSelectionTime() {
182 return this.selectionTime;
183 }
184
185
186
187
188
189
190 public void setIsMajor(boolean isMajor) {
191 this.isMajor = isMajor;
192 }
193
194 @Override
195 public String toString() {
196 String fsList = Joiner.on(", ").join(
197 Collections2.transform(Collections2.filter(
198 this.getFiles(),
199 new Predicate<StoreFile>() {
200 public boolean apply(StoreFile sf) {
201 return sf.getReader() != null;
202 }
203 }), new Function<StoreFile, String>() {
204 public String apply(StoreFile sf) {
205 return StringUtils.humanReadableInt(sf.getReader().length());
206 }
207 }));
208
209 return "regionName=" + regionName + ", storeName=" + storeName +
210 ", fileCount=" + this.getFiles().size() +
211 ", fileSize=" + StringUtils.humanReadableInt(totalSize) +
212 ((fsList.isEmpty()) ? "" : " (" + fsList + ")") +
213 ", priority=" + priority + ", time=" + timeInNanos;
214 }
215
216
217
218
219
220 private void recalculateSize() {
221 long sz = 0;
222 for (StoreFile sf : this.filesToCompact) {
223 sz += sf.getReader().length();
224 }
225 this.totalSize = sz;
226 }
227 }
228