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 java.io.BufferedReader;
22  import java.io.BufferedWriter;
23  import java.io.File;
24  import java.io.FileNotFoundException;
25  import java.io.FileReader;
26  import java.io.FileWriter;
27  import java.io.IOException;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.hadoop.conf.Configuration;
32  import org.apache.hadoop.hbase.zookeeper.MasterAddressTracker;
33  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
34  
35  /**
36   * <p>Contains a set of methods for the collaboration between the start/stop scripts and the
37   * servers. It allows to delete immediately the znode when the master or the regions server crashes.
38   * The region server / master writes a specific file when it starts / becomes main master. When they
39   * end properly, they delete the file.</p>
40   * <p>In the script, we check for the existence of these files when the program ends. If they still
41   * exist we conclude that the server crashed, likely without deleting their znode. To have a faster
42   * recovery we delete immediately the znode.</p>
43   * <p>The strategy depends on the server type. For a region server we store the znode path in the
44   * file, and use it to delete it. for a master, as the znode path constant whatever the server, we
45   * check its content to make sure that the backup server is not now in charge.</p>
46   */
47  public class ZNodeClearer {
48    public static final Log LOG = LogFactory.getLog(ZNodeClearer.class);
49  
50    private ZNodeClearer() {}
51  
52    /**
53     * Logs the errors without failing on exception.
54     */
55    public static void writeMyEphemeralNodeOnDisk(String fileContent) {
56      String fileName = ZNodeClearer.getMyEphemeralNodeFileName();
57      if (fileName == null) {
58        LOG.warn("Environment variable HBASE_ZNODE_FILE not set; znodes will not be cleared " +
59          "on crash by start scripts (Longer MTTR!)");
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 FileNotFoundException("No filename; set environment variable HBASE_ZNODE_FILE");
95      }
96      FileReader znodeFile = new FileReader(fileName);
97      BufferedReader br = null;
98      try {
99        br = new BufferedReader(znodeFile);
100       String file_content = br.readLine();
101       return file_content;
102     } finally {
103       if (br != null) br.close();
104     }
105   }
106 
107   /**
108    * Get the name of the file used to store the znode contents
109    */
110   public static String getMyEphemeralNodeFileName() {
111     return System.getenv().get("HBASE_ZNODE_FILE");
112   }
113 
114   /**
115    *  delete the znode file
116    */
117   public static void deleteMyEphemeralNodeOnDisk() {
118     String fileName = getMyEphemeralNodeFileName();
119 
120     if (fileName != null) {
121       new File(fileName).delete();
122     }
123   }
124 
125   /**
126    * Delete the master znode if its content (ServerName string) is the same
127    *  as the one in the znode file. (env: HBASE_ZNODE_FILE).
128    * @return true on successful deletion, false otherwise.
129    */
130   public static boolean clear(Configuration conf) {
131     Configuration tempConf = new Configuration(conf);
132     tempConf.setInt("zookeeper.recovery.retry", 0);
133 
134     ZooKeeperWatcher zkw;
135     try {
136       zkw = new ZooKeeperWatcher(tempConf, "clean znode for master",
137           new Abortable() {
138             @Override public void abort(String why, Throwable e) {}
139             @Override public boolean isAborted() { return false; }
140           });
141     } catch (IOException e) {
142       LOG.warn("Can't connect to zookeeper to read the master znode", e);
143       return false;
144     }
145 
146     String znodeFileContent;
147     try {
148       znodeFileContent = ZNodeClearer.readMyEphemeralNodeOnDisk();
149       return MasterAddressTracker.deleteIfEquals(zkw, znodeFileContent);
150     } catch (FileNotFoundException fnfe) {
151       // If no file, just keep going -- return success.
152       LOG.warn("Can't find the znode file; presume non-fatal", fnfe);
153       return true;
154     } catch (IOException e) {
155       LOG.warn("Can't read the content of the znode file", e);
156       return false;
157     } finally {
158       zkw.close();
159     }
160   }
161 }