From 7f064248395aa93a99b625ee92c145702c874149 Mon Sep 17 00:00:00 2001 From: libisthanks Date: Fri, 24 Nov 2017 09:41:52 +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 | 53 ++++++++++++++++++++-- hbase-shell/src/main/ruby/shell.rb | 2 + .../ruby/shell/commands/move_namespaces_rsgroup.rb | 37 +++++++++++++++ .../commands/move_servers_namespaces_rsgroup.rb | 37 +++++++++++++++ .../src/test/ruby/shell/rsgroup_shell_test.rb | 12 ++++- 5 files changed, 136 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 befed01..576bbbb 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,10 +87,24 @@ 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) + tablelist = @connection.getAdmin.listTableNamesByNamespace(ns).map { |t| t.getNameAsString() } + tablelist.each do |table| + tables.add(org.apache.hadoop.hbase.TableName.valueOf(table)) + end + end + @admin.moveTables(tables, dest) + end + + #-------------------------------------------------------------------------- # get group of server def get_rsgroup_of_server(server) res = @admin.getRSGroupOfServer( - org.apache.hadoop.hbase.net.Address.fromString(server) + org.apache.hadoop.hbase.net.Address.fromString(server) ) raise(ArgumentError, 'Server has no group: ' + server) if res.nil? res @@ -99,7 +114,7 @@ module Hbase # get group of table def get_rsgroup_of_table(table) res = @admin.getRSGroupInfoOfTable( - org.apache.hadoop.hbase.TableName.valueOf(table) + org.apache.hadoop.hbase.TableName.valueOf(table) ) raise(ArgumentError, 'Table has no group: ' + table) if res.nil? res @@ -118,5 +133,35 @@ module Hbase end @admin.moveServersAndTables(servers, tables, dest) end + + + #-------------------------------------------------------------------------- + # move server and namespace to a group + def move_servers_namespaces(dest, *args) + servers = 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 |ns| + raise(ArgumentError, "Can't find a namespace: #{ns}") unless namespace_exists?(ns) + tablelist = @connection.getAdmin.listTableNamesByNamespace(ns).map { |table| table.getNameAsString() } + tablelist.each do |table| + tables.add(org.apache.hadoop.hbase.TableName.valueOf(table)) + end + 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 => e + return false + end + + end -end +end \ No newline at end of file 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..3d93e1f --- /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 + class MoveNamespacesRsgroup < Command + def help + <<-EOF +Reassign tables from one RegionServer group to another. + +Example: + + hbase> move_namespaces_rsgroup 'dest',['ns1','ns2'] + +EOF + 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..7d9b169 --- /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 + class MoveServersNamespacesRsgroup < Command + def help + <<-EOF +Reassign RegionServers and Namespaces from one group to another. + +Example: + + hbase> move_servers_namespaces_rsgroup 'dest',['server1:port','server2:port'],['ns1','ns2'] + +EOF + 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..3498463 100644 --- a/hbase-shell/src/test/ruby/shell/rsgroup_shell_test.rb +++ b/hbase-shell/src/test/ruby/shell/rsgroup_shell_test.rb @@ -33,6 +33,11 @@ module Hbase define_test 'Test Basic RSGroup Commands' do group_name = 'test_group' table_name = 'test_table' + namespace_name = 'test_namespace' + ns_table_name = 'test_namespace:test_table' + + @shell.command('create_namespace', namespace_name) + @shell.command('create', ns_table_name, 'f') @shell.command('create', table_name, 'f') @@ -62,10 +67,15 @@ module Hbase [table_name]) assert_equal(1, @rsgroup_admin.getRSGroupInfo(group_name).getTables.count) + @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(1, group.getServers.count) - assert_equal(1, group.getTables.count) + assert_equal(2, group.getTables.count) assert_equal(hostPortStr, group.getServers.iterator.next.toString) assert_equal(table_name, group.getTables.iterator.next.toString) -- 1.9.3 (Apple Git-50)