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
046  public void init(ActionContext context) throws IOException {
047    super.init(context);
048    this.admin = context.getHBaseIntegrationTestingUtility().getAdmin();
049  }
050
051  @Override
052  public void perform() throws Exception {
053    TableDescriptor tableDescriptor = admin.getDescriptor(tableName);
054    ColumnFamilyDescriptor columnDescriptor = null;
055
056    while (columnDescriptor == null
057        || tableDescriptor.getColumnFamily(columnDescriptor.getName()) != null) {
058      columnDescriptor = ColumnFamilyDescriptorBuilder.of(RandomStringUtils.randomAlphabetic(5));
059    }
060
061    // Don't try the modify if we're stopping
062    if (context.isStopping()) {
063      return;
064    }
065
066    LOG.debug("Performing action: Adding " + columnDescriptor + " to " + tableName);
067
068    TableDescriptor modifiedTable = TableDescriptorBuilder.newBuilder(tableDescriptor)
069        .setColumnFamily(columnDescriptor).build();
070    admin.modifyTable(modifiedTable);
071  }
072}