View Javadoc

1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.zookeeper;
21  
22  import java.util.List;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.hbase.Abortable;
27  import org.apache.hadoop.hbase.HServerAddress;
28  import org.apache.hadoop.hbase.HServerInfo;
29  import org.apache.hadoop.hbase.master.ServerManager;
30  import org.apache.zookeeper.KeeperException;
31  
32  /**
33   * Tracks the online region servers via ZK.
34   *
35   * <p>Handling of new RSs checking in is done via RPC.  This class
36   * is only responsible for watching for expired nodes.  It handles
37   * listening for changes in the RS node list and watching each node.
38   *
39   * <p>If an RS node gets deleted, this automatically handles calling of
40   * {@link ServerManager#expireServer(org.apache.hadoop.hbase.HServerInfo)}.
41   */
42  public class RegionServerTracker extends ZooKeeperListener {
43    private static final Log LOG = LogFactory.getLog(RegionServerTracker.class);
44  
45    private ServerManager serverManager;
46    private Abortable abortable;
47  
48    public RegionServerTracker(ZooKeeperWatcher watcher,
49        Abortable abortable, ServerManager serverManager) {
50      super(watcher);
51      this.abortable = abortable;
52      this.serverManager = serverManager;
53    }
54  
55    /**
56     * Starts the tracking of online RegionServers.
57     *
58     * <p>All RSs will be tracked after this method is called.
59     *
60     * @throws KeeperException
61     */
62    public void start() throws KeeperException {
63      watcher.registerListener(this);
64      ZKUtil.watchAndGetNewChildren(watcher, watcher.rsZNode);
65    }
66  
67    @Override
68    public void nodeDeleted(String path) {
69      if(path.startsWith(watcher.rsZNode)) {
70        String serverName = ZKUtil.getNodeName(path);
71        LOG.info("RegionServer ephemeral node deleted, processing expiration [" +
72            serverName + "]");
73        HServerInfo hsi = serverManager.getServerInfo(serverName);
74        if(hsi == null) {
75          LOG.info("No HServerInfo found for " + serverName);
76          return;
77        }
78        serverManager.expireServer(hsi);
79      }
80    }
81  
82    @Override
83    public void nodeChildrenChanged(String path) {
84      if(path.equals(watcher.rsZNode)) {
85        try {
86          ZKUtil.watchAndGetNewChildren(watcher, watcher.rsZNode);
87        } catch (KeeperException e) {
88          abortable.abort("Unexpected zk exception getting RS nodes", e);
89        }
90      }
91    }
92  
93    /**
94     * Gets the online servers.
95     * @return list of online servers from zk
96     * @throws KeeperException
97     */
98    public List<HServerAddress> getOnlineServers() throws KeeperException {
99      return ZKUtil.listChildrenAndGetAsAddresses(watcher, watcher.rsZNode);
100   }
101 }