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 protected Logger getLogger() {
51      return LOG;
52    }
53  
54    @Override
55    public void perform() throws Exception {
56      Random random = new Random();
57      HBaseTestingUtility util = context.getHBaseIntegrationTestingUtility();
58      Admin admin = util.getHBaseAdmin();
59  
60      getLogger().info("Performing action: Change bloom filter on all columns of table "
61          + tableName);
62      HTableDescriptor tableDescriptor = admin.getTableDescriptor(tableName);
63      HColumnDescriptor[] columnDescriptors = tableDescriptor.getColumnFamilies();
64  
65      if (columnDescriptors == null || columnDescriptors.length == 0) {
66        return;
67      }
68  
69      final BloomType[] bloomArray = BloomType.values();
70      final int bloomArraySize = bloomArray.length;
71  
72      for (HColumnDescriptor descriptor : columnDescriptors) {
73        int bloomFilterIndex = random.nextInt(bloomArraySize);
74        getLogger().debug("Performing action: About to set bloom filter type to "
75            + bloomArray[bloomFilterIndex] + " on column "
76            + descriptor.getNameAsString() + " of table " + tableName);
77        descriptor.setBloomFilterType(bloomArray[bloomFilterIndex]);
78        getLogger().debug("Performing action: Just set bloom filter type to "
79            + bloomArray[bloomFilterIndex] + " on column "
80            + descriptor.getNameAsString() + " of table " + tableName);
81      }
82  
83      // Don't try the modify if we're stopping
84      if (context.isStopping()) {
85        return;
86      }
87      admin.modifyTable(tableName, tableDescriptor);
88    }
89  }