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 protected Logger getLogger() {
48 return LOG;
49 }
50
51 @Override
52 public void init(ActionContext context) throws IOException {
53 super.init(context);
54 this.admin = context.getHBaseIntegrationTestingUtility().getHBaseAdmin();
55 }
56
57 @Override
58 public void perform() throws Exception {
59 HTableDescriptor tableDescriptor = admin.getTableDescriptor(tableName);
60 HColumnDescriptor[] columnDescriptors = tableDescriptor.getColumnFamilies();
61
62 if (columnDescriptors == null || columnDescriptors.length == 0) {
63 return;
64 }
65
66 getLogger().debug("Performing action: Changing encodings on " + tableName);
67
68 int[] possibleIds = {0, 2, 3, 4, 6};
69 for (HColumnDescriptor descriptor : columnDescriptors) {
70 short id = (short) possibleIds[random.nextInt(possibleIds.length)];
71 descriptor.setDataBlockEncoding(DataBlockEncoding.getEncodingById(id));
72 getLogger().debug("Set encoding of column family " + descriptor.getNameAsString()
73 + " to: " + descriptor.getDataBlockEncoding());
74 }
75
76
77 if (context.isStopping()) {
78 return;
79 }
80 admin.modifyTable(tableName, tableDescriptor);
81 }
82 }