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