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.regionserver;
019
020import java.io.IOException;
021import org.apache.hadoop.fs.Path;
022import org.apache.hadoop.hbase.*;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.client.Get;
025import org.apache.hadoop.hbase.client.Put;
026import org.apache.hadoop.hbase.client.RowTooBigException;
027import org.apache.hadoop.hbase.testclassification.MediumTests;
028import org.apache.hadoop.hbase.testclassification.RegionServerTests;
029import org.apache.hadoop.hbase.util.Bytes;
030import org.junit.AfterClass;
031import org.junit.BeforeClass;
032import org.junit.ClassRule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035
036/**
037 * Test case to check HRS throws {@link org.apache.hadoop.hbase.client.RowTooBigException}
038 * when row size exceeds configured limits.
039 */
040@Category({RegionServerTests.class, MediumTests.class})
041public class TestRowTooBig {
042
043  @ClassRule
044  public static final HBaseClassTestRule CLASS_RULE =
045      HBaseClassTestRule.forClass(TestRowTooBig.class);
046
047  private final static HBaseTestingUtility HTU = HBaseTestingUtility.createLocalHTU();
048  private static Path rootRegionDir;
049  private static final HTableDescriptor TEST_HTD =
050    new HTableDescriptor(TableName.valueOf(TestRowTooBig.class.getSimpleName()));
051
052  @BeforeClass
053  public static void before() throws Exception {
054    HTU.startMiniCluster();
055    HTU.getConfiguration().setLong(HConstants.TABLE_MAX_ROWSIZE_KEY,
056      10 * 1024 * 1024L);
057    rootRegionDir = HTU.getDataTestDirOnTestFS("TestRowTooBig");
058  }
059
060  @AfterClass
061  public static void after() throws Exception {
062    HTU.shutdownMiniCluster();
063  }
064
065  /**
066   * Usecase:
067   *  - create a row with 5 large  cells (5 Mb each)
068   *  - flush memstore but don't compact storefiles.
069   *  - try to Get whole row.
070   *
071   * OOME happened before we actually get to reading results, but
072   * during seeking, as each StoreFile gets it's own scanner,
073   * and each scanner seeks after the first KV.
074   * @throws IOException
075   */
076  @Test(expected = RowTooBigException.class)
077  public void testScannersSeekOnFewLargeCells() throws IOException {
078    byte[] row1 = Bytes.toBytes("row1");
079    byte[] fam1 = Bytes.toBytes("fam1");
080
081    HTableDescriptor htd = TEST_HTD;
082    HColumnDescriptor hcd = new HColumnDescriptor(fam1);
083    if (htd.hasFamily(hcd.getName())) {
084      htd.modifyFamily(hcd);
085    } else {
086      htd.addFamily(hcd);
087    }
088
089    final HRegionInfo hri =
090      new HRegionInfo(htd.getTableName(), HConstants.EMPTY_END_ROW,
091        HConstants.EMPTY_END_ROW);
092    HRegion region =
093        HBaseTestingUtility.createRegionAndWAL(hri, rootRegionDir, HTU.getConfiguration(), htd);
094    try {
095      // Add 5 cells to memstore
096      for (int i = 0; i < 5 ; i++) {
097        Put put = new Put(row1);
098
099        byte[] value = new byte[5 * 1024 * 1024];
100        put.addColumn(fam1, Bytes.toBytes("col_" + i), value);
101        region.put(put);
102        region.flush(true);
103      }
104
105      Get get = new Get(row1);
106      region.get(get);
107    } finally {
108      HBaseTestingUtility.closeRegionAndWAL(region);
109    }
110  }
111
112  /**
113   * Usecase:
114   *
115   *  - create a row with 1M cells, 10 bytes in each
116   *  - flush & run major compaction
117   *  - try to Get whole row.
118   *
119   *  OOME happened in StoreScanner.next(..).
120   *
121   * @throws IOException
122   */
123  @Test(expected = RowTooBigException.class)
124  public void testScanAcrossManySmallColumns() throws IOException {
125    byte[] row1 = Bytes.toBytes("row1");
126    byte[] fam1 = Bytes.toBytes("fam1");
127
128    HTableDescriptor htd = TEST_HTD;
129    HColumnDescriptor hcd = new HColumnDescriptor(fam1);
130    if (htd.hasFamily(hcd.getName())) {
131      htd.modifyFamily(hcd);
132    } else {
133      htd.addFamily(hcd);
134    }
135
136    final HRegionInfo hri =
137      new HRegionInfo(htd.getTableName(), HConstants.EMPTY_END_ROW,
138        HConstants.EMPTY_END_ROW);
139    HRegion region =
140        HBaseTestingUtility.createRegionAndWAL(hri, rootRegionDir, HTU.getConfiguration(), htd);
141    try {
142      // Add to memstore
143      for (int i = 0; i < 10; i++) {
144        Put put = new Put(row1);
145        for (int j = 0; j < 10 * 10000; j++) {
146          byte[] value = new byte[10];
147          put.addColumn(fam1, Bytes.toBytes("col_" + i + "_" + j), value);
148        }
149        region.put(put);
150        region.flush(true);
151      }
152      region.compact(true);
153
154      Get get = new Get(row1);
155      region.get(get);
156    } finally {
157      HBaseTestingUtility.closeRegionAndWAL(region);
158    }
159  }
160}