001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.hbase.chaos.actions;
020
021import java.io.IOException;
022import java.util.Random;
023import java.util.Set;
024
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.client.Admin;
027import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
028import org.apache.hadoop.hbase.client.TableDescriptor;
029import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
030import org.apache.hadoop.hbase.util.Bytes;
031
032/**
033 * Action that removes a column family.
034 */
035public class RemoveColumnAction extends Action {
036  private final TableName tableName;
037  private final Set<String> protectedColumns;
038  private Admin admin;
039  private Random random;
040
041  public RemoveColumnAction(TableName tableName, Set<String> protectedColumns) {
042    this.tableName = tableName;
043    this.protectedColumns = protectedColumns;
044    random = new Random();
045  }
046
047  @Override
048  public void init(ActionContext context) throws IOException {
049    super.init(context);
050    this.admin = context.getHBaseIntegrationTestingUtility().getAdmin();
051  }
052
053  @Override
054  public void perform() throws Exception {
055    TableDescriptor tableDescriptor = admin.getDescriptor(tableName);
056    ColumnFamilyDescriptor[] columnDescriptors = tableDescriptor.getColumnFamilies();
057
058    if (columnDescriptors.length <= (protectedColumns == null ? 1 : protectedColumns.size())) {
059      return;
060    }
061
062    int index = random.nextInt(columnDescriptors.length);
063    while(protectedColumns != null &&
064          protectedColumns.contains(columnDescriptors[index].getNameAsString())) {
065      index = random.nextInt(columnDescriptors.length);
066    }
067    byte[] colDescName = columnDescriptors[index].getName();
068    LOG.debug("Performing action: Removing " + Bytes.toString(colDescName)+ " from "
069        + tableName.getNameAsString());
070
071    TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableDescriptor);
072    builder.removeColumnFamily(colDescName);
073
074    // Don't try the modify if we're stopping
075    if (context.isStopping()) {
076      return;
077    }
078    admin.modifyTable(builder.build());
079  }
080}