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.testclassification.MediumTests;
29  import org.apache.hadoop.hbase.util.Bytes;
30  import org.apache.hadoop.hbase.util.FSTableDescriptors;
31  import org.apache.hadoop.hbase.util.FSUtils;
32  import org.junit.AfterClass;
33  import org.junit.Before;
34  import org.junit.BeforeClass;
35  import org.junit.Rule;
36  import org.junit.Test;
37  import org.junit.experimental.categories.Category;
38  import org.junit.rules.TestName;
39  
40  
41  /**
42   * Verify that the HColumnDescriptor version is set correctly by default, hbase-site.xml, and user
43   * input
44   */
45  @Category(MediumTests.class)
46  public class TestHColumnDescriptorDefaultVersions {
47  
48    @Rule
49    public TestName name = new TestName();
50    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
51    private static TableName TABLE_NAME = null;
52    private static final byte[] FAMILY = Bytes.toBytes("cf0");
53  
54    /**
55     * Start up a mini cluster and put a small table of empty regions into it.
56     * @throws Exception
57     */
58    @BeforeClass
59    public static void beforeAllTests() throws Exception {
60      TEST_UTIL.startMiniCluster(1);
61    }
62  
63    @Before
64    public void setup() {
65      TABLE_NAME = TableName.valueOf(name.getMethodName());
66  
67    }
68  
69    @AfterClass
70    public static void afterAllTests() throws Exception {
71      TEST_UTIL.shutdownMiniCluster();
72    }
73  
74    @Test
75    public void testCreateTableWithDefault() throws IOException {
76      HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
77      // Create a table with one family
78      HTableDescriptor baseHtd = new HTableDescriptor(TABLE_NAME);
79      HColumnDescriptor hcd = new HColumnDescriptor(FAMILY);
80      baseHtd.addFamily(hcd);
81      admin.createTable(baseHtd);
82      admin.disableTable(TABLE_NAME);
83      try {
84        // Verify the column descriptor
85        verifyHColumnDescriptor(1, TABLE_NAME, FAMILY);
86      } finally {
87        admin.deleteTable(TABLE_NAME);
88      }
89    }
90  
91    @Test
92    public void testCreateTableWithDefaultFromConf() throws Exception {
93      TEST_UTIL.shutdownMiniCluster();
94      TEST_UTIL.getConfiguration().setInt("hbase.column.max.version", 3);
95      TEST_UTIL.startMiniCluster(1);
96  
97      HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
98      // Create a table with one family
99      HTableDescriptor baseHtd = new HTableDescriptor(TABLE_NAME);
100     HColumnDescriptor hcd = new HColumnDescriptor(FAMILY);
101     hcd.setMaxVersions(TEST_UTIL.getConfiguration().getInt("hbase.column.max.version", 1));
102     baseHtd.addFamily(hcd);
103     admin.createTable(baseHtd);
104     admin.disableTable(TABLE_NAME);
105     try {
106       // Verify the column descriptor
107       verifyHColumnDescriptor(3, TABLE_NAME, FAMILY);
108     } finally {
109       admin.deleteTable(TABLE_NAME);
110     }
111   }
112 
113   @Test
114   public void testCreateTableWithSetVersion() throws Exception {
115     TEST_UTIL.shutdownMiniCluster();
116     TEST_UTIL.getConfiguration().setInt("hbase.column.max.version", 3);
117     TEST_UTIL.startMiniCluster(1);
118 
119     HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
120     // Create a table with one family
121     HTableDescriptor baseHtd = new HTableDescriptor(TABLE_NAME);
122     HColumnDescriptor hcd =
123         new HColumnDescriptor(FAMILY, 5, HColumnDescriptor.DEFAULT_COMPRESSION,
124             HColumnDescriptor.DEFAULT_IN_MEMORY, HColumnDescriptor.DEFAULT_BLOCKCACHE,
125             HColumnDescriptor.DEFAULT_TTL, HColumnDescriptor.DEFAULT_BLOOMFILTER);
126     baseHtd.addFamily(hcd);
127     admin.createTable(baseHtd);
128     admin.disableTable(TABLE_NAME);
129     try {
130       // Verify the column descriptor
131       verifyHColumnDescriptor(5, TABLE_NAME, FAMILY);
132 
133     } finally {
134       admin.deleteTable(TABLE_NAME);
135     }
136   }
137 
138   private void verifyHColumnDescriptor(int expected, final TableName tableName,
139       final byte[]... families) throws IOException {
140     HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
141 
142     // Verify descriptor from master
143     HTableDescriptor htd = admin.getTableDescriptor(tableName);
144     HColumnDescriptor[] hcds = htd.getColumnFamilies();
145     verifyHColumnDescriptor(expected, hcds, tableName, families);
146 
147     // Verify descriptor from HDFS
148     MasterFileSystem mfs = TEST_UTIL.getMiniHBaseCluster().getMaster().getMasterFileSystem();
149     Path tableDir = FSUtils.getTableDir(mfs.getRootDir(), tableName);
150     htd = FSTableDescriptors.getTableDescriptorFromFs(mfs.getFileSystem(), tableDir);
151     hcds = htd.getColumnFamilies();
152     verifyHColumnDescriptor(expected, hcds, tableName, families);
153   }
154 
155   private void verifyHColumnDescriptor(int expected, final HColumnDescriptor[] hcds,
156       final TableName tableName,
157       final byte[]... families) {
158     for (HColumnDescriptor hcd : hcds) {
159       assertEquals(expected, hcd.getMaxVersions());
160     }
161   }
162 
163 }