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

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

S
Shengliang Guan 已提交
21
int32_t dmReadFile(SMgmtWrapper *pWrapper, 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
shm  
Shengliang Guan 已提交
29
  snprintf(file, sizeof(file), "%s%s%s.json", pWrapper->path, TD_DIRSEP, pWrapper->name);
S
shm  
Shengliang Guan 已提交
30 31
  pFile = taosOpenFile(file, TD_FILE_READ);
  if (pFile == NULL) {
S
Shengliang Guan 已提交
32
    // dDebug("file %s not exist", file);
S
shm  
Shengliang Guan 已提交
33
    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
Shengliang Guan 已提交
67
int32_t dmWriteFile(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

78
  pFile = taosOpenFile(file, TD_FILE_CREATE | 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
Shengliang Guan 已提交
120
TdFilePtr dmCheckRunning(const char *dataDir) {
S
shm  
Shengliang Guan 已提交
121 122 123
  char filepath[PATH_MAX] = {0};
  snprintf(filepath, sizeof(filepath), "%s%s.running", dataDir, TD_DIRSEP);

124
  TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
S
shm  
Shengliang Guan 已提交
125 126
  if (pFile == NULL) {
    terrno = TAOS_SYSTEM_ERROR(errno);
wafwerar's avatar
wafwerar 已提交
127
    dError("failed to open file:%s since %s", filepath, terrstr());
S
shm  
Shengliang Guan 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    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 已提交
143
int32_t dmReadShmFile(SMgmtWrapper *pWrapper) {
S
shm  
Shengliang Guan 已提交
144 145 146 147 148 149
  int32_t   code = -1;
  char      content[MAXLEN + 1] = {0};
  char      file[PATH_MAX] = {0};
  cJSON    *root = NULL;
  TdFilePtr pFile = NULL;

S
Shengliang Guan 已提交
150
  snprintf(file, sizeof(file), "%s%sshmfile", pWrapper->path, TD_DIRSEP);
S
shm  
Shengliang Guan 已提交
151
  pFile = taosOpenFile(file, TD_FILE_READ);
S
shm  
Shengliang Guan 已提交
152
  if (pFile == NULL) {
S
Shengliang Guan 已提交
153
    // dDebug("node:%s, file %s not exist", pWrapper->name, file);
S
shm  
Shengliang Guan 已提交
154
    code = 0;
S
shm  
Shengliang Guan 已提交
155 156 157 158 159 160
    goto _OVER;
  }

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

S
Shengliang Guan 已提交
166 167 168 169 170 171 172 173
    cJSON *shmid = cJSON_GetObjectItem(root, "shmid");
    if (shmid && shmid->type == cJSON_Number) {
      pWrapper->procShm.id = shmid->valueint;
    }

    cJSON *shmsize = cJSON_GetObjectItem(root, "shmsize");
    if (shmsize && shmsize->type == cJSON_Number) {
      pWrapper->procShm.size = shmsize->valueint;
S
shm  
Shengliang Guan 已提交
174 175 176
    }
  }

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

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

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

  return code;
}

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

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

  len += snprintf(content + len, MAXLEN - len, "{\n");
S
Shengliang Guan 已提交
220 221
  len += snprintf(content + len, MAXLEN - len, "  \"shmid\":%d,\n", pWrapper->procShm.id);
  len += snprintf(content + len, MAXLEN - len, "  \"shmsize\":%d\n", pWrapper->procShm.size);
S
shm  
Shengliang Guan 已提交
222 223 224 225
  len += snprintf(content + len, MAXLEN - len, "}\n");

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

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

  taosCloseFile(&pFile);

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

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

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

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