dndFile.c 7.9 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
  code = 0;

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

  return code;
}

int32_t dndOpenRuntimeFile(SDnode *pDnode) {
  int32_t   code = -1;
  char      itemName[24] = {0};
  char      content[MAXLEN + 1] = {0};
  char      file[PATH_MAX] = {0};
  cJSON    *root = NULL;
  TdFilePtr pFile = NULL;

  snprintf(file, sizeof(file), "%s%s.running", pDnode->dataDir, TD_DIRSEP);
  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;
  }

  if (taosLockFile(pFile) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    dError("failed to lock file:%s since %s", file, terrstr());
    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;
    }

    for (ENodeType ntype = DNODE + 1; ntype < NODE_MAX; ++ntype) {
      snprintf(itemName, sizeof(itemName), "%s_shmid", dndNodeProcStr(ntype));
      cJSON *shmid = cJSON_GetObjectItem(root, itemName);
      if (shmid && shmid->type == cJSON_Number) {
        pDnode->wrappers[ntype].shm.id = shmid->valueint;
      }

      snprintf(itemName, sizeof(itemName), "%s_shmsize", dndNodeProcStr(ntype));
      cJSON *shmsize = cJSON_GetObjectItem(root, itemName);
      if (shmsize && shmsize->type == cJSON_Number) {
        pDnode->wrappers[ntype].shm.size = shmsize->valueint;
      }
    }
  }

  if (tsMultiProcess || pDnode->ntype == DNODE) {
    for (ENodeType ntype = DNODE; ntype < NODE_MAX; ++ntype) {
      SMgmtWrapper *pWrapper = &pDnode->wrappers[pDnode->ntype];
      if (pWrapper->shm.id > 0) {
        dDebug("shmid:%d, is closed, size:%d", pWrapper->shm.id, pWrapper->shm.size);
        taosDropShm(&pWrapper->shm);
      }
    }
  } else {
    SMgmtWrapper *pWrapper = &pDnode->wrappers[pDnode->ntype];
    if (taosAttachShm(&pWrapper->shm) != 0) {
      terrno = TAOS_SYSTEM_ERROR(errno);
      dError("shmid:%d, failed to attach since %s", pWrapper->shm.id, terrstr());
      goto _OVER;
    }
    dDebug("shmid:%d, is attached, size:%d", pWrapper->shm.id, pWrapper->shm.size);
  }

  dDebug("successed to open %s", file);
  code = 0;

_OVER:
  if (root != NULL) cJSON_Delete(root);
  if (code != 0) {
    if (pFile != NULL) taosCloseFile(&pFile);
  } else {
    pDnode->runtimeFile = pFile;
  }

  return code;
}

int32_t dndWriteRuntimeFile(SDnode *pDnode) {
  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;

  snprintf(file, sizeof(file), "%s%s.running.bak", pDnode->dataDir, TD_DIRSEP);
  snprintf(realfile, sizeof(realfile), "%s%s.running", pDnode->dataDir, TD_DIRSEP);

  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");
  for (ENodeType ntype = DNODE + 1; ntype < NODE_MAX; ++ntype) {
    SMgmtWrapper *pWrapper = &pDnode->wrappers[pDnode->ntype];
    len += snprintf(content + len, MAXLEN - len, "  \"%s_shmid\": %d,\n", dndNodeProcStr(ntype), pWrapper->shm.id);
    if (ntype == NODE_MAX - 1) {
      len += snprintf(content + len, MAXLEN - len, "  \"%s_shmsize\": %d\n", dndNodeProcStr(ntype), pWrapper->shm.size);
    } else {
      len += snprintf(content + len, MAXLEN - len, "  \"%s_shmsize\": %d,\n", dndNodeProcStr(ntype), pWrapper->shm.size);
    }
  }
  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;
  }

  dDebug("successed to write %s", realfile);
  code = 0;

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

  return code;
S
shm  
Shengliang Guan 已提交
256
}
S
shm  
Shengliang Guan 已提交
257 258 259 260 261 262 263 264

void dndCloseRuntimeFile(SDnode *pDnode) {
  if (pDnode->runtimeFile) {
    taosUnLockFile(pDnode->runtimeFile);
    taosCloseFile(&pDnode->runtimeFile);
    pDnode->runtimeFile = NULL;
  }
}