Skip to content

Commit bb3c419

Browse files
committed
fix ut resource clean up
1 parent 69894b3 commit bb3c419

File tree

3 files changed

+35
-20
lines changed

3 files changed

+35
-20
lines changed

hudi-sync/hudi-hive-sync/src/test/java/org/apache/hudi/hive/TestHiveSyncTool.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@
2727
import org.apache.hudi.common.util.Option;
2828
import org.apache.hudi.common.util.StringUtils;
2929
import org.apache.hudi.common.util.collection.ImmutablePair;
30+
import org.apache.hudi.hive.ddl.QueryBasedDDLExecutor;
3031
import org.apache.hudi.hive.testutils.HiveTestUtil;
31-
import org.apache.hudi.sync.common.util.ConfigUtils;
3232
import org.apache.hudi.sync.common.AbstractSyncHoodieClient.PartitionEvent;
3333
import org.apache.hudi.sync.common.AbstractSyncHoodieClient.PartitionEvent.PartitionEventType;
34+
import org.apache.hudi.sync.common.util.ConfigUtils;
3435

3536
import org.apache.avro.Schema;
3637
import org.apache.avro.Schema.Field;
@@ -60,7 +61,6 @@
6061
import java.util.stream.Collectors;
6162

6263
import static org.apache.hudi.hive.testutils.HiveTestUtil.basePath;
63-
import static org.apache.hudi.hive.testutils.HiveTestUtil.ddlExecutor;
6464
import static org.apache.hudi.hive.testutils.HiveTestUtil.fileSystem;
6565
import static org.apache.hudi.hive.testutils.HiveTestUtil.getHiveConf;
6666
import static org.apache.hudi.hive.testutils.HiveTestUtil.hiveSyncProps;
@@ -94,9 +94,10 @@ private static Iterable<Object[]> syncModeAndSchemaFromCommitMetadata() {
9494

9595
private HiveSyncTool hiveSyncTool;
9696
private HoodieHiveClient hiveClient;
97+
private QueryBasedDDLExecutor ddlExecutor;
9798

9899
@AfterAll
99-
public static void cleanUpClass() {
100+
public static void cleanUpClass() throws Exception {
100101
HiveTestUtil.shutdown();
101102
}
102103

@@ -122,11 +123,18 @@ private static Iterable<Object[]> syncDataSourceTableParams() {
122123
@BeforeEach
123124
public void setUp() throws Exception {
124125
HiveTestUtil.setUp();
126+
reinitHiveSyncClient();
125127
}
126128

127129
@AfterEach
128130
public void teardown() throws Exception {
129131
HiveTestUtil.clear();
132+
if (ddlExecutor != null) {
133+
ddlExecutor.close();
134+
}
135+
if (hiveClient != null) {
136+
hiveClient.close();
137+
}
130138
}
131139

132140
@ParameterizedTest
@@ -1129,16 +1137,17 @@ public void testSyncWithoutDiffs(String syncMode) throws Exception {
11291137
assertEquals(commitTime1, hiveClient.getLastCommitTimeSynced(tableName).get());
11301138
}
11311139

1132-
private void reSyncHiveTable() {
1140+
private void reSyncHiveTable() throws HiveException, MetaException {
11331141
hiveSyncTool.syncHoodieTable();
11341142
// we need renew the hiveclient after tool.syncHoodieTable(), because it will close hive
11351143
// session, then lead to connection retry, we can see there is a exception at log.
11361144
reinitHiveSyncClient();
11371145
}
11381146

1139-
private void reinitHiveSyncClient() {
1147+
private void reinitHiveSyncClient() throws HiveException, MetaException {
11401148
hiveSyncTool = new HiveSyncTool(hiveSyncProps, HiveTestUtil.getHiveConf(), fileSystem);
11411149
hiveClient = (HoodieHiveClient) hiveSyncTool.hoodieHiveClient;
1150+
ddlExecutor = HiveTestUtil.getQueryBasedDDLExecutor();
11421151
}
11431152

11441153
private int getPartitionFieldSize() {

hudi-sync/hudi-hive-sync/src/test/java/org/apache/hudi/hive/testutils/HiveTestUtil.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ public class HiveTestUtil {
9595
public static TypedProperties hiveSyncProps;
9696
public static HiveTestService hiveTestService;
9797
public static FileSystem fileSystem;
98-
public static QueryBasedDDLExecutor ddlExecutor;
9998

10099
private static ZooKeeperServer zkServer;
101100
private static HiveServer2 hiveServer;
@@ -105,7 +104,7 @@ public class HiveTestUtil {
105104
private static DateTimeFormatter dtfOut;
106105
private static Set<String> createdTablesSet = new HashSet<>();
107106

108-
public static void setUp() throws IOException, InterruptedException, HiveException, MetaException {
107+
public static void setUp() throws Exception {
109108
configuration = new Configuration();
110109
if (zkServer == null) {
111110
zkService = new ZookeeperTestService(configuration);
@@ -115,7 +114,9 @@ public static void setUp() throws IOException, InterruptedException, HiveExcepti
115114
hiveTestService = new HiveTestService(configuration);
116115
hiveServer = hiveTestService.start();
117116
}
118-
fileSystem = FileSystem.get(configuration);
117+
if (fileSystem == null) {
118+
fileSystem = FileSystem.get(configuration);
119+
}
119120

120121
basePath = Files.createTempDirectory("hivesynctest" + Instant.now().toEpochMilli()).toUri().toString();
121122

@@ -134,39 +135,46 @@ public static void setUp() throws IOException, InterruptedException, HiveExcepti
134135
hiveSyncConfig = new HiveSyncConfig(hiveSyncProps);
135136

136137
dtfOut = DateTimeFormatter.ofPattern("yyyy/MM/dd");
137-
ddlExecutor = new HiveQueryDDLExecutor(hiveSyncConfig, fileSystem, getHiveConf());
138138

139139
clear();
140140
}
141141

142-
public static void clearIncrementalPullSetup(String path1, String path2) throws IOException, HiveException, MetaException {
142+
public static void clearIncrementalPullSetup(String path1, String path2) throws Exception {
143143
fileSystem.delete(new Path(path1), true);
144144
if (path2 != null) {
145145
fileSystem.delete(new Path(path2), true);
146146
}
147147
clear();
148148
}
149149

150-
public static void clear() throws IOException, HiveException, MetaException {
150+
public static void clear() throws Exception {
151151
fileSystem.delete(new Path(basePath), true);
152152
HoodieTableMetaClient.withPropertyBuilder()
153153
.setTableType(HoodieTableType.COPY_ON_WRITE)
154154
.setTableName(TABLE_NAME)
155155
.setPayloadClass(HoodieAvroPayload.class)
156156
.initTable(configuration, basePath);
157157

158-
for (String tableName : createdTablesSet) {
159-
ddlExecutor.runSQL("drop table if exists " + tableName);
158+
try (QueryBasedDDLExecutor ddlExecutor = getQueryBasedDDLExecutor()) {
159+
for (String tableName : createdTablesSet) {
160+
ddlExecutor.runSQL("drop table if exists " + tableName);
161+
}
162+
createdTablesSet.clear();
163+
ddlExecutor.runSQL("drop database if exists " + DB_NAME + " cascade");
160164
}
161-
createdTablesSet.clear();
162-
ddlExecutor.runSQL("drop database if exists " + DB_NAME + " cascade");
165+
}
166+
167+
public static QueryBasedDDLExecutor getQueryBasedDDLExecutor() throws HiveException, MetaException {
168+
HiveConf hiveConf = new HiveConf(configuration, HiveConf.class);
169+
hiveConf.addResource(fileSystem.getConf());
170+
return new HiveQueryDDLExecutor(hiveSyncConfig, fileSystem, hiveConf);
163171
}
164172

165173
public static HiveConf getHiveConf() {
166174
return hiveServer.getHiveConf();
167175
}
168176

169-
public static void shutdown() {
177+
public static void shutdown() throws Exception {
170178
if (hiveServer != null) {
171179
hiveServer.stop();
172180
}

hudi-utilities/src/test/java/org/apache/hudi/utilities/TestHiveIncrementalPuller.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@
1818

1919
package org.apache.hudi.utilities;
2020

21-
import org.apache.hadoop.hive.metastore.api.MetaException;
22-
import org.apache.hadoop.hive.ql.metadata.HiveException;
23-
2421
import org.apache.hudi.common.config.TypedProperties;
2522
import org.apache.hudi.hive.HiveSyncConfig;
2623
import org.apache.hudi.hive.HiveSyncTool;
2724
import org.apache.hudi.hive.HoodieHiveClient;
2825
import org.apache.hudi.hive.testutils.HiveTestUtil;
2926
import org.apache.hudi.sync.common.HoodieSyncConfig;
3027
import org.apache.hudi.utilities.exception.HoodieIncrementalPullSQLException;
28+
3129
import org.junit.jupiter.api.AfterEach;
3230
import org.junit.jupiter.api.BeforeEach;
3331
import org.junit.jupiter.api.Test;
@@ -52,7 +50,7 @@ public class TestHiveIncrementalPuller {
5250
private String targetBasePath = null;
5351

5452
@BeforeEach
55-
public void setup() throws HiveException, IOException, InterruptedException, MetaException {
53+
public void setup() throws Exception {
5654
config = new HiveIncrementalPuller.Config();
5755
HiveTestUtil.setUp();
5856
}

0 commit comments

Comments
 (0)