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.apache.hadoop.hbase.client.AsyncConnectionConfiguration.START_LOG_ERRORS_AFTER_COUNT_KEY;
021import static org.junit.jupiter.api.Assertions.assertArrayEquals;
022import static org.junit.jupiter.api.Assertions.assertFalse;
023import static org.junit.jupiter.api.Assertions.assertTrue;
024
025import java.util.HashMap;
026import java.util.Map;
027import java.util.concurrent.ThreadLocalRandom;
028import java.util.function.Supplier;
029import org.apache.hadoop.hbase.HBaseParameterizedTestTemplate;
030import org.apache.hadoop.hbase.HConstants;
031import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
032import org.apache.hadoop.hbase.procedure.ProcedureManagerHost;
033import org.apache.hadoop.hbase.procedure.SimpleMasterProcedureManager;
034import org.apache.hadoop.hbase.procedure.SimpleRSProcedureManager;
035import org.apache.hadoop.hbase.testclassification.ClientTests;
036import org.apache.hadoop.hbase.testclassification.LargeTests;
037import org.apache.hadoop.hbase.util.Bytes;
038import org.junit.jupiter.api.AfterAll;
039import org.junit.jupiter.api.BeforeAll;
040import org.junit.jupiter.api.Tag;
041import org.junit.jupiter.api.TestTemplate;
042
043/**
044 * Class to test asynchronous procedure admin operations.
045 */
046@Tag(LargeTests.TAG)
047@Tag(ClientTests.TAG)
048@HBaseParameterizedTestTemplate(name = "{index}: policy = {0}")
049public class TestAsyncProcedureAdminApi extends TestAsyncAdminBase {
050
051  public TestAsyncProcedureAdminApi(Supplier<AsyncAdmin> admin) {
052    super(admin);
053  }
054
055  @BeforeAll
056  public static void setUpBeforeClass() throws Exception {
057    TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_RPC_TIMEOUT_KEY, 60000);
058    TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_OPERATION_TIMEOUT, 120000);
059    TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2);
060    TEST_UTIL.getConfiguration().setInt(START_LOG_ERRORS_AFTER_COUNT_KEY, 0);
061    TEST_UTIL.getConfiguration().set(ProcedureManagerHost.MASTER_PROCEDURE_CONF_KEY,
062      SimpleMasterProcedureManager.class.getName());
063    TEST_UTIL.getConfiguration().set(ProcedureManagerHost.REGIONSERVER_PROCEDURE_CONF_KEY,
064      SimpleRSProcedureManager.class.getName());
065    TEST_UTIL.getConfiguration().setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
066    TEST_UTIL.startMiniCluster(2);
067    ASYNC_CONN = ConnectionFactory.createAsyncConnection(TEST_UTIL.getConfiguration()).get();
068  }
069
070  @AfterAll
071  public static void tearDownAfterClass() throws Exception {
072    TestAsyncAdminBase.tearDownAfterClass();
073  }
074
075  @TestTemplate
076  public void testExecProcedure() throws Exception {
077    String snapshotString = "offlineTableSnapshot";
078    try {
079      Table table = TEST_UTIL.createTable(tableName, Bytes.toBytes("cf"));
080      for (int i = 0; i < 100; i++) {
081        Put put = new Put(Bytes.toBytes(i)).addColumn(Bytes.toBytes("cf"), null, Bytes.toBytes(i));
082        table.put(put);
083      }
084      // take a snapshot of the enabled table
085      Map<String, String> props = new HashMap<>();
086      props.put("table", tableName.getNameAsString());
087      admin.execProcedure(SnapshotManager.ONLINE_SNAPSHOT_CONTROLLER_DESCRIPTION, snapshotString,
088        props).get();
089      LOG.debug("Snapshot completed.");
090    } finally {
091      admin.deleteSnapshot(snapshotString).join();
092      TEST_UTIL.deleteTable(tableName);
093    }
094  }
095
096  @TestTemplate
097  public void testExecProcedureWithRet() throws Exception {
098    byte[] result = admin.execProcedureWithReturn(SimpleMasterProcedureManager.SIMPLE_SIGNATURE,
099      "myTest2", new HashMap<>()).get();
100    assertArrayEquals(Bytes.toBytes(SimpleMasterProcedureManager.SIMPLE_DATA), result,
101      "Incorrect return data from execProcedure");
102  }
103
104  @TestTemplate
105  public void listProcedure() throws Exception {
106    String procList = admin.getProcedures().get();
107    assertTrue(procList.startsWith("["));
108  }
109
110  @TestTemplate
111  public void isProcedureFinished() throws Exception {
112    boolean failed = false;
113    try {
114      admin.isProcedureFinished("fake-signature", "fake-instance", new HashMap<>()).get();
115    } catch (Exception e) {
116      failed = true;
117    }
118    assertTrue(failed);
119  }
120
121  @TestTemplate
122  public void abortProcedure() throws Exception {
123    long procId = ThreadLocalRandom.current().nextLong();
124    boolean abortResult = admin.abortProcedure(procId, true).get();
125    assertFalse(abortResult);
126  }
127}