HBASE-26921 Rewrite the counting cells part in TestMultiVersions (#4316)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / TestColumnFamilyDescriptorDefaultVersions.java
blob67bdcd10d4b877af6e4f7fbcc53c0cd02ac26699
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.CommonFSUtils;
34 import org.apache.hadoop.hbase.util.FSTableDescriptors;
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 ColumnFamilyDescriptor version is set correctly by default, hbase-site.xml, and
46 * user input
48 @Category({ MiscTests.class, MediumTests.class })
49 public class TestColumnFamilyDescriptorDefaultVersions {
51 @ClassRule
52 public static final HBaseClassTestRule CLASS_RULE =
53 HBaseClassTestRule.forClass(TestColumnFamilyDescriptorDefaultVersions.class);
55 @Rule
56 public TestName name = new TestName();
57 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
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.
64 @BeforeClass
65 public static void beforeAllTests() throws Exception {
66 TEST_UTIL.startMiniCluster(1);
69 @Before
70 public void setup() {
71 TABLE_NAME = TableName.valueOf(name.getMethodName());
75 @AfterClass
76 public static void afterAllTests() throws Exception {
77 TEST_UTIL.shutdownMiniCluster();
80 @Test
81 public void testCreateTableWithDefault() throws IOException {
82 Admin admin = TEST_UTIL.getAdmin();
83 // Create a table with one family
84 TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(TABLE_NAME)
85 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY)).build();
86 admin.createTable(tableDescriptor);
87 admin.disableTable(TABLE_NAME);
88 try {
89 // Verify the column descriptor
90 verifyHColumnDescriptor(1, TABLE_NAME, FAMILY);
91 } finally {
92 admin.deleteTable(TABLE_NAME);
96 @Test
97 public void testCreateTableWithDefaultFromConf() throws Exception {
98 TEST_UTIL.shutdownMiniCluster();
99 TEST_UTIL.getConfiguration().setInt("hbase.column.max.version", 3);
100 TEST_UTIL.startMiniCluster(1);
102 Admin admin = TEST_UTIL.getAdmin();
103 // Create a table with one family
104 TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(TABLE_NAME)
105 .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(FAMILY)
106 .setMaxVersions(TEST_UTIL.getConfiguration().getInt("hbase.column.max.version", 1)).build())
107 .build();
108 admin.createTable(tableDescriptor);
109 admin.disableTable(TABLE_NAME);
110 try {
111 // Verify the column descriptor
112 verifyHColumnDescriptor(3, TABLE_NAME, FAMILY);
113 } finally {
114 admin.deleteTable(TABLE_NAME);
118 @Test
119 public void testCreateTableWithSetVersion() throws Exception {
120 TEST_UTIL.shutdownMiniCluster();
121 TEST_UTIL.getConfiguration().setInt("hbase.column.max.version", 3);
122 TEST_UTIL.startMiniCluster(1);
124 Admin admin = TEST_UTIL.getAdmin();
125 // Create a table with one family
126 TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(TABLE_NAME)
127 .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(FAMILY).setMaxVersions(5).build())
128 .build();
129 admin.createTable(tableDescriptor);
130 admin.disableTable(TABLE_NAME);
131 try {
132 // Verify the column descriptor
133 verifyHColumnDescriptor(5, TABLE_NAME, FAMILY);
135 } finally {
136 admin.deleteTable(TABLE_NAME);
140 @Test
141 public void testHColumnDescriptorCachedMaxVersions() throws Exception {
142 ColumnFamilyDescriptor familyDescriptor =
143 ColumnFamilyDescriptorBuilder.newBuilder(FAMILY).setMaxVersions(5).build();
144 // Verify the max version
145 assertEquals(5, familyDescriptor.getMaxVersions());
147 // modify the max version
148 familyDescriptor = ColumnFamilyDescriptorBuilder.newBuilder(familyDescriptor)
149 .setValue(Bytes.toBytes(HConstants.VERSIONS), Bytes.toBytes("8")).build();
150 // Verify the max version
151 assertEquals(8, familyDescriptor.getMaxVersions());
154 private void verifyHColumnDescriptor(int expected, final TableName tableName,
155 final byte[]... families) throws IOException {
156 Admin admin = TEST_UTIL.getAdmin();
158 // Verify descriptor from master
159 TableDescriptor htd = admin.getDescriptor(tableName);
160 ColumnFamilyDescriptor[] hcds = htd.getColumnFamilies();
161 verifyColumnFamilyDescriptor(expected, hcds, tableName, families);
163 // Verify descriptor from HDFS
164 MasterFileSystem mfs = TEST_UTIL.getMiniHBaseCluster().getMaster().getMasterFileSystem();
165 Path tableDir = CommonFSUtils.getTableDir(mfs.getRootDir(), tableName);
166 TableDescriptor td = FSTableDescriptors.getTableDescriptorFromFs(mfs.getFileSystem(), tableDir);
167 hcds = td.getColumnFamilies();
168 verifyColumnFamilyDescriptor(expected, hcds, tableName, families);
171 private void verifyColumnFamilyDescriptor(int expected, final ColumnFamilyDescriptor[] hcds,
172 final TableName tableName, final byte[]... families) {
173 for (ColumnFamilyDescriptor hcd : hcds) {
174 assertEquals(expected, hcd.getMaxVersions());