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; 031import org.slf4j.Logger; 032import org.slf4j.LoggerFactory; 033 034/** 035 * Action that removes a column family. 036 */ 037public class RemoveColumnAction extends Action { 038 private static final Logger LOG = 039 LoggerFactory.getLogger(RemoveColumnAction.class); 040 private final TableName tableName; 041 private final Set<String> protectedColumns; 042 private Admin admin; 043 private Random random; 044 045 public RemoveColumnAction(TableName tableName, Set<String> protectedColumns) { 046 this.tableName = tableName; 047 this.protectedColumns = protectedColumns; 048 random = new Random(); 049 } 050 051 @Override 052 public void init(ActionContext context) throws IOException { 053 super.init(context); 054 this.admin = context.getHBaseIntegrationTestingUtility().getAdmin(); 055 } 056 057 @Override 058 public void perform() throws Exception { 059 TableDescriptor tableDescriptor = admin.getDescriptor(tableName); 060 ColumnFamilyDescriptor[] columnDescriptors = tableDescriptor.getColumnFamilies(); 061 062 if (columnDescriptors.length <= (protectedColumns == null ? 1 : protectedColumns.size())) { 063 return; 064 } 065 066 int index = random.nextInt(columnDescriptors.length); 067 while(protectedColumns != null && 068 protectedColumns.contains(columnDescriptors[index].getNameAsString())) { 069 index = random.nextInt(columnDescriptors.length); 070 } 071 byte[] colDescName = columnDescriptors[index].getName(); 072 LOG.debug("Performing action: Removing " + Bytes.toString(colDescName)+ " from " 073 + tableName.getNameAsString()); 074 075 TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableDescriptor); 076 builder.removeColumnFamily(colDescName); 077 078 // Don't try the modify if we're stopping 079 if (context.isStopping()) { 080 return; 081 } 082 admin.modifyTable(builder.build()); 083 } 084}