HBASE-16666 Add append and remove peer namespaces cmds for replication (Guanghao Zhang)
Project: http://git-wip-us.apache.org/repos/asf/hbase/repo Commit: http://git-wip-us.apache.org/repos/asf/hbase/commit/f5abe17b Tree: http://git-wip-us.apache.org/repos/asf/hbase/tree/f5abe17b Diff: http://git-wip-us.apache.org/repos/asf/hbase/diff/f5abe17b Branch: refs/heads/hbase-12439 Commit: f5abe17bc66ae9b780daec3afb6f08e69a5cf392 Parents: ccde439 Author: tedyu <yuzhih...@gmail.com> Authored: Sun Oct 9 21:22:50 2016 -0700 Committer: tedyu <yuzhih...@gmail.com> Committed: Sun Oct 9 21:22:50 2016 -0700 ---------------------------------------------------------------------- .../src/main/ruby/hbase/replication_admin.rb | 37 ++++++++ hbase-shell/src/main/ruby/shell.rb | 2 + .../shell/commands/append_peer_namespaces.rb | 44 +++++++++ .../shell/commands/remove_peer_namespaces.rb | 41 +++++++++ .../test/ruby/hbase/replication_admin_test.rb | 93 +++++++++++++++++++- 5 files changed, 214 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hbase/blob/f5abe17b/hbase-shell/src/main/ruby/hbase/replication_admin.rb ---------------------------------------------------------------------- diff --git a/hbase-shell/src/main/ruby/hbase/replication_admin.rb b/hbase-shell/src/main/ruby/hbase/replication_admin.rb index f99ccae..8aa158b 100644 --- a/hbase-shell/src/main/ruby/hbase/replication_admin.rb +++ b/hbase-shell/src/main/ruby/hbase/replication_admin.rb @@ -205,10 +205,47 @@ module Hbase end end + # Add some namespaces for the specified peer + def add_peer_namespaces(id, namespaces) + unless namespaces.nil? + rpc = get_peer_config(id) + unless rpc.nil? + ns_set = rpc.getNamespaces() + if ns_set.nil? + ns_set = java.util.HashSet.new + end + namespaces.each do |n| + ns_set.add(n) + end + rpc.setNamespaces(ns_set) + @replication_admin.updatePeerConfig(id, rpc) + end + end + end + + # Remove some namespaces for the specified peer + def remove_peer_namespaces(id, namespaces) + unless namespaces.nil? + rpc = get_peer_config(id) + unless rpc.nil? + ns_set = rpc.getNamespaces() + unless ns_set.nil? + namespaces.each do |n| + ns_set.remove(n) + end + end + rpc.setNamespaces(ns_set) + @replication_admin.updatePeerConfig(id, rpc) + end + end + end + # Show the current namespaces config for the specified peer def show_peer_namespaces(peer_config) namespaces = peer_config.get_namespaces if !namespaces.nil? + namespaces = java.util.ArrayList.new(namespaces) + java.util.Collections.sort(namespaces) return namespaces.join(';') else return nil http://git-wip-us.apache.org/repos/asf/hbase/blob/f5abe17b/hbase-shell/src/main/ruby/shell.rb ---------------------------------------------------------------------- diff --git a/hbase-shell/src/main/ruby/shell.rb b/hbase-shell/src/main/ruby/shell.rb index ee508e9..02f8191 100644 --- a/hbase-shell/src/main/ruby/shell.rb +++ b/hbase-shell/src/main/ruby/shell.rb @@ -371,6 +371,8 @@ Shell.load_command_group( enable_peer disable_peer set_peer_namespaces + append_peer_namespaces + remove_peer_namespaces show_peer_tableCFs set_peer_tableCFs list_replicated_tables http://git-wip-us.apache.org/repos/asf/hbase/blob/f5abe17b/hbase-shell/src/main/ruby/shell/commands/append_peer_namespaces.rb ---------------------------------------------------------------------- diff --git a/hbase-shell/src/main/ruby/shell/commands/append_peer_namespaces.rb b/hbase-shell/src/main/ruby/shell/commands/append_peer_namespaces.rb new file mode 100644 index 0000000..2585754 --- /dev/null +++ b/hbase-shell/src/main/ruby/shell/commands/append_peer_namespaces.rb @@ -0,0 +1,44 @@ +# +# Copyright The Apache Software Foundation +# +# 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 AppendPeerNamespaces< Command + def help + return <<-EOF + Append some namespaces to be replicable for the specified peer. + + Set a namespace in the peer config means that all tables in this + namespace (with replication_scope != 0 ) will be replicated. + + Examples: + + # append ns1,ns2 to be replicable for peer '2'. + hbase> append_peer_namespaces '2', ["ns1", "ns2"] + + EOF + end + + def command(id, namespaces) + replication_admin.add_peer_namespaces(id, namespaces) + end + end + end +end http://git-wip-us.apache.org/repos/asf/hbase/blob/f5abe17b/hbase-shell/src/main/ruby/shell/commands/remove_peer_namespaces.rb ---------------------------------------------------------------------- diff --git a/hbase-shell/src/main/ruby/shell/commands/remove_peer_namespaces.rb b/hbase-shell/src/main/ruby/shell/commands/remove_peer_namespaces.rb new file mode 100644 index 0000000..0b668e5 --- /dev/null +++ b/hbase-shell/src/main/ruby/shell/commands/remove_peer_namespaces.rb @@ -0,0 +1,41 @@ +# +# Copyright The Apache Software Foundation +# +# 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 RemovePeerNamespaces< Command + def help + return <<-EOF + Remove some namespaces from the namespaces config for the specified peer. + + Examples: + + # remove ns1 from the replicable namespaces for peer '2'. + hbase> remove_peer_namespaces '2', ["ns1"] + + EOF + end + + def command(id, namespaces) + replication_admin.remove_peer_namespaces(id, namespaces) + end + end + end +end http://git-wip-us.apache.org/repos/asf/hbase/blob/f5abe17b/hbase-shell/src/test/ruby/hbase/replication_admin_test.rb ---------------------------------------------------------------------- diff --git a/hbase-shell/src/test/ruby/hbase/replication_admin_test.rb b/hbase-shell/src/test/ruby/hbase/replication_admin_test.rb index daa8f96..04aa67f 100644 --- a/hbase-shell/src/test/ruby/hbase/replication_admin_test.rb +++ b/hbase-shell/src/test/ruby/hbase/replication_admin_test.rb @@ -124,7 +124,7 @@ module Hbase define_test "add_peer: multiple zk cluster key and namespaces" do cluster_key = "zk4,zk5,zk6:11000:/hbase-test" namespaces = ["ns1", "ns2", "ns3"] - namespaces_str = "ns2;ns1;ns3" + namespaces_str = "ns1;ns2;ns3" args = { CLUSTER_KEY => cluster_key, NAMESPACES => namespaces } command(:add_peer, @peer_id, args) @@ -145,7 +145,7 @@ module Hbase namespaces = ["ns1", "ns2"] table_cfs = { "ns3:table1" => [], "ns3:table2" => ["cf1"], "ns3:table3" => ["cf1", "cf2"] } - namespaces_str = "ns2;ns1" + namespaces_str = "ns1;ns2" table_cfs_str = "ns3.table1;ns3.table3:cf1,cf2;ns3.table2:cf1" args = { CLUSTER_KEY => cluster_key, NAMESPACES => namespaces, @@ -198,7 +198,7 @@ module Hbase define_test "set_peer_namespaces: works with namespaces array" do cluster_key = "zk4,zk5,zk6:11000:/hbase-test" namespaces = ["ns1", "ns2"] - namespaces_str = "ns2;ns1" + namespaces_str = "ns1;ns2" args = { CLUSTER_KEY => cluster_key } command(:add_peer, @peer_id, args) @@ -219,6 +219,93 @@ module Hbase command(:remove_peer, @peer_id) end + define_test "append_peer_namespaces: works with namespaces array" do + cluster_key = "zk4,zk5,zk6:11000:/hbase-test" + namespaces = ["ns1", "ns2"] + namespaces_str = "ns1;ns2" + + args = { CLUSTER_KEY => cluster_key } + command(:add_peer, @peer_id, args) + + # Normally the ReplicationSourceManager will call ReplicationPeer#peer_added + # but here we have to do it ourselves + replication_admin.peer_added(@peer_id) + + command(:append_peer_namespaces, @peer_id, namespaces) + + assert_equal(1, command(:list_peers).length) + assert(command(:list_peers).key?(@peer_id)) + peer_config = command(:list_peers).fetch(@peer_id) + assert_equal(namespaces_str, + replication_admin.show_peer_namespaces(peer_config)) + + namespaces = ["ns3"] + namespaces_str = "ns1;ns2;ns3" + command(:append_peer_namespaces, @peer_id, namespaces) + + assert_equal(1, command(:list_peers).length) + assert(command(:list_peers).key?(@peer_id)) + peer_config = command(:list_peers).fetch(@peer_id) + assert_equal(namespaces_str, + replication_admin.show_peer_namespaces(peer_config)) + + # append a namespace which is already in the peer config + command(:append_peer_namespaces, @peer_id, namespaces) + + assert_equal(1, command(:list_peers).length) + assert(command(:list_peers).key?(@peer_id)) + peer_config = command(:list_peers).fetch(@peer_id) + assert_equal(namespaces_str, + replication_admin.show_peer_namespaces(peer_config)) + + # cleanup for future tests + command(:remove_peer, @peer_id) + end + + define_test "remove_peer_namespaces: works with namespaces array" do + cluster_key = "zk4,zk5,zk6:11000:/hbase-test" + namespaces = ["ns1", "ns2", "ns3"] + + args = { CLUSTER_KEY => cluster_key, NAMESPACES => namespaces } + command(:add_peer, @peer_id, args) + + # Normally the ReplicationSourceManager will call ReplicationPeer#peer_added + # but here we have to do it ourselves + replication_admin.peer_added(@peer_id) + + namespaces = ["ns1", "ns2"] + namespaces_str = "ns3" + command(:remove_peer_namespaces, @peer_id, namespaces) + + assert_equal(1, command(:list_peers).length) + assert(command(:list_peers).key?(@peer_id)) + peer_config = command(:list_peers).fetch(@peer_id) + assert_equal(namespaces_str, + replication_admin.show_peer_namespaces(peer_config)) + + namespaces = ["ns3"] + namespaces_str = nil + command(:remove_peer_namespaces, @peer_id, namespaces) + + assert_equal(1, command(:list_peers).length) + assert(command(:list_peers).key?(@peer_id)) + peer_config = command(:list_peers).fetch(@peer_id) + assert_equal(namespaces_str, + replication_admin.show_peer_namespaces(peer_config)) + + # remove a namespace which is not in peer config + command(:remove_peer_namespaces, @peer_id, namespaces) + + assert_equal(1, command(:list_peers).length) + assert(command(:list_peers).key?(@peer_id)) + peer_config = command(:list_peers).fetch(@peer_id) + assert_equal(namespaces_str, + replication_admin.show_peer_namespaces(peer_config)) + + # cleanup for future tests + command(:remove_peer, @peer_id) + end + define_test "get_peer_config: works with simple clusterKey peer" do cluster_key = "localhost:2181:/hbase-test" args = { CLUSTER_KEY => cluster_key }