001/*
002  * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.zookeeper;
019
020import java.io.IOException;
021import java.io.UnsupportedEncodingException;
022import java.net.URLDecoder;
023import java.net.URLEncoder;
024
025import org.apache.hadoop.fs.FileSystem;
026import org.apache.hadoop.fs.Path;
027import org.apache.hadoop.hbase.HConstants;
028import org.apache.yetus.audience.InterfaceAudience;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032/**
033 * Common methods and attributes used by SplitLogManager and SplitLogWorker running distributed
034 * splitting of WAL logs.
035 * @deprecated  since 2.4.0 and 3.0.0 replaced by procedure-based WAL splitting; see
036 *    SplitWALManager.
037 */
038@Deprecated
039@InterfaceAudience.Private
040public final class ZKSplitLog {
041  private static final Logger LOG = LoggerFactory.getLogger(ZKSplitLog.class);
042
043  private ZKSplitLog() {
044  }
045
046  /**
047   * Gets the full path node name for the log file being split.
048   * This method will url encode the filename.
049   * @param zkw zk reference
050   * @param filename log file name (only the basename)
051   */
052  public static String getEncodedNodeName(ZKWatcher zkw, String filename) {
053    return ZNodePaths.joinZNode(zkw.getZNodePaths().splitLogZNode, encode(filename));
054  }
055
056  public static String getFileName(String node) {
057    String basename = node.substring(node.lastIndexOf('/') + 1);
058    return decode(basename);
059  }
060
061  static String encode(String s) {
062    try {
063      return URLEncoder.encode(s, "UTF-8");
064    } catch (UnsupportedEncodingException e) {
065      throw new RuntimeException("URLENCODER doesn't support UTF-8");
066    }
067  }
068
069  static String decode(String s) {
070    try {
071      return URLDecoder.decode(s, "UTF-8");
072    } catch (UnsupportedEncodingException e) {
073      throw new RuntimeException("URLDecoder doesn't support UTF-8");
074    }
075  }
076
077  public static String getRescanNode(ZKWatcher zkw) {
078    return ZNodePaths.joinZNode(zkw.getZNodePaths().splitLogZNode, "RESCAN");
079  }
080
081  /**
082   * @param name the last part in path
083   * @return whether the node name represents a rescan node
084   */
085  public static boolean isRescanNode(String name) {
086    return name.startsWith("RESCAN");
087  }
088
089  /**
090   * Checks if the given path represents a rescan node.
091   *
092   * @param zkw reference to the {@link ZKWatcher} which also contains configuration and constants
093   * @param path the absolute path, starts with '/'
094   * @return whether the path represents a rescan node
095   */
096  public static boolean isRescanNode(ZKWatcher zkw, String path) {
097    String prefix = getRescanNode(zkw);
098    if (path.length() <= prefix.length()) {
099      return false;
100    }
101    for (int i = 0; i < prefix.length(); i++) {
102      if (prefix.charAt(i) != path.charAt(i)) {
103        return false;
104      }
105    }
106    return true;
107  }
108
109  public static Path getSplitLogDir(Path rootdir, String tmpname) {
110    return new Path(new Path(rootdir, HConstants.SPLIT_LOGDIR_NAME), tmpname);
111  }
112
113  public static void markCorrupted(Path rootdir, String logFileName,
114      FileSystem fs) {
115    Path file = new Path(getSplitLogDir(rootdir, logFileName), "corrupt");
116    try {
117      fs.createNewFile(file);
118    } catch (IOException e) {
119      LOG.warn("Could not flag a log file as corrupted. Failed to create " +
120          file, e);
121    }
122  }
123
124  public static boolean isCorrupted(Path rootdir, String logFileName,
125      FileSystem fs) throws IOException {
126    Path file = new Path(getSplitLogDir(rootdir, logFileName), "corrupt");
127    boolean isCorrupt;
128    isCorrupt = fs.exists(file);
129    return isCorrupt;
130  }
131}