Skip to content
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
160 changes: 15 additions & 145 deletions client-v2/src/main/java/com/clickhouse/client/api/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import com.clickhouse.client.api.http.ClickHouseHttpProto;
import com.clickhouse.client.api.insert.InsertResponse;
import com.clickhouse.client.api.insert.InsertSettings;
import com.clickhouse.client.api.internal.ClickHouseLZ4OutputStream;
import com.clickhouse.client.api.internal.ClientStatisticsHolder;
import com.clickhouse.client.api.internal.HttpAPIClientHelper;
import com.clickhouse.client.api.internal.MapUtils;
Expand Down Expand Up @@ -80,9 +79,6 @@
import java.util.function.Supplier;
import java.util.stream.Collectors;

import static java.time.temporal.ChronoUnit.MILLIS;
import static java.time.temporal.ChronoUnit.SECONDS;

/**
* <p>Client is the starting point for all interactions with ClickHouse. </p>
*
Expand Down Expand Up @@ -141,12 +137,12 @@ public class Client implements AutoCloseable {
private int retries;
private LZ4Factory lz4Factory = null;

private Client(Set<String> endpoints, Map<String,String> configuration, boolean useNewImplementation,
private Client(Set<String> endpoints, Map<String,String> configuration,
ExecutorService sharedOperationExecutor, ColumnToMethodMatchingStrategy columnToMethodMatchingStrategy) {
this(endpoints, configuration, useNewImplementation, sharedOperationExecutor, columnToMethodMatchingStrategy, null);
this(endpoints, configuration, sharedOperationExecutor, columnToMethodMatchingStrategy, null);
}

private Client(Set<String> endpoints, Map<String,String> configuration, boolean useNewImplementation,
private Client(Set<String> endpoints, Map<String,String> configuration,
ExecutorService sharedOperationExecutor, ColumnToMethodMatchingStrategy columnToMethodMatchingStrategy, Object metricsRegistry) {
// Simple initialization
this.configuration = configuration;
Expand Down Expand Up @@ -256,14 +252,22 @@ public static class Builder {

// Read-only configuration
private Map<String, String> configuration;
private boolean useNewImplementation = true;

private ExecutorService sharedOperationExecutor = null;
private ColumnToMethodMatchingStrategy columnToMethodMatchingStrategy;
private Object metricRegistry = null;
public Builder() {
this.endpoints = new HashSet<>();
this.configuration = new HashMap<String, String>();
this.configuration = new HashMap<>();

for (ClientConfigProperties p : ClientConfigProperties.values()) {
if (p.getDefaultValue() != null) {
this.configuration.put(p.getKey(), p.getDefaultValue());
}
}

allowBinaryReaderToReuseBuffers(false);
columnToMethodMatchingStrategy = DefaultColumnToMethodMatchingStrategy.INSTANCE;
}

/**
Expand Down Expand Up @@ -510,7 +514,7 @@ public Builder setSocketRcvbuf(long size) {
* @param size - socket send buffer size in bytes
*/
public Builder setSocketSndbuf(long size) {
this.configuration.put(ClientConfigProperties.SOCKET_RCVBUF_OPT.getKey(), String.valueOf(size));
this.configuration.put(ClientConfigProperties.SOCKET_SNDBUF_OPT.getKey(), String.valueOf(size));
return this;
}

Expand Down Expand Up @@ -656,25 +660,12 @@ public Builder setExecutionTimeout(long timeout, ChronoUnit timeUnit) {
return this;
}

/**
* Switches to new implementation of the client. Default is true.
* Throws exception if {@code useNewImplementation == false}
* @deprecated
*/
public Builder useNewImplementation(boolean useNewImplementation) {
if (!useNewImplementation) {
throw new ClientException("switch between new and old version is remove because old version is deprecated.");
}
return this;
}

public Builder setHttpCookiesEnabled(boolean enabled) {
//TODO: extract to settings string constants
this.configuration.put("client.http.cookies_enabled", String.valueOf(enabled));
return this;
}


/**
* Defines path to the trust store file. It cannot be combined with
* certificates. Either trust store or certificates should be used.
Expand Down Expand Up @@ -1013,8 +1004,6 @@ public Builder setServerVersion(String serverVersion) {
}

public Client build() {
setDefaults();

// check if endpoint are empty. so can not initiate client
if (this.endpoints.isEmpty()) {
throw new IllegalArgumentException("At least one endpoint is required");
Expand Down Expand Up @@ -1070,128 +1059,9 @@ public Client build() {
throw new IllegalArgumentException("Nor server timezone nor specific timezone is set");
}

return new Client(this.endpoints, this.configuration, this.useNewImplementation, this.sharedOperationExecutor,
return new Client(this.endpoints, this.configuration, this.sharedOperationExecutor,
this.columnToMethodMatchingStrategy, this.metricRegistry);
}


private static final int DEFAULT_NETWORK_BUFFER_SIZE = 300_000;

/**
* Default size for a buffers used in networking.
*/
public static final int DEFAULT_BUFFER_SIZE = 8192;
public static final int DEFAULT_SOCKET_BUFFER_SIZE = 804800;

private void setDefaults() {

// set default database name if not specified
if (!configuration.containsKey(ClientConfigProperties.DATABASE.getKey())) {
setDefaultDatabase((String) "default");
}

if (!configuration.containsKey(ClientConfigProperties.MAX_EXECUTION_TIME.getKey())) {
setExecutionTimeout(0, MILLIS);
}

if (!configuration.containsKey(ClientConfigProperties.MAX_THREADS_PER_CLIENT.getKey())) {
configuration.put(ClientConfigProperties.MAX_THREADS_PER_CLIENT.getKey(),
String.valueOf(0));
}

if (!configuration.containsKey("compression.lz4.uncompressed_buffer_size")) {
setLZ4UncompressedBufferSize(ClickHouseLZ4OutputStream.UNCOMPRESSED_BUFF_SIZE);
}

if (!configuration.containsKey(ClientConfigProperties.DISABLE_NATIVE_COMPRESSION.getKey())) {
disableNativeCompression(false);
}

if (!configuration.containsKey(ClientConfigProperties.USE_SERVER_TIMEZONE.getKey())) {
useServerTimeZone(true);
}

if (!configuration.containsKey(ClientConfigProperties.SERVER_TIMEZONE.getKey())) {
setServerTimeZone("UTC");
}

if (!configuration.containsKey(ClientConfigProperties.ASYNC_OPERATIONS.getKey())) {
useAsyncRequests(false);
}

if (!configuration.containsKey(ClientConfigProperties.HTTP_MAX_OPEN_CONNECTIONS.getKey())) {
setMaxConnections(10);
}

if (!configuration.containsKey(ClientConfigProperties.CONNECTION_REQUEST_TIMEOUT.getKey())) {
setConnectionRequestTimeout(10, SECONDS);
}

if (!configuration.containsKey(ClientConfigProperties.CONNECTION_REUSE_STRATEGY.getKey())) {
setConnectionReuseStrategy(ConnectionReuseStrategy.FIFO);
}

if (!configuration.containsKey(ClientConfigProperties.CONNECTION_POOL_ENABLED.getKey())) {
enableConnectionPool(true);
}

if (!configuration.containsKey(ClientConfigProperties.CONNECTION_TTL.getKey())) {
setConnectionTTL(-1, MILLIS);
}

if (!configuration.containsKey(ClientConfigProperties.CLIENT_RETRY_ON_FAILURE.getKey())) {
retryOnFailures(ClientFaultCause.NoHttpResponse, ClientFaultCause.ConnectTimeout,
ClientFaultCause.ConnectionRequestTimeout);
}

if (!configuration.containsKey(ClientConfigProperties.CLIENT_NETWORK_BUFFER_SIZE.getKey())) {
setClientNetworkBufferSize(DEFAULT_NETWORK_BUFFER_SIZE);
}

if (!configuration.containsKey(ClientConfigProperties.RETRY_ON_FAILURE.getKey())) {
setMaxRetries(3);
}

if (!configuration.containsKey("client_allow_binary_reader_to_reuse_buffers")) {
allowBinaryReaderToReuseBuffers(false);
}

if (columnToMethodMatchingStrategy == null) {
columnToMethodMatchingStrategy = DefaultColumnToMethodMatchingStrategy.INSTANCE;
}

if (!configuration.containsKey(ClientConfigProperties.HTTP_USE_BASIC_AUTH.getKey())) {
useHTTPBasicAuth(true);
}

if (!configuration.containsKey(ClientConfigProperties.COMPRESS_CLIENT_REQUEST.getKey())) {
compressClientRequest(false);
}

if (!configuration.containsKey(ClientConfigProperties.COMPRESS_SERVER_RESPONSE.getKey())) {
compressServerResponse(true);
}

if (!configuration.containsKey(ClientConfigProperties.USE_HTTP_COMPRESSION.getKey())) {
useHttpCompression(false);
}

if (!configuration.containsKey(ClientConfigProperties.APP_COMPRESSED_DATA.getKey())) {
appCompressedData(false);
}

if (!configuration.containsKey(ClientConfigProperties.SOCKET_OPERATION_TIMEOUT.getKey())) {
setSocketTimeout(0, SECONDS);
}

if (!configuration.containsKey(ClientConfigProperties.SOCKET_RCVBUF_OPT.getKey())) {
setSocketRcvbuf(DEFAULT_SOCKET_BUFFER_SIZE);
}

if (!configuration.containsKey(ClientConfigProperties.SOCKET_SNDBUF_OPT.getKey())) {
setSocketSndbuf(DEFAULT_SOCKET_BUFFER_SIZE);
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.clickhouse.client.api;

import com.clickhouse.client.api.internal.ClickHouseLZ4OutputStream;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -16,11 +18,11 @@ public enum ClientConfigProperties {

SETTING_LOG_COMMENT(serverSetting("log_comment")),

HTTP_USE_BASIC_AUTH("http_use_basic_auth"),
HTTP_USE_BASIC_AUTH("http_use_basic_auth", "true"),

USER("user", "default"),

PASSWORD("password", ""),
PASSWORD("password"),

/**
* Maximum number of active connection in internal connection pool.
Expand All @@ -32,27 +34,27 @@ public enum ClientConfigProperties {
*/
HTTP_KEEP_ALIVE_TIMEOUT("http_keep_alive_timeout"),

USE_SERVER_TIMEZONE("use_server_time_zone"),
USE_SERVER_TIMEZONE("use_server_time_zone", "true"),

USE_TIMEZONE("use_time_zone"),

SERVER_VERSION("server_version"),

SERVER_TIMEZONE("server_time_zone"),
SERVER_TIMEZONE("server_time_zone", "UTC"),

ASYNC_OPERATIONS("async"),
ASYNC_OPERATIONS("async", "false"),

CONNECTION_TTL("connection_ttl"),
CONNECTION_TTL("connection_ttl", "-1"),

CONNECTION_TIMEOUT("connection_timeout"),

CONNECTION_REUSE_STRATEGY("connection_reuse_strategy"),
CONNECTION_REUSE_STRATEGY("connection_reuse_strategy", String.valueOf(ConnectionReuseStrategy.FIFO)),

SOCKET_OPERATION_TIMEOUT("socket_timeout"),
SOCKET_OPERATION_TIMEOUT("socket_timeout", "0"),

SOCKET_RCVBUF_OPT("socket_rcvbuf"),
SOCKET_RCVBUF_OPT("socket_rcvbuf", "8196"),

SOCKET_SNDBUF_OPT("socket_sndbuf"),
SOCKET_SNDBUF_OPT("socket_sndbuf", "8196"),

SOCKET_REUSEADDR_OPT("socket_reuseaddr"),

Expand All @@ -64,13 +66,13 @@ public enum ClientConfigProperties {

DATABASE("database", "default"),

COMPRESS_SERVER_RESPONSE("compress"), // actually a server setting, but has client effect too
COMPRESS_SERVER_RESPONSE("compress", "true"), // actually a server setting, but has client effect too

COMPRESS_CLIENT_REQUEST("decompress"), // actually a server setting, but has client effect too
COMPRESS_CLIENT_REQUEST("decompress", "false"), // actually a server setting, but has client effect too

USE_HTTP_COMPRESSION("client.use_http_compression"),
USE_HTTP_COMPRESSION("client.use_http_compression", "false"),

COMPRESSION_LZ4_UNCOMPRESSED_BUF_SIZE("compression.lz4.uncompressed_buffer_size"),
COMPRESSION_LZ4_UNCOMPRESSED_BUF_SIZE("compression.lz4.uncompressed_buffer_size", String.valueOf(ClickHouseLZ4OutputStream.UNCOMPRESSED_BUFF_SIZE)),

DISABLE_NATIVE_COMPRESSION("disable_native_compression", "false"),

Expand All @@ -84,7 +86,7 @@ public enum ClientConfigProperties {

PROXY_PASSWORD("proxy_password"),

MAX_EXECUTION_TIME("max_execution_time"),
MAX_EXECUTION_TIME("max_execution_time", "0"),

SSL_TRUST_STORE("trust_store"),

Expand All @@ -100,23 +102,25 @@ public enum ClientConfigProperties {

SSL_CERTIFICATE("sslcert"),

RETRY_ON_FAILURE("retry"),
RETRY_ON_FAILURE("retry", "3"),

INPUT_OUTPUT_FORMAT("format"),

MAX_THREADS_PER_CLIENT("max_threads_per_client"),
MAX_THREADS_PER_CLIENT("max_threads_per_client", "0"),

QUERY_ID("query_id"), // actually a server setting, but has client effect too

CLIENT_NETWORK_BUFFER_SIZE("client_network_buffer_size", String.valueOf(Client.Builder.DEFAULT_BUFFER_SIZE)),
CLIENT_NETWORK_BUFFER_SIZE("client_network_buffer_size", "300000"),

ACCESS_TOKEN("access_token"), SSL_AUTH("ssl_authentication"),

CONNECTION_POOL_ENABLED("connection_pool_enabled"),
CONNECTION_POOL_ENABLED("connection_pool_enabled", "true"),

CONNECTION_REQUEST_TIMEOUT("connection_request_timeout"),
CONNECTION_REQUEST_TIMEOUT("connection_request_timeout", "10000"),

CLIENT_RETRY_ON_FAILURE("client_retry_on_failures"),
CLIENT_RETRY_ON_FAILURE("client_retry_on_failures",
String.join(",", ClientFaultCause.NoHttpResponse.name(), ClientFaultCause.ConnectTimeout.name(),
ClientFaultCause.ConnectionRequestTimeout.name())),

CLIENT_NAME("client_name"),

Expand All @@ -130,18 +134,19 @@ public enum ClientConfigProperties {
/**
* Indicates that data provided for write operation is compressed by application.
*/
APP_COMPRESSED_DATA("app_compressed_data"),
APP_COMPRESSED_DATA("app_compressed_data", "false"),

/**
*
* Name of the group under which client metrics appear
*/
METRICS_GROUP_NAME("metrics_name"),
;

private String key;
private final String key;

private String defaultValue;
private final String defaultValue;

private List<String> choices;
private final List<String> choices;


ClientConfigProperties(String key) {
Expand Down
Loading
Loading