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