2 # Licensed to the Apache Software Foundation (ASF) under one
3 # or more contributor license agreements. See the NOTICE file
4 # distributed with this work for additional information
5 # regarding copyright ownership. The ASF licenses this file
6 # to you under the Apache License, Version 2.0 (the
7 # "License"); you may not use this file except in compliance
8 # with the License. You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
19 # Add or remove servers from draining mode via zookeeper
20 # Deprecated in 2.0, and will be removed in 3.0. Use Admin decommission
26 java_import org.apache.hadoop.hbase.HBaseConfiguration
27 java_import org.apache.hadoop.hbase.client.ConnectionFactory
28 java_import org.apache.hadoop.hbase.client.HBaseAdmin
29 java_import org.apache.hadoop.hbase.zookeeper.ZKUtil
30 java_import org.apache.hadoop.hbase.zookeeper.ZNodePaths
31 java_import org.slf4j.LoggerFactory
34 NAME = 'draining_servers'.freeze
36 # Do command-line parsing
38 optparse = OptionParser.new do |opts|
39 opts.banner = "Usage: ./hbase org.jruby.Main #{NAME}.rb [options] add|remove|list <hostname>|<host:port>|<servername> ..."
40 opts.separator 'Add remove or list servers in draining mode. Can accept either hostname to drain all region servers' \
41 'in that host, a host:port pair or a host,port,startCode triplet. More than one server can be given separated by space'
42 opts.on('-h', '--help', 'Display usage information') do
49 # Return array of servernames where servername is hostname+port+startcode
52 serverInfos = admin.getClusterMetrics.getLiveServerMetrics.keySet
54 serverInfos.each do |server|
55 servers << server.getServerName
60 # rubocop:disable Metrics/AbcSize
61 def getServerNames(hostOrServers, config)
63 connection = ConnectionFactory.createConnection(config)
65 hostOrServers.each do |host_or_server|
66 # check whether it is already serverName. No need to connect to cluster
67 parts = host_or_server.split(',')
71 admin ||= connection.getAdmin
72 servers = getServers(admin)
74 host_or_server = host_or_server.tr(':', ',')
75 servers.each do |server|
76 ret << server if server.start_with?(host_or_server)
86 def addServers(_options, hostOrServers)
87 config = HBaseConfiguration.create
88 servers = getServerNames(hostOrServers, config)
90 zkw = org.apache.hadoop.hbase.zookeeper.ZKWatcher.new(config, 'draining_servers', nil)
93 parentZnode = zkw.getZNodePaths.drainingZNode
94 servers.each do |server|
95 node = ZNodePaths.joinZNode(parentZnode, server)
96 ZKUtil.createAndFailSilent(zkw, node)
103 def removeServers(_options, hostOrServers)
104 config = HBaseConfiguration.create
105 servers = getServerNames(hostOrServers, config)
107 zkw = org.apache.hadoop.hbase.zookeeper.ZKWatcher.new(config, 'draining_servers', nil)
110 parentZnode = zkw.getZNodePaths.drainingZNode
111 servers.each do |server|
112 node = ZNodePaths.joinZNode(parentZnode, server)
113 ZKUtil.deleteNodeFailSilent(zkw, node)
119 # rubocop:enable Metrics/AbcSize
121 # list servers in draining mode
122 def listServers(_options)
123 config = HBaseConfiguration.create
125 zkw = org.apache.hadoop.hbase.zookeeper.ZKWatcher.new(config, 'draining_servers', nil)
128 parentZnode = zkw.getZNodePaths.drainingZNode
129 servers = ZKUtil.listChildrenNoWatch(zkw, parentZnode)
130 servers.each { |server| puts server }
136 hostOrServers = ARGV[1..ARGV.size]
138 # Create a logger and save it to ruby global
139 $LOG = LoggerFactory.getLogger(NAME)
146 addServers(options, hostOrServers)
152 removeServers(options, hostOrServers)