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