001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.master; 019 020import java.io.IOException; 021import java.util.List; 022import java.util.NavigableSet; 023import java.util.TreeSet; 024import org.apache.hadoop.hbase.Abortable; 025import org.apache.hadoop.hbase.ServerName; 026import org.apache.hadoop.hbase.zookeeper.ZKListener; 027import org.apache.hadoop.hbase.zookeeper.ZKUtil; 028import org.apache.hadoop.hbase.zookeeper.ZKWatcher; 029import org.apache.yetus.audience.InterfaceAudience; 030import org.apache.zookeeper.KeeperException; 031import org.slf4j.Logger; 032import org.slf4j.LoggerFactory; 033 034/** 035 * Tracks the list of draining region servers via ZK. 036 * <p> 037 * This class is responsible for watching for changes to the draining servers list. It handles 038 * adds/deletes in the draining RS list and watches each node. 039 * <p> 040 * If an RS gets deleted from draining list, we call 041 * {@link ServerManager#removeServerFromDrainList(ServerName)} 042 * <p> 043 * If an RS gets added to the draining list, we add a watcher to it and call 044 * {@link ServerManager#addServerToDrainList(ServerName)} 045 * <p> 046 * This class is deprecated in 2.0 because decommission/draining API goes through master in 2.0. Can 047 * remove this class in 3.0. 048 */ 049@InterfaceAudience.Private 050public class DrainingServerTracker extends ZKListener { 051 private static final Logger LOG = LoggerFactory.getLogger(DrainingServerTracker.class); 052 053 private ServerManager serverManager; 054 private final NavigableSet<ServerName> drainingServers = new TreeSet<>(); 055 private Abortable abortable; 056 057 public DrainingServerTracker(ZKWatcher watcher, Abortable abortable, 058 ServerManager serverManager) { 059 super(watcher); 060 this.abortable = abortable; 061 this.serverManager = serverManager; 062 } 063 064 /** 065 * Starts the tracking of draining RegionServers. 066 * <p> 067 * All Draining RSs will be tracked after this method is called. 068 */ 069 public void start() throws KeeperException, IOException { 070 watcher.registerListener(this); 071 // Add a ServerListener to check if a server is draining when it's added. 072 serverManager.registerListener(new ServerListener() { 073 @Override 074 public void serverAdded(ServerName sn) { 075 if (drainingServers.contains(sn)) { 076 serverManager.addServerToDrainList(sn); 077 } 078 } 079 }); 080 List<String> servers = 081 ZKUtil.listChildrenAndWatchThem(watcher, watcher.getZNodePaths().drainingZNode); 082 add(servers); 083 } 084 085 private void add(final List<String> servers) throws IOException { 086 synchronized (this.drainingServers) { 087 this.drainingServers.clear(); 088 for (String n : servers) { 089 final ServerName sn = ServerName.valueOf(ZKUtil.getNodeName(n)); 090 this.drainingServers.add(sn); 091 this.serverManager.addServerToDrainList(sn); 092 LOG.info("Draining RS node created, adding to list [" + sn + "]"); 093 094 } 095 } 096 } 097 098 private void remove(final ServerName sn) { 099 synchronized (this.drainingServers) { 100 this.drainingServers.remove(sn); 101 this.serverManager.removeServerFromDrainList(sn); 102 } 103 } 104 105 @Override 106 public void nodeDeleted(final String path) { 107 if (path.startsWith(watcher.getZNodePaths().drainingZNode)) { 108 final ServerName sn = ServerName.valueOf(ZKUtil.getNodeName(path)); 109 LOG.info("Draining RS node deleted, removing from list [" + sn + "]"); 110 remove(sn); 111 } 112 } 113 114 @Override 115 public void nodeChildrenChanged(final String path) { 116 if (path.equals(watcher.getZNodePaths().drainingZNode)) { 117 try { 118 final List<String> newNodes = 119 ZKUtil.listChildrenAndWatchThem(watcher, watcher.getZNodePaths().drainingZNode); 120 add(newNodes); 121 } catch (KeeperException e) { 122 abortable.abort("Unexpected zk exception getting RS nodes", e); 123 } catch (IOException e) { 124 abortable.abort("Unexpected zk exception getting RS nodes", e); 125 } 126 } 127 } 128}