View Javadoc

1   /**
2     * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * Common methods and attributes used by {@link SplitLogManager} and {@link SplitLogWorker}
36   * running distributed splitting of WAL logs.
37   */
38  @InterfaceAudience.Private
39  public class ZKSplitLog {
40    private static final Log LOG = LogFactory.getLog(ZKSplitLog.class);
41  
42    /**
43     * Gets the full path node name for the log file being split.
44     * This method will url encode the filename.
45     * @param zkw zk reference
46     * @param filename log file name (only the basename)
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 }