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.io.ByteBuffAllocator.BUFFER_SIZE_KEY;
021
022import java.io.IOException;
023import java.util.stream.Stream;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.HBaseConfiguration;
026import org.apache.hadoop.hbase.HBaseParameterizedTestTemplate;
027import org.apache.hadoop.hbase.HBaseTestingUtil;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.ipc.NettyRpcServer;
030import org.apache.hadoop.hbase.ipc.RpcServerFactory;
031import org.apache.hadoop.hbase.ipc.SimpleRpcServer;
032import org.apache.hadoop.hbase.testclassification.ClientTests;
033import org.apache.hadoop.hbase.testclassification.MediumTests;
034import org.apache.hadoop.hbase.util.Bytes;
035import org.junit.jupiter.api.AfterEach;
036import org.junit.jupiter.api.BeforeEach;
037import org.junit.jupiter.api.Tag;
038import org.junit.jupiter.api.TestTemplate;
039import org.junit.jupiter.params.provider.Arguments;
040
041/**
042 * HBASE-19496 noticed that the RegionLoad/ServerLoad may be corrupted if rpc server reuses the
043 * bytebuffer backed, so this test call the Admin#getLastMajorCompactionTimestamp() to invoke
044 * HMaster to iterate all stored server/region loads.
045 */
046@Tag(MediumTests.TAG)
047@Tag(ClientTests.TAG)
048@HBaseParameterizedTestTemplate
049public class TestServerLoadDurability {
050
051  private static final byte[] FAMILY = Bytes.toBytes("testFamily");
052
053  private final Configuration conf;
054
055  public TestServerLoadDurability(Configuration conf) {
056    this.conf = conf;
057  }
058
059  public static Stream<Arguments> parameters() {
060    return Stream.of(Arguments.of(createConfigurationForSimpleRpcServer()),
061      Arguments.of(createConfigurationForNettyRpcServer()));
062  }
063
064  private static Configuration createConfigurationForSimpleRpcServer() {
065    Configuration conf = HBaseConfiguration.create();
066    conf.set(RpcServerFactory.CUSTOM_RPC_SERVER_IMPL_CONF_KEY, SimpleRpcServer.class.getName());
067    conf.setInt(BUFFER_SIZE_KEY, 20);
068    return conf;
069  }
070
071  private static Configuration createConfigurationForNettyRpcServer() {
072    Configuration conf = HBaseConfiguration.create();
073    conf.set(RpcServerFactory.CUSTOM_RPC_SERVER_IMPL_CONF_KEY, NettyRpcServer.class.getName());
074    return conf;
075  }
076
077  protected HBaseTestingUtil utility;
078  protected Connection conn;
079  protected Admin admin;
080
081  protected TableName tableName;
082
083  @BeforeEach
084  public void setUp() throws Exception {
085    utility = new HBaseTestingUtil(conf);
086    utility.startMiniCluster(2);
087    conn = ConnectionFactory.createConnection(utility.getConfiguration());
088    admin = conn.getAdmin();
089    tableName = TableName.valueOf("testTable");
090  }
091
092  @AfterEach
093  public void tearDown() throws Exception {
094    utility.shutdownMiniCluster();
095  }
096
097  @TestTemplate
098  public void testCompactionTimestamps() throws Exception {
099    createTableWithDefaultConf(tableName);
100    try (Table table = conn.getTable(tableName)) {
101      admin.getLastMajorCompactionTimestamp(tableName);
102    }
103  }
104
105  private void createTableWithDefaultConf(TableName tableName) throws IOException {
106    TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName);
107    builder.setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY));
108    admin.createTable(builder.build());
109  }
110}