Skip to content

Commit 779a703

Browse files
committed
Merge pull request brianfrankcooper#591 from bijugs/hbase_changes
[hbase] Changes to support secured HBase cluster
2 parents 42f6bf3 + d058bfc commit 779a703

File tree

3 files changed

+72
-7
lines changed

3 files changed

+72
-7
lines changed

hbase098/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,6 @@ Following options can be configurable using `-p`.
7474
* `hbase.usepagefilter` : If true, HBase
7575
[PageFilter](https://hbase.apache.org/apidocs/org/apache/hadoop/hbase/filter/PageFilter.html)s
7676
are used to limit the number of records consumed in a scan operation. The default is true.
77+
* `principal`: If testing need to be done against a secure HBase cluster using Kerberos Keytab,
78+
this property can be used to pass the principal in the keytab file.
79+
* `keytab`: The Kerberos keytab file name and location can be passed through this property.

hbase098/src/main/java/com/yahoo/ycsb/db/HBaseClient.java

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@
2323
import com.yahoo.ycsb.Status;
2424
import com.yahoo.ycsb.measurements.Measurements;
2525

26+
import org.apache.hadoop.security.UserGroupInformation;
2627
import org.apache.hadoop.conf.Configuration;
2728
import org.apache.hadoop.hbase.HBaseConfiguration;
2829
import org.apache.hadoop.hbase.HTableDescriptor;
2930
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;
3034
import org.apache.hadoop.hbase.client.Delete;
3135
import org.apache.hadoop.hbase.client.Get;
3236
import org.apache.hadoop.hbase.client.HTable;
@@ -46,6 +50,7 @@
4650
import java.util.Random;
4751
import java.util.Set;
4852
import java.util.Vector;
53+
import java.util.concurrent.atomic.AtomicInteger;
4954

5055
/**
5156
* HBase client for YCSB framework
@@ -55,11 +60,13 @@ public class HBaseClient extends com.yahoo.ycsb.DB
5560
// BFC: Change to fix broken build (with HBase 0.20.6)
5661
//private static final Configuration config = HBaseConfiguration.create();
5762
private static final Configuration config = HBaseConfiguration.create(); //new HBaseConfiguration();
63+
private static final AtomicInteger THREAD_COUNT = new AtomicInteger(0);
5864

5965
public boolean _debug=false;
6066

6167
public String _table="";
62-
public HTable _hTable=null;
68+
private static HConnection _hConn=null;
69+
public HTableInterface _hTable=null;
6370
public String _columnFamily="";
6471
public byte _columnFamilyBytes[];
6572
public boolean _clientSideBuffering = false;
@@ -94,7 +101,29 @@ public void init() throws DBException
94101
if ("false".equals(getProperties().getProperty("hbase.usepagefilter", "true"))) {
95102
_usePageFilter = false;
96103
}
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+
}
98127
_columnFamily = getProperties().getProperty("columnfamily");
99128
if (_columnFamily == null)
100129
{
@@ -109,7 +138,7 @@ public void init() throws DBException
109138
String table = com.yahoo.ycsb.workloads.CoreWorkload.table;
110139
try
111140
{
112-
HTable ht = new HTable(config, table);
141+
HTableInterface ht = _hConn.getTable(table);
113142
ht.getTableDescriptor();
114143
}
115144
catch (IOException e)
@@ -132,6 +161,12 @@ public void cleanup() throws DBException
132161
if (_hTable != null) {
133162
_hTable.flushCommits();
134163
}
164+
synchronized(THREAD_COUNT) {
165+
int threadCount = THREAD_COUNT.decrementAndGet();
166+
if (threadCount <= 0 && _hConn != null) {
167+
_hConn.close();
168+
}
169+
}
135170
long en=System.nanoTime();
136171
_measurements.measure("UPDATE", (int)((en-st)/1000));
137172
} catch (IOException e) {
@@ -142,7 +177,7 @@ public void cleanup() throws DBException
142177
public void getHTable(String table) throws IOException
143178
{
144179
synchronized (tableLock) {
145-
_hTable = new HTable(config, table);
180+
_hTable = _hConn.getTable(table);
146181
//2 suggestions from http://ryantwopointoh.blogspot.com/2009/01/performance-of-hbase-importing.html
147182
_hTable.setAutoFlush(!_clientSideBuffering, true);
148183
_hTable.setWriteBufferSize(_writeBufferSize);

hbase10/src/main/java/com/yahoo/ycsb/db/HBaseClient10.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.yahoo.ycsb.Status;
2525
import com.yahoo.ycsb.measurements.Measurements;
2626

27+
import org.apache.hadoop.security.UserGroupInformation;
2728
import org.apache.hadoop.conf.Configuration;
2829
import org.apache.hadoop.hbase.Cell;
2930
import org.apache.hadoop.hbase.CellUtil;
@@ -50,6 +51,7 @@
5051
import java.util.Map;
5152
import java.util.Set;
5253
import java.util.Vector;
54+
import java.util.concurrent.atomic.AtomicInteger;
5355

5456
/**
5557
* HBase 1.0 client for YCSB framework.
@@ -62,11 +64,12 @@
6264
*/
6365
public class HBaseClient10 extends com.yahoo.ycsb.DB {
6466
private Configuration config = HBaseConfiguration.create();
67+
private static final AtomicInteger THREAD_COUNT = new AtomicInteger(0);
6568

6669
private boolean debug = false;
6770

6871
private String tableName = "";
69-
private Connection connection = null;
72+
private static Connection connection = null;
7073

7174
// Depending on the value of clientSideBuffering, either bufferedMutator
7275
// (clientSideBuffering) or currentTable (!clientSideBuffering) will be used.
@@ -112,8 +115,27 @@ public void init() throws DBException {
112115
Durability.valueOf(getProperties().getProperty("durability"));
113116
}
114117

118+
if ("kerberos".equalsIgnoreCase(config.get("hbase.security.authentication"))) {
119+
config.set("hadoop.security.authentication", "Kerberos");
120+
UserGroupInformation.setConfiguration(config);
121+
}
122+
123+
if ((getProperties().getProperty("principal")!=null)
124+
&& (getProperties().getProperty("keytab")!=null)) {
125+
try {
126+
UserGroupInformation.loginUserFromKeytab(getProperties().getProperty("principal"),
127+
getProperties().getProperty("keytab"));
128+
} catch (IOException e) {
129+
System.err.println("Keytab file is not readable or not found");
130+
throw new DBException(e);
131+
}
132+
}
133+
115134
try {
116-
connection = ConnectionFactory.createConnection(config);
135+
THREAD_COUNT.getAndIncrement();
136+
synchronized(THREAD_COUNT) {
137+
connection = ConnectionFactory.createConnection(config);
138+
}
117139
} catch (java.io.IOException e) {
118140
throw new DBException(e);
119141
}
@@ -168,7 +190,12 @@ public void cleanup() throws DBException {
168190
long en = System.nanoTime();
169191
final String type = clientSideBuffering ? "UPDATE" : "CLEANUP";
170192
measurements.measure(type, (int) ((en - st) / 1000));
171-
connection.close();
193+
synchronized(THREAD_COUNT) {
194+
int threadCount = THREAD_COUNT.decrementAndGet();
195+
if (threadCount <= 0 && connection != null) {
196+
connection.close();
197+
}
198+
}
172199
} catch (IOException e) {
173200
throw new DBException(e);
174201
}

0 commit comments

Comments
 (0)