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