From 3f151772400c43aa969d96f4beb7ab216ac43c01 Mon Sep 17 00:00:00 2001 From: libisthanks Date: Thu, 30 Nov 2017 15:12:38 +0800 Subject: [PATCH] HBASE-19336 Improve rsgroup to allow assign all tables within a specified namespace by only writing namespace --- hbase-shell/src/main/ruby/hbase/rsgroup_admin.rb | 50 +++++++++++++++++++++- hbase-shell/src/main/ruby/shell.rb | 2 + .../ruby/shell/commands/move_namespaces_rsgroup.rb | 36 ++++++++++++++++ .../commands/move_servers_namespaces_rsgroup.rb | 36 ++++++++++++++++ .../src/test/ruby/shell/rsgroup_shell_test.rb | 42 ++++++++++++++++++ 5 files changed, 165 insertions(+), 1 deletion(-) 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 befed01..2d2f826 100644 --- a/hbase-shell/src/main/ruby/hbase/rsgroup_admin.rb +++ b/hbase-shell/src/main/ruby/hbase/rsgroup_admin.rb @@ -26,6 +26,7 @@ module Hbase include HBaseConstants def initialize(connection) + @connection = connection @admin = org.apache.hadoop.hbase.rsgroup.RSGroupAdminClient.new(connection) end @@ -76,7 +77,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| @@ -86,6 +87,18 @@ module Hbase end #-------------------------------------------------------------------------- + # move namespaces to a group + def move_namespaces(dest, *args) + tables = java.util.HashSet.new + args[0].each do |ns| + raise(ArgumentError, + "Can't find a namespace: #{ns}") unless namespace_exists?(ns) + tables = get_tables_by_namespace(ns) + end + @admin.moveTables(tables, dest) + end + + #-------------------------------------------------------------------------- # get group of server def get_rsgroup_of_server(server) res = @admin.getRSGroupOfServer( @@ -118,5 +131,40 @@ module Hbase end @admin.moveServersAndTables(servers, tables, dest) end + + #-------------------------------------------------------------------------- + # move server and namespace to a group + def move_servers_namespaces(dest, *args) + tables, servers = java.util.HashSet.new, java.util.HashSet.new + args[0].each do |s| + servers.add(org.apache.hadoop.hbase.net.Address.fromString(s)) + end + args[1].each do |ns| + raise(ArgumentError, + "Can't find a namespace: #{ns}") unless namespace_exists?(ns) + tables = get_tables_by_namespace(ns) + end + @admin.moveServersAndTables(servers, tables, dest) + end + + # Does Namespace exist + def namespace_exists?(namespace_name) + return !@connection.getAdmin. + getNamespaceDescriptor(namespace_name).nil? + rescue org.apache.hadoop.hbase.NamespaceNotFoundException + return false + end + + # Get tables by namespace + def get_tables_by_namespace(namespace_name) + tables = java.util.HashSet.new + tablelist = @connection.getAdmin. + listTableNamesByNamespace(namespace_name). + map(&:getNameAsString) + tablelist.each do |table| + tables.add(org.apache.hadoop.hbase.TableName.valueOf(table)) + end + return tables + end end end diff --git a/hbase-shell/src/main/ruby/shell.rb b/hbase-shell/src/main/ruby/shell.rb index 687af12..1093dcf 100644 --- a/hbase-shell/src/main/ruby/shell.rb +++ b/hbase-shell/src/main/ruby/shell.rb @@ -479,7 +479,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 ] 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 0000000..aa64062 --- /dev/null +++ b/hbase-shell/src/main/ruby/shell/commands/move_namespaces_rsgroup.rb @@ -0,0 +1,36 @@ +# 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 0000000..d78f276 --- /dev/null +++ b/hbase-shell/src/main/ruby/shell/commands/move_servers_namespaces_rsgroup.rb @@ -0,0 +1,36 @@ +# 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 8c33459..2da7c59 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]) + + hostport = @rsgroup_admin.getRSGroupInfo('default').getServers.iterator.next + hostPortStr = hostport.toString + @shell.command('move_servers_namespaces_rsgroup', + ns_group_name, + [hostPortStr], + [namespace_name]) + ns_group = @hbase.rsgroup_admin.get_rsgroup(ns_group_name) + assert_not_nil(ns_group) + assert_equal(hostPortStr, 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 -- 1.9.3 (Apple Git-50)