From 4d2d052b7db1b440a2f2da6c9ff7e89d031a5608 Mon Sep 17 00:00:00 2001 From: Tomas Fernandez Lobbe Date: Tue, 3 Mar 2020 11:42:20 -0800 Subject: [PATCH] Fix resource leak in TestPolicyCloud (#1312) --- .../cloud/autoscaling/TestPolicyCloud.java | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/solr/core/src/test/org/apache/solr/cloud/autoscaling/TestPolicyCloud.java b/solr/core/src/test/org/apache/solr/cloud/autoscaling/TestPolicyCloud.java index 5d7981f51ad..61c79bc13c1 100644 --- a/solr/core/src/test/org/apache/solr/cloud/autoscaling/TestPolicyCloud.java +++ b/solr/core/src/test/org/apache/solr/cloud/autoscaling/TestPolicyCloud.java @@ -145,34 +145,34 @@ public class TestPolicyCloud extends SolrCloudTestCase { "}"; AutoScalingConfig config = new AutoScalingConfig((Map) Utils.fromJSONString(autoScaleJson)); AtomicInteger count = new AtomicInteger(0); - SolrCloudManager cloudManager = new SolrClientCloudManager(new ZkDistributedQueueFactory(cluster.getZkClient()), cluster.getSolrClient()); - String nodeName = cloudManager.getClusterStateProvider().getLiveNodes().iterator().next(); - SolrClientNodeStateProvider nodeStateProvider = (SolrClientNodeStateProvider) cloudManager.getNodeStateProvider(); - Map>> result = nodeStateProvider.getReplicaInfo(nodeName, Collections.singleton("UPDATE./update.requests")); - nodeStateProvider.forEachReplica(nodeName, replicaInfo -> { - if (replicaInfo.getVariables().containsKey("UPDATE./update.requests")) count.incrementAndGet(); - }); - assertTrue(count.get() > 0); + try (SolrCloudManager cloudManager = new SolrClientCloudManager(new ZkDistributedQueueFactory(cluster.getZkClient()), cluster.getSolrClient())) { + String nodeName = cloudManager.getClusterStateProvider().getLiveNodes().iterator().next(); + SolrClientNodeStateProvider nodeStateProvider = (SolrClientNodeStateProvider) cloudManager.getNodeStateProvider(); + Map>> result = nodeStateProvider.getReplicaInfo(nodeName, Collections.singleton("UPDATE./update.requests")); + nodeStateProvider.forEachReplica(nodeName, replicaInfo -> { + if (replicaInfo.getVariables().containsKey("UPDATE./update.requests")) count.incrementAndGet(); + }); + assertTrue(count.get() > 0); - Policy.Session session = config.getPolicy().createSession(cloudManager); + Policy.Session session = config.getPolicy().createSession(cloudManager); - for (Row row : session.getSortedNodes()) { - Object val = row.getVal(Type.TOTALDISK.tagName, null); - log.info("node: {} , totaldisk : {}, freedisk : {}", row.node, val, row.getVal("freedisk",null)); - assertTrue(val != null); + for (Row row : session.getSortedNodes()) { + Object val = row.getVal(Type.TOTALDISK.tagName, null); + log.info("node: {} , totaldisk : {}, freedisk : {}", row.node, val, row.getVal("freedisk",null)); + assertTrue(val != null); + } + + count .set(0); + for (Row row : session.getSortedNodes()) { + row.collectionVsShardVsReplicas.forEach((c, shardVsReplicas) -> shardVsReplicas.forEach((s, replicaInfos) -> { + for (ReplicaInfo replicaInfo : replicaInfos) { + if (replicaInfo.getVariables().containsKey(Type.CORE_IDX.tagName)) count.incrementAndGet(); + } + })); + } + assertTrue(count.get() > 0); } - - count .set(0); - for (Row row : session.getSortedNodes()) { - row.collectionVsShardVsReplicas.forEach((c, shardVsReplicas) -> shardVsReplicas.forEach((s, replicaInfos) -> { - for (ReplicaInfo replicaInfo : replicaInfos) { - if (replicaInfo.getVariables().containsKey(Type.CORE_IDX.tagName)) count.incrementAndGet(); - } - })); - } - assertTrue(count.get() > 0); - } private static CollectionStatePredicate expectAllReplicasOnSpecificNode