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