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.client;
019
020import static org.junit.Assert.assertFalse;
021import static org.junit.Assert.assertTrue;
022import static org.junit.Assert.fail;
023import static org.mockito.ArgumentMatchers.contains;
024import static org.mockito.Mockito.mock;
025import static org.mockito.Mockito.verify;
026
027import java.io.IOException;
028import java.lang.reflect.Field;
029import org.apache.hadoop.conf.Configuration;
030import org.apache.hadoop.hbase.HBaseClassTestRule;
031import org.apache.hadoop.hbase.HBaseTestingUtility;
032import org.apache.hadoop.hbase.HColumnDescriptor;
033import org.apache.hadoop.hbase.HConstants;
034import org.apache.hadoop.hbase.HTableDescriptor;
035import org.apache.hadoop.hbase.TableName;
036import org.apache.hadoop.hbase.testclassification.ClientTests;
037import org.apache.hadoop.hbase.testclassification.LargeTests;
038import org.apache.hadoop.hbase.util.Bytes;
039import org.apache.hadoop.hbase.util.TableDescriptorChecker;
040import org.junit.AfterClass;
041import org.junit.BeforeClass;
042import org.junit.ClassRule;
043import org.junit.Rule;
044import org.junit.Test;
045import org.junit.experimental.categories.Category;
046import org.junit.rules.TestName;
047import org.slf4j.Logger;
048
049@Category({ LargeTests.class, ClientTests.class })
050public class TestIllegalTableDescriptor {
051
052  @ClassRule
053  public static final HBaseClassTestRule CLASS_RULE =
054    HBaseClassTestRule.forClass(TestIllegalTableDescriptor.class);
055
056  // NOTE: Increment tests were moved to their own class, TestIncrementsFromClientSide.
057  private static final Logger LOGGER;
058
059  protected final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
060
061  private static byte[] FAMILY = Bytes.toBytes("testFamily");
062
063  @Rule
064  public TestName name = new TestName();
065
066  static {
067    LOGGER = mock(Logger.class);
068  }
069
070  @BeforeClass
071  public static void setUpBeforeClass() throws Exception {
072    // replacing HMaster.LOG with our mock logger for verifying logging
073    Field field = TableDescriptorChecker.class.getDeclaredField("LOG");
074    field.setAccessible(true);
075    field.set(null, LOGGER);
076    Configuration conf = TEST_UTIL.getConfiguration();
077    conf.setBoolean(TableDescriptorChecker.TABLE_SANITY_CHECKS, true); // enable for below tests
078    TEST_UTIL.startMiniCluster(1);
079  }
080
081  @AfterClass
082  public static void tearDownAfterClass() throws Exception {
083    TEST_UTIL.shutdownMiniCluster();
084  }
085
086  @Test
087  public void testIllegalTableDescriptor() throws Exception {
088    HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(name.getMethodName()));
089    HColumnDescriptor hcd = new HColumnDescriptor(FAMILY);
090
091    // create table with 0 families
092    checkTableIsIllegal(htd);
093    htd.addFamily(hcd);
094    checkTableIsLegal(htd);
095
096    htd.setMaxFileSize(1024); // 1K
097    checkTableIsIllegal(htd);
098    htd.setMaxFileSize(0);
099    checkTableIsIllegal(htd);
100    htd.setMaxFileSize(1024 * 1024 * 1024); // 1G
101    checkTableIsLegal(htd);
102
103    htd.setMemStoreFlushSize(1024);
104    checkTableIsIllegal(htd);
105    htd.setMemStoreFlushSize(0);
106    checkTableIsIllegal(htd);
107    htd.setMemStoreFlushSize(128 * 1024 * 1024); // 128M
108    checkTableIsLegal(htd);
109
110    htd.setRegionSplitPolicyClassName("nonexisting.foo.class");
111    checkTableIsIllegal(htd);
112    htd.setRegionSplitPolicyClassName(null);
113    checkTableIsLegal(htd);
114
115    htd.setValue(HConstants.HBASE_REGION_SPLIT_POLICY_KEY, "nonexisting.foo.class");
116    checkTableIsIllegal(htd);
117    htd.remove(HConstants.HBASE_REGION_SPLIT_POLICY_KEY);
118    checkTableIsLegal(htd);
119
120    hcd.setBlocksize(0);
121    checkTableIsIllegal(htd);
122    hcd.setBlocksize(1024 * 1024 * 128); // 128M
123    checkTableIsIllegal(htd);
124    hcd.setBlocksize(1024);
125    checkTableIsLegal(htd);
126
127    hcd.setTimeToLive(0);
128    checkTableIsIllegal(htd);
129    hcd.setTimeToLive(-1);
130    checkTableIsIllegal(htd);
131    hcd.setTimeToLive(1);
132    checkTableIsLegal(htd);
133
134    hcd.setMinVersions(-1);
135    checkTableIsIllegal(htd);
136    hcd.setMinVersions(3);
137    try {
138      hcd.setMaxVersions(2);
139      fail();
140    } catch (IllegalArgumentException ex) {
141      // expected
142      hcd.setMaxVersions(10);
143    }
144    checkTableIsLegal(htd);
145
146    // HBASE-13776 Setting illegal versions for HColumnDescriptor
147    // does not throw IllegalArgumentException
148    // finally, minVersions must be less than or equal to maxVersions
149    hcd.setMaxVersions(4);
150    hcd.setMinVersions(5);
151    checkTableIsIllegal(htd);
152    hcd.setMinVersions(3);
153
154    hcd.setScope(-1);
155    checkTableIsIllegal(htd);
156    hcd.setScope(0);
157    checkTableIsLegal(htd);
158
159    hcd.setValue(ColumnFamilyDescriptorBuilder.IN_MEMORY_COMPACTION, "INVALID");
160    checkTableIsIllegal(htd);
161    hcd.setValue(ColumnFamilyDescriptorBuilder.IN_MEMORY_COMPACTION, "NONE");
162    checkTableIsLegal(htd);
163
164    try {
165      hcd.setDFSReplication((short) -1);
166      fail("Illegal value for setDFSReplication did not throw");
167    } catch (IllegalArgumentException e) {
168      // pass
169    }
170    // set an illegal DFS replication value by hand
171    hcd.setValue(HColumnDescriptor.DFS_REPLICATION, "-1");
172    checkTableIsIllegal(htd);
173    try {
174      hcd.setDFSReplication((short) -1);
175      fail("Should throw exception if an illegal value is explicitly being set");
176    } catch (IllegalArgumentException e) {
177      // pass
178    }
179
180    // check the conf settings to disable sanity checks
181    htd.setMemStoreFlushSize(0);
182
183    // Check that logs warn on invalid table but allow it.
184    htd.setConfiguration(TableDescriptorChecker.TABLE_SANITY_CHECKS, Boolean.FALSE.toString());
185    checkTableIsLegal(htd);
186
187    verify(LOGGER).warn(contains("MEMSTORE_FLUSHSIZE for table "
188      + "descriptor or \"hbase.hregion.memstore.flush.size\" (0) is too small, which might "
189      + "cause very frequent flushing."));
190  }
191
192  private void checkTableIsLegal(HTableDescriptor htd) throws IOException {
193    Admin admin = TEST_UTIL.getAdmin();
194    admin.createTable(htd);
195    assertTrue(admin.tableExists(htd.getTableName()));
196    TEST_UTIL.deleteTable(htd.getTableName());
197  }
198
199  private void checkTableIsIllegal(HTableDescriptor htd) throws IOException {
200    Admin admin = TEST_UTIL.getAdmin();
201    try {
202      admin.createTable(htd);
203      fail();
204    } catch (Exception ex) {
205      // should throw ex
206    }
207    assertFalse(admin.tableExists(htd.getTableName()));
208  }
209}