From 79bbaa58c796e5d82df966f39a812c9fcf877288 Mon Sep 17 00:00:00 2001 From: hantmac Date: Tue, 23 Apr 2024 09:57:57 +0800 Subject: [PATCH 1/3] feat: add debug flag in DSN --- .../databend/jdbc/ConnectionProperties.java | 10 ++++++ .../com/databend/jdbc/DatabendConnection.java | 36 +++++++++++++++++-- .../com/databend/jdbc/DatabendDriverUri.java | 6 ++++ .../jdbc/parser/BatchInsertUtils.java | 2 +- 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/ConnectionProperties.java b/databend-jdbc/src/main/java/com/databend/jdbc/ConnectionProperties.java index 40eddb03..e6a21e36 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/ConnectionProperties.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/ConnectionProperties.java @@ -17,6 +17,8 @@ public final class ConnectionProperties { public static final ConnectionProperty SSL = new Ssl(); public static final ConnectionProperty USE_VERIFY = new UseVerify(); + + public static final ConnectionProperty DEBUG = new Debug(); public static final ConnectionProperty STRNULL_AS_NULL = new StrNullAsNull(); public static final ConnectionProperty WAREHOUSE = new Warehouse(); public static final ConnectionProperty SSL_MODE = new SSLMode(); @@ -43,6 +45,7 @@ public final class ConnectionProperties { .add(PASSWORD) .add(SSL) .add(USE_VERIFY) + .add(DEBUG) .add(STRNULL_AS_NULL) .add(WAREHOUSE) .add(SSL_MODE) @@ -96,6 +99,13 @@ public UseVerify() { } } + public static class Debug + extends AbstractConnectionProperty { + public Debug() { + super("debug", Optional.of("false"), NOT_REQUIRED, ALLOWED, BOOLEAN_CONVERTER); + } + } + public static class StrNullAsNull extends AbstractConnectionProperty { public StrNullAsNull() { diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendConnection.java b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendConnection.java index 66289228..d9ca6df6 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendConnection.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendConnection.java @@ -39,8 +39,7 @@ import java.util.concurrent.Executor; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; -import java.util.logging.Level; -import java.util.logging.Logger; +import java.util.logging.*; import java.util.zip.GZIPOutputStream; import static com.databend.client.ClientSettings.*; @@ -51,6 +50,23 @@ public class DatabendConnection implements Connection, FileTransferAPI { private static final Logger logger = Logger.getLogger(DatabendConnection.class.getPackage().getName()); + private static final FileHandler FILE_HANDLER; + + static { + try { + // 创建一个 FileHandler + FILE_HANDLER = new FileHandler("databend-jdbc-debug.log"); + // 设置 FileHandler 级别为 ALL + FILE_HANDLER.setLevel(Level.ALL); + // 设置 FileHandler 的格式化器为 SimpleFormatter + FILE_HANDLER.setFormatter(new SimpleFormatter()); + // 添加 FileHandler 到 Logger + logger.addHandler(FILE_HANDLER); + } catch (Exception e) { + throw new RuntimeException("Failed to create FileHandler", e); + } + } + private final AtomicBoolean closed = new AtomicBoolean(); private final AtomicBoolean autoCommit = new AtomicBoolean(true); private final URI httpUri; @@ -511,6 +527,10 @@ public Boolean useVerify() { return this.driverUri.getUseVerify(); } + public Boolean debug() { + return this.driverUri.getDebug(); + } + public String tenant() { return this.driverUri.getTenant(); } @@ -663,12 +683,22 @@ public void uploadStream(String stageName, String destPrefix, InputStream inputS DatabendPresignClient cli = new DatabendPresignClientV1(httpClient, this.httpUri.toString()); cli.presignUpload(null, dataStream, s, p + "/", destFileName, fileSize, true); } else { - logger.log(Level.FINE, "presign to @" + s + "/" + dest); +// logger.log(Level.FINE, "presign to @" + s + "/" + dest); + long presignStartTime = System.nanoTime(); PresignContext ctx = PresignContext.getPresignContext(this, PresignContext.PresignMethod.UPLOAD, s, dest); + long presignEndTime = System.nanoTime(); + if (this.debug()) { + logger.info("presign cost time: " + (presignEndTime - presignStartTime) / 1000000.0 + "ms"); + } Headers h = ctx.getHeaders(); String presignUrl = ctx.getUrl(); DatabendPresignClient cli = new DatabendPresignClientV1(new OkHttpClient(), this.httpUri.toString()); + long uploadStartTime = System.nanoTime(); cli.presignUpload(null, dataStream, h, presignUrl, fileSize, true); + long uploadEndTime = System.nanoTime(); + if (this.debug()) { + logger.info("upload cost time: " + (uploadEndTime - uploadStartTime) / 1000000.0 + "ms"); + } } } catch (JsonProcessingException e) { System.out.println(e.getMessage()); diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDriverUri.java b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDriverUri.java index 199d466f..8719eeb4 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDriverUri.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDriverUri.java @@ -41,6 +41,7 @@ public final class DatabendDriverUri { private final URI uri; private final boolean useSecureConnection; private final boolean useVerify; + private final boolean debug; private final boolean strNullAsNull; private final String warehouse; private final String sslmode; @@ -65,6 +66,7 @@ private DatabendDriverUri(String url, Properties driverProperties) this.properties = mergeProperties(uriAndProperties.getKey(), uriAndProperties.getValue(), driverProperties); this.useSecureConnection = SSL.getValue(properties).orElse(false); this.useVerify = USE_VERIFY.getValue(properties).orElse(false); + this.debug = DEBUG.getValue(properties).orElse(false); this.strNullAsNull = STRNULL_AS_NULL.getValue(properties).orElse(true); this.warehouse = WAREHOUSE.getValue(properties).orElse(""); this.sslmode = SSL_MODE.getValue(properties).orElse("disable"); @@ -270,6 +272,10 @@ public boolean getUseVerify() { return useVerify; } + public boolean getDebug() { + return debug; + } + public String getSslmode() { return sslmode; } diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/parser/BatchInsertUtils.java b/databend-jdbc/src/main/java/com/databend/jdbc/parser/BatchInsertUtils.java index 19da42e4..fe7f2dd6 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/parser/BatchInsertUtils.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/parser/BatchInsertUtils.java @@ -115,7 +115,7 @@ public File saveBatchToCSV(List values, File file) { for (String[] row : values) { w.writeRow(row); } - logger.log(Level.FINE, "save batch insert to csv file: " + file.getAbsolutePath() + "rows: " + values.size() + " columns: " + rowSize); +// logger.log(Level.FINE, "save batch insert to csv file: " + file.getAbsolutePath() + "rows: " + values.size() + " columns: " + rowSize); return file; } catch (IOException e) { throw new RuntimeException(e); From 41703ff43c8580a41b8bf5406a44a31942b01d93 Mon Sep 17 00:00:00 2001 From: hantmac Date: Tue, 23 Apr 2024 09:59:13 +0800 Subject: [PATCH 2/3] remove comment --- .../src/main/java/com/databend/jdbc/DatabendConnection.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendConnection.java b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendConnection.java index d9ca6df6..a5113a32 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendConnection.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendConnection.java @@ -54,13 +54,9 @@ public class DatabendConnection implements Connection, FileTransferAPI { static { try { - // 创建一个 FileHandler FILE_HANDLER = new FileHandler("databend-jdbc-debug.log"); - // 设置 FileHandler 级别为 ALL FILE_HANDLER.setLevel(Level.ALL); - // 设置 FileHandler 的格式化器为 SimpleFormatter FILE_HANDLER.setFormatter(new SimpleFormatter()); - // 添加 FileHandler 到 Logger logger.addHandler(FILE_HANDLER); } catch (Exception e) { throw new RuntimeException("Failed to create FileHandler", e); From 0527242340230d18beb3c1193d36039532e8d3eb Mon Sep 17 00:00:00 2001 From: hantmac Date: Tue, 23 Apr 2024 09:59:58 +0800 Subject: [PATCH 3/3] add docs --- docs/Connection.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/Connection.md b/docs/Connection.md index a60f47c9..f26aee28 100644 --- a/docs/Connection.md +++ b/docs/Connection.md @@ -91,3 +91,4 @@ String url="jdbc:databend://databend:secret@0.0.0.0:8000/hello_databend"; | null_display | null value display | \N | jdbc:databend://0.0.0.0:8000/hello_databend?null_display=null | | binary_format | binary format, support hex and base64 | hex | jdbc:databend://0.0.0.0:8000/default?binary_format=hex | | use_verify | whether verify the server before establishing the connection | true | jdbc:databend://0.0.0.0:8000/default?use_verify=true | +| debug | whether enable debug mode | false | jdbc:databend://0.0.0.0:8000/default?debug=true |