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.Assert.assertFalse;
021import static org.junit.Assert.assertThrows;
022
023import java.util.ArrayList;
024import java.util.List;
025import java.util.concurrent.CompletableFuture;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HBaseTestingUtil;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.procedure.flush.MasterFlushTableProcedureManager;
031import org.apache.hadoop.hbase.regionserver.HRegion;
032import org.apache.hadoop.hbase.regionserver.NoSuchColumnFamilyException;
033import org.apache.hadoop.hbase.testclassification.ClientTests;
034import org.apache.hadoop.hbase.testclassification.MediumTests;
035import org.apache.hadoop.hbase.util.Bytes;
036import org.apache.hadoop.hbase.util.FutureUtils;
037import org.junit.After;
038import org.junit.AfterClass;
039import org.junit.Before;
040import org.junit.BeforeClass;
041import org.junit.ClassRule;
042import org.junit.Rule;
043import org.junit.Test;
044import org.junit.experimental.categories.Category;
045import org.junit.rules.TestName;
046import org.slf4j.Logger;
047import org.slf4j.LoggerFactory;
048
049import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
050
051@Category({ MediumTests.class, ClientTests.class })
052public class TestFlushFromClientWithDisabledFlushProcedure {
053
054  @ClassRule
055  public static final HBaseClassTestRule CLASS_RULE =
056    HBaseClassTestRule.forClass(TestFlushFromClientWithDisabledFlushProcedure.class);
057
058  private static final Logger LOG =
059    LoggerFactory.getLogger(TestFlushFromClientWithDisabledFlushProcedure.class);
060  private final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
061  private static AsyncConnection asyncConn;
062  private static final byte[] FAMILY = Bytes.toBytes("info");
063  private static final byte[] QUALIFIER = Bytes.toBytes("name");
064
065  @Rule
066  public TestName name = new TestName();
067
068  private TableName tableName;
069
070  @BeforeClass
071  public static void setUpBeforeClass() throws Exception {
072    Configuration configuration = TEST_UTIL.getConfiguration();
073    configuration.setBoolean(MasterFlushTableProcedureManager.FLUSH_PROCEDURE_ENABLED, false);
074    TEST_UTIL.startMiniCluster(1);
075    asyncConn = ConnectionFactory.createAsyncConnection(TEST_UTIL.getConfiguration()).get();
076  }
077
078  @AfterClass
079  public static void tearDownAfterClass() throws Exception {
080    Closeables.close(asyncConn, true);
081    TEST_UTIL.shutdownMiniCluster();
082  }
083
084  @Before
085  public void setUp() throws Exception {
086    tableName = TableName.valueOf(name.getMethodName());
087    try (Table t = TEST_UTIL.createTable(tableName, FAMILY)) {
088      List<Put> puts = new ArrayList<>();
089      for (int i = 0; i <= 10; ++i) {
090        Put put = new Put(Bytes.toBytes(i));
091        put.addColumn(FAMILY, QUALIFIER, Bytes.toBytes(i));
092        puts.add(put);
093      }
094      t.put(puts);
095    }
096    List<HRegion> regions = TEST_UTIL.getHBaseCluster().getRegions(tableName);
097    assertFalse(regions.isEmpty());
098  }
099
100  @After
101  public void tearDown() throws Exception {
102    for (TableDescriptor htd : TEST_UTIL.getAdmin().listTableDescriptors()) {
103      LOG.info("Tear down, remove table=" + htd.getTableName());
104      TEST_UTIL.deleteTable(htd.getTableName());
105    }
106  }
107
108  @Test
109  public void flushTableWithNonExistingFamily() {
110    AsyncAdmin admin = asyncConn.getAdmin();
111    List<byte[]> families = new ArrayList<>();
112    families.add(FAMILY);
113    families.add(Bytes.toBytes("non_family01"));
114    families.add(Bytes.toBytes("non_family02"));
115    assertFalse(TEST_UTIL.getConfiguration().getBoolean(
116      MasterFlushTableProcedureManager.FLUSH_PROCEDURE_ENABLED,
117      MasterFlushTableProcedureManager.FLUSH_PROCEDURE_ENABLED_DEFAULT));
118    CompletableFuture<Void> future = CompletableFuture.allOf(admin.flush(tableName, families));
119    assertThrows(NoSuchColumnFamilyException.class, () -> FutureUtils.get(future));
120  }
121}