23
23
import com .yahoo .ycsb .Status ;
24
24
import com .yahoo .ycsb .measurements .Measurements ;
25
25
26
+ import org .apache .hadoop .security .UserGroupInformation ;
26
27
import org .apache .hadoop .conf .Configuration ;
27
28
import org .apache .hadoop .hbase .HBaseConfiguration ;
28
29
import org .apache .hadoop .hbase .HTableDescriptor ;
29
30
import org .apache .hadoop .hbase .KeyValue ;
31
+ import org .apache .hadoop .hbase .client .HConnectionManager ;
32
+ import org .apache .hadoop .hbase .client .HConnection ;
33
+ import org .apache .hadoop .hbase .client .HTableInterface ;
30
34
import org .apache .hadoop .hbase .client .Delete ;
31
35
import org .apache .hadoop .hbase .client .Get ;
32
36
import org .apache .hadoop .hbase .client .HTable ;
46
50
import java .util .Random ;
47
51
import java .util .Set ;
48
52
import java .util .Vector ;
53
+ import java .util .concurrent .atomic .AtomicInteger ;
49
54
50
55
/**
51
56
* HBase client for YCSB framework
@@ -55,11 +60,13 @@ public class HBaseClient extends com.yahoo.ycsb.DB
55
60
// BFC: Change to fix broken build (with HBase 0.20.6)
56
61
//private static final Configuration config = HBaseConfiguration.create();
57
62
private static final Configuration config = HBaseConfiguration .create (); //new HBaseConfiguration();
63
+ private static final AtomicInteger THREAD_COUNT = new AtomicInteger (0 );
58
64
59
65
public boolean _debug =false ;
60
66
61
67
public String _table ="" ;
62
- public HTable _hTable =null ;
68
+ private static HConnection _hConn =null ;
69
+ public HTableInterface _hTable =null ;
63
70
public String _columnFamily ="" ;
64
71
public byte _columnFamilyBytes [];
65
72
public boolean _clientSideBuffering = false ;
@@ -94,7 +101,29 @@ public void init() throws DBException
94
101
if ("false" .equals (getProperties ().getProperty ("hbase.usepagefilter" , "true" ))) {
95
102
_usePageFilter = false ;
96
103
}
97
-
104
+ if ("kerberos" .equalsIgnoreCase (config .get ("hbase.security.authentication" ))) {
105
+ config .set ("hadoop.security.authentication" , "Kerberos" );
106
+ UserGroupInformation .setConfiguration (config );
107
+ }
108
+ if ( (getProperties ().getProperty ("principal" )!=null ) && (getProperties ().getProperty ("keytab" )!=null ) ){
109
+ try {
110
+ UserGroupInformation .loginUserFromKeytab (getProperties ().getProperty ("principal" ), getProperties ().getProperty ("keytab" ));
111
+ } catch (IOException e ) {
112
+ System .err .println ("Keytab file is not readable or not found" );
113
+ throw new DBException (e );
114
+ }
115
+ }
116
+ try {
117
+ THREAD_COUNT .getAndIncrement ();
118
+ synchronized (THREAD_COUNT ) {
119
+ if (_hConn == null ){
120
+ _hConn = HConnectionManager .createConnection (config );
121
+ }
122
+ }
123
+ } catch (IOException e ) {
124
+ System .err .println ("Connection to HBase was not successful" );
125
+ throw new DBException (e );
126
+ }
98
127
_columnFamily = getProperties ().getProperty ("columnfamily" );
99
128
if (_columnFamily == null )
100
129
{
@@ -109,7 +138,7 @@ public void init() throws DBException
109
138
String table = com .yahoo .ycsb .workloads .CoreWorkload .table ;
110
139
try
111
140
{
112
- HTable ht = new HTable ( config , table );
141
+ HTableInterface ht = _hConn . getTable ( table );
113
142
ht .getTableDescriptor ();
114
143
}
115
144
catch (IOException e )
@@ -132,6 +161,12 @@ public void cleanup() throws DBException
132
161
if (_hTable != null ) {
133
162
_hTable .flushCommits ();
134
163
}
164
+ synchronized (THREAD_COUNT ) {
165
+ int threadCount = THREAD_COUNT .decrementAndGet ();
166
+ if (threadCount <= 0 && _hConn != null ) {
167
+ _hConn .close ();
168
+ }
169
+ }
135
170
long en =System .nanoTime ();
136
171
_measurements .measure ("UPDATE" , (int )((en -st )/1000 ));
137
172
} catch (IOException e ) {
@@ -142,7 +177,7 @@ public void cleanup() throws DBException
142
177
public void getHTable (String table ) throws IOException
143
178
{
144
179
synchronized (tableLock ) {
145
- _hTable = new HTable ( config , table );
180
+ _hTable = _hConn . getTable ( table );
146
181
//2 suggestions from http://ryantwopointoh.blogspot.com/2009/01/performance-of-hbase-importing.html
147
182
_hTable .setAutoFlush (!_clientSideBuffering , true );
148
183
_hTable .setWriteBufferSize (_writeBufferSize );
0 commit comments