1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.regionserver.compactions;
21
22 import java.util.ArrayList;
23 import java.util.Calendar;
24 import java.util.GregorianCalendar;
25 import java.util.List;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.conf.Configuration;
30 import org.apache.hadoop.hbase.regionserver.StoreFile;
31
32 public class CompactSelection {
33 private static final long serialVersionUID = 1L;
34 static final Log LOG = LogFactory.getLog(CompactSelection.class);
35
36
37 List<StoreFile> filesToCompact = new ArrayList<StoreFile>();
38
39
40
41
42
43 static long numOutstandingOffPeakCompactions = 0;
44
45
46
47
48 private final static Object compactionCountLock = new Object();
49
50
51 Configuration conf;
52
53 boolean isOffPeakCompaction = false;
54
55
56 private double compactRatio;
57
58 private double compactRatioOffPeak;
59
60 private int offPeakStartHour = -1;
61
62 private int offPeakEndHour = -1;
63
64 public CompactSelection(Configuration conf, List<StoreFile> filesToCompact) {
65 this.filesToCompact = filesToCompact;
66 this.conf = conf;
67 this.compactRatio = conf.getFloat("hbase.hstore.compaction.ratio", 1.2F);
68 this.compactRatioOffPeak = conf.getFloat("hbase.hstore.compaction.ratio.offpeak", 5.0F);
69
70
71 this.offPeakStartHour = conf.getInt("hbase.offpeak.start.hour", -1);
72 this.offPeakEndHour = conf.getInt("hbase.offpeak.end.hour", -1);
73 if (!isValidHour(this.offPeakStartHour) || !isValidHour(this.offPeakEndHour)) {
74 if (!(this.offPeakStartHour == -1 && this.offPeakEndHour == -1)) {
75 LOG.warn("Invalid start/end hour for peak hour : start = " +
76 this.offPeakStartHour + " end = " + this.offPeakEndHour +
77 ". Valid numbers are [0-23]");
78 }
79 this.offPeakStartHour = this.offPeakEndHour = -1;
80 }
81 }
82
83
84
85
86
87
88
89
90
91
92 public CompactSelection selectExpiredStoreFilesToCompact(
93 long maxExpiredTimeStamp) {
94 if (filesToCompact == null || filesToCompact.size() == 0)
95 return null;
96 ArrayList<StoreFile> expiredStoreFiles = null;
97 boolean hasExpiredStoreFiles = false;
98 CompactSelection expiredSFSelection = null;
99
100 for (StoreFile storeFile : this.filesToCompact) {
101 if (storeFile.getReader().getMaxTimestamp() < maxExpiredTimeStamp) {
102 LOG.info("Deleting the expired store file by compaction: "
103 + storeFile.getPath() + " whose maxTimeStamp is "
104 + storeFile.getReader().getMaxTimestamp()
105 + " while the max expired timestamp is " + maxExpiredTimeStamp);
106 if (!hasExpiredStoreFiles) {
107 expiredStoreFiles = new ArrayList<StoreFile>();
108 hasExpiredStoreFiles = true;
109 }
110 expiredStoreFiles.add(storeFile);
111 }
112 }
113
114 if (hasExpiredStoreFiles) {
115 expiredSFSelection = new CompactSelection(conf, expiredStoreFiles);
116 }
117 return expiredSFSelection;
118 }
119
120
121
122
123
124
125
126
127
128
129 public double getCompactSelectionRatio() {
130 double r = this.compactRatio;
131 synchronized(compactionCountLock) {
132 if (isOffPeakHour() && numOutstandingOffPeakCompactions == 0) {
133 r = this.compactRatioOffPeak;
134 numOutstandingOffPeakCompactions++;
135 isOffPeakCompaction = true;
136 }
137 }
138 if(isOffPeakCompaction) {
139 LOG.info("Running an off-peak compaction, selection ratio = " +
140 compactRatioOffPeak + ", numOutstandingOffPeakCompactions is now " +
141 numOutstandingOffPeakCompactions);
142 }
143 return r;
144 }
145
146
147
148
149
150 public void finishRequest() {
151 if (isOffPeakCompaction) {
152 synchronized(compactionCountLock) {
153 numOutstandingOffPeakCompactions--;
154 isOffPeakCompaction = false;
155 }
156 LOG.info("Compaction done, numOutstandingOffPeakCompactions is now " +
157 numOutstandingOffPeakCompactions);
158 }
159 }
160
161 public List<StoreFile> getFilesToCompact() {
162 return filesToCompact;
163 }
164
165
166
167
168
169 public void emptyFileList() {
170 filesToCompact.clear();
171 if (isOffPeakCompaction) {
172 synchronized(compactionCountLock) {
173
174 numOutstandingOffPeakCompactions--;
175 isOffPeakCompaction = false;
176 }
177 LOG.info("Nothing to compact, numOutstandingOffPeakCompactions is now " +
178 numOutstandingOffPeakCompactions);
179 }
180 }
181
182 public boolean isOffPeakCompaction() {
183 return this.isOffPeakCompaction;
184 }
185
186 private boolean isOffPeakHour() {
187 int currentHour = (new GregorianCalendar()).get(Calendar.HOUR_OF_DAY);
188
189 if (this.offPeakStartHour == this.offPeakEndHour) {
190 return false;
191 }
192 if (this.offPeakStartHour < this.offPeakEndHour) {
193 return (currentHour >= this.offPeakStartHour && currentHour < this.offPeakEndHour);
194 }
195 return (currentHour >= this.offPeakStartHour || currentHour < this.offPeakEndHour);
196 }
197
198 public CompactSelection subList(int start, int end) {
199 throw new UnsupportedOperationException();
200 }
201
202 public CompactSelection getSubList(int start, int end) {
203 filesToCompact = filesToCompact.subList(start, end);
204 return this;
205 }
206
207 public void clearSubList(int start, int end) {
208 filesToCompact.subList(start, end).clear();
209 }
210
211 private boolean isValidHour(int hour) {
212 return (hour >= 0 && hour <= 23);
213 }
214 }