001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.replication; 019 020import java.util.HashMap; 021import java.util.HashSet; 022import java.util.List; 023import java.util.Map; 024import java.util.Set; 025import org.apache.hadoop.hbase.HBaseClassTestRule; 026import org.apache.hadoop.hbase.TableName; 027import org.apache.hadoop.hbase.testclassification.ReplicationTests; 028import org.apache.hadoop.hbase.testclassification.SmallTests; 029import org.junit.Assert; 030import org.junit.ClassRule; 031import org.junit.Test; 032import org.junit.experimental.categories.Category; 033 034@Category({ ReplicationTests.class, SmallTests.class }) 035public class TestReplicationUtil { 036 037 @ClassRule 038 public static final HBaseClassTestRule CLASS_RULE = 039 HBaseClassTestRule.forClass(TestReplicationUtil.class); 040 041 private static TableName TABLE_A = TableName.valueOf("replication", "testA"); 042 private static TableName TABLE_B = TableName.valueOf("replication", "testB"); 043 044 @Test 045 public void testContainsWithReplicatingAll() { 046 ReplicationPeerConfig peerConfig; 047 ReplicationPeerConfig.ReplicationPeerConfigBuilderImpl builder = 048 new ReplicationPeerConfig.ReplicationPeerConfigBuilderImpl(); 049 Map<TableName, List<String>> tableCfs = new HashMap<>(); 050 Set<String> namespaces = new HashSet<>(); 051 052 // 1. replication_all flag is true, no namespaces and table-cfs config 053 builder.setReplicateAllUserTables(true); 054 peerConfig = builder.build(); 055 Assert.assertTrue(ReplicationUtils.contains(peerConfig, TABLE_A)); 056 057 // 2. replicate_all flag is true, and config in excludedTableCfs 058 builder.setExcludeNamespaces(null); 059 // empty map 060 tableCfs = new HashMap<>(); 061 builder.setReplicateAllUserTables(true); 062 builder.setExcludeTableCFsMap(tableCfs); 063 peerConfig = builder.build(); 064 Assert.assertTrue(ReplicationUtils.contains(peerConfig, TABLE_A)); 065 066 // table testB 067 tableCfs = new HashMap<>(); 068 tableCfs.put(TABLE_B, null); 069 builder.setReplicateAllUserTables(true); 070 builder.setExcludeTableCFsMap(tableCfs); 071 peerConfig = builder.build(); 072 Assert.assertTrue(ReplicationUtils.contains(peerConfig, TABLE_A)); 073 074 // table testA 075 tableCfs = new HashMap<>(); 076 tableCfs.put(TABLE_A, null); 077 builder.setReplicateAllUserTables(true); 078 builder.setExcludeTableCFsMap(tableCfs); 079 peerConfig = builder.build(); 080 Assert.assertFalse(ReplicationUtils.contains(peerConfig, TABLE_A)); 081 082 // 3. replicate_all flag is true, and config in excludeNamespaces 083 builder.setExcludeTableCFsMap(null); 084 // empty set 085 namespaces = new HashSet<>(); 086 builder.setReplicateAllUserTables(true); 087 builder.setExcludeNamespaces(namespaces); 088 peerConfig = builder.build(); 089 Assert.assertTrue(ReplicationUtils.contains(peerConfig, TABLE_A)); 090 091 // namespace default 092 namespaces = new HashSet<>(); 093 namespaces.add("default"); 094 builder.setReplicateAllUserTables(true); 095 builder.setExcludeNamespaces(namespaces); 096 peerConfig = builder.build(); 097 Assert.assertTrue(ReplicationUtils.contains(peerConfig, TABLE_A)); 098 099 // namespace replication 100 namespaces = new HashSet<>(); 101 namespaces.add("replication"); 102 builder.setReplicateAllUserTables(true); 103 builder.setExcludeNamespaces(namespaces); 104 peerConfig = builder.build(); 105 Assert.assertFalse(ReplicationUtils.contains(peerConfig, TABLE_A)); 106 107 // 4. replicate_all flag is true, and config excludeNamespaces and excludedTableCfs both 108 // Namespaces config doesn't conflict with table-cfs config 109 namespaces = new HashSet<>(); 110 tableCfs = new HashMap<>(); 111 namespaces.add("replication"); 112 tableCfs.put(TABLE_A, null); 113 builder.setReplicateAllUserTables(true); 114 builder.setExcludeTableCFsMap(tableCfs); 115 builder.setExcludeNamespaces(namespaces); 116 peerConfig = builder.build(); 117 Assert.assertFalse(ReplicationUtils.contains(peerConfig, TABLE_A)); 118 119 // Namespaces config conflicts with table-cfs config 120 namespaces = new HashSet<>(); 121 tableCfs = new HashMap<>(); 122 namespaces.add("default"); 123 tableCfs.put(TABLE_A, null); 124 builder.setReplicateAllUserTables(true); 125 builder.setExcludeTableCFsMap(tableCfs); 126 builder.setExcludeNamespaces(namespaces); 127 peerConfig = builder.build(); 128 Assert.assertFalse(ReplicationUtils.contains(peerConfig, TABLE_A)); 129 130 namespaces = new HashSet<>(); 131 tableCfs = new HashMap<>(); 132 namespaces.add("replication"); 133 tableCfs.put(TABLE_B, null); 134 builder.setReplicateAllUserTables(true); 135 builder.setExcludeTableCFsMap(tableCfs); 136 builder.setExcludeNamespaces(namespaces); 137 peerConfig = builder.build(); 138 Assert.assertFalse(ReplicationUtils.contains(peerConfig, TABLE_A)); 139 140 } 141 142 @Test 143 public void testContainsWithoutReplicatingAll() { 144 ReplicationPeerConfig peerConfig; 145 ReplicationPeerConfig.ReplicationPeerConfigBuilderImpl builder = 146 new ReplicationPeerConfig.ReplicationPeerConfigBuilderImpl(); 147 Map<TableName, List<String>> tableCfs = new HashMap<>(); 148 Set<String> namespaces = new HashSet<>(); 149 150 // 1. replication_all flag is false, no namespaces and table-cfs config 151 builder.setReplicateAllUserTables(false); 152 peerConfig = builder.build(); 153 Assert.assertFalse(ReplicationUtils.contains(peerConfig, TABLE_A)); 154 155 // 2. replicate_all flag is false, and only config table-cfs in peer 156 // empty map 157 builder.setReplicateAllUserTables(false); 158 builder.setTableCFsMap(tableCfs); 159 peerConfig = builder.build(); 160 Assert.assertFalse(ReplicationUtils.contains(peerConfig, TABLE_A)); 161 162 // table testB 163 tableCfs = new HashMap<>(); 164 tableCfs.put(TABLE_B, null); 165 builder.setReplicateAllUserTables(false); 166 builder.setTableCFsMap(tableCfs); 167 peerConfig = builder.build(); 168 Assert.assertFalse(ReplicationUtils.contains(peerConfig, TABLE_A)); 169 170 // table testA 171 tableCfs = new HashMap<>(); 172 tableCfs.put(TABLE_A, null); 173 builder.setReplicateAllUserTables(false); 174 builder.setTableCFsMap(tableCfs); 175 peerConfig = builder.build(); 176 Assert.assertTrue(ReplicationUtils.contains(peerConfig, TABLE_A)); 177 178 // 3. replication_all flag is false, and only config namespace in peer 179 builder.setTableCFsMap(null); 180 // empty set 181 builder.setReplicateAllUserTables(false); 182 builder.setNamespaces(namespaces); 183 peerConfig = builder.build(); 184 Assert.assertFalse(ReplicationUtils.contains(peerConfig, TABLE_A)); 185 186 // namespace default 187 namespaces = new HashSet<>(); 188 namespaces.add("default"); 189 builder.setReplicateAllUserTables(false); 190 builder.setNamespaces(namespaces); 191 peerConfig = builder.build(); 192 Assert.assertFalse(ReplicationUtils.contains(peerConfig, TABLE_A)); 193 194 // namespace replication 195 namespaces = new HashSet<>(); 196 namespaces.add("replication"); 197 builder.setReplicateAllUserTables(false); 198 builder.setNamespaces(namespaces); 199 peerConfig = builder.build(); 200 Assert.assertTrue(ReplicationUtils.contains(peerConfig, TABLE_A)); 201 202 // 4. replicate_all flag is false, and config namespaces and table-cfs both 203 // Namespaces config doesn't conflict with table-cfs config 204 namespaces = new HashSet<>(); 205 tableCfs = new HashMap<>(); 206 namespaces.add("replication"); 207 tableCfs.put(TABLE_A, null); 208 builder.setReplicateAllUserTables(false); 209 builder.setTableCFsMap(tableCfs); 210 builder.setNamespaces(namespaces); 211 peerConfig = builder.build(); 212 Assert.assertTrue(ReplicationUtils.contains(peerConfig, TABLE_A)); 213 214 // Namespaces config conflicts with table-cfs config 215 namespaces = new HashSet<>(); 216 tableCfs = new HashMap<>(); 217 namespaces.add("default"); 218 tableCfs.put(TABLE_A, null); 219 builder.setReplicateAllUserTables(false); 220 builder.setTableCFsMap(tableCfs); 221 builder.setNamespaces(namespaces); 222 peerConfig = builder.build(); 223 Assert.assertTrue(ReplicationUtils.contains(peerConfig, TABLE_A)); 224 225 namespaces = new HashSet<>(); 226 tableCfs = new HashMap<>(); 227 namespaces.add("replication"); 228 tableCfs.put(TABLE_B, null); 229 builder.setReplicateAllUserTables(false); 230 builder.setTableCFsMap(tableCfs); 231 builder.setNamespaces(namespaces); 232 peerConfig = builder.build(); 233 Assert.assertTrue(ReplicationUtils.contains(peerConfig, TABLE_A)); 234 } 235}