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.regionserver; 019 020import static org.junit.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertTrue; 022import static org.mockito.Mockito.mock; 023import static org.mockito.Mockito.when; 024 025import java.util.ArrayList; 026import java.util.List; 027import org.apache.hadoop.hbase.Cell; 028import org.apache.hadoop.hbase.CellUtil; 029import org.apache.hadoop.hbase.ExtendedCell; 030import org.apache.hadoop.hbase.HBaseTestingUtil; 031import org.apache.hadoop.hbase.KeyValue; 032import org.apache.hadoop.hbase.KeyValue.Type; 033import org.apache.hadoop.hbase.TableName; 034import org.apache.hadoop.hbase.replication.ReplicationEndpoint.Context; 035import org.apache.hadoop.hbase.replication.ReplicationPeer; 036import org.apache.hadoop.hbase.replication.ReplicationPeerConfig; 037import org.apache.hadoop.hbase.testclassification.MediumTests; 038import org.apache.hadoop.hbase.testclassification.ReplicationTests; 039import org.apache.hadoop.hbase.util.Bytes; 040import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 041import org.apache.hadoop.hbase.wal.WAL.Entry; 042import org.apache.hadoop.hbase.wal.WALEdit; 043import org.apache.hadoop.hbase.wal.WALEditInternalHelper; 044import org.apache.hadoop.hbase.wal.WALKeyImpl; 045import org.junit.jupiter.api.AfterAll; 046import org.junit.jupiter.api.BeforeAll; 047import org.junit.jupiter.api.Tag; 048import org.junit.jupiter.api.Test; 049 050import org.apache.hbase.thirdparty.com.google.common.collect.Lists; 051 052/** 053 * Tests {@link HBaseInterClusterReplicationEndpoint#filterNotExistColumnFamilyEdits(List)} and 054 * {@link HBaseInterClusterReplicationEndpoint#filterNotExistTableEdits(List)} 055 */ 056@Tag(ReplicationTests.TAG) 057@Tag(MediumTests.TAG) 058public class TestHBaseInterClusterReplicationEndpointFilterEdits { 059 060 private static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); 061 062 private static HBaseInterClusterReplicationEndpoint endpoint; 063 064 private static final TableName TABLE1 = TableName.valueOf("T1"); 065 private static final TableName TABLE2 = TableName.valueOf("T2"); 066 067 private static final byte[] FAMILY = Bytes.toBytes("CF"); 068 private static final byte[] NON_EXISTING_FAMILY = Bytes.toBytes("NECF"); 069 private static final byte[] QUALIFIER = Bytes.toBytes("Q"); 070 private static final byte[] ROW = Bytes.toBytes("r"); 071 private static final byte[] VALUE = Bytes.toBytes("v"); 072 073 @BeforeAll 074 public static void setUpBeforeClass() throws Exception { 075 UTIL.startMiniCluster(); 076 ReplicationPeer replicationPeer = mock(ReplicationPeer.class); 077 ReplicationPeerConfig rpc = mock(ReplicationPeerConfig.class); 078 when(rpc.isSerial()).thenReturn(false); 079 when(replicationPeer.getPeerConfig()).thenReturn(rpc); 080 when(rpc.getClusterKey()).thenReturn("hbase+zk://localhost:2181"); 081 Context context = new Context(null, null, UTIL.getConfiguration(), UTIL.getConfiguration(), 082 null, null, null, replicationPeer, null, null, null); 083 endpoint = new HBaseInterClusterReplicationEndpoint(); 084 endpoint.init(context); 085 086 UTIL.createTable(TABLE1, FAMILY); 087 } 088 089 @AfterAll 090 public static void tearDownAfterClass() throws Exception { 091 UTIL.shutdownMiniCluster(); 092 } 093 094 @Test 095 public void testFilterNotExistColumnFamilyEdits() { 096 List<List<Entry>> entryList = new ArrayList<>(); 097 // should be filtered 098 ExtendedCell c1 = new KeyValue(ROW, NON_EXISTING_FAMILY, QUALIFIER, 099 EnvironmentEdgeManager.currentTime(), Type.Put, VALUE); 100 Entry e1 = new Entry(new WALKeyImpl(new byte[32], TABLE1, EnvironmentEdgeManager.currentTime()), 101 WALEditInternalHelper.addExtendedCell(new WALEdit(), c1)); 102 entryList.add(Lists.newArrayList(e1)); 103 // should be kept 104 ExtendedCell c2 = 105 new KeyValue(ROW, FAMILY, QUALIFIER, EnvironmentEdgeManager.currentTime(), Type.Put, VALUE); 106 Entry e2 = new Entry(new WALKeyImpl(new byte[32], TABLE1, EnvironmentEdgeManager.currentTime()), 107 WALEditInternalHelper.addExtendedCell(new WALEdit(), c2)); 108 entryList.add(Lists.newArrayList(e2, e1)); 109 List<List<Entry>> filtered = endpoint.filterNotExistColumnFamilyEdits(entryList); 110 assertEquals(1, filtered.size()); 111 assertEquals(1, filtered.get(0).get(0).getEdit().getCells().size()); 112 Cell cell = filtered.get(0).get(0).getEdit().getCells().get(0); 113 assertTrue(CellUtil.matchingFamily(cell, FAMILY)); 114 } 115 116 @Test 117 public void testFilterNotExistTableEdits() { 118 List<List<Entry>> entryList = new ArrayList<>(); 119 // should be filtered 120 ExtendedCell c1 = 121 new KeyValue(ROW, FAMILY, QUALIFIER, EnvironmentEdgeManager.currentTime(), Type.Put, VALUE); 122 Entry e1 = new Entry(new WALKeyImpl(new byte[32], TABLE2, EnvironmentEdgeManager.currentTime()), 123 WALEditInternalHelper.addExtendedCell(new WALEdit(), c1)); 124 entryList.add(Lists.newArrayList(e1)); 125 // should be kept 126 ExtendedCell c2 = 127 new KeyValue(ROW, FAMILY, QUALIFIER, EnvironmentEdgeManager.currentTime(), Type.Put, VALUE); 128 Entry e2 = new Entry(new WALKeyImpl(new byte[32], TABLE1, EnvironmentEdgeManager.currentTime()), 129 WALEditInternalHelper.addExtendedCell(new WALEdit(), c2)); 130 entryList.add(Lists.newArrayList(e2)); 131 List<List<Entry>> filtered = endpoint.filterNotExistTableEdits(entryList); 132 assertEquals(1, filtered.size()); 133 Entry entry = filtered.get(0).get(0); 134 assertEquals(1, entry.getEdit().getCells().size()); 135 assertEquals(TABLE1, entry.getKey().getTableName()); 136 } 137 138}