You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/11/09 04:23:42 UTC

[shardingsphere] branch master updated: Refactor : refactor the CSV generator to avoid the multi-thread issue (#22028)

This is an automated email from the ASF dual-hosted git repository.

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 196e7a3d86d Refactor : refactor the CSV generator to avoid the multi-thread issue (#22028)
196e7a3d86d is described below

commit 196e7a3d86dbc181fc0925f25bdc9c3b164fafe7
Author: 孙念君 Nianjun Sun <su...@apache.org>
AuthorDate: Wed Nov 9 12:23:34 2022 +0800

    Refactor : refactor the CSV generator to avoid the multi-thread issue (#22028)
    
    * Refactor : refactor the CSV generator to avoid the multi-thread issue (#21999)
    
    * Fix : fix the checkstyle violation (#21999)
---
 .../sql/parser/env/IntegrationTestEnvironment.java  |  4 ++++
 .../sql/parser/result/CSVResultGenerator.java       | 21 ++++++++++++++++-----
 .../src/test/resources/env/it-env.properties        |  1 +
 3 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/env/IntegrationTestEnvironment.java b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/env/IntegrationTestEnvironment.java
index cde505ed78f..0d3e8554682 100644
--- a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/env/IntegrationTestEnvironment.java
+++ b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/env/IntegrationTestEnvironment.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.sql.parser.env;
 
 import lombok.Getter;
 import lombok.SneakyThrows;
+
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Properties;
@@ -32,10 +33,13 @@ public final class IntegrationTestEnvironment {
     
     private final boolean sqlParserITEnabled;
     
+    private final String resultPath;
+    
     private IntegrationTestEnvironment() {
         props = loadProperties();
         String sqlParserITEnabledStr = null == System.getProperty("sql.parser.it.enabled") ? props.get("sql.parser.it.enabled").toString() : System.getProperty("sql.parser.it.enabled");
         sqlParserITEnabled = null == sqlParserITEnabledStr ? false : Boolean.parseBoolean(sqlParserITEnabledStr);
+        resultPath = props.get("sql.parser.it.report.path").toString();
     }
     
     /**
diff --git a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/result/CSVResultGenerator.java b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/result/CSVResultGenerator.java
index 176c7afd4aa..821cb190879 100644
--- a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/result/CSVResultGenerator.java
+++ b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/result/CSVResultGenerator.java
@@ -19,10 +19,11 @@ package org.apache.shardingsphere.sql.parser.result;
 
 import org.apache.commons.csv.CSVFormat;
 import org.apache.commons.csv.CSVPrinter;
+import org.apache.shardingsphere.sql.parser.env.IntegrationTestEnvironment;
 
+import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
-import java.io.Writer;
 
 /**
  *  CSV result generator.
@@ -32,15 +33,26 @@ public final class CSVResultGenerator {
     private final CSVPrinter printer;
     
     public CSVResultGenerator(final String databaseType) {
-        CSVFormat csvFormat = CSVFormat.DEFAULT.builder().setHeader("SQLCaseId", "DatabaseType", "Result", "SQL").setSkipHeaderRecord(false).build();
         try {
-            Writer out = new FileWriter(databaseType + "-result.csv", true);
-            printer = new CSVPrinter(out, csvFormat);
+            File csvFile = new File(IntegrationTestEnvironment.getInstance().getResultPath() + databaseType + "-result.csv");
+            createHeader(csvFile);
+            printer = new CSVPrinter(new FileWriter(csvFile, true), CSVFormat.DEFAULT.builder().setSkipHeaderRecord(true).build());
         } catch (final IOException ex) {
             throw new RuntimeException("Create CSV file failed.", ex);
         }
     }
     
+    private synchronized void createHeader(final File csvFile) {
+        if (!csvFile.exists()) {
+            try (CSVPrinter printer = new CSVPrinter(new FileWriter(csvFile), CSVFormat.DEFAULT.builder().setSkipHeaderRecord(false).build())) {
+                printer.printRecord("SQLCaseId", "DatabaseType", "Result", "SQL");
+                printer.flush();
+            } catch (final IOException ex) {
+                throw new RuntimeException("Create CSV file header failed.", ex);
+            }
+        }
+    }
+    
     /**
      * Process the result.
      *
@@ -49,7 +61,6 @@ public final class CSVResultGenerator {
     public void processResult(final Object... params) {
         try {
             printer.printRecord(params);
-            // TODO this may be optimized in next step.
             printer.flush();
         } catch (final IOException ex) {
             throw new RuntimeException("Write CSV file failed.", ex);
diff --git a/test/integration-test/sql-parser/src/test/resources/env/it-env.properties b/test/integration-test/sql-parser/src/test/resources/env/it-env.properties
index 1a78fd77a4b..49c3ba632f0 100644
--- a/test/integration-test/sql-parser/src/test/resources/env/it-env.properties
+++ b/test/integration-test/sql-parser/src/test/resources/env/it-env.properties
@@ -16,3 +16,4 @@
 #
 
 sql.parser.it.enabled=false
+sql.parser.it.report.path=/tmp/