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.HBaseTestingUtil; 032import org.apache.hadoop.hbase.HConstants; 033import org.apache.hadoop.hbase.TableName; 034import org.apache.hadoop.hbase.regionserver.DataTieringManager; 035import org.apache.hadoop.hbase.regionserver.DataTieringType; 036import org.apache.hadoop.hbase.regionserver.StoreEngine; 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 HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 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 TableDescriptorBuilder builder = 090 TableDescriptorBuilder.newBuilder(TableName.valueOf(name.getMethodName())); 091 ColumnFamilyDescriptorBuilder cfBuilder = ColumnFamilyDescriptorBuilder.newBuilder(FAMILY); 092 093 // create table with 0 families 094 checkTableIsIllegal(builder.build()); 095 checkTableIsLegal(builder.setColumnFamily(cfBuilder.build()).build()); 096 097 builder.setMaxFileSize(1024); // 1K 098 checkTableIsIllegal(builder.build()); 099 builder.setMaxFileSize(0); 100 checkTableIsIllegal(builder.build()); 101 builder.setMaxFileSize(1024 * 1024 * 1024); // 1G 102 checkTableIsLegal(builder.build()); 103 104 builder.setMemStoreFlushSize(1024); 105 checkTableIsIllegal(builder.build()); 106 builder.setMemStoreFlushSize(0); 107 checkTableIsIllegal(builder.build()); 108 builder.setMemStoreFlushSize(128 * 1024 * 1024); // 128M 109 checkTableIsLegal(builder.build()); 110 111 builder.setRegionSplitPolicyClassName("nonexisting.foo.class"); 112 checkTableIsIllegal(builder.build()); 113 builder.setRegionSplitPolicyClassName(null); 114 checkTableIsLegal(builder.build()); 115 116 builder.setValue(HConstants.HBASE_REGION_SPLIT_POLICY_KEY, "nonexisting.foo.class"); 117 checkTableIsIllegal(builder.build()); 118 builder.removeValue(Bytes.toBytes(HConstants.HBASE_REGION_SPLIT_POLICY_KEY)); 119 checkTableIsLegal(builder.build()); 120 121 cfBuilder.setBlocksize(0); 122 checkTableIsIllegal(builder.modifyColumnFamily(cfBuilder.build()).build()); 123 cfBuilder.setBlocksize(1024 * 1024 * 128); // 128M 124 checkTableIsIllegal(builder.modifyColumnFamily(cfBuilder.build()).build()); 125 cfBuilder.setBlocksize(1024); 126 checkTableIsLegal(builder.modifyColumnFamily(cfBuilder.build()).build()); 127 128 cfBuilder.setTimeToLive(0); 129 checkTableIsIllegal(builder.modifyColumnFamily(cfBuilder.build()).build()); 130 cfBuilder.setTimeToLive(-1); 131 checkTableIsIllegal(builder.modifyColumnFamily(cfBuilder.build()).build()); 132 cfBuilder.setTimeToLive(1); 133 checkTableIsLegal(builder.modifyColumnFamily(cfBuilder.build()).build()); 134 135 cfBuilder.setMinVersions(-1); 136 checkTableIsIllegal(builder.modifyColumnFamily(cfBuilder.build()).build()); 137 cfBuilder.setMinVersions(3); 138 try { 139 cfBuilder.setMaxVersions(2); 140 fail(); 141 } catch (IllegalArgumentException ex) { 142 // expected 143 cfBuilder.setMaxVersions(10); 144 } 145 checkTableIsLegal(builder.modifyColumnFamily(cfBuilder.build()).build()); 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 cfBuilder.setMaxVersions(4); 151 cfBuilder.setMinVersions(5); 152 checkTableIsIllegal(builder.modifyColumnFamily(cfBuilder.build()).build()); 153 cfBuilder.setMinVersions(3); 154 155 cfBuilder.setScope(-1); 156 checkTableIsIllegal(builder.modifyColumnFamily(cfBuilder.build()).build()); 157 cfBuilder.setScope(0); 158 checkTableIsLegal(builder.modifyColumnFamily(cfBuilder.build()).build()); 159 160 cfBuilder.setValue(ColumnFamilyDescriptorBuilder.IN_MEMORY_COMPACTION, "INVALID"); 161 checkTableIsIllegal(builder.modifyColumnFamily(cfBuilder.build()).build()); 162 cfBuilder.setValue(ColumnFamilyDescriptorBuilder.IN_MEMORY_COMPACTION, "NONE"); 163 checkTableIsLegal(builder.modifyColumnFamily(cfBuilder.build()).build()); 164 165 try { 166 cfBuilder.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 cfBuilder.setValue(ColumnFamilyDescriptorBuilder.DFS_REPLICATION, "-1"); 173 checkTableIsIllegal(builder.modifyColumnFamily(cfBuilder.build()).build()); 174 try { 175 cfBuilder.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 builder.setMemStoreFlushSize(0); 183 184 // Check that logs warn on invalid table but allow it. 185 builder.setValue(TableDescriptorChecker.TABLE_SANITY_CHECKS, Boolean.FALSE.toString()); 186 checkTableIsLegal(builder.build()); 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 @Test 194 public void testIllegalTableDescriptorWithDataTiering() throws IOException { 195 // table level configuration changes 196 TableDescriptorBuilder builder = 197 TableDescriptorBuilder.newBuilder(TableName.valueOf(name.getMethodName())); 198 ColumnFamilyDescriptorBuilder cfBuilder = ColumnFamilyDescriptorBuilder.newBuilder(FAMILY); 199 builder.setColumnFamily(cfBuilder.build()); 200 201 // First scenario: DataTieringType set to TIME_RANGE without DateTieredStoreEngine 202 builder.setValue(DataTieringManager.DATATIERING_KEY, DataTieringType.TIME_RANGE.name()); 203 checkTableIsIllegal(builder.build()); 204 205 // Second scenario: DataTieringType set to TIME_RANGE with DateTieredStoreEngine 206 builder.setValue(StoreEngine.STORE_ENGINE_CLASS_KEY, 207 "org.apache.hadoop.hbase.regionserver.DateTieredStoreEngine"); 208 checkTableIsLegal(builder.build()); 209 210 // Third scenario: Disabling DateTieredStoreEngine while Time Range DataTiering is active 211 builder.setValue(StoreEngine.STORE_ENGINE_CLASS_KEY, 212 "org.apache.hadoop.hbase.regionserver.DefaultStoreEngine"); 213 checkTableIsIllegal(builder.build()); 214 215 // column family level configuration changes 216 builder = TableDescriptorBuilder.newBuilder(TableName.valueOf(name.getMethodName())); 217 cfBuilder = ColumnFamilyDescriptorBuilder.newBuilder(FAMILY); 218 219 // First scenario: DataTieringType set to TIME_RANGE without DateTieredStoreEngine 220 cfBuilder.setConfiguration(DataTieringManager.DATATIERING_KEY, 221 DataTieringType.TIME_RANGE.name()); 222 checkTableIsIllegal(builder.setColumnFamily(cfBuilder.build()).build()); 223 224 // Second scenario: DataTieringType set to TIME_RANGE with DateTieredStoreEngine 225 cfBuilder.setConfiguration(StoreEngine.STORE_ENGINE_CLASS_KEY, 226 "org.apache.hadoop.hbase.regionserver.DateTieredStoreEngine"); 227 checkTableIsLegal(builder.modifyColumnFamily(cfBuilder.build()).build()); 228 229 // Third scenario: Disabling DateTieredStoreEngine while Time Range DataTiering is active 230 cfBuilder.setConfiguration(StoreEngine.STORE_ENGINE_CLASS_KEY, 231 "org.apache.hadoop.hbase.regionserver.DefaultStoreEngine"); 232 checkTableIsIllegal(builder.modifyColumnFamily(cfBuilder.build()).build()); 233 } 234 235 private void checkTableIsLegal(TableDescriptor tableDescriptor) throws IOException { 236 Admin admin = TEST_UTIL.getAdmin(); 237 admin.createTable(tableDescriptor); 238 assertTrue(admin.tableExists(tableDescriptor.getTableName())); 239 TEST_UTIL.deleteTable(tableDescriptor.getTableName()); 240 } 241 242 private void checkTableIsIllegal(TableDescriptor tableDescriptor) throws IOException { 243 Admin admin = TEST_UTIL.getAdmin(); 244 try { 245 admin.createTable(tableDescriptor); 246 fail(); 247 } catch (Exception ex) { 248 // should throw ex 249 } 250 assertFalse(admin.tableExists(tableDescriptor.getTableName())); 251 } 252}