dmFile.c 7.4 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
Shengliang 已提交
17
#include "dmUtil.h"
S
shm  
Shengliang Guan 已提交
18

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

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

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

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

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

  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 已提交
51
    goto _OVER;
S
shm  
Shengliang Guan 已提交
52
  }
S
shm  
Shengliang Guan 已提交
53
  *pDeployed = deployed->valueint != 0;
S
shm  
Shengliang Guan 已提交
54

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

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

  terrno = code;
  return code;
}

S
Shengliang 已提交
66
int32_t dmWriteFile(const char *path, const char *name, bool deployed) {
S
shm  
Shengliang Guan 已提交
67 68 69 70 71 72 73
  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
Shengliang 已提交
74 75
  snprintf(file, sizeof(file), "%s%s%s.json", path, TD_DIRSEP, name);
  snprintf(realfile, sizeof(realfile), "%s%s%s.json", path, TD_DIRSEP, name);
S
shm  
Shengliang Guan 已提交
76

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

S
shm  
Shengliang Guan 已提交
84 85 86
  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 已提交
87

S
shm  
Shengliang Guan 已提交
88 89 90 91 92
  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 已提交
93

S
shm  
Shengliang Guan 已提交
94 95 96 97 98
  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 已提交
99

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

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

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

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

  return code;
}

S
Shengliang Guan 已提交
119
TdFilePtr dmCheckRunning(const char *dataDir) {
S
shm  
Shengliang Guan 已提交
120 121 122
  char filepath[PATH_MAX] = {0};
  snprintf(filepath, sizeof(filepath), "%s%s.running", dataDir, TD_DIRSEP);

123
  TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
S
shm  
Shengliang Guan 已提交
124 125
  if (pFile == NULL) {
    terrno = TAOS_SYSTEM_ERROR(errno);
wafwerar's avatar
wafwerar 已提交
126
    dError("failed to open file:%s since %s", filepath, terrstr());
S
shm  
Shengliang Guan 已提交
127 128 129
    return NULL;
  }

130 131 132 133 134 135
  int32_t retryTimes = 0;
  int32_t ret = 0;
  do {
    ret = taosLockFile(pFile);
    if (ret == 0) break;
    terrno = TAOS_SYSTEM_ERROR(errno);
136
    taosMsleep(1000);
137 138
    retryTimes++;
    dError("failed to lock file:%s since %s, retryTimes:%d", filepath, terrstr(), retryTimes);
139
  } while (retryTimes < 6);
140 141

  if (ret < 0) {
S
shm  
Shengliang Guan 已提交
142 143 144 145 146
    terrno = TAOS_SYSTEM_ERROR(errno);
    taosCloseFile(&pFile);
    return NULL;
  }

147
  terrno = 0;
S
Shengliang Guan 已提交
148
  dDebug("lock file:%s to prevent repeated starts", filepath);
S
shm  
Shengliang Guan 已提交
149 150 151
  return pFile;
}

S
Shengliang Guan 已提交
152
int32_t dmReadShmFile(const char *path, const char *name, EDndNodeType runType, SShm *pShm) {
S
shm  
Shengliang Guan 已提交
153 154 155 156 157 158
  int32_t   code = -1;
  char      content[MAXLEN + 1] = {0};
  char      file[PATH_MAX] = {0};
  cJSON    *root = NULL;
  TdFilePtr pFile = NULL;

S
Shengliang 已提交
159
  snprintf(file, sizeof(file), "%s%sshmfile", path, TD_DIRSEP);
S
shm  
Shengliang Guan 已提交
160
  pFile = taosOpenFile(file, TD_FILE_READ);
S
shm  
Shengliang Guan 已提交
161
  if (pFile == NULL) {
S
shm  
Shengliang Guan 已提交
162
    code = 0;
S
shm  
Shengliang Guan 已提交
163 164 165 166 167 168
    goto _OVER;
  }

  if (taosReadFile(pFile, content, MAXLEN) > 0) {
    root = cJSON_Parse(content);
    if (root == NULL) {
169
      terrno = TSDB_CODE_INVALID_JSON_FORMAT;
S
Shengliang 已提交
170
      dError("node:%s, failed to read %s since invalid json format", name, file);
S
shm  
Shengliang Guan 已提交
171 172 173
      goto _OVER;
    }

S
Shengliang Guan 已提交
174 175
    cJSON *shmid = cJSON_GetObjectItem(root, "shmid");
    if (shmid && shmid->type == cJSON_Number) {
S
Shengliang 已提交
176
      pShm->id = shmid->valueint;
S
Shengliang Guan 已提交
177 178 179 180
    }

    cJSON *shmsize = cJSON_GetObjectItem(root, "shmsize");
    if (shmsize && shmsize->type == cJSON_Number) {
S
Shengliang 已提交
181
      pShm->size = shmsize->valueint;
S
shm  
Shengliang Guan 已提交
182 183 184
    }
  }

S
Shengliang Guan 已提交
185
  if (!tsMultiProcess || runType == DNODE || runType == NODE_END) {
S
Shengliang 已提交
186 187 188
    if (pShm->id >= 0) {
      dDebug("node:%s, shmid:%d, is closed, size:%d", name, pShm->id, pShm->size);
      taosDropShm(pShm);
S
shm  
Shengliang Guan 已提交
189 190
    }
  } else {
S
Shengliang 已提交
191
    if (taosAttachShm(pShm) != 0) {
S
shm  
Shengliang Guan 已提交
192
      terrno = TAOS_SYSTEM_ERROR(errno);
S
Shengliang 已提交
193
      dError("shmid:%d, failed to attach shm since %s", pShm->id, terrstr());
S
shm  
Shengliang Guan 已提交
194 195
      goto _OVER;
    }
S
Shengliang 已提交
196
    dInfo("node:%s, shmid:%d is attached, size:%d", name, pShm->id, pShm->size);
S
shm  
Shengliang Guan 已提交
197 198
  }

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

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

  return code;
}

S
Shengliang 已提交
209
int32_t dmWriteShmFile(const char *path, const char *name, const SShm *pShm) {
S
shm  
Shengliang Guan 已提交
210 211 212 213 214 215 216
  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
Shengliang 已提交
217 218
  snprintf(file, sizeof(file), "%s%sshmfile.bak", path, TD_DIRSEP);
  snprintf(realfile, sizeof(realfile), "%s%sshmfile", path, TD_DIRSEP);
S
shm  
Shengliang Guan 已提交
219

220
  pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
S
shm  
Shengliang Guan 已提交
221 222
  if (pFile == NULL) {
    terrno = TAOS_SYSTEM_ERROR(errno);
S
Shengliang 已提交
223
    dError("node:%s, failed to open file:%s since %s", name, file, terrstr());
S
shm  
Shengliang Guan 已提交
224 225 226 227
    goto _OVER;
  }

  len += snprintf(content + len, MAXLEN - len, "{\n");
S
Shengliang 已提交
228 229
  len += snprintf(content + len, MAXLEN - len, "  \"shmid\":%d,\n", pShm->id);
  len += snprintf(content + len, MAXLEN - len, "  \"shmsize\":%d\n", pShm->size);
S
shm  
Shengliang Guan 已提交
230 231 232 233
  len += snprintf(content + len, MAXLEN - len, "}\n");

  if (taosWriteFile(pFile, content, len) != len) {
    terrno = TAOS_SYSTEM_ERROR(errno);
S
Shengliang 已提交
234
    dError("node:%s, failed to write file:%s since %s", name, file, terrstr());
S
shm  
Shengliang Guan 已提交
235 236 237 238 239
    goto _OVER;
  }

  if (taosFsyncFile(pFile) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
S
Shengliang 已提交
240
    dError("node:%s, failed to fsync file:%s since %s", name, file, terrstr());
S
shm  
Shengliang Guan 已提交
241 242 243 244 245 246 247
    goto _OVER;
  }

  taosCloseFile(&pFile);

  if (taosRenameFile(file, realfile) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
S
Shengliang 已提交
248
    dError("node:%s, failed to rename %s to %s since %s", name, file, realfile, terrstr());
S
shm  
Shengliang Guan 已提交
249 250 251
    return -1;
  }

S
Shengliang 已提交
252
  dInfo("node:%s, successed to write %s", name, realfile);
S
shm  
Shengliang Guan 已提交
253 254 255 256 257 258 259 260
  code = 0;

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

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