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.HBaseTestingUtility;
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.wal.WAL.Entry;
042import org.apache.hadoop.hbase.wal.WALEdit;
043import org.apache.hadoop.hbase.wal.WALKeyImpl;
044import org.junit.AfterClass;
045import org.junit.BeforeClass;
046import org.junit.ClassRule;
047import org.junit.Test;
048import org.junit.experimental.categories.Category;
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@Category({ ReplicationTests.class, MediumTests.class })
057public class TestHBaseInterClusterReplicationEndpointFilterEdits {
058
059  @ClassRule
060  public static final HBaseClassTestRule CLASS_RULE =
061      HBaseClassTestRule.forClass(TestHBaseInterClusterReplicationEndpointFilterEdits.class);
062
063  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
064
065  private static HBaseInterClusterReplicationEndpoint endpoint;
066
067  private static final TableName TABLE1 = TableName.valueOf("T1");
068  private static final TableName TABLE2 = TableName.valueOf("T2");
069
070  private static final byte[] FAMILY = Bytes.toBytes("CF");
071  private static final byte[] NON_EXISTING_FAMILY = Bytes.toBytes("NECF");
072  private static final byte[] QUALIFIER = Bytes.toBytes("Q");
073  private static final byte[] ROW = Bytes.toBytes("r");
074  private static final byte[] VALUE = Bytes.toBytes("v");
075
076  @BeforeClass
077  public static void setUpBeforeClass() throws Exception {
078    UTIL.startMiniCluster();
079    ReplicationPeer replicationPeer = mock(ReplicationPeer.class);
080    ReplicationPeerConfig rpc = mock(ReplicationPeerConfig.class);
081    when(rpc.isSerial()).thenReturn(false);
082    when(replicationPeer.getPeerConfig()).thenReturn(rpc);
083    Context context = new Context(UTIL.getConfiguration(), UTIL.getConfiguration(), null,
084        null, null, replicationPeer, null, null, null);
085    endpoint = new HBaseInterClusterReplicationEndpoint();
086    endpoint.init(context);
087
088    UTIL.createTable(TABLE1, FAMILY);
089  }
090
091  @AfterClass
092  public static void tearDownAfterClass() throws Exception {
093    UTIL.shutdownMiniCluster();
094  }
095
096  @Test
097  public void testFilterNotExistColumnFamilyEdits() {
098    List<List<Entry>> entryList = new ArrayList<>();
099    // should be filtered
100    Cell c1 = new KeyValue(ROW, NON_EXISTING_FAMILY, QUALIFIER, System.currentTimeMillis(),
101        Type.Put, VALUE);
102    Entry e1 = new Entry(new WALKeyImpl(new byte[32], TABLE1, System.currentTimeMillis()),
103        new WALEdit().add(c1));
104    entryList.add(Lists.newArrayList(e1));
105    // should be kept
106    Cell c2 = new KeyValue(ROW, FAMILY, QUALIFIER, System.currentTimeMillis(), Type.Put, VALUE);
107    Entry e2 = new Entry(new WALKeyImpl(new byte[32], TABLE1, System.currentTimeMillis()),
108        new WALEdit().add(c2));
109    entryList.add(Lists.newArrayList(e2, e1));
110    List<List<Entry>> filtered = endpoint.filterNotExistColumnFamilyEdits(entryList);
111    assertEquals(1, filtered.size());
112    assertEquals(1, filtered.get(0).get(0).getEdit().getCells().size());
113    Cell cell = filtered.get(0).get(0).getEdit().getCells().get(0);
114    assertTrue(CellUtil.matchingFamily(cell, FAMILY));
115  }
116
117  @Test
118  public void testFilterNotExistTableEdits() {
119    List<List<Entry>> entryList = new ArrayList<>();
120    // should be filtered
121    Cell c1 = new KeyValue(ROW, FAMILY, QUALIFIER, System.currentTimeMillis(), Type.Put, VALUE);
122    Entry e1 = new Entry(new WALKeyImpl(new byte[32], TABLE2, System.currentTimeMillis()),
123        new WALEdit().add(c1));
124    entryList.add(Lists.newArrayList(e1));
125    // should be kept
126    Cell c2 = new KeyValue(ROW, FAMILY, QUALIFIER, System.currentTimeMillis(), Type.Put, VALUE);
127    Entry e2 = new Entry(new WALKeyImpl(new byte[32], TABLE1, System.currentTimeMillis()),
128        new WALEdit().add(c2));
129    entryList.add(Lists.newArrayList(e2));
130    List<List<Entry>> filtered = endpoint.filterNotExistTableEdits(entryList);
131    assertEquals(1, filtered.size());
132    Entry entry = filtered.get(0).get(0);
133    assertEquals(1, entry.getEdit().getCells().size());
134    assertEquals(TABLE1, entry.getKey().getTableName());
135  }
136
137}