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;
022import java.util.Random;
023import org.apache.hadoop.hbase.HConstants;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.client.Admin;
026import org.apache.hadoop.hbase.client.TableDescriptor;
027import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031public class DecreaseMaxHFileSizeAction extends Action {
032  private static final Logger LOG = LoggerFactory.getLogger(DecreaseMaxHFileSizeAction.class);
033
034  private static final long minFileSize = 1024 * 1024 * 1024L;
035
036  private final long sleepTime;
037  private final TableName tableName;
038  private final Random random;
039  private Admin admin;
040
041  public DecreaseMaxHFileSizeAction(long sleepTime, TableName tableName) {
042    this.sleepTime = sleepTime;
043    this.tableName = tableName;
044    this.random = new Random();
045  }
046
047  @Override protected Logger getLogger() {
048    return LOG;
049  }
050
051  @Override
052  public void init(ActionContext context) throws IOException {
053    super.init(context);
054    this.admin = context.getHBaseIntegrationTestingUtility().getAdmin();
055  }
056
057  @Override
058  public void perform() throws Exception {
059    TableDescriptor td = admin.getDescriptor(tableName);
060
061    // Try and get the current value.
062    long currentValue = td.getMaxFileSize();
063
064    // If the current value is not set use the default for the cluster.
065    // If configs are really weird this might not work.
066    // That's ok. We're trying to cause chaos.
067    if (currentValue <= 0) {
068      currentValue =
069          context.getHBaseCluster().getConf().getLong(HConstants.HREGION_MAX_FILESIZE,
070              HConstants.DEFAULT_MAX_FILE_SIZE);
071    }
072
073    // Decrease by 10% at a time.
074    long newValue = (long) (currentValue * 0.9);
075
076    // We don't want to go too far below 1gb.
077    // So go to about 1gb +/- 512 on each side.
078    newValue = Math.max(minFileSize, newValue) - (512 - random.nextInt(1024));
079
080    // Change the table descriptor.
081    TableDescriptor modifiedTable =
082        TableDescriptorBuilder.newBuilder(td).setMaxFileSize(newValue).build();
083
084    // Don't try the modify if we're stopping
085    if (context.isStopping()) {
086      return;
087    }
088
089    // modify the table.
090    admin.modifyTable(modifiedTable);
091
092    // Sleep some time.
093    if (sleepTime > 0) {
094      Thread.sleep(sleepTime);
095    }
096  }
097}