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.mob;
019
020import java.util.List;
021import org.apache.hadoop.hbase.Cell;
022import org.apache.hadoop.hbase.CellUtil;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HBaseTestingUtility;
025import org.apache.hadoop.hbase.HColumnDescriptor;
026import org.apache.hadoop.hbase.HTableDescriptor;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.client.*;
029import org.apache.hadoop.hbase.testclassification.LargeTests;
030import org.apache.hadoop.hbase.util.Bytes;
031import org.junit.AfterClass;
032import org.junit.Assert;
033import org.junit.BeforeClass;
034import org.junit.ClassRule;
035import org.junit.Rule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038import org.junit.rules.TestName;
039
040@Category(LargeTests.class)
041public class TestDefaultMobStoreFlusher {
042
043  @ClassRule
044  public static final HBaseClassTestRule CLASS_RULE =
045      HBaseClassTestRule.forClass(TestDefaultMobStoreFlusher.class);
046
047 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
048 private final static byte [] row1 = Bytes.toBytes("row1");
049 private final static byte [] row2 = Bytes.toBytes("row2");
050 private final static byte [] family = Bytes.toBytes("family");
051 private final static byte [] qf1 = Bytes.toBytes("qf1");
052 private final static byte [] qf2 = Bytes.toBytes("qf2");
053 private final static byte [] value1 = Bytes.toBytes("value1");
054 private final static byte [] value2 = Bytes.toBytes("value2");
055
056 @Rule
057 public TestName name = new TestName();
058
059 @BeforeClass
060 public static void setUpBeforeClass() throws Exception {
061   TEST_UTIL.startMiniCluster(1);
062 }
063
064 @AfterClass
065 public static void tearDownAfterClass() throws Exception {
066   TEST_UTIL.shutdownMiniCluster();
067 }
068
069 @Test
070 public void testFlushNonMobFile() throws Exception {
071   final TableName tableName = TableName.valueOf(name.getMethodName());
072   HTableDescriptor desc = new HTableDescriptor(tableName);
073   HColumnDescriptor hcd = new HColumnDescriptor(family);
074   hcd.setMaxVersions(4);
075   desc.addFamily(hcd);
076
077   testFlushFile(desc);
078 }
079
080 @Test
081 public void testFlushMobFile() throws Exception {
082   final TableName tableName = TableName.valueOf(name.getMethodName());
083   HTableDescriptor desc = new HTableDescriptor(tableName);
084   HColumnDescriptor hcd = new HColumnDescriptor(family);
085   hcd.setMobEnabled(true);
086   hcd.setMobThreshold(3L);
087   hcd.setMaxVersions(4);
088   desc.addFamily(hcd);
089
090   testFlushFile(desc);
091 }
092
093 private void testFlushFile(HTableDescriptor htd) throws Exception {
094    Table table = null;
095    try {
096      table = TEST_UTIL.createTable(htd, null);
097
098      //put data
099      Put put0 = new Put(row1);
100      put0.addColumn(family, qf1, 1, value1);
101      table.put(put0);
102
103      //put more data
104      Put put1 = new Put(row2);
105      put1.addColumn(family, qf2, 1, value2);
106      table.put(put1);
107
108      //flush
109      TEST_UTIL.flush(htd.getTableName());
110
111      //Scan
112      Scan scan = new Scan();
113      scan.addColumn(family, qf1);
114      scan.setMaxVersions(4);
115      ResultScanner scanner = table.getScanner(scan);
116
117      //Compare
118      int size = 0;
119      for (Result result: scanner) {
120        size++;
121        List<Cell> cells = result.getColumnCells(family, qf1);
122        // Verify the cell size
123        Assert.assertEquals(1, cells.size());
124        // Verify the value
125        Assert.assertArrayEquals(value1, CellUtil.cloneValue(cells.get(0)));
126      }
127      scanner.close();
128      Assert.assertEquals(1, size);
129    } finally {
130      table.close();
131    }
132  }
133}