1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.chaos.actions;
20
21 import java.io.IOException;
22 import java.util.Random;
23
24 import org.apache.hadoop.hbase.HColumnDescriptor;
25 import org.apache.hadoop.hbase.HTableDescriptor;
26 import org.apache.hadoop.hbase.TableName;
27 import org.apache.hadoop.hbase.client.Admin;
28 import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32
33
34
35 public class ChangeEncodingAction extends Action {
36 private final TableName tableName;
37
38 private Admin admin;
39 private Random random;
40 private static final Logger LOG = LoggerFactory.getLogger(ChangeEncodingAction.class);
41
42 public ChangeEncodingAction(TableName tableName) {
43 this.tableName = tableName;
44 this.random = new Random();
45 }
46
47 @Override
48 public void init(ActionContext context) throws IOException {
49 super.init(context);
50 this.admin = context.getHBaseIntegrationTestingUtility().getHBaseAdmin();
51 }
52
53 @Override
54 public void perform() throws Exception {
55 HTableDescriptor tableDescriptor = admin.getTableDescriptor(tableName);
56 HColumnDescriptor[] columnDescriptors = tableDescriptor.getColumnFamilies();
57
58 if (columnDescriptors == null || columnDescriptors.length == 0) {
59 return;
60 }
61
62 LOG.debug("Performing action: Changing encodings on " + tableName);
63
64 int[] possibleIds = {0, 2, 3, 4, 6};
65 for (HColumnDescriptor descriptor : columnDescriptors) {
66 short id = (short) possibleIds[random.nextInt(possibleIds.length)];
67 descriptor.setDataBlockEncoding(DataBlockEncoding.getEncodingById(id));
68 LOG.debug("Set encoding of column family " + descriptor.getNameAsString()
69 + " to: " + descriptor.getDataBlockEncoding());
70 }
71
72
73 if (context.isStopping()) {
74 return;
75 }
76 admin.modifyTable(tableName, tableDescriptor);
77 }
78 }