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