Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 240F1200C4E for ; Thu, 6 Apr 2017 09:28:20 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 22E55160B83; Thu, 6 Apr 2017 07:28:20 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 02DA2160BA4 for ; Thu, 6 Apr 2017 09:28:18 +0200 (CEST) Received: (qmail 7812 invoked by uid 500); 6 Apr 2017 07:28:18 -0000 Mailing-List: contact commits-help@flink.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@flink.apache.org Delivered-To: mailing list commits@flink.apache.org Received: (qmail 5779 invoked by uid 99); 6 Apr 2017 07:28:15 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 06 Apr 2017 07:28:15 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 28DB0E8E72; Thu, 6 Apr 2017 07:28:15 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: fhueske@apache.org To: commits@flink.apache.org Date: Thu, 06 Apr 2017 07:28:43 -0000 Message-Id: <4c6fc89984274ca9a2b6bae8eda067a5@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [30/37] flink git commit: [hotfix] [py] Code cleanup - StreamPrinter archived-at: Thu, 06 Apr 2017 07:28:20 -0000 [hotfix] [py] Code cleanup - StreamPrinter - implements Runnable instead of extending Thread - use AtomicRefence instead of StringBuilder - remove redundant wrapInException argument Project: http://git-wip-us.apache.org/repos/asf/flink/repo Commit: http://git-wip-us.apache.org/repos/asf/flink/commit/258ed179 Tree: http://git-wip-us.apache.org/repos/asf/flink/tree/258ed179 Diff: http://git-wip-us.apache.org/repos/asf/flink/diff/258ed179 Branch: refs/heads/table-retraction Commit: 258ed1791a3e66443b29e4a4e14dcc4e03ffd9e6 Parents: f4324ba Author: zentol Authored: Thu Mar 30 19:41:54 2017 +0200 Committer: zentol Committed: Wed Apr 5 20:43:55 2017 +0200 ---------------------------------------------------------------------- .../api/streaming/data/PythonStreamer.java | 7 +++-- .../api/streaming/plan/PythonPlanStreamer.java | 4 +-- .../api/streaming/util/StreamPrinter.java | 30 ++++++++++++-------- 3 files changed, 24 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flink/blob/258ed179/flink-libraries/flink-python/src/main/java/org/apache/flink/python/api/streaming/data/PythonStreamer.java ---------------------------------------------------------------------- diff --git a/flink-libraries/flink-python/src/main/java/org/apache/flink/python/api/streaming/data/PythonStreamer.java b/flink-libraries/flink-python/src/main/java/org/apache/flink/python/api/streaming/data/PythonStreamer.java index 219ae27..3c79b1f 100644 --- a/flink-libraries/flink-python/src/main/java/org/apache/flink/python/api/streaming/data/PythonStreamer.java +++ b/flink-libraries/flink-python/src/main/java/org/apache/flink/python/api/streaming/data/PythonStreamer.java @@ -32,6 +32,7 @@ import java.net.ServerSocket; import java.net.Socket; import java.net.SocketTimeoutException; import java.util.Iterator; +import java.util.concurrent.atomic.AtomicReference; import static org.apache.flink.python.api.PythonPlanBinder.FLINK_PYTHON2_BINARY_PATH; import static org.apache.flink.python.api.PythonPlanBinder.FLINK_PYTHON3_BINARY_PATH; @@ -71,7 +72,7 @@ public class PythonStreamer implements Serializable protected S sender; protected PythonReceiver receiver; - protected StringBuilder msg = new StringBuilder(); + protected AtomicReference msg = new AtomicReference<>(); protected final AbstractRichFunction function; @@ -118,9 +119,9 @@ public class PythonStreamer implements Serializable } process = Runtime.getRuntime().exec(pythonBinaryPath + " -O -B " + planPath + planArguments); - outPrinter = new StreamPrinter(process.getInputStream()); + outPrinter = new Thread(new StreamPrinter(process.getInputStream())); outPrinter.start(); - errorPrinter = new StreamPrinter(process.getErrorStream(), true, msg); + errorPrinter = new Thread(new StreamPrinter(process.getErrorStream(), msg)); errorPrinter.start(); shutdownThread = new Thread() { http://git-wip-us.apache.org/repos/asf/flink/blob/258ed179/flink-libraries/flink-python/src/main/java/org/apache/flink/python/api/streaming/plan/PythonPlanStreamer.java ---------------------------------------------------------------------- diff --git a/flink-libraries/flink-python/src/main/java/org/apache/flink/python/api/streaming/plan/PythonPlanStreamer.java b/flink-libraries/flink-python/src/main/java/org/apache/flink/python/api/streaming/plan/PythonPlanStreamer.java index 4eb0f51..9b62563 100644 --- a/flink-libraries/flink-python/src/main/java/org/apache/flink/python/api/streaming/plan/PythonPlanStreamer.java +++ b/flink-libraries/flink-python/src/main/java/org/apache/flink/python/api/streaming/plan/PythonPlanStreamer.java @@ -67,8 +67,8 @@ public class PythonPlanStreamer { } process = Runtime.getRuntime().exec(pythonBinaryPath + " -B " + tmpPath + FLINK_PYTHON_PLAN_NAME + args); - new StreamPrinter(process.getInputStream()).start(); - new StreamPrinter(process.getErrorStream()).start(); + new Thread(new StreamPrinter(process.getInputStream())).start(); + new Thread(new StreamPrinter(process.getErrorStream())).start(); server = new ServerSocket(0); server.setSoTimeout(50); http://git-wip-us.apache.org/repos/asf/flink/blob/258ed179/flink-libraries/flink-python/src/main/java/org/apache/flink/python/api/streaming/util/StreamPrinter.java ---------------------------------------------------------------------- diff --git a/flink-libraries/flink-python/src/main/java/org/apache/flink/python/api/streaming/util/StreamPrinter.java b/flink-libraries/flink-python/src/main/java/org/apache/flink/python/api/streaming/util/StreamPrinter.java index 30a728c..c6a1ede 100644 --- a/flink-libraries/flink-python/src/main/java/org/apache/flink/python/api/streaming/util/StreamPrinter.java +++ b/flink-libraries/flink-python/src/main/java/org/apache/flink/python/api/streaming/util/StreamPrinter.java @@ -18,40 +18,46 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.util.concurrent.atomic.AtomicReference; /** * Simple utility class to print all contents of an inputstream to stdout. */ -public class StreamPrinter extends Thread { +public class StreamPrinter implements Runnable { private final BufferedReader reader; - private final boolean wrapInException; - private StringBuilder msg; + private final AtomicReference output; public StreamPrinter(InputStream stream) { - this(stream, false, null); + this(stream, null); } - public StreamPrinter(InputStream stream, boolean wrapInException, StringBuilder msg) { + public StreamPrinter(InputStream stream, AtomicReference output) { this.reader = new BufferedReader(new InputStreamReader(stream, ConfigConstants.DEFAULT_CHARSET)); - this.wrapInException = wrapInException; - this.msg = msg; + this.output = output; } @Override public void run() { String line; - try { - if (wrapInException) { + if (output != null) { + StringBuilder msg = new StringBuilder(); + try { while ((line = reader.readLine()) != null) { - msg.append("\n" + line); + msg.append(line); + msg.append("\n"); } - } else { + } catch (IOException ignored) { + } finally { + output.set(msg.toString()); + } + } else { + try { while ((line = reader.readLine()) != null) { System.out.println(line); System.out.flush(); } + } catch (IOException ignored) { } - } catch (IOException ex) { } } }