View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.regionserver;
21  
22  import org.apache.hadoop.hbase.*;
23  import org.apache.hadoop.hbase.client.Get;
24  import org.apache.hadoop.hbase.client.Put;
25  import org.apache.hadoop.hbase.testclassification.MediumTests;
26  import org.apache.hadoop.hbase.util.Bytes;
27  import org.junit.AfterClass;
28  import org.junit.BeforeClass;
29  import org.junit.Test;
30  import org.junit.experimental.categories.Category;
31  
32  import java.io.IOException;
33  
34  /**
35   * Test case to check HRS throws {@link RowTooBigException}
36   * when row size exceeds configured limits.
37   */
38  @Category(MediumTests.class)
39  public class TestRowTooBig {
40    private final static HBaseTestingUtility HTU = HBaseTestingUtility.createLocalHTU();
41    private static final HTableDescriptor TEST_HTD =
42      new HTableDescriptor(TableName.valueOf(TestRowTooBig.class.getSimpleName()));
43  
44    @BeforeClass
45    public static void before() throws Exception {
46      HTU.startMiniCluster();
47      HTU.getConfiguration().setLong(HConstants.TABLE_MAX_ROWSIZE_KEY,
48        10 * 1024 * 1024L);
49    }
50  
51    @AfterClass
52    public static void after() throws Exception {
53      HTU.shutdownMiniCluster();
54    }
55  
56    /**
57     * Usecase:
58     *  - create a row with 5 large  cells (5 Mb each)
59     *  - flush memstore but don't compact storefiles.
60     *  - try to Get whole row.
61     *
62     * OOME happened before we actually get to reading results, but
63     * during seeking, as each StoreFile gets it's own scanner,
64     * and each scanner seeks after the first KV.
65     * @throws IOException
66     */
67    @Test(expected = RowTooBigException.class)
68    public void testScannersSeekOnFewLargeCells() throws IOException {
69      byte[] row1 = Bytes.toBytes("row1");
70      byte[] fam1 = Bytes.toBytes("fam1");
71  
72      HTableDescriptor htd = TEST_HTD;
73      HColumnDescriptor hcd = new HColumnDescriptor(fam1);
74      if (htd.hasFamily(hcd.getName())) {
75        htd.modifyFamily(hcd);
76      } else {
77        htd.addFamily(hcd);
78      }
79  
80      final HRegionInfo hri =
81        new HRegionInfo(htd.getTableName(), HConstants.EMPTY_END_ROW,
82          HConstants.EMPTY_END_ROW);
83      HRegion region = HTU.createLocalHRegion(hri,  htd);
84  
85      // Add 5 cells to memstore
86      for (int i = 0; i < 5 ; i++) {
87        Put put = new Put(row1);
88  
89        put.add(fam1, Bytes.toBytes("col_" + i ), new byte[5 * 1024 * 1024]);
90        region.put(put);
91        region.flushcache();
92      }
93  
94      Get get = new Get(row1);
95      region.get(get);
96    }
97  
98    /**
99     * Usecase:
100    *
101    *  - create a row with 1M cells, 10 bytes in each
102    *  - flush & run major compaction
103    *  - try to Get whole row.
104    *
105    *  OOME happened in StoreScanner.next(..).
106    *
107    * @throws IOException
108    */
109   @Test(expected = RowTooBigException.class)
110   public void testScanAcrossManySmallColumns() throws IOException {
111     byte[] row1 = Bytes.toBytes("row1");
112     byte[] fam1 = Bytes.toBytes("fam1");
113 
114     HTableDescriptor htd = TEST_HTD;
115     HColumnDescriptor hcd = new HColumnDescriptor(fam1);
116     if (htd.hasFamily(hcd.getName())) {
117       htd.modifyFamily(hcd);
118     } else {
119       htd.addFamily(hcd);
120     }
121 
122     final HRegionInfo hri =
123       new HRegionInfo(htd.getTableName(), HConstants.EMPTY_END_ROW,
124         HConstants.EMPTY_END_ROW);
125     HRegion region = HTU.createLocalHRegion(hri,  htd);
126 
127     // Add to memstore
128     for (int i = 0; i < 10; i++) {
129       Put put = new Put(row1);
130       for (int j = 0; j < 10 * 10000; j++) {
131         put.add(fam1, Bytes.toBytes("col_" + i + "_" + j), new byte[10]);
132       }
133       region.put(put);
134       region.flushcache();
135     }
136     region.compactStores(true);
137 
138     Get get = new Get(row1);
139     region.get(get);
140   }
141 }