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