1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.util;
19
20 import java.io.IOException;
21 import java.io.PrintWriter;
22 import java.io.StringWriter;
23 import java.security.PrivilegedExceptionAction;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.conf.Configuration;
30 import org.apache.hadoop.hbase.TableName;
31 import org.apache.hadoop.hbase.client.Append;
32 import org.apache.hadoop.hbase.client.Delete;
33 import org.apache.hadoop.hbase.client.Get;
34 import org.apache.hadoop.hbase.client.HTable;
35 import org.apache.hadoop.hbase.client.Increment;
36 import org.apache.hadoop.hbase.client.Mutation;
37 import org.apache.hadoop.hbase.client.Put;
38 import org.apache.hadoop.hbase.client.Result;
39 import org.apache.hadoop.hbase.client.RetriesExhaustedWithDetailsException;
40 import org.apache.hadoop.hbase.security.User;
41 import org.apache.hadoop.hbase.util.test.LoadTestDataGenerator;
42 import org.apache.hadoop.security.UserGroupInformation;
43 import org.apache.hadoop.util.StringUtils;
44
45
46
47
48 public class MultiThreadedUpdaterWithACL extends MultiThreadedUpdater {
49 private static final Log LOG = LogFactory.getLog(MultiThreadedUpdaterWithACL.class);
50 private final static String COMMA= ",";
51 private User userOwner;
52
53
54
55
56 private Map<String, HTable> userVsTable = new HashMap<String, HTable>();
57 private Map<String, User> users = new HashMap<String, User>();
58 private String[] userNames;
59
60 public MultiThreadedUpdaterWithACL(LoadTestDataGenerator dataGen, Configuration conf,
61 TableName tableName, double updatePercent, User userOwner, String userNames) {
62 super(dataGen, conf, tableName, updatePercent);
63 this.userOwner = userOwner;
64 this.userNames = userNames.split(COMMA);
65 }
66
67 @Override
68 protected void addUpdaterThreads(int numThreads) throws IOException {
69 for (int i = 0; i < numThreads; ++i) {
70 HBaseUpdaterThread updater = new HBaseUpdaterThreadWithACL(i);
71 updaters.add(updater);
72 }
73 }
74
75 public class HBaseUpdaterThreadWithACL extends HBaseUpdaterThread {
76
77 private HTable table;
78 private MutateAccessAction mutateAction = new MutateAccessAction();
79
80 public HBaseUpdaterThreadWithACL(int updaterId) throws IOException {
81 super(updaterId);
82 }
83
84 @Override
85 protected HTable createTable() throws IOException {
86 return null;
87 }
88
89 @Override
90 protected void closeHTable() {
91 try {
92 if (table != null) {
93 table.close();
94 }
95 for (HTable table : userVsTable.values()) {
96 try {
97 table.close();
98 } catch (Exception e) {
99 LOG.error("Error while closing the table " + table.getName(), e);
100 }
101 }
102 } catch (Exception e) {
103 LOG.error("Error while closing the HTable "+table.getName(), e);
104 }
105 }
106
107 @Override
108 protected Result getRow(final Get get, final long rowKeyBase, final byte[] cf) {
109 PrivilegedExceptionAction<Object> action = new PrivilegedExceptionAction<Object>() {
110
111 @Override
112 public Object run() throws Exception {
113 Result res = null;
114 HTable localTable = null;
115 try {
116 int mod = ((int) rowKeyBase % userNames.length);
117 if (userVsTable.get(userNames[mod]) == null) {
118 localTable = new HTable(conf, tableName);
119 userVsTable.put(userNames[mod], localTable);
120 res = localTable.get(get);
121 } else {
122 localTable = userVsTable.get(userNames[mod]);
123 res = localTable.get(get);
124 }
125 } catch (IOException ie) {
126 LOG.warn("Failed to get the row for key = [" + get.getRow() + "], column family = ["
127 + Bytes.toString(cf) + "]", ie);
128 }
129 return res;
130 }
131 };
132
133 if (userNames != null && userNames.length > 0) {
134 int mod = ((int) rowKeyBase % userNames.length);
135 User user;
136 if(!users.containsKey(userNames[mod])) {
137 UserGroupInformation realUserUgi = UserGroupInformation.createRemoteUser(userNames[mod]);
138 user = User.create(realUserUgi);
139 users.put(userNames[mod], user);
140 } else {
141 user = users.get(userNames[mod]);
142 }
143 try {
144 Result result = (Result) user.runAs(action);
145 return result;
146 } catch (Exception ie) {
147 LOG.warn("Failed to get the row for key = [" + get.getRow() + "], column family = ["
148 + Bytes.toString(cf) + "]", ie);
149 }
150 }
151
152 return null;
153 }
154
155 @Override
156 public void mutate(final HTable table, Mutation m, final long keyBase, final byte[] row,
157 final byte[] cf, final byte[] q, final byte[] v) {
158 final long start = System.currentTimeMillis();
159 try {
160 m = dataGenerator.beforeMutate(keyBase, m);
161 mutateAction.setMutation(m);
162 mutateAction.setCF(cf);
163 mutateAction.setRow(row);
164 mutateAction.setQualifier(q);
165 mutateAction.setValue(v);
166 mutateAction.setStartTime(start);
167 mutateAction.setKeyBase(keyBase);
168 userOwner.runAs(mutateAction);
169 } catch (IOException e) {
170 recordFailure(m, keyBase, start, e);
171 } catch (InterruptedException e) {
172 failedKeySet.add(keyBase);
173 }
174 }
175
176 class MutateAccessAction implements PrivilegedExceptionAction<Object> {
177 private HTable table;
178 private long start;
179 private Mutation m;
180 private long keyBase;
181 private byte[] row;
182 private byte[] cf;
183 private byte[] q;
184 private byte[] v;
185
186 public MutateAccessAction() {
187
188 }
189
190 public void setStartTime(final long start) {
191 this.start = start;
192 }
193
194 public void setMutation(final Mutation m) {
195 this.m = m;
196 }
197
198 public void setRow(final byte[] row) {
199 this.row = row;
200 }
201
202 public void setCF(final byte[] cf) {
203 this.cf = cf;
204 }
205
206 public void setQualifier(final byte[] q) {
207 this.q = q;
208 }
209
210 public void setValue(final byte[] v) {
211 this.v = v;
212 }
213
214 public void setKeyBase(final long keyBase) {
215 this.keyBase = keyBase;
216 }
217
218 @Override
219 public Object run() throws Exception {
220 try {
221 if (table == null) {
222 table = new HTable(conf, tableName);
223 }
224 if (m instanceof Increment) {
225 table.increment((Increment) m);
226 } else if (m instanceof Append) {
227 table.append((Append) m);
228 } else if (m instanceof Put) {
229 table.checkAndPut(row, cf, q, v, (Put) m);
230 } else if (m instanceof Delete) {
231 table.checkAndDelete(row, cf, q, v, (Delete) m);
232 } else {
233 throw new IllegalArgumentException("unsupported mutation "
234 + m.getClass().getSimpleName());
235 }
236 totalOpTimeMs.addAndGet(System.currentTimeMillis() - start);
237 } catch (IOException e) {
238 recordFailure(m, keyBase, start, e);
239 }
240 return null;
241 }
242 }
243
244 private void recordFailure(final Mutation m, final long keyBase,
245 final long start, IOException e) {
246 failedKeySet.add(keyBase);
247 String exceptionInfo;
248 if (e instanceof RetriesExhaustedWithDetailsException) {
249 RetriesExhaustedWithDetailsException aggEx = (RetriesExhaustedWithDetailsException) e;
250 exceptionInfo = aggEx.getExhaustiveDescription();
251 } else {
252 StringWriter stackWriter = new StringWriter();
253 PrintWriter pw = new PrintWriter(stackWriter);
254 e.printStackTrace(pw);
255 pw.flush();
256 exceptionInfo = StringUtils.stringifyException(e);
257 }
258 LOG.error("Failed to mutate: " + keyBase + " after " + (System.currentTimeMillis() - start)
259 + "ms; region information: " + getRegionDebugInfoSafe(table, m.getRow()) + "; errors: "
260 + exceptionInfo);
261 }
262 }
263 }