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 */ 018package org.apache.hadoop.hbase.chaos.actions; 019 020import java.io.IOException; 021import java.util.Random; 022import java.util.Set; 023import java.util.concurrent.ThreadLocalRandom; 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 = LoggerFactory.getLogger(RemoveColumnAction.class); 038 private final TableName tableName; 039 private final Set<String> protectedColumns; 040 private Admin admin; 041 042 public RemoveColumnAction(TableName tableName, Set<String> protectedColumns) { 043 this.tableName = tableName; 044 this.protectedColumns = protectedColumns; 045 } 046 047 @Override 048 protected Logger getLogger() { 049 return LOG; 050 } 051 052 @Override 053 public void init(ActionContext context) throws IOException { 054 super.init(context); 055 this.admin = context.getHBaseIntegrationTestingUtility().getAdmin(); 056 } 057 058 @Override 059 public void perform() throws Exception { 060 TableDescriptor tableDescriptor = admin.getDescriptor(tableName); 061 ColumnFamilyDescriptor[] columnDescriptors = tableDescriptor.getColumnFamilies(); 062 Random rand = ThreadLocalRandom.current(); 063 064 if (columnDescriptors.length <= (protectedColumns == null ? 1 : protectedColumns.size())) { 065 return; 066 } 067 int index = rand.nextInt(columnDescriptors.length); 068 while ( 069 protectedColumns != null 070 && protectedColumns.contains(columnDescriptors[index].getNameAsString()) 071 ) { 072 index = rand.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}