Skip to content

Commit 5d61506

Browse files
authored
Merge pull request #1074 from zhicwu/develop
Offload ID generation to ClickHouseRequestManager
2 parents 70cf5f9 + f9cc46f commit 5d61506

File tree

7 files changed

+29
-15
lines changed

7 files changed

+29
-15
lines changed

clickhouse-cli-client/src/main/java/com/clickhouse/client/cli/ClickHouseCommandLine.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.List;
2121
import java.util.Map;
2222
import java.util.Optional;
23-
import java.util.UUID;
2423
import java.util.concurrent.CancellationException;
2524
import java.util.concurrent.CompletableFuture;
2625
import java.util.concurrent.CompletionException;
@@ -209,7 +208,7 @@ static Process startProcess(ClickHouseRequest<?> request) {
209208
if (!tableFile.isAvailable() || !tableFile.getFile().getAbsolutePath().startsWith(hostDir)) {
210209
// creating a hard link is faster but it's not platform-independent
211210
File f = ClickHouseInputStream.save(
212-
Paths.get(hostDir, "chc_".concat(UUID.randomUUID().toString())).toFile(),
211+
Paths.get(hostDir, "chc_".concat(request.getManager().createUniqueId())).toFile(),
213212
table.getContent(), config.getWriteBufferSize(), config.getSocketTimeout(), true);
214213
filePath = containerDir.concat(f.getName());
215214
} else {
@@ -275,13 +274,13 @@ static Process startProcess(ClickHouseRequest<?> request) {
275274
String fileName = f.getName();
276275
int len = fileName.length();
277276
int index = fileName.indexOf('.', 1);
278-
String uuid = UUID.randomUUID().toString();
277+
String uuid = request.getManager().createUniqueId();
279278
if (index > 0 && index + 1 < len) {
280279
fileName = new StringBuilder(len + uuid.length() + 1).append(fileName.substring(0, index))
281280
.append('_').append(uuid).append(fileName.substring(index)).toString();
282281
} else {
283282
fileName = new StringBuilder(len + uuid.length() + 1).append(fileName).append('_')
284-
.append(UUID.randomUUID().toString()).toString();
283+
.append(request.getManager().createUniqueId()).toString();
285284
}
286285
Path newPath = Paths.get(hostDir, fileName);
287286
try {

clickhouse-client/src/main/java/com/clickhouse/client/ClickHouseRequestManager.java

+19-5
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,34 @@ public static ClickHouseRequestManager getInstance() {
3030
}
3131

3232
/**
33-
* Creates a new query ID.
33+
* Creates a new query ID. By default, it simply forwards the call to
34+
* {@link #createUniqueId()}.
3435
*
35-
* @return non-null query ID
36+
* @return non-null unique query ID
3637
*/
3738
public String createQueryId() {
38-
return UUID.randomUUID().toString();
39+
return createUniqueId();
3940
}
4041

4142
/**
42-
* Creates a new session ID.
43+
* Creates a new session ID. By default, it simply forwards the call to
44+
* {@link #createUniqueId()}.
4345
*
44-
* @return non-null session ID
46+
* @return non-null unique session ID
4547
*/
4648
public String createSessionId() {
49+
return createUniqueId();
50+
}
51+
52+
/**
53+
* Creates a global, URL-safe unique ID. By default it uses
54+
* {@link UUID#randomUUID()} for least dependency, but you can override this
55+
* method to use any other alternatives like TimeUUID, snowflake and maybe
56+
* NanoID.
57+
*
58+
* @return non-empty global unique ID
59+
*/
60+
public String createUniqueId() {
4761
return UUID.randomUUID().toString();
4862
}
4963

clickhouse-grpc-client/src/main/java/com/clickhouse/client/grpc/ClickHouseGrpcClient.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.util.Collections;
88
import java.util.List;
99
import java.util.Optional;
10-
import java.util.UUID;
1110
import java.util.Map.Entry;
1211
import java.util.concurrent.CompletionException;
1312
import java.util.concurrent.TimeUnit;
@@ -130,7 +129,7 @@ protected static QueryInfo convert(ClickHouseRequest<?> request) {
130129
}
131130

132131
if (ClickHouseChecker.isNullOrEmpty(builder.getSessionId())) {
133-
builder.setSessionId(UUID.randomUUID().toString());
132+
builder.setSessionId(request.getManager().createSessionId());
134133
}
135134

136135
// builder.getSessionTimeout()

clickhouse-http-client/src/main/java/com/clickhouse/client/http/ClickHouseHttpConnection.java

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.clickhouse.client.ClickHouseNode;
2323
import com.clickhouse.client.ClickHouseOutputStream;
2424
import com.clickhouse.client.ClickHouseRequest;
25+
import com.clickhouse.client.ClickHouseRequestManager;
2526
import com.clickhouse.client.ClickHouseUtils;
2627
import com.clickhouse.client.config.ClickHouseClientOption;
2728
import com.clickhouse.client.config.ClickHouseOption;
@@ -214,6 +215,7 @@ protected static Map<String, String> createDefaultHeaders(ClickHouseConfig confi
214215
protected final ClickHouseOutputStream output;
215216
protected final String url;
216217
protected final Map<String, String> defaultHeaders;
218+
protected final ClickHouseRequestManager rm;
217219

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

230233
protected void closeQuietly() {

clickhouse-http-client/src/main/java/com/clickhouse/client/http/HttpUrlConnectionImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ protected ClickHouseHttpResponse post(String sql, ClickHouseInputStream data, Li
220220
Charset ascii = StandardCharsets.US_ASCII;
221221
byte[] boundary = null;
222222
if (tables != null && !tables.isEmpty()) {
223-
String uuid = UUID.randomUUID().toString();
223+
String uuid = rm.createUniqueId();
224224
conn.setRequestProperty("content-type", "multipart/form-data; boundary=".concat(uuid));
225225
boundary = uuid.getBytes(ascii);
226226
} else {

clickhouse-http-client/src/main/java11/com/clickhouse/client/http/HttpClientConnectionImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ protected ClickHouseHttpResponse post(String sql, ClickHouseInputStream data, Li
336336
.timeout(Duration.ofMillis(c.getSocketTimeout()));
337337
byte[] boundary = null;
338338
if (tables != null && !tables.isEmpty()) {
339-
String uuid = UUID.randomUUID().toString();
339+
String uuid = rm.createUniqueId();
340340
reqBuilder.setHeader("content-type", "multipart/form-data; boundary=" + uuid);
341341
boundary = uuid.getBytes(StandardCharsets.US_ASCII);
342342
} else {

clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/JdbcTransaction.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.util.Iterator;
66
import java.util.LinkedList;
77
import java.util.List;
8-
import java.util.UUID;
98

109
import com.clickhouse.client.ClickHouseChecker;
1110
import com.clickhouse.client.ClickHouseException;
@@ -32,7 +31,7 @@ public class JdbcTransaction {
3231

3332
public JdbcTransaction(ClickHouseTransaction tx) {
3433
this.tx = tx;
35-
this.id = tx != null ? tx.getId().asTupleString() : UUID.randomUUID().toString();
34+
this.id = tx != null ? tx.getId().asTupleString() : ClickHouseRequestManager.getInstance().createUniqueId();
3635
this.queries = new LinkedList<>();
3736
this.savepoints = new LinkedList<>();
3837
}

0 commit comments

Comments
 (0)