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