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

taosdata / TDengine / #3523

06 Nov 2024 02:29AM UTC coverage: 55.861% (-2.4%) from 58.216%
#3523

push

travis-ci

web-flow
Merge pull request #28551 from taosdata/feat/TS-5215-2

test(blob): testing & fixes for blob

106075 of 245834 branches covered (43.15%)

Branch coverage included in aggregate %.

0 of 15 new or added lines in 2 files covered. (0.0%)

17003 existing lines in 254 files now uncovered.

181910 of 269703 relevant lines covered (67.45%)

1527639.59 hits per line

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

63.99
/source/dnode/mgmt/mgmt_vnode/src/vmFile.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 "tjson.h"
18
#include "vmInt.h"
19

20
#define MAX_CONTENT_LEN 2 * 1024 * 1024
21

22
int32_t vmGetAllVnodeListFromHash(SVnodeMgmt *pMgmt, int32_t *numOfVnodes, SVnodeObj ***ppVnodes) {
10,824✔
23
  (void)taosThreadRwlockRdlock(&pMgmt->lock);
10,824✔
24

25
  int32_t num = 0;
10,824✔
26
  int32_t size = taosHashGetSize(pMgmt->hash);
10,824✔
27
  int32_t closedSize = taosHashGetSize(pMgmt->closedHash);
10,824✔
28
  size += closedSize;
10,824✔
29
  SVnodeObj **pVnodes = taosMemoryCalloc(size, sizeof(SVnodeObj *));
10,824✔
30
  if (pVnodes == NULL) {
10,824!
31
    (void)taosThreadRwlockUnlock(&pMgmt->lock);
×
32
    return terrno;
×
33
  }
34

35
  void *pIter = taosHashIterate(pMgmt->hash, NULL);
10,824✔
36
  while (pIter) {
2,050,821✔
37
    SVnodeObj **ppVnode = pIter;
2,039,997✔
38
    SVnodeObj  *pVnode = *ppVnode;
2,039,997✔
39
    if (pVnode && num < size) {
4,079,861!
40
      int32_t refCount = atomic_add_fetch_32(&pVnode->refCount, 1);
2,040,075✔
41
      dTrace("vgId:%d,acquire vnode, vnode:%p, ref:%d", pVnode->vgId, pVnode, refCount);
2,040,871✔
42
      pVnodes[num++] = (*ppVnode);
2,040,871✔
43
      pIter = taosHashIterate(pMgmt->hash, pIter);
2,040,871✔
44
    } else {
45
      taosHashCancelIterate(pMgmt->hash, pIter);
×
46
    }
47
  }
48

49
  pIter = taosHashIterate(pMgmt->closedHash, NULL);
10,824✔
50
  while (pIter) {
10,824!
51
    SVnodeObj **ppVnode = pIter;
×
52
    SVnodeObj  *pVnode = *ppVnode;
×
53
    if (pVnode && num < size) {
×
54
      int32_t refCount = atomic_add_fetch_32(&pVnode->refCount, 1);
×
55
      dTrace("vgId:%d, acquire vnode, vnode:%p, ref:%d", pVnode->vgId, pVnode, refCount);
×
56
      pVnodes[num++] = (*ppVnode);
×
57
      pIter = taosHashIterate(pMgmt->closedHash, pIter);
×
58
    } else {
59
      taosHashCancelIterate(pMgmt->closedHash, pIter);
×
60
    }
61
  }
62

63
  (void)taosThreadRwlockUnlock(&pMgmt->lock);
10,824✔
64
  *numOfVnodes = num;
10,824✔
65
  *ppVnodes = pVnodes;
10,824✔
66

67
  return 0;
10,824✔
68
}
69

70
int32_t vmGetVnodeListFromHash(SVnodeMgmt *pMgmt, int32_t *numOfVnodes, SVnodeObj ***ppVnodes) {
8,329✔
71
  (void)taosThreadRwlockRdlock(&pMgmt->lock);
8,329✔
72

73
  int32_t     num = 0;
8,351✔
74
  int32_t     size = taosHashGetSize(pMgmt->hash);
8,351✔
75
  SVnodeObj **pVnodes = taosMemoryCalloc(size, sizeof(SVnodeObj *));
8,332✔
76
  if (pVnodes == NULL) {
8,334!
77
    (void)taosThreadRwlockUnlock(&pMgmt->lock);
×
78
    return terrno;
×
79
  }
80

81
  void *pIter = taosHashIterate(pMgmt->hash, NULL);
8,334✔
82
  while (pIter) {
752,145✔
83
    SVnodeObj **ppVnode = pIter;
743,814✔
84
    SVnodeObj  *pVnode = *ppVnode;
743,814✔
85
    if (pVnode && num < size) {
1,487,617!
86
      int32_t refCount = atomic_add_fetch_32(&pVnode->refCount, 1);
743,733✔
87
      dTrace("vgId:%d, acquire vnode, vnode:%p, ref:%d", pVnode->vgId, pVnode, refCount);
744,144✔
88
      pVnodes[num++] = (*ppVnode);
744,144✔
89
      pIter = taosHashIterate(pMgmt->hash, pIter);
744,144✔
90
    } else {
91
      taosHashCancelIterate(pMgmt->hash, pIter);
81✔
92
    }
93
  }
94

95
  (void)taosThreadRwlockUnlock(&pMgmt->lock);
8,331✔
96
  *numOfVnodes = num;
8,350✔
97
  *ppVnodes = pVnodes;
8,350✔
98

99
  return 0;
8,350✔
100
}
101

102
static int32_t vmDecodeVnodeList(SJson *pJson, SVnodeMgmt *pMgmt, SWrapperCfg **ppCfgs, int32_t *numOfVnodes) {
170✔
103
  int32_t      code = -1;
170✔
104
  SWrapperCfg *pCfgs = NULL;
170✔
105
  *ppCfgs = NULL;
170✔
106

107
  SJson *vnodes = tjsonGetObjectItem(pJson, "vnodes");
170✔
108
  if (vnodes == NULL) return TSDB_CODE_INVALID_JSON_FORMAT;
170!
109

110
  int32_t vnodesNum = cJSON_GetArraySize(vnodes);
170✔
111
  if (vnodesNum > 0) {
170!
112
    pCfgs = taosMemoryCalloc(vnodesNum, sizeof(SWrapperCfg));
170✔
113
    if (pCfgs == NULL) return terrno;
170!
114
  }
115

116
  for (int32_t i = 0; i < vnodesNum; ++i) {
585✔
117
    SJson *vnode = tjsonGetArrayItem(vnodes, i);
415✔
118
    if (vnode == NULL) {
415!
119
      code = TSDB_CODE_INVALID_JSON_FORMAT;
×
120
      goto _OVER;
×
121
    }
122

123
    SWrapperCfg *pCfg = &pCfgs[i];
415✔
124
    tjsonGetInt32ValueFromDouble(vnode, "vgId", pCfg->vgId, code);
415✔
125
    if (code != 0) goto _OVER;
415!
126
    tjsonGetInt32ValueFromDouble(vnode, "dropped", pCfg->dropped, code);
415✔
127
    if (code != 0) goto _OVER;
415!
128
    tjsonGetInt32ValueFromDouble(vnode, "vgVersion", pCfg->vgVersion, code);
415✔
129
    if (code != 0) goto _OVER;
415!
130
    tjsonGetInt32ValueFromDouble(vnode, "diskPrimary", pCfg->diskPrimary, code);
415✔
131
    if (code != 0) goto _OVER;
415!
132
    tjsonGetInt32ValueFromDouble(vnode, "toVgId", pCfg->toVgId, code);
415✔
133
    if (code != 0) goto _OVER;
415!
134

135
    snprintf(pCfg->path, sizeof(pCfg->path), "%s%svnode%d", pMgmt->path, TD_DIRSEP, pCfg->vgId);
415✔
136
  }
137

138
  code = 0;
170✔
139
  *ppCfgs = pCfgs;
170✔
140
  *numOfVnodes = vnodesNum;
170✔
141

142
_OVER:
170✔
143
  if (*ppCfgs == NULL) taosMemoryFree(pCfgs);
170!
144
  return code;
170✔
145
}
146

147
int32_t vmGetVnodeListFromFile(SVnodeMgmt *pMgmt, SWrapperCfg **ppCfgs, int32_t *numOfVnodes) {
853✔
148
  int32_t      code = -1;
853✔
149
  TdFilePtr    pFile = NULL;
853✔
150
  char        *pData = NULL;
853✔
151
  SJson       *pJson = NULL;
853✔
152
  char         file[PATH_MAX] = {0};
853✔
153
  SWrapperCfg *pCfgs = NULL;
853✔
154
  snprintf(file, sizeof(file), "%s%svnodes.json", pMgmt->path, TD_DIRSEP);
853✔
155

156
  if (taosStatFile(file, NULL, NULL, NULL) < 0) {
853✔
157
    code = terrno;
683✔
158
    dInfo("vnode file:%s not exist, reason:%s", file, tstrerror(code));
683!
159
    code = 0;
683✔
160
    return code;
683✔
161
  }
162

163
  pFile = taosOpenFile(file, TD_FILE_READ);
170✔
164
  if (pFile == NULL) {
170!
165
    code = terrno;
×
166
    dError("failed to open vnode file:%s since %s", file, tstrerror(code));
×
167
    goto _OVER;
×
168
  }
169

170
  int64_t size = 0;
170✔
171
  code = taosFStatFile(pFile, &size, NULL);
170✔
172
  if (code != 0) {
170!
173
    dError("failed to fstat mnode file:%s since %s", file, tstrerror(code));
×
174
    goto _OVER;
×
175
  }
176

177
  pData = taosMemoryMalloc(size + 1);
170✔
178
  if (pData == NULL) {
170!
179
    code = terrno;
×
180
    goto _OVER;
×
181
  }
182

183
  if (taosReadFile(pFile, pData, size) != size) {
170!
184
    code = terrno;
×
185
    dError("failed to read vnode file:%s since %s", file, tstrerror(code));
×
186
    goto _OVER;
×
187
  }
188

189
  pData[size] = '\0';
170✔
190

191
  pJson = tjsonParse(pData);
170✔
192
  if (pJson == NULL) {
170!
193
    code = TSDB_CODE_INVALID_JSON_FORMAT;
×
194
    goto _OVER;
×
195
  }
196

197
  if (vmDecodeVnodeList(pJson, pMgmt, ppCfgs, numOfVnodes) < 0) {
170!
198
    code = TSDB_CODE_INVALID_JSON_FORMAT;
×
199
    goto _OVER;
×
200
  }
201

202
  code = 0;
170✔
203
  dInfo("succceed to read vnode file %s", file);
170!
204

205
_OVER:
×
206
  if (pData != NULL) taosMemoryFree(pData);
170!
207
  if (pJson != NULL) cJSON_Delete(pJson);
170!
208
  if (pFile != NULL) taosCloseFile(&pFile);
170!
209

210
  if (code != 0) {
170!
211
    dError("failed to read vnode file:%s since %s", file, tstrerror(code));
×
212
  }
213
  return code;
170✔
214
}
215

216
static int32_t vmEncodeVnodeList(SJson *pJson, SVnodeObj **ppVnodes, int32_t numOfVnodes) {
10,824✔
217
  int32_t code = 0;
10,824✔
218
  SJson  *vnodes = tjsonCreateArray();
10,824✔
219
  if (vnodes == NULL) {
10,824!
220
    return terrno;
×
221
  }
222
  if ((code = tjsonAddItemToObject(pJson, "vnodes", vnodes)) < 0) {
10,824!
223
    tjsonDelete(vnodes);
×
224
    return code;
×
225
  };
226

227
  for (int32_t i = 0; i < numOfVnodes; ++i) {
2,034,192✔
228
    SVnodeObj *pVnode = ppVnodes[i];
2,023,020✔
229
    if (pVnode == NULL) continue;
2,023,020!
230

231
    SJson *vnode = tjsonCreateObject();
2,023,020✔
232
    if (vnode == NULL) return terrno;
2,036,010!
233
    if ((code = tjsonAddDoubleToObject(vnode, "vgId", pVnode->vgId)) < 0) return code;
2,036,010!
234
    if ((code = tjsonAddDoubleToObject(vnode, "dropped", pVnode->dropped)) < 0) return code;
2,028,755!
235
    if ((code = tjsonAddDoubleToObject(vnode, "vgVersion", pVnode->vgVersion)) < 0) return code;
2,028,746!
236
    if ((code = tjsonAddDoubleToObject(vnode, "diskPrimary", pVnode->diskPrimary)) < 0) return code;
2,029,532!
237
    if (pVnode->toVgId) {
2,027,994!
UNCOV
238
      if ((code = tjsonAddDoubleToObject(vnode, "toVgId", pVnode->toVgId)) < 0) return code;
×
239
    }
240
    if ((code = tjsonAddItemToArray(vnodes, vnode)) < 0) return code;
2,027,994!
241
  }
242

243
  return 0;
11,172✔
244
}
245

246
int32_t vmWriteVnodeListToFile(SVnodeMgmt *pMgmt) {
10,824✔
247
  int32_t     code = -1;
10,824✔
248
  char       *buffer = NULL;
10,824✔
249
  SJson      *pJson = NULL;
10,824✔
250
  TdFilePtr   pFile = NULL;
10,824✔
251
  SVnodeObj **ppVnodes = NULL;
10,824✔
252
  char        file[PATH_MAX] = {0};
10,824✔
253
  char        realfile[PATH_MAX] = {0};
10,824✔
254
  int32_t     lino = 0;
10,824✔
255
  int32_t     ret = -1;
10,824✔
256

257
  int32_t nBytes = snprintf(file, sizeof(file), "%s%svnodes_tmp.json", pMgmt->path, TD_DIRSEP);
10,824✔
258
  if (nBytes <= 0 || nBytes >= sizeof(file)) {
10,824!
259
    return TSDB_CODE_OUT_OF_RANGE;
×
260
  }
261

262
  nBytes = snprintf(realfile, sizeof(realfile), "%s%svnodes.json", pMgmt->path, TD_DIRSEP);
10,824✔
263
  if (nBytes <= 0 || nBytes >= sizeof(realfile)) {
10,824!
264
    return TSDB_CODE_OUT_OF_RANGE;
×
265
  }
266

267
  int32_t numOfVnodes = 0;
10,824✔
268
  TAOS_CHECK_GOTO(vmGetAllVnodeListFromHash(pMgmt, &numOfVnodes, &ppVnodes), &lino, _OVER);
10,824!
269

270
  // terrno = TSDB_CODE_OUT_OF_MEMORY;
271
  pJson = tjsonCreateObject();
10,824✔
272
  if (pJson == NULL) {
10,824!
273
    code = terrno;
×
274
    goto _OVER;
×
275
  }
276
  TAOS_CHECK_GOTO(vmEncodeVnodeList(pJson, ppVnodes, numOfVnodes), &lino, _OVER);
10,824!
277

278
  buffer = tjsonToString(pJson);
10,824✔
279
  if (buffer == NULL) {
10,824!
280
    code = TSDB_CODE_INVALID_JSON_FORMAT;
×
281
    lino = __LINE__;
×
282
    goto _OVER;
×
283
  }
284

285
  code = taosThreadMutexLock(&pMgmt->fileLock);
10,824✔
286
  if (code != 0) {
10,824!
287
    lino = __LINE__;
×
288
    goto _OVER;
×
289
  }
290

291
  pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_WRITE_THROUGH);
10,824✔
292
  if (pFile == NULL) {
10,824!
293
    code = terrno;
×
294
    lino = __LINE__;
×
295
    goto _OVER1;
×
296
  }
297

298
  int32_t len = strlen(buffer);
10,824✔
299
  if (taosWriteFile(pFile, buffer, len) <= 0) {
10,824!
300
    code = terrno;
×
301
    lino = __LINE__;
×
302
    goto _OVER1;
×
303
  }
304
  if (taosFsyncFile(pFile) < 0) {
10,824!
305
    code = TAOS_SYSTEM_ERROR(errno);
×
306
    lino = __LINE__;
×
307
    goto _OVER1;
×
308
  }
309

310
  code = taosCloseFile(&pFile);
10,824✔
311
  if (code != 0) {
10,824!
312
    code = TAOS_SYSTEM_ERROR(errno);
×
313
    lino = __LINE__;
×
314
    goto _OVER1;
×
315
  }
316
  TAOS_CHECK_GOTO(taosRenameFile(file, realfile), &lino, _OVER1);
10,824!
317

318
  dInfo("succeed to write vnodes file:%s, vnodes:%d", realfile, numOfVnodes);
10,824!
319

320
_OVER1:
×
321
  ret = taosThreadMutexUnlock(&pMgmt->fileLock);
10,824✔
322
  if (ret != 0) {
10,824!
323
    dError("failed to unlock since %s", tstrerror(ret));
×
324
  }
325

326
_OVER:
10,824✔
327
  if (pJson != NULL) tjsonDelete(pJson);
10,824!
328
  if (buffer != NULL) taosMemoryFree(buffer);
10,824!
329
  if (pFile != NULL) taosCloseFile(&pFile);
10,824!
330
  if (ppVnodes != NULL) {
10,824!
331
    for (int32_t i = 0; i < numOfVnodes; ++i) {
2,055,268✔
332
      SVnodeObj *pVnode = ppVnodes[i];
2,044,444✔
333
      if (pVnode != NULL) {
2,044,444!
334
        vmReleaseVnode(pMgmt, pVnode);
2,044,444✔
335
      }
336
    }
337
    taosMemoryFree(ppVnodes);
10,824✔
338
  }
339

340
  if (code != 0) {
10,824!
341
    dError("failed to write vnodes file:%s at line:%d since %s, vnodes:%d", realfile, lino, tstrerror(code),
×
342
           numOfVnodes);
343
  }
344
  return code;
10,824✔
345
}
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