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