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.Arrays;
021import java.util.Collection;
022import java.util.List;
023import org.apache.hadoop.hbase.Cell;
024import org.apache.hadoop.hbase.CellUtil;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseTestingUtility;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
029import org.apache.hadoop.hbase.client.Put;
030import org.apache.hadoop.hbase.client.Result;
031import org.apache.hadoop.hbase.client.ResultScanner;
032import org.apache.hadoop.hbase.client.Scan;
033import org.apache.hadoop.hbase.client.Table;
034import org.apache.hadoop.hbase.client.TableDescriptor;
035import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
036import org.apache.hadoop.hbase.regionserver.storefiletracker.StoreFileTrackerFactory;
037import org.apache.hadoop.hbase.testclassification.LargeTests;
038import org.apache.hadoop.hbase.util.Bytes;
039import org.junit.After;
040import org.junit.Assert;
041import org.junit.Before;
042import org.junit.ClassRule;
043import org.junit.Rule;
044import org.junit.Test;
045import org.junit.experimental.categories.Category;
046import org.junit.rules.TestName;
047import org.junit.runner.RunWith;
048import org.junit.runners.Parameterized;
049
050@RunWith(Parameterized.class)
051@Category(LargeTests.class)
052public class TestDefaultMobStoreFlusher {
053
054  @ClassRule
055  public static final HBaseClassTestRule CLASS_RULE =
056    HBaseClassTestRule.forClass(TestDefaultMobStoreFlusher.class);
057
058  private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
059  private final static byte[] row1 = Bytes.toBytes("row1");
060  private final static byte[] row2 = Bytes.toBytes("row2");
061  private final static byte[] family = Bytes.toBytes("family");
062  private final static byte[] qf1 = Bytes.toBytes("qf1");
063  private final static byte[] qf2 = Bytes.toBytes("qf2");
064  private final static byte[] value1 = Bytes.toBytes("value1");
065  private final static byte[] value2 = Bytes.toBytes("value2");
066
067  @Rule
068  public TestName name = new TestName();
069
070  protected Boolean useFileBasedSFT;
071
072  public TestDefaultMobStoreFlusher(Boolean useFileBasedSFT) {
073    this.useFileBasedSFT = useFileBasedSFT;
074  }
075
076  @Parameterized.Parameters
077  public static Collection<Boolean> data() {
078    Boolean[] data = { false, true };
079    return Arrays.asList(data);
080  }
081
082  @Before
083  public void setUpBefore() throws Exception {
084    if (useFileBasedSFT) {
085      TEST_UTIL.getConfiguration().set(StoreFileTrackerFactory.TRACKER_IMPL,
086        "org.apache.hadoop.hbase.regionserver.storefiletracker.FileBasedStoreFileTracker");
087    }
088    TEST_UTIL.startMiniCluster(1);
089  }
090
091  @After
092  public void tearDownAfter() throws Exception {
093    TEST_UTIL.shutdownMiniCluster();
094  }
095
096  @Test
097  public void testFlushNonMobFile() throws Exception {
098    final TableName tableName = TableName.valueOf(TestMobUtils.getTableName(name));
099    TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName)
100      .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(family).setMaxVersions(4).build())
101      .build();
102    testFlushFile(tableDescriptor);
103  }
104
105  @Test
106  public void testFlushMobFile() throws Exception {
107    final TableName tableName = TableName.valueOf(TestMobUtils.getTableName(name));
108    TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName)
109      .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(family).setMobEnabled(true)
110        .setMobThreshold(3L).setMaxVersions(4).build())
111      .build();
112    testFlushFile(tableDescriptor);
113  }
114
115  private void testFlushFile(TableDescriptor htd) throws Exception {
116    Table table = null;
117    try {
118      table = TEST_UTIL.createTable(htd, null);
119
120      // put data
121      Put put0 = new Put(row1);
122      put0.addColumn(family, qf1, 1, value1);
123      table.put(put0);
124
125      // put more data
126      Put put1 = new Put(row2);
127      put1.addColumn(family, qf2, 1, value2);
128      table.put(put1);
129
130      // flush
131      TEST_UTIL.flush(htd.getTableName());
132
133      // Scan
134      Scan scan = new Scan();
135      scan.addColumn(family, qf1);
136      scan.setMaxVersions(4);
137      ResultScanner scanner = table.getScanner(scan);
138
139      // Compare
140      int size = 0;
141      for (Result result : scanner) {
142        size++;
143        List<Cell> cells = result.getColumnCells(family, qf1);
144        // Verify the cell size
145        Assert.assertEquals(1, cells.size());
146        // Verify the value
147        Assert.assertArrayEquals(value1, CellUtil.cloneValue(cells.get(0)));
148      }
149      scanner.close();
150      Assert.assertEquals(1, size);
151    } finally {
152      table.close();
153    }
154  }
155}