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
;
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
;
51 public void start(long startKey
, long endKey
, int numThreads
) throws IOException
{
52 super.start(startKey
, endKey
, numThreads
);
56 protected void createWriterThreads(int numThreads
) throws IOException
{
57 for (int i
= 0; i
< numThreads
; ++i
) {
58 HBaseWriterThread writer
= new HBaseWriterThreadWithACL(i
);
63 public class HBaseWriterThreadWithACL
extends HBaseWriterThread
{
66 private WriteAccessAction writerAction
= new WriteAccessAction();
68 public HBaseWriterThreadWithACL(int writerId
) throws IOException
{
73 protected Table
createTable() throws IOException
{
78 protected void closeHTable() {
82 } catch (Exception e
) {
83 LOG
.error("Error in closing the table "+table
.getName(), e
);
89 public void insert(final Table table
, Put put
, final long keyBase
) {
90 final long start
= EnvironmentEdgeManager
.currentTime();
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
> {
106 private long keyBase
;
109 public WriteAccessAction() {
112 public void setPut(final Put put
) {
116 public void setKeyBase(final long keyBase
) {
117 this.keyBase
= keyBase
;
120 public void setStartTime(final long start
) {
125 public Object
run() throws Exception
{
128 table
= connection
.getTable(tableName
);
131 } catch (IOException e
) {
132 recordFailure(table
, put
, keyBase
, start
, e
);
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();
147 StringWriter stackWriter
= new StringWriter();
148 PrintWriter pw
= new PrintWriter(stackWriter
);
149 e
.printStackTrace(pw
);
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
);