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 protected Logger getLogger() {
51      return LOG;
52    }
53  
54    @Override
55    public void init(ActionContext context) throws IOException {
56      super.init(context);
57      this.admin = context.getHBaseIntegrationTestingUtility().getHBaseAdmin();
58    }
59  
60    @Override
61    public void perform() throws Exception {
62      HTableDescriptor tableDescriptor = admin.getTableDescriptor(tableName);
63      HColumnDescriptor[] columnDescriptors = tableDescriptor.getColumnFamilies();
64  
65      if (columnDescriptors.length <= (protectedColumns == null ? 1 : protectedColumns.size())) {
66        return;
67      }
68  
69      int index = random.nextInt(columnDescriptors.length);
70      while(protectedColumns != null &&
71            protectedColumns.contains(columnDescriptors[index].getNameAsString())) {
72        index = random.nextInt(columnDescriptors.length);
73      }
74      byte[] colDescName = columnDescriptors[index].getName();
75      getLogger().debug("Performing action: Removing " + Bytes.toString(colDescName)+ " from "
76          + tableName.getNameAsString());
77      tableDescriptor.removeFamily(colDescName);
78  
79      // Don't try the modify if we're stopping
80      if (context.isStopping()) {
81        return;
82      }
83      admin.modifyTable(tableName, tableDescriptor);
84    }
85  }