HBASE-19336 Improve rsgroup to allow assign all tables within a specified namespace by only writing namespace

Signed-off-by: Guanghao Zhang <zghao@apache.org>
This commit is contained in:
libisthanks 2017-12-01 21:49:37 +08:00 committed by Guanghao Zhang
parent 79ac70ac86
commit d1e9c3d972
5 changed files with 172 additions and 5 deletions

View File

@ -26,7 +26,9 @@ module Hbase
include HBaseConstants include HBaseConstants
def initialize(connection) def initialize(connection)
@connection = connection
@admin = org.apache.hadoop.hbase.rsgroup.RSGroupAdminClient.new(connection) @admin = org.apache.hadoop.hbase.rsgroup.RSGroupAdminClient.new(connection)
@hb_admin = @connection.getAdmin
end end
def close def close
@ -76,7 +78,7 @@ module Hbase
end end
#-------------------------------------------------------------------------- #--------------------------------------------------------------------------
# move server to a group # move tables to a group
def move_tables(dest, *args) def move_tables(dest, *args)
tables = java.util.HashSet.new tables = java.util.HashSet.new
args[0].each do |s| args[0].each do |s|
@ -85,6 +87,13 @@ module Hbase
@admin.moveTables(tables, dest) @admin.moveTables(tables, dest)
end end
#--------------------------------------------------------------------------
# move namespaces to a group
def move_namespaces(dest, *args)
tables = get_tables(args[0])
@admin.moveTables(tables, dest)
end
#-------------------------------------------------------------------------- #--------------------------------------------------------------------------
# get group of server # get group of server
def get_rsgroup_of_server(server) def get_rsgroup_of_server(server)
@ -108,17 +117,57 @@ module Hbase
#-------------------------------------------------------------------------- #--------------------------------------------------------------------------
# move server and table to a group # move server and table to a group
def move_servers_tables(dest, *args) def move_servers_tables(dest, *args)
servers = java.util.HashSet.new servers = get_servers(args[0])
tables = java.util.HashSet.new tables = java.util.HashSet.new
args[0].each do |s|
servers.add(org.apache.hadoop.hbase.net.Address.fromString(s))
end
args[1].each do |t| args[1].each do |t|
tables.add(org.apache.hadoop.hbase.TableName.valueOf(t)) tables.add(org.apache.hadoop.hbase.TableName.valueOf(t))
end end
@admin.moveServersAndTables(servers, tables, dest) @admin.moveServersAndTables(servers, tables, dest)
end end
#--------------------------------------------------------------------------
# move server and namespace to a group
def move_servers_namespaces(dest, *args)
servers = get_servers(args[0])
tables = get_tables(args[1])
@admin.moveServersAndTables(servers, tables, dest)
end
def get_servers(servers)
server_set = java.util.HashSet.new
servers.each do |s|
server_set.add(org.apache.hadoop.hbase.net.Address.fromString(s))
end
server_set
end
def get_tables(namespaces)
table_set = java.util.HashSet.new
error = "Can't find a namespace: "
namespaces.each do |ns|
raise(ArgumentError, "#{error}#{ns}") unless namespace_exists?(ns)
table_set.addAll(get_tables_by_namespace(ns))
end
table_set
end
# Get tables by namespace
def get_tables_by_namespace(ns)
tables = java.util.HashSet.new
tablelist = @hb_admin.listTableNamesByNamespace(ns).map(&:getNameAsString)
tablelist.each do |table|
tables.add(org.apache.hadoop.hbase.TableName.valueOf(table))
end
tables
end
# Does Namespace exist
def namespace_exists?(ns)
return !@hb_admin.getNamespaceDescriptor(ns).nil?
rescue org.apache.hadoop.hbase.NamespaceNotFoundException
return false
end
#-------------------------------------------------------------------------- #--------------------------------------------------------------------------
# remove decommissioned server from rsgroup # remove decommissioned server from rsgroup
def remove_servers(*args) def remove_servers(*args)

View File

@ -480,7 +480,9 @@ Shell.load_command_group(
balance_rsgroup balance_rsgroup
move_servers_rsgroup move_servers_rsgroup
move_tables_rsgroup move_tables_rsgroup
move_namespaces_rsgroup
move_servers_tables_rsgroup move_servers_tables_rsgroup
move_servers_namespaces_rsgroup
get_server_rsgroup get_server_rsgroup
get_table_rsgroup get_table_rsgroup
remove_servers_rsgroup remove_servers_rsgroup

View File

@ -0,0 +1,37 @@
# 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.
#
module Shell
module Commands
# Reassign tables of specified namespaces
# from one RegionServer group to another.
class MoveNamespacesRsgroup < Command
def help
<<-CMD
Example:
hbase> move_namespaces_rsgroup 'dest',['ns1','ns2']
CMD
end
def command(dest, namespaces)
rsgroup_admin.move_namespaces(dest, namespaces)
end
end
end
end

View File

@ -0,0 +1,37 @@
# 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.
#
module Shell
module Commands
# Reassign RegionServers and Tables of
# specified namespaces from one group to another.
class MoveServersNamespacesRsgroup < Command
def help
<<-CMD
Example:
hbase> move_servers_namespaces_rsgroup 'dest',['server1:port','server2:port'],['ns1','ns2']
CMD
end
def command(dest, servers, namespaces)
rsgroup_admin.move_servers_namespaces(dest, servers, namespaces)
end
end
end
end

View File

@ -75,6 +75,48 @@ module Hbase
@hbase.rsgroup_admin.balance_rs_group(group_name) @hbase.rsgroup_admin.balance_rs_group(group_name)
end end
define_test 'Test RSGroup Move Namespace RSGroup Commands' do
group_name = 'test_group'
namespace_name = 'test_namespace'
ns_table_name = 'test_namespace:test_ns_table'
@shell.command('create_namespace', namespace_name)
@shell.command('create', ns_table_name, 'f')
@shell.command('move_namespaces_rsgroup',
group_name,
[namespace_name])
assert_equal(2, @rsgroup_admin.getRSGroupInfo(group_name).getTables.count)
group = @hbase.rsgroup_admin.get_rsgroup(group_name)
assert_not_nil(group)
assert_equal(ns_table_name, group.getTables.iterator.next.toString)
end
define_test 'Test RSGroup Move Server Namespace RSGroup Commands' do
ns_group_name = 'test_ns_group'
namespace_name = 'test_namespace'
ns_table_name = 'test_namespace:test_ns_table'
@shell.command('add_rsgroup', ns_group_name)
assert_not_nil(@rsgroup_admin.getRSGroupInfo(ns_group_name))
@shell.command('move_tables_rsgroup',
'default',
[ns_table_name])
group_servers = @rsgroup_admin.getRSGroupInfo('default').getServers
hostport_str = group_servers.iterator.next.toString
@shell.command('move_servers_namespaces_rsgroup',
ns_group_name,
[hostport_str],
[namespace_name])
ns_group = @hbase.rsgroup_admin.get_rsgroup(ns_group_name)
assert_not_nil(ns_group)
assert_equal(hostport_str, ns_group.getServers.iterator.next.toString)
assert_equal(ns_table_name, ns_group.getTables.iterator.next.toString)
end
# we test exceptions that could be thrown by the ruby wrappers # we test exceptions that could be thrown by the ruby wrappers
define_test 'Test bogus arguments' do define_test 'Test bogus arguments' do
assert_raise(ArgumentError) do assert_raise(ArgumentError) do