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.io.InterruptedIOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.NavigableMap;
26  import java.util.NavigableSet;
27  import java.util.TreeMap;
28  import java.util.TreeSet;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.hadoop.classification.InterfaceAudience;
33  import org.apache.hadoop.hbase.Abortable;
34  import org.apache.hadoop.hbase.ServerName;
35  import org.apache.hadoop.hbase.master.ServerManager;
36  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
37  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.RegionServerInfo;
38  import org.apache.hadoop.hbase.util.Bytes;
39  import org.apache.zookeeper.KeeperException;
40  
41  /**
42   * Tracks the online region servers via ZK.
43   *
44   * <p>Handling of new RSs checking in is done via RPC.  This class
45   * is only responsible for watching for expired nodes.  It handles
46   * listening for changes in the RS node list and watching each node.
47   *
48   * <p>If an RS node gets deleted, this automatically handles calling of
49   * {@link ServerManager#expireServer(ServerName)}
50   */
51  @InterfaceAudience.Private
52  public class RegionServerTracker extends ZooKeeperListener {
53    private static final Log LOG = LogFactory.getLog(RegionServerTracker.class);
54    private NavigableMap<ServerName, RegionServerInfo> regionServers = 
55  		  new TreeMap<ServerName, RegionServerInfo>();
56    private ServerManager serverManager;
57    private Abortable abortable;
58  
59    public RegionServerTracker(ZooKeeperWatcher watcher,
60        Abortable abortable, ServerManager serverManager) {
61      super(watcher);
62      this.abortable = abortable;
63      this.serverManager = serverManager;
64    }
65  
66    /**
67     * Starts the tracking of online RegionServers.
68     *
69     * <p>All RSs will be tracked after this method is called.
70     *
71     * @throws KeeperException
72     * @throws IOException
73     */
74    public void start() throws KeeperException, IOException {
75      watcher.registerListener(this);
76      List<String> servers =
77        ZKUtil.listChildrenAndWatchThem(watcher, watcher.rsZNode);
78      add(servers);
79    }
80  
81    private void add(final List<String> servers) throws IOException {
82      synchronized(this.regionServers) {
83        this.regionServers.clear();
84        for (String n: servers) {
85          ServerName sn = ServerName.parseServerName(ZKUtil.getNodeName(n));
86          if (regionServers.get(sn) == null) {
87            RegionServerInfo.Builder rsInfoBuilder = RegionServerInfo.newBuilder();
88            try {
89              String nodePath = ZKUtil.joinZNode(watcher.rsZNode, n);
90              byte[] data = ZKUtil.getData(watcher, nodePath);
91              if (LOG.isDebugEnabled()) {
92                LOG.debug("RS node: " + nodePath + " data: " + Bytes.toString(data));
93              }
94              if (data != null && data.length > 0 && ProtobufUtil.isPBMagicPrefix(data)) {
95                int magicLen = ProtobufUtil.lengthOfPBMagic();
96                rsInfoBuilder.mergeFrom(data, magicLen, data.length - magicLen);
97              }
98            } catch (KeeperException e) {
99              LOG.warn("Get Rs info port from ephemeral node", e);
100           } catch (IOException e) {
101             LOG.warn("Illegal data from ephemeral node", e);
102           } catch (InterruptedException e) {
103             throw new InterruptedIOException();
104           }
105           this.regionServers.put(sn, rsInfoBuilder.build());
106         }
107       }
108     }
109   }
110 
111   private void remove(final ServerName sn) {
112     synchronized(this.regionServers) {
113       this.regionServers.remove(sn);
114     }
115   }
116 
117   @Override
118   public void nodeDeleted(String path) {
119     if (path.startsWith(watcher.rsZNode)) {
120       String serverName = ZKUtil.getNodeName(path);
121       LOG.info("RegionServer ephemeral node deleted, processing expiration [" +
122         serverName + "]");
123       ServerName sn = ServerName.parseServerName(serverName);
124       if (!serverManager.isServerOnline(sn)) {
125         LOG.warn(serverName.toString() + " is not online or isn't known to the master."+
126          "The latter could be caused by a DNS misconfiguration.");
127         return;
128       }
129       remove(sn);
130       this.serverManager.expireServer(sn);
131     }
132   }
133 
134   @Override
135   public void nodeChildrenChanged(String path) {
136     if (path.equals(watcher.rsZNode)) {
137       try {
138         List<String> servers =
139           ZKUtil.listChildrenAndWatchThem(watcher, watcher.rsZNode);
140         add(servers);
141       } catch (IOException e) {
142         abortable.abort("Unexpected zk exception getting RS nodes", e);
143       } catch (KeeperException e) {
144         abortable.abort("Unexpected zk exception getting RS nodes", e);
145       }
146     }
147   }
148 
149   public RegionServerInfo getRegionServerInfo(final ServerName sn) {
150     return regionServers.get(sn);
151   }
152   
153   /**
154    * Gets the online servers.
155    * @return list of online servers
156    */
157   public List<ServerName> getOnlineServers() {
158     synchronized (this.regionServers) {
159       return new ArrayList<ServerName>(this.regionServers.keySet());
160     }
161   }
162 }