001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.master;
020
021import java.io.IOException;
022import java.io.InterruptedIOException;
023import java.util.ArrayList;
024import java.util.List;
025import java.util.NavigableMap;
026import java.util.TreeMap;
027
028import org.apache.hadoop.hbase.ServerName;
029import org.apache.hadoop.hbase.zookeeper.ZKListener;
030import org.apache.hadoop.hbase.zookeeper.ZKUtil;
031import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
032import org.apache.hadoop.hbase.zookeeper.ZNodePaths;
033import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
034import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.RegionServerInfo;
035import org.apache.yetus.audience.InterfaceAudience;
036import org.apache.zookeeper.KeeperException;
037import org.slf4j.Logger;
038import org.slf4j.LoggerFactory;
039
040/**
041 * Tracks the online region servers via ZK.
042 *
043 * <p>Handling of new RSs checking in is done via RPC.  This class
044 * is only responsible for watching for expired nodes.  It handles
045 * listening for changes in the RS node list and watching each node.
046 *
047 * <p>If an RS node gets deleted, this automatically handles calling of
048 * {@link ServerManager#expireServer(ServerName)}
049 */
050@InterfaceAudience.Private
051public class RegionServerTracker extends ZKListener {
052  private static final Logger LOG = LoggerFactory.getLogger(RegionServerTracker.class);
053  private final NavigableMap<ServerName, RegionServerInfo> regionServers = new TreeMap<>();
054  private ServerManager serverManager;
055  private MasterServices server;
056
057  public RegionServerTracker(ZKWatcher watcher,
058      MasterServices server, ServerManager serverManager) {
059    super(watcher);
060    this.server = server;
061    this.serverManager = serverManager;
062  }
063
064  /**
065   * Starts the tracking of online RegionServers.
066   *
067   * <p>All RSs will be tracked after this method is called.
068   *
069   * @throws KeeperException
070   * @throws IOException
071   */
072  public void start() throws KeeperException, IOException {
073    watcher.registerListener(this);
074    List<String> servers =
075      ZKUtil.listChildrenAndWatchThem(watcher, watcher.znodePaths.rsZNode);
076    refresh(servers);
077  }
078
079  private void refresh(final List<String> servers) throws IOException {
080    synchronized(this.regionServers) {
081      this.regionServers.clear();
082      for (String n: servers) {
083        ServerName sn = ServerName.parseServerName(ZKUtil.getNodeName(n));
084        if (regionServers.get(sn) == null) {
085          RegionServerInfo.Builder rsInfoBuilder = RegionServerInfo.newBuilder();
086          try {
087            String nodePath = ZNodePaths.joinZNode(watcher.znodePaths.rsZNode, n);
088            byte[] data = ZKUtil.getData(watcher, nodePath);
089            if (data != null && data.length > 0 && ProtobufUtil.isPBMagicPrefix(data)) {
090              int magicLen = ProtobufUtil.lengthOfPBMagic();
091              ProtobufUtil.mergeFrom(rsInfoBuilder, data, magicLen, data.length - magicLen);
092            }
093            if (LOG.isTraceEnabled()) {
094              LOG.trace("Added tracking of RS " + nodePath);
095            }
096          } catch (KeeperException e) {
097            LOG.warn("Get Rs info port from ephemeral node", e);
098          } catch (IOException e) {
099            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 nodeCreated(String path) {
117    if (path.startsWith(watcher.znodePaths.rsZNode)) {
118      String serverName = ZKUtil.getNodeName(path);
119      LOG.info("RegionServer ephemeral node created, adding [" + serverName + "]");
120      if (server.isInitialized()) {
121        // Only call the check to move servers if a RegionServer was added to the cluster; in this
122        // case it could be a server with a new version so it makes sense to run the check.
123        server.checkIfShouldMoveSystemRegionAsync();
124      }
125    }
126  }
127
128  @Override
129  public void nodeDeleted(String path) {
130    if (path.startsWith(watcher.znodePaths.rsZNode)) {
131      String serverName = ZKUtil.getNodeName(path);
132      LOG.info("RegionServer ephemeral node deleted, processing expiration [" +
133        serverName + "]");
134      ServerName sn = ServerName.parseServerName(serverName);
135      if (!serverManager.isServerOnline(sn)) {
136        LOG.warn(serverName.toString() + " is not online or isn't known to the master."+
137         "The latter could be caused by a DNS misconfiguration.");
138        return;
139      }
140      remove(sn);
141      this.serverManager.expireServer(sn);
142    }
143  }
144
145  @Override
146  public void nodeChildrenChanged(String path) {
147    if (path.equals(watcher.znodePaths.rsZNode)
148        && !server.isAborted() && !server.isStopped()) {
149      try {
150        List<String> servers =
151          ZKUtil.listChildrenAndWatchThem(watcher, watcher.znodePaths.rsZNode);
152        refresh(servers);
153      } catch (IOException e) {
154        server.abort("Unexpected zk exception getting RS nodes", e);
155      } catch (KeeperException e) {
156        server.abort("Unexpected zk exception getting RS nodes", e);
157      }
158    }
159  }
160
161  public RegionServerInfo getRegionServerInfo(final ServerName sn) {
162    return regionServers.get(sn);
163  }
164
165  /**
166   * Gets the online servers.
167   * @return list of online servers
168   */
169  public List<ServerName> getOnlineServers() {
170    synchronized (this.regionServers) {
171      return new ArrayList<>(this.regionServers.keySet());
172    }
173  }
174}