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 ColumnFamilyDescriptor version is set correctly by default, hbase-site.xml, and
046 * user input
047 */
048@Category({ MiscTests.class, MediumTests.class })
049public class TestColumnFamilyDescriptorDefaultVersions {
050
051  @ClassRule
052  public static final HBaseClassTestRule CLASS_RULE =
053    HBaseClassTestRule.forClass(TestColumnFamilyDescriptorDefaultVersions.class);
054
055  @Rule
056  public TestName name = new TestName();
057  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
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   */
064  @BeforeClass
065  public static void beforeAllTests() throws Exception {
066    TEST_UTIL.startMiniCluster(1);
067  }
068
069  @Before
070  public void setup() {
071    TABLE_NAME = TableName.valueOf(name.getMethodName());
072
073  }
074
075  @AfterClass
076  public static void afterAllTests() throws Exception {
077    TEST_UTIL.shutdownMiniCluster();
078  }
079
080  @Test
081  public void testCreateTableWithDefault() throws IOException {
082    Admin admin = TEST_UTIL.getAdmin();
083    // Create a table with one family
084    TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(TABLE_NAME)
085      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY)).build();
086    admin.createTable(tableDescriptor);
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    TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(TABLE_NAME)
105      .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(FAMILY)
106        .setMaxVersions(TEST_UTIL.getConfiguration().getInt("hbase.column.max.version", 1)).build())
107      .build();
108    admin.createTable(tableDescriptor);
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    TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(TABLE_NAME)
127      .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(FAMILY).setMaxVersions(5).build())
128      .build();
129    admin.createTable(tableDescriptor);
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    ColumnFamilyDescriptor familyDescriptor =
143      ColumnFamilyDescriptorBuilder.newBuilder(FAMILY).setMaxVersions(5).build();
144    // Verify the max version
145    assertEquals(5, familyDescriptor.getMaxVersions());
146
147    // modify the max version
148    familyDescriptor = ColumnFamilyDescriptorBuilder.newBuilder(familyDescriptor)
149      .setValue(Bytes.toBytes(HConstants.VERSIONS), Bytes.toBytes("8")).build();
150    // Verify the max version
151    assertEquals(8, familyDescriptor.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    verifyColumnFamilyDescriptor(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    verifyColumnFamilyDescriptor(expected, hcds, tableName, families);
169  }
170
171  private void verifyColumnFamilyDescriptor(int expected, final ColumnFamilyDescriptor[] hcds,
172    final TableName tableName, final byte[]... families) {
173    for (ColumnFamilyDescriptor hcd : hcds) {
174      assertEquals(expected, hcd.getMaxVersions());
175    }
176  }
177}