• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #4881

14 Dec 2025 03:48AM UTC coverage: 60.617% (+0.5%) from 60.092%
#4881

push

travis-ci

web-flow
test: update coverage workflow time (#33918)

156854 of 258761 relevant lines covered (60.62%)

75258957.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.03
/source/dnode/mgmt/mgmt_mnode/src/mmFile.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http:www.gnu.org/licenses/>.
14
 */
15

16
#define _DEFAULT_SOURCE
17
#include "mmInt.h"
18
#include "tjson.h"
19

20
static int32_t mmDecodeOption(SJson *pJson, SMnodeOpt *pOption) {
343,066✔
21
  int32_t code = 0;
343,066✔
22

23
  tjsonGetInt32ValueFromDouble(pJson, "deployed", pOption->deploy, code);
343,066✔
24
  if (code < 0) return code;
343,066✔
25
  tjsonGetInt32ValueFromDouble(pJson, "version", pOption->version, code);
343,066✔
26
  if (code < 0) return code;
343,066✔
27
  tjsonGetInt32ValueFromDouble(pJson, "selfIndex", pOption->selfIndex, code);
343,066✔
28
  if (code < 0) return code;
343,066✔
29
  tjsonGetInt32ValueFromDouble(pJson, "lastIndex", pOption->lastIndex, code);
343,066✔
30
  if (code < 0) return code;
343,066✔
31

32
  SJson *replicas = tjsonGetObjectItem(pJson, "replicas");
343,066✔
33
  if (replicas == NULL) return 0;
343,066✔
34
  pOption->numOfTotalReplicas = tjsonGetArraySize(replicas);
11,164✔
35

36
  pOption->numOfReplicas = 0;
11,164✔
37

38
  for (int32_t i = 0; i < pOption->numOfTotalReplicas; ++i) {
38,452✔
39
    SJson *replica = tjsonGetArrayItem(replicas, i);
27,288✔
40
    if (replica == NULL) return TSDB_CODE_INVALID_JSON_FORMAT;
27,288✔
41

42
    SReplica *pReplica = pOption->replicas + i;
27,288✔
43
    tjsonGetInt32ValueFromDouble(replica, "id", pReplica->id, code);
27,288✔
44
    if (code < 0) return code;
27,288✔
45
    code = tjsonGetStringValue(replica, "fqdn", pReplica->fqdn);
27,288✔
46
    if (code < 0) return code;
27,288✔
47
    tjsonGetUInt16ValueFromDouble(replica, "port", pReplica->port, code);
27,288✔
48
    if (code < 0) return code;
27,288✔
49
    tjsonGetInt32ValueFromDouble(replica, "role", pOption->nodeRoles[i], code);
27,288✔
50
    if (code < 0) return code;
27,288✔
51
    if (pOption->nodeRoles[i] == TAOS_SYNC_ROLE_VOTER) {
27,288✔
52
      pOption->numOfReplicas++;
21,706✔
53
    }
54
  }
55

56
  for (int32_t i = 0; i < pOption->numOfTotalReplicas; ++i) {
38,452✔
57
  }
58

59
  return 0;
11,164✔
60
}
61

62
int32_t mmReadFile(const char *path, SMnodeOpt *pOption) {
771,386✔
63
  int32_t   code = -1;
771,386✔
64
  TdFilePtr pFile = NULL;
771,386✔
65
  char     *pData = NULL;
771,386✔
66
  SJson    *pJson = NULL;
771,386✔
67
  char      file[PATH_MAX] = {0};
771,386✔
68

69
  int32_t nBytes = snprintf(file, sizeof(file), "%s%smnode.json", path, TD_DIRSEP);
771,386✔
70
  if (nBytes <= 0 || nBytes >= sizeof(file)) {
771,386✔
71
    code = TSDB_CODE_OUT_OF_BUFFER;
×
72
    goto _OVER;
×
73
  }
74

75
  if (taosStatFile(file, NULL, NULL, NULL) < 0) {
771,386✔
76
    dInfo("mnode file:%s not exist, reason:%s", file, tstrerror(terrno));
428,320✔
77
    return 0;
428,320✔
78
  }
79

80
  pFile = taosOpenFile(file, TD_FILE_READ);
343,066✔
81
  if (pFile == NULL) {
343,066✔
82
    code = terrno;
×
83
    dError("failed to open mnode file:%s since %s", file, tstrerror(code));
×
84
    goto _OVER;
×
85
  }
86

87
  int64_t size = 0;
343,066✔
88
  code = taosFStatFile(pFile, &size, NULL);
343,066✔
89
  if (code != 0) {
343,066✔
90
    dError("failed to fstat mnode file:%s since %s", file, tstrerror(code));
×
91
    goto _OVER;
×
92
  }
93

94
  pData = taosMemoryMalloc(size + 1);
343,066✔
95
  if (pData == NULL) {
343,066✔
96
    code = terrno;
×
97
    goto _OVER;
×
98
  }
99

100
  if (taosReadFile(pFile, pData, size) != size) {
343,066✔
101
    code = terrno;
×
102
    dError("failed to read mnode file:%s since %s", file, tstrerror(code));
×
103
    goto _OVER;
×
104
  }
105

106
  pData[size] = '\0';
343,066✔
107

108
  pJson = tjsonParse(pData);
343,066✔
109
  if (pJson == NULL) {
343,066✔
110
    code = TSDB_CODE_INVALID_JSON_FORMAT;
×
111
    goto _OVER;
×
112
  }
113

114
  if ((code = mmDecodeOption(pJson, pOption)) < 0) {
343,066✔
115
    goto _OVER;
×
116
  }
117

118
  code = 0;
343,066✔
119
  dInfo("succceed to read mnode file %s", file);
343,066✔
120

121
_OVER:
342,948✔
122
  if (pData != NULL) taosMemoryFree(pData);
343,066✔
123
  if (pJson != NULL) cJSON_Delete(pJson);
343,066✔
124
  if (pFile != NULL) taosCloseFile(&pFile);
343,066✔
125

126
  if (code != 0) {
343,066✔
127
    dError("failed to read mnode file:%s since %s", file, tstrerror(code));
×
128
  }
129
  return code;
343,066✔
130
}
131

132
static int32_t mmEncodeOption(SJson *pJson, const SMnodeOpt *pOption) {
361,848✔
133
  int32_t code = 0;
361,848✔
134
  if (pOption->deploy && pOption->numOfTotalReplicas > 0) {
361,848✔
135
    if ((code = tjsonAddDoubleToObject(pJson, "selfIndex", pOption->selfIndex)) < 0) return code;
11,164✔
136

137
    SJson *replicas = tjsonCreateArray();
11,164✔
138
    if (replicas == NULL) {
11,164✔
139
      return terrno;
×
140
    }
141
    if ((code = tjsonAddItemToObject(pJson, "replicas", replicas)) < 0) return code;
11,164✔
142

143
    for (int32_t i = 0; i < pOption->numOfTotalReplicas; ++i) {
38,452✔
144
      SJson *replica = tjsonCreateObject();
27,288✔
145
      if (replica == NULL) {
27,288✔
146
        return terrno;
×
147
      }
148

149
      const SReplica *pReplica = pOption->replicas + i;
27,288✔
150
      if ((code = tjsonAddDoubleToObject(replica, "id", pReplica->id)) < 0) return code;
27,288✔
151
      if ((code = tjsonAddStringToObject(replica, "fqdn", pReplica->fqdn)) < 0) return code;
27,288✔
152
      if ((code = tjsonAddDoubleToObject(replica, "port", pReplica->port)) < 0) return code;
27,288✔
153
      if ((code = tjsonAddDoubleToObject(replica, "role", pOption->nodeRoles[i])) < 0) return code;
27,288✔
154
      if ((code = tjsonAddItemToArray(replicas, replica)) < 0) return code;
27,288✔
155
    }
156
  }
157

158
  if ((code = tjsonAddDoubleToObject(pJson, "lastIndex", pOption->lastIndex)) < 0) return code;
361,848✔
159

160
  if ((code = tjsonAddDoubleToObject(pJson, "deployed", pOption->deploy)) < 0) return code;
361,848✔
161

162
  if ((code = tjsonAddDoubleToObject(pJson, "version", pOption->version)) < 0) return code;
361,848✔
163

164
  return code;
361,848✔
165
}
166

167
int32_t mmWriteFile(const char *path, const SMnodeOpt *pOption) {
361,848✔
168
  int32_t   code = -1;
361,848✔
169
  char     *buffer = NULL;
361,848✔
170
  SJson    *pJson = NULL;
361,848✔
171
  TdFilePtr pFile = NULL;
361,848✔
172
  char      file[PATH_MAX] = {0};
361,848✔
173
  char      realfile[PATH_MAX] = {0};
361,848✔
174

175
  int32_t nBytes = snprintf(file, sizeof(file), "%s%smnode.json.bak", path, TD_DIRSEP);
361,848✔
176
  if (nBytes <= 0 || nBytes >= sizeof(file)) {
361,848✔
177
    code = TSDB_CODE_OUT_OF_BUFFER;
×
178
    goto _OVER;
×
179
  }
180

181
  nBytes = snprintf(realfile, sizeof(realfile), "%s%smnode.json", path, TD_DIRSEP);
361,848✔
182
  if (nBytes <= 0 || nBytes >= sizeof(realfile)) {
361,848✔
183
    code = TSDB_CODE_OUT_OF_BUFFER;
×
184
    goto _OVER;
×
185
  }
186

187
  // terrno = TSDB_CODE_OUT_OF_MEMORY;
188
  pJson = tjsonCreateObject();
361,848✔
189
  if (pJson == NULL) {
361,848✔
190
    code = terrno;
×
191
    goto _OVER;
×
192
  }
193

194
  TAOS_CHECK_GOTO(mmEncodeOption(pJson, pOption), NULL, _OVER);
361,848✔
195

196
  buffer = tjsonToString(pJson);
361,848✔
197
  if (buffer == NULL) {
361,848✔
198
    code = TSDB_CODE_INVALID_JSON_FORMAT;
×
199
    goto _OVER;
×
200
  }
201

202
  pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_WRITE_THROUGH);
361,848✔
203
  if (pFile == NULL) {
361,848✔
204
    code = terrno;
×
205
    goto _OVER;
×
206
  }
207

208
  int32_t len = strlen(buffer);
361,848✔
209
  if (taosWriteFile(pFile, buffer, len) <= 0) {
361,848✔
210
    code = terrno;
×
211
    goto _OVER;
×
212
  }
213
  if (taosFsyncFile(pFile) < 0) {
361,848✔
214
    code = terrno;
×
215
    goto _OVER;
×
216
  }
217

218
  if (taosCloseFile(&pFile) < 0) {
361,848✔
219
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
220
    goto _OVER;
×
221
  }
222
  TAOS_CHECK_GOTO(taosRenameFile(file, realfile), NULL, _OVER);
361,848✔
223

224
  dInfo("succeed to write mnode file:%s, deloyed:%d", realfile, pOption->deploy);
361,848✔
225

226
_OVER:
361,742✔
227
  if (pJson != NULL) tjsonDelete(pJson);
361,848✔
228
  if (buffer != NULL) taosMemoryFree(buffer);
361,848✔
229
  if (pFile != NULL) taosCloseFile(&pFile);
361,848✔
230

231
  if (code != 0) {
361,848✔
232
    dError("failed to write mnode file:%s since %s, deloyed:%d", realfile, tstrerror(code), pOption->deploy);
×
233
  }
234
  return code;
361,848✔
235
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc