Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offload ID generation to ClickHouseRequestManager #1074

Merged
merged 2 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
Expand Down Expand Up @@ -209,7 +208,7 @@ static Process startProcess(ClickHouseRequest<?> request) {
if (!tableFile.isAvailable() || !tableFile.getFile().getAbsolutePath().startsWith(hostDir)) {
// creating a hard link is faster but it's not platform-independent
File f = ClickHouseInputStream.save(
Paths.get(hostDir, "chc_".concat(UUID.randomUUID().toString())).toFile(),
Paths.get(hostDir, "chc_".concat(request.getManager().createUniqueId())).toFile(),
table.getContent(), config.getWriteBufferSize(), config.getSocketTimeout(), true);
filePath = containerDir.concat(f.getName());
} else {
Expand Down Expand Up @@ -275,13 +274,13 @@ static Process startProcess(ClickHouseRequest<?> request) {
String fileName = f.getName();
int len = fileName.length();
int index = fileName.indexOf('.', 1);
String uuid = UUID.randomUUID().toString();
String uuid = request.getManager().createUniqueId();
if (index > 0 && index + 1 < len) {
fileName = new StringBuilder(len + uuid.length() + 1).append(fileName.substring(0, index))
.append('_').append(uuid).append(fileName.substring(index)).toString();
} else {
fileName = new StringBuilder(len + uuid.length() + 1).append(fileName).append('_')
.append(UUID.randomUUID().toString()).toString();
.append(request.getManager().createUniqueId()).toString();
}
Path newPath = Paths.get(hostDir, fileName);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,34 @@ public static ClickHouseRequestManager getInstance() {
}

/**
* Creates a new query ID.
* Creates a new query ID. By default, it simply forwards the call to
* {@link #createUniqueId()}.
*
* @return non-null query ID
* @return non-null unique query ID
*/
public String createQueryId() {
return UUID.randomUUID().toString();
return createUniqueId();
}

/**
* Creates a new session ID.
* Creates a new session ID. By default, it simply forwards the call to
* {@link #createUniqueId()}.
*
* @return non-null session ID
* @return non-null unique session ID
*/
public String createSessionId() {
return createUniqueId();
}

/**
* Creates a global, URL-safe unique ID. By default it uses
* {@link UUID#randomUUID()} for least dependency, but you can override this
* method to use any other alternatives like TimeUUID, snowflake and maybe
* NanoID.
*
* @return non-empty global unique ID
*/
public String createUniqueId() {
return UUID.randomUUID().toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.Map.Entry;
import java.util.concurrent.CompletionException;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -130,7 +129,7 @@ protected static QueryInfo convert(ClickHouseRequest<?> request) {
}

if (ClickHouseChecker.isNullOrEmpty(builder.getSessionId())) {
builder.setSessionId(UUID.randomUUID().toString());
builder.setSessionId(request.getManager().createSessionId());
}

// builder.getSessionTimeout()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.clickhouse.client.ClickHouseNode;
import com.clickhouse.client.ClickHouseOutputStream;
import com.clickhouse.client.ClickHouseRequest;
import com.clickhouse.client.ClickHouseRequestManager;
import com.clickhouse.client.ClickHouseUtils;
import com.clickhouse.client.config.ClickHouseClientOption;
import com.clickhouse.client.config.ClickHouseOption;
Expand Down Expand Up @@ -214,6 +215,7 @@ protected static Map<String, String> createDefaultHeaders(ClickHouseConfig confi
protected final ClickHouseOutputStream output;
protected final String url;
protected final Map<String, String> defaultHeaders;
protected final ClickHouseRequestManager rm;

protected ClickHouseHttpConnection(ClickHouseNode server, ClickHouseRequest<?> request) {
if (server == null || request == null) {
Expand All @@ -225,6 +227,7 @@ protected ClickHouseHttpConnection(ClickHouseNode server, ClickHouseRequest<?> r
this.output = request.getOutputStream().orElse(null);
this.url = buildUrl(server.getBaseUri(), request);
this.defaultHeaders = Collections.unmodifiableMap(createDefaultHeaders(config, server));
this.rm = request.getManager();
}

protected void closeQuietly() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ protected ClickHouseHttpResponse post(String sql, ClickHouseInputStream data, Li
Charset ascii = StandardCharsets.US_ASCII;
byte[] boundary = null;
if (tables != null && !tables.isEmpty()) {
String uuid = UUID.randomUUID().toString();
String uuid = rm.createUniqueId();
conn.setRequestProperty("content-type", "multipart/form-data; boundary=".concat(uuid));
boundary = uuid.getBytes(ascii);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ protected ClickHouseHttpResponse post(String sql, ClickHouseInputStream data, Li
.timeout(Duration.ofMillis(c.getSocketTimeout()));
byte[] boundary = null;
if (tables != null && !tables.isEmpty()) {
String uuid = UUID.randomUUID().toString();
String uuid = rm.createUniqueId();
reqBuilder.setHeader("content-type", "multipart/form-data; boundary=" + uuid);
boundary = uuid.getBytes(StandardCharsets.US_ASCII);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

import com.clickhouse.client.ClickHouseChecker;
import com.clickhouse.client.ClickHouseException;
Expand All @@ -32,7 +31,7 @@ public class JdbcTransaction {

public JdbcTransaction(ClickHouseTransaction tx) {
this.tx = tx;
this.id = tx != null ? tx.getId().asTupleString() : UUID.randomUUID().toString();
this.id = tx != null ? tx.getId().asTupleString() : ClickHouseRequestManager.getInstance().createUniqueId();
this.queries = new LinkedList<>();
this.savepoints = new LinkedList<>();
}
Expand Down