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