Support configuration property file (#295)

Signed-off-by: marsishandsome <marsishandsome@gmail.com>
This commit is contained in:
Liangliang Gu 2021-10-27 16:52:58 +08:00 committed by GitHub
parent b82276231c
commit 11bd22234d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 72 additions and 1 deletions

View File

@ -82,6 +82,21 @@ public class Main {
The following includes JVM related parameters.
#### tikv.configuration_file
- tikv java client configuration file
- default: null
This parameter can be passed by `-Dtikv.configuration_file=/path/to/tikv_client_config.properties`.
Here is an example of `tikv_client_config.properties`.
```
tikv.grpc.timeout_in_ms=150ms
tikv.grpc.forward_timeout_in_ms=200ms
tikv.metrics.enable=true
tikv.metrics.port=3140
```
#### tikv.pd.addresses
- pd addresses, separated by comma
- default: 127.0.0.1:2379

View File

@ -19,6 +19,8 @@ import org.tikv.common.util.BackOffer;
import org.tikv.kvproto.Kvrpcpb;
public class ConfigUtils {
public static final String TIKV_CONFIGURATION_FILE = "tikv.configuration_file";
public static final String TIKV_PD_ADDRESSES = "tikv.pd.addresses";
public static final String TIKV_GRPC_TIMEOUT = "tikv.grpc.timeout_in_ms";
public static final String TIKV_GRPC_INGEST_TIMEOUT = "tikv.grpc.ingest_timeout_in_ms";

View File

@ -18,6 +18,8 @@ package org.tikv.common;
import static org.tikv.common.ConfigUtils.*;
import io.grpc.Metadata;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.net.URI;
import java.util.*;
@ -39,8 +41,11 @@ public class TiConfiguration implements Serializable {
Metadata.Key.of("pd-forwarded-host", Metadata.ASCII_STRING_MARSHALLER);
static {
// priority: system environment > config file > default
loadFromSystemProperties();
loadFromConfigurationFile();
loadFromDefaultProperties();
listAll();
}
private static void loadFromSystemProperties() {
@ -51,6 +56,24 @@ public class TiConfiguration implements Serializable {
}
}
private static void loadFromConfigurationFile() {
Optional<String> file = getOption(TIKV_CONFIGURATION_FILE);
if (file.isPresent()) {
Properties properties = new Properties();
try {
properties.load(new FileInputStream(file.get()));
} catch (IOException e) {
logger.error("load config file error, path = " + file.get(), e);
}
for (String key : properties.stringPropertyNames()) {
if (key.startsWith("tikv.")) {
String value = properties.getProperty(key);
setIfMissing(key, value);
}
}
}
}
private static void loadFromDefaultProperties() {
setIfMissing(TIKV_PD_ADDRESSES, DEF_PD_ADDRESSES);
setIfMissing(TIKV_GRPC_TIMEOUT, DEF_TIMEOUT);
@ -94,7 +117,7 @@ public class TiConfiguration implements Serializable {
}
public static void listAll() {
logger.info(new ArrayList<>(settings.entrySet()).toString());
logger.warn("static configurations are:" + new ArrayList<>(settings.entrySet()).toString());
}
private static void set(String key, String value) {

View File

@ -0,0 +1,30 @@
/*
* Copyright 2021 PingCAP, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.tikv.common;
import static org.junit.Assert.assertEquals;
import org.junit.Ignore;
public class TiConfigurationTest {
// Set `-Dtikv.configuration_file=src/test/resources/tikv_client_config.properties` to test
@Ignore
public void configFileTest() {
TiConfiguration conf = TiConfiguration.createRawDefault();
assertEquals("configFileTest", conf.getDBPrefix());
}
}

View File

@ -0,0 +1 @@
tikv.db_prefix=configFileTest