-
Notifications
You must be signed in to change notification settings - Fork 623
Expand file tree
/
Copy pathClientConfigProperties.java
More file actions
472 lines (357 loc) · 16.2 KB
/
ClientConfigProperties.java
File metadata and controls
472 lines (357 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package com.clickhouse.client.api;
import com.clickhouse.client.api.data_formats.internal.AbstractBinaryFormatReader;
import com.clickhouse.client.api.internal.ClickHouseLZ4OutputStream;
import com.clickhouse.data.ClickHouseDataType;
import com.clickhouse.data.ClickHouseFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* Enumerates all client properties that are known at release.
*/
public enum ClientConfigProperties {
SESSION_DB_ROLES("session_db_roles", List.class),
SETTING_LOG_COMMENT(serverSetting("log_comment"), String.class),
HTTP_USE_BASIC_AUTH("http_use_basic_auth", Boolean.class, "true"),
USER("user", String.class, "default"),
PASSWORD("password", String.class),
/**
* Maximum number of active connection in internal connection pool.
*/
HTTP_MAX_OPEN_CONNECTIONS("max_open_connections", Integer.class, "10"),
/**
* HTTP keep-alive timeout override.
*/
HTTP_KEEP_ALIVE_TIMEOUT("http_keep_alive_timeout", Long.class),
USE_SERVER_TIMEZONE("use_server_time_zone", Boolean.class, "true"),
USE_TIMEZONE("use_time_zone", TimeZone.class),
SERVER_VERSION("server_version", String.class),
SERVER_TIMEZONE("server_time_zone", TimeZone.class, "UTC"),
ASYNC_OPERATIONS("async", Boolean.class, "false"),
CONNECTION_TTL("connection_ttl", Long.class, "-1"),
CONNECTION_TIMEOUT("connection_timeout", Long.class),
CONNECTION_REUSE_STRATEGY("connection_reuse_strategy", ConnectionReuseStrategy.class, String.valueOf(ConnectionReuseStrategy.FIFO)),
SOCKET_OPERATION_TIMEOUT("socket_timeout", Integer.class, "0"),
SOCKET_RCVBUF_OPT("socket_rcvbuf", Integer.class, "804800"),
SOCKET_SNDBUF_OPT("socket_sndbuf", Integer.class,"804800"),
SOCKET_REUSEADDR_OPT("socket_reuseaddr", Boolean.class),
SOCKET_KEEPALIVE_OPT("socket_keepalive", Boolean.class),
SOCKET_TCP_NO_DELAY_OPT("socket_tcp_nodelay", Boolean.class),
SOCKET_LINGER_OPT("socket_linger", Integer.class),
DATABASE("database", String.class, "default"),
COMPRESS_SERVER_RESPONSE("compress", Boolean.class, "true"), // actually a server setting, but has client effect too
COMPRESS_CLIENT_REQUEST("decompress", Boolean.class, "false"), // actually a server setting, but has client effect too
USE_HTTP_COMPRESSION("client.use_http_compression", Boolean.class, "false"),
COMPRESSION_LZ4_UNCOMPRESSED_BUF_SIZE("compression.lz4.uncompressed_buffer_size", Integer.class, String.valueOf(ClickHouseLZ4OutputStream.UNCOMPRESSED_BUFF_SIZE)),
DISABLE_NATIVE_COMPRESSION("disable_native_compression", Boolean.class, "false"),
PROXY_TYPE("proxy_type", String.class), // "http"
PROXY_HOST("proxy_host", String.class),
PROXY_PORT("proxy_port", Integer.class),
PROXY_USER("proxy_user", String.class),
PROXY_PASSWORD("proxy_password", String.class),
MAX_EXECUTION_TIME("max_execution_time", Integer.class,"0"),
SSL_TRUST_STORE("trust_store", String.class),
SSL_KEYSTORE_TYPE("key_store_type", String.class),
SSL_KEY_STORE("ssl_key_store", String.class),
SSL_KEY_STORE_PASSWORD("key_store_password", String.class),
SSL_KEY("ssl_key", String.class),
CA_CERTIFICATE("sslrootcert", String.class),
SSL_CERTIFICATE("sslcert", String.class),
RETRY_ON_FAILURE("retry", Integer.class, "3"),
INPUT_OUTPUT_FORMAT("format", ClickHouseFormat.class),
MAX_THREADS_PER_CLIENT("max_threads_per_client", Integer.class, "0"),
QUERY_ID("query_id", String.class), // actually a server setting, but has client effect too
CLIENT_NETWORK_BUFFER_SIZE("client_network_buffer_size", Integer.class, "300000"),
/**
* Preferred client setting for token-based authentication like JWT and Oauth.
* For Http it is translated to Authorization Bearer header.
*/
ACCESS_TOKEN("access_token", String.class),
SSL_AUTH("ssl_authentication", Boolean.class, "false"),
CONNECTION_POOL_ENABLED("connection_pool_enabled", Boolean.class, "true"),
CONNECTION_REQUEST_TIMEOUT("connection_request_timeout", Long.class, "10000"),
CLIENT_RETRY_ON_FAILURE("client_retry_on_failures", List.class,
String.join(",", ClientFaultCause.NoHttpResponse.name(), ClientFaultCause.ConnectTimeout.name(),
ClientFaultCause.ConnectionRequestTimeout.name(), ClientFaultCause.ServerRetryable.name())) {
@Override
public Object parseValue(String value) {
List<String> strValues = (List<String>) super.parseValue(value);
List<ClientFaultCause> failures = new ArrayList<ClientFaultCause>();
if (strValues != null) {
for (String strValue : strValues) {
failures.add(ClientFaultCause.valueOf(strValue));
}
}
return failures;
}
},
CLIENT_NAME("client_name", String.class, ""),
/**
* An old alias to {@link ClientConfigProperties#CLIENT_NAME}. Using the last one is preferred.
*/
@Deprecated
PRODUCT_NAME("product_name", String.class),
/**
* HTTP-specific alias for {@link ClientConfigProperties#ACCESS_TOKEN}.
* Prefer using {@link ClientConfigProperties#ACCESS_TOKEN}.
*/
BEARERTOKEN_AUTH ("bearer_token", String.class),
/**
* Indicates that data provided for write operation is compressed by application.
*/
APP_COMPRESSED_DATA("app_compressed_data", Boolean.class, "false"),
/**
* Name of the group under which client metrics appear
*/
METRICS_GROUP_NAME("metrics_name", String.class, "ch-http-pool"),
HTTP_SAVE_COOKIES("client.http.cookies_enabled", Boolean.class, "false"),
BINARY_READER_USE_PREALLOCATED_BUFFERS("client_allow_binary_reader_to_reuse_buffers", Boolean.class, "false"),
/**
* Defines mapping between ClickHouse data type and target Java type
* Used by binary readers to convert values into desired Java type.
*/
TYPE_HINT_MAPPING("type_hint_mapping", Map.class),
/**
* SNI SSL parameter that will be set for each outbound SSL socket.
*/
SSL_SOCKET_SNI("ssl_socket_sni", String.class,""),
/**
* If parameters should be sent in request body.
* Note: work only with Server side compression. If compression is enabled on client level it will be disabled
* for query requests with parameters.
*/
HTTP_SEND_PARAMS_IN_BODY("client.http.use_form_request_for_query", Boolean.class, "false"),
/**
* Prefix for custom settings. Should be aligned with server configuration.
* See <a href="https://clickhouse.com/docs/operations/settings/query-level#custom_settings">ClickHouse Docs</a>
*/
CUSTOM_SETTINGS_PREFIX("custom_settings_prefix", String.class, "custom_"),
;
private static final Logger LOG = LoggerFactory.getLogger(ClientConfigProperties.class);
private final String key;
private final Class<?> valueType;
private final String defaultValue;
private final Object defaultObjValue;
ClientConfigProperties(String key, Class<?> valueType) {
this(key, valueType, null);
}
ClientConfigProperties(String key, Class<?> valueType, String defaultValue) {
this.key = key;
this.valueType = valueType;
this.defaultValue = defaultValue;
this.defaultObjValue = parseValue(defaultValue);
}
public String getKey() {
return key;
}
public String getDefaultValue() {
return defaultValue;
}
@SuppressWarnings("unchecked")
public <T> T getDefObjVal() {
return (T) defaultObjValue;
}
public static final String HTTP_HEADER_PREFIX = "http_header_";
public static final String SERVER_SETTING_PREFIX = "clickhouse_setting_";
// Key used to identify default value in configuration map
public static final String DEFAULT_KEY = "_default_";
public static final String IGNORE_UNKNOWN_CONFIG_KEY = "ignore_unknown_config_key";
public static String serverSetting(String key) {
return SERVER_SETTING_PREFIX + key;
}
public static String httpHeader(String key) {
return HTTP_HEADER_PREFIX + key.toUpperCase(Locale.US);
}
public static String commaSeparated(Collection<?> values) {
StringBuilder sb = new StringBuilder();
for (Object value : values) {
sb.append(value.toString().replaceAll(",", "\\\\,")).append(",");
}
if (sb.length() > 0) {
sb.setLength(sb.length() - 1);
}
return sb.toString();
}
public static List<String> valuesFromCommaSeparated(String value) {
if (value == null || value.isEmpty()) {
return Collections.emptyList();
}
return Arrays.stream(value.split("(?<!\\\\),")).map(s -> s.replaceAll("\\\\,", ","))
.collect(Collectors.toList());
}
public Object parseValue(String value) {
if (value == null) {
return null;
}
if (valueType.equals(String.class)) {
return value;
}
if (valueType.equals(Boolean.class)) {
if (value.equals("1")) return true;
if (value.equals("0")) return false;
return Boolean.parseBoolean(value);
}
if (valueType.equals(Integer.class)) {
return Integer.parseInt(value);
}
if (valueType.equals(Long.class)) {
return Long.parseLong(value);
}
if (valueType.equals(List.class)) {
return valuesFromCommaSeparated(value);
}
if (valueType.isEnum()) {
Object[] constants = valueType.getEnumConstants();
for (Object constant : constants) {
if (constant.toString().equals(value)) {
return constant;
}
}
throw new IllegalArgumentException("Invalid constant name '" + value + "' for enum " + valueType.getName());
}
if (valueType.equals(TimeZone.class)) {
return TimeZone.getTimeZone(value);
}
if (valueType.equals(Map.class)) {
return toKeyValuePairs(value);
}
return null;
}
@SuppressWarnings("unchecked")
public <T> T getOrDefault(Map<String, Object> configMap) {
return (T) configMap.getOrDefault(getKey(), getDefObjVal());
}
public <T> void applyIfSet(Map<String, Object> configMap, Consumer<T> consumer) {
T value = (T) configMap.get(getKey());
if (value != null) {
consumer.accept(value);
}
}
public static Map<String, Object> parseConfigMap(Map<String, String> configMap) {
Map<String, Object> parsedConfig = new HashMap<>();
Map<String, String> tmpMap = new HashMap<>(configMap);
for (ClientConfigProperties config : ClientConfigProperties.values()) {
String value = tmpMap.remove(config.getKey());
if (value != null) {
Object parsedValue;
switch (config) {
case TYPE_HINT_MAPPING:
parsedValue = translateTypeHintMapping(value);
break;
default:
parsedValue = config.parseValue(value);
}
parsedConfig.put(config.getKey(), parsedValue);
}
}
final String customSettingsPrefix = configMap.getOrDefault(ClientConfigProperties.CUSTOM_SETTINGS_PREFIX.getKey(),
CUSTOM_SETTINGS_PREFIX.getDefaultValue());
if (customSettingsPrefix == null || customSettingsPrefix.isEmpty()) {
throw new ClientException(ClientConfigProperties.CUSTOM_SETTINGS_PREFIX.getKey() + " must be not-blank");
}
for (String key : new HashSet<>(tmpMap.keySet())) {
if (key.startsWith(HTTP_HEADER_PREFIX) || key.startsWith(SERVER_SETTING_PREFIX)) {
parsedConfig.put(key, tmpMap.remove(key));
} else if (key.startsWith(customSettingsPrefix)) {
parsedConfig.put(serverSetting(key), tmpMap.remove(key));
}
}
tmpMap.remove(ClientConfigProperties.IGNORE_UNKNOWN_CONFIG_KEY);
if (!tmpMap.isEmpty()) {
String msg = "Unknown and unmapped config properties: " + tmpMap.keySet();
if (Boolean.parseBoolean(String.valueOf(configMap.get(IGNORE_UNKNOWN_CONFIG_KEY)))) {
LOG.warn(msg);
} else {
throw new ClientMisconfigurationException(msg);
}
}
return parsedConfig;
}
/**
* Converts given string to key value pairs.
* This is very simple implementation that do not handle edge cases like
* {@code k1=v1, ,k2=v2}
*
* @param str string
* @return non-null key value pairs
*/
public static Map<String, String> toKeyValuePairs(String str) {
if (str == null || str.isEmpty()) {
return Collections.emptyMap();
}
Map<String, String> map = new LinkedHashMap<>();
String key = null;
StringBuilder builder = new StringBuilder();
for (int i = 0, len = str.length(); i < len; i++) {
char ch = str.charAt(i);
if (ch == '\\' && i + 1 < len) {
ch = str.charAt(++i);
builder.append(ch);
continue;
}
if (Character.isWhitespace(ch)) {
if (builder.length() > 0) {
builder.append(ch);
}
} else if (ch == '=' && key == null) {
key = builder.toString().trim();
builder.setLength(0);
} else if (ch == ',' && key != null) {
String value = builder.toString().trim();
builder.setLength(0);
if (!key.isEmpty() && !value.isEmpty()) {
map.put(key, value);
}
key = null;
} else {
builder.append(ch);
}
}
if (key != null && builder.length() > 0) {
String value = builder.toString().trim();
if (!key.isEmpty() && !value.isEmpty()) {
map.put(key, value);
}
}
return Collections.unmodifiableMap(map);
}
public static String mapToString(Map<?,?> map, Function<Object, String> valueConverter) {
StringBuilder sb = new StringBuilder();
for (Map.Entry<?, ?> entry : map.entrySet()) {
sb.append(entry.getKey()).append("=").append(valueConverter.apply(entry.getValue())).append(",");
}
if (sb.length() > 0) {
sb.setLength(sb.length() - 1);
}
return sb.toString();
}
public static Map<ClickHouseDataType, Class<?>> translateTypeHintMapping(String mappingStr) {
if (mappingStr == null || mappingStr.isEmpty()) {
return AbstractBinaryFormatReader.NO_TYPE_HINT_MAPPING;
}
Map<String, String> mapping= ClientConfigProperties.toKeyValuePairs(mappingStr);
Map<ClickHouseDataType, Class<?>> hintMapping = new HashMap<>();
try {
for (Map.Entry<String, String> entry : mapping.entrySet()) {
hintMapping.put(ClickHouseDataType.of(entry.getKey()),
Class.forName(entry.getValue()));
}
} catch (ClassNotFoundException e) {
throw new ClientMisconfigurationException("Failed to translate type-hint mapping", e);
}
return hintMapping;
}
}