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;
019
020import java.io.IOException;
021import java.util.Iterator;
022import java.util.Optional;
023import org.apache.hadoop.hbase.procedure2.Procedure;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026
027import org.apache.hbase.thirdparty.com.google.gson.JsonArray;
028import org.apache.hbase.thirdparty.com.google.gson.JsonElement;
029import org.apache.hbase.thirdparty.com.google.gson.JsonObject;
030import org.apache.hbase.thirdparty.com.google.gson.JsonParser;
031
032import org.apache.hadoop.hbase.shaded.protobuf.generated.ProcedureProtos.ProcedureState;
033
034public final class ProcedureTestUtil {
035
036  private static final Logger LOG = LoggerFactory.getLogger(ProcedureTestUtil.class);
037
038  private ProcedureTestUtil() {
039  }
040
041  private static Optional<JsonObject> getProcedure(HBaseTestingUtility util,
042    Class<? extends Procedure<?>> clazz, JsonParser parser) throws IOException {
043    JsonArray array = parser.parse(util.getAdmin().getProcedures()).getAsJsonArray();
044    Iterator<JsonElement> iterator = array.iterator();
045    while (iterator.hasNext()) {
046      JsonElement element = iterator.next();
047      JsonObject obj = element.getAsJsonObject();
048      String className = obj.get("className").getAsString();
049      if (className.equals(clazz.getName())) {
050        return Optional.of(obj);
051      }
052    }
053    return Optional.empty();
054  }
055
056  public static void waitUntilProcedureWaitingTimeout(HBaseTestingUtility util,
057    Class<? extends Procedure<?>> clazz, long timeout) throws IOException {
058    JsonParser parser = new JsonParser();
059    util.waitFor(timeout,
060      () -> getProcedure(util, clazz, parser)
061        .filter(o -> ProcedureState.WAITING_TIMEOUT.name().equals(o.get("state").getAsString()))
062        .isPresent());
063  }
064
065  public static void waitUntilProcedureTimeoutIncrease(HBaseTestingUtility util,
066    Class<? extends Procedure<?>> clazz, int times) throws IOException, InterruptedException {
067    JsonParser parser = new JsonParser();
068    long oldTimeout = 0;
069    int timeoutIncrements = 0;
070    for (;;) {
071      long timeout = getProcedure(util, clazz, parser).filter(o -> o.has("timeout"))
072        .map(o -> o.get("timeout").getAsLong()).orElse(-1L);
073      if (timeout > oldTimeout) {
074        LOG.info("Timeout incremented, was {}, now is {}, increments={}", timeout, oldTimeout,
075          timeoutIncrements);
076        oldTimeout = timeout;
077        timeoutIncrements++;
078        if (timeoutIncrements > times) {
079          break;
080        }
081      }
082      Thread.sleep(1000);
083    }
084  }
085}