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 e4fb1cc6..bc990178 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendConnection.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendConnection.java @@ -39,19 +39,32 @@ import java.util.concurrent.Executor; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; +import java.util.logging.*; import java.util.function.Consumer; import java.util.logging.Level; import java.util.logging.Logger; import java.util.zip.GZIPOutputStream; import static com.databend.client.ClientSettings.*; -import static com.databend.client.DatabendClientV1.*; import static com.google.common.base.Preconditions.checkState; import static java.util.Collections.newSetFromMap; import static java.util.Objects.requireNonNull; public class DatabendConnection implements Connection, FileTransferAPI, Consumer { private static final Logger logger = Logger.getLogger(DatabendConnection.class.getPackage().getName()); + private static final FileHandler FILE_HANDLER; + + static { + try { + FILE_HANDLER = new FileHandler("databend-jdbc-debug.log"); + FILE_HANDLER.setLevel(Level.ALL); + FILE_HANDLER.setFormatter(new SimpleFormatter()); + 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; @@ -512,6 +525,10 @@ public Boolean useVerify() { return this.driverUri.getUseVerify(); } + public Boolean debug() { + return this.driverUri.getDebug(); + } + public String tenant() { return this.driverUri.getTenant(); } @@ -668,12 +685,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); 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 |