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