From dca6a9d2238efe1a6beea42ec84b2b63397e6d68 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Sat, 21 Aug 2021 11:01:25 +0800 Subject: [PATCH] [TD-5992] add set config interface for connector --- .../jni/com_taosdata_jdbc_TSDBJNIConnector.h | 8 ++ src/client/src/TSDBJNIConnector.c | 16 +++- src/client/src/tscSystem.c | 81 ++++++++++--------- 3 files changed, 68 insertions(+), 37 deletions(-) diff --git a/src/client/jni/com_taosdata_jdbc_TSDBJNIConnector.h b/src/client/jni/com_taosdata_jdbc_TSDBJNIConnector.h index 7181c658dd..b1f1b003b4 100644 --- a/src/client/jni/com_taosdata_jdbc_TSDBJNIConnector.h +++ b/src/client/jni/com_taosdata_jdbc_TSDBJNIConnector.h @@ -41,6 +41,14 @@ JNIEXPORT void JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_initImp JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_setOptions (JNIEnv *, jclass, jint, jstring); +/* + * Class: com_taosdata_jdbc_TSDBJNIConnector + * Method: setConfig + * Signature: (ILjava/lang/String;)I + */ +JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_setConfig + (JNIEnv *, jclass, jstring); + /* * Class: com_taosdata_jdbc_TSDBJNIConnector * Method: getTsCharset diff --git a/src/client/src/TSDBJNIConnector.c b/src/client/src/TSDBJNIConnector.c index 7ba613de88..12add5528a 100644 --- a/src/client/src/TSDBJNIConnector.c +++ b/src/client/src/TSDBJNIConnector.c @@ -170,6 +170,20 @@ JNIEXPORT void JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_initImp(JNIEnv *e jniDebug("jni initialized successfully, config directory: %s", configDir); } +JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_setConfig(JNIEnv *env, jclass jobj, jstring config){ + if (config == NULL) { + jniDebug("config value is null"); + return -1; + } + + const char *cfg = (*env)->GetStringUTFChars(env, config, NULL); + if (!cfg) { + return -1; + } + + return taos_set_config(cfg); +} + JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_setOptions(JNIEnv *env, jobject jobj, jint optionIndex, jstring optionValue) { if (optionValue == NULL) { @@ -213,7 +227,7 @@ JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_setOptions(JNIEnv return res; } -JNIEXPORT jlong JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_connectImp(JNIEnv *env, jobject jobj, jstring jhost, +JNIEXPORT jlong JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_connectImp(JNIEnv *env, jobject j obj, jstring jhost, jint jport, jstring jdbName, jstring juser, jstring jpass) { jlong ret = 0; diff --git a/src/client/src/tscSystem.c b/src/client/src/tscSystem.c index 884c172577..badd096b73 100644 --- a/src/client/src/tscSystem.c +++ b/src/client/src/tscSystem.c @@ -120,42 +120,6 @@ int32_t tscAcquireRpc(const char *key, const char *user, const char *secretEncry return 0; } -#include "cJSON.h" -static pthread_mutex_t setConfMutex = PTHREAD_MUTEX_INITIALIZER; -static bool setConfFlag = false; -int taos_set_config(const char *config){ - if(taos_init()){ - tscError("failed to call taos_init"); - return -1; - } - - pthread_mutex_lock(&setConfMutex); - - if (setConfFlag) { - tscError("already set config"); - return 0; - } - cJSON *root = cJSON_Parse(config); - if (root == NULL) { - tscError("failed to set config, invalid json format: %s", config); - return -1; - } - - int size = cJSON_GetArraySize(root); - for(int i = 0; i < size; i++){ - cJSON *item = cJSON_GetArrayItem(root, i); - if (!item) { - tscError("failed to read index:%d", i); - continue; - } - taosReadConfigOption(item->string, item->valuestring, NULL, NULL); - } - taosPrintGlobalCfg(); - setConfFlag = true; - pthread_mutex_unlock(&setConfMutex); - return 0; -} - void taos_init_imp(void) { char temp[128] = {0}; @@ -473,3 +437,48 @@ int taos_options(TSDB_OPTION option, const void *arg, ...) { atomic_store_32(&lock, 0); return ret; } + +#include "cJSON.h" +static int taos_set_config_imp(const char *config){ + static bool setConfFlag = false; + if (setConfFlag) { + tscError("already set config"); + return -1; + } + cJSON *root = cJSON_Parse(config); + if (root == NULL) { + tscError("failed to set config, invalid json format: %s", config); + return -1; + } + + int size = cJSON_GetArraySize(root); + for(int i = 0; i < size; i++){ + cJSON *item = cJSON_GetArrayItem(root, i); + if (!item) { + tscError("failed to read index:%d", i); + continue; + } + taosReadConfigOption(item->string, item->valuestring, NULL, NULL); + } + taosPrintGlobalCfg(); + setConfFlag = true; +} + +int taos_set_config(const char *config){ + if(taos_init()){ + tscError("failed to call taos_init"); + return -1; + } + + static int32_t lock = 0; + + for (int i = 1; atomic_val_compare_exchange_32(&lock, 0, 1) != 0; ++i) { + if (i % 1000 == 0) { + tscInfo("haven't acquire lock after spin %d times.", i); + sched_yield(); + } + } + int ret = taos_set_config_imp(config); + atomic_store_32(&lock, 0); + return ret; +} -- GitLab