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  
29  public class DecreaseMaxHFileSizeAction extends Action {
30  
31    private static final long minFileSize = 1 *  1024 * 1024 * 1024L;
32  
33    private final long sleepTime;
34    private final TableName tableName;
35    private final Random random;
36  
37    public DecreaseMaxHFileSizeAction(long sleepTime, TableName tableName) {
38      this.sleepTime = sleepTime;
39      this.tableName = tableName;
40      this.random = new Random();
41    }
42  
43    @Override
44    public void perform() throws Exception {
45      HBaseTestingUtility util = context.getHBaseIntegrationTestingUtility();
46      Admin admin = util.getHBaseAdmin();
47      HTableDescriptor htd = admin.getTableDescriptor(tableName);
48  
49      // Try and get the current value.
50      long currentValue = htd.getMaxFileSize();
51  
52      // If the current value is not set use the default for the cluster.
53      // If configs are really weird this might not work.
54      // That's ok. We're trying to cause chaos.
55      if (currentValue <= 0) {
56        currentValue =
57            context.getHBaseCluster().getConf().getLong(HConstants.HREGION_MAX_FILESIZE,
58                HConstants.DEFAULT_MAX_FILE_SIZE);
59      }
60  
61      // Decrease by 10% at a time.
62      long newValue = (long) (currentValue * 0.9);
63  
64      // We don't want to go too far below 1gb.
65      // So go to about 1gb +/- 512 on each side.
66      newValue = Math.max(minFileSize, newValue) - (512 - random.nextInt(1024));
67  
68      // Change the table descriptor.
69      htd.setMaxFileSize(newValue);
70  
71      // Don't try the modify if we're stopping
72      if (context.isStopping()) {
73        return;
74      }
75  
76      // modify the table.
77      admin.modifyTable(tableName, htd);
78  
79      // Sleep some time.
80      if (sleepTime > 0) {
81        Thread.sleep(sleepTime);
82      }
83    }
84  }