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