tscSystem.c 10.7 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 20 21 22 23
#include "taosmsg.h"
#include "tcache.h"
#include "trpc.h"
#include "tsystem.h"
#include "ttime.h"
#include "ttimer.h"
#include "tutil.h"
S
slguan 已提交
24
#include "tsched.h"
S
slguan 已提交
25
#include "tscLog.h"
H
hzcheng 已提交
26
#include "tsclient.h"
S
slguan 已提交
27 28 29 30
#include "tglobal.h"
#include "tconfig.h"
#include "ttimezone.h"
#include "tlocale.h"
S
slguan 已提交
31

H
hzcheng 已提交
32
// global, not configurable
J
jtao1735 已提交
33
void *  pDnodeConn;
H
hzcheng 已提交
34 35 36
void *  tscCacheHandle;
void *  tscTmr;
void *  tscQhandle;
S
slguan 已提交
37
void *  tscCheckDiskUsageTmr;
H
hzcheng 已提交
38 39
int     tsInsertHeadSize;

40
int tscNumOfThreads;
H
hzcheng 已提交
41

42
static pthread_once_t tscinit = PTHREAD_ONCE_INIT;
L
lihui 已提交
43
void taosInitNote(int numOfNoteLines, int maxNotes, char* lable);
S
slguan 已提交
44
void tscUpdateIpSet(void *ahandle, SRpcIpSet *pIpSet);
L
lihui 已提交
45

46
void tscCheckDiskUsage(void *UNUSED_PARAM(para), void* UNUSED_PARAM(param)) {
S
slguan 已提交
47 48 49 50
  taosGetDisk();
  taosTmrReset(tscCheckDiskUsage, 1000, NULL, tscTmr, &tscCheckDiskUsageTmr);
}

J
jtao1735 已提交
51
int32_t tscInitRpc(const char *user, const char *secret) {
S
slguan 已提交
52 53 54 55
  SRpcInit rpcInit;
  char secretEncrypt[32] = {0};
  taosEncryptPass((uint8_t *)secret, strlen(secret), secretEncrypt);

J
jtao1735 已提交
56
  if (pDnodeConn == NULL) {
S
slguan 已提交
57 58
    memset(&rpcInit, 0, sizeof(rpcInit));
    rpcInit.localPort = 0;
J
jtao1735 已提交
59
    rpcInit.label = "TSC";
S
slguan 已提交
60 61 62 63
    rpcInit.numOfThreads = tscNumOfThreads;
    rpcInit.cfp = tscProcessMsgFromServer;
    rpcInit.sessions = tsMaxVnodeConnections;
    rpcInit.connType = TAOS_CONN_CLIENT;
64
    rpcInit.user = (char*)user;
H
hzcheng 已提交
65
    rpcInit.idleTime = 2000;
S
slguan 已提交
66 67 68
    rpcInit.ckey = "key";
    rpcInit.secret = secretEncrypt;

J
jtao1735 已提交
69 70
    pDnodeConn = rpcOpen(&rpcInit);
    if (pDnodeConn == NULL) {
S
slguan 已提交
71 72 73 74 75 76 77 78
      tscError("failed to init connection to vnode");
      return -1;
    }
  }

  return 0;
}

H
hzcheng 已提交
79 80 81 82
void taos_init_imp() {
  char        temp[128];
  struct stat dirstat;

83
  errno = TSDB_CODE_SUCCESS;
H
hzcheng 已提交
84
  srand(taosGetTimestampSec());
L
lihui 已提交
85
  deltaToUtcInitOnce();
H
hzcheng 已提交
86 87 88 89

  if (tscEmbedded == 0) {

    // Read global configuration.
S
slguan 已提交
90 91
    taosInitGlobalCfg();
    taosReadGlobalLogCfg();
H
hzcheng 已提交
92 93

    // For log directory
S
slguan 已提交
94
    if (stat(tsLogDir, &dirstat) < 0) mkdir(tsLogDir, 0755);
H
hzcheng 已提交
95

S
slguan 已提交
96
    sprintf(temp, "%s/taoslog", tsLogDir);
H
hzcheng 已提交
97
    if (taosInitLog(temp, tsNumOfLogLines, 10) < 0) {
S
slguan 已提交
98
      printf("failed to open log file in directory:%s\n", tsLogDir);
H
hzcheng 已提交
99 100
    }

S
slguan 已提交
101 102 103
    taosReadGlobalCfg();
    taosCheckGlobalCfg();
    taosPrintGlobalCfg();
H
hzcheng 已提交
104 105

    tscTrace("starting to initialize TAOS client ...");
J
jtao1735 已提交
106
    tscTrace("Local End Point is:%s", tsLocalEp);
H
hzcheng 已提交
107 108
  }

L
lihui 已提交
109 110
  taosSetCoreDump();

L
lihui 已提交
111 112 113
  if (tsTscEnableRecordSql != 0) {
    taosInitNote(tsNumOfLogLines / 10, 1, (char*)"tsc_note");
  }
S
slguan 已提交
114

S
slguan 已提交
115 116
  tscMgmtIpSet.inUse = 0;
  tscMgmtIpSet.numOfIps = 1;
S
slguan 已提交
117
  taosGetFqdnPortFromEp(tsFirst, tscMgmtIpSet.fqdn[0], &tscMgmtIpSet.port[0]);
S
slguan 已提交
118

S
slguan 已提交
119
  if (tsSecond[0] && strcmp(tsSecond, tsFirst) != 0) {
S
slguan 已提交
120
    tscMgmtIpSet.numOfIps = 2;
J
jtao1735 已提交
121
    taosGetFqdnPortFromEp(tsSecond, tscMgmtIpSet.fqdn[1], &tscMgmtIpSet.port[1]);
S
slguan 已提交
122 123
  }

124
  tscInitMsgsFp();
H
hzcheng 已提交
125 126 127 128 129 130 131 132 133 134 135
  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 已提交
136 137 138 139
  if (NULL == tscQhandle) {
    tscError("failed to init scheduler");
    return;
  }
H
hzcheng 已提交
140 141

  tscTmr = taosTmrInit(tsMaxMgmtConnections * 2, 200, 60000, "TSC");
S
slguan 已提交
142 143
  if(0 == tscEmbedded){
    taosTmrReset(tscCheckDiskUsage, 10, NULL, tscTmr, &tscCheckDiskUsageTmr);      
S
slguan 已提交
144
  }
145
  
H
hjxilinx 已提交
146
  int64_t refreshTime = tsTableMetaKeepTimer;
H
hzcheng 已提交
147 148 149
  refreshTime = refreshTime > 2 ? 2 : refreshTime;
  refreshTime = refreshTime < 1 ? 1 : refreshTime;

150 151 152
  if (tscCacheHandle == NULL) {
    tscCacheHandle = taosCacheInit(tscTmr, refreshTime);
  }
H
hzcheng 已提交
153

S
slguan 已提交
154
  tscTrace("client is initialized successfully");
H
hzcheng 已提交
155 156 157 158
}

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

159 160 161 162 163 164 165 166 167 168
void taos_cleanup() {
  if (tscCacheHandle != NULL) {
    taosCacheCleanup(tscCacheHandle);
  }
  
  if (tscQhandle != NULL) {
    taosCleanUpScheduler(tscQhandle);
    tscQhandle = NULL;
  }
  
S
slguan 已提交
169
  taosCloseLog();
170
  
J
jtao1735 已提交
171 172 173
  if (pDnodeConn != NULL) {
    rpcClose(pDnodeConn);
    pDnodeConn = NULL;
174 175 176 177 178
  }
  
  taosTmrCleanUp(tscTmr);
}

weixin_48148422's avatar
weixin_48148422 已提交
179
static int taos_options_imp(TSDB_OPTION option, const char *pStr) {
S
slguan 已提交
180
  SGlobalCfg *cfg = NULL;
H
hzcheng 已提交
181 182 183

  switch (option) {
    case TSDB_OPTION_CONFIGDIR:
S
slguan 已提交
184
      cfg = taosGetConfigOption("configDir");
H
hjxilinx 已提交
185 186
      assert(cfg != NULL);
    
S
slguan 已提交
187
      if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_OPTION) {
H
hzcheng 已提交
188
        strncpy(configDir, pStr, TSDB_FILENAME_LEN);
S
slguan 已提交
189
        cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION;
H
hzcheng 已提交
190 191
        tscPrint("set config file directory:%s", pStr);
      } else {
weixin_48148422's avatar
weixin_48148422 已提交
192 193
        tscWarn("config option:%s, input value:%s, is configured by %s, use %s", cfg->option, pStr,
                tsCfgStatusStr[cfg->cfgStatus], (char *)cfg->ptr);
H
hzcheng 已提交
194 195
      }
      break;
S
slguan 已提交
196

H
hzcheng 已提交
197
    case TSDB_OPTION_SHELL_ACTIVITY_TIMER:
S
slguan 已提交
198
      cfg = taosGetConfigOption("shellActivityTimer");
H
hjxilinx 已提交
199 200
      assert(cfg != NULL);
    
S
slguan 已提交
201
      if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_OPTION) {
weixin_48148422's avatar
weixin_48148422 已提交
202
        tsShellActivityTimer = atoi(pStr);
H
hzcheng 已提交
203 204
        if (tsShellActivityTimer < 1) tsShellActivityTimer = 1;
        if (tsShellActivityTimer > 3600) tsShellActivityTimer = 3600;
S
slguan 已提交
205
        cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION;
H
hzcheng 已提交
206 207
        tscPrint("set shellActivityTimer:%d", tsShellActivityTimer);
      } else {
weixin_48148422's avatar
weixin_48148422 已提交
208 209
        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 已提交
210 211
      }
      break;
S
slguan 已提交
212

H
hzcheng 已提交
213
    case TSDB_OPTION_LOCALE: {  // set locale
S
slguan 已提交
214
      cfg = taosGetConfigOption("locale");
H
hjxilinx 已提交
215 216
      assert(cfg != NULL);
  
H
hzcheng 已提交
217 218 219 220 221 222
      size_t len = strlen(pStr);
      if (len == 0 || len > TSDB_LOCALE_LEN) {
        tscPrint("Invalid locale:%s, use default", pStr);
        return -1;
      }

S
slguan 已提交
223
      if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_OPTION) {
H
hzcheng 已提交
224 225
        char sep = '.';

S
slguan 已提交
226 227 228 229 230 231
        if (strlen(tsLocale) == 0) { // locale does not set yet
          char* defaultLocale = setlocale(LC_CTYPE, "");
          strcpy(tsLocale, defaultLocale);
        }

        // set the user specified locale
H
hzcheng 已提交
232 233 234
        char *locale = setlocale(LC_CTYPE, pStr);

        if (locale != NULL) {
S
slguan 已提交
235
          tscPrint("locale set, prev locale:%s, new locale:%s", tsLocale, locale);
S
slguan 已提交
236
          cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION;
S
slguan 已提交
237
        } else { // set the user-specified localed failed, use default LC_CTYPE as current locale
S
slguan 已提交
238 239
          locale = setlocale(LC_CTYPE, tsLocale);
          tscPrint("failed to set locale:%s, current locale:%s", pStr, tsLocale);
H
hzcheng 已提交
240 241
        }

S
slguan 已提交
242
        strncpy(tsLocale, locale, tListLen(tsLocale));
H
hzcheng 已提交
243 244 245 246 247 248 249 250

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

          charset = taosCharsetReplace(charset);

          if (taosValidateEncodec(charset)) {
S
slguan 已提交
251 252 253 254 255 256
            if (strlen(tsCharset) == 0) {
              tscPrint("charset set:%s", charset);
            } else {
              tscPrint("charset changed from %s to %s", tsCharset, charset);
            }

H
hzcheng 已提交
257
            strncpy(tsCharset, charset, tListLen(tsCharset));
S
slguan 已提交
258
            cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION;
S
slguan 已提交
259

H
hzcheng 已提交
260 261 262
          } else {
            tscPrint("charset:%s is not valid in locale, charset remains:%s", charset, tsCharset);
          }
S
slguan 已提交
263

H
hzcheng 已提交
264
          free(charset);
S
slguan 已提交
265
        } else { // it may be windows system
H
hzcheng 已提交
266 267 268
          tscPrint("charset remains:%s", tsCharset);
        }
      } else {
weixin_48148422's avatar
weixin_48148422 已提交
269 270
        tscWarn("config option:%s, input value:%s, is configured by %s, use %s", cfg->option, pStr,
                tsCfgStatusStr[cfg->cfgStatus], (char *)cfg->ptr);
H
hzcheng 已提交
271 272 273 274 275 276
      }
      break;
    }

    case TSDB_OPTION_CHARSET: {
      /* set charset will override the value of charset, assigned during system locale changed */
S
slguan 已提交
277
      cfg = taosGetConfigOption("charset");
H
hjxilinx 已提交
278 279
      assert(cfg != NULL);
      
H
hzcheng 已提交
280 281
      size_t len = strlen(pStr);
      if (len == 0 || len > TSDB_LOCALE_LEN) {
S
slguan 已提交
282
        tscPrint("failed to set charset:%s", pStr);
H
hzcheng 已提交
283 284 285
        return -1;
      }

S
slguan 已提交
286
      if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_OPTION) {
H
hzcheng 已提交
287
        if (taosValidateEncodec(pStr)) {
S
slguan 已提交
288 289 290 291 292 293
          if (strlen(tsCharset) == 0) {
            tscPrint("charset is set:%s", pStr);
          } else {
            tscPrint("charset changed from %s to %s", tsCharset, pStr);
          }

H
hzcheng 已提交
294
          strncpy(tsCharset, pStr, tListLen(tsCharset));
S
slguan 已提交
295
          cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION;
H
hzcheng 已提交
296
        } else {
S
slguan 已提交
297
          tscPrint("charset:%s not valid", pStr);
H
hzcheng 已提交
298 299
        }
      } else {
weixin_48148422's avatar
weixin_48148422 已提交
300 301
        tscWarn("config option:%s, input value:%s, is configured by %s, use %s", cfg->option, pStr,
                tsCfgStatusStr[cfg->cfgStatus], (char *)cfg->ptr);
H
hzcheng 已提交
302 303 304 305 306 307
      }

      break;
    }

    case TSDB_OPTION_TIMEZONE:
S
slguan 已提交
308
      cfg = taosGetConfigOption("timezone");
H
hjxilinx 已提交
309 310
      assert(cfg != NULL);
    
S
slguan 已提交
311
      if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_OPTION) {
H
hzcheng 已提交
312 313
        strcpy(tsTimezone, pStr);
        tsSetTimeZone();
S
slguan 已提交
314
        cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION;
H
hzcheng 已提交
315 316
        tscTrace("timezone set:%s, input:%s by taos_options", tsTimezone, pStr);
      } else {
weixin_48148422's avatar
weixin_48148422 已提交
317 318
        tscWarn("config option:%s, input value:%s, is configured by %s, use %s", cfg->option, pStr,
                tsCfgStatusStr[cfg->cfgStatus], (char *)cfg->ptr);
H
hzcheng 已提交
319 320
      }
      break;
321 322

    case TSDB_OPTION_SOCKET_TYPE:
S
slguan 已提交
323
      cfg = taosGetConfigOption("sockettype");
H
hjxilinx 已提交
324 325
      assert(cfg != NULL);
    
S
slguan 已提交
326
      if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_OPTION) {
S
slguan 已提交
327 328 329 330
//        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;
//        }
331

weixin_48148422's avatar
weixin_48148422 已提交
332
        strncpy(tsSocketType, pStr, tListLen(tsSocketType));
S
slguan 已提交
333
        cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION;
334 335 336 337
        tscPrint("socket type is set:%s", tsSocketType);
      }
      break;

H
hzcheng 已提交
338
    default:
H
hjxilinx 已提交
339
      // TODO return the correct error code to client in the format for taos_errstr()
H
hzcheng 已提交
340 341 342 343 344 345
      tscError("Invalid option %d", option);
      return -1;
  }

  return 0;
}
weixin_48148422's avatar
weixin_48148422 已提交
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360

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 已提交
361
}