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  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   * Common methods and attributes used by {@link SplitLogManager} and {@link SplitLogWorker}
38   * running distributed splitting of WAL logs.
39   */
40  @InterfaceAudience.Private
41  public class ZKSplitLog {
42    private static final Log LOG = LogFactory.getLog(ZKSplitLog.class);
43  
44    /**
45     * Gets the full path node name for the log file being split.
46     * This method will url encode the filename.
47     * @param zkw zk reference
48     * @param filename log file name (only the basename)
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 }