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.apache.hadoop.metrics2.impl.JmxCacheBuster;
042import org.junit.jupiter.api.AfterAll;
043import org.junit.jupiter.api.BeforeAll;
044import org.junit.jupiter.api.Tag;
045import org.junit.jupiter.api.Test;
046import org.slf4j.Logger;
047import org.slf4j.LoggerFactory;
048
049import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.CreateTableState;
050
051@Tag(MasterTests.TAG)
052@Tag(MediumTests.TAG)
053public class TestCreateTableNoRegionServer {
054
055  private static final Logger LOG = LoggerFactory.getLogger(TestCreateTableNoRegionServer.class);
056
057  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
058
059  private static TableName TABLE_NAME = TableName.valueOf("test");
060
061  private static byte[] FAMILY = Bytes.toBytes("f1");
062
063  private static CountDownLatch ARRIVE;
064
065  private static CountDownLatch RESUME;
066
067  public static final class HMasterForTest extends HMaster {
068
069    public HMasterForTest(Configuration conf) throws IOException {
070      super(conf);
071    }
072
073    private boolean isInAssignRegionsState() {
074      try {
075        for (StackTraceElement e : Thread.currentThread().getStackTrace()) {
076          if (
077            e.getClassName().equals(CreateTableProcedure.class.getName())
078              && e.getMethodName().equals("executeFromState")
079          ) {
080            for (Procedure<?> proc : getProcedures()) {
081              if (
082                proc instanceof CreateTableProcedure && !proc.isFinished()
083                  && ((CreateTableProcedure) proc).getCurrentStateId()
084                      == CreateTableState.CREATE_TABLE_ASSIGN_REGIONS_VALUE
085              ) {
086                return true;
087              }
088            }
089          }
090        }
091      } catch (IOException e) {
092        throw new UncheckedIOException(e);
093      }
094      return false;
095    }
096
097    @Override
098    public AssignmentManager getAssignmentManager() {
099      if (ARRIVE != null && isInAssignRegionsState()) {
100        ARRIVE.countDown();
101        ARRIVE = null;
102        try {
103          RESUME.await();
104        } catch (InterruptedException e) {
105        }
106      }
107      return super.getAssignmentManager();
108    }
109  }
110
111  @BeforeAll
112  public static void setUp() throws Exception {
113    UTIL.startMiniCluster(
114      StartTestingClusterOption.builder().masterClass(HMasterForTest.class).build());
115    // this may cause dead lock if there is no live region server and want to start a new server.
116    // In JmxCacheBuster we will reinitialize the metrics system so it will get some metrics which
117    // will need to access meta, since there is no region server, the request will hang there for a
118    // long time while holding the lock of MetricsSystemImpl, but when start a new region server, we
119    // also need to update metrics in handleReportForDutyResponse, since we are all in the same
120    // process and uses the same metrics instance, we hit dead lock.
121    JmxCacheBuster.stop();
122  }
123
124  @AfterAll
125  public static void tearDown() throws Exception {
126    UTIL.shutdownMiniCluster();
127  }
128
129  @Test
130  public void testCreate() throws Exception {
131    TableDescriptor td = TableDescriptorBuilder.newBuilder(TABLE_NAME)
132      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY)).build();
133    Admin admin = UTIL.getAdmin();
134    ARRIVE = new CountDownLatch(1);
135    RESUME = new CountDownLatch(1);
136    Future<Void> future = admin.createTableAsync(td);
137    ARRIVE.await();
138
139    UTIL.getMiniHBaseCluster().stopRegionServer(0).join();
140
141    // make sure we the region server is done.
142    UTIL.waitFor(30000,
143      () -> UTIL.getMiniHBaseCluster().getMaster().getServerManager().getOnlineServers().isEmpty());
144    RESUME.countDown();
145
146    Thread.sleep(10000);
147    // the procedure should still be in the CREATE_TABLE_ASSIGN_REGIONS state, but here, we just
148    // warn it as it may cause more serious problem later.
149    for (Procedure<?> proc : UTIL.getMiniHBaseCluster().getMaster().getProcedures()) {
150      if (
151        proc instanceof CreateTableProcedure && !proc.isFinished()
152          && ((CreateTableProcedure) proc).getCurrentStateId()
153              != CreateTableState.CREATE_TABLE_ASSIGN_REGIONS_VALUE
154      ) {
155        LOG.warn("Create table procedure {} assigned regions without a region server!", proc);
156      }
157    }
158    UTIL.getMiniHBaseCluster().startRegionServer();
159    // the creation should finally be done
160    future.get(30, TimeUnit.SECONDS);
161    // make sure we could put to the table
162    try (Table table =
163      UTIL.getConnection().getTableBuilder(TABLE_NAME, null).setOperationTimeout(5000).build()) {
164      table.put(new Put(Bytes.toBytes(0)).addColumn(FAMILY, Bytes.toBytes("q"), Bytes.toBytes(0)));
165    }
166  }
167}