Skip to content

Commit 5553887

Browse files
committed
HDDS-1949. Missing or error-prone test cleanup.
Contributed by Doroszlai, Attila.
1 parent dd08346 commit 5553887

File tree

9 files changed

+273
-168
lines changed

9 files changed

+273
-168
lines changed

hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/MiniOzoneCluster.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ abstract class Builder {
240240
protected static final int ACTIVE_OMS_NOT_SET = -1;
241241

242242
protected final OzoneConfiguration conf;
243-
protected final String path;
243+
protected String path;
244244

245245
protected String clusterId;
246246
protected String omServiceId;
@@ -269,9 +269,7 @@ abstract class Builder {
269269

270270
protected Builder(OzoneConfiguration conf) {
271271
this.conf = conf;
272-
this.clusterId = UUID.randomUUID().toString();
273-
this.path = GenericTestUtils.getTempPath(
274-
MiniOzoneClusterImpl.class.getSimpleName() + "-" + clusterId);
272+
setClusterId(UUID.randomUUID().toString());
275273
}
276274

277275
/**
@@ -283,6 +281,8 @@ protected Builder(OzoneConfiguration conf) {
283281
*/
284282
public Builder setClusterId(String id) {
285283
clusterId = id;
284+
path = GenericTestUtils.getTempPath(
285+
MiniOzoneClusterImpl.class.getSimpleName() + "-" + clusterId);
286286
return this;
287287
}
288288

hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/MiniOzoneClusterImpl.java

Lines changed: 68 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import java.io.File;
2121
import java.util.ArrayList;
22+
import java.util.Collection;
23+
import java.util.Collections;
2224
import java.util.List;
2325
import java.util.Optional;
2426
import org.apache.commons.io.FileUtils;
@@ -317,6 +319,7 @@ public void shutdown() {
317319
stop();
318320
FileUtils.deleteDirectory(baseDir);
319321
ContainerCache.getInstance(conf).shutdownCache();
322+
DefaultMetricsSystem.shutdown();
320323
} catch (IOException e) {
321324
LOG.error("Exception while shutting down the cluster.", e);
322325
}
@@ -325,26 +328,9 @@ public void shutdown() {
325328
@Override
326329
public void stop() {
327330
LOG.info("Stopping the Mini Ozone Cluster");
328-
if (ozoneManager != null) {
329-
LOG.info("Stopping the OzoneManager");
330-
ozoneManager.stop();
331-
ozoneManager.join();
332-
}
333-
334-
if (!hddsDatanodes.isEmpty()) {
335-
LOG.info("Shutting the HddsDatanodes");
336-
hddsDatanodes.parallelStream()
337-
.forEach(dn -> {
338-
dn.stop();
339-
dn.join();
340-
});
341-
}
342-
343-
if (scm != null) {
344-
LOG.info("Stopping the StorageContainerManager");
345-
scm.stop();
346-
scm.join();
347-
}
331+
stopOM(ozoneManager);
332+
stopDatanodes(hddsDatanodes);
333+
stopSCM(scm);
348334
}
349335

350336
/**
@@ -385,6 +371,37 @@ private void setCAClient(CertificateClient client) {
385371
this.caClient = client;
386372
}
387373

374+
private static void stopDatanodes(
375+
Collection<HddsDatanodeService> hddsDatanodes) {
376+
if (!hddsDatanodes.isEmpty()) {
377+
LOG.info("Stopping the HddsDatanodes");
378+
hddsDatanodes.parallelStream()
379+
.forEach(MiniOzoneClusterImpl::stopDatanode);
380+
}
381+
}
382+
383+
private static void stopDatanode(HddsDatanodeService dn) {
384+
if (dn != null) {
385+
dn.stop();
386+
dn.join();
387+
}
388+
}
389+
390+
private static void stopSCM(StorageContainerManager scm) {
391+
if (scm != null) {
392+
LOG.info("Stopping the StorageContainerManager");
393+
scm.stop();
394+
scm.join();
395+
}
396+
}
397+
398+
private static void stopOM(OzoneManager om) {
399+
if (om != null) {
400+
LOG.info("Stopping the OzoneManager");
401+
om.stop();
402+
om.join();
403+
}
404+
}
388405

389406
/**
390407
* Builder for configuring the MiniOzoneCluster to run.
@@ -404,28 +421,42 @@ public Builder(OzoneConfiguration conf) {
404421
public MiniOzoneCluster build() throws IOException {
405422
DefaultMetricsSystem.setMiniClusterMode(true);
406423
initializeConfiguration();
407-
StorageContainerManager scm;
408-
OzoneManager om;
424+
StorageContainerManager scm = null;
425+
OzoneManager om = null;
426+
List<HddsDatanodeService> hddsDatanodes = Collections.emptyList();
409427
try {
410428
scm = createSCM();
411429
scm.start();
412430
om = createOM();
413431
if(certClient != null) {
414432
om.setCertClient(certClient);
415433
}
416-
} catch (AuthenticationException ex) {
417-
throw new IOException("Unable to build MiniOzoneCluster. ", ex);
418-
}
434+
om.start();
435+
436+
hddsDatanodes = createHddsDatanodes(scm);
437+
MiniOzoneClusterImpl cluster = new MiniOzoneClusterImpl(conf, om, scm,
438+
hddsDatanodes);
439+
cluster.setCAClient(certClient);
440+
if (startDataNodes) {
441+
cluster.startHddsDatanodes();
442+
}
443+
return cluster;
444+
} catch (Exception ex) {
445+
stopOM(om);
446+
if (startDataNodes) {
447+
stopDatanodes(hddsDatanodes);
448+
}
449+
stopSCM(scm);
450+
removeConfiguration();
419451

420-
om.start();
421-
final List<HddsDatanodeService> hddsDatanodes = createHddsDatanodes(scm);
422-
MiniOzoneClusterImpl cluster = new MiniOzoneClusterImpl(conf, om, scm,
423-
hddsDatanodes);
424-
cluster.setCAClient(certClient);
425-
if (startDataNodes) {
426-
cluster.startHddsDatanodes();
452+
if (ex instanceof IOException) {
453+
throw (IOException) ex;
454+
}
455+
if (ex instanceof RuntimeException) {
456+
throw (RuntimeException) ex;
457+
}
458+
throw new IOException("Unable to build MiniOzoneCluster. ", ex);
427459
}
428-
return cluster;
429460
}
430461

431462
/**
@@ -466,6 +497,10 @@ void initializeConfiguration() throws IOException {
466497
configureTrace();
467498
}
468499

500+
void removeConfiguration() {
501+
FileUtils.deleteQuietly(new File(path));
502+
}
503+
469504
/**
470505
* Creates a new StorageContainerManager instance.
471506
*

hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/TestMiniOzoneCluster.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.HashSet;
3636
import java.util.List;
3737

38+
import org.apache.commons.io.FileUtils;
3839
import org.apache.commons.lang3.RandomUtils;
3940
import org.apache.hadoop.conf.Configuration;
4041
import org.apache.hadoop.hdds.HddsConfigKeys;
@@ -53,6 +54,7 @@
5354
import org.apache.hadoop.ozone.container.ozoneimpl.TestOzoneContainer;
5455
import org.apache.hadoop.test.PathUtils;
5556
import org.apache.hadoop.test.TestGenericTestUtils;
57+
import org.junit.After;
5658
import org.junit.AfterClass;
5759
import org.junit.Assert;
5860
import org.junit.BeforeClass;
@@ -64,7 +66,7 @@
6466
*/
6567
public class TestMiniOzoneCluster {
6668

67-
private static MiniOzoneCluster cluster;
69+
private MiniOzoneCluster cluster;
6870
private static OzoneConfiguration conf;
6971

7072
private final static File TEST_ROOT = TestGenericTestUtils.getTestDir();
@@ -78,17 +80,21 @@ public static void setup() {
7880
conf.setBoolean(DFS_CONTAINER_RATIS_IPC_RANDOM_PORT, true);
7981
WRITE_TMP.mkdirs();
8082
READ_TMP.mkdirs();
81-
WRITE_TMP.deleteOnExit();
82-
READ_TMP.deleteOnExit();
8383
}
8484

85-
@AfterClass
86-
public static void cleanup() {
85+
@After
86+
public void cleanup() {
8787
if (cluster != null) {
8888
cluster.shutdown();
8989
}
9090
}
9191

92+
@AfterClass
93+
public static void afterClass() {
94+
FileUtils.deleteQuietly(WRITE_TMP);
95+
FileUtils.deleteQuietly(READ_TMP);
96+
}
97+
9298
@Test(timeout = 30000)
9399
public void testStartMultipleDatanodes() throws Exception {
94100
final int numberOfNodes = 3;

0 commit comments

Comments
 (0)