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: " + getColumnDesc(admin) + "\n");
094
095    // The table may not have all regions on line yet. Assert online before
096    // moving to major compact.
097    assertAllOnLine(t);
098
099    System.err.println("\nCompacting the table\n");
100    admin.majorCompact(TABLE);
101    // Wait until compaction completes
102    Threads.sleepWithoutInterrupt(5000);
103    HRegionServer rs = TEST_UTIL.getMiniHBaseCluster().getRegionServer(0);
104    while (rs.getCompactSplitThread().getCompactionQueueSize() > 0) {
105      Threads.sleep(50);
106    }
107
108    System.err.println("\nDone with the test, shutting down the cluster\n");
109  }
110
111  private void assertAllOnLine(final Table t) throws IOException {
112    List<HRegionLocation> regions;
113    try (RegionLocator rl = TEST_UTIL.getConnection().getRegionLocator(t.getName())) {
114      regions = rl.getAllRegionLocations();
115    }
116    for (HRegionLocation e : regions) {
117      byte[] startkey = e.getRegion().getStartKey();
118      Scan s = new Scan().withStartRow(startkey);
119      ResultScanner scanner = t.getScanner(s);
120      Result r = scanner.next();
121      org.junit.Assert.assertTrue(r != null && r.size() > 0);
122      scanner.close();
123    }
124  }
125}