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.wal;
21
22 import java.io.IOException;
23 import java.util.NavigableSet;
24 import java.util.TreeSet;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.hadoop.conf.Configuration;
31 import org.apache.hadoop.fs.FileSystem;
32 import org.apache.hadoop.fs.FileStatus;
33 import org.apache.hadoop.fs.Path;
34 import org.apache.hadoop.fs.PathFilter;
35 import org.apache.hadoop.hbase.ServerName;
36 import org.apache.hadoop.hbase.util.FSUtils;
37
38 import org.apache.hadoop.hbase.HConstants;
39 import org.apache.hadoop.hbase.util.Bytes;
40
41 public class HLogUtil {
42 static final Log LOG = LogFactory.getLog(HLogUtil.class);
43
44
45
46
47
48 public static boolean isMetaFamily(byte[] family) {
49 return Bytes.equals(HLog.METAFAMILY, family);
50 }
51
52 @SuppressWarnings("unchecked")
53 public static Class<? extends HLogKey> getKeyClass(Configuration conf) {
54 return (Class<? extends HLogKey>) conf.getClass(
55 "hbase.regionserver.hlog.keyclass", HLogKey.class);
56 }
57
58 public static HLogKey newKey(Configuration conf) throws IOException {
59 Class<? extends HLogKey> keyClass = getKeyClass(conf);
60 try {
61 return keyClass.newInstance();
62 } catch (InstantiationException e) {
63 throw new IOException("cannot create hlog key");
64 } catch (IllegalAccessException e) {
65 throw new IOException("cannot create hlog key");
66 }
67 }
68
69
70
71
72 private static final Pattern pattern =
73 Pattern.compile(".*\\.\\d*("+HLog.META_HLOG_FILE_EXTN+")*");
74
75
76
77
78
79
80
81 public static boolean validateHLogFilename(String filename) {
82 return pattern.matcher(filename).matches();
83 }
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141 public static String getHLogDirectoryName(final String serverName) {
142 StringBuilder dirName = new StringBuilder(HConstants.HREGION_LOGDIR_NAME);
143 dirName.append("/");
144 dirName.append(serverName);
145 return dirName.toString();
146 }
147
148
149
150
151
152
153
154 public static Path getRegionDirRecoveredEditsDir(final Path regiondir) {
155 return new Path(regiondir, HConstants.RECOVERED_EDITS_DIR);
156 }
157
158
159
160
161
162
163
164
165
166
167 public static Path moveAsideBadEditsFile(final FileSystem fs, final Path edits)
168 throws IOException {
169 Path moveAsideName = new Path(edits.getParent(), edits.getName() + "."
170 + System.currentTimeMillis());
171 if (!fs.rename(edits, moveAsideName)) {
172 LOG.warn("Rename failed from " + edits + " to " + moveAsideName);
173 }
174 return moveAsideName;
175 }
176
177
178
179
180
181
182
183
184
185 public static ServerName getServerNameFromHLogDirectoryName(
186 Configuration conf, String path) throws IOException {
187 if (path == null
188 || path.length() <= HConstants.HREGION_LOGDIR_NAME.length()) {
189 return null;
190 }
191
192 if (conf == null) {
193 throw new IllegalArgumentException("parameter conf must be set");
194 }
195
196 final String rootDir = conf.get(HConstants.HBASE_DIR);
197 if (rootDir == null || rootDir.isEmpty()) {
198 throw new IllegalArgumentException(HConstants.HBASE_DIR
199 + " key not found in conf.");
200 }
201
202 final StringBuilder startPathSB = new StringBuilder(rootDir);
203 if (!rootDir.endsWith("/"))
204 startPathSB.append('/');
205 startPathSB.append(HConstants.HREGION_LOGDIR_NAME);
206 if (!HConstants.HREGION_LOGDIR_NAME.endsWith("/"))
207 startPathSB.append('/');
208 final String startPath = startPathSB.toString();
209
210 String fullPath;
211 try {
212 fullPath = FileSystem.get(conf).makeQualified(new Path(path)).toString();
213 } catch (IllegalArgumentException e) {
214 LOG.info("Call to makeQualified failed on " + path + " " + e.getMessage());
215 return null;
216 }
217
218 if (!fullPath.startsWith(startPath)) {
219 return null;
220 }
221
222 final String serverNameAndFile = fullPath.substring(startPath.length());
223
224 if (serverNameAndFile.indexOf('/') < "a,0,0".length()) {
225
226 return null;
227 }
228
229 final String serverName = serverNameAndFile.substring(0,
230 serverNameAndFile.indexOf('/') - 1);
231
232 if (!ServerName.isFullServerName(serverName)) {
233 return null;
234 }
235
236 return ServerName.parseServerName(serverName);
237 }
238
239
240
241
242
243
244
245
246
247
248 public static NavigableSet<Path> getSplitEditFilesSorted(final FileSystem fs,
249 final Path regiondir) throws IOException {
250 NavigableSet<Path> filesSorted = new TreeSet<Path>();
251 Path editsdir = HLogUtil.getRegionDirRecoveredEditsDir(regiondir);
252 if (!fs.exists(editsdir))
253 return filesSorted;
254 FileStatus[] files = FSUtils.listStatus(fs, editsdir, new PathFilter() {
255 @Override
256 public boolean accept(Path p) {
257 boolean result = false;
258 try {
259
260
261
262
263 Matcher m = HLog.EDITFILES_NAME_PATTERN.matcher(p.getName());
264 result = fs.isFile(p) && m.matches();
265
266
267 if (p.getName().endsWith(HLog.RECOVERED_LOG_TMPFILE_SUFFIX)) {
268 result = false;
269 }
270 } catch (IOException e) {
271 LOG.warn("Failed isFile check on " + p);
272 }
273 return result;
274 }
275 });
276 if (files == null)
277 return filesSorted;
278 for (FileStatus status : files) {
279 filesSorted.add(status.getPath());
280 }
281 return filesSorted;
282 }
283
284 public static boolean isMetaFile(Path p) {
285 if (p.getName().endsWith(HLog.META_HLOG_FILE_EXTN)) {
286 return true;
287 }
288 return false;
289 }
290 }