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.io.encoding;
019
020import java.io.IOException;
021import java.util.Arrays;
022import java.util.Collection;
023import java.util.List;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.HRegionLocation;
026import org.apache.hadoop.hbase.client.Admin;
027import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
028import org.apache.hadoop.hbase.client.RegionLocator;
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.io.compress.Compression;
034import org.apache.hadoop.hbase.io.hfile.CacheConfig;
035import org.apache.hadoop.hbase.regionserver.HRegionServer;
036import org.apache.hadoop.hbase.testclassification.IOTests;
037import org.apache.hadoop.hbase.testclassification.MediumTests;
038import org.apache.hadoop.hbase.util.TestMiniClusterLoadSequential;
039import org.apache.hadoop.hbase.util.Threads;
040import org.junit.ClassRule;
041import org.junit.Test;
042import org.junit.experimental.categories.Category;
043import org.junit.runners.Parameterized.Parameters;
044
045/**
046 * Uses the load tester
047 */
048@Category({ IOTests.class, MediumTests.class })
049public class TestLoadAndSwitchEncodeOnDisk extends TestMiniClusterLoadSequential {
050
051  @ClassRule
052  public static final HBaseClassTestRule CLASS_RULE =
053      HBaseClassTestRule.forClass(TestLoadAndSwitchEncodeOnDisk.class);
054
055  /** We do not alternate the multi-put flag in this test. */
056  private static final boolean USE_MULTI_PUT = true;
057
058  /** Un-parameterize the test */
059  @Parameters
060  public static Collection<Object[]> parameters() {
061    return Arrays.asList(new Object[][]{ new Object[0] });
062  }
063
064  public TestLoadAndSwitchEncodeOnDisk() {
065    super(USE_MULTI_PUT, DataBlockEncoding.PREFIX);
066    conf.setBoolean(CacheConfig.CACHE_BLOCKS_ON_WRITE_KEY, true);
067  }
068
069  @Override
070  protected int numKeys() {
071    return 3000;
072  }
073
074  @Override
075  @Test
076  public void loadTest() throws Exception {
077    Admin admin = TEST_UTIL.getAdmin();
078
079    compression = Compression.Algorithm.GZ; // used for table setup
080    super.loadTest();
081
082    ColumnFamilyDescriptor hcd = getColumnDesc(admin);
083    System.err.println("\nDisabling encode-on-disk. Old column descriptor: " + hcd + "\n");
084    Table t = TEST_UTIL.getConnection().getTable(TABLE);
085    assertAllOnLine(t);
086
087    admin.disableTable(TABLE);
088    admin.modifyColumnFamily(TABLE, hcd);
089
090    System.err.println("\nRe-enabling table\n");
091    admin.enableTable(TABLE);
092
093    System.err.println("\nNew column descriptor: " +
094        getColumnDesc(admin) + "\n");
095
096    // The table may not have all regions on line yet.  Assert online before
097    // moving to major compact.
098    assertAllOnLine(t);
099
100    System.err.println("\nCompacting the table\n");
101    admin.majorCompact(TABLE);
102    // Wait until compaction completes
103    Threads.sleepWithoutInterrupt(5000);
104    HRegionServer rs = TEST_UTIL.getMiniHBaseCluster().getRegionServer(0);
105    while (rs.getCompactSplitThread().getCompactionQueueSize() > 0) {
106      Threads.sleep(50);
107    }
108
109    System.err.println("\nDone with the test, shutting down the cluster\n");
110  }
111
112  private void assertAllOnLine(final Table t) throws IOException {
113    List<HRegionLocation> regions;
114    try(RegionLocator rl = TEST_UTIL.getConnection().getRegionLocator(t.getName())) {
115      regions = rl.getAllRegionLocations();
116    }
117    for (HRegionLocation e: regions) {
118      byte [] startkey = e.getRegion().getStartKey();
119      Scan s = new Scan().withStartRow(startkey);
120      ResultScanner scanner = t.getScanner(s);
121      Result r = scanner.next();
122      org.junit.Assert.assertTrue(r != null && r.size() > 0);
123      scanner.close();
124    }
125  }
126}