diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegion.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegion.java index 8b0db01c178..58a39276c1e 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegion.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegion.java @@ -4665,6 +4665,7 @@ public class HRegion implements HeapSize { // , Writable{ long size = 0; long txid = 0; + checkReadOnly(); // Lock row startRegionOperation(); this.writeRequestsCount.increment(); @@ -4829,6 +4830,7 @@ public class HRegion implements HeapSize { // , Writable{ long size = 0; long txid = 0; + checkReadOnly(); // Lock row startRegionOperation(); this.writeRequestsCount.increment(); diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestHRegion.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestHRegion.java index c64164abeb3..49a72c45d8c 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestHRegion.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestHRegion.java @@ -519,6 +519,41 @@ public class TestHRegion extends HBaseTestCase { } } + public void testAppendWithReadOnlyTable() throws Exception { + byte[] TABLE = Bytes.toBytes("readOnlyTable"); + this.region = initHRegion(TABLE, getName(), conf, true, Bytes.toBytes("somefamily")); + boolean exceptionCaught = false; + Append append = new Append(Bytes.toBytes("somerow")); + append.add(Bytes.toBytes("somefamily"), Bytes.toBytes("somequalifier"), + Bytes.toBytes("somevalue")); + try { + region.append(append, false); + } catch (IOException e) { + exceptionCaught = true; + } finally { + HRegion.closeHRegion(this.region); + this.region = null; + } + assertTrue(exceptionCaught == true); + } + + public void testIncrWithReadOnlyTable() throws Exception { + byte[] TABLE = Bytes.toBytes("readOnlyTable"); + this.region = initHRegion(TABLE, getName(), conf, true, Bytes.toBytes("somefamily")); + boolean exceptionCaught = false; + Increment inc = new Increment(Bytes.toBytes("somerow")); + inc.addColumn(Bytes.toBytes("somefamily"), Bytes.toBytes("somequalifier"), 1L); + try { + region.increment(inc, false); + } catch (IOException e) { + exceptionCaught = true; + } finally { + HRegion.closeHRegion(this.region); + this.region = null; + } + assertTrue(exceptionCaught == true); + } + private void deleteColumns(HRegion r, String value, String keyPrefix) throws IOException { InternalScanner scanner = buildScanner(keyPrefix, value, r); @@ -3119,7 +3154,7 @@ public class TestHRegion extends HBaseTestCase { byte[] tableName = Bytes.toBytes(method); byte[] family = Bytes.toBytes("family"); this.region = initHRegion(tableName, Bytes.toBytes("x"), Bytes.toBytes("z"), method, - conf, family); + conf, false, family); try { byte[] rowNotServed = Bytes.toBytes("a"); Get g = new Get(rowNotServed); @@ -3841,7 +3876,22 @@ public class TestHRegion extends HBaseTestCase { public static HRegion initHRegion (byte [] tableName, String callingMethod, Configuration conf, byte [] ... families) throws IOException{ - return initHRegion(tableName, null, null, callingMethod, conf, families); + return initHRegion(tableName, null, null, callingMethod, conf, false, families); + } + + /** + * @param tableName + * @param callingMethod + * @param conf + * @param isReadOnly + * @param families + * @throws IOException + * @return A region on which you must call {@link HRegion#closeHRegion(HRegion)} when done. + */ + public static HRegion initHRegion (byte [] tableName, String callingMethod, + Configuration conf, boolean isReadOnly, byte [] ... families) + throws IOException{ + return initHRegion(tableName, null, null, callingMethod, conf, isReadOnly, families); } /** @@ -3850,14 +3900,16 @@ public class TestHRegion extends HBaseTestCase { * @param stopKey * @param callingMethod * @param conf + * @param isReadOnly * @param families * @throws IOException * @return A region on which you must call {@link HRegion#closeHRegion(HRegion)} when done. */ private static HRegion initHRegion(byte[] tableName, byte[] startKey, byte[] stopKey, - String callingMethod, Configuration conf, byte[]... families) + String callingMethod, Configuration conf, boolean isReadOnly, byte[]... families) throws IOException { HTableDescriptor htd = new HTableDescriptor(tableName); + htd.setReadOnly(isReadOnly); for(byte [] family : families) { htd.addFamily(new HColumnDescriptor(family)); }