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