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