shellImport.c 7.4 KB
Newer Older
S
#928  
slguan 已提交
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/>.
 */

16
#define _GNU_SOURCE
S
#928  
slguan 已提交
17 18 19 20 21 22
#define _XOPEN_SOURCE
#define _DEFAULT_SOURCE

#include "os.h"
#include "shell.h"
#include "shellCommand.h"
S
slguan 已提交
23
#include "tglobal.h"
S
#928  
slguan 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
#include "tutil.h"

static char **shellSQLFiles = NULL;
static int32_t shellSQLFileNum = 0;
static char shellTablesSQLFile[TSDB_FILENAME_LEN] = {0};

typedef struct {
  pthread_t threadID;
  int       threadIndex;
  int       totalThreads;
  void     *taos;
} ShellThreadObj;

static int shellGetFilesNum(const char *directoryName, const char *prefix)
{
  char cmd[1024] = { 0 };
  sprintf(cmd, "ls %s/*.%s | wc -l ", directoryName, prefix);

  FILE *fp = popen(cmd, "r");
  if (fp == NULL) {
    fprintf(stderr, "ERROR: failed to execute:%s, error:%s\n", cmd, strerror(errno));
    exit(0);
  }

  int fileNum = 0;
  if (fscanf(fp, "%d", &fileNum) != 1) {
    fprintf(stderr, "ERROR: failed to execute:%s, parse result error\n", cmd);
    exit(0);
  }

  if (fileNum <= 0) {
    fprintf(stderr, "ERROR: directory:%s is empry\n", directoryName);
    exit(0);
  }

  pclose(fp);
  return fileNum;
}

static void shellParseDirectory(const char *directoryName, const char *prefix, char **fileArray, int totalFiles)
{
  char cmd[1024] = { 0 };
  sprintf(cmd, "ls %s/*.%s | sort", directoryName, prefix);

  FILE *fp = popen(cmd, "r");
  if (fp == NULL) {
    fprintf(stderr, "ERROR: failed to execute:%s, error:%s\n", cmd, strerror(errno));
    exit(0);
  }

  int fileNum = 0;
H
Hui Li 已提交
75
  while (fscanf(fp, "%128s", fileArray[fileNum++])) {
S
#928  
slguan 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    if (strcmp(fileArray[fileNum-1], shellTablesSQLFile) == 0) {
      fileNum--;
    }
    if (fileNum >= totalFiles) {
      break;
    }
  }

  if (fileNum != totalFiles) {
    fprintf(stderr, "ERROR: directory:%s changed while read\n", directoryName);
    exit(0);
  }

  pclose(fp);
}

static void shellCheckTablesSQLFile(const char *directoryName)
{
S
slguan 已提交
94
  sprintf(shellTablesSQLFile, "%s/tables.sql", directoryName);
S
#928  
slguan 已提交
95

S
slguan 已提交
96 97 98
  struct stat fstat;
  if (stat(shellTablesSQLFile, &fstat) < 0) {
    shellTablesSQLFile[0] = 0;
S
#928  
slguan 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  }
}

static void shellMallocSQLFiles()
{
  shellSQLFiles = (char**)calloc(shellSQLFileNum, sizeof(char*));
  for (int i = 0; i < shellSQLFileNum; i++) {
    shellSQLFiles[i] = calloc(1, TSDB_FILENAME_LEN);
  }
}

static void shellGetDirectoryFileList(char *inputDir)
{
  struct stat fileStat;
  if (stat(inputDir, &fileStat) < 0) {
    fprintf(stderr, "ERROR: %s not exist\n", inputDir);
    exit(0);
  }

  if (fileStat.st_mode & S_IFDIR) {
    shellCheckTablesSQLFile(inputDir);
    shellSQLFileNum = shellGetFilesNum(inputDir, "sql");
    int totalSQLFileNum = shellSQLFileNum;
    if (shellTablesSQLFile[0] != 0) {
      shellSQLFileNum--;
    }
    shellMallocSQLFiles();
    shellParseDirectory(inputDir, "sql", shellSQLFiles, shellSQLFileNum);
S
#928  
slguan 已提交
127
    fprintf(stdout, "\nstart to dispose %d files in %s\n", totalSQLFileNum, inputDir);
S
#928  
slguan 已提交
128 129 130 131 132 133 134 135 136 137
  }
  else {
    fprintf(stderr, "ERROR: %s is not a directory\n", inputDir);
    exit(0);
  }
}

static void shellSourceFile(TAOS *con, char *fptr) {
  wordexp_t full_path;
  int       read_len = 0;
138
  char *    cmd = malloc(tsMaxSQLStringLen);
S
#928  
slguan 已提交
139 140 141 142 143 144
  size_t    cmd_len = 0;
  char *    line = NULL;
  size_t    line_len = 0;

  if (wordexp(fptr, &full_path, 0) != 0) {
    fprintf(stderr, "ERROR: illegal file name\n");
145
    free(cmd);
S
#928  
slguan 已提交
146 147 148 149
    return;
  }

  char *fname = full_path.we_wordv[0];
150 151
  if (fname == NULL) {
    fprintf(stderr, "ERROR: invalid filename\n");
H
Hui Li 已提交
152
    free(cmd);
153 154 155
    return;
  }

H
Hui Li 已提交
156
  /*
157 158 159 160 161 162 163 164 165
  if (access(fname, F_OK) != 0) {
    fprintf(stderr, "ERROR: file %s is not exist\n", fptr);
    
    wordfree(&full_path);
    free(cmd);
    return;
  }
  
  if (access(fname, R_OK) != 0) {
S
#928  
slguan 已提交
166
    fprintf(stderr, "ERROR: file %s is not readable\n", fptr);
167
    
S
#928  
slguan 已提交
168
    wordfree(&full_path);
169
    free(cmd);
S
#928  
slguan 已提交
170 171
    return;
  }
H
Hui Li 已提交
172
  */
S
#928  
slguan 已提交
173 174 175 176 177

  FILE *f = fopen(fname, "r");
  if (f == NULL) {
    fprintf(stderr, "ERROR: failed to open file %s\n", fname);
    wordfree(&full_path);
178
    free(cmd);
S
#928  
slguan 已提交
179 180 181
    return;
  }

182
  fprintf(stdout, "begin import file:%s\n", fname);
S
#928  
slguan 已提交
183

184
  int lineNo = 0;
S
#928  
slguan 已提交
185
  while ((read_len = getline(&line, &line_len, f)) != -1) {
186
    ++lineNo;
187
    if (read_len >= tsMaxSQLStringLen) continue;
S
#928  
slguan 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201
    line[--read_len] = '\0';

    if (read_len == 0 || isCommentLine(line)) {  // line starts with #
      continue;
    }

    if (line[read_len - 1] == '\\') {
      line[read_len - 1] = ' ';
      memcpy(cmd + cmd_len, line, read_len);
      cmd_len += read_len;
      continue;
    }

    memcpy(cmd + cmd_len, line, read_len);
H
Haojun Liao 已提交
202 203 204 205 206
    
    TAOS_RES* pSql = taos_query(con, cmd);
    int32_t code = taos_errno(pSql);
    
    if (code != 0) {
207
      fprintf(stderr, "DB error: %s: %s (%d)\n", taos_errstr(pSql), fname, lineNo);
S
#928  
slguan 已提交
208
    }
H
Hui Li 已提交
209 210 211
    
    /* free local resouce: allocated memory/metric-meta refcnt */
    taos_free_result(pSql);
S
#928  
slguan 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225

    memset(cmd, 0, MAX_COMMAND_SIZE);
    cmd_len = 0;
  }

  free(cmd);
  if (line) free(line);
  wordfree(&full_path);
  fclose(f);
}

void* shellImportThreadFp(void *arg)
{
  ShellThreadObj *pThread = (ShellThreadObj*)arg;
226 227
  setThreadName("shellImportThrd");

S
#928  
slguan 已提交
228 229 230 231 232 233 234 235 236 237
  for (int f = 0; f < shellSQLFileNum; ++f) {
    if (f % pThread->totalThreads == pThread->threadIndex) {
      char *SQLFileName = shellSQLFiles[f];
      shellSourceFile(pThread->taos, SQLFileName);
    }
  }

  return NULL;
}

238
static void shellRunImportThreads(SShellArguments* _args)
S
#928  
slguan 已提交
239 240
{
  pthread_attr_t thattr;
241 242
  ShellThreadObj *threadObj = (ShellThreadObj *)calloc(_args->threadNum, sizeof(ShellThreadObj));
  for (int t = 0; t < _args->threadNum; ++t) {
S
#928  
slguan 已提交
243 244
    ShellThreadObj *pThread = threadObj + t;
    pThread->threadIndex = t;
245 246
    pThread->totalThreads = _args->threadNum;
    pThread->taos = taos_connect(_args->host, _args->user, _args->password, _args->database, tsDnodeShellPort);
S
#928  
slguan 已提交
247
    if (pThread->taos == NULL) {
248
      fprintf(stderr, "ERROR: thread:%d failed connect to TDengine, error:%s\n", pThread->threadIndex, "null taos"/*taos_errstr(pThread->taos)*/);
S
#928  
slguan 已提交
249 250 251 252 253 254 255 256 257 258 259 260
      exit(0);
    }

    pthread_attr_init(&thattr);
    pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE);

    if (pthread_create(&(pThread->threadID), &thattr, shellImportThreadFp, (void*)pThread) != 0) {
      fprintf(stderr, "ERROR: thread:%d failed to start\n", pThread->threadIndex);
      exit(0);
    }
  }

261
  for (int t = 0; t < _args->threadNum; ++t) {
S
#928  
slguan 已提交
262 263 264
    pthread_join(threadObj[t].threadID, NULL);
  }

265
  for (int t = 0; t < _args->threadNum; ++t) {
S
#928  
slguan 已提交
266 267 268 269 270
    taos_close(threadObj[t].taos);
  }
  free(threadObj);
}

271 272
void source_dir(TAOS* con, SShellArguments* _args) {
  shellGetDirectoryFileList(_args->dir);
S
#928  
slguan 已提交
273 274 275 276 277 278 279 280
  int64_t start = taosGetTimestampMs();

  if (shellTablesSQLFile[0] != 0) {
    shellSourceFile(con, shellTablesSQLFile);
    int64_t end = taosGetTimestampMs();
    fprintf(stdout, "import %s finished, time spent %.2f seconds\n", shellTablesSQLFile, (end - start) / 1000.0);
  }

281
  shellRunImportThreads(_args);
S
#928  
slguan 已提交
282
  int64_t end = taosGetTimestampMs();
283
  fprintf(stdout, "import %s finished, time spent %.2f seconds\n", _args->dir, (end - start) / 1000.0);
S
#928  
slguan 已提交
284
}