View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase;
20  
21  import static org.junit.Assert.assertEquals;
22  
23  import java.io.IOException;
24  
25  import org.apache.hadoop.fs.Path;
26  import org.apache.hadoop.hbase.client.HBaseAdmin;
27  import org.apache.hadoop.hbase.master.MasterFileSystem;
28  import org.apache.hadoop.hbase.util.Bytes;
29  import org.apache.hadoop.hbase.util.FSTableDescriptors;
30  import org.apache.hadoop.hbase.util.FSUtils;
31  import org.junit.AfterClass;
32  import org.junit.Before;
33  import org.junit.BeforeClass;
34  import org.junit.Rule;
35  import org.junit.Test;
36  import org.junit.experimental.categories.Category;
37  import org.junit.rules.TestName;
38  
39  
40  /**
41   * Verify that the HColumnDescriptor version is set correctly by default, hbase-site.xml, and user
42   * input
43   */
44  @Category(MediumTests.class)
45  public class TestHColumnDescriptorDefaultVersions {
46  
47    @Rule
48    public TestName name = new TestName();
49    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
50    private static TableName TABLE_NAME = null;
51    private static final byte[] FAMILY = Bytes.toBytes("cf0");
52  
53    /**
54     * Start up a mini cluster and put a small table of empty regions into it.
55     * @throws Exception
56     */
57    @BeforeClass
58    public static void beforeAllTests() throws Exception {
59      TEST_UTIL.startMiniCluster(1);
60    }
61  
62    @Before
63    public void setup() {
64      TABLE_NAME = TableName.valueOf(name.getMethodName());
65  
66    }
67  
68    @AfterClass
69    public static void afterAllTests() throws Exception {
70      TEST_UTIL.shutdownMiniCluster();
71    }
72  
73    @Test
74    public void testCreateTableWithDefault() throws IOException {
75      HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
76      // Create a table with one family
77      HTableDescriptor baseHtd = new HTableDescriptor(TABLE_NAME);
78      HColumnDescriptor hcd = new HColumnDescriptor(FAMILY);
79      baseHtd.addFamily(hcd);
80      admin.createTable(baseHtd);
81      admin.disableTable(TABLE_NAME);
82      try {
83        // Verify the column descriptor
84        verifyHColumnDescriptor(1, TABLE_NAME, FAMILY);
85      } finally {
86        admin.deleteTable(TABLE_NAME);
87      }
88    }
89  
90    @Test
91    public void testCreateTableWithDefaultFromConf() throws Exception {
92      TEST_UTIL.shutdownMiniCluster();
93      TEST_UTIL.getConfiguration().setInt("hbase.column.max.version", 3);
94      TEST_UTIL.startMiniCluster(1);
95  
96      HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
97      // Create a table with one family
98      HTableDescriptor baseHtd = new HTableDescriptor(TABLE_NAME);
99      HColumnDescriptor hcd = new HColumnDescriptor(FAMILY);
100     hcd.setMaxVersions(TEST_UTIL.getConfiguration().getInt("hbase.column.max.version", 1));
101     baseHtd.addFamily(hcd);
102     admin.createTable(baseHtd);
103     admin.disableTable(TABLE_NAME);
104     try {
105       // Verify the column descriptor
106       verifyHColumnDescriptor(3, TABLE_NAME, FAMILY);
107     } finally {
108       admin.deleteTable(TABLE_NAME);
109     }
110   }
111 
112   @Test
113   public void testCreateTableWithSetVersion() throws Exception {
114     TEST_UTIL.shutdownMiniCluster();
115     TEST_UTIL.getConfiguration().setInt("hbase.column.max.version", 3);
116     TEST_UTIL.startMiniCluster(1);
117 
118     HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
119     // Create a table with one family
120     HTableDescriptor baseHtd = new HTableDescriptor(TABLE_NAME);
121     HColumnDescriptor hcd =
122         new HColumnDescriptor(FAMILY, 5, HColumnDescriptor.DEFAULT_COMPRESSION,
123             HColumnDescriptor.DEFAULT_IN_MEMORY, HColumnDescriptor.DEFAULT_BLOCKCACHE,
124             HColumnDescriptor.DEFAULT_TTL, HColumnDescriptor.DEFAULT_BLOOMFILTER);
125     baseHtd.addFamily(hcd);
126     admin.createTable(baseHtd);
127     admin.disableTable(TABLE_NAME);
128     try {
129       // Verify the column descriptor
130       verifyHColumnDescriptor(5, TABLE_NAME, FAMILY);
131 
132     } finally {
133       admin.deleteTable(TABLE_NAME);
134     }
135   }
136 
137   private void verifyHColumnDescriptor(int expected, final TableName tableName,
138       final byte[]... families) throws IOException {
139     HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
140 
141     // Verify descriptor from master
142     HTableDescriptor htd = admin.getTableDescriptor(tableName);
143     HColumnDescriptor[] hcds = htd.getColumnFamilies();
144     verifyHColumnDescriptor(expected, hcds, tableName, families);
145 
146     // Verify descriptor from HDFS
147     MasterFileSystem mfs = TEST_UTIL.getMiniHBaseCluster().getMaster().getMasterFileSystem();
148     Path tableDir = FSUtils.getTableDir(mfs.getRootDir(), tableName);
149     htd = FSTableDescriptors.getTableDescriptorFromFs(mfs.getFileSystem(), tableDir);
150     hcds = htd.getColumnFamilies();
151     verifyHColumnDescriptor(expected, hcds, tableName, families);
152   }
153 
154   private void verifyHColumnDescriptor(int expected, final HColumnDescriptor[] hcds,
155       final TableName tableName,
156       final byte[]... families) {
157     for (HColumnDescriptor hcd : hcds) {
158       assertEquals(expected, hcd.getMaxVersions());
159     }
160   }
161 
162 }