dmFile.c 7.2 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 130 131 132 133 134 135 136 137 138 139 140 141
    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;
}

S
Shengliang Guan 已提交
142
int32_t dmReadShmFile(const char *path, const char *name, EDndNodeType runType, SShm *pShm) {
S
shm  
Shengliang Guan 已提交
143 144 145 146 147 148
  int32_t   code = -1;
  char      content[MAXLEN + 1] = {0};
  char      file[PATH_MAX] = {0};
  cJSON    *root = NULL;
  TdFilePtr pFile = NULL;

S
Shengliang 已提交
149
  snprintf(file, sizeof(file), "%s%sshmfile", path, TD_DIRSEP);
S
shm  
Shengliang Guan 已提交
150
  pFile = taosOpenFile(file, TD_FILE_READ);
S
shm  
Shengliang Guan 已提交
151
  if (pFile == NULL) {
S
shm  
Shengliang Guan 已提交
152
    code = 0;
S
shm  
Shengliang Guan 已提交
153 154 155 156 157 158
    goto _OVER;
  }

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

S
Shengliang Guan 已提交
164 165
    cJSON *shmid = cJSON_GetObjectItem(root, "shmid");
    if (shmid && shmid->type == cJSON_Number) {
S
Shengliang 已提交
166
      pShm->id = shmid->valueint;
S
Shengliang Guan 已提交
167 168 169 170
    }

    cJSON *shmsize = cJSON_GetObjectItem(root, "shmsize");
    if (shmsize && shmsize->type == cJSON_Number) {
S
Shengliang 已提交
171
      pShm->size = shmsize->valueint;
S
shm  
Shengliang Guan 已提交
172 173 174
    }
  }

S
Shengliang Guan 已提交
175
  if (!tsMultiProcess || runType == DNODE || runType == NODE_END) {
S
Shengliang 已提交
176 177 178
    if (pShm->id >= 0) {
      dDebug("node:%s, shmid:%d, is closed, size:%d", name, pShm->id, pShm->size);
      taosDropShm(pShm);
S
shm  
Shengliang Guan 已提交
179 180
    }
  } else {
S
Shengliang 已提交
181
    if (taosAttachShm(pShm) != 0) {
S
shm  
Shengliang Guan 已提交
182
      terrno = TAOS_SYSTEM_ERROR(errno);
S
Shengliang 已提交
183
      dError("shmid:%d, failed to attach shm since %s", pShm->id, terrstr());
S
shm  
Shengliang Guan 已提交
184 185
      goto _OVER;
    }
S
Shengliang 已提交
186
    dInfo("node:%s, shmid:%d is attached, size:%d", name, pShm->id, pShm->size);
S
shm  
Shengliang Guan 已提交
187 188
  }

S
Shengliang 已提交
189
  dDebug("node:%s, successed to load %s", name, file);
S
shm  
Shengliang Guan 已提交
190 191 192 193
  code = 0;

_OVER:
  if (root != NULL) cJSON_Delete(root);
S
shm  
Shengliang Guan 已提交
194
  if (pFile != NULL) taosCloseFile(&pFile);
S
shm  
Shengliang Guan 已提交
195 196 197 198

  return code;
}

S
Shengliang 已提交
199
int32_t dmWriteShmFile(const char *path, const char *name, const SShm *pShm) {
S
shm  
Shengliang Guan 已提交
200 201 202 203 204 205 206
  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 已提交
207 208
  snprintf(file, sizeof(file), "%s%sshmfile.bak", path, TD_DIRSEP);
  snprintf(realfile, sizeof(realfile), "%s%sshmfile", path, TD_DIRSEP);
S
shm  
Shengliang Guan 已提交
209

210
  pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
S
shm  
Shengliang Guan 已提交
211 212
  if (pFile == NULL) {
    terrno = TAOS_SYSTEM_ERROR(errno);
S
Shengliang 已提交
213
    dError("node:%s, failed to open file:%s since %s", name, file, terrstr());
S
shm  
Shengliang Guan 已提交
214 215 216 217
    goto _OVER;
  }

  len += snprintf(content + len, MAXLEN - len, "{\n");
S
Shengliang 已提交
218 219
  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 已提交
220 221 222 223
  len += snprintf(content + len, MAXLEN - len, "}\n");

  if (taosWriteFile(pFile, content, len) != len) {
    terrno = TAOS_SYSTEM_ERROR(errno);
S
Shengliang 已提交
224
    dError("node:%s, failed to write file:%s since %s", name, file, terrstr());
S
shm  
Shengliang Guan 已提交
225 226 227 228 229
    goto _OVER;
  }

  if (taosFsyncFile(pFile) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
S
Shengliang 已提交
230
    dError("node:%s, failed to fsync file:%s since %s", name, file, terrstr());
S
shm  
Shengliang Guan 已提交
231 232 233 234 235 236 237
    goto _OVER;
  }

  taosCloseFile(&pFile);

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

S
Shengliang 已提交
242
  dInfo("node:%s, successed to write %s", name, realfile);
S
shm  
Shengliang Guan 已提交
243 244 245 246 247 248 249 250
  code = 0;

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

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