dndFile.c 8.1 KB
Newer Older
S
shm  
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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/>.
 */

#define _DEFAULT_SOURCE
S
shm  
Shengliang Guan 已提交
17
#include "dndInt.h"
S
shm  
Shengliang Guan 已提交
18

S
Shengliang Guan 已提交
19 20
#define MAXLEN 1024

S
shm  
Shengliang Guan 已提交
21
int32_t dndReadFile(SMgmtWrapper *pWrapper, bool *pDeployed) {
S
shm  
Shengliang Guan 已提交
22 23 24 25 26 27
  int32_t   code = TSDB_CODE_NODE_PARSE_FILE_ERROR;
  int64_t   len = 0;
  char      content[MAXLEN + 1] = {0};
  cJSON    *root = NULL;
  char      file[PATH_MAX];
  TdFilePtr pFile = NULL;
S
shm  
Shengliang Guan 已提交
28

S
shm  
Shengliang Guan 已提交
29
  snprintf(file, sizeof(file), "%s%s%s.json", pWrapper->path, TD_DIRSEP, pWrapper->name);
S
shm  
Shengliang Guan 已提交
30 31 32 33
  pFile = taosOpenFile(file, TD_FILE_READ);
  if (pFile == NULL) {
    dDebug("file %s not exist", file);
    code = 0;
S
shm  
Shengliang Guan 已提交
34
    goto _OVER;
S
shm  
Shengliang Guan 已提交
35 36
  }

S
shm  
Shengliang Guan 已提交
37
  len = taosReadFile(pFile, content, MAXLEN);
S
shm  
Shengliang Guan 已提交
38 39
  if (len <= 0) {
    dError("failed to read %s since content is null", file);
S
shm  
Shengliang Guan 已提交
40
    goto _OVER;
S
shm  
Shengliang Guan 已提交
41 42 43 44 45
  }

  root = cJSON_Parse(content);
  if (root == NULL) {
    dError("failed to read %s since invalid json format", file);
S
shm  
Shengliang Guan 已提交
46
    goto _OVER;
S
shm  
Shengliang Guan 已提交
47 48 49 50 51
  }

  cJSON *deployed = cJSON_GetObjectItem(root, "deployed");
  if (!deployed || deployed->type != cJSON_Number) {
    dError("failed to read %s since deployed not found", file);
S
shm  
Shengliang Guan 已提交
52
    goto _OVER;
S
shm  
Shengliang Guan 已提交
53
  }
S
shm  
Shengliang Guan 已提交
54
  *pDeployed = deployed->valueint != 0;
S
shm  
Shengliang Guan 已提交
55

S
shm  
Shengliang Guan 已提交
56
  dDebug("succcessed to read file %s, deployed:%d", file, *pDeployed);
S
shm  
Shengliang Guan 已提交
57
  code = 0;
S
shm  
Shengliang Guan 已提交
58

S
shm  
Shengliang Guan 已提交
59
_OVER:
S
shm  
Shengliang Guan 已提交
60 61 62 63 64 65 66
  if (root != NULL) cJSON_Delete(root);
  if (pFile != NULL) taosCloseFile(&pFile);

  terrno = code;
  return code;
}

S
shm  
Shengliang Guan 已提交
67
int32_t dndWriteFile(SMgmtWrapper *pWrapper, bool deployed) {
S
shm  
Shengliang Guan 已提交
68 69 70 71 72 73 74
  int32_t   code = -1;
  int32_t   len = 0;
  char      content[MAXLEN + 1] = {0};
  char      file[PATH_MAX] = {0};
  char      realfile[PATH_MAX] = {0};
  TdFilePtr pFile = NULL;

S
shm  
Shengliang Guan 已提交
75
  snprintf(file, sizeof(file), "%s%s%s.json", pWrapper->path, TD_DIRSEP, pWrapper->name);
S
shm  
Shengliang Guan 已提交
76
  snprintf(realfile, sizeof(realfile), "%s%s%s.json", pWrapper->path, TD_DIRSEP, pWrapper->name);
S
shm  
Shengliang Guan 已提交
77

S
shm  
Shengliang Guan 已提交
78
  pFile = taosOpenFile(file, TD_FILE_CTEATE | TD_FILE_WRITE | TD_FILE_TRUNC);
S
shm  
Shengliang Guan 已提交
79
  if (pFile == NULL) {
S
shm  
Shengliang Guan 已提交
80
    terrno = TAOS_SYSTEM_ERROR(errno);
S
shm  
Shengliang Guan 已提交
81
    dError("failed to write %s since %s", file, terrstr());
S
shm  
Shengliang Guan 已提交
82
    goto _OVER;
S
shm  
Shengliang Guan 已提交
83 84
  }

S
shm  
Shengliang Guan 已提交
85 86 87
  len += snprintf(content + len, MAXLEN - len, "{\n");
  len += snprintf(content + len, MAXLEN - len, "  \"deployed\": %d\n", deployed);
  len += snprintf(content + len, MAXLEN - len, "}\n");
S
shm  
Shengliang Guan 已提交
88

S
shm  
Shengliang Guan 已提交
89 90 91 92 93
  if (taosWriteFile(pFile, content, len) != len) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    dError("failed to write file:%s since %s", file, terrstr());
    goto _OVER;
  }
S
shm  
Shengliang Guan 已提交
94

S
shm  
Shengliang Guan 已提交
95 96 97 98 99
  if (taosFsyncFile(pFile) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    dError("failed to fsync file:%s since %s", file, terrstr());
    goto _OVER;
  }
S
shm  
Shengliang Guan 已提交
100

S
shm  
Shengliang Guan 已提交
101
  taosCloseFile(&pFile);
S
shm  
Shengliang Guan 已提交
102 103

  if (taosRenameFile(file, realfile) != 0) {
S
shm  
Shengliang Guan 已提交
104
    terrno = TAOS_SYSTEM_ERROR(errno);
S
shm  
Shengliang Guan 已提交
105 106 107 108
    dError("failed to rename %s since %s", file, terrstr());
    return -1;
  }

S
shm  
Shengliang Guan 已提交
109
  dInfo("successed to write %s, deployed:%d", realfile, deployed);
S
shm  
Shengliang Guan 已提交
110 111 112 113 114 115 116 117 118 119
  code = 0;

_OVER:
  if (pFile != NULL) {
    taosCloseFile(&pFile);
  }

  return code;
}

S
shm  
Shengliang Guan 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
TdFilePtr dndCheckRunning(const char *dataDir) {
  char filepath[PATH_MAX] = {0};
  snprintf(filepath, sizeof(filepath), "%s%s.running", dataDir, TD_DIRSEP);

  TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_CTEATE | TD_FILE_WRITE | TD_FILE_TRUNC);
  if (pFile == NULL) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    dError("failed to lock file:%s since %s", filepath, terrstr());
    return NULL;
  }

  int32_t ret = taosLockFile(pFile);
  if (ret != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    dError("failed to lock file:%s since %s", filepath, terrstr());
    taosCloseFile(&pFile);
    return NULL;
  }

  dDebug("file:%s is locked", filepath);
  return pFile;
}

int32_t dndReadShmFile(SDnode *pDnode) {
S
shm  
Shengliang Guan 已提交
144 145 146 147 148 149 150
  int32_t   code = -1;
  char      itemName[24] = {0};
  char      content[MAXLEN + 1] = {0};
  char      file[PATH_MAX] = {0};
  cJSON    *root = NULL;
  TdFilePtr pFile = NULL;

S
shm  
Shengliang Guan 已提交
151 152
  snprintf(file, sizeof(file), "%s%s.shmfile", pDnode->dataDir, TD_DIRSEP);
  pFile = taosOpenFile(file, TD_FILE_READ);
S
shm  
Shengliang Guan 已提交
153
  if (pFile == NULL) {
S
shm  
Shengliang Guan 已提交
154 155
    dDebug("file %s not exist", file);
    code = 0;
S
shm  
Shengliang Guan 已提交
156 157 158 159 160 161 162 163 164 165 166
    goto _OVER;
  }

  if (taosReadFile(pFile, content, MAXLEN) > 0) {
    root = cJSON_Parse(content);
    if (root == NULL) {
      terrno = TSDB_CODE_NODE_PARSE_FILE_ERROR;
      dError("failed to read %s since invalid json format", file);
      goto _OVER;
    }

S
Shengliang Guan 已提交
167
    for (EDndType ntype = DNODE + 1; ntype < NODE_MAX; ++ntype) {
S
shm  
Shengliang Guan 已提交
168 169
      snprintf(itemName, sizeof(itemName), "%s_shmid", dndNodeProcStr(ntype));
      cJSON *shmid = cJSON_GetObjectItem(root, itemName);
S
shm  
Shengliang Guan 已提交
170 171
      if (shmid && shmid->type == cJSON_Number) {
        pDnode->wrappers[ntype].shm.id = shmid->valueint;
S
shm  
Shengliang Guan 已提交
172 173 174 175
      }

      snprintf(itemName, sizeof(itemName), "%s_shmsize", dndNodeProcStr(ntype));
      cJSON *shmsize = cJSON_GetObjectItem(root, itemName);
S
shm  
Shengliang Guan 已提交
176 177
      if (shmsize && shmsize->type == cJSON_Number) {
        pDnode->wrappers[ntype].shm.size = shmsize->valueint;
S
shm  
Shengliang Guan 已提交
178 179 180 181
      }
    }
  }

S
shm  
Shengliang Guan 已提交
182
  if (!tsMultiProcess || pDnode->ntype == DNODE || pDnode->ntype == NODE_MAX) {
S
Shengliang Guan 已提交
183
    for (EDndType ntype = DNODE; ntype < NODE_MAX; ++ntype) {
S
shm  
Shengliang Guan 已提交
184 185 186
      SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype];
      if (pWrapper->shm.id >= 0) {
        dDebug("shmid:%d, is closed, size:%d", pWrapper->shm.id, pWrapper->shm.size);
S
shm  
Shengliang Guan 已提交
187 188 189 190 191 192 193
        taosDropShm(&pWrapper->shm);
      }
    }
  } else {
    SMgmtWrapper *pWrapper = &pDnode->wrappers[pDnode->ntype];
    if (taosAttachShm(&pWrapper->shm) != 0) {
      terrno = TAOS_SYSTEM_ERROR(errno);
S
shm  
Shengliang Guan 已提交
194
      dError("shmid:%d, failed to attach shm since %s", pWrapper->shm.id, terrstr());
S
shm  
Shengliang Guan 已提交
195 196
      goto _OVER;
    }
S
Shengliang Guan 已提交
197
    dInfo("node:%s, shmid:%d is attached, size:%d", pWrapper->name, pWrapper->shm.id, pWrapper->shm.size);
S
shm  
Shengliang Guan 已提交
198 199
  }

S
shm  
Shengliang Guan 已提交
200
  dDebug("successed to load %s", file);
S
shm  
Shengliang Guan 已提交
201 202 203 204
  code = 0;

_OVER:
  if (root != NULL) cJSON_Delete(root);
S
shm  
Shengliang Guan 已提交
205
  if (pFile != NULL) taosCloseFile(&pFile);
S
shm  
Shengliang Guan 已提交
206 207 208 209

  return code;
}

S
shm  
Shengliang Guan 已提交
210
int32_t dndWriteShmFile(SDnode *pDnode) {
S
shm  
Shengliang Guan 已提交
211 212 213 214 215 216 217
  int32_t   code = -1;
  int32_t   len = 0;
  char      content[MAXLEN + 1] = {0};
  char      file[PATH_MAX] = {0};
  char      realfile[PATH_MAX] = {0};
  TdFilePtr pFile = NULL;

S
shm  
Shengliang Guan 已提交
218 219
  snprintf(file, sizeof(file), "%s%s.shmfile.bak", pDnode->dataDir, TD_DIRSEP);
  snprintf(realfile, sizeof(realfile), "%s%s.shmfile", pDnode->dataDir, TD_DIRSEP);
S
shm  
Shengliang Guan 已提交
220 221 222 223 224 225 226 227 228

  pFile = taosOpenFile(file, TD_FILE_CTEATE | TD_FILE_WRITE | TD_FILE_TRUNC);
  if (pFile == NULL) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    dError("failed to open file:%s since %s", file, terrstr());
    goto _OVER;
  }

  len += snprintf(content + len, MAXLEN - len, "{\n");
S
Shengliang Guan 已提交
229
  for (EDndType ntype = DNODE + 1; ntype < NODE_MAX; ++ntype) {
S
shm  
Shengliang Guan 已提交
230 231
    SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype];
    len += snprintf(content + len, MAXLEN - len, "  \"%s_shmid\":%d,\n", dndNodeProcStr(ntype), pWrapper->shm.id);
S
shm  
Shengliang Guan 已提交
232
    if (ntype == NODE_MAX - 1) {
S
shm  
Shengliang Guan 已提交
233
      len += snprintf(content + len, MAXLEN - len, "  \"%s_shmsize\":%d\n", dndNodeProcStr(ntype), pWrapper->shm.size);
S
shm  
Shengliang Guan 已提交
234
    } else {
S
shm  
Shengliang Guan 已提交
235
      len += snprintf(content + len, MAXLEN - len, "  \"%s_shmsize\":%d,\n", dndNodeProcStr(ntype), pWrapper->shm.size);
S
shm  
Shengliang Guan 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
    }
  }
  len += snprintf(content + len, MAXLEN - len, "}\n");

  if (taosWriteFile(pFile, content, len) != len) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    dError("failed to write file:%s since %s", file, terrstr());
    goto _OVER;
  }

  if (taosFsyncFile(pFile) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    dError("failed to fsync file:%s since %s", file, terrstr());
    goto _OVER;
  }

  taosCloseFile(&pFile);

  if (taosRenameFile(file, realfile) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    dError("failed to rename %s to %s since %s", file, realfile, terrstr());
    return -1;
  }

S
shm  
Shengliang Guan 已提交
260
  dInfo("successed to write %s", realfile);
S
shm  
Shengliang Guan 已提交
261 262 263 264 265 266 267 268
  code = 0;

_OVER:
  if (pFile != NULL) {
    taosCloseFile(&pFile);
  }

  return code;
S
shm  
Shengliang Guan 已提交
269
}