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;
019
020import static org.junit.Assert.assertFalse;
021
022import java.util.concurrent.CountDownLatch;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HBaseTestingUtility;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
027import org.apache.hadoop.hbase.master.procedure.TableProcedureInterface;
028import org.apache.hadoop.hbase.procedure2.Procedure;
029import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
030import org.apache.hadoop.hbase.procedure2.ProcedureSuspendedException;
031import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility.NoopProcedure;
032import org.apache.hadoop.hbase.procedure2.ProcedureYieldException;
033import org.apache.hadoop.hbase.testclassification.MasterTests;
034import org.apache.hadoop.hbase.testclassification.MediumTests;
035import org.junit.AfterClass;
036import org.junit.BeforeClass;
037import org.junit.ClassRule;
038import org.junit.Test;
039import org.junit.experimental.categories.Category;
040
041import org.apache.hadoop.hbase.shaded.protobuf.generated.ProcedureProtos.ProcedureState;
042
043/**
044 * Testcase for HBASE-21490.
045 */
046@Category({ MasterTests.class, MediumTests.class })
047public class TestLoadProcedureError {
048
049  @ClassRule
050  public static final HBaseClassTestRule CLASS_RULE =
051    HBaseClassTestRule.forClass(TestLoadProcedureError.class);
052
053  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
054
055  private static TableName NAME = TableName.valueOf("Load");
056
057  private static volatile CountDownLatch ARRIVE;
058
059  private static volatile boolean FINISH_PROC;
060
061  private static volatile boolean FAIL_LOAD;
062
063  public static final class TestProcedure extends NoopProcedure<MasterProcedureEnv>
064      implements TableProcedureInterface {
065
066    @Override
067    protected Procedure<MasterProcedureEnv>[] execute(MasterProcedureEnv env)
068        throws ProcedureYieldException, ProcedureSuspendedException, InterruptedException {
069      if (ARRIVE != null) {
070        ARRIVE.countDown();
071        ARRIVE = null;
072      }
073      if (FINISH_PROC) {
074        return null;
075      }
076      setTimeout(1000);
077      setState(ProcedureState.WAITING_TIMEOUT);
078      throw new ProcedureSuspendedException();
079    }
080
081    @Override
082    protected synchronized boolean setTimeoutFailure(MasterProcedureEnv env) {
083      setState(ProcedureState.RUNNABLE);
084      env.getProcedureScheduler().addBack(this);
085      return false;
086    }
087
088    @Override
089    protected void afterReplay(MasterProcedureEnv env) {
090      if (FAIL_LOAD) {
091        throw new RuntimeException("Inject error");
092      }
093    }
094
095    @Override
096    public TableName getTableName() {
097      return NAME;
098    }
099
100    @Override
101    public TableOperationType getTableOperationType() {
102      return TableOperationType.READ;
103    }
104  }
105
106  @BeforeClass
107  public static void setUp() throws Exception {
108    UTIL.startMiniCluster(1);
109  }
110
111  @AfterClass
112  public static void tearDown() throws Exception {
113    UTIL.shutdownMiniCluster();
114  }
115
116  private void waitNoMaster() {
117    UTIL.waitFor(30000, () -> UTIL.getMiniHBaseCluster().getLiveMasterThreads().isEmpty());
118  }
119
120  @Test
121  public void testLoadError() throws Exception {
122    ProcedureExecutor<MasterProcedureEnv> procExec =
123      UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor();
124    ARRIVE = new CountDownLatch(1);
125    long procId = procExec.submitProcedure(new TestProcedure());
126    ARRIVE.await();
127    FAIL_LOAD = true;
128    // do not persist the store tracker
129    UTIL.getMiniHBaseCluster().getMaster().getProcedureStore().stop(true);
130    UTIL.getMiniHBaseCluster().getMaster().abort("for testing");
131    waitNoMaster();
132    // restart twice, and should fail twice, as we will throw an exception in the afterReplay above
133    // in order to reproduce the problem in HBASE-21490 stably, here we will wait until a master is
134    // fully done, before starting the new master, otherwise the new master may start too early and
135    // call recoverLease on the proc wal files and cause we fail to persist the store tracker when
136    // shutting down
137    UTIL.getMiniHBaseCluster().startMaster();
138    waitNoMaster();
139    UTIL.getMiniHBaseCluster().startMaster();
140    waitNoMaster();
141    FAIL_LOAD = false;
142    HMaster master = UTIL.getMiniHBaseCluster().startMaster().getMaster();
143    UTIL.waitFor(30000, () -> master.isActiveMaster() && master.isInitialized());
144    // assert the procedure is still there and not finished yet
145    TestProcedure proc = (TestProcedure) master.getMasterProcedureExecutor().getProcedure(procId);
146    assertFalse(proc.isFinished());
147    FINISH_PROC = true;
148    UTIL.waitFor(30000, () -> proc.isFinished());
149  }
150}