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.util.Random;
22  
23  import org.apache.hadoop.hbase.HBaseTestingUtility;
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.regionserver.BloomType;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  /**
33   * Action that tries to adjust the bloom filter setting on all the columns of a
34   * table
35   */
36  public class ChangeBloomFilterAction extends Action {
37    private final long sleepTime;
38    private final TableName tableName;
39    private static final Logger LOG = LoggerFactory.getLogger(ChangeBloomFilterAction.class);
40  
41    public ChangeBloomFilterAction(TableName tableName) {
42      this(-1, tableName);
43    }
44  
45    public ChangeBloomFilterAction(int sleepTime, TableName tableName) {
46      this.sleepTime = sleepTime;
47      this.tableName = tableName;
48    }
49  
50    @Override
51    public void perform() throws Exception {
52      Random random = new Random();
53      HBaseTestingUtility util = context.getHBaseIntegrationTestingUtility();
54      Admin admin = util.getHBaseAdmin();
55  
56      LOG.info("Performing action: Change bloom filter on all columns of table "
57          + tableName);
58      HTableDescriptor tableDescriptor = admin.getTableDescriptor(tableName);
59      HColumnDescriptor[] columnDescriptors = tableDescriptor.getColumnFamilies();
60  
61      if (columnDescriptors == null || columnDescriptors.length == 0) {
62        return;
63      }
64  
65      final BloomType[] bloomArray = BloomType.values();
66      final int bloomArraySize = bloomArray.length;
67  
68      for (HColumnDescriptor descriptor : columnDescriptors) {
69        int bloomFilterIndex = random.nextInt(bloomArraySize);
70        LOG.debug("Performing action: About to set bloom filter type to "
71            + bloomArray[bloomFilterIndex] + " on column "
72            + descriptor.getNameAsString() + " of table " + tableName);
73        descriptor.setBloomFilterType(bloomArray[bloomFilterIndex]);
74        LOG.debug("Performing action: Just set bloom filter type to "
75            + bloomArray[bloomFilterIndex] + " on column "
76            + descriptor.getNameAsString() + " of table " + tableName);
77      }
78  
79      // Don't try the modify if we're stopping
80      if (context.isStopping()) {
81        return;
82      }
83      admin.modifyTable(tableName, tableDescriptor);
84    }
85  }