View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.zookeeper;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.NavigableSet;
25  import java.util.TreeSet;
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.hbase.Abortable;
31  import org.apache.hadoop.hbase.ServerName;
32  import org.apache.hadoop.hbase.master.ServerManager;
33  import org.apache.zookeeper.KeeperException;
34  
35  /**
36   * Tracks the online region servers via ZK.
37   *
38   * <p>Handling of new RSs checking in is done via RPC.  This class
39   * is only responsible for watching for expired nodes.  It handles
40   * listening for changes in the RS node list and watching each node.
41   *
42   * <p>If an RS node gets deleted, this automatically handles calling of
43   * {@link ServerManager#expireServer(ServerName)}
44   */
45  @InterfaceAudience.Private
46  public class RegionServerTracker extends ZooKeeperListener {
47    private static final Log LOG = LogFactory.getLog(RegionServerTracker.class);
48    private NavigableSet<ServerName> regionServers = new TreeSet<ServerName>();
49    private ServerManager serverManager;
50    private Abortable abortable;
51  
52    public RegionServerTracker(ZooKeeperWatcher watcher,
53        Abortable abortable, ServerManager serverManager) {
54      super(watcher);
55      this.abortable = abortable;
56      this.serverManager = serverManager;
57    }
58  
59    /**
60     * Starts the tracking of online RegionServers.
61     *
62     * <p>All RSs will be tracked after this method is called.
63     *
64     * @throws KeeperException
65     * @throws IOException
66     */
67    public void start() throws KeeperException, IOException {
68      watcher.registerListener(this);
69      List<String> servers =
70        ZKUtil.listChildrenAndWatchThem(watcher, watcher.rsZNode);
71      add(servers);
72    }
73  
74    private void add(final List<String> servers) throws IOException {
75      synchronized(this.regionServers) {
76        this.regionServers.clear();
77        for (String n: servers) {
78          ServerName sn = ServerName.parseServerName(ZKUtil.getNodeName(n));
79          this.regionServers.add(sn);
80        }
81      }
82    }
83  
84    private void remove(final ServerName sn) {
85      synchronized(this.regionServers) {
86        this.regionServers.remove(sn);
87      }
88    }
89  
90    @Override
91    public void nodeDeleted(String path) {
92      if (path.startsWith(watcher.rsZNode)) {
93        String serverName = ZKUtil.getNodeName(path);
94        LOG.info("RegionServer ephemeral node deleted, processing expiration [" +
95          serverName + "]");
96        ServerName sn = ServerName.parseServerName(serverName);
97        if (!serverManager.isServerOnline(sn)) {
98          LOG.warn(serverName.toString() + " is not online or isn't known to the master."+
99           "The latter could be caused by a DNS misconfiguration.");
100         return;
101       }
102       remove(sn);
103       this.serverManager.expireServer(sn);
104     }
105   }
106 
107   @Override
108   public void nodeChildrenChanged(String path) {
109     if (path.equals(watcher.rsZNode)) {
110       try {
111         List<String> servers =
112           ZKUtil.listChildrenAndWatchThem(watcher, watcher.rsZNode);
113         add(servers);
114       } catch (IOException e) {
115         abortable.abort("Unexpected zk exception getting RS nodes", e);
116       } catch (KeeperException e) {
117         abortable.abort("Unexpected zk exception getting RS nodes", e);
118       }
119     }
120   }
121 
122   /**
123    * Gets the online servers.
124    * @return list of online servers
125    */
126   public List<ServerName> getOnlineServers() {
127     synchronized (this.regionServers) {
128       return new ArrayList<ServerName>(this.regionServers);
129     }
130   }
131 }