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.snapshot;
019
020import static org.junit.Assert.assertEquals;
021
022import java.util.HashMap;
023import java.util.Map;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.HBaseTestingUtil;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
028import org.apache.hadoop.hbase.client.Put;
029import org.apache.hadoop.hbase.client.Table;
030import org.apache.hadoop.hbase.client.TableDescriptor;
031import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
032import org.apache.hadoop.hbase.snapshot.SnapshotTTLExpiredException;
033import org.apache.hadoop.hbase.testclassification.MediumTests;
034import org.apache.hadoop.hbase.util.Bytes;
035import org.junit.After;
036import org.junit.Before;
037import org.junit.ClassRule;
038import org.junit.Rule;
039import org.junit.Test;
040import org.junit.experimental.categories.Category;
041import org.junit.rules.TestName;
042
043/**
044 * Unfortunately, couldn't test TakeSnapshotHandler using mocks, because it relies on TableLock,
045 * which is tightly coupled to LockManager and LockProcedure classes, which are both final and
046 * prevents us from mocking its behaviour. Looks like an overkill having to emulate a whole cluster
047 * run for such a small optional property behaviour.
048 */
049@Category({ MediumTests.class })
050public class TestTakeSnapshotHandler {
051
052  private static HBaseTestingUtil UTIL;
053
054  @ClassRule
055  public static final HBaseClassTestRule CLASS_RULE =
056    HBaseClassTestRule.forClass(TestTakeSnapshotHandler.class);
057
058  @Rule
059  public TestName name = new TestName();
060
061  @Before
062  public void setup() {
063    UTIL = new HBaseTestingUtil();
064  }
065
066  public TableDescriptor createTableInsertDataAndTakeSnapshot(Map<String, Object> snapshotProps)
067    throws Exception {
068    TableDescriptor descriptor =
069      TableDescriptorBuilder.newBuilder(TableName.valueOf(name.getMethodName()))
070        .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("f")).build())
071        .build();
072    UTIL.getConnection().getAdmin().createTable(descriptor);
073    Table table = UTIL.getConnection().getTable(descriptor.getTableName());
074    Put put = new Put(Bytes.toBytes("1"));
075    put.addColumn(Bytes.toBytes("f"), Bytes.toBytes("1"), Bytes.toBytes("v1"));
076    table.put(put);
077    String snapName = "snap" + name.getMethodName();
078    UTIL.getAdmin().snapshot(snapName, descriptor.getTableName(), snapshotProps);
079    TableName cloned = TableName.valueOf(name.getMethodName() + "clone");
080    UTIL.getAdmin().cloneSnapshot(snapName, cloned);
081    return descriptor;
082  }
083
084  @Test
085  public void testPreparePreserveMaxFileSizeEnabled() throws Exception {
086    UTIL.startMiniCluster();
087    Map<String, Object> snapshotProps = new HashMap<>();
088    snapshotProps.put(TableDescriptorBuilder.MAX_FILESIZE, Long.parseLong("21474836480"));
089    TableDescriptor descriptor = createTableInsertDataAndTakeSnapshot(snapshotProps);
090    TableName cloned = TableName.valueOf(name.getMethodName() + "clone");
091    assertEquals(-1, UTIL.getAdmin().getDescriptor(descriptor.getTableName()).getMaxFileSize());
092    assertEquals(21474836480L, UTIL.getAdmin().getDescriptor(cloned).getMaxFileSize());
093  }
094
095  @Test
096  public void testPreparePreserveMaxFileSizeDisabled() throws Exception {
097    UTIL.startMiniCluster();
098    TableDescriptor descriptor = createTableInsertDataAndTakeSnapshot(null);
099    TableName cloned = TableName.valueOf(name.getMethodName() + "clone");
100    assertEquals(-1, UTIL.getAdmin().getDescriptor(descriptor.getTableName()).getMaxFileSize());
101    assertEquals(-1, UTIL.getAdmin().getDescriptor(cloned).getMaxFileSize());
102  }
103
104  @Test(expected = SnapshotTTLExpiredException.class)
105  public void testSnapshotEarlyExpiration() throws Exception {
106    UTIL.startMiniCluster();
107    Map<String, Object> snapshotProps = new HashMap<>();
108    snapshotProps.put("TTL", 1L);
109    createTableInsertDataAndTakeSnapshot(snapshotProps);
110  }
111
112  @After
113  public void shutdown() throws Exception {
114    UTIL.shutdownMiniCluster();
115  }
116}