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.ipc;
019
020import static org.junit.Assert.assertNotNull;
021import static org.junit.Assert.assertTrue;
022
023import java.util.Arrays;
024import java.util.Collection;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseTestingUtility;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.TableNameTestRule;
029import org.apache.hadoop.hbase.client.Get;
030import org.apache.hadoop.hbase.client.Put;
031import org.apache.hadoop.hbase.client.Result;
032import org.apache.hadoop.hbase.client.Table;
033import org.apache.hadoop.hbase.client.TableDescriptor;
034import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
035import org.apache.hadoop.hbase.regionserver.DisabledRegionSplitPolicy;
036import org.apache.hadoop.hbase.testclassification.MediumTests;
037import org.apache.hadoop.hbase.testclassification.RPCTests;
038import org.apache.hadoop.hbase.util.Bytes;
039import org.apache.hadoop.hbase.util.LoadTestKVGenerator;
040import org.junit.After;
041import org.junit.Before;
042import org.junit.ClassRule;
043import org.junit.Rule;
044import org.junit.Test;
045import org.junit.experimental.categories.Category;
046import org.junit.runner.RunWith;
047import org.junit.runners.Parameterized;
048import org.junit.runners.Parameterized.Parameters;
049
050@Category({ RPCTests.class, MediumTests.class })
051@RunWith(Parameterized.class)
052public class TestNettyRpcServer {
053
054  @ClassRule
055  public static final HBaseClassTestRule CLASS_RULE =
056    HBaseClassTestRule.forClass(TestNettyRpcServer.class);
057
058  private static final byte[] FAMILY = Bytes.toBytes("f");
059  private static final byte[] QUALIFIER = Bytes.toBytes("q");
060  private static final int NUM_ROWS = 100;
061  private static final int MIN_LEN = 1000;
062  private static final int MAX_LEN = 1000000;
063  protected static final LoadTestKVGenerator GENERATOR = new LoadTestKVGenerator(MIN_LEN, MAX_LEN);
064  protected static HBaseTestingUtility TEST_UTIL;
065
066  @Rule
067  public TableNameTestRule name = new TableNameTestRule();
068
069  @Parameterized.Parameter
070  public String allocatorType;
071
072  @Parameters
073  public static Collection<Object[]> parameters() {
074    return Arrays.asList(new Object[][] { { NettyRpcServer.POOLED_ALLOCATOR_TYPE },
075      { NettyRpcServer.UNPOOLED_ALLOCATOR_TYPE }, { NettyRpcServer.HEAP_ALLOCATOR_TYPE },
076      { SimpleByteBufAllocator.class.getName() } });
077  }
078
079  @Before
080  public void setup() throws Exception {
081    // A subclass may have already created TEST_UTIL and is now upcalling to us
082    if (TEST_UTIL == null) {
083      TEST_UTIL = new HBaseTestingUtility();
084    }
085    TEST_UTIL.getConfiguration().set(RpcServerFactory.CUSTOM_RPC_SERVER_IMPL_CONF_KEY,
086      NettyRpcServer.class.getName());
087    TEST_UTIL.getConfiguration().set(NettyRpcServer.HBASE_NETTY_ALLOCATOR_KEY, allocatorType);
088    TEST_UTIL.startMiniCluster();
089  }
090
091  @After
092  public void tearDown() throws Exception {
093    TEST_UTIL.shutdownMiniCluster();
094  }
095
096  @Test
097  public void testNettyRpcServer() throws Exception {
098    doTest(name.getTableName());
099  }
100
101  protected void doTest(TableName tableName) throws Exception {
102    // Splitting just complicates the test scenario, disable it
103    final TableDescriptor desc = TableDescriptorBuilder.newBuilder(tableName)
104      .setRegionSplitPolicyClassName(DisabledRegionSplitPolicy.class.getName()).build();
105    try (Table table =
106      TEST_UTIL.createTable(desc, new byte[][] { FAMILY }, TEST_UTIL.getConfiguration())) {
107      // put some test data
108      for (int i = 0; i < NUM_ROWS; i++) {
109        final byte[] rowKey = Bytes.toBytes(LoadTestKVGenerator.md5PrefixedKey(i));
110        final byte[] v = GENERATOR.generateRandomSizeValue(rowKey, QUALIFIER);
111        table.put(new Put(rowKey).addColumn(FAMILY, QUALIFIER, v));
112      }
113      // read to verify it.
114      for (int i = 0; i < NUM_ROWS; i++) {
115        final byte[] rowKey = Bytes.toBytes(LoadTestKVGenerator.md5PrefixedKey(i));
116        final Result r = table.get(new Get(rowKey).addColumn(FAMILY, QUALIFIER));
117        assertNotNull("Result was empty", r);
118        final byte[] v = r.getValue(FAMILY, QUALIFIER);
119        assertNotNull("Result did not contain expected value", v);
120        assertTrue("Value was not verified", LoadTestKVGenerator.verify(v, rowKey, QUALIFIER));
121      }
122    }
123  }
124
125}