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.master.procedure; 019 020import java.io.IOException; 021import java.io.UncheckedIOException; 022import java.util.concurrent.CountDownLatch; 023import java.util.concurrent.Future; 024import java.util.concurrent.TimeUnit; 025import org.apache.hadoop.conf.Configuration; 026import org.apache.hadoop.hbase.HBaseTestingUtil; 027import org.apache.hadoop.hbase.StartTestingClusterOption; 028import org.apache.hadoop.hbase.TableName; 029import org.apache.hadoop.hbase.client.Admin; 030import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 031import org.apache.hadoop.hbase.client.Put; 032import org.apache.hadoop.hbase.client.Table; 033import org.apache.hadoop.hbase.client.TableDescriptor; 034import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 035import org.apache.hadoop.hbase.master.HMaster; 036import org.apache.hadoop.hbase.master.assignment.AssignmentManager; 037import org.apache.hadoop.hbase.procedure2.Procedure; 038import org.apache.hadoop.hbase.testclassification.MasterTests; 039import org.apache.hadoop.hbase.testclassification.MediumTests; 040import org.apache.hadoop.hbase.util.Bytes; 041import org.junit.jupiter.api.AfterAll; 042import org.junit.jupiter.api.BeforeAll; 043import org.junit.jupiter.api.Tag; 044import org.junit.jupiter.api.Test; 045import org.slf4j.Logger; 046import org.slf4j.LoggerFactory; 047 048import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.CreateTableState; 049 050@Tag(MasterTests.TAG) 051@Tag(MediumTests.TAG) 052public class TestCreateTableNoRegionServer { 053 054 private static final Logger LOG = LoggerFactory.getLogger(TestCreateTableNoRegionServer.class); 055 056 private static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); 057 058 private static TableName TABLE_NAME = TableName.valueOf("test"); 059 060 private static byte[] FAMILY = Bytes.toBytes("f1"); 061 062 private static CountDownLatch ARRIVE; 063 064 private static CountDownLatch RESUME; 065 066 public static final class HMasterForTest extends HMaster { 067 068 public HMasterForTest(Configuration conf) throws IOException { 069 super(conf); 070 } 071 072 private boolean isInAssignRegionsState() { 073 try { 074 for (StackTraceElement e : Thread.currentThread().getStackTrace()) { 075 if ( 076 e.getClassName().equals(CreateTableProcedure.class.getName()) 077 && e.getMethodName().equals("executeFromState") 078 ) { 079 for (Procedure<?> proc : getProcedures()) { 080 if ( 081 proc instanceof CreateTableProcedure && !proc.isFinished() 082 && ((CreateTableProcedure) proc).getCurrentStateId() 083 == CreateTableState.CREATE_TABLE_ASSIGN_REGIONS_VALUE 084 ) { 085 return true; 086 } 087 } 088 } 089 } 090 } catch (IOException e) { 091 throw new UncheckedIOException(e); 092 } 093 return false; 094 } 095 096 @Override 097 public AssignmentManager getAssignmentManager() { 098 if (ARRIVE != null && isInAssignRegionsState()) { 099 ARRIVE.countDown(); 100 ARRIVE = null; 101 try { 102 RESUME.await(); 103 } catch (InterruptedException e) { 104 } 105 } 106 return super.getAssignmentManager(); 107 } 108 } 109 110 @BeforeAll 111 public static void setUp() throws Exception { 112 UTIL.startMiniCluster( 113 StartTestingClusterOption.builder().masterClass(HMasterForTest.class).build()); 114 } 115 116 @AfterAll 117 public static void tearDown() throws Exception { 118 UTIL.shutdownMiniCluster(); 119 } 120 121 @Test 122 public void testCreate() throws Exception { 123 TableDescriptor td = TableDescriptorBuilder.newBuilder(TABLE_NAME) 124 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY)).build(); 125 Admin admin = UTIL.getAdmin(); 126 ARRIVE = new CountDownLatch(1); 127 RESUME = new CountDownLatch(1); 128 Future<Void> future = admin.createTableAsync(td); 129 ARRIVE.await(); 130 131 UTIL.getMiniHBaseCluster().stopRegionServer(0).join(); 132 133 // make sure we the region server is done. 134 UTIL.waitFor(30000, 135 () -> UTIL.getMiniHBaseCluster().getMaster().getServerManager().getOnlineServers().isEmpty()); 136 RESUME.countDown(); 137 138 Thread.sleep(10000); 139 // the procedure should still be in the CREATE_TABLE_ASSIGN_REGIONS state, but here, we just 140 // warn it as it may cause more serious problem later. 141 for (Procedure<?> proc : UTIL.getMiniHBaseCluster().getMaster().getProcedures()) { 142 if ( 143 proc instanceof CreateTableProcedure && !proc.isFinished() 144 && ((CreateTableProcedure) proc).getCurrentStateId() 145 != CreateTableState.CREATE_TABLE_ASSIGN_REGIONS_VALUE 146 ) { 147 LOG.warn("Create table procedure {} assigned regions without a region server!", proc); 148 } 149 } 150 UTIL.getMiniHBaseCluster().startRegionServer(); 151 // the creation should finally be done 152 future.get(30, TimeUnit.SECONDS); 153 // make sure we could put to the table 154 try (Table table = 155 UTIL.getConnection().getTableBuilder(TABLE_NAME, null).setOperationTimeout(5000).build()) { 156 table.put(new Put(Bytes.toBytes(0)).addColumn(FAMILY, Bytes.toBytes("q"), Bytes.toBytes(0))); 157 } 158 } 159}