tscSystem.c 10.6 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"
26
#include "tscUtil.h"
H
hzcheng 已提交
27
#include "tsclient.h"
S
slguan 已提交
28 29 30 31
#include "tglobal.h"
#include "tconfig.h"
#include "ttimezone.h"
#include "tlocale.h"
S
slguan 已提交
32

H
hzcheng 已提交
33 34 35 36
// global, not configurable
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);
}

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

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
    rpcInit.ckey = "key";
S
Shengliang Guan 已提交
67
    rpcInit.spi = 1;
S
slguan 已提交
68 69
    rpcInit.secret = secretEncrypt;

70 71 72
    *pDnodeConn = rpcOpen(&rpcInit);
    if (*pDnodeConn == NULL) {
      tscError("failed to init connection to TDengine");
S
slguan 已提交
73
      return -1;
74 75
    } else {
      tscTrace("dnodeConn:%p is created, user:%s", *pDnodeConn, user);
S
slguan 已提交
76 77 78 79 80 81
    }
  }

  return 0;
}

H
hzcheng 已提交
82 83 84 85
void taos_init_imp() {
  char        temp[128];
  struct stat dirstat;

86
  errno = TSDB_CODE_SUCCESS;
H
hzcheng 已提交
87
  srand(taosGetTimestampSec());
L
lihui 已提交
88
  deltaToUtcInitOnce();
H
hzcheng 已提交
89 90 91 92

  if (tscEmbedded == 0) {

    // Read global configuration.
S
slguan 已提交
93 94
    taosInitGlobalCfg();
    taosReadGlobalLogCfg();
H
hzcheng 已提交
95 96

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

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

S
slguan 已提交
104 105 106
    taosReadGlobalCfg();
    taosCheckGlobalCfg();
    taosPrintGlobalCfg();
H
hzcheng 已提交
107 108

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

L
lihui 已提交
112 113
  taosSetCoreDump();

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

118
  if (tscSetMgmtIpListFromCfg(tsFirst, tsSecond) < 0) {
S
Shengliang Guan 已提交
119
    tscError("failed to init mnode IP list");
120 121
    return;
  } 
S
slguan 已提交
122

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

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

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

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

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

158 159 160 161 162 163 164 165 166 167
void taos_cleanup() {
  if (tscCacheHandle != NULL) {
    taosCacheCleanup(tscCacheHandle);
  }
  
  if (tscQhandle != NULL) {
    taosCleanUpScheduler(tscQhandle);
    tscQhandle = NULL;
  }
  
S
slguan 已提交
168
  taosCloseLog();
169 170 171 172
  
  taosTmrCleanUp(tscTmr);
}

weixin_48148422's avatar
weixin_48148422 已提交
173
static int taos_options_imp(TSDB_OPTION option, const char *pStr) {
S
slguan 已提交
174
  SGlobalCfg *cfg = NULL;
H
hzcheng 已提交
175 176 177

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

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

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

S
slguan 已提交
217
      if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_OPTION) {
H
hzcheng 已提交
218 219
        char sep = '.';

S
slguan 已提交
220 221 222 223 224 225
        if (strlen(tsLocale) == 0) { // locale does not set yet
          char* defaultLocale = setlocale(LC_CTYPE, "");
          strcpy(tsLocale, defaultLocale);
        }

        // set the user specified locale
H
hzcheng 已提交
226 227 228
        char *locale = setlocale(LC_CTYPE, pStr);

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

S
slguan 已提交
236
        strncpy(tsLocale, locale, tListLen(tsLocale));
H
hzcheng 已提交
237 238 239 240 241 242 243 244

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

          charset = taosCharsetReplace(charset);

          if (taosValidateEncodec(charset)) {
S
slguan 已提交
245 246 247 248 249 250
            if (strlen(tsCharset) == 0) {
              tscPrint("charset set:%s", charset);
            } else {
              tscPrint("charset changed from %s to %s", tsCharset, charset);
            }

H
hzcheng 已提交
251
            strncpy(tsCharset, charset, tListLen(tsCharset));
S
slguan 已提交
252
            cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION;
S
slguan 已提交
253

H
hzcheng 已提交
254 255 256
          } else {
            tscPrint("charset:%s is not valid in locale, charset remains:%s", charset, tsCharset);
          }
S
slguan 已提交
257

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

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

S
slguan 已提交
280
      if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_OPTION) {
H
hzcheng 已提交
281
        if (taosValidateEncodec(pStr)) {
S
slguan 已提交
282 283 284 285 286 287
          if (strlen(tsCharset) == 0) {
            tscPrint("charset is set:%s", pStr);
          } else {
            tscPrint("charset changed from %s to %s", tsCharset, pStr);
          }

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

      break;
    }

    case TSDB_OPTION_TIMEZONE:
S
slguan 已提交
302
      cfg = taosGetConfigOption("timezone");
H
hjxilinx 已提交
303 304
      assert(cfg != NULL);
    
S
slguan 已提交
305
      if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_OPTION) {
H
hzcheng 已提交
306 307
        strcpy(tsTimezone, pStr);
        tsSetTimeZone();
S
slguan 已提交
308
        cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION;
H
hzcheng 已提交
309 310
        tscTrace("timezone set:%s, input:%s by taos_options", tsTimezone, pStr);
      } 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
      }
      break;
315 316

    case TSDB_OPTION_SOCKET_TYPE:
S
slguan 已提交
317
      cfg = taosGetConfigOption("sockettype");
H
hjxilinx 已提交
318 319
      assert(cfg != NULL);
    
S
slguan 已提交
320
      if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_OPTION) {
S
slguan 已提交
321 322 323 324
//        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;
//        }
325

weixin_48148422's avatar
weixin_48148422 已提交
326
        strncpy(tsSocketType, pStr, tListLen(tsSocketType));
S
slguan 已提交
327
        cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION;
328 329 330 331
        tscPrint("socket type is set:%s", tsSocketType);
      }
      break;

H
hzcheng 已提交
332
    default:
H
hjxilinx 已提交
333
      // TODO return the correct error code to client in the format for taos_errstr()
H
hzcheng 已提交
334 335 336 337 338 339
      tscError("Invalid option %d", option);
      return -1;
  }

  return 0;
}
weixin_48148422's avatar
weixin_48148422 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354

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