tscSystem.c 11.2 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
#include "taosmsg.h"
#include "tcache.h"
#include "tlog.h"
#include "trpc.h"
S
slguan 已提交
21
#include "taosdef.h"
H
hzcheng 已提交
22 23 24 25 26
#include "tsocket.h"
#include "tsystem.h"
#include "ttime.h"
#include "ttimer.h"
#include "tutil.h"
S
slguan 已提交
27
#include "tsched.h"
H
hzcheng 已提交
28
#include "tsclient.h"
S
slguan 已提交
29

H
hzcheng 已提交
30 31 32 33 34 35 36 37 38 39 40 41
// global, not configurable
void *  pVnodeConn;
void *  pVMeterConn;
void *  pTscMgmtConn;
void *  pSlaveConn;
void *  tscCacheHandle;
uint8_t globalCode = 0;
int     initialized = 0;
int     slaveIndex;
void *  tscTmr;
void *  tscQhandle;
void *  tscConnCache;
S
slguan 已提交
42
void *  tscCheckDiskUsageTmr;
H
hzcheng 已提交
43 44 45 46 47 48
int     tsInsertHeadSize;

extern int            tscEmbedded;
int                   tscNumOfThreads;
static pthread_once_t tscinit = PTHREAD_ONCE_INIT;

L
lihui 已提交
49 50 51
extern int  tsTscEnableRecordSql;
extern int  tsNumOfLogLines;
void taosInitNote(int numOfNoteLines, int maxNotes, char* lable);
L
lihui 已提交
52
void deltaToUtcInitOnce();
L
lihui 已提交
53

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

H
hzcheng 已提交
59 60 61 62 63 64
void taos_init_imp() {
  char        temp[128];
  struct stat dirstat;
  SRpcInit    rpcInit;

  srand(taosGetTimestampSec());
L
lihui 已提交
65
  deltaToUtcInitOnce();
H
hzcheng 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

  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.
    tsReadGlobalLogConfig();

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

    sprintf(temp, "%s/taoslog", logDir);
    if (taosInitLog(temp, tsNumOfLogLines, 10) < 0) {
S
slguan 已提交
83
      printf("failed to open log file in directory:%s\n", logDir);
H
hzcheng 已提交
84 85 86 87 88 89 90 91 92
    }

    tsReadGlobalConfig();
    tsPrintGlobalConfig();

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

L
lihui 已提交
93 94
  taosSetCoreDump();

L
lihui 已提交
95 96 97
  if (tsTscEnableRecordSql != 0) {
    taosInitNote(tsNumOfLogLines / 10, 1, (char*)"tsc_note");
  }
S
slguan 已提交
98

S
slguan 已提交
99 100 101 102
  tscMgmtIpList.index = 0;
  tscMgmtIpList.port = tsMgmtShellPort;
  tscMgmtIpList.numOfIps = 1;
  tscMgmtIpList.ip[0] = inet_addr(tsMasterIp);
S
slguan 已提交
103

S
slguan 已提交
104
  if (tsSecondIp[0] && strcmp(tsSecondIp, tsMasterIp) != 0) {
S
slguan 已提交
105 106
    tscMgmtIpList.numOfIps = 2;
    tscMgmtIpList.ip[1] = inet_addr(tsSecondIp);
S
slguan 已提交
107 108
  }

H
hzcheng 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121
  tscInitMsgs();
  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 已提交
122 123 124 125
  if (NULL == tscQhandle) {
    tscError("failed to init scheduler");
    return;
  }
H
hzcheng 已提交
126 127

  memset(&rpcInit, 0, sizeof(rpcInit));
S
slguan 已提交
128
  rpcInit.localIp = "0.0.0.0";//tsLocalIp;
H
hzcheng 已提交
129 130 131
  rpcInit.localPort = 0;
  rpcInit.label = "TSC-vnode";
  rpcInit.numOfThreads = tscNumOfThreads;
S
slguan 已提交
132
  rpcInit.cfp = tscProcessMsgFromServer;
S
slguan 已提交
133
  rpcInit.sessions = tsMaxVnodeConnections;
S
slguan 已提交
134
  rpcInit.connType = TAOS_CONN_CLIENT;
S
slguan 已提交
135
  pVnodeConn = rpcOpen(&rpcInit);
H
hzcheng 已提交
136 137 138 139 140 141
  if (pVnodeConn == NULL) {
    tscError("failed to init connection to vnode");
    return;
  }

  memset(&rpcInit, 0, sizeof(rpcInit));
S
slguan 已提交
142
  rpcInit.localIp = "0.0.0.0";//tsLocalIp;
H
hzcheng 已提交
143 144 145
  rpcInit.localPort = 0;
  rpcInit.label = "TSC-mgmt";
  rpcInit.numOfThreads = 1;
S
slguan 已提交
146
  rpcInit.cfp = tscProcessMsgFromServer;
S
slguan 已提交
147
  rpcInit.sessions = tsMaxMgmtConnections;
S
slguan 已提交
148 149 150 151 152 153 154 155 156
  rpcInit.connType = TAOS_CONN_CLIENT;
  rpcInit.idleTime = 2000;
  rpcInit.user         = "root";
  rpcInit.ckey         = "key";

  char secret[32] = {0};
  taosEncryptPass((uint8_t *)"taosdata", strlen("taosdata"), secret);
  rpcInit.secret = secret;

S
slguan 已提交
157
  pTscMgmtConn = rpcOpen(&rpcInit);
H
hzcheng 已提交
158 159 160 161 162 163
  if (pTscMgmtConn == NULL) {
    tscError("failed to init connection to mgmt");
    return;
  }

  tscTmr = taosTmrInit(tsMaxMgmtConnections * 2, 200, 60000, "TSC");
S
slguan 已提交
164 165
  if(0 == tscEmbedded){
    taosTmrReset(tscCheckDiskUsage, 10, NULL, tscTmr, &tscCheckDiskUsageTmr);      
S
slguan 已提交
166
  }
H
hzcheng 已提交
167 168 169 170 171 172
  int64_t refreshTime = tsMetricMetaKeepTimer < tsMeterMetaKeepTimer ? tsMetricMetaKeepTimer : tsMeterMetaKeepTimer;
  refreshTime = refreshTime > 2 ? 2 : refreshTime;
  refreshTime = refreshTime < 1 ? 1 : refreshTime;

  if (tscCacheHandle == NULL) tscCacheHandle = taosInitDataCache(tsMaxMeterConnections / 2, tscTmr, refreshTime);

S
slguan 已提交
173
  tscConnCache = taosOpenConnCache(tsMaxMeterConnections * 2, NULL/*taosCloseRpcConn*/, tscTmr, tsShellActivityTimer * 1000);
H
hzcheng 已提交
174 175

  initialized = 1;
S
slguan 已提交
176
  tscTrace("client is initialized successfully");
H
hzcheng 已提交
177 178 179 180 181
  tsInsertHeadSize = tsRpcHeadSize + sizeof(SShellSubmitMsg);
}

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

weixin_48148422's avatar
weixin_48148422 已提交
182 183
static int taos_options_imp(TSDB_OPTION option, const char *pStr) {
  SGlobalConfig *cfg = NULL;
H
hzcheng 已提交
184 185 186

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

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

H
hzcheng 已提交
216
    case TSDB_OPTION_LOCALE: {  // set locale
weixin_48148422's avatar
weixin_48148422 已提交
217
      cfg = tsGetConfigOption("locale");
H
hjxilinx 已提交
218 219
      assert(cfg != NULL);
  
H
hzcheng 已提交
220 221 222 223 224 225
      size_t len = strlen(pStr);
      if (len == 0 || len > TSDB_LOCALE_LEN) {
        tscPrint("Invalid locale:%s, use default", pStr);
        return -1;
      }

H
hjxilinx 已提交
226
      if (cfg->cfgStatus <= TSDB_CFG_CSTATUS_OPTION) {
H
hzcheng 已提交
227 228
        char sep = '.';

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

        // set the user specified locale
H
hzcheng 已提交
235 236 237
        char *locale = setlocale(LC_CTYPE, pStr);

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

S
slguan 已提交
245
        strncpy(tsLocale, locale, tListLen(tsLocale));
H
hzcheng 已提交
246 247 248 249 250 251 252 253

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

          charset = taosCharsetReplace(charset);

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

H
hzcheng 已提交
260
            strncpy(tsCharset, charset, tListLen(tsCharset));
weixin_48148422's avatar
weixin_48148422 已提交
261
            cfg->cfgStatus = TSDB_CFG_CSTATUS_OPTION;
S
slguan 已提交
262

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

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

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

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

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

      break;
    }

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

    case TSDB_OPTION_SOCKET_TYPE:
weixin_48148422's avatar
weixin_48148422 已提交
326
      cfg = tsGetConfigOption("sockettype");
H
hjxilinx 已提交
327 328 329
      assert(cfg != NULL);
    
      if (cfg->cfgStatus <= TSDB_CFG_CSTATUS_OPTION) {
S
slguan 已提交
330 331 332 333
//        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;
//        }
334

weixin_48148422's avatar
weixin_48148422 已提交
335 336
        strncpy(tsSocketType, pStr, tListLen(tsSocketType));
        cfg->cfgStatus = TSDB_CFG_CSTATUS_OPTION;
337 338 339 340
        tscPrint("socket type is set:%s", tsSocketType);
      }
      break;

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

  return 0;
}
weixin_48148422's avatar
weixin_48148422 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364


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