1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.zookeeper;
19
20 import java.io.IOException;
21 import java.io.UnsupportedEncodingException;
22 import java.net.URLDecoder;
23 import java.net.URLEncoder;
24 import java.util.ArrayList;
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.classification.InterfaceAudience;
30 import org.apache.hadoop.fs.FileSystem;
31 import org.apache.hadoop.fs.Path;
32 import org.apache.hadoop.hbase.HConstants;
33 import org.apache.hadoop.hbase.master.SplitLogManager;
34 import org.apache.hadoop.hbase.regionserver.SplitLogWorker;
35
36
37
38
39
40 @InterfaceAudience.Private
41 public class ZKSplitLog {
42 private static final Log LOG = LogFactory.getLog(ZKSplitLog.class);
43
44
45
46
47
48
49
50 public static String getEncodedNodeName(ZooKeeperWatcher zkw, String filename) {
51 return ZKUtil.joinZNode(zkw.splitLogZNode, encode(filename));
52 }
53
54 public static String getFileName(String node) {
55 String basename = node.substring(node.lastIndexOf('/') + 1);
56 return decode(basename);
57 }
58
59 static String encode(String s) {
60 try {
61 return URLEncoder.encode(s, "UTF-8");
62 } catch (UnsupportedEncodingException e) {
63 throw new RuntimeException("URLENCODER doesn't support UTF-8");
64 }
65 }
66
67 static String decode(String s) {
68 try {
69 return URLDecoder.decode(s, "UTF-8");
70 } catch (UnsupportedEncodingException e) {
71 throw new RuntimeException("URLDecoder doesn't support UTF-8");
72 }
73 }
74
75 public static String getRescanNode(ZooKeeperWatcher zkw) {
76 return ZKUtil.joinZNode(zkw.splitLogZNode, "RESCAN");
77 }
78
79 public static boolean isRescanNode(ZooKeeperWatcher zkw, String path) {
80 String prefix = getRescanNode(zkw);
81 if (path.length() <= prefix.length()) {
82 return false;
83 }
84 for (int i = 0; i < prefix.length(); i++) {
85 if (prefix.charAt(i) != path.charAt(i)) {
86 return false;
87 }
88 }
89 return true;
90 }
91
92 public static boolean isTaskPath(ZooKeeperWatcher zkw, String path) {
93 String dirname = path.substring(0, path.lastIndexOf('/'));
94 return dirname.equals(zkw.splitLogZNode);
95 }
96
97 public static Path getSplitLogDir(Path rootdir, String tmpname) {
98 return new Path(new Path(rootdir, HConstants.SPLIT_LOGDIR_NAME), tmpname);
99 }
100
101
102 public static String getSplitLogDirTmpComponent(final String worker, String file) {
103 return worker + "_" + ZKSplitLog.encode(file);
104 }
105
106 public static void markCorrupted(Path rootdir, String logFileName,
107 FileSystem fs) {
108 Path file = new Path(getSplitLogDir(rootdir, logFileName), "corrupt");
109 try {
110 fs.createNewFile(file);
111 } catch (IOException e) {
112 LOG.warn("Could not flag a log file as corrupted. Failed to create " +
113 file, e);
114 }
115 }
116
117 public static boolean isCorrupted(Path rootdir, String logFileName,
118 FileSystem fs) throws IOException {
119 Path file = new Path(getSplitLogDir(rootdir, logFileName), "corrupt");
120 boolean isCorrupt;
121 isCorrupt = fs.exists(file);
122 return isCorrupt;
123 }
124
125 }