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.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 public static boolean isRescanNode(ZooKeeperWatcher zkw, String path) {
78 String prefix = getRescanNode(zkw);
79 if (path.length() <= prefix.length()) {
80 return false;
81 }
82 for (int i = 0; i < prefix.length(); i++) {
83 if (prefix.charAt(i) != path.charAt(i)) {
84 return false;
85 }
86 }
87 return true;
88 }
89
90 public static boolean isTaskPath(ZooKeeperWatcher zkw, String path) {
91 String dirname = path.substring(0, path.lastIndexOf('/'));
92 return dirname.equals(zkw.splitLogZNode);
93 }
94
95 public static Path getSplitLogDir(Path rootdir, String tmpname) {
96 return new Path(new Path(rootdir, HConstants.SPLIT_LOGDIR_NAME), tmpname);
97 }
98
99
100 public static String getSplitLogDirTmpComponent(final String worker, String file) {
101 return worker + "_" + ZKSplitLog.encode(file);
102 }
103
104 public static void markCorrupted(Path rootdir, String logFileName,
105 FileSystem fs) {
106 Path file = new Path(getSplitLogDir(rootdir, logFileName), "corrupt");
107 try {
108 fs.createNewFile(file);
109 } catch (IOException e) {
110 LOG.warn("Could not flag a log file as corrupted. Failed to create " +
111 file, e);
112 }
113 }
114
115 public static boolean isCorrupted(Path rootdir, String logFileName,
116 FileSystem fs) throws IOException {
117 Path file = new Path(getSplitLogDir(rootdir, logFileName), "corrupt");
118 boolean isCorrupt;
119 isCorrupt = fs.exists(file);
120 return isCorrupt;
121 }
122
123 }