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