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.client;
019
020import static org.junit.Assert.assertEquals;
021
022import java.io.IOException;
023import java.util.concurrent.CountDownLatch;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.HBaseTestingUtil;
026import org.apache.hadoop.hbase.MasterNotRunningException;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.Waiter;
029import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
030import org.apache.hadoop.hbase.master.procedure.TableProcedureInterface;
031import org.apache.hadoop.hbase.procedure2.Procedure;
032import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
033import org.apache.hadoop.hbase.procedure2.ProcedureStateSerializer;
034import org.apache.hadoop.hbase.procedure2.ProcedureSuspendedException;
035import org.apache.hadoop.hbase.procedure2.ProcedureYieldException;
036import org.apache.hadoop.hbase.testclassification.MasterTests;
037import org.apache.hadoop.hbase.testclassification.MediumTests;
038import org.junit.AfterClass;
039import org.junit.BeforeClass;
040import org.junit.ClassRule;
041import org.junit.Test;
042import org.junit.experimental.categories.Category;
043
044import org.apache.hbase.thirdparty.com.google.protobuf.ServiceException;
045
046import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.GetProcedureResultRequest;
047import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.GetProcedureResultResponse;
048
049/**
050 * Testcase for HBASE-19608.
051 */
052@Category({ MasterTests.class, MediumTests.class })
053public class TestGetProcedureResult {
054
055  @ClassRule
056  public static final HBaseClassTestRule CLASS_RULE =
057    HBaseClassTestRule.forClass(TestGetProcedureResult.class);
058
059  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
060
061  public static final class DummyProcedure extends Procedure<MasterProcedureEnv>
062    implements TableProcedureInterface {
063
064    private final CountDownLatch failureSet = new CountDownLatch(1);
065
066    private final CountDownLatch canRollback = new CountDownLatch(1);
067
068    @Override
069    public TableName getTableName() {
070      return TableName.valueOf("dummy");
071    }
072
073    @Override
074    public TableOperationType getTableOperationType() {
075      return TableOperationType.READ;
076    }
077
078    @Override
079    protected Procedure<MasterProcedureEnv>[] execute(MasterProcedureEnv env)
080      throws ProcedureYieldException, ProcedureSuspendedException, InterruptedException {
081      setFailure("dummy", new IOException("inject error"));
082      failureSet.countDown();
083      return null;
084    }
085
086    @Override
087    protected void rollback(MasterProcedureEnv env) throws IOException, InterruptedException {
088      canRollback.await();
089    }
090
091    @Override
092    protected boolean abort(MasterProcedureEnv env) {
093      return false;
094    }
095
096    @Override
097    protected void serializeStateData(ProcedureStateSerializer serializer) throws IOException {
098    }
099
100    @Override
101    protected void deserializeStateData(ProcedureStateSerializer serializer) throws IOException {
102    }
103  }
104
105  @BeforeClass
106  public static void setUp() throws Exception {
107    UTIL.startMiniCluster(1);
108  }
109
110  @AfterClass
111  public static void tearDown() throws Exception {
112    UTIL.shutdownMiniCluster();
113  }
114
115  private GetProcedureResultResponse.State getState(long procId)
116    throws MasterNotRunningException, IOException, ServiceException {
117    GetProcedureResultResponse resp = UTIL.getMiniHBaseCluster().getMaster().getMasterRpcServices()
118      .getProcedureResult(null, GetProcedureResultRequest.newBuilder().setProcId(procId).build());
119    return resp.getState();
120  }
121
122  @Test
123  public void testRace() throws Exception {
124    ProcedureExecutor<MasterProcedureEnv> executor =
125      UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor();
126    DummyProcedure p = new DummyProcedure();
127    long procId = executor.submitProcedure(p);
128    p.failureSet.await();
129    assertEquals(GetProcedureResultResponse.State.RUNNING, getState(procId));
130    p.canRollback.countDown();
131    UTIL.waitFor(30000, new Waiter.ExplainingPredicate<Exception>() {
132
133      @Override
134      public boolean evaluate() throws Exception {
135        return getState(procId) == GetProcedureResultResponse.State.FINISHED;
136      }
137
138      @Override
139      public String explainFailure() throws Exception {
140        return "Procedure pid=" + procId + " is still in " + getState(procId) + " state, expected "
141          + GetProcedureResultResponse.State.FINISHED;
142      }
143    });
144  }
145}