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