From 90b9ed15231c01f979c76620f5201bdcd4975ac9 Mon Sep 17 00:00:00 2001 From: meiyi Date: Wed, 2 Jan 2019 14:08:22 +0800 Subject: [PATCH] HBASE-21662 Add append_peer_exclude_namespaces and remove_peer_exclude_namespaces shell commands Signed-off-by: Guanghao Zhang --- .../src/main/ruby/hbase/replication_admin.rb | 39 ++++++++++ hbase-shell/src/main/ruby/shell.rb | 2 + .../append_peer_exclude_namespaces.rb | 47 ++++++++++++ .../remove_peer_exclude_namespaces.rb | 45 +++++++++++ .../test/ruby/hbase/replication_admin_test.rb | 74 +++++++++++++++++++ 5 files changed, 207 insertions(+) create mode 100644 hbase-shell/src/main/ruby/shell/commands/append_peer_exclude_namespaces.rb create mode 100644 hbase-shell/src/main/ruby/shell/commands/remove_peer_exclude_namespaces.rb diff --git a/hbase-shell/src/main/ruby/hbase/replication_admin.rb b/hbase-shell/src/main/ruby/hbase/replication_admin.rb index d46b46810dc..af2b0718367 100644 --- a/hbase-shell/src/main/ruby/hbase/replication_admin.rb +++ b/hbase-shell/src/main/ruby/hbase/replication_admin.rb @@ -282,6 +282,45 @@ module Hbase end end + # Append exclude namespaces config for the specified peer + def append_peer_exclude_namespaces(id, namespaces) + unless namespaces.nil? + rpc = get_peer_config(id) + unless rpc.nil? + if rpc.getExcludeNamespaces.nil? + ns_set = java.util.HashSet.new + else + ns_set = java.util.HashSet.new(rpc.getExcludeNamespaces) + end + namespaces.each do |n| + ns_set.add(n) + end + builder = ReplicationPeerConfig.newBuilder(rpc) + builder.setExcludeNamespaces(ns_set) + @admin.updateReplicationPeerConfig(id, builder.build) + end + end + end + + # Remove exclude namespaces config for the specified peer + def remove_peer_exclude_namespaces(id, namespaces) + unless namespaces.nil? + rpc = get_peer_config(id) + unless rpc.nil? + ns_set = rpc.getExcludeNamespaces + unless ns_set.nil? + ns_set = java.util.HashSet.new(ns_set) + namespaces.each do |n| + ns_set.remove(n) + end + end + builder = ReplicationPeerConfig.newBuilder(rpc) + builder.setExcludeNamespaces(ns_set) + @admin.updateReplicationPeerConfig(id, builder.build) + end + end + end + def set_peer_replicate_all(id, replicate_all) rpc = get_peer_config(id) return if rpc.nil? diff --git a/hbase-shell/src/main/ruby/shell.rb b/hbase-shell/src/main/ruby/shell.rb index 9aada3a62b1..0ed2623dfde 100644 --- a/hbase-shell/src/main/ruby/shell.rb +++ b/hbase-shell/src/main/ruby/shell.rb @@ -384,6 +384,8 @@ Shell.load_command_group( append_peer_namespaces remove_peer_namespaces set_peer_exclude_namespaces + append_peer_exclude_namespaces + remove_peer_exclude_namespaces show_peer_tableCFs set_peer_tableCFs set_peer_exclude_tableCFs diff --git a/hbase-shell/src/main/ruby/shell/commands/append_peer_exclude_namespaces.rb b/hbase-shell/src/main/ruby/shell/commands/append_peer_exclude_namespaces.rb new file mode 100644 index 00000000000..4f500c8dcef --- /dev/null +++ b/hbase-shell/src/main/ruby/shell/commands/append_peer_exclude_namespaces.rb @@ -0,0 +1,47 @@ +# +# +# 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 AppendPeerExcludeNamespaces < Command + def help + <<-EOF +Append the namespaces which not replicated for the specified peer. + +Note: + 1. The replicate_all flag need to be true when append exclude namespaces. + 2. Append a exclude namespace in the peer config means that all tables in this + namespace will not be replicated to the peer cluster. If peer config + already has a exclude table, then not allow append this table's namespace + as a exclude namespace. + +Examples: + + # append ns1,ns2 to be not replicable for peer '2'. + hbase> append_peer_exclude_namespaces '2', ["ns1", "ns2"] + + EOF + end + + def command(id, namespaces) + replication_admin.append_peer_exclude_namespaces(id, namespaces) + end + end + end +end diff --git a/hbase-shell/src/main/ruby/shell/commands/remove_peer_exclude_namespaces.rb b/hbase-shell/src/main/ruby/shell/commands/remove_peer_exclude_namespaces.rb new file mode 100644 index 00000000000..0298ce9b6d1 --- /dev/null +++ b/hbase-shell/src/main/ruby/shell/commands/remove_peer_exclude_namespaces.rb @@ -0,0 +1,45 @@ +# +# +# 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 RemovePeerExcludeNamespaces < Command + def help + <<-EOF +Remove the namespaces which not replicated for the specified peer. + +Note: + 1. The replicate_all flag need to be true when remove exclude namespaces. + 2. Remove a exclude namespace in the peer config means that all tables in this + namespace will be replicated to the peer cluster. + +Examples: + + # remove ns1 from the not replicable namespaces for peer '2'. + hbase> remove_peer_exclude_namespaces '2', ["ns1", "ns2"] + + EOF + end + + def command(id, namespaces) + replication_admin.remove_peer_exclude_namespaces(id, namespaces) + end + end + end +end 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 072aab214b6..5e2dbea2cba 100644 --- a/hbase-shell/src/test/ruby/hbase/replication_admin_test.rb +++ b/hbase-shell/src/test/ruby/hbase/replication_admin_test.rb @@ -587,6 +587,80 @@ module Hbase assert_equal("value2", Bytes.to_string(peer_config.get_peer_data.get(Bytes.toBytes("data2")))) end + define_test "append_peer_exclude_namespaces: works with namespaces array" do + cluster_key = "zk4,zk5,zk6:11000:/hbase-test" + args = {CLUSTER_KEY => cluster_key} + command(:add_peer, @peer_id, args) + command(:set_peer_replicate_all, @peer_id, true) + + namespaces = ["ns1", "ns2"] + namespaces_str = "!ns1;ns2" + command(:append_peer_exclude_namespaces, @peer_id, namespaces) + assert_equal(1, command(:list_peers).length) + assert_equal(@peer_id, command(:list_peers).get(0).getPeerId) + peer_config = command(:list_peers).get(0).getPeerConfig + assert_equal(namespaces_str, + replication_admin.show_peer_exclude_namespaces(peer_config)) + + namespaces = ["ns3"] + namespaces_str = "!ns1;ns2;ns3" + command(:append_peer_exclude_namespaces, @peer_id, namespaces) + assert_equal(1, command(:list_peers).length) + assert_equal(@peer_id, command(:list_peers).get(0).getPeerId) + peer_config = command(:list_peers).get(0).getPeerConfig + assert_equal(namespaces_str, + replication_admin.show_peer_exclude_namespaces(peer_config)) + + # append a namespace which is already excluded in the peer config + command(:append_peer_exclude_namespaces, @peer_id, namespaces) + assert_equal(1, command(:list_peers).length) + assert_equal(@peer_id, command(:list_peers).get(0).getPeerId) + peer_config = command(:list_peers).get(0).getPeerConfig + assert_equal(namespaces_str, + replication_admin.show_peer_exclude_namespaces(peer_config)) + + # cleanup for future tests + command(:remove_peer, @peer_id) + end + + define_test "remove_peer_exclude_namespaces: works with namespaces array" do + cluster_key = "zk4,zk5,zk6:11000:/hbase-test" + args = {CLUSTER_KEY => cluster_key} + command(:add_peer, @peer_id, args) + + namespaces = ["ns1", "ns2", "ns3"] + command(:set_peer_exclude_namespaces, @peer_id, namespaces) + + namespaces = ["ns1", "ns2"] + namespaces_str = "!ns3" + command(:remove_peer_exclude_namespaces, @peer_id, namespaces) + assert_equal(1, command(:list_peers).length) + assert_equal(@peer_id, command(:list_peers).get(0).getPeerId) + peer_config = command(:list_peers).get(0).getPeerConfig + assert_equal(namespaces_str, + replication_admin.show_peer_exclude_namespaces(peer_config)) + + namespaces = ["ns3"] + namespaces_str = nil + command(:remove_peer_exclude_namespaces, @peer_id, namespaces) + assert_equal(1, command(:list_peers).length) + assert_equal(@peer_id, command(:list_peers).get(0).getPeerId) + peer_config = command(:list_peers).get(0).getPeerConfig + assert_equal(namespaces_str, + replication_admin.show_peer_exclude_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_equal(@peer_id, command(:list_peers).get(0).getPeerId) + peer_config = command(:list_peers).get(0).getPeerConfig + assert_equal(namespaces_str, + replication_admin.show_peer_exclude_namespaces(peer_config)) + + # cleanup for future tests + command(:remove_peer, @peer_id) + end + # assert_raise fails on native exceptions - https://jira.codehaus.org/browse/JRUBY-5279 # Can't catch native Java exception with assert_raise in JRuby 1.6.8 as in the test below. # define_test "add_peer: adding a second peer with same id should error" do