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.HBaseTestingUtility;
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;
047import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.GetProcedureResultRequest;
048import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.GetProcedureResultResponse;
049
050/**
051 * Testcase for HBASE-19608.
052 */
053@Category({ MasterTests.class, MediumTests.class })
054public class TestGetProcedureResult {
055
056  @ClassRule
057  public static final HBaseClassTestRule CLASS_RULE =
058      HBaseClassTestRule.forClass(TestGetProcedureResult.class);
059
060  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
061
062  public static final class DummyProcedure extends Procedure<MasterProcedureEnv>
063      implements TableProcedureInterface {
064
065    private final CountDownLatch failureSet = new CountDownLatch(1);
066
067    private final CountDownLatch canRollback = new CountDownLatch(1);
068
069    @Override
070    public TableName getTableName() {
071      return TableName.valueOf("dummy");
072    }
073
074    @Override
075    public TableOperationType getTableOperationType() {
076      return TableOperationType.READ;
077    }
078
079    @Override
080    protected Procedure<MasterProcedureEnv>[] execute(MasterProcedureEnv env)
081        throws ProcedureYieldException, ProcedureSuspendedException, InterruptedException {
082      setFailure("dummy", new IOException("inject error"));
083      failureSet.countDown();
084      return null;
085    }
086
087    @Override
088    protected void rollback(MasterProcedureEnv env) throws IOException, InterruptedException {
089      canRollback.await();
090    }
091
092    @Override
093    protected boolean abort(MasterProcedureEnv env) {
094      return false;
095    }
096
097    @Override
098    protected void serializeStateData(ProcedureStateSerializer serializer) throws IOException {
099    }
100
101    @Override
102    protected void deserializeStateData(ProcedureStateSerializer serializer) throws IOException {
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 GetProcedureResultResponse.State getState(long procId)
117      throws MasterNotRunningException, IOException, ServiceException {
118    MasterProtos.MasterService.BlockingInterface master =
119      ((ConnectionImplementation) UTIL.getConnection()).getMaster();
120    GetProcedureResultResponse resp = master.getProcedureResult(null,
121      GetProcedureResultRequest.newBuilder().setProcId(procId).build());
122    return resp.getState();
123  }
124
125  @Test
126  public void testRace() throws Exception {
127    ProcedureExecutor<MasterProcedureEnv> executor =
128      UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor();
129    DummyProcedure p = new DummyProcedure();
130    long procId = executor.submitProcedure(p);
131    p.failureSet.await();
132    assertEquals(GetProcedureResultResponse.State.RUNNING, getState(procId));
133    p.canRollback.countDown();
134    UTIL.waitFor(30000, new Waiter.ExplainingPredicate<Exception>() {
135
136      @Override
137      public boolean evaluate() throws Exception {
138        return getState(procId) == GetProcedureResultResponse.State.FINISHED;
139      }
140
141      @Override
142      public String explainFailure() throws Exception {
143        return "Procedure pid=" + procId + " is still in " + getState(procId) +
144          " state, expected " + GetProcedureResultResponse.State.FINISHED;
145      }
146    });
147  }
148}