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.ipc.RpcServer.MAX_REQUEST_SIZE;
021import static org.junit.Assert.assertThrows;
022
023import java.util.concurrent.ThreadLocalRandom;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.HBaseTestingUtil;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.exceptions.RequestTooBigException;
028import org.apache.hadoop.hbase.testclassification.ClientTests;
029import org.apache.hadoop.hbase.testclassification.MediumTests;
030import org.apache.hadoop.hbase.util.Bytes;
031import org.junit.AfterClass;
032import org.junit.BeforeClass;
033import org.junit.ClassRule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036
037import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
038
039@Category({ MediumTests.class, ClientTests.class })
040public class TestRequestTooBigException {
041
042  @ClassRule
043  public static final HBaseClassTestRule CLASS_RULE =
044    HBaseClassTestRule.forClass(TestRequestTooBigException.class);
045
046  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
047
048  private static final TableName NAME = TableName.valueOf("request_too_big");
049
050  private static final byte[] FAMILY = Bytes.toBytes("family");
051
052  private static Table TABLE;
053
054  @BeforeClass
055  public static void setUpBeforeClass() throws Exception {
056    TEST_UTIL.getConfiguration().setInt(MAX_REQUEST_SIZE, 10 * 1024);
057    TEST_UTIL.startMiniCluster(1);
058    TABLE = TEST_UTIL.createTable(NAME, FAMILY);
059    TEST_UTIL.waitTableAvailable(NAME);
060  }
061
062  @AfterClass
063  public static void tearDownAfterClass() throws Exception {
064    Closeables.close(TABLE, true);
065    TEST_UTIL.shutdownMiniCluster();
066  }
067
068  @Test
069  public void testHbasePutDeleteCell() throws Exception {
070    byte[] value = new byte[1024];
071    ThreadLocalRandom.current().nextBytes(value);
072    for (int m = 0; m < 100; m++) {
073      Put p = new Put(Bytes.toBytes("bigrow-" + m));
074      // max request is 10K, big request = 100 * 1K
075      for (int i = 0; i < 100; i++) {
076        p.addColumn(FAMILY, Bytes.toBytes("someQualifier" + i), value);
077      }
078      final Put finalPut = p;
079      assertThrows(RequestTooBigException.class, () -> TABLE.put(finalPut));
080    }
081  }
082}