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.ArrayList;
024import java.util.Collection;
025import java.util.List;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HBaseConfiguration;
029import org.apache.hadoop.hbase.HBaseTestingUtility;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.ipc.NettyRpcServer;
032import org.apache.hadoop.hbase.ipc.RpcServerFactory;
033import org.apache.hadoop.hbase.ipc.SimpleRpcServer;
034import org.apache.hadoop.hbase.testclassification.ClientTests;
035import org.apache.hadoop.hbase.testclassification.MediumTests;
036import org.apache.hadoop.hbase.util.Bytes;
037import org.junit.After;
038import org.junit.Before;
039import org.junit.ClassRule;
040import org.junit.Rule;
041import org.junit.Test;
042import org.junit.experimental.categories.Category;
043import org.junit.rules.TestName;
044import org.junit.runner.RunWith;
045import org.junit.runners.Parameterized;
046
047/**
048 * HBASE-19496 noticed that the RegionLoad/ServerLoad may be corrupted if rpc server
049 * reuses the bytebuffer backed, so this test call the Admin#getLastMajorCompactionTimestamp() to
050 * invoke HMaster to iterate all stored server/region loads.
051 */
052@RunWith(Parameterized.class)
053@Category({ MediumTests.class, ClientTests.class })
054public class TestServerLoadDurability {
055
056  @ClassRule
057  public static final HBaseClassTestRule CLASS_RULE =
058      HBaseClassTestRule.forClass(TestServerLoadDurability.class);
059
060  private static final byte[] FAMILY = Bytes.toBytes("testFamily");
061
062  @Parameterized.Parameter
063  public Configuration conf;
064
065  @Parameterized.Parameters
066  public static final Collection<Object[]> parameters() {
067    List<Object[]> configurations = new ArrayList<>();
068    configurations.add(new Object[] { createConfigurationForSimpleRpcServer() });
069    configurations.add(new Object[] { createConfigurationForNettyRpcServer() });
070    return configurations;
071  }
072
073  private static Configuration createConfigurationForSimpleRpcServer() {
074    Configuration conf = HBaseConfiguration.create();
075    conf.set(RpcServerFactory.CUSTOM_RPC_SERVER_IMPL_CONF_KEY, SimpleRpcServer.class.getName());
076    conf.setInt(BUFFER_SIZE_KEY, 20);
077    return conf;
078  }
079
080  private static Configuration createConfigurationForNettyRpcServer() {
081    Configuration conf = HBaseConfiguration.create();
082    conf.set(RpcServerFactory.CUSTOM_RPC_SERVER_IMPL_CONF_KEY,
083        NettyRpcServer.class.getName());
084    return conf;
085  }
086
087  protected HBaseTestingUtility utility;
088  protected Connection conn;
089  protected Admin admin;
090
091  @Rule
092  public TestName testName = new TestName();
093  protected TableName tableName;
094
095  @Before
096  public void setUp() throws Exception {
097    utility = new HBaseTestingUtility(conf);
098    utility.startMiniCluster(2);
099    conn = ConnectionFactory.createConnection(utility.getConfiguration());
100    admin = conn.getAdmin();
101    String methodName = testName.getMethodName();
102    tableName = TableName.valueOf(methodName.substring(0, methodName.length() - 3));
103  }
104
105  @After
106  public void tearDown() throws Exception {
107    utility.shutdownMiniCluster();
108  }
109
110  @Test
111  public void testCompactionTimestamps() throws Exception {
112    createTableWithDefaultConf(tableName);
113    try (Table table = conn.getTable(tableName)) {
114      long ts = admin.getLastMajorCompactionTimestamp(tableName);
115    }
116  }
117
118  private void createTableWithDefaultConf(TableName tableName) throws IOException {
119    TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName);
120    builder.setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY));
121    admin.createTable(builder.build());
122  }
123
124}