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  import java.util.Set;
24  
25  import org.apache.hadoop.hbase.HColumnDescriptor;
26  import org.apache.hadoop.hbase.HTableDescriptor;
27  import org.apache.hadoop.hbase.client.HBaseAdmin;
28  import org.apache.hadoop.hbase.util.Bytes;
29  
30  /**
31   * Action that removes a column family.
32   */
33  public class RemoveColumnAction extends Action {
34    private final byte[] tableName;
35    private final Set<String> protectedColumns;
36    private HBaseAdmin admin;
37    private Random random;
38  
39    public RemoveColumnAction(String tableName, Set<String> protectedColumns) {
40      this.tableName = Bytes.toBytes(tableName);
41      this.protectedColumns = protectedColumns;
42      random = new Random();
43    }
44  
45    @Override
46    public void init(ActionContext context) throws IOException {
47      super.init(context);
48      this.admin = context.getHaseIntegrationTestingUtility().getHBaseAdmin();
49    }
50  
51    @Override
52    public void perform() throws Exception {
53      HTableDescriptor tableDescriptor = admin.getTableDescriptor(tableName);
54      HColumnDescriptor[] columnDescriptors = tableDescriptor.getColumnFamilies();
55  
56      if (columnDescriptors.length <= 1) {
57        return;
58      }
59  
60      int index = random.nextInt(columnDescriptors.length);
61      while(protectedColumns.contains(columnDescriptors[index].getNameAsString())) {
62        index = random.nextInt(columnDescriptors.length);
63      }
64  
65      tableDescriptor.removeFamily(columnDescriptors[index].getName());
66  
67      admin.modifyTable(tableName, tableDescriptor);
68    }
69  }