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