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;
022
023import org.apache.commons.lang3.RandomStringUtils;
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.ColumnFamilyDescriptorBuilder;
028import org.apache.hadoop.hbase.client.TableDescriptor;
029import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032
033/**
034 * Action the adds a column family to a table.
035 */
036public class AddColumnAction extends Action {
037  private final TableName tableName;
038  private Admin admin;
039  private static final Logger LOG = LoggerFactory.getLogger(AddColumnAction.class);
040
041  public AddColumnAction(TableName tableName) {
042    this.tableName = tableName;
043  }
044
045  @Override protected Logger getLogger() {
046    return LOG;
047  }
048
049  @Override
050  public void init(ActionContext context) throws IOException {
051    super.init(context);
052    this.admin = context.getHBaseIntegrationTestingUtility().getAdmin();
053  }
054
055  @Override
056  public void perform() throws Exception {
057    TableDescriptor tableDescriptor = admin.getDescriptor(tableName);
058    ColumnFamilyDescriptor columnDescriptor = null;
059
060    while (columnDescriptor == null
061        || tableDescriptor.getColumnFamily(columnDescriptor.getName()) != null) {
062      columnDescriptor = ColumnFamilyDescriptorBuilder.of(RandomStringUtils.randomAlphabetic(5));
063    }
064
065    // Don't try the modify if we're stopping
066    if (context.isStopping()) {
067      return;
068    }
069
070    getLogger().debug("Performing action: Adding " + columnDescriptor + " to " + tableName);
071
072    TableDescriptor modifiedTable = TableDescriptorBuilder.newBuilder(tableDescriptor)
073        .setColumnFamily(columnDescriptor).build();
074    admin.modifyTable(modifiedTable);
075  }
076}