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 static org.junit.jupiter.api.Assertions.assertEquals;
021
022import java.io.IOException;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.fs.Path;
025import org.apache.hadoop.hbase.HBaseTestingUtil;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
028import org.apache.hadoop.hbase.client.RegionInfo;
029import org.apache.hadoop.hbase.client.RegionInfoBuilder;
030import org.apache.hadoop.hbase.client.TableDescriptor;
031import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
032import org.apache.hadoop.hbase.master.HMaster;
033import org.apache.hadoop.hbase.procedure2.Procedure;
034import org.apache.hadoop.hbase.procedure2.ProcedureEvent;
035import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
036import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
037import org.apache.hadoop.hbase.procedure2.store.wal.WALProcedureStore;
038import org.apache.hadoop.hbase.testclassification.MasterTests;
039import org.apache.hadoop.hbase.testclassification.MediumTests;
040import org.apache.hadoop.hbase.util.CommonFSUtils;
041import org.junit.jupiter.api.AfterAll;
042import org.junit.jupiter.api.AfterEach;
043import org.junit.jupiter.api.BeforeAll;
044import org.junit.jupiter.api.BeforeEach;
045import org.junit.jupiter.api.Tag;
046import org.junit.jupiter.api.Test;
047import org.junit.jupiter.api.TestInfo;
048import org.slf4j.Logger;
049import org.slf4j.LoggerFactory;
050
051@Tag(MasterTests.TAG)
052@Tag(MediumTests.TAG)
053public class TestMasterProcedureEvents {
054
055  private static final Logger LOG = LoggerFactory.getLogger(TestMasterProcedureEvents.class);
056
057  protected static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
058  private String testMethodName;
059
060  @BeforeEach
061  public void setTestMethod(TestInfo testInfo) {
062    testMethodName = testInfo.getTestMethod().get().getName();
063  }
064
065  private static void setupConf(Configuration conf) throws IOException {
066    conf.setInt(MasterProcedureConstants.MASTER_PROCEDURE_THREADS, 1);
067    conf.setBoolean(WALProcedureStore.USE_HSYNC_CONF_KEY, false);
068  }
069
070  @BeforeAll
071  public static void setupCluster() throws Exception {
072    Configuration conf = UTIL.getConfiguration();
073    setupConf(UTIL.getConfiguration());
074    UTIL.startMiniDFSCluster(3);
075    CommonFSUtils.setWALRootDir(conf, new Path(conf.get("fs.defaultFS"), "/tmp/wal"));
076    UTIL.startMiniCluster(2);
077    UTIL.waitUntilNoRegionsInTransition();
078  }
079
080  @AfterAll
081  public static void cleanupTest() throws Exception {
082    try {
083      UTIL.shutdownMiniCluster();
084    } catch (Exception e) {
085      LOG.warn("failure shutting down cluster", e);
086    }
087  }
088
089  @AfterEach
090  public void tearDown() throws Exception {
091    for (TableDescriptor htd : UTIL.getAdmin().listTableDescriptors()) {
092      LOG.info("Tear down, remove table=" + htd.getTableName());
093      UTIL.deleteTable(htd.getTableName());
094    }
095  }
096
097  @Test
098  public void testMasterInitializedEvent() throws Exception {
099    final TableName tableName = TableName.valueOf(testMethodName);
100    HMaster master = UTIL.getMiniHBaseCluster().getMaster();
101    ProcedureExecutor<MasterProcedureEnv> procExec = master.getMasterProcedureExecutor();
102
103    RegionInfo hri = RegionInfoBuilder.newBuilder(tableName).build();
104    TableDescriptor htd = TableDescriptorBuilder.newBuilder(tableName)
105      .setColumnFamily(ColumnFamilyDescriptorBuilder.of("f")).build();
106
107    while (!master.isInitialized()) {
108      Thread.sleep(250);
109    }
110    master.setInitialized(false); // fake it, set back later
111
112    // check event wait/wake
113    testProcedureEventWaitWake(master, master.getInitializedEvent(),
114      new CreateTableProcedure(procExec.getEnvironment(), htd, new RegionInfo[] { hri }));
115  }
116
117  private void testProcedureEventWaitWake(final HMaster master, final ProcedureEvent<?> event,
118    final Procedure<MasterProcedureEnv> proc) throws Exception {
119    final ProcedureExecutor<MasterProcedureEnv> procExec = master.getMasterProcedureExecutor();
120    final MasterProcedureScheduler procSched = procExec.getEnvironment().getProcedureScheduler();
121
122    final long startPollCalls = procSched.getPollCalls();
123    final long startNullPollCalls = procSched.getNullPollCalls();
124
125    // check that nothing is in the event queue
126    LOG.debug("checking " + event);
127    assertEquals(false, event.isReady());
128    assertEquals(0, event.getSuspendedProcedures().size());
129
130    // submit the procedure
131    LOG.debug("submit " + proc);
132    long procId = procExec.submitProcedure(proc);
133
134    // wait until the event is in the queue (proc executed and got into suspended state)
135    LOG.debug("wait procedure suspended on " + event);
136    while (event.getSuspendedProcedures().size() < 1)
137      Thread.sleep(25);
138
139    // check that the proc is in the event queue
140    LOG.debug("checking " + event + " size=" + event.getSuspendedProcedures().size());
141    assertEquals(false, event.isReady());
142    assertEquals(1, event.getSuspendedProcedures().size());
143
144    // wake the event
145    LOG.debug("wake " + event);
146    event.wake(procSched);
147    assertEquals(true, event.isReady());
148
149    // wait until proc completes
150    LOG.debug("waiting " + proc);
151    ProcedureTestingUtility.waitProcedure(procExec, procId);
152
153    // check that nothing is in the event queue and the event is not suspended
154    assertEquals(true, event.isReady());
155    assertEquals(0, event.getSuspendedProcedures().size());
156    LOG.debug(
157      "completed execution of " + proc + " pollCalls=" + (procSched.getPollCalls() - startPollCalls)
158        + " nullPollCalls=" + (procSched.getNullPollCalls() - startNullPollCalls));
159  }
160}