HBASE-24033 Add ut for loading the corrupt recovered hfiles (#1322)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / TestHColumnDescriptorDefaultVersions.java
blob76cd6e4178bb26bc25664590f1268fb6093c89a5
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase;
20 import static org.junit.Assert.assertEquals;
22 import java.io.IOException;
23 import org.apache.hadoop.fs.Path;
24 import org.apache.hadoop.hbase.client.Admin;
25 import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
26 import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
27 import org.apache.hadoop.hbase.client.TableDescriptor;
28 import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
29 import org.apache.hadoop.hbase.master.MasterFileSystem;
30 import org.apache.hadoop.hbase.testclassification.MediumTests;
31 import org.apache.hadoop.hbase.testclassification.MiscTests;
32 import org.apache.hadoop.hbase.util.Bytes;
33 import org.apache.hadoop.hbase.util.FSTableDescriptors;
34 import org.apache.hadoop.hbase.util.FSUtils;
35 import org.junit.AfterClass;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.ClassRule;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.experimental.categories.Category;
42 import org.junit.rules.TestName;
44 /**
45 * Verify that the HColumnDescriptor version is set correctly by default, hbase-site.xml, and user
46 * input
48 @Category({MiscTests.class, MediumTests.class})
49 public class TestHColumnDescriptorDefaultVersions {
51 @ClassRule
52 public static final HBaseClassTestRule CLASS_RULE =
53 HBaseClassTestRule.forClass(TestHColumnDescriptorDefaultVersions.class);
55 @Rule
56 public TestName name = new TestName();
57 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
58 private static TableName TABLE_NAME = null;
59 private static final byte[] FAMILY = Bytes.toBytes("cf0");
61 /**
62 * Start up a mini cluster and put a small table of empty regions into it.
63 * @throws Exception
65 @BeforeClass
66 public static void beforeAllTests() throws Exception {
67 TEST_UTIL.startMiniCluster(1);
70 @Before
71 public void setup() {
72 TABLE_NAME = TableName.valueOf(name.getMethodName());
76 @AfterClass
77 public static void afterAllTests() throws Exception {
78 TEST_UTIL.shutdownMiniCluster();
81 @Test
82 public void testCreateTableWithDefault() throws IOException {
83 Admin admin = TEST_UTIL.getAdmin();
84 // Create a table with one family
85 TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor =
86 new TableDescriptorBuilder.ModifyableTableDescriptor(TABLE_NAME);
87 ColumnFamilyDescriptor familyDescriptor =
88 new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(FAMILY);
89 tableDescriptor.setColumnFamily(familyDescriptor);
90 admin.createTable(tableDescriptor);
91 admin.disableTable(TABLE_NAME);
92 try {
93 // Verify the column descriptor
94 verifyHColumnDescriptor(1, TABLE_NAME, FAMILY);
95 } finally {
96 admin.deleteTable(TABLE_NAME);
100 @Test
101 public void testCreateTableWithDefaultFromConf() throws Exception {
102 TEST_UTIL.shutdownMiniCluster();
103 TEST_UTIL.getConfiguration().setInt("hbase.column.max.version", 3);
104 TEST_UTIL.startMiniCluster(1);
106 Admin admin = TEST_UTIL.getAdmin();
107 // Create a table with one family
108 TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor =
109 new TableDescriptorBuilder.ModifyableTableDescriptor(TABLE_NAME);
110 ColumnFamilyDescriptor familyDescriptor =
111 new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(FAMILY)
112 .setMaxVersions(TEST_UTIL.getConfiguration().getInt("hbase.column.max.version", 1));
113 tableDescriptor.setColumnFamily(familyDescriptor);
114 admin.createTable(tableDescriptor);
115 admin.disableTable(TABLE_NAME);
116 try {
117 // Verify the column descriptor
118 verifyHColumnDescriptor(3, TABLE_NAME, FAMILY);
119 } finally {
120 admin.deleteTable(TABLE_NAME);
124 @Test
125 public void testCreateTableWithSetVersion() throws Exception {
126 TEST_UTIL.shutdownMiniCluster();
127 TEST_UTIL.getConfiguration().setInt("hbase.column.max.version", 3);
128 TEST_UTIL.startMiniCluster(1);
130 Admin admin = TEST_UTIL.getAdmin();
131 // Create a table with one family
132 TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor =
133 new TableDescriptorBuilder.ModifyableTableDescriptor(TABLE_NAME);
134 ColumnFamilyDescriptor familyDescriptor =
135 new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(FAMILY)
136 .setMaxVersions(5);
137 tableDescriptor.setColumnFamily(familyDescriptor);
138 admin.createTable(tableDescriptor);
139 admin.disableTable(TABLE_NAME);
140 try {
141 // Verify the column descriptor
142 verifyHColumnDescriptor(5, TABLE_NAME, FAMILY);
144 } finally {
145 admin.deleteTable(TABLE_NAME);
149 @Test
150 public void testHColumnDescriptorCachedMaxVersions() throws Exception {
151 ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor familyDescriptor =
152 new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(FAMILY);
153 familyDescriptor.setMaxVersions(5);
154 // Verify the max version
155 assertEquals(5, familyDescriptor.getMaxVersions());
157 // modify the max version
158 familyDescriptor.setValue(Bytes.toBytes(HConstants.VERSIONS), Bytes.toBytes("8"));
159 // Verify the max version
160 assertEquals(8, familyDescriptor.getMaxVersions());
163 private void verifyHColumnDescriptor(int expected, final TableName tableName,
164 final byte[]... families) throws IOException {
165 Admin admin = TEST_UTIL.getAdmin();
167 // Verify descriptor from master
168 TableDescriptor htd = admin.getDescriptor(tableName);
169 ColumnFamilyDescriptor[] hcds = htd.getColumnFamilies();
170 verifyHColumnDescriptor(expected, hcds, tableName, families);
172 // Verify descriptor from HDFS
173 MasterFileSystem mfs = TEST_UTIL.getMiniHBaseCluster().getMaster().getMasterFileSystem();
174 Path tableDir = FSUtils.getTableDir(mfs.getRootDir(), tableName);
175 TableDescriptor td = FSTableDescriptors.getTableDescriptorFromFs(mfs.getFileSystem(), tableDir);
176 hcds = td.getColumnFamilies();
177 verifyHColumnDescriptor(expected, hcds, tableName, families);
180 private void verifyHColumnDescriptor(int expected, final ColumnFamilyDescriptor[] hcds,
181 final TableName tableName,
182 final byte[]... families) {
183 for (ColumnFamilyDescriptor hcd : hcds) {
184 assertEquals(expected, hcd.getMaxVersions());