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.master.procedure;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HBaseTestingUtil;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.client.SnapshotDescription;
031import org.apache.hadoop.hbase.client.SnapshotType;
032import org.apache.hadoop.hbase.client.Table;
033import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
034import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
035import org.apache.hadoop.hbase.procedure2.RemoteProcedureDispatcher;
036import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
037import org.apache.hadoop.hbase.testclassification.MasterTests;
038import org.apache.hadoop.hbase.testclassification.MediumTests;
039import org.apache.hadoop.hbase.util.Bytes;
040import org.apache.hadoop.hbase.util.RegionSplitter;
041import org.junit.Before;
042import org.junit.ClassRule;
043import org.junit.Test;
044import org.junit.experimental.categories.Category;
045
046import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
047import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.SnapshotState;
048import org.apache.hadoop.hbase.shaded.protobuf.generated.SnapshotProtos;
049
050@Category({ MasterTests.class, MediumTests.class })
051public class TestSnapshotProcedureEarlyExpiration extends TestSnapshotProcedure {
052  @ClassRule
053  public static final HBaseClassTestRule CLASS_RULE =
054    HBaseClassTestRule.forClass(TestSnapshotProcedureEarlyExpiration.class);
055
056  @Before
057  @Override
058  public void setup() throws Exception { // Copied from TestSnapshotProcedure with modified
059                                         // SnapshotDescription
060    TEST_UTIL = new HBaseTestingUtil();
061    Configuration config = TEST_UTIL.getConfiguration();
062    // using SnapshotVerifyProcedure to verify snapshot
063    config.setInt("hbase.snapshot.remote.verify.threshold", 1);
064    // disable info server. Info server is useful when we run unit tests locally,
065    // but it will
066    // fails integration testing of jenkins.
067    // config.setInt(HConstants.MASTER_INFO_PORT, 8080);
068
069    // delay dispatch so that we can do something, for example kill a target server
070    config.setInt(RemoteProcedureDispatcher.DISPATCH_DELAY_CONF_KEY, 10000);
071    config.setInt(RemoteProcedureDispatcher.DISPATCH_MAX_QUEUE_SIZE_CONF_KEY, 128);
072    TEST_UTIL.startMiniCluster(3);
073    master = TEST_UTIL.getHBaseCluster().getMaster();
074    TABLE_NAME = TableName.valueOf(Bytes.toBytes("SPTestTable"));
075    CF = Bytes.toBytes("cf");
076    SNAPSHOT_NAME = "SnapshotProcedureTest";
077
078    Map<String, Object> properties = new HashMap<>();
079    properties.put("TTL", 1L);
080    snapshot = new SnapshotDescription(SNAPSHOT_NAME, TABLE_NAME, SnapshotType.FLUSH, null, -1, -1,
081      properties);
082
083    snapshotProto = ProtobufUtil.createHBaseProtosSnapshotDesc(snapshot);
084    snapshotProto = SnapshotDescriptionUtils.validate(snapshotProto, master.getConfiguration());
085    final byte[][] splitKeys = new RegionSplitter.HexStringSplit().split(10);
086    Table table = TEST_UTIL.createTable(TABLE_NAME, CF, splitKeys);
087    TEST_UTIL.loadTable(table, CF, false);
088  }
089
090  @Test
091  public void testSnapshotEarlyExpiration() throws Exception {
092    ProcedureExecutor<MasterProcedureEnv> procExec = master.getMasterProcedureExecutor();
093    MasterProcedureEnv env = procExec.getEnvironment();
094    SnapshotProcedure sp = new SnapshotProcedure(env, snapshotProto);
095    SnapshotProcedure spySp = getDelayedOnSpecificStateSnapshotProcedure(sp,
096      procExec.getEnvironment(), SnapshotState.SNAPSHOT_COMPLETE_SNAPSHOT);
097
098    long procId = procExec.submitProcedure(spySp);
099
100    ProcedureTestingUtility.waitProcedure(master.getMasterProcedureExecutor(), procId);
101    assertTrue(spySp.isFailed());
102    List<SnapshotProtos.SnapshotDescription> snapshots =
103      master.getSnapshotManager().getCompletedSnapshots();
104    assertEquals(0, snapshots.size());
105  }
106}