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 org.apache.hadoop.hbase.HBaseClassTestRule;
021import org.apache.hadoop.hbase.HConstants;
022import org.apache.hadoop.hbase.TableName;
023import org.apache.hadoop.hbase.client.Admin;
024import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
025import org.apache.hadoop.hbase.client.Connection;
026import org.apache.hadoop.hbase.client.ConnectionFactory;
027import org.apache.hadoop.hbase.client.Put;
028import org.apache.hadoop.hbase.client.Table;
029import org.apache.hadoop.hbase.client.TableDescriptor;
030import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
031import org.apache.hadoop.hbase.testclassification.LargeTests;
032import org.apache.hadoop.hbase.testclassification.ReplicationTests;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.junit.BeforeClass;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038
039@Category({ ReplicationTests.class, LargeTests.class })
040public class TestEditsBehindDroppedTableTiming extends ReplicationDroppedTablesTestBase {
041
042  @ClassRule
043  public static final HBaseClassTestRule CLASS_RULE =
044    HBaseClassTestRule.forClass(TestEditsBehindDroppedTableTiming.class);
045
046  @BeforeClass
047  public static void setUpBeforeClass() throws Exception {
048    setupClusters(true);
049  }
050
051  @Test
052  public void testEditsBehindDroppedTableTiming() throws Exception {
053    TableName tablename = TableName.valueOf("testdroppedtimed");
054    byte[] familyName = Bytes.toBytes("fam");
055    byte[] row = Bytes.toBytes("row");
056
057    TableDescriptor table =
058      TableDescriptorBuilder.newBuilder(tablename).setColumnFamily(ColumnFamilyDescriptorBuilder
059        .newBuilder(familyName).setScope(HConstants.REPLICATION_SCOPE_GLOBAL).build()).build();
060
061    Connection connection1 = ConnectionFactory.createConnection(CONF1);
062    Connection connection2 = ConnectionFactory.createConnection(CONF2);
063    try (Admin admin1 = connection1.getAdmin()) {
064      admin1.createTable(table);
065    }
066    try (Admin admin2 = connection2.getAdmin()) {
067      admin2.createTable(table);
068    }
069    UTIL1.waitUntilAllRegionsAssigned(tablename);
070    UTIL2.waitUntilAllRegionsAssigned(tablename);
071
072    // now suspend replication
073    try (Admin admin1 = connection1.getAdmin()) {
074      admin1.disableReplicationPeer(PEER_ID2);
075    }
076
077    // put some data (lead with 0 so the edit gets sorted before the other table's edits
078    // in the replication batch) write a bunch of edits, making sure we fill a batch
079    try (Table droppedTable = connection1.getTable(tablename)) {
080      byte[] rowKey = Bytes.toBytes(0 + " put on table to be dropped");
081      Put put = new Put(rowKey);
082      put.addColumn(familyName, row, VALUE);
083      droppedTable.put(put);
084    }
085
086    try (Table table1 = connection1.getTable(tableName)) {
087      for (int i = 0; i < ROWS_COUNT; i++) {
088        Put put = new Put(generateRowKey(i)).addColumn(famName, row, VALUE);
089        table1.put(put);
090      }
091    }
092
093    try (Admin admin2 = connection2.getAdmin()) {
094      admin2.disableTable(tablename);
095      admin2.deleteTable(tablename);
096    }
097
098    // edit should still be stuck
099    try (Admin admin1 = connection1.getAdmin()) {
100      // enable the replication peer.
101      admin1.enableReplicationPeer(PEER_ID2);
102      // the source table still exists, replication should be stalled
103      verifyReplicationStuck();
104
105      admin1.disableTable(tablename);
106      // still stuck, source table still exists
107      verifyReplicationStuck();
108
109      admin1.deleteTable(tablename);
110      // now the source table is gone, replication should proceed, the
111      // offending edits be dropped
112      verifyReplicationProceeded();
113    }
114  }
115}