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;
019
020import static org.junit.Assert.assertEquals;
021
022import java.io.IOException;
023import org.apache.hadoop.fs.Path;
024import org.apache.hadoop.hbase.client.Admin;
025import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
026import org.apache.hadoop.hbase.client.TableDescriptor;
027import org.apache.hadoop.hbase.master.MasterFileSystem;
028import org.apache.hadoop.hbase.testclassification.MediumTests;
029import org.apache.hadoop.hbase.testclassification.MiscTests;
030import org.apache.hadoop.hbase.util.Bytes;
031import org.apache.hadoop.hbase.util.CommonFSUtils;
032import org.apache.hadoop.hbase.util.FSTableDescriptors;
033import org.junit.AfterClass;
034import org.junit.Before;
035import org.junit.BeforeClass;
036import org.junit.ClassRule;
037import org.junit.Rule;
038import org.junit.Test;
039import org.junit.experimental.categories.Category;
040import org.junit.rules.TestName;
041
042/**
043 * Verify that the HColumnDescriptor version is set correctly by default, hbase-site.xml, and user
044 * input
045 */
046@Category({MiscTests.class, MediumTests.class})
047public class TestHColumnDescriptorDefaultVersions {
048
049  @ClassRule
050  public static final HBaseClassTestRule CLASS_RULE =
051      HBaseClassTestRule.forClass(TestHColumnDescriptorDefaultVersions.class);
052
053  @Rule
054  public TestName name = new TestName();
055  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
056  private static TableName TABLE_NAME = null;
057  private static final byte[] FAMILY = Bytes.toBytes("cf0");
058
059  /**
060   * Start up a mini cluster and put a small table of empty regions into it.
061   * @throws Exception
062   */
063  @BeforeClass
064  public static void beforeAllTests() throws Exception {
065    TEST_UTIL.startMiniCluster(1);
066  }
067
068  @Before
069  public void setup() {
070    TABLE_NAME = TableName.valueOf(name.getMethodName());
071
072  }
073
074  @AfterClass
075  public static void afterAllTests() throws Exception {
076    TEST_UTIL.shutdownMiniCluster();
077  }
078
079  @Test
080  public void testCreateTableWithDefault() throws IOException {
081    Admin admin = TEST_UTIL.getAdmin();
082    // Create a table with one family
083    HTableDescriptor baseHtd = new HTableDescriptor(TABLE_NAME);
084    HColumnDescriptor hcd = new HColumnDescriptor(FAMILY);
085    baseHtd.addFamily(hcd);
086    admin.createTable(baseHtd);
087    admin.disableTable(TABLE_NAME);
088    try {
089      // Verify the column descriptor
090      verifyHColumnDescriptor(1, TABLE_NAME, FAMILY);
091    } finally {
092      admin.deleteTable(TABLE_NAME);
093    }
094  }
095
096  @Test
097  public void testCreateTableWithDefaultFromConf() throws Exception {
098    TEST_UTIL.shutdownMiniCluster();
099    TEST_UTIL.getConfiguration().setInt("hbase.column.max.version", 3);
100    TEST_UTIL.startMiniCluster(1);
101
102    Admin admin = TEST_UTIL.getAdmin();
103    // Create a table with one family
104    HTableDescriptor baseHtd = new HTableDescriptor(TABLE_NAME);
105    HColumnDescriptor hcd = new HColumnDescriptor(FAMILY);
106    hcd.setMaxVersions(TEST_UTIL.getConfiguration().getInt("hbase.column.max.version", 1));
107    baseHtd.addFamily(hcd);
108    admin.createTable(baseHtd);
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);
115    }
116  }
117
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);
123
124    Admin admin = TEST_UTIL.getAdmin();
125    // Create a table with one family
126    HTableDescriptor baseHtd = new HTableDescriptor(TABLE_NAME);
127    HColumnDescriptor hcd = new HColumnDescriptor(FAMILY);
128    hcd.setMaxVersions(5);
129    baseHtd.addFamily(hcd);
130    admin.createTable(baseHtd);
131    admin.disableTable(TABLE_NAME);
132    try {
133      // Verify the column descriptor
134      verifyHColumnDescriptor(5, TABLE_NAME, FAMILY);
135
136    } finally {
137      admin.deleteTable(TABLE_NAME);
138    }
139  }
140
141  @Test
142  public void testHColumnDescriptorCachedMaxVersions() throws Exception {
143    HColumnDescriptor hcd = new HColumnDescriptor(FAMILY);
144    hcd.setMaxVersions(5);
145    // Verify the max version
146    assertEquals(5, hcd.getMaxVersions());
147
148    // modify the max version
149    hcd.setValue(Bytes.toBytes(HConstants.VERSIONS), Bytes.toBytes("8"));
150    // Verify the max version
151    assertEquals(8, hcd.getMaxVersions());
152  }
153
154  private void verifyHColumnDescriptor(int expected, final TableName tableName,
155      final byte[]... families) throws IOException {
156    Admin admin = TEST_UTIL.getAdmin();
157
158    // Verify descriptor from master
159    TableDescriptor htd = admin.getDescriptor(tableName);
160    ColumnFamilyDescriptor[] hcds = htd.getColumnFamilies();
161    verifyHColumnDescriptor(expected, hcds, tableName, families);
162
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    verifyHColumnDescriptor(expected, hcds, tableName, families);
169  }
170
171  private void verifyHColumnDescriptor(int expected, final ColumnFamilyDescriptor[] hcds,
172      final TableName tableName,
173      final byte[]... families) {
174    for (ColumnFamilyDescriptor hcd : hcds) {
175      assertEquals(expected, hcd.getMaxVersions());
176    }
177  }
178
179}