Skip to content

Commit d8ea8af

Browse files
committed
[pinpoint-apm#4088] Refactor hbase Connection creation
1 parent 374aa9a commit d8ea8af

File tree

9 files changed

+252
-90
lines changed

9 files changed

+252
-90
lines changed

collector/src/main/resources/applicationContext-hbase.xml

+13-5
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,19 @@
4545
<property name="preStartAllCoreThreads" value="true"/>
4646
</bean>
4747

48-
<bean id="connectionFactory" class="com.navercorp.pinpoint.common.hbase.PooledHTableFactory">
48+
<bean id="connectionFactory" class="com.navercorp.pinpoint.common.hbase.ConnectionFactoryBean">
4949
<constructor-arg type="org.apache.hadoop.conf.Configuration" ref="hbaseConfiguration"/>
5050
<constructor-arg type="java.util.concurrent.ExecutorService" ref="hbaseThreadPool"/>
5151
</bean>
5252

53+
<bean id="hbaseTableFactory" class="com.navercorp.pinpoint.common.hbase.HbaseTableFactory">
54+
<constructor-arg ref="connectionFactory"/>
55+
</bean>
56+
5357
<bean class="org.apache.hadoop.util.ShutdownHookManagerProxy"/>
5458

5559
<bean id="asyncOperation" class="com.navercorp.pinpoint.common.hbase.HBaseAsyncOperationFactory" factory-method="create">
56-
<constructor-arg type="org.apache.hadoop.hbase.client.Connection" value="#{connectionFactory.getConnection()}"/>
60+
<constructor-arg type="org.apache.hadoop.hbase.client.Connection" ref="connectionFactory"/>
5761
<constructor-arg type="org.apache.hadoop.conf.Configuration" ref="hbaseConfiguration"/>
5862
</bean>
5963

@@ -63,12 +67,16 @@
6367

6468
<bean id="hbaseTemplate" class="com.navercorp.pinpoint.common.hbase.HbaseTemplate2" destroy-method="destroy">
6569
<property name="configuration" ref="hbaseConfiguration"/>
66-
<property name="tableFactory" ref="connectionFactory"/>
70+
<property name="tableFactory" ref="hbaseTableFactory"/>
6771
<property name="asyncOperation" ref="asyncOperation"/>
6872
</bean>
6973

70-
<bean id="hBaseAdminTemplate" class="com.navercorp.pinpoint.common.hbase.HBaseAdminTemplate" destroy-method="close">
71-
<constructor-arg ref="hbaseConfiguration" index="0"/>
74+
<bean id="hbaseAdminFactory" class="com.navercorp.pinpoint.common.hbase.HbaseAdminFactory">
75+
<constructor-arg ref="connectionFactory"/>
76+
</bean>
77+
78+
<bean id="hbaseAdminTemplate" class="com.navercorp.pinpoint.common.hbase.HBaseAdminTemplate">
79+
<constructor-arg ref="hbaseAdminFactory"/>
7280
</bean>
7381

7482
<bean id="applicationTraceIndexDistributor" class="com.sematext.hbase.wd.RowKeyDistributorByHashPrefix">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2018 NAVER Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.navercorp.pinpoint.common.hbase;
18+
19+
import org.apache.hadoop.hbase.client.Admin;
20+
21+
/**
22+
* @author HyunGil Jeong
23+
*/
24+
public interface AdminFactory {
25+
26+
Admin getAdmin();
27+
28+
void releaseAdmin(Admin admin);
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2018 NAVER Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.navercorp.pinpoint.common.hbase;
18+
19+
import org.apache.hadoop.conf.Configuration;
20+
import org.apache.hadoop.hbase.client.Connection;
21+
import org.apache.hadoop.hbase.client.ConnectionFactory;
22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
24+
import org.springframework.beans.factory.DisposableBean;
25+
import org.springframework.beans.factory.FactoryBean;
26+
27+
import java.io.IOException;
28+
import java.util.Objects;
29+
import java.util.concurrent.ExecutorService;
30+
31+
/**
32+
* @author HyunGil Jeong
33+
*/
34+
public class ConnectionFactoryBean implements FactoryBean<Connection>, DisposableBean {
35+
36+
private final Logger logger = LoggerFactory.getLogger(this.getClass());
37+
38+
private final Connection connection;
39+
40+
public ConnectionFactoryBean(Configuration configuration) {
41+
Objects.requireNonNull(configuration, " must not be null");
42+
try {
43+
connection = ConnectionFactory.createConnection(configuration);
44+
} catch (IOException e) {
45+
throw new HbaseSystemException(e);
46+
}
47+
}
48+
49+
public ConnectionFactoryBean(Configuration configuration, ExecutorService executorService) {
50+
Objects.requireNonNull(configuration, "configuration must not be null");
51+
Objects.requireNonNull(executorService, "executorService must not be null");
52+
try {
53+
connection = ConnectionFactory.createConnection(configuration, executorService);
54+
} catch (IOException e) {
55+
throw new HbaseSystemException(e);
56+
}
57+
}
58+
59+
@Override
60+
public Connection getObject() throws Exception {
61+
return connection;
62+
}
63+
64+
@Override
65+
public Class<?> getObjectType() {
66+
if (connection == null) {
67+
return Connection.class;
68+
}
69+
return connection.getClass();
70+
}
71+
72+
@Override
73+
public boolean isSingleton() {
74+
return true;
75+
}
76+
77+
@Override
78+
public void destroy() throws Exception {
79+
logger.info("Hbase Connection destroy()");
80+
if (connection != null) {
81+
try {
82+
connection.close();
83+
} catch (IOException e) {
84+
logger.warn("Hbase Connection.close() error: " + e.getMessage(), e);
85+
}
86+
}
87+
}
88+
}

commons-hbase/src/main/java/com/navercorp/pinpoint/common/hbase/HBaseAdminTemplate.java

+24-27
Original file line numberDiff line numberDiff line change
@@ -17,79 +17,76 @@
1717
package com.navercorp.pinpoint.common.hbase;
1818

1919
import java.io.IOException;
20+
import java.util.Objects;
2021

21-
import org.apache.hadoop.conf.Configuration;
2222
import org.apache.hadoop.hbase.HTableDescriptor;
2323
import org.apache.hadoop.hbase.TableName;
2424
import org.apache.hadoop.hbase.client.Admin;
25-
import org.apache.hadoop.hbase.client.Connection;
26-
import org.apache.hadoop.hbase.client.ConnectionFactory;
2725

2826
/**
2927
* @author emeroad
28+
* @author HyunGil Jeong
3029
*/
3130
public class HBaseAdminTemplate {
3231

33-
private final Admin admin;
34-
private final Connection connection;
32+
private final AdminFactory adminFactory;
3533

36-
public HBaseAdminTemplate(Configuration configuration) {
37-
try {
38-
connection = ConnectionFactory.createConnection(configuration);
39-
admin = connection.getAdmin();
40-
} catch (Exception e) {
41-
throw new HbaseSystemException(e);
42-
}
34+
public HBaseAdminTemplate(AdminFactory adminFactory) {
35+
this.adminFactory = Objects.requireNonNull(adminFactory, "adminFactory must not be null");
4336
}
4437

4538
public boolean createTableIfNotExist(HTableDescriptor htd) {
39+
Admin admin = adminFactory.getAdmin();
4640
try {
47-
if (!admin.tableExists(htd.getTableName())) {
48-
this.admin.createTable(htd);
41+
TableName tableName = htd.getTableName();
42+
if (!admin.tableExists(tableName)) {
43+
admin.createTable(htd);
4944
return true;
5045
}
5146
return false;
5247
} catch (IOException e) {
5348
throw new HbaseSystemException(e);
49+
} finally {
50+
adminFactory.releaseAdmin(admin);
5451
}
5552
}
5653

5754
public boolean tableExists(TableName tableName) {
55+
Admin admin = adminFactory.getAdmin();
5856
try {
5957
return admin.tableExists(tableName);
6058
} catch (IOException e) {
6159
throw new HbaseSystemException(e);
60+
} finally {
61+
adminFactory.releaseAdmin(admin);
6262
}
6363
}
6464

6565
public boolean dropTableIfExist(TableName tableName) {
66+
Admin admin = adminFactory.getAdmin();
6667
try {
6768
if (admin.tableExists(tableName)) {
68-
this.admin.disableTable(tableName);
69-
this.admin.deleteTable(tableName);
69+
admin.disableTable(tableName);
70+
admin.deleteTable(tableName);
7071
return true;
7172
}
7273
return false;
7374
} catch (IOException e) {
7475
throw new HbaseSystemException(e);
76+
} finally {
77+
adminFactory.releaseAdmin(admin);
7578
}
7679
}
7780

7881
public void dropTable(TableName tableName) {
82+
Admin admin = adminFactory.getAdmin();
7983
try {
80-
this.admin.disableTable(tableName);
81-
this.admin.deleteTable(tableName);
82-
} catch (IOException e) {
83-
throw new HbaseSystemException(e);
84-
}
85-
}
86-
87-
public void close() {
88-
try {
89-
this.admin.close();
90-
this.connection.close();
84+
admin.disableTable(tableName);
85+
admin.deleteTable(tableName);
9186
} catch (IOException e) {
9287
throw new HbaseSystemException(e);
88+
} finally {
89+
adminFactory.releaseAdmin(admin);
9390
}
9491
}
9592
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2018 NAVER Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.navercorp.pinpoint.common.hbase;
18+
19+
import org.apache.hadoop.hbase.client.Admin;
20+
import org.apache.hadoop.hbase.client.Connection;
21+
22+
import java.io.IOException;
23+
import java.util.Objects;
24+
25+
/**
26+
* @author HyunGil Jeong
27+
*/
28+
public class HbaseAdminFactory implements AdminFactory {
29+
30+
private final Connection connection;
31+
32+
public HbaseAdminFactory(Connection connection) {
33+
this.connection = Objects.requireNonNull(connection, "connection must not be null");
34+
}
35+
36+
@Override
37+
public Admin getAdmin() {
38+
try {
39+
return connection.getAdmin();
40+
} catch (IOException e) {
41+
throw new HbaseSystemException(e);
42+
}
43+
}
44+
45+
@Override
46+
public void releaseAdmin(Admin admin) {
47+
if (admin == null) {
48+
return;
49+
}
50+
try {
51+
admin.close();
52+
} catch (IOException e) {
53+
throw new HbaseSystemException(e);
54+
}
55+
}
56+
}

commons-hbase/src/main/java/com/navercorp/pinpoint/common/hbase/PooledHTableFactory.java commons-hbase/src/main/java/com/navercorp/pinpoint/common/hbase/HbaseTableFactory.java

+6-39
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,9 @@
1616

1717
package com.navercorp.pinpoint.common.hbase;
1818

19-
import org.apache.hadoop.conf.Configuration;
2019
import org.apache.hadoop.hbase.TableName;
2120
import org.apache.hadoop.hbase.client.Connection;
22-
import org.apache.hadoop.hbase.client.ConnectionFactory;
2321
import org.apache.hadoop.hbase.client.Table;
24-
import org.slf4j.Logger;
25-
import org.slf4j.LoggerFactory;
26-
import org.springframework.beans.factory.DisposableBean;
2722

2823
import java.io.IOException;
2924
import java.util.Objects;
@@ -32,35 +27,21 @@
3227
/**
3328
* HTableInterfaceFactory based on HTablePool.
3429
* @author emeroad
35-
* @autor minwoo.jung
30+
* @author minwoo.jung
31+
* @author HyunGil Jeong
3632
*/
37-
public class PooledHTableFactory implements TableFactory, DisposableBean {
33+
public class HbaseTableFactory implements TableFactory {
3834

39-
private final Logger logger = LoggerFactory.getLogger(this.getClass());
40-
41-
private final ExecutorService executor;
4235
private final Connection connection;
4336

44-
public PooledHTableFactory(Configuration config, ExecutorService executor) {
45-
Objects.requireNonNull(config, "config must not be null");
46-
this.executor = Objects.requireNonNull(executor, "executor must not be null");
47-
48-
try {
49-
this.connection = ConnectionFactory.createConnection(config, executor);
50-
} catch (IOException e) {
51-
throw new HbaseSystemException(e);
52-
}
53-
}
54-
55-
public Connection getConnection() {
56-
return connection;
37+
public HbaseTableFactory(Connection connection) {
38+
this.connection = Objects.requireNonNull(connection, "connection must not be null");
5739
}
5840

59-
6041
@Override
6142
public Table getTable(TableName tableName) {
6243
try {
63-
return connection.getTable(tableName, executor);
44+
return connection.getTable(tableName);
6445
} catch (IOException e) {
6546
throw new HbaseSystemException(e);
6647
}
@@ -87,18 +68,4 @@ public void releaseTable(Table table) {
8768
throw new HbaseSystemException(ex);
8869
}
8970
}
90-
91-
92-
@Override
93-
public void destroy() throws Exception {
94-
logger.info("PooledHTableFactory.destroy()");
95-
96-
if (connection != null) {
97-
try {
98-
this.connection.close();
99-
} catch (IOException ex) {
100-
logger.warn("Connection.close() error:" + ex.getMessage(), ex);
101-
}
102-
}
103-
}
10471
}

0 commit comments

Comments
 (0)