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.client.HBaseAdmin;
27  import org.apache.hadoop.hbase.regionserver.BloomType;
28  import org.apache.hadoop.hbase.util.Bytes;
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 byte[] tableNameBytes;
36    private final long sleepTime;
37    private final String tableName;
38  
39    public ChangeBloomFilterAction(String tableName) {
40      this(-1, tableName);
41    }
42  
43    public ChangeBloomFilterAction(int sleepTime, String tableName) {
44      this.tableNameBytes = Bytes.toBytes(tableName);
45      this.sleepTime = sleepTime;
46      this.tableName = tableName;
47    }
48  
49    @Override
50    public void perform() throws Exception {
51      Random random = new Random();
52      HBaseTestingUtility util = context.getHBaseIntegrationTestingUtility();
53      HBaseAdmin admin = util.getHBaseAdmin();
54  
55      LOG.info("Performing action: Change bloom filter on all columns of table "
56          + tableName);
57      HTableDescriptor tableDescriptor = admin.getTableDescriptor(Bytes
58          .toBytes(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      admin.modifyTable(tableName, tableDescriptor);
80    }
81  }