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   */
062  @BeforeClass
063  public static void beforeAllTests() throws Exception {
064    TEST_UTIL.startMiniCluster(1);
065  }
066
067  @Before
068  public void setup() {
069    TABLE_NAME = TableName.valueOf(name.getMethodName());
070
071  }
072
073  @AfterClass
074  public static void afterAllTests() throws Exception {
075    TEST_UTIL.shutdownMiniCluster();
076  }
077
078  @Test
079  public void testCreateTableWithDefault() throws IOException {
080    Admin admin = TEST_UTIL.getAdmin();
081    // Create a table with one family
082    HTableDescriptor baseHtd = new HTableDescriptor(TABLE_NAME);
083    HColumnDescriptor hcd = new HColumnDescriptor(FAMILY);
084    baseHtd.addFamily(hcd);
085    admin.createTable(baseHtd);
086    admin.disableTable(TABLE_NAME);
087    try {
088      // Verify the column descriptor
089      verifyHColumnDescriptor(1, TABLE_NAME, FAMILY);
090    } finally {
091      admin.deleteTable(TABLE_NAME);
092    }
093  }
094
095  @Test
096  public void testCreateTableWithDefaultFromConf() throws Exception {
097    TEST_UTIL.shutdownMiniCluster();
098    TEST_UTIL.getConfiguration().setInt("hbase.column.max.version", 3);
099    TEST_UTIL.startMiniCluster(1);
100
101    Admin admin = TEST_UTIL.getAdmin();
102    // Create a table with one family
103    HTableDescriptor baseHtd = new HTableDescriptor(TABLE_NAME);
104    HColumnDescriptor hcd = new HColumnDescriptor(FAMILY);
105    hcd.setMaxVersions(TEST_UTIL.getConfiguration().getInt("hbase.column.max.version", 1));
106    baseHtd.addFamily(hcd);
107    admin.createTable(baseHtd);
108    admin.disableTable(TABLE_NAME);
109    try {
110      // Verify the column descriptor
111      verifyHColumnDescriptor(3, TABLE_NAME, FAMILY);
112    } finally {
113      admin.deleteTable(TABLE_NAME);
114    }
115  }
116
117  @Test
118  public void testCreateTableWithSetVersion() throws Exception {
119    TEST_UTIL.shutdownMiniCluster();
120    TEST_UTIL.getConfiguration().setInt("hbase.column.max.version", 3);
121    TEST_UTIL.startMiniCluster(1);
122
123    Admin admin = TEST_UTIL.getAdmin();
124    // Create a table with one family
125    HTableDescriptor baseHtd = new HTableDescriptor(TABLE_NAME);
126    HColumnDescriptor hcd = new HColumnDescriptor(FAMILY);
127    hcd.setMaxVersions(5);
128    baseHtd.addFamily(hcd);
129    admin.createTable(baseHtd);
130    admin.disableTable(TABLE_NAME);
131    try {
132      // Verify the column descriptor
133      verifyHColumnDescriptor(5, TABLE_NAME, FAMILY);
134
135    } finally {
136      admin.deleteTable(TABLE_NAME);
137    }
138  }
139
140  @Test
141  public void testHColumnDescriptorCachedMaxVersions() throws Exception {
142    HColumnDescriptor hcd = new HColumnDescriptor(FAMILY);
143    hcd.setMaxVersions(5);
144    // Verify the max version
145    assertEquals(5, hcd.getMaxVersions());
146
147    // modify the max version
148    hcd.setValue(Bytes.toBytes(HConstants.VERSIONS), Bytes.toBytes("8"));
149    // Verify the max version
150    assertEquals(8, hcd.getMaxVersions());
151  }
152
153  private void verifyHColumnDescriptor(int expected, final TableName tableName,
154    final byte[]... families) throws IOException {
155    Admin admin = TEST_UTIL.getAdmin();
156
157    // Verify descriptor from master
158    TableDescriptor htd = admin.getDescriptor(tableName);
159    ColumnFamilyDescriptor[] hcds = htd.getColumnFamilies();
160    verifyHColumnDescriptor(expected, hcds, tableName, families);
161
162    // Verify descriptor from HDFS
163    MasterFileSystem mfs = TEST_UTIL.getMiniHBaseCluster().getMaster().getMasterFileSystem();
164    Path tableDir = CommonFSUtils.getTableDir(mfs.getRootDir(), tableName);
165    TableDescriptor td = FSTableDescriptors.getTableDescriptorFromFs(mfs.getFileSystem(), tableDir);
166    hcds = td.getColumnFamilies();
167    verifyHColumnDescriptor(expected, hcds, tableName, families);
168  }
169
170  private void verifyHColumnDescriptor(int expected, final ColumnFamilyDescriptor[] hcds,
171    final TableName tableName, final byte[]... families) {
172    for (ColumnFamilyDescriptor hcd : hcds) {
173      assertEquals(expected, hcd.getMaxVersions());
174    }
175  }
176
177}