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