From d1e9c3d9728501260bc7eafb22482473f07b86e9 Mon Sep 17 00:00:00 2001 From: libisthanks Date: Fri, 1 Dec 2017 21:49:37 +0800 Subject: [PATCH] HBASE-19336 Improve rsgroup to allow assign all tables within a specified namespace by only writing namespace Signed-off-by: Guanghao Zhang --- .../src/main/ruby/hbase/rsgroup_admin.rb | 59 +++++++++++++++++-- hbase-shell/src/main/ruby/shell.rb | 2 + .../shell/commands/move_namespaces_rsgroup.rb | 37 ++++++++++++ .../move_servers_namespaces_rsgroup.rb | 37 ++++++++++++ .../src/test/ruby/shell/rsgroup_shell_test.rb | 42 +++++++++++++ 5 files changed, 172 insertions(+), 5 deletions(-) create mode 100644 hbase-shell/src/main/ruby/shell/commands/move_namespaces_rsgroup.rb create mode 100644 hbase-shell/src/main/ruby/shell/commands/move_servers_namespaces_rsgroup.rb diff --git a/hbase-shell/src/main/ruby/hbase/rsgroup_admin.rb b/hbase-shell/src/main/ruby/hbase/rsgroup_admin.rb index 1d3ca7cb463..4e32ea4447b 100644 --- a/hbase-shell/src/main/ruby/hbase/rsgroup_admin.rb +++ b/hbase-shell/src/main/ruby/hbase/rsgroup_admin.rb @@ -26,7 +26,9 @@ module Hbase include HBaseConstants def initialize(connection) + @connection = connection @admin = org.apache.hadoop.hbase.rsgroup.RSGroupAdminClient.new(connection) + @hb_admin = @connection.getAdmin end def close @@ -76,7 +78,7 @@ module Hbase end #-------------------------------------------------------------------------- - # move server to a group + # move tables to a group def move_tables(dest, *args) tables = java.util.HashSet.new args[0].each do |s| @@ -85,6 +87,13 @@ module Hbase @admin.moveTables(tables, dest) 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 def get_rsgroup_of_server(server) @@ -108,17 +117,57 @@ module Hbase #-------------------------------------------------------------------------- # move server and table to a group def move_servers_tables(dest, *args) - servers = java.util.HashSet.new + servers = get_servers(args[0]) 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| tables.add(org.apache.hadoop.hbase.TableName.valueOf(t)) end @admin.moveServersAndTables(servers, tables, dest) 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 def remove_servers(*args) diff --git a/hbase-shell/src/main/ruby/shell.rb b/hbase-shell/src/main/ruby/shell.rb index 78526254fac..58886fc9ca5 100644 --- a/hbase-shell/src/main/ruby/shell.rb +++ b/hbase-shell/src/main/ruby/shell.rb @@ -480,7 +480,9 @@ Shell.load_command_group( balance_rsgroup move_servers_rsgroup move_tables_rsgroup + move_namespaces_rsgroup move_servers_tables_rsgroup + move_servers_namespaces_rsgroup get_server_rsgroup get_table_rsgroup remove_servers_rsgroup diff --git a/hbase-shell/src/main/ruby/shell/commands/move_namespaces_rsgroup.rb b/hbase-shell/src/main/ruby/shell/commands/move_namespaces_rsgroup.rb new file mode 100644 index 00000000000..0151f7aec6a --- /dev/null +++ b/hbase-shell/src/main/ruby/shell/commands/move_namespaces_rsgroup.rb @@ -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 diff --git a/hbase-shell/src/main/ruby/shell/commands/move_servers_namespaces_rsgroup.rb b/hbase-shell/src/main/ruby/shell/commands/move_servers_namespaces_rsgroup.rb new file mode 100644 index 00000000000..16b1ce996c1 --- /dev/null +++ b/hbase-shell/src/main/ruby/shell/commands/move_servers_namespaces_rsgroup.rb @@ -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 diff --git a/hbase-shell/src/test/ruby/shell/rsgroup_shell_test.rb b/hbase-shell/src/test/ruby/shell/rsgroup_shell_test.rb index 8c33459aac8..ab7ba0d41fb 100644 --- a/hbase-shell/src/test/ruby/shell/rsgroup_shell_test.rb +++ b/hbase-shell/src/test/ruby/shell/rsgroup_shell_test.rb @@ -75,6 +75,48 @@ module Hbase @hbase.rsgroup_admin.balance_rs_group(group_name) 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 define_test 'Test bogus arguments' do assert_raise(ArgumentError) do