HBASE-26567 Remove IndexType from ChunkCreator (#3947)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / ProcedureTestUtil.java
blobb4ba729b1d5219c2838731d39b27f98741fb86b4
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase;
20 import java.io.IOException;
21 import java.util.Iterator;
22 import java.util.Optional;
23 import org.apache.hadoop.hbase.procedure2.Procedure;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import org.apache.hbase.thirdparty.com.google.gson.JsonArray;
28 import org.apache.hbase.thirdparty.com.google.gson.JsonElement;
29 import org.apache.hbase.thirdparty.com.google.gson.JsonObject;
30 import org.apache.hbase.thirdparty.com.google.gson.JsonParser;
32 import org.apache.hadoop.hbase.shaded.protobuf.generated.ProcedureProtos.ProcedureState;
34 public final class ProcedureTestUtil {
36 private static final Logger LOG = LoggerFactory.getLogger(ProcedureTestUtil.class);
38 private ProcedureTestUtil() {
41 private static Optional<JsonObject> getProcedure(HBaseTestingUtil util,
42 Class<? extends Procedure<?>> clazz, JsonParser parser) throws IOException {
43 JsonArray array = parser.parse(util.getAdmin().getProcedures()).getAsJsonArray();
44 Iterator<JsonElement> iterator = array.iterator();
45 while (iterator.hasNext()) {
46 JsonElement element = iterator.next();
47 JsonObject obj = element.getAsJsonObject();
48 String className = obj.get("className").getAsString();
49 if (className.equals(clazz.getName())) {
50 return Optional.of(obj);
53 return Optional.empty();
56 public static void waitUntilProcedureWaitingTimeout(HBaseTestingUtil util,
57 Class<? extends Procedure<?>> clazz, long timeout) throws IOException {
58 JsonParser parser = new JsonParser();
59 util.waitFor(timeout,
60 () -> getProcedure(util, clazz, parser)
61 .filter(o -> ProcedureState.WAITING_TIMEOUT.name().equals(o.get("state").getAsString()))
62 .isPresent());
65 public static void waitUntilProcedureTimeoutIncrease(HBaseTestingUtil util,
66 Class<? extends Procedure<?>> clazz, int times) throws IOException, InterruptedException {
67 JsonParser parser = new JsonParser();
68 long oldTimeout = 0;
69 int timeoutIncrements = 0;
70 for (;;) {
71 long timeout = getProcedure(util, clazz, parser).filter(o -> o.has("timeout"))
72 .map(o -> o.get("timeout").getAsLong()).orElse(-1L);
73 if (timeout > oldTimeout) {
74 LOG.info("Timeout incremented, was {}, now is {}, increments={}", timeout, oldTimeout,
75 timeoutIncrements);
76 oldTimeout = timeout;
77 timeoutIncrements++;
78 if (timeoutIncrements > times) {
79 break;
82 Thread.sleep(1000);