2014-03-12 15:00:47 -04:00
|
|
|
#
|
|
|
|
# Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
# or more contributor license agreements. See the NOTICE file
|
|
|
|
# distributed with this work for additional information
|
|
|
|
# regarding copyright ownership. The ASF licenses this file
|
|
|
|
# to you under the Apache License, Version 2.0 (the
|
|
|
|
# "License"); you may not use this file except in compliance
|
|
|
|
# with the License. You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
|
2017-07-19 13:05:26 -04:00
|
|
|
# Add or remove servers from draining mode via zookeeper
|
2017-10-20 00:44:38 -04:00
|
|
|
# Deprecated in 2.0, and will be removed in 3.0. Use Admin decommission
|
|
|
|
# API instead.
|
2014-03-12 15:00:47 -04:00
|
|
|
|
|
|
|
require 'optparse'
|
|
|
|
include Java
|
|
|
|
|
2017-05-18 12:32:40 -04:00
|
|
|
java_import org.apache.hadoop.hbase.HBaseConfiguration
|
|
|
|
java_import org.apache.hadoop.hbase.client.ConnectionFactory
|
|
|
|
java_import org.apache.hadoop.hbase.client.HBaseAdmin
|
|
|
|
java_import org.apache.hadoop.hbase.zookeeper.ZKUtil
|
2018-12-23 03:59:53 -05:00
|
|
|
java_import org.apache.hadoop.hbase.zookeeper.ZNodePaths
|
2017-12-21 22:20:29 -05:00
|
|
|
java_import org.slf4j.LoggerFactory
|
2014-03-12 15:00:47 -04:00
|
|
|
|
|
|
|
# Name of this script
|
2017-07-19 13:05:26 -04:00
|
|
|
NAME = 'draining_servers'.freeze
|
2014-03-12 15:00:47 -04:00
|
|
|
|
|
|
|
# Do command-line parsing
|
|
|
|
options = {}
|
|
|
|
optparse = OptionParser.new do |opts|
|
|
|
|
opts.banner = "Usage: ./hbase org.jruby.Main #{NAME}.rb [options] add|remove|list <hostname>|<host:port>|<servername> ..."
|
2017-07-19 13:05:26 -04:00
|
|
|
opts.separator 'Add remove or list servers in draining mode. Can accept either hostname to drain all region servers' \
|
2014-03-12 15:00:47 -04:00
|
|
|
'in that host, a host:port pair or a host,port,startCode triplet. More than one server can be given separated by space'
|
|
|
|
opts.on('-h', '--help', 'Display usage information') do
|
|
|
|
puts opts
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
optparse.parse!
|
|
|
|
|
|
|
|
# Return array of servernames where servername is hostname+port+startcode
|
|
|
|
# comma-delimited
|
|
|
|
def getServers(admin)
|
2017-07-19 13:05:26 -04:00
|
|
|
serverInfos = admin.getClusterStatus.getServers
|
2014-03-12 15:00:47 -04:00
|
|
|
servers = []
|
|
|
|
for server in serverInfos
|
2017-07-19 13:05:26 -04:00
|
|
|
servers << server.getServerName
|
2014-03-12 15:00:47 -04:00
|
|
|
end
|
2017-07-19 13:05:26 -04:00
|
|
|
servers
|
2014-03-12 15:00:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def getServerNames(hostOrServers, config)
|
|
|
|
ret = []
|
2015-12-29 10:06:43 -05:00
|
|
|
connection = ConnectionFactory.createConnection(config)
|
2017-07-19 13:05:26 -04:00
|
|
|
|
2014-03-12 15:00:47 -04:00
|
|
|
for hostOrServer in hostOrServers
|
|
|
|
# check whether it is already serverName. No need to connect to cluster
|
|
|
|
parts = hostOrServer.split(',')
|
2017-07-19 13:05:26 -04:00
|
|
|
if parts.size == 3
|
2014-03-12 15:00:47 -04:00
|
|
|
ret << hostOrServer
|
2017-07-19 13:05:26 -04:00
|
|
|
else
|
|
|
|
admin = connection.getAdmin unless admin
|
2014-03-12 15:00:47 -04:00
|
|
|
servers = getServers(admin)
|
|
|
|
|
2017-07-19 13:05:26 -04:00
|
|
|
hostOrServer = hostOrServer.tr(':', ',')
|
|
|
|
for server in servers
|
2014-03-12 15:00:47 -04:00
|
|
|
ret << server if server.start_with?(hostOrServer)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-07-19 13:05:26 -04:00
|
|
|
|
|
|
|
admin.close if admin
|
|
|
|
connection.close
|
|
|
|
ret
|
2014-03-12 15:00:47 -04:00
|
|
|
end
|
|
|
|
|
2017-07-19 13:05:26 -04:00
|
|
|
def addServers(_options, hostOrServers)
|
|
|
|
config = HBaseConfiguration.create
|
2014-03-12 15:00:47 -04:00
|
|
|
servers = getServerNames(hostOrServers, config)
|
2017-07-19 13:05:26 -04:00
|
|
|
|
2018-03-02 14:09:50 -05:00
|
|
|
zkw = org.apache.hadoop.hbase.zookeeper.ZKWatcher.new(config, 'draining_servers', nil)
|
2017-07-19 13:05:26 -04:00
|
|
|
|
2014-03-12 15:00:47 -04:00
|
|
|
begin
|
2018-12-23 03:59:53 -05:00
|
|
|
parentZnode = zkw.getZNodePaths.drainingZNode
|
2014-03-12 15:00:47 -04:00
|
|
|
for server in servers
|
2018-12-23 03:59:53 -05:00
|
|
|
node = ZNodePaths.joinZNode(parentZnode, server)
|
2014-03-12 15:00:47 -04:00
|
|
|
ZKUtil.createAndFailSilent(zkw, node)
|
|
|
|
end
|
|
|
|
ensure
|
2017-07-19 13:05:26 -04:00
|
|
|
zkw.close
|
2014-03-12 15:00:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-19 13:05:26 -04:00
|
|
|
def removeServers(_options, hostOrServers)
|
|
|
|
config = HBaseConfiguration.create
|
2014-03-12 15:00:47 -04:00
|
|
|
servers = getServerNames(hostOrServers, config)
|
2017-07-19 13:05:26 -04:00
|
|
|
|
2018-03-02 14:09:50 -05:00
|
|
|
zkw = org.apache.hadoop.hbase.zookeeper.ZKWatcher.new(config, 'draining_servers', nil)
|
2017-07-19 13:05:26 -04:00
|
|
|
|
2014-03-12 15:00:47 -04:00
|
|
|
begin
|
2018-12-23 03:59:53 -05:00
|
|
|
parentZnode = zkw.getZNodePaths.drainingZNode
|
2014-03-12 15:00:47 -04:00
|
|
|
for server in servers
|
2018-12-23 03:59:53 -05:00
|
|
|
node = ZNodePaths.joinZNode(parentZnode, server)
|
2014-03-12 15:00:47 -04:00
|
|
|
ZKUtil.deleteNodeFailSilent(zkw, node)
|
|
|
|
end
|
|
|
|
ensure
|
2017-07-19 13:05:26 -04:00
|
|
|
zkw.close
|
2014-03-12 15:00:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# list servers in draining mode
|
2017-07-19 13:05:26 -04:00
|
|
|
def listServers(_options)
|
|
|
|
config = HBaseConfiguration.create
|
|
|
|
|
2018-03-02 14:09:50 -05:00
|
|
|
zkw = org.apache.hadoop.hbase.zookeeper.ZKWatcher.new(config, 'draining_servers', nil)
|
2014-03-12 15:00:47 -04:00
|
|
|
|
2018-12-23 03:59:53 -05:00
|
|
|
begin
|
|
|
|
parentZnode = zkw.getZNodePaths.drainingZNode
|
|
|
|
servers = ZKUtil.listChildrenNoWatch(zkw, parentZnode)
|
|
|
|
servers.each { |server| puts server }
|
|
|
|
ensure
|
|
|
|
zkw.close
|
|
|
|
end
|
2014-03-12 15:00:47 -04:00
|
|
|
end
|
|
|
|
|
2017-07-19 13:05:26 -04:00
|
|
|
hostOrServers = ARGV[1..ARGV.size]
|
2014-03-12 15:00:47 -04:00
|
|
|
|
|
|
|
# Create a logger and save it to ruby global
|
2017-12-21 22:20:29 -05:00
|
|
|
$LOG = LoggerFactory.getLogger(NAME)
|
2014-03-12 15:00:47 -04:00
|
|
|
case ARGV[0]
|
2017-07-19 13:05:26 -04:00
|
|
|
when 'add'
|
|
|
|
if ARGV.length < 2
|
2014-03-12 15:00:47 -04:00
|
|
|
puts optparse
|
2017-07-19 13:05:26 -04:00
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
addServers(options, hostOrServers)
|
|
|
|
when 'remove'
|
|
|
|
if ARGV.length < 2
|
|
|
|
puts optparse
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
removeServers(options, hostOrServers)
|
|
|
|
when 'list'
|
|
|
|
listServers(options)
|
|
|
|
else
|
|
|
|
puts optparse
|
|
|
|
exit 3
|
2014-03-12 15:00:47 -04:00
|
|
|
end
|