From a5a6036883d17dea822fb0cc343b2819485a7e92 Mon Sep 17 00:00:00 2001 From: tedyu Date: Wed, 14 Dec 2016 20:03:44 -0800 Subject: [PATCH] HBASE-17309 Fix connection leaks in TestAcidGuarantees (huaxiang sun) --- .../hadoop/hbase/MultithreadedTestUtil.java | 10 ++++-- .../hadoop/hbase/TestAcidGuarantees.java | 36 +++++++++++++++++-- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/MultithreadedTestUtil.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/MultithreadedTestUtil.java index cea10ebde0e..7e251e77ecd 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/MultithreadedTestUtil.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/MultithreadedTestUtil.java @@ -18,6 +18,7 @@ */ package org.apache.hadoop.hbase; +import java.io.IOException; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -143,12 +144,17 @@ public abstract class MultithreadedTestUtil { } public final void doWork() throws Exception { - while (ctx.shouldRun() && !stopped) { - doAnAction(); + try { + while (ctx.shouldRun() && !stopped) { + doAnAction(); + } + } finally { + workDone(); } } public abstract void doAnAction() throws Exception; + public void workDone() throws IOException {} } /** diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/TestAcidGuarantees.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/TestAcidGuarantees.java index e6c71242f2a..0c86145f061 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/TestAcidGuarantees.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/TestAcidGuarantees.java @@ -118,6 +118,7 @@ public class TestAcidGuarantees implements Tool { byte data[] = new byte[10]; byte targetRows[][]; byte targetFamilies[][]; + Connection connection; Table table; AtomicLong numWritten = new AtomicLong(); @@ -126,7 +127,7 @@ public class TestAcidGuarantees implements Tool { super(ctx); this.targetRows = targetRows; this.targetFamilies = targetFamilies; - Connection connection = ConnectionFactory.createConnection(ctx.getConf()); + connection = ConnectionFactory.createConnection(ctx.getConf()); table = connection.getTable(TABLE_NAME); } public void doAnAction() throws Exception { @@ -144,6 +145,15 @@ public class TestAcidGuarantees implements Tool { table.put(p); numWritten.getAndIncrement(); } + + @Override + public void workDone() throws IOException { + try { + table.close(); + } finally { + connection.close(); + } + } } /** @@ -153,6 +163,7 @@ public class TestAcidGuarantees implements Tool { public static class AtomicGetReader extends RepeatingTestThread { byte targetRow[]; byte targetFamilies[][]; + Connection connection; Table table; int numVerified = 0; AtomicLong numRead = new AtomicLong(); @@ -162,7 +173,7 @@ public class TestAcidGuarantees implements Tool { super(ctx); this.targetRow = targetRow; this.targetFamilies = targetFamilies; - Connection connection = ConnectionFactory.createConnection(ctx.getConf()); + connection = ConnectionFactory.createConnection(ctx.getConf()); table = connection.getTable(TABLE_NAME); } @@ -191,6 +202,15 @@ public class TestAcidGuarantees implements Tool { numRead.getAndIncrement(); } + @Override + public void workDone() throws IOException { + try { + table.close(); + } finally { + connection.close(); + } + } + private void gotFailure(byte[] expected, Result res) { StringBuilder msg = new StringBuilder(); msg.append("Failed after ").append(numVerified).append("!"); @@ -213,6 +233,7 @@ public class TestAcidGuarantees implements Tool { public static class AtomicScanReader extends RepeatingTestThread { byte targetFamilies[][]; Table table; + Connection connection; AtomicLong numScans = new AtomicLong(); AtomicLong numRowsScanned = new AtomicLong(); @@ -220,7 +241,7 @@ public class TestAcidGuarantees implements Tool { byte targetFamilies[][]) throws IOException { super(ctx); this.targetFamilies = targetFamilies; - Connection connection = ConnectionFactory.createConnection(ctx.getConf()); + connection = ConnectionFactory.createConnection(ctx.getConf()); table = connection.getTable(TABLE_NAME); } @@ -249,6 +270,15 @@ public class TestAcidGuarantees implements Tool { numScans.getAndIncrement(); } + @Override + public void workDone() throws IOException { + try { + table.close(); + } finally { + connection.close(); + } + } + private void gotFailure(byte[] expected, Result res) { StringBuilder msg = new StringBuilder(); msg.append("Failed after ").append(numRowsScanned).append("!");