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.ClientTests; 028import org.apache.hadoop.hbase.testclassification.SmallTests; 029import org.apache.hadoop.hbase.util.BuilderStyleTest; 030import org.junit.Assert; 031import org.junit.ClassRule; 032import org.junit.Test; 033import org.junit.experimental.categories.Category; 034 035@Category({ClientTests.class, SmallTests.class}) 036public class TestReplicationPeerConfig { 037 038 @ClassRule 039 public static final HBaseClassTestRule CLASS_RULE = 040 HBaseClassTestRule.forClass(TestReplicationPeerConfig.class); 041 042 private static TableName TABLE_A = TableName.valueOf("replication", "testA"); 043 private static TableName TABLE_B = TableName.valueOf("replication", "testB"); 044 045 @Test 046 public void testClassMethodsAreBuilderStyle() { 047 /* ReplicationPeerConfig should have a builder style setup where setXXX/addXXX methods 048 * can be chainable together: 049 * . For example: 050 * ReplicationPeerConfig htd 051 * = new ReplicationPeerConfig() 052 * .setFoo(foo) 053 * .setBar(bar) 054 * .setBuz(buz) 055 * 056 * This test ensures that all methods starting with "set" returns the declaring object 057 */ 058 059 BuilderStyleTest.assertClassesAreBuilderStyle(ReplicationPeerConfig.class); 060 } 061 062 @Test 063 public void testNeedToReplicateWithReplicatingAll() { 064 ReplicationPeerConfig peerConfig; 065 ReplicationPeerConfig.ReplicationPeerConfigBuilderImpl builder = 066 new ReplicationPeerConfig.ReplicationPeerConfigBuilderImpl(); 067 Map<TableName, List<String>> tableCfs = new HashMap<>(); 068 Set<String> namespaces = new HashSet<>(); 069 070 // 1. replication_all flag is true, no namespaces and table-cfs config 071 builder.setReplicateAllUserTables(true); 072 peerConfig = builder.build(); 073 Assert.assertTrue(peerConfig.needToReplicate(TABLE_A)); 074 075 // 2. replicate_all flag is true, and config in excludedTableCfs 076 builder.setExcludeNamespaces(null); 077 // empty map 078 tableCfs = new HashMap<>(); 079 builder.setReplicateAllUserTables(true); 080 builder.setExcludeTableCFsMap(tableCfs); 081 peerConfig = builder.build(); 082 Assert.assertTrue(peerConfig.needToReplicate(TABLE_A)); 083 084 // table testB 085 tableCfs = new HashMap<>(); 086 tableCfs.put(TABLE_B, null); 087 builder.setReplicateAllUserTables(true); 088 builder.setExcludeTableCFsMap(tableCfs); 089 peerConfig = builder.build(); 090 Assert.assertTrue(peerConfig.needToReplicate(TABLE_A)); 091 092 // table testA 093 tableCfs = new HashMap<>(); 094 tableCfs.put(TABLE_A, null); 095 builder.setReplicateAllUserTables(true); 096 builder.setExcludeTableCFsMap(tableCfs); 097 peerConfig = builder.build(); 098 Assert.assertFalse(peerConfig.needToReplicate(TABLE_A)); 099 100 // 3. replicate_all flag is true, and config in excludeNamespaces 101 builder.setExcludeTableCFsMap(null); 102 // empty set 103 namespaces = new HashSet<>(); 104 builder.setReplicateAllUserTables(true); 105 builder.setExcludeNamespaces(namespaces); 106 peerConfig = builder.build(); 107 Assert.assertTrue(peerConfig.needToReplicate(TABLE_A)); 108 109 // namespace default 110 namespaces = new HashSet<>(); 111 namespaces.add("default"); 112 builder.setReplicateAllUserTables(true); 113 builder.setExcludeNamespaces(namespaces); 114 peerConfig = builder.build(); 115 Assert.assertTrue(peerConfig.needToReplicate(TABLE_A)); 116 117 // namespace replication 118 namespaces = new HashSet<>(); 119 namespaces.add("replication"); 120 builder.setReplicateAllUserTables(true); 121 builder.setExcludeNamespaces(namespaces); 122 peerConfig = builder.build(); 123 Assert.assertFalse(peerConfig.needToReplicate(TABLE_A)); 124 125 // 4. replicate_all flag is true, and config excludeNamespaces and excludedTableCfs both 126 // Namespaces config doesn't conflict with table-cfs config 127 namespaces = new HashSet<>(); 128 tableCfs = new HashMap<>(); 129 namespaces.add("replication"); 130 tableCfs.put(TABLE_A, null); 131 builder.setReplicateAllUserTables(true); 132 builder.setExcludeTableCFsMap(tableCfs); 133 builder.setExcludeNamespaces(namespaces); 134 peerConfig = builder.build(); 135 Assert.assertFalse(peerConfig.needToReplicate(TABLE_A)); 136 137 // Namespaces config conflicts with table-cfs config 138 namespaces = new HashSet<>(); 139 tableCfs = new HashMap<>(); 140 namespaces.add("default"); 141 tableCfs.put(TABLE_A, null); 142 builder.setReplicateAllUserTables(true); 143 builder.setExcludeTableCFsMap(tableCfs); 144 builder.setExcludeNamespaces(namespaces); 145 peerConfig = builder.build(); 146 Assert.assertFalse(peerConfig.needToReplicate(TABLE_A)); 147 148 namespaces = new HashSet<>(); 149 tableCfs = new HashMap<>(); 150 namespaces.add("replication"); 151 tableCfs.put(TABLE_B, null); 152 builder.setReplicateAllUserTables(true); 153 builder.setExcludeTableCFsMap(tableCfs); 154 builder.setExcludeNamespaces(namespaces); 155 peerConfig = builder.build(); 156 Assert.assertFalse(peerConfig.needToReplicate(TABLE_A)); 157 158 } 159 160 @Test 161 public void testNeedToReplicateWithoutReplicatingAll() { 162 ReplicationPeerConfig peerConfig; 163 ReplicationPeerConfig.ReplicationPeerConfigBuilderImpl builder = 164 new ReplicationPeerConfig.ReplicationPeerConfigBuilderImpl(); 165 Map<TableName, List<String>> tableCfs = new HashMap<>(); 166 Set<String> namespaces = new HashSet<>(); 167 168 // 1. replication_all flag is false, no namespaces and table-cfs config 169 builder.setReplicateAllUserTables(false); 170 peerConfig = builder.build(); 171 Assert.assertFalse(peerConfig.needToReplicate(TABLE_A)); 172 173 // 2. replicate_all flag is false, and only config table-cfs in peer 174 // empty map 175 builder.setReplicateAllUserTables(false); 176 builder.setTableCFsMap(tableCfs); 177 peerConfig = builder.build(); 178 Assert.assertFalse(peerConfig.needToReplicate(TABLE_A)); 179 180 // table testB 181 tableCfs = new HashMap<>(); 182 tableCfs.put(TABLE_B, null); 183 builder.setReplicateAllUserTables(false); 184 builder.setTableCFsMap(tableCfs); 185 peerConfig = builder.build(); 186 Assert.assertFalse(peerConfig.needToReplicate(TABLE_A)); 187 188 // table testA 189 tableCfs = new HashMap<>(); 190 tableCfs.put(TABLE_A, null); 191 builder.setReplicateAllUserTables(false); 192 builder.setTableCFsMap(tableCfs); 193 peerConfig = builder.build(); 194 Assert.assertTrue(peerConfig.needToReplicate(TABLE_A)); 195 196 // 3. replication_all flag is false, and only config namespace in peer 197 builder.setTableCFsMap(null); 198 // empty set 199 builder.setReplicateAllUserTables(false); 200 builder.setNamespaces(namespaces); 201 peerConfig = builder.build(); 202 Assert.assertFalse(peerConfig.needToReplicate(TABLE_A)); 203 204 // namespace default 205 namespaces = new HashSet<>(); 206 namespaces.add("default"); 207 builder.setReplicateAllUserTables(false); 208 builder.setNamespaces(namespaces); 209 peerConfig = builder.build(); 210 Assert.assertFalse(peerConfig.needToReplicate(TABLE_A)); 211 212 // namespace replication 213 namespaces = new HashSet<>(); 214 namespaces.add("replication"); 215 builder.setReplicateAllUserTables(false); 216 builder.setNamespaces(namespaces); 217 peerConfig = builder.build(); 218 Assert.assertTrue(peerConfig.needToReplicate(TABLE_A)); 219 220 // 4. replicate_all flag is false, and config namespaces and table-cfs both 221 // Namespaces config doesn't conflict with table-cfs config 222 namespaces = new HashSet<>(); 223 tableCfs = new HashMap<>(); 224 namespaces.add("replication"); 225 tableCfs.put(TABLE_A, null); 226 builder.setReplicateAllUserTables(false); 227 builder.setTableCFsMap(tableCfs); 228 builder.setNamespaces(namespaces); 229 peerConfig = builder.build(); 230 Assert.assertTrue(peerConfig.needToReplicate(TABLE_A)); 231 232 // Namespaces config conflicts with table-cfs config 233 namespaces = new HashSet<>(); 234 tableCfs = new HashMap<>(); 235 namespaces.add("default"); 236 tableCfs.put(TABLE_A, null); 237 builder.setReplicateAllUserTables(false); 238 builder.setTableCFsMap(tableCfs); 239 builder.setNamespaces(namespaces); 240 peerConfig = builder.build(); 241 Assert.assertTrue(peerConfig.needToReplicate(TABLE_A)); 242 243 namespaces = new HashSet<>(); 244 tableCfs = new HashMap<>(); 245 namespaces.add("replication"); 246 tableCfs.put(TABLE_B, null); 247 builder.setReplicateAllUserTables(false); 248 builder.setTableCFsMap(tableCfs); 249 builder.setNamespaces(namespaces); 250 peerConfig = builder.build(); 251 Assert.assertTrue(peerConfig.needToReplicate(TABLE_A)); 252 } 253}