HBASE-26416 Implement a new method for region replication instead of using replay...
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / util / MultiThreadedWriterWithACL.java
blob1b2d40da827daf11dc09c7a25a4d4644b57fe6e2
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.hadoop.hbase.util;
20 import java.io.IOException;
21 import java.io.PrintWriter;
22 import java.io.StringWriter;
23 import java.security.PrivilegedExceptionAction;
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.hbase.TableName;
27 import org.apache.hadoop.hbase.client.Put;
28 import org.apache.hadoop.hbase.client.RetriesExhaustedWithDetailsException;
29 import org.apache.hadoop.hbase.client.Table;
30 import org.apache.hadoop.hbase.security.User;
31 import org.apache.hadoop.hbase.util.test.LoadTestDataGenerator;
32 import org.apache.hadoop.util.StringUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 /**
37 * MultiThreadedWriter that helps in testing ACL
39 public class MultiThreadedWriterWithACL extends MultiThreadedWriter {
41 private static final Logger LOG = LoggerFactory.getLogger(MultiThreadedWriterWithACL.class);
42 private User userOwner;
44 public MultiThreadedWriterWithACL(LoadTestDataGenerator dataGen, Configuration conf,
45 TableName tableName, User userOwner) throws IOException {
46 super(dataGen, conf, tableName);
47 this.userOwner = userOwner;
50 @Override
51 public void start(long startKey, long endKey, int numThreads) throws IOException {
52 super.start(startKey, endKey, numThreads);
55 @Override
56 protected void createWriterThreads(int numThreads) throws IOException {
57 for (int i = 0; i < numThreads; ++i) {
58 HBaseWriterThread writer = new HBaseWriterThreadWithACL(i);
59 writers.add(writer);
63 public class HBaseWriterThreadWithACL extends HBaseWriterThread {
65 private Table table;
66 private WriteAccessAction writerAction = new WriteAccessAction();
68 public HBaseWriterThreadWithACL(int writerId) throws IOException {
69 super(writerId);
72 @Override
73 protected Table createTable() throws IOException {
74 return null;
77 @Override
78 protected void closeHTable() {
79 if (table != null) {
80 try {
81 table.close();
82 } catch (Exception e) {
83 LOG.error("Error in closing the table "+table.getName(), e);
88 @Override
89 public void insert(final Table table, Put put, final long keyBase) {
90 final long start = EnvironmentEdgeManager.currentTime();
91 try {
92 put = (Put) dataGenerator.beforeMutate(keyBase, put);
93 writerAction.setPut(put);
94 writerAction.setKeyBase(keyBase);
95 writerAction.setStartTime(start);
96 userOwner.runAs(writerAction);
97 } catch (IOException e) {
98 recordFailure(table, put, keyBase, start, e);
99 } catch (InterruptedException e) {
100 failedKeySet.add(keyBase);
104 class WriteAccessAction implements PrivilegedExceptionAction<Object> {
105 private Put put;
106 private long keyBase;
107 private long start;
109 public WriteAccessAction() {
112 public void setPut(final Put put) {
113 this.put = put;
116 public void setKeyBase(final long keyBase) {
117 this.keyBase = keyBase;
120 public void setStartTime(final long start) {
121 this.start = start;
124 @Override
125 public Object run() throws Exception {
126 try {
127 if (table == null) {
128 table = connection.getTable(tableName);
130 table.put(put);
131 } catch (IOException e) {
132 recordFailure(table, put, keyBase, start, e);
134 return null;
139 private void recordFailure(final Table table, final Put put, final long keyBase,
140 final long start, IOException e) {
141 failedKeySet.add(keyBase);
142 String exceptionInfo;
143 if (e instanceof RetriesExhaustedWithDetailsException) {
144 RetriesExhaustedWithDetailsException aggEx = (RetriesExhaustedWithDetailsException) e;
145 exceptionInfo = aggEx.getExhaustiveDescription();
146 } else {
147 StringWriter stackWriter = new StringWriter();
148 PrintWriter pw = new PrintWriter(stackWriter);
149 e.printStackTrace(pw);
150 pw.flush();
151 exceptionInfo = StringUtils.stringifyException(e);
153 LOG.error("Failed to insert: " + keyBase + " after " +
154 (EnvironmentEdgeManager.currentTime() - start) + "ms; region information: " +
155 getRegionDebugInfoSafe(table, put.getRow()) + "; errors: " + exceptionInfo);