1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.hadoop.hbase.util;
18
19 import java.io.IOException;
20 import java.security.PrivilegedExceptionAction;
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.hbase.TableName;
28 import org.apache.hadoop.hbase.client.Get;
29 import org.apache.hadoop.hbase.client.HTable;
30 import org.apache.hadoop.hbase.client.Result;
31 import org.apache.hadoop.hbase.security.User;
32 import org.apache.hadoop.hbase.util.test.LoadTestDataGenerator;
33 import org.apache.hadoop.security.UserGroupInformation;
34
35
36
37
38 public class MultiThreadedReaderWithACL extends MultiThreadedReader {
39 private static final Log LOG = LogFactory.getLog(MultiThreadedReaderWithACL.class);
40
41 private static final String COMMA = ",";
42
43
44
45
46 private Map<String, HTable> userVsTable = new HashMap<String, HTable>();
47 private Map<String, User> users = new HashMap<String, User>();
48 private String[] userNames;
49
50 public MultiThreadedReaderWithACL(LoadTestDataGenerator dataGen, Configuration conf,
51 TableName tableName, double verifyPercent) {
52 super(dataGen, conf, tableName, verifyPercent);
53 userNames = dataGenerator.getArgs()[0].split(COMMA);
54 }
55
56 @Override
57 protected void addReaderThreads(int numThreads) throws IOException {
58 for (int i = 0; i < numThreads; ++i) {
59 HBaseReaderThread reader = new HBaseReaderThreadWithACL(i);
60 readers.add(reader);
61 }
62 }
63
64 public class HBaseReaderThreadWithACL extends HBaseReaderThread {
65
66 public HBaseReaderThreadWithACL(int readerId) throws IOException {
67 super(readerId);
68 }
69
70 @Override
71 protected HTable createTable() throws IOException {
72 return null;
73 }
74
75 @Override
76 protected void closeTable() {
77 for (HTable table : userVsTable.values()) {
78 try {
79 table.close();
80 } catch (Exception e) {
81 LOG.error("Error while closing the table " + table.getName(), e);
82 }
83 }
84 }
85
86 @Override
87 public void queryKey(final Get get, final boolean verify, final long keyToRead)
88 throws IOException {
89 final String rowKey = Bytes.toString(get.getRow());
90
91
92 final long start = System.currentTimeMillis();
93 PrivilegedExceptionAction<Object> action = new PrivilegedExceptionAction<Object>() {
94 @Override
95 public Object run() throws Exception {
96 HTable localTable = null;
97 try {
98 get.setACLStrategy(true);
99 Result result = null;
100 int specialPermCellInsertionFactor = Integer.parseInt(dataGenerator.getArgs()[1]);
101 int mod = ((int) keyToRead % userNames.length);
102 if (userVsTable.get(userNames[mod]) == null) {
103 localTable = new HTable(conf, tableName);
104 userVsTable.put(userNames[mod], localTable);
105 result = localTable.get(get);
106 } else {
107 localTable = userVsTable.get(userNames[mod]);
108 result = localTable.get(get);
109 }
110 boolean isNullExpected = ((((int) keyToRead % specialPermCellInsertionFactor)) == 0);
111 LOG.info("Read happening from ACL " + isNullExpected);
112 getResultMetricUpdation(verify, rowKey, start, result, localTable, isNullExpected);
113 } catch (IOException e) {
114 recordFailure(keyToRead);
115 }
116 return null;
117 }
118 };
119 if (userNames != null && userNames.length > 0) {
120 int mod = ((int) keyToRead % userNames.length);
121 User user;
122 if(!users.containsKey(userNames[mod])) {
123 UserGroupInformation realUserUgi = UserGroupInformation.createRemoteUser(userNames[mod]);
124 user = User.create(realUserUgi);
125 users.put(userNames[mod], user);
126 } else {
127 user = users.get(userNames[mod]);
128 }
129 try {
130 user.runAs(action);
131 } catch (Exception e) {
132 recordFailure(keyToRead);
133 }
134 }
135 }
136
137 private void recordFailure(final long keyToRead) {
138 numReadFailures.addAndGet(1);
139 LOG.debug("[" + readerId + "] FAILED read, key = " + (keyToRead + "") + ", "
140 + "time from start: " + (System.currentTimeMillis() - startTimeMs) + " ms");
141 }
142 }
143
144 }