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  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  /**
35   * Action that changes the compression algorithm on a column family from a list of tables.
36   */
37  public class ChangeCompressionAction extends Action {
38    private final TableName tableName;
39  
40    private Admin admin;
41    private Random random;
42    private static final Logger LOG = LoggerFactory.getLogger(ChangeCompressionAction.class);
43  
44    public ChangeCompressionAction(TableName tableName) {
45      this.tableName = tableName;
46      this.random = new Random();
47    }
48  
49    @Override protected Logger getLogger() {
50      return LOG;
51    }
52  
53    @Override
54    public void init(ActionContext context) throws IOException {
55      super.init(context);
56      this.admin = context.getHBaseIntegrationTestingUtility().getHBaseAdmin();
57    }
58  
59    @Override
60    public void perform() throws Exception {
61      HTableDescriptor tableDescriptor = admin.getTableDescriptor(tableName);
62      HColumnDescriptor[] columnDescriptors = tableDescriptor.getColumnFamilies();
63  
64      if (columnDescriptors == null || columnDescriptors.length == 0) {
65        return;
66      }
67  
68      // Possible compression algorithms. If an algorithm is not supported,
69      // modifyTable will fail, so there is no harm.
70      Algorithm[] possibleAlgos = Algorithm.values();
71  
72      // Since not every compression algorithm is supported,
73      // let's use the same algorithm for all column families.
74  
75      // If an unsupported compression algorithm is chosen, pick a different one.
76      // This is to work around the issue that modifyTable() does not throw remote
77      // exception.
78      Algorithm algo;
79      do {
80        algo = possibleAlgos[random.nextInt(possibleAlgos.length)];
81  
82        try {
83          Compressor c = algo.getCompressor();
84  
85          // call returnCompressor() to release the Compressor
86          algo.returnCompressor(c);
87          break;
88        } catch (Throwable t) {
89          getLogger().info("Performing action: Changing compression algorithms to " + algo +
90                  " is not supported, pick another one");
91        }
92      } while (true);
93  
94      getLogger().debug("Performing action: Changing compression algorithms on "
95        + tableName.getNameAsString() + " to " + algo);
96      for (HColumnDescriptor descriptor : columnDescriptors) {
97        if (random.nextBoolean()) {
98          descriptor.setCompactionCompressionType(algo);
99        } else {
100         descriptor.setCompressionType(algo);
101       }
102     }
103 
104     // Don't try the modify if we're stopping
105     if (context.isStopping()) {
106       return;
107     }
108 
109     admin.modifyTable(tableName, tableDescriptor);
110   }
111 }