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.TableName;
28  import org.apache.hadoop.hbase.client.Admin;
29  import org.apache.hadoop.hbase.util.Bytes;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  /**
34   * Action that removes a column family.
35   */
36  public class RemoveColumnAction extends Action {
37    private static final Logger LOG =
38        LoggerFactory.getLogger(RemoveColumnAction.class);
39    private final TableName tableName;
40    private final Set<String> protectedColumns;
41    private Admin admin;
42    private Random random;
43  
44    public RemoveColumnAction(TableName tableName, Set<String> protectedColumns) {
45      this.tableName = tableName;
46      this.protectedColumns = protectedColumns;
47      random = new Random();
48    }
49  
50    @Override
51    public void init(ActionContext context) throws IOException {
52      super.init(context);
53      this.admin = context.getHBaseIntegrationTestingUtility().getHBaseAdmin();
54    }
55  
56    @Override
57    public void perform() throws Exception {
58      HTableDescriptor tableDescriptor = admin.getTableDescriptor(tableName);
59      HColumnDescriptor[] columnDescriptors = tableDescriptor.getColumnFamilies();
60  
61      if (columnDescriptors.length <= (protectedColumns == null ? 1 : protectedColumns.size())) {
62        return;
63      }
64  
65      int index = random.nextInt(columnDescriptors.length);
66      while(protectedColumns != null &&
67            protectedColumns.contains(columnDescriptors[index].getNameAsString())) {
68        index = random.nextInt(columnDescriptors.length);
69      }
70      byte[] colDescName = columnDescriptors[index].getName();
71      LOG.debug("Performing action: Removing " + Bytes.toString(colDescName)+ " from "
72          + tableName.getNameAsString());
73      tableDescriptor.removeFamily(colDescName);
74  
75      // Don't try the modify if we're stopping
76      if (context.isStopping()) {
77        return;
78      }
79      admin.modifyTable(tableName, tableDescriptor);
80    }
81  }