Skip to content

Commit

Permalink
Merge pull request #200 from datafuselabs/feat/debug-param-dsn
Browse files Browse the repository at this point in the history
feat: add debug flag in DSN
  • Loading branch information
hantmac authored Apr 23, 2024
2 parents 6608506 + 4adb264 commit bb0d8df
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public final class ConnectionProperties {
public static final ConnectionProperty<Boolean> SSL = new Ssl();

public static final ConnectionProperty<Boolean> USE_VERIFY = new UseVerify();

public static final ConnectionProperty<Boolean> DEBUG = new Debug();
public static final ConnectionProperty<Boolean> STRNULL_AS_NULL = new StrNullAsNull();
public static final ConnectionProperty<String> WAREHOUSE = new Warehouse();
public static final ConnectionProperty<String> SSL_MODE = new SSLMode();
Expand All @@ -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)
Expand Down Expand Up @@ -96,6 +99,13 @@ public UseVerify() {
}
}

public static class Debug
extends AbstractConnectionProperty<Boolean> {
public Debug() {
super("debug", Optional.of("false"), NOT_REQUIRED, ALLOWED, BOOLEAN_CONVERTER);
}
}

public static class StrNullAsNull
extends AbstractConnectionProperty<Boolean> {
public StrNullAsNull() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<DatabendSession> {
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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand Down Expand Up @@ -270,6 +272,10 @@ public boolean getUseVerify() {
return useVerify;
}

public boolean getDebug() {
return debug;
}

public String getSslmode() {
return sslmode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public File saveBatchToCSV(List<String[]> 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);
Expand Down
1 change: 1 addition & 0 deletions docs/Connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,4 @@ String url="jdbc:databend://databend:[email protected]: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 |

0 comments on commit bb0d8df

Please sign in to comment.