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