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