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 java.io.IOException;
021import java.util.ArrayList;
022import java.util.Collection;
023import java.util.List;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseConfiguration;
027import org.apache.hadoop.hbase.HBaseTestingUtility;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.io.ByteBufferPool;
030import org.apache.hadoop.hbase.ipc.NettyRpcServer;
031import org.apache.hadoop.hbase.ipc.RpcServerFactory;
032import org.apache.hadoop.hbase.ipc.SimpleRpcServer;
033import org.apache.hadoop.hbase.testclassification.ClientTests;
034import org.apache.hadoop.hbase.testclassification.MediumTests;
035import org.apache.hadoop.hbase.util.Bytes;
036import org.junit.After;
037import org.junit.Before;
038import org.junit.ClassRule;
039import org.junit.Rule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042import org.junit.rules.TestName;
043import org.junit.runner.RunWith;
044import org.junit.runners.Parameterized;
045
046/**
047 * HBASE-19496 noticed that the RegionLoad/ServerLoad may be corrupted if rpc server
048 * reuses the bytebuffer backed, so this test call the Admin#getLastMajorCompactionTimestamp() to
049 * invoke HMaster to iterate all stored server/region loads.
050 */
051@RunWith(Parameterized.class)
052@Category({ MediumTests.class, ClientTests.class })
053public class TestServerLoadDurability {
054
055  @ClassRule
056  public static final HBaseClassTestRule CLASS_RULE =
057      HBaseClassTestRule.forClass(TestServerLoadDurability.class);
058
059  private static final byte[] FAMILY = Bytes.toBytes("testFamily");
060
061  @Parameterized.Parameter
062  public Configuration conf;
063
064  @Parameterized.Parameters
065  public static final Collection<Object[]> parameters() {
066    List<Object[]> configurations = new ArrayList<>();
067    configurations.add(new Object[] { createConfigurationForSimpleRpcServer() });
068    configurations.add(new Object[] { createConfigurationForNettyRpcServer() });
069    return configurations;
070  }
071
072  private static Configuration createConfigurationForSimpleRpcServer() {
073    Configuration conf = HBaseConfiguration.create();
074    conf.set(RpcServerFactory.CUSTOM_RPC_SERVER_IMPL_CONF_KEY,
075        SimpleRpcServer.class.getName());
076    conf.setInt(ByteBufferPool.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}