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.client;
019
020import java.io.IOException;
021import java.io.InterruptedIOException;
022import java.util.Optional;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HConstants;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.coprocessor.ObserverContext;
027import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor;
028import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
029import org.apache.hadoop.hbase.coprocessor.RegionObserver;
030import org.apache.hadoop.hbase.master.cleaner.TimeToLiveHFileCleaner;
031import org.apache.hadoop.hbase.mob.MobConstants;
032import org.apache.hadoop.hbase.regionserver.FlushLifeCycleTracker;
033import org.apache.hadoop.hbase.snapshot.MobSnapshotTestingUtils;
034import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
035import org.apache.hadoop.hbase.testclassification.ClientTests;
036import org.apache.hadoop.hbase.testclassification.LargeTests;
037import org.apache.hadoop.hbase.util.Bytes;
038import org.junit.BeforeClass;
039import org.junit.ClassRule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042
043@Category({ LargeTests.class, ClientTests.class })
044public class TestMobCloneSnapshotFromClientCloneLinksAfterDelete
045    extends CloneSnapshotFromClientCloneLinksAfterDeleteTestBase {
046
047  @ClassRule
048  public static final HBaseClassTestRule CLASS_RULE =
049    HBaseClassTestRule.forClass(TestMobCloneSnapshotFromClientCloneLinksAfterDelete.class);
050
051  private static boolean delayFlush = false;
052
053  /**
054   * This coprocessor is used to delay the flush.
055   */
056  public static class DelayFlushCoprocessor implements RegionCoprocessor, RegionObserver {
057
058    @Override
059    public Optional<RegionObserver> getRegionObserver() {
060      return Optional.of(this);
061    }
062
063    @Override
064    public void preFlush(ObserverContext<RegionCoprocessorEnvironment> e,
065        FlushLifeCycleTracker tracker) throws IOException {
066      if (delayFlush) {
067        try {
068          if (Bytes.compareTo(e.getEnvironment().getRegionInfo().getStartKey(),
069            HConstants.EMPTY_START_ROW) != 0) {
070            Thread.sleep(100);
071          }
072        } catch (InterruptedException e1) {
073          throw new InterruptedIOException(e1.getMessage());
074        }
075      }
076    }
077  }
078
079  protected static void setupConfiguration() {
080    CloneSnapshotFromClientTestBase.setupConfiguration();
081    TEST_UTIL.getConfiguration().setLong(TimeToLiveHFileCleaner.TTL_CONF_KEY, 0);
082    TEST_UTIL.getConfiguration().setInt(MobConstants.MOB_FILE_CACHE_SIZE_KEY, 0);
083  }
084
085  @BeforeClass
086  public static void setUpBeforeClass() throws Exception {
087    setupConfiguration();
088    TEST_UTIL.startMiniCluster(3);
089  }
090
091  @Override
092  protected void createTable() throws IOException, InterruptedException {
093    MobSnapshotTestingUtils.createMobTable(TEST_UTIL, tableName,
094      SnapshotTestingUtils.getSplitKeys(), getNumReplicas(), DelayFlushCoprocessor.class.getName(),
095      FAMILY);
096  }
097
098  @Override
099  protected int numRowsToLoad() {
100    return 20;
101  }
102
103  @Override
104  protected int countRows(Table table) throws IOException {
105    return MobSnapshotTestingUtils.countMobRows(table);
106  }
107
108  @Test
109  @Override
110  public void testCloneLinksAfterDelete() throws IOException, InterruptedException {
111    // delay the flush to make sure
112    delayFlush = true;
113    SnapshotTestingUtils.loadData(TEST_UTIL, tableName, 20, FAMILY);
114    long tid = System.currentTimeMillis();
115    byte[] snapshotName3 = Bytes.toBytes("snaptb3-" + tid);
116    TableName clonedTableName3 =
117      TableName.valueOf(name.getMethodName() + System.currentTimeMillis());
118    admin.snapshot(snapshotName3, tableName);
119    delayFlush = false;
120    int snapshot3Rows = -1;
121    try (Table table = TEST_UTIL.getConnection().getTable(tableName)) {
122      snapshot3Rows = TEST_UTIL.countRows(table);
123    }
124    admin.cloneSnapshot(snapshotName3, clonedTableName3);
125    admin.deleteSnapshot(snapshotName3);
126    super.testCloneLinksAfterDelete();
127    verifyRowCount(TEST_UTIL, clonedTableName3, snapshot3Rows);
128    admin.disableTable(clonedTableName3);
129    admin.deleteTable(clonedTableName3);
130  }
131}