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

taosdata / TDengine / #4875

09 Dec 2025 01:22AM UTC coverage: 64.472% (-0.2%) from 64.623%
#4875

push

travis-ci

guanshengliang
fix: temporarily disable memory leak detection for UDF tests (#33856)

162014 of 251293 relevant lines covered (64.47%)

104318075.66 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) {
803,008✔
21
  int32_t code = 0;
803,008✔
22

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

32
  SJson *replicas = tjsonGetObjectItem(pJson, "replicas");
803,008✔
33
  if (replicas == NULL) return 0;
803,008✔
34
  pOption->numOfTotalReplicas = tjsonGetArraySize(replicas);
60,960✔
35

36
  pOption->numOfReplicas = 0;
60,960✔
37

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

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

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

59
  return 0;
60,960✔
60
}
61

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

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

75
  if (taosStatFile(file, NULL, NULL, NULL) < 0) {
1,718,044✔
76
    dInfo("mnode file:%s not exist, reason:%s", file, tstrerror(terrno));
915,036✔
77
    return 0;
915,036✔
78
  }
79

80
  pFile = taosOpenFile(file, TD_FILE_READ);
803,008✔
81
  if (pFile == NULL) {
803,008✔
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;
803,008✔
88
  code = taosFStatFile(pFile, &size, NULL);
803,008✔
89
  if (code != 0) {
803,008✔
90
    dError("failed to fstat mnode file:%s since %s", file, tstrerror(code));
×
91
    goto _OVER;
×
92
  }
93

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

100
  if (taosReadFile(pFile, pData, size) != size) {
803,008✔
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';
803,008✔
107

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

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

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

121
_OVER:
797,216✔
122
  if (pData != NULL) taosMemoryFree(pData);
803,008✔
123
  if (pJson != NULL) cJSON_Delete(pJson);
803,008✔
124
  if (pFile != NULL) taosCloseFile(&pFile);
803,008✔
125

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

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

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

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

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

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

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

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

164
  return code;
843,226✔
165
}
166

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

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

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

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

194
  TAOS_CHECK_GOTO(mmEncodeOption(pJson, pOption), NULL, _OVER);
843,226✔
195

196
  buffer = tjsonToString(pJson);
843,226✔
197
  if (buffer == NULL) {
843,226✔
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);
843,226✔
203
  if (pFile == NULL) {
843,226✔
204
    code = terrno;
×
205
    goto _OVER;
×
206
  }
207

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

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

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

226
_OVER:
840,178✔
227
  if (pJson != NULL) tjsonDelete(pJson);
843,226✔
228
  if (buffer != NULL) taosMemoryFree(buffer);
843,226✔
229
  if (pFile != NULL) taosCloseFile(&pFile);
843,226✔
230

231
  if (code != 0) {
843,226✔
232
    dError("failed to write mnode file:%s since %s, deloyed:%d", realfile, tstrerror(code), pOption->deploy);
×
233
  }
234
  return code;
843,226✔
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