tscSystem.c 11.8 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

S
slguan 已提交
16
#include "os.h"
H
hzcheng 已提交
17 18 19
#include "taosmsg.h"
#include "tcache.h"
#include "trpc.h"
S
slguan 已提交
20
#include "taosdef.h"
H
hzcheng 已提交
21 22 23 24 25
#include "tsocket.h"
#include "tsystem.h"
#include "ttime.h"
#include "ttimer.h"
#include "tutil.h"
S
slguan 已提交
26
#include "tsched.h"
S
slguan 已提交
27
#include "tscLog.h"
H
hzcheng 已提交
28
#include "tsclient.h"
S
slguan 已提交
29 30 31 32
#include "tglobal.h"
#include "tconfig.h"
#include "ttimezone.h"
#include "tlocale.h"
S
slguan 已提交
33

H
hzcheng 已提交
34 35 36 37 38 39 40 41 42
// global, not configurable
void *  pVnodeConn;
void *  pVMeterConn;
void *  pTscMgmtConn;
void *  pSlaveConn;
void *  tscCacheHandle;
int     slaveIndex;
void *  tscTmr;
void *  tscQhandle;
S
slguan 已提交
43
void *  tscCheckDiskUsageTmr;
H
hzcheng 已提交
44 45
int     tsInsertHeadSize;

46
int tscNumOfThreads;
H
hzcheng 已提交
47

48
static pthread_once_t tscinit = PTHREAD_ONCE_INIT;
L
lihui 已提交
49
void taosInitNote(int numOfNoteLines, int maxNotes, char* lable);
S
slguan 已提交
50
void tscUpdateIpSet(void *ahandle, SRpcIpSet *pIpSet);
L
lihui 已提交
51

S
slguan 已提交
52 53 54 55 56
void tscCheckDiskUsage(void *para, void *unused) {
  taosGetDisk();
  taosTmrReset(tscCheckDiskUsage, 1000, NULL, tscTmr, &tscCheckDiskUsageTmr);
}

S
slguan 已提交
57 58 59 60 61 62 63 64 65 66 67 68
int32_t tscInitRpc(const char *user, const char *secret) {
  SRpcInit rpcInit;
  char secretEncrypt[32] = {0};
  taosEncryptPass((uint8_t *)secret, strlen(secret), secretEncrypt);

  if (pVnodeConn == NULL) {
    memset(&rpcInit, 0, sizeof(rpcInit));
    rpcInit.localIp = tsLocalIp;
    rpcInit.localPort = 0;
    rpcInit.label = "TSC-vnode";
    rpcInit.numOfThreads = tscNumOfThreads;
    rpcInit.cfp = tscProcessMsgFromServer;
S
slguan 已提交
69
    rpcInit.ufp = tscUpdateIpSet;
S
slguan 已提交
70 71
    rpcInit.sessions = tsMaxVnodeConnections;
    rpcInit.connType = TAOS_CONN_CLIENT;
72
    rpcInit.user = (char*)user;
H
hzcheng 已提交
73
    rpcInit.idleTime = 2000;
S
slguan 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    rpcInit.ckey = "key";
    rpcInit.secret = secretEncrypt;

    pVnodeConn = rpcOpen(&rpcInit);
    if (pVnodeConn == NULL) {
      tscError("failed to init connection to vnode");
      return -1;
    }
  }

  if (pTscMgmtConn == NULL) {
    memset(&rpcInit, 0, sizeof(rpcInit));
    rpcInit.localIp = tsLocalIp;
    rpcInit.localPort = 0;
    rpcInit.label = "TSC-mgmt";
    rpcInit.numOfThreads = 1;
    rpcInit.cfp = tscProcessMsgFromServer;
    rpcInit.sessions = tsMaxMgmtConnections;
    rpcInit.connType = TAOS_CONN_CLIENT;
    rpcInit.idleTime = 2000;
S
slguan 已提交
94
    rpcInit.user = (char*)user;
S
slguan 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107
    rpcInit.ckey = "key";
    rpcInit.secret = secretEncrypt;

    pTscMgmtConn = rpcOpen(&rpcInit);
    if (pTscMgmtConn == NULL) {
      tscError("failed to init connection to mgmt");
      return -1;
    }
  }

  return 0;
}

H
hzcheng 已提交
108 109 110 111
void taos_init_imp() {
  char        temp[128];
  struct stat dirstat;

112
  errno = TSDB_CODE_SUCCESS;
H
hzcheng 已提交
113
  srand(taosGetTimestampSec());
L
lihui 已提交
114
  deltaToUtcInitOnce();
H
hzcheng 已提交
115 116 117 118 119 120 121 122 123 124

  if (tscEmbedded == 0) {
    /*
     * set localIp = 0
     * means unset tsLocalIp in client
     * except read from config file
     */
    strcpy(tsLocalIp, "0.0.0.0");

    // Read global configuration.
S
slguan 已提交
125 126
    taosInitGlobalCfg();
    taosReadGlobalLogCfg();
H
hzcheng 已提交
127 128 129 130 131 132

    // For log directory
    if (stat(logDir, &dirstat) < 0) mkdir(logDir, 0755);

    sprintf(temp, "%s/taoslog", logDir);
    if (taosInitLog(temp, tsNumOfLogLines, 10) < 0) {
S
slguan 已提交
133
      printf("failed to open log file in directory:%s\n", logDir);
H
hzcheng 已提交
134 135
    }

S
slguan 已提交
136 137 138
    taosReadGlobalCfg();
    taosCheckGlobalCfg();
    taosPrintGlobalCfg();
H
hzcheng 已提交
139 140 141 142 143

    tscTrace("starting to initialize TAOS client ...");
    tscTrace("Local IP address is:%s", tsLocalIp);
  }

L
lihui 已提交
144 145
  taosSetCoreDump();

L
lihui 已提交
146 147 148
  if (tsTscEnableRecordSql != 0) {
    taosInitNote(tsNumOfLogLines / 10, 1, (char*)"tsc_note");
  }
S
slguan 已提交
149

S
slguan 已提交
150
  tscMgmtIpList.inUse = 0;
S
slguan 已提交
151
  tscMgmtIpList.port = tsMnodeShellPort;
S
slguan 已提交
152 153
  tscMgmtIpList.numOfIps = 1;
  tscMgmtIpList.ip[0] = inet_addr(tsMasterIp);
S
slguan 已提交
154

S
slguan 已提交
155
  if (tsSecondIp[0] && strcmp(tsSecondIp, tsMasterIp) != 0) {
S
slguan 已提交
156 157
    tscMgmtIpList.numOfIps = 2;
    tscMgmtIpList.ip[1] = inet_addr(tsSecondIp);
S
slguan 已提交
158 159
  }

160
  tscInitMsgsFp();
H
hzcheng 已提交
161 162 163 164 165 166 167 168 169 170 171 172
  slaveIndex = rand();
  int queueSize = tsMaxVnodeConnections + tsMaxMeterConnections + tsMaxMgmtConnections + tsMaxMgmtConnections;

  if (tscEmbedded == 0) {
    tscNumOfThreads = tsNumOfCores * tsNumOfThreadsPerCore / 2.0;
  } else {
    tscNumOfThreads = tsNumOfCores * tsNumOfThreadsPerCore / 4.0;
  }

  if (tscNumOfThreads < 2) tscNumOfThreads = 2;

  tscQhandle = taosInitScheduler(queueSize, tscNumOfThreads, "tsc");
S
slguan 已提交
173 174 175 176
  if (NULL == tscQhandle) {
    tscError("failed to init scheduler");
    return;
  }
H
hzcheng 已提交
177 178

  tscTmr = taosTmrInit(tsMaxMgmtConnections * 2, 200, 60000, "TSC");
S
slguan 已提交
179 180
  if(0 == tscEmbedded){
    taosTmrReset(tscCheckDiskUsage, 10, NULL, tscTmr, &tscCheckDiskUsageTmr);      
S
slguan 已提交
181
  }
182
  
H
hzcheng 已提交
183 184 185 186
  int64_t refreshTime = tsMetricMetaKeepTimer < tsMeterMetaKeepTimer ? tsMetricMetaKeepTimer : tsMeterMetaKeepTimer;
  refreshTime = refreshTime > 2 ? 2 : refreshTime;
  refreshTime = refreshTime < 1 ? 1 : refreshTime;

187 188 189
  if (tscCacheHandle == NULL) {
    tscCacheHandle = taosCacheInit(tscTmr, refreshTime);
  }
H
hzcheng 已提交
190

S
slguan 已提交
191
  tscTrace("client is initialized successfully");
H
hzcheng 已提交
192 193 194 195
}

void taos_init() { pthread_once(&tscinit, taos_init_imp); }

196 197 198 199 200 201 202 203 204 205
void taos_cleanup() {
  if (tscCacheHandle != NULL) {
    taosCacheCleanup(tscCacheHandle);
  }
  
  if (tscQhandle != NULL) {
    taosCleanUpScheduler(tscQhandle);
    tscQhandle = NULL;
  }
  
S
slguan 已提交
206
  taosCloseLog();
207 208 209 210 211 212 213 214 215 216 217 218 219 220
  
  if (pVnodeConn != NULL) {
    rpcClose(pVnodeConn);
    pVnodeConn = NULL;
  }
  
  if (pTscMgmtConn != NULL) {
    rpcClose(pTscMgmtConn);
    pTscMgmtConn = NULL;
  }
  
  taosTmrCleanUp(tscTmr);
}

weixin_48148422's avatar
weixin_48148422 已提交
221
static int taos_options_imp(TSDB_OPTION option, const char *pStr) {
S
slguan 已提交
222
  SGlobalCfg *cfg = NULL;
H
hzcheng 已提交
223 224 225

  switch (option) {
    case TSDB_OPTION_CONFIGDIR:
S
slguan 已提交
226
      cfg = taosGetConfigOption("configDir");
H
hjxilinx 已提交
227 228
      assert(cfg != NULL);
    
S
slguan 已提交
229
      if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_OPTION) {
H
hzcheng 已提交
230
        strncpy(configDir, pStr, TSDB_FILENAME_LEN);
S
slguan 已提交
231
        cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION;
H
hzcheng 已提交
232 233
        tscPrint("set config file directory:%s", pStr);
      } else {
weixin_48148422's avatar
weixin_48148422 已提交
234 235
        tscWarn("config option:%s, input value:%s, is configured by %s, use %s", cfg->option, pStr,
                tsCfgStatusStr[cfg->cfgStatus], (char *)cfg->ptr);
H
hzcheng 已提交
236 237
      }
      break;
S
slguan 已提交
238

H
hzcheng 已提交
239
    case TSDB_OPTION_SHELL_ACTIVITY_TIMER:
S
slguan 已提交
240
      cfg = taosGetConfigOption("shellActivityTimer");
H
hjxilinx 已提交
241 242
      assert(cfg != NULL);
    
S
slguan 已提交
243
      if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_OPTION) {
weixin_48148422's avatar
weixin_48148422 已提交
244
        tsShellActivityTimer = atoi(pStr);
H
hzcheng 已提交
245 246
        if (tsShellActivityTimer < 1) tsShellActivityTimer = 1;
        if (tsShellActivityTimer > 3600) tsShellActivityTimer = 3600;
S
slguan 已提交
247
        cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION;
H
hzcheng 已提交
248 249
        tscPrint("set shellActivityTimer:%d", tsShellActivityTimer);
      } else {
weixin_48148422's avatar
weixin_48148422 已提交
250 251
        tscWarn("config option:%s, input value:%s, is configured by %s, use %d", cfg->option, pStr,
                tsCfgStatusStr[cfg->cfgStatus], (int32_t *)cfg->ptr);
H
hzcheng 已提交
252 253
      }
      break;
S
slguan 已提交
254

H
hzcheng 已提交
255
    case TSDB_OPTION_LOCALE: {  // set locale
S
slguan 已提交
256
      cfg = taosGetConfigOption("locale");
H
hjxilinx 已提交
257 258
      assert(cfg != NULL);
  
H
hzcheng 已提交
259 260 261 262 263 264
      size_t len = strlen(pStr);
      if (len == 0 || len > TSDB_LOCALE_LEN) {
        tscPrint("Invalid locale:%s, use default", pStr);
        return -1;
      }

S
slguan 已提交
265
      if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_OPTION) {
H
hzcheng 已提交
266 267
        char sep = '.';

S
slguan 已提交
268 269 270 271 272 273
        if (strlen(tsLocale) == 0) { // locale does not set yet
          char* defaultLocale = setlocale(LC_CTYPE, "");
          strcpy(tsLocale, defaultLocale);
        }

        // set the user specified locale
H
hzcheng 已提交
274 275 276
        char *locale = setlocale(LC_CTYPE, pStr);

        if (locale != NULL) {
S
slguan 已提交
277
          tscPrint("locale set, prev locale:%s, new locale:%s", tsLocale, locale);
S
slguan 已提交
278
          cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION;
S
slguan 已提交
279
        } else { // set the user-specified localed failed, use default LC_CTYPE as current locale
S
slguan 已提交
280 281
          locale = setlocale(LC_CTYPE, tsLocale);
          tscPrint("failed to set locale:%s, current locale:%s", pStr, tsLocale);
H
hzcheng 已提交
282 283
        }

S
slguan 已提交
284
        strncpy(tsLocale, locale, tListLen(tsLocale));
H
hzcheng 已提交
285 286 287 288 289 290 291 292

        char *charset = strrchr(tsLocale, sep);
        if (charset != NULL) {
          charset += 1;

          charset = taosCharsetReplace(charset);

          if (taosValidateEncodec(charset)) {
S
slguan 已提交
293 294 295 296 297 298
            if (strlen(tsCharset) == 0) {
              tscPrint("charset set:%s", charset);
            } else {
              tscPrint("charset changed from %s to %s", tsCharset, charset);
            }

H
hzcheng 已提交
299
            strncpy(tsCharset, charset, tListLen(tsCharset));
S
slguan 已提交
300
            cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION;
S
slguan 已提交
301

H
hzcheng 已提交
302 303 304
          } else {
            tscPrint("charset:%s is not valid in locale, charset remains:%s", charset, tsCharset);
          }
S
slguan 已提交
305

H
hzcheng 已提交
306
          free(charset);
S
slguan 已提交
307
        } else { // it may be windows system
H
hzcheng 已提交
308 309 310
          tscPrint("charset remains:%s", tsCharset);
        }
      } else {
weixin_48148422's avatar
weixin_48148422 已提交
311 312
        tscWarn("config option:%s, input value:%s, is configured by %s, use %s", cfg->option, pStr,
                tsCfgStatusStr[cfg->cfgStatus], (char *)cfg->ptr);
H
hzcheng 已提交
313 314 315 316 317 318
      }
      break;
    }

    case TSDB_OPTION_CHARSET: {
      /* set charset will override the value of charset, assigned during system locale changed */
S
slguan 已提交
319
      cfg = taosGetConfigOption("charset");
H
hjxilinx 已提交
320 321
      assert(cfg != NULL);
      
H
hzcheng 已提交
322 323
      size_t len = strlen(pStr);
      if (len == 0 || len > TSDB_LOCALE_LEN) {
S
slguan 已提交
324
        tscPrint("failed to set charset:%s", pStr);
H
hzcheng 已提交
325 326 327
        return -1;
      }

S
slguan 已提交
328
      if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_OPTION) {
H
hzcheng 已提交
329
        if (taosValidateEncodec(pStr)) {
S
slguan 已提交
330 331 332 333 334 335
          if (strlen(tsCharset) == 0) {
            tscPrint("charset is set:%s", pStr);
          } else {
            tscPrint("charset changed from %s to %s", tsCharset, pStr);
          }

H
hzcheng 已提交
336
          strncpy(tsCharset, pStr, tListLen(tsCharset));
S
slguan 已提交
337
          cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION;
H
hzcheng 已提交
338
        } else {
S
slguan 已提交
339
          tscPrint("charset:%s not valid", pStr);
H
hzcheng 已提交
340 341
        }
      } else {
weixin_48148422's avatar
weixin_48148422 已提交
342 343
        tscWarn("config option:%s, input value:%s, is configured by %s, use %s", cfg->option, pStr,
                tsCfgStatusStr[cfg->cfgStatus], (char *)cfg->ptr);
H
hzcheng 已提交
344 345 346 347 348 349
      }

      break;
    }

    case TSDB_OPTION_TIMEZONE:
S
slguan 已提交
350
      cfg = taosGetConfigOption("timezone");
H
hjxilinx 已提交
351 352
      assert(cfg != NULL);
    
S
slguan 已提交
353
      if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_OPTION) {
H
hzcheng 已提交
354 355
        strcpy(tsTimezone, pStr);
        tsSetTimeZone();
S
slguan 已提交
356
        cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION;
H
hzcheng 已提交
357 358
        tscTrace("timezone set:%s, input:%s by taos_options", tsTimezone, pStr);
      } else {
weixin_48148422's avatar
weixin_48148422 已提交
359 360
        tscWarn("config option:%s, input value:%s, is configured by %s, use %s", cfg->option, pStr,
                tsCfgStatusStr[cfg->cfgStatus], (char *)cfg->ptr);
H
hzcheng 已提交
361 362
      }
      break;
363 364

    case TSDB_OPTION_SOCKET_TYPE:
S
slguan 已提交
365
      cfg = taosGetConfigOption("sockettype");
H
hjxilinx 已提交
366 367
      assert(cfg != NULL);
    
S
slguan 已提交
368
      if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_OPTION) {
S
slguan 已提交
369 370 371 372
//        if (strcasecmp(pStr, TAOS_SOCKET_TYPE_NAME_UDP) != 0 && strcasecmp(pStr, TAOS_SOCKET_TYPE_NAME_TCP) != 0) {
//          tscError("only 'tcp' or 'udp' allowed for configuring the socket type");
//          return -1;
//        }
373

weixin_48148422's avatar
weixin_48148422 已提交
374
        strncpy(tsSocketType, pStr, tListLen(tsSocketType));
S
slguan 已提交
375
        cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION;
376 377 378 379
        tscPrint("socket type is set:%s", tsSocketType);
      }
      break;

H
hzcheng 已提交
380
    default:
H
hjxilinx 已提交
381
      // TODO return the correct error code to client in the format for taos_errstr()
H
hzcheng 已提交
382 383 384 385 386 387
      tscError("Invalid option %d", option);
      return -1;
  }

  return 0;
}
weixin_48148422's avatar
weixin_48148422 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403


int taos_options(TSDB_OPTION option, const void *arg, ...) {
  static int32_t lock = 0;

  for (int i = 1; atomic_val_compare_exchange_32(&lock, 0, 1) != 0; ++i) {
    if (i % 1000 == 0) {
      tscPrint("haven't acquire lock after spin %d times.", i);
      sched_yield();
    }
  }

  int ret = taos_options_imp(option, (const char*)arg);

  atomic_store_32(&lock, 0);
  return ret;
L
lihui 已提交
404
}