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