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.chaos.actions;
019
020import java.io.IOException;
021import java.util.Random;
022import java.util.concurrent.ThreadLocalRandom;
023import org.apache.hadoop.hbase.TableName;
024import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
025import org.apache.hadoop.io.compress.Compressor;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029/**
030 * Action that changes the compression algorithm on a column family from a list of tables.
031 */
032public class ChangeCompressionAction extends Action {
033  private final TableName tableName;
034  private static final Logger LOG = LoggerFactory.getLogger(ChangeCompressionAction.class);
035
036  public ChangeCompressionAction(TableName tableName) {
037    this.tableName = tableName;
038  }
039
040  @Override
041  protected Logger getLogger() {
042    return LOG;
043  }
044
045  @Override
046  public void perform() throws IOException {
047    // Possible compression algorithms. If an algorithm is not supported,
048    // modifyTable will fail, so there is no harm.
049    Algorithm[] possibleAlgos = Algorithm.values();
050    // Since not every compression algorithm is supported,
051    // let's use the same algorithm for all column families.
052    Random rand = ThreadLocalRandom.current();
053    // If an unsupported compression algorithm is chosen, pick a different one.
054    // This is to work around the issue that modifyTable() does not throw remote
055    // exception.
056    Algorithm algo;
057    do {
058      algo = possibleAlgos[rand.nextInt(possibleAlgos.length)];
059
060      try {
061        Compressor c = algo.getCompressor();
062
063        // call returnCompressor() to release the Compressor
064        algo.returnCompressor(c);
065        break;
066      } catch (Throwable t) {
067        getLogger().info("Performing action: Changing compression algorithms to " + algo
068          + " is not supported, pick another one");
069      }
070    } while (true);
071
072    final Algorithm chosenAlgo = algo; // for use in lambda
073    getLogger().debug("Performing action: Changing compression algorithms on "
074      + tableName.getNameAsString() + " to " + chosenAlgo);
075    modifyAllTableColumns(tableName, columnFamilyDescriptorBuilder -> {
076      if (rand.nextBoolean()) {
077        columnFamilyDescriptorBuilder.setCompactionCompressionType(chosenAlgo);
078      } else {
079        columnFamilyDescriptorBuilder.setCompressionType(chosenAlgo);
080      }
081    });
082  }
083}