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 static org.junit.Assert.assertArrayEquals; 021import static org.junit.Assert.assertEquals; 022import static org.junit.Assert.fail; 023import java.io.IOException; 024import java.util.ArrayList; 025import java.util.List; 026import java.util.Optional; 027import org.apache.hadoop.conf.Configuration; 028import org.apache.hadoop.hbase.ArrayBackedTag; 029import org.apache.hadoop.hbase.Cell; 030import org.apache.hadoop.hbase.CellUtil; 031import org.apache.hadoop.hbase.HBaseClassTestRule; 032import org.apache.hadoop.hbase.HBaseConfiguration; 033import org.apache.hadoop.hbase.HBaseTestingUtility; 034import org.apache.hadoop.hbase.HColumnDescriptor; 035import org.apache.hadoop.hbase.HConstants; 036import org.apache.hadoop.hbase.HTableDescriptor; 037import org.apache.hadoop.hbase.KeyValue; 038import org.apache.hadoop.hbase.KeyValueUtil; 039import org.apache.hadoop.hbase.PrivateCellUtil; 040import org.apache.hadoop.hbase.TableName; 041import org.apache.hadoop.hbase.Tag; 042import org.apache.hadoop.hbase.client.Admin; 043import org.apache.hadoop.hbase.client.Connection; 044import org.apache.hadoop.hbase.client.ConnectionFactory; 045import org.apache.hadoop.hbase.client.Durability; 046import org.apache.hadoop.hbase.client.Get; 047import org.apache.hadoop.hbase.client.Put; 048import org.apache.hadoop.hbase.client.Result; 049import org.apache.hadoop.hbase.client.Table; 050import org.apache.hadoop.hbase.client.replication.ReplicationAdmin; 051import org.apache.hadoop.hbase.codec.KeyValueCodecWithTags; 052import org.apache.hadoop.hbase.coprocessor.CoprocessorHost; 053import org.apache.hadoop.hbase.coprocessor.ObserverContext; 054import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor; 055import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment; 056import org.apache.hadoop.hbase.coprocessor.RegionObserver; 057import org.apache.hadoop.hbase.testclassification.MediumTests; 058import org.apache.hadoop.hbase.testclassification.ReplicationTests; 059import org.apache.hadoop.hbase.util.Bytes; 060import org.apache.hadoop.hbase.wal.WALEdit; 061import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster; 062import org.junit.AfterClass; 063import org.junit.BeforeClass; 064import org.junit.ClassRule; 065import org.junit.Test; 066import org.junit.experimental.categories.Category; 067import org.slf4j.Logger; 068import org.slf4j.LoggerFactory; 069 070@Category({ReplicationTests.class, MediumTests.class}) 071public class TestReplicationWithTags { 072 073 @ClassRule 074 public static final HBaseClassTestRule CLASS_RULE = 075 HBaseClassTestRule.forClass(TestReplicationWithTags.class); 076 077 private static final Logger LOG = LoggerFactory.getLogger(TestReplicationWithTags.class); 078 private static final byte TAG_TYPE = 1; 079 080 private static Configuration conf1 = HBaseConfiguration.create(); 081 private static Configuration conf2; 082 083 private static ReplicationAdmin replicationAdmin; 084 085 private static Connection connection1; 086 private static Connection connection2; 087 088 private static Table htable1; 089 private static Table htable2; 090 091 private static HBaseTestingUtility utility1; 092 private static HBaseTestingUtility utility2; 093 private static final long SLEEP_TIME = 500; 094 private static final int NB_RETRIES = 10; 095 096 private static final TableName TABLE_NAME = TableName.valueOf("TestReplicationWithTags"); 097 private static final byte[] FAMILY = Bytes.toBytes("f"); 098 private static final byte[] ROW = Bytes.toBytes("row"); 099 100 @BeforeClass 101 public static void setUpBeforeClass() throws Exception { 102 conf1.setInt("hfile.format.version", 3); 103 conf1.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/1"); 104 conf1.setInt("replication.source.size.capacity", 10240); 105 conf1.setLong("replication.source.sleepforretries", 100); 106 conf1.setInt("hbase.regionserver.maxlogs", 10); 107 conf1.setLong("hbase.master.logcleaner.ttl", 10); 108 conf1.setInt("zookeeper.recovery.retry", 1); 109 conf1.setInt("zookeeper.recovery.retry.intervalmill", 10); 110 conf1.setLong(HConstants.THREAD_WAKE_FREQUENCY, 100); 111 conf1.setInt("replication.stats.thread.period.seconds", 5); 112 conf1.setBoolean("hbase.tests.use.shortcircuit.reads", false); 113 conf1.setStrings(HConstants.REPLICATION_CODEC_CONF_KEY, KeyValueCodecWithTags.class.getName()); 114 conf1.setStrings(CoprocessorHost.USER_REGION_COPROCESSOR_CONF_KEY, 115 TestCoprocessorForTagsAtSource.class.getName()); 116 117 utility1 = new HBaseTestingUtility(conf1); 118 utility1.startMiniZKCluster(); 119 MiniZooKeeperCluster miniZK = utility1.getZkCluster(); 120 // Have to reget conf1 in case zk cluster location different 121 // than default 122 conf1 = utility1.getConfiguration(); 123 LOG.info("Setup first Zk"); 124 125 // Base conf2 on conf1 so it gets the right zk cluster. 126 conf2 = HBaseConfiguration.create(conf1); 127 conf2.setInt("hfile.format.version", 3); 128 conf2.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/2"); 129 conf2.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 6); 130 conf2.setBoolean("hbase.tests.use.shortcircuit.reads", false); 131 conf2.setStrings(HConstants.REPLICATION_CODEC_CONF_KEY, KeyValueCodecWithTags.class.getName()); 132 conf2.setStrings(CoprocessorHost.USER_REGION_COPROCESSOR_CONF_KEY, 133 TestCoprocessorForTagsAtSink.class.getName()); 134 135 utility2 = new HBaseTestingUtility(conf2); 136 utility2.setZkCluster(miniZK); 137 138 LOG.info("Setup second Zk"); 139 utility1.startMiniCluster(2); 140 utility2.startMiniCluster(2); 141 142 replicationAdmin = new ReplicationAdmin(conf1); 143 ReplicationPeerConfig rpc = new ReplicationPeerConfig(); 144 rpc.setClusterKey(utility2.getClusterKey()); 145 replicationAdmin.addPeer("2", rpc, null); 146 147 HTableDescriptor table = new HTableDescriptor(TABLE_NAME); 148 HColumnDescriptor fam = new HColumnDescriptor(FAMILY); 149 fam.setMaxVersions(3); 150 fam.setScope(HConstants.REPLICATION_SCOPE_GLOBAL); 151 table.addFamily(fam); 152 try (Connection conn = ConnectionFactory.createConnection(conf1); 153 Admin admin = conn.getAdmin()) { 154 admin.createTable(table, HBaseTestingUtility.KEYS_FOR_HBA_CREATE_TABLE); 155 } 156 try (Connection conn = ConnectionFactory.createConnection(conf2); 157 Admin admin = conn.getAdmin()) { 158 admin.createTable(table, HBaseTestingUtility.KEYS_FOR_HBA_CREATE_TABLE); 159 } 160 htable1 = utility1.getConnection().getTable(TABLE_NAME); 161 htable2 = utility2.getConnection().getTable(TABLE_NAME); 162 } 163 164 @AfterClass 165 public static void tearDownAfterClass() throws Exception { 166 utility2.shutdownMiniCluster(); 167 utility1.shutdownMiniCluster(); 168 } 169 170 @Test 171 public void testReplicationWithCellTags() throws Exception { 172 LOG.info("testSimplePutDelete"); 173 Put put = new Put(ROW); 174 put.setAttribute("visibility", Bytes.toBytes("myTag3")); 175 put.addColumn(FAMILY, ROW, ROW); 176 177 htable1 = utility1.getConnection().getTable(TABLE_NAME); 178 htable1.put(put); 179 180 Get get = new Get(ROW); 181 try { 182 for (int i = 0; i < NB_RETRIES; i++) { 183 if (i == NB_RETRIES - 1) { 184 fail("Waited too much time for put replication"); 185 } 186 Result res = htable2.get(get); 187 if (res.isEmpty()) { 188 LOG.info("Row not available"); 189 Thread.sleep(SLEEP_TIME); 190 } else { 191 assertArrayEquals(ROW, res.value()); 192 assertEquals(1, TestCoprocessorForTagsAtSink.TAGS.size()); 193 Tag tag = TestCoprocessorForTagsAtSink.TAGS.get(0); 194 assertEquals(TAG_TYPE, tag.getType()); 195 break; 196 } 197 } 198 } finally { 199 TestCoprocessorForTagsAtSink.TAGS = null; 200 } 201 } 202 203 public static class TestCoprocessorForTagsAtSource implements RegionCoprocessor, RegionObserver { 204 @Override 205 public Optional<RegionObserver> getRegionObserver() { 206 return Optional.of(this); 207 } 208 209 @Override 210 public void prePut(final ObserverContext<RegionCoprocessorEnvironment> e, final Put put, 211 final WALEdit edit, final Durability durability) throws IOException { 212 byte[] attribute = put.getAttribute("visibility"); 213 byte[] cf = null; 214 List<Cell> updatedCells = new ArrayList<>(); 215 if (attribute != null) { 216 for (List<? extends Cell> edits : put.getFamilyCellMap().values()) { 217 for (Cell cell : edits) { 218 KeyValue kv = KeyValueUtil.ensureKeyValue(cell); 219 if (cf == null) { 220 cf = CellUtil.cloneFamily(kv); 221 } 222 Tag tag = new ArrayBackedTag(TAG_TYPE, attribute); 223 List<Tag> tagList = new ArrayList<>(1); 224 tagList.add(tag); 225 226 KeyValue newKV = new KeyValue(CellUtil.cloneRow(kv), 0, kv.getRowLength(), 227 CellUtil.cloneFamily(kv), 0, kv.getFamilyLength(), CellUtil.cloneQualifier(kv), 0, 228 kv.getQualifierLength(), kv.getTimestamp(), 229 KeyValue.Type.codeToType(kv.getTypeByte()), CellUtil.cloneValue(kv), 0, 230 kv.getValueLength(), tagList); 231 ((List<Cell>) updatedCells).add(newKV); 232 } 233 } 234 put.getFamilyCellMap().remove(cf); 235 // Update the family map 236 put.getFamilyCellMap().put(cf, updatedCells); 237 } 238 } 239 } 240 241 public static class TestCoprocessorForTagsAtSink implements RegionCoprocessor, RegionObserver { 242 private static List<Tag> TAGS = null; 243 244 @Override 245 public Optional<RegionObserver> getRegionObserver() { 246 return Optional.of(this); 247 } 248 249 @Override 250 public void postGetOp(ObserverContext<RegionCoprocessorEnvironment> e, Get get, 251 List<Cell> results) throws IOException { 252 if (results.size() > 0) { 253 // Check tag presence in the 1st cell in 1st Result 254 if (!results.isEmpty()) { 255 Cell cell = results.get(0); 256 TAGS = PrivateCellUtil.getTags(cell); 257 } 258 } 259 } 260 } 261}