View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.chaos.actions;
20  
21  import java.io.IOException;
22  import java.util.Random;
23  
24  import org.apache.hadoop.hbase.HColumnDescriptor;
25  import org.apache.hadoop.hbase.HTableDescriptor;
26  import org.apache.hadoop.hbase.TableName;
27  import org.apache.hadoop.hbase.client.Admin;
28  import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
29  import org.apache.hadoop.io.compress.Compressor;
30  
31  /**
32   * Action that changes the compression algorithm on a column family from a list of tables.
33   */
34  public class ChangeCompressionAction extends Action {
35    private final TableName tableName;
36  
37    private Admin admin;
38    private Random random;
39  
40    public ChangeCompressionAction(TableName tableName) {
41      this.tableName = tableName;
42      this.random = new Random();
43    }
44  
45    @Override
46    public void init(ActionContext context) throws IOException {
47      super.init(context);
48      this.admin = context.getHBaseIntegrationTestingUtility().getHBaseAdmin();
49    }
50  
51    @Override
52    public void perform() throws Exception {
53      HTableDescriptor tableDescriptor = admin.getTableDescriptor(tableName);
54      HColumnDescriptor[] columnDescriptors = tableDescriptor.getColumnFamilies();
55  
56      if (columnDescriptors == null || columnDescriptors.length == 0) {
57        return;
58      }
59  
60      // Possible compression algorithms. If an algorithm is not supported,
61      // modifyTable will fail, so there is no harm.
62      Algorithm[] possibleAlgos = Algorithm.values();
63  
64      // Since not every compression algorithm is supported,
65      // let's use the same algorithm for all column families.
66  
67      // If an unsupported compression algorithm is chosen, pick a different one.
68      // This is to work around the issue that modifyTable() does not throw remote
69      // exception.
70      Algorithm algo;
71      do {
72        algo = possibleAlgos[random.nextInt(possibleAlgos.length)];
73  
74        try {
75          Compressor c = algo.getCompressor();
76  
77          // call returnCompressor() to release the Compressor
78          algo.returnCompressor(c);
79          break;
80        } catch (Throwable t) {
81          LOG.info("Performing action: Changing compression algorithms to " + algo +
82                  " is not supported, pick another one");
83        }
84      } while (true);
85  
86      LOG.debug("Performing action: Changing compression algorithms on "
87        + tableName.getNameAsString() + " to " + algo);
88      for (HColumnDescriptor descriptor : columnDescriptors) {
89        if (random.nextBoolean()) {
90          descriptor.setCompactionCompressionType(algo);
91        } else {
92          descriptor.setCompressionType(algo);
93        }
94      }
95  
96      // Don't try the modify if we're stopping
97      if (context.isStopping()) {
98        return;
99      }
100 
101     admin.modifyTable(tableName, tableDescriptor);
102   }
103 }