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