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      if (fileName == null) {
57        LOG.warn("Environment variable HBASE_ZNODE_FILE not set; znodes will not be cleared " +
58          "on crash by start scripts (Longer MTTR!)");
59        return;
60      }
61  
62      FileWriter fstream;
63      try {
64        fstream = new FileWriter(fileName);
65      } catch (IOException e) {
66        LOG.warn("Can't write znode file "+fileName, e);
67        return;
68      }
69  
70      BufferedWriter out = new BufferedWriter(fstream);
71  
72      try {
73        try {
74          out.write(fileContent + "\n");
75        } finally {
76          try {
77            out.close();
78          } finally {
79            fstream.close();
80          }
81        }
82      } catch (IOException e) {
83        LOG.warn("Can't write znode file "+fileName, e);
84      }
85    }
86  
87    /**
88     * read the content of znode file, expects a single line.
89     */
90    public static String readMyEphemeralNodeOnDisk() throws IOException {
91      String fileName = getMyEphemeralNodeFileName();
92      if (fileName == null){
93        throw new IOException("No filename");
94      }
95      FileReader znodeFile = new FileReader(fileName);
96      BufferedReader br = new BufferedReader(znodeFile);
97      String file_content = br.readLine();
98      br.close();
99      return file_content;
100   }
101 
102   /**
103    * Get the name of the file used to store the znode contents
104    */
105   public static String getMyEphemeralNodeFileName() {
106     return System.getenv().get("HBASE_ZNODE_FILE");
107   }
108 
109   /**
110    *  delete the znode file
111    */
112   public static void deleteMyEphemeralNodeOnDisk() {
113     String fileName = getMyEphemeralNodeFileName();
114 
115     if (fileName != null) {
116       new File(fileName).delete();
117     }
118   }
119 
120   /**
121    * Delete the master znode if its content (ServerName string) is the same
122    *  as the one in the znode file. (env: HBASE_ZNODE_FILE).
123    * @return true on successful deletion, false otherwise.
124    */
125   public static boolean clear(Configuration conf) {
126     Configuration tempConf = new Configuration(conf);
127     tempConf.setInt("zookeeper.recovery.retry", 0);
128 
129     ZooKeeperWatcher zkw;
130     try {
131       zkw = new ZooKeeperWatcher(tempConf, "clean znode for master",
132           new Abortable() {
133             @Override public void abort(String why, Throwable e) {}
134             @Override public boolean isAborted() { return false; }
135           });
136     } catch (IOException e) {
137       LOG.warn("Can't connect to zookeeper to read the master znode", e);
138       return false;
139     }
140 
141     String znodeFileContent;
142     try {
143       znodeFileContent = ZNodeClearer.readMyEphemeralNodeOnDisk();
144     } catch (IOException e) {
145       LOG.warn("Can't read the content of the znode file", e);
146       return false;
147     }
148 
149     return MasterAddressTracker.deleteIfEquals(zkw, znodeFileContent);
150   }
151 }