View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.chaos.actions;
20  
21  import org.apache.hadoop.hbase.HBaseTestingUtility;
22  import org.apache.hadoop.hbase.HConstants;
23  import org.apache.hadoop.hbase.HTableDescriptor;
24  import org.apache.hadoop.hbase.TableName;
25  import org.apache.hadoop.hbase.client.Admin;
26  
27  import java.util.Random;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  public class DecreaseMaxHFileSizeAction extends Action {
32    private static final Logger LOG = LoggerFactory.getLogger(DecreaseMaxHFileSizeAction.class);
33  
34    private static final long minFileSize = 1024 * 1024 * 1024L;
35  
36    private final long sleepTime;
37    private final TableName tableName;
38    private final Random random;
39  
40    public DecreaseMaxHFileSizeAction(long sleepTime, TableName tableName) {
41      this.sleepTime = sleepTime;
42      this.tableName = tableName;
43      this.random = new Random();
44    }
45  
46    @Override protected Logger getLogger() {
47      return LOG;
48    }
49  
50    @Override
51    public void perform() throws Exception {
52      HBaseTestingUtility util = context.getHBaseIntegrationTestingUtility();
53      Admin admin = util.getHBaseAdmin();
54      HTableDescriptor htd = admin.getTableDescriptor(tableName);
55  
56      // Try and get the current value.
57      long currentValue = htd.getMaxFileSize();
58  
59      // If the current value is not set use the default for the cluster.
60      // If configs are really weird this might not work.
61      // That's ok. We're trying to cause chaos.
62      if (currentValue <= 0) {
63        currentValue =
64            context.getHBaseCluster().getConf().getLong(HConstants.HREGION_MAX_FILESIZE,
65                HConstants.DEFAULT_MAX_FILE_SIZE);
66      }
67  
68      // Decrease by 10% at a time.
69      long newValue = (long) (currentValue * 0.9);
70  
71      // We don't want to go too far below 1gb.
72      // So go to about 1gb +/- 512 on each side.
73      newValue = Math.max(minFileSize, newValue) - (512 - random.nextInt(1024));
74  
75      // Change the table descriptor.
76      htd.setMaxFileSize(newValue);
77  
78      // Don't try the modify if we're stopping
79      if (context.isStopping()) {
80        return;
81      }
82  
83      // modify the table.
84      admin.modifyTable(tableName, htd);
85  
86      // Sleep some time.
87      if (sleepTime > 0) {
88        Thread.sleep(sleepTime);
89      }
90    }
91  }