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