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  
19  package org.apache.hadoop.hbase;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.hadoop.conf.Configuration;
24  import org.apache.hadoop.hbase.zookeeper.MasterAddressTracker;
25  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
26  
27  import java.io.BufferedReader;
28  import java.io.BufferedWriter;
29  import java.io.File;
30  import java.io.FileReader;
31  import java.io.FileWriter;
32  import java.io.IOException;
33  
34  /**
35   * <p>Contains a set of methods for the collaboration between the start/stop scripts and the
36   * servers. It allows to delete immediately the znode when the master or the regions server crashes.
37   * The region server / master writes a specific file when it starts / becomes main master. When they
38   * end properly, they delete the file.</p>
39   * <p>In the script, we check for the existence of these files when the program ends. If they still
40   * exist we conclude that the server crashed, likely without deleting their znode. To have a faster
41   * recovery we delete immediately the znode.</p>
42   * <p>The strategy depends on the server type. For a region server we store the znode path in the
43   * file, and use it to delete it. for a master, as the znode path constant whatever the server, we
44   * check its content to make sure that the backup server is not now in charge.</p>
45   */
46  public class ZNodeClearer {
47    public static final Log LOG = LogFactory.getLog(ZNodeClearer.class);
48  
49    private ZNodeClearer() {}
50  
51    /**
52     * Logs the errors without failing on exception.
53     */
54    public static void writeMyEphemeralNodeOnDisk(String fileContent) {
55      String fileName = ZNodeClearer.getMyEphemeralNodeFileName();
56  
57      if (fileName == null) {
58        LOG.warn("No filename given to save the znode used, it won't be saved " +
59            "(Environment variable HBASE_ZNODE_FILE is not set).");
60        return;
61      }
62  
63      FileWriter fstream;
64      try {
65        fstream = new FileWriter(fileName);
66      } catch (IOException e) {
67        LOG.warn("Can't write znode file "+fileName, e);
68        return;
69      }
70  
71      BufferedWriter out = new BufferedWriter(fstream);
72  
73      try {
74        try {
75          out.write(fileContent + "\n");
76        } finally {
77          try {
78            out.close();
79          } finally {
80            fstream.close();
81          }
82        }
83      } catch (IOException e) {
84        LOG.warn("Can't write znode file "+fileName, e);
85      }
86    }
87  
88    /**
89     * read the content of znode file, expects a single line.
90     */
91    public static String readMyEphemeralNodeOnDisk() throws IOException {
92      String fileName = getMyEphemeralNodeFileName();
93      if (fileName == null){
94        throw new IOException("No filename");
95      }
96      FileReader znodeFile = new FileReader(fileName);
97      BufferedReader br = new BufferedReader(znodeFile);
98      String file_content = br.readLine();
99      br.close();
100     return file_content;
101   }
102 
103   /**
104    * Get the name of the file used to store the znode contents
105    */
106   public static String getMyEphemeralNodeFileName() {
107     return System.getenv().get("HBASE_ZNODE_FILE");
108   }
109 
110   /**
111    *  delete the znode file
112    */
113   public static void deleteMyEphemeralNodeOnDisk() {
114     String fileName = getMyEphemeralNodeFileName();
115 
116     if (fileName != null) {
117       new File(fileName).delete();
118     }
119   }
120 
121   /**
122    * Delete the master znode if its content (ServerName string) is the same
123    *  as the one in the znode file. (env: HBASE_ZNODE_FILE).
124    * @return true on successful deletion, false otherwise.
125    */
126   public static boolean clear(Configuration conf) {
127     Configuration tempConf = new Configuration(conf);
128     tempConf.setInt("zookeeper.recovery.retry", 0);
129 
130     ZooKeeperWatcher zkw;
131     try {
132       zkw = new ZooKeeperWatcher(tempConf, "clean znode for master",
133           new Abortable() {
134             @Override public void abort(String why, Throwable e) {}
135             @Override public boolean isAborted() { return false; }
136           });
137     } catch (IOException e) {
138       LOG.warn("Can't connect to zookeeper to read the master znode", e);
139       return false;
140     }
141 
142     String znodeFileContent;
143     try {
144       znodeFileContent = ZNodeClearer.readMyEphemeralNodeOnDisk();
145     } catch (IOException e) {
146       LOG.warn("Can't read the content of the znode file", e);
147       return false;
148     }
149 
150     return MasterAddressTracker.deleteIfEquals(zkw, znodeFileContent);
151   }
152 }