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