From ff93fd23b25fd5be7b58040d47eb13f34735c3a3 Mon Sep 17 00:00:00 2001 From: tianjingyun Date: Sat, 24 Mar 2018 18:57:48 +0800 Subject: [PATCH] HBASE-20227 Add UT for ReplicationUtils.contains method Signed-off-by: zhangduo --- .../hadoop/hbase/replication/ReplicationUtils.java | 2 +- .../hbase/replication/TestReplicationUtil.java | 231 +++++++++++++++++++++ 2 files changed, 232 insertions(+), 1 deletion(-) create mode 100644 hbase-replication/src/test/java/org/apache/hadoop/hbase/replication/TestReplicationUtil.java diff --git a/hbase-replication/src/main/java/org/apache/hadoop/hbase/replication/ReplicationUtils.java b/hbase-replication/src/main/java/org/apache/hadoop/hbase/replication/ReplicationUtils.java index 1c42de4..c7568bb 100644 --- a/hbase-replication/src/main/java/org/apache/hadoop/hbase/replication/ReplicationUtils.java +++ b/hbase-replication/src/main/java/org/apache/hadoop/hbase/replication/ReplicationUtils.java @@ -145,7 +145,7 @@ public final class ReplicationUtils { if (excludeNamespaces != null && excludeNamespaces.contains(namespace)) { return false; } - Map> excludedTableCFs = peerConfig.getTableCFsMap(); + Map> excludedTableCFs = peerConfig.getExcludeTableCFsMap(); // trap here, must check existence first since HashMap allows null value. if (excludedTableCFs == null || !excludedTableCFs.containsKey(tableName)) { return true; diff --git a/hbase-replication/src/test/java/org/apache/hadoop/hbase/replication/TestReplicationUtil.java b/hbase-replication/src/test/java/org/apache/hadoop/hbase/replication/TestReplicationUtil.java new file mode 100644 index 0000000..b41628b --- /dev/null +++ b/hbase-replication/src/test/java/org/apache/hadoop/hbase/replication/TestReplicationUtil.java @@ -0,0 +1,231 @@ +/** + * 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. + */ + +package org.apache.hadoop.hbase.replication; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.testclassification.ReplicationTests; +import org.apache.hadoop.hbase.testclassification.SmallTests; +import org.junit.Assert; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +@Category({ ReplicationTests.class, SmallTests.class }) +public class TestReplicationUtil { + + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestReplicationUtil.class); + + static TableName a = TableName.valueOf("replication", "testA"); + static TableName b = TableName.valueOf("replication", "testB"); + + @Test + public void testContainsWithReplicatingAll() { + ReplicationPeerConfig peerConfig; + ReplicationPeerConfig.ReplicationPeerConfigBuilderImpl builder = + new ReplicationPeerConfig.ReplicationPeerConfigBuilderImpl(); + Map> tableCfs = new HashMap<>(); + Set namespaces = new HashSet<>(); + + // 1. replication_all flag is true, no namespaces and table-cfs config + builder.setReplicateAllUserTables(true); + peerConfig = builder.build(); + Assert.assertTrue(ReplicationUtils.contains(peerConfig, a)); + + // 2. replicate_all flag is true, and config in excludedTableCfs + builder.setExcludeNamespaces(null); + // empty map + tableCfs = new HashMap<>(); + builder.setReplicateAllUserTables(true); + builder.setExcludeTableCFsMap(tableCfs); + peerConfig = builder.build(); + Assert.assertTrue(ReplicationUtils.contains(peerConfig, a)); + + // table testB + tableCfs = new HashMap<>(); + tableCfs.put(b, null); + builder.setReplicateAllUserTables(true); + builder.setExcludeTableCFsMap(tableCfs); + peerConfig = builder.build(); + Assert.assertTrue(ReplicationUtils.contains(peerConfig, a)); + + // table testA + tableCfs = new HashMap<>(); + tableCfs.put(a, null); + builder.setReplicateAllUserTables(true); + builder.setExcludeTableCFsMap(tableCfs); + peerConfig = builder.build(); + Assert.assertFalse(ReplicationUtils.contains(peerConfig, a)); + + // 3. replicate_all flag is true, and config in excludeNamespaces + builder.setExcludeTableCFsMap(null); + // empty set + namespaces = new HashSet<>(); + builder.setReplicateAllUserTables(true); + builder.setExcludeNamespaces(namespaces); + peerConfig = builder.build(); + Assert.assertTrue(ReplicationUtils.contains(peerConfig, a)); + + // namespace default + namespaces = new HashSet<>(); + namespaces.add("default"); + builder.setReplicateAllUserTables(true); + builder.setExcludeNamespaces(namespaces); + peerConfig = builder.build(); + Assert.assertTrue(ReplicationUtils.contains(peerConfig, a)); + + // namespace replication + namespaces = new HashSet<>(); + namespaces.add("replication"); + builder.setReplicateAllUserTables(true); + builder.setExcludeNamespaces(namespaces); + peerConfig = builder.build(); + Assert.assertFalse(ReplicationUtils.contains(peerConfig, a)); + + // 4. replicate_all flag is true, and config excludeNamespaces and excludedTableCfs both + // Namespaces config doesn't conflict with table-cfs config + namespaces = new HashSet<>(); + tableCfs = new HashMap<>(); + namespaces.add("replication"); + tableCfs.put(a, null); + builder.setReplicateAllUserTables(true); + builder.setExcludeTableCFsMap(tableCfs); + builder.setExcludeNamespaces(namespaces); + peerConfig = builder.build(); + Assert.assertFalse(ReplicationUtils.contains(peerConfig, a)); + + // Namespaces config conflicts with table-cfs config + namespaces = new HashSet<>(); + tableCfs = new HashMap<>(); + namespaces.add("default"); + tableCfs.put(a, null); + builder.setReplicateAllUserTables(true); + builder.setExcludeTableCFsMap(tableCfs); + builder.setExcludeNamespaces(namespaces); + peerConfig = builder.build(); + Assert.assertFalse(ReplicationUtils.contains(peerConfig, a)); + + namespaces = new HashSet<>(); + tableCfs = new HashMap<>(); + namespaces.add("replication"); + tableCfs.put(b, null); + builder.setReplicateAllUserTables(true); + builder.setExcludeTableCFsMap(tableCfs); + builder.setExcludeNamespaces(namespaces); + peerConfig = builder.build(); + Assert.assertFalse(ReplicationUtils.contains(peerConfig, a)); + + } + + @Test + public void testContainsWithoutReplicatingAll() { + ReplicationPeerConfig peerConfig; + ReplicationPeerConfig.ReplicationPeerConfigBuilderImpl builder = + new ReplicationPeerConfig.ReplicationPeerConfigBuilderImpl(); + Map> tableCfs = new HashMap<>(); + Set namespaces = new HashSet<>(); + + // 1. replication_all flag is false, no namespaces and table-cfs config + builder.setReplicateAllUserTables(false); + peerConfig = builder.build(); + Assert.assertFalse(ReplicationUtils.contains(peerConfig, a)); + + // 2. replicate_all flag is false, and only config table-cfs in peer + // empty map + builder.setReplicateAllUserTables(false); + builder.setTableCFsMap(tableCfs); + peerConfig = builder.build(); + Assert.assertFalse(ReplicationUtils.contains(peerConfig, a)); + + // table testB + tableCfs = new HashMap<>(); + tableCfs.put(b, null); + builder.setReplicateAllUserTables(false); + builder.setTableCFsMap(tableCfs); + peerConfig = builder.build(); + Assert.assertFalse(ReplicationUtils.contains(peerConfig, a)); + + // table testA + tableCfs = new HashMap<>(); + tableCfs.put(a, null); + builder.setReplicateAllUserTables(false); + builder.setTableCFsMap(tableCfs); + peerConfig = builder.build(); + Assert.assertTrue(ReplicationUtils.contains(peerConfig, a)); + + // 3. replication_all flag is false, and only config namespace in peer + builder.setTableCFsMap(null); + // empty set + builder.setReplicateAllUserTables(false); + builder.setNamespaces(namespaces); + peerConfig = builder.build(); + Assert.assertFalse(ReplicationUtils.contains(peerConfig, a)); + + // namespace default + namespaces = new HashSet<>(); + namespaces.add("default"); + builder.setReplicateAllUserTables(false); + builder.setNamespaces(namespaces); + peerConfig = builder.build(); + Assert.assertFalse(ReplicationUtils.contains(peerConfig, a)); + + // namespace replication + namespaces = new HashSet<>(); + namespaces.add("replication"); + builder.setReplicateAllUserTables(false); + builder.setNamespaces(namespaces); + peerConfig = builder.build(); + Assert.assertTrue(ReplicationUtils.contains(peerConfig, a)); + + // 4. replicate_all flag is false, and config namespaces and table-cfs both + // Namespaces config doesn't conflict with table-cfs config + namespaces = new HashSet<>(); + tableCfs = new HashMap<>(); + namespaces.add("replication"); + tableCfs.put(a, null); + builder.setReplicateAllUserTables(false); + builder.setTableCFsMap(tableCfs); + builder.setNamespaces(namespaces); + peerConfig = builder.build(); + Assert.assertTrue(ReplicationUtils.contains(peerConfig, a)); + + // Namespaces config conflicts with table-cfs config + namespaces = new HashSet<>(); + tableCfs = new HashMap<>(); + namespaces.add("default"); + tableCfs.put(a, null); + builder.setReplicateAllUserTables(false); + builder.setTableCFsMap(tableCfs); + builder.setNamespaces(namespaces); + peerConfig = builder.build(); + Assert.assertTrue(ReplicationUtils.contains(peerConfig, a)); + + namespaces = new HashSet<>(); + tableCfs = new HashMap<>(); + namespaces.add("replication"); + tableCfs.put(b, null); + builder.setReplicateAllUserTables(false); + builder.setTableCFsMap(tableCfs); + builder.setNamespaces(namespaces); + peerConfig = builder.build(); + Assert.assertTrue(ReplicationUtils.contains(peerConfig, a)); + } + + } -- 2.7.4