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

taosdata / TDengine / #3541

26 Nov 2024 03:56AM UTC coverage: 60.776% (-0.07%) from 60.846%
#3541

push

travis-ci

web-flow
Merge pull request #28920 from taosdata/fix/TD-33008-3.0

fix(query)[TD-33008]. fix error handling in tsdbCacheRead

120076 of 252763 branches covered (47.51%)

Branch coverage included in aggregate %.

0 of 2 new or added lines in 1 file covered. (0.0%)

1395 existing lines in 154 files now uncovered.

200995 of 275526 relevant lines covered (72.95%)

19612328.37 hits per line

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

66.89
/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) {
21,643✔
23
  (void)taosThreadRwlockRdlock(&pMgmt->lock);
21,643✔
24

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

35
  void *pIter = taosHashIterate(pMgmt->hash, NULL);
21,643✔
36
  while (pIter) {
135,465✔
37
    SVnodeObj **ppVnode = pIter;
113,822✔
38
    SVnodeObj  *pVnode = *ppVnode;
113,822✔
39
    if (pVnode && num < size) {
227,644!
40
      int32_t refCount = atomic_add_fetch_32(&pVnode->refCount, 1);
113,822✔
41
      dTrace("vgId:%d,acquire vnode, vnode:%p, ref:%d", pVnode->vgId, pVnode, refCount);
113,822✔
42
      pVnodes[num++] = (*ppVnode);
113,822✔
43
      pIter = taosHashIterate(pMgmt->hash, pIter);
113,822✔
44
    } else {
UNCOV
45
      taosHashCancelIterate(pMgmt->hash, pIter);
×
46
    }
47
  }
48

49
  pIter = taosHashIterate(pMgmt->closedHash, NULL);
21,643✔
50
  while (pIter) {
21,643!
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);
21,643✔
64
  *numOfVnodes = num;
21,643✔
65
  *ppVnodes = pVnodes;
21,643✔
66

67
  return 0;
21,643✔
68
}
69

70
int32_t vmGetAllVnodeListFromHashWithCreating(SVnodeMgmt *pMgmt, int32_t *numOfVnodes, SVnodeObj ***ppVnodes) {
11,117✔
71
  (void)taosThreadRwlockRdlock(&pMgmt->lock);
11,117✔
72

73
  int32_t num = 0;
11,117✔
74
  int32_t size = taosHashGetSize(pMgmt->hash);
11,117✔
75
  int32_t creatingSize = taosHashGetSize(pMgmt->creatingHash);
11,117✔
76
  size += creatingSize;
11,117✔
77
  SVnodeObj **pVnodes = taosMemoryCalloc(size, sizeof(SVnodeObj *));
11,117✔
78
  if (pVnodes == NULL) {
11,117!
79
    (void)taosThreadRwlockUnlock(&pMgmt->lock);
×
80
    return terrno;
×
81
  }
82

83
  void *pIter = taosHashIterate(pMgmt->hash, NULL);
11,117✔
84
  while (pIter) {
42,868✔
85
    SVnodeObj **ppVnode = pIter;
31,751✔
86
    SVnodeObj  *pVnode = *ppVnode;
31,751✔
87
    if (pVnode && num < size) {
63,502!
88
      int32_t refCount = atomic_add_fetch_32(&pVnode->refCount, 1);
31,751✔
89
      dTrace("vgId:%d,acquire vnode, vnode:%p, ref:%d", pVnode->vgId, pVnode, refCount);
31,751✔
90
      pVnodes[num++] = (*ppVnode);
31,751✔
91
      pIter = taosHashIterate(pMgmt->hash, pIter);
31,751✔
92
    } else {
93
      taosHashCancelIterate(pMgmt->hash, pIter);
×
94
    }
95
  }
96

97
  pIter = taosHashIterate(pMgmt->creatingHash, NULL);
11,117✔
98
  while (pIter) {
22,101✔
99
    SVnodeObj **ppVnode = pIter;
10,984✔
100
    SVnodeObj  *pVnode = *ppVnode;
10,984✔
101
    if (pVnode && num < size) {
21,968!
102
      int32_t refCount = atomic_add_fetch_32(&pVnode->refCount, 1);
10,984✔
103
      dTrace("vgId:%d, acquire vnode, vnode:%p, ref:%d", pVnode->vgId, pVnode, refCount);
10,984✔
104
      pVnodes[num++] = (*ppVnode);
10,984✔
105
      pIter = taosHashIterate(pMgmt->creatingHash, pIter);
10,984✔
106
    } else {
107
      taosHashCancelIterate(pMgmt->creatingHash, pIter);
×
108
    }
109
  }
110
  (void)taosThreadRwlockUnlock(&pMgmt->lock);
11,117✔
111

112
  *numOfVnodes = num;
11,117✔
113
  *ppVnodes = pVnodes;
11,117✔
114

115
  return 0;
11,117✔
116
}
117

118
int32_t vmGetVnodeListFromHash(SVnodeMgmt *pMgmt, int32_t *numOfVnodes, SVnodeObj ***ppVnodes) {
7,518✔
119
  (void)taosThreadRwlockRdlock(&pMgmt->lock);
7,518✔
120

121
  int32_t     num = 0;
7,518✔
122
  int32_t     size = taosHashGetSize(pMgmt->hash);
7,518✔
123
  SVnodeObj **pVnodes = taosMemoryCalloc(size, sizeof(SVnodeObj *));
7,518✔
124
  if (pVnodes == NULL) {
7,518!
125
    (void)taosThreadRwlockUnlock(&pMgmt->lock);
×
126
    return terrno;
×
127
  }
128

129
  void *pIter = taosHashIterate(pMgmt->hash, NULL);
7,518✔
130
  while (pIter) {
45,961✔
131
    SVnodeObj **ppVnode = pIter;
38,443✔
132
    SVnodeObj  *pVnode = *ppVnode;
38,443✔
133
    if (pVnode && num < size) {
76,886!
134
      int32_t refCount = atomic_add_fetch_32(&pVnode->refCount, 1);
38,443✔
135
      dTrace("vgId:%d, acquire vnode, vnode:%p, ref:%d", pVnode->vgId, pVnode, refCount);
38,443✔
136
      pVnodes[num++] = (*ppVnode);
38,443✔
137
      pIter = taosHashIterate(pMgmt->hash, pIter);
38,443✔
138
    } else {
139
      taosHashCancelIterate(pMgmt->hash, pIter);
×
140
    }
141
  }
142

143
  (void)taosThreadRwlockUnlock(&pMgmt->lock);
7,518✔
144
  *numOfVnodes = num;
7,518✔
145
  *ppVnodes = pVnodes;
7,518✔
146

147
  return 0;
7,518✔
148
}
149

150
static int32_t vmDecodeVnodeList(SJson *pJson, SVnodeMgmt *pMgmt, SWrapperCfg **ppCfgs, int32_t *numOfVnodes) {
408✔
151
  int32_t      code = -1;
408✔
152
  SWrapperCfg *pCfgs = NULL;
408✔
153
  *ppCfgs = NULL;
408✔
154

155
  SJson *vnodes = tjsonGetObjectItem(pJson, "vnodes");
408✔
156
  if (vnodes == NULL) return TSDB_CODE_INVALID_JSON_FORMAT;
408!
157

158
  int32_t vnodesNum = cJSON_GetArraySize(vnodes);
408✔
159
  if (vnodesNum > 0) {
408✔
160
    pCfgs = taosMemoryCalloc(vnodesNum, sizeof(SWrapperCfg));
406✔
161
    if (pCfgs == NULL) return terrno;
406!
162
  }
163

164
  for (int32_t i = 0; i < vnodesNum; ++i) {
1,480✔
165
    SJson *vnode = tjsonGetArrayItem(vnodes, i);
1,072✔
166
    if (vnode == NULL) {
1,072!
167
      code = TSDB_CODE_INVALID_JSON_FORMAT;
×
168
      goto _OVER;
×
169
    }
170

171
    SWrapperCfg *pCfg = &pCfgs[i];
1,072✔
172
    tjsonGetInt32ValueFromDouble(vnode, "vgId", pCfg->vgId, code);
1,072✔
173
    if (code != 0) goto _OVER;
1,072!
174
    tjsonGetInt32ValueFromDouble(vnode, "dropped", pCfg->dropped, code);
1,072✔
175
    if (code != 0) goto _OVER;
1,072!
176
    tjsonGetInt32ValueFromDouble(vnode, "vgVersion", pCfg->vgVersion, code);
1,072✔
177
    if (code != 0) goto _OVER;
1,072!
178
    tjsonGetInt32ValueFromDouble(vnode, "diskPrimary", pCfg->diskPrimary, code);
1,072✔
179
    if (code != 0) goto _OVER;
1,072!
180
    tjsonGetInt32ValueFromDouble(vnode, "toVgId", pCfg->toVgId, code);
1,072✔
181
    if (code != 0) goto _OVER;
1,072!
182

183
    snprintf(pCfg->path, sizeof(pCfg->path), "%s%svnode%d", pMgmt->path, TD_DIRSEP, pCfg->vgId);
1,072✔
184
  }
185

186
  code = 0;
408✔
187
  *ppCfgs = pCfgs;
408✔
188
  *numOfVnodes = vnodesNum;
408✔
189

190
_OVER:
408✔
191
  if (*ppCfgs == NULL) taosMemoryFree(pCfgs);
408✔
192
  return code;
408✔
193
}
194

195
int32_t vmGetVnodeListFromFile(SVnodeMgmt *pMgmt, SWrapperCfg **ppCfgs, int32_t *numOfVnodes) {
2,393✔
196
  int32_t      code = -1;
2,393✔
197
  TdFilePtr    pFile = NULL;
2,393✔
198
  char        *pData = NULL;
2,393✔
199
  SJson       *pJson = NULL;
2,393✔
200
  char         file[PATH_MAX] = {0};
2,393✔
201
  SWrapperCfg *pCfgs = NULL;
2,393✔
202
  snprintf(file, sizeof(file), "%s%svnodes.json", pMgmt->path, TD_DIRSEP);
2,393✔
203

204
  if (taosStatFile(file, NULL, NULL, NULL) < 0) {
2,393✔
205
    code = terrno;
1,985✔
206
    dInfo("vnode file:%s not exist, reason:%s", file, tstrerror(code));
1,985!
207
    code = 0;
1,985✔
208
    return code;
1,985✔
209
  }
210

211
  pFile = taosOpenFile(file, TD_FILE_READ);
408✔
212
  if (pFile == NULL) {
408!
213
    code = terrno;
×
214
    dError("failed to open vnode file:%s since %s", file, tstrerror(code));
×
215
    goto _OVER;
×
216
  }
217

218
  int64_t size = 0;
408✔
219
  code = taosFStatFile(pFile, &size, NULL);
408✔
220
  if (code != 0) {
408!
221
    dError("failed to fstat mnode file:%s since %s", file, tstrerror(code));
×
222
    goto _OVER;
×
223
  }
224

225
  pData = taosMemoryMalloc(size + 1);
408✔
226
  if (pData == NULL) {
408!
227
    code = terrno;
×
228
    goto _OVER;
×
229
  }
230

231
  if (taosReadFile(pFile, pData, size) != size) {
408!
232
    code = terrno;
×
233
    dError("failed to read vnode file:%s since %s", file, tstrerror(code));
×
234
    goto _OVER;
×
235
  }
236

237
  pData[size] = '\0';
408✔
238

239
  pJson = tjsonParse(pData);
408✔
240
  if (pJson == NULL) {
408!
241
    code = TSDB_CODE_INVALID_JSON_FORMAT;
×
242
    goto _OVER;
×
243
  }
244

245
  if (vmDecodeVnodeList(pJson, pMgmt, ppCfgs, numOfVnodes) < 0) {
408!
246
    code = TSDB_CODE_INVALID_JSON_FORMAT;
×
247
    goto _OVER;
×
248
  }
249

250
  code = 0;
408✔
251
  dInfo("succceed to read vnode file %s", file);
408!
252

253
_OVER:
×
254
  if (pData != NULL) taosMemoryFree(pData);
408!
255
  if (pJson != NULL) cJSON_Delete(pJson);
408!
256
  if (pFile != NULL) taosCloseFile(&pFile);
408!
257

258
  if (code != 0) {
408!
259
    dError("failed to read vnode file:%s since %s", file, tstrerror(code));
×
260
  }
261
  return code;
408✔
262
}
263

264
static int32_t vmEncodeVnodeList(SJson *pJson, SVnodeObj **ppVnodes, int32_t numOfVnodes) {
21,643✔
265
  int32_t code = 0;
21,643✔
266
  SJson  *vnodes = tjsonCreateArray();
21,643✔
267
  if (vnodes == NULL) {
21,643!
268
    return terrno;
×
269
  }
270
  if ((code = tjsonAddItemToObject(pJson, "vnodes", vnodes)) < 0) {
21,643!
271
    tjsonDelete(vnodes);
×
272
    return code;
×
273
  };
274

275
  for (int32_t i = 0; i < numOfVnodes; ++i) {
135,455✔
276
    SVnodeObj *pVnode = ppVnodes[i];
113,814✔
277
    if (pVnode == NULL) continue;
113,814!
278

279
    SJson *vnode = tjsonCreateObject();
113,814✔
280
    if (vnode == NULL) return terrno;
113,817!
281
    if ((code = tjsonAddDoubleToObject(vnode, "vgId", pVnode->vgId)) < 0) return code;
113,817!
282
    if ((code = tjsonAddDoubleToObject(vnode, "dropped", pVnode->dropped)) < 0) return code;
113,812!
283
    if ((code = tjsonAddDoubleToObject(vnode, "vgVersion", pVnode->vgVersion)) < 0) return code;
113,814!
284
    if ((code = tjsonAddDoubleToObject(vnode, "diskPrimary", pVnode->diskPrimary)) < 0) return code;
113,817!
285
    if (pVnode->toVgId) {
113,813✔
286
      if ((code = tjsonAddDoubleToObject(vnode, "toVgId", pVnode->toVgId)) < 0) return code;
86!
287
    }
288
    if ((code = tjsonAddItemToArray(vnodes, vnode)) < 0) return code;
113,813!
289
  }
290

291
  return 0;
21,641✔
292
}
293

294
int32_t vmWriteVnodeListToFile(SVnodeMgmt *pMgmt) {
21,643✔
295
  int32_t     code = -1;
21,643✔
296
  char       *buffer = NULL;
21,643✔
297
  SJson      *pJson = NULL;
21,643✔
298
  TdFilePtr   pFile = NULL;
21,643✔
299
  SVnodeObj **ppVnodes = NULL;
21,643✔
300
  char        file[PATH_MAX] = {0};
21,643✔
301
  char        realfile[PATH_MAX] = {0};
21,643✔
302
  int32_t     lino = 0;
21,643✔
303
  int32_t     ret = -1;
21,643✔
304

305
  int32_t nBytes = snprintf(file, sizeof(file), "%s%svnodes_tmp.json", pMgmt->path, TD_DIRSEP);
21,643✔
306
  if (nBytes <= 0 || nBytes >= sizeof(file)) {
21,643!
307
    return TSDB_CODE_OUT_OF_RANGE;
×
308
  }
309

310
  nBytes = snprintf(realfile, sizeof(realfile), "%s%svnodes.json", pMgmt->path, TD_DIRSEP);
21,643✔
311
  if (nBytes <= 0 || nBytes >= sizeof(realfile)) {
21,643!
312
    return TSDB_CODE_OUT_OF_RANGE;
×
313
  }
314

315
  int32_t numOfVnodes = 0;
21,643✔
316
  TAOS_CHECK_GOTO(vmGetAllVnodeListFromHash(pMgmt, &numOfVnodes, &ppVnodes), &lino, _OVER);
21,643!
317

318
  // terrno = TSDB_CODE_OUT_OF_MEMORY;
319
  pJson = tjsonCreateObject();
21,643✔
320
  if (pJson == NULL) {
21,642!
321
    code = terrno;
×
322
    goto _OVER;
×
323
  }
324
  TAOS_CHECK_GOTO(vmEncodeVnodeList(pJson, ppVnodes, numOfVnodes), &lino, _OVER);
21,642!
325

326
  buffer = tjsonToString(pJson);
21,642✔
327
  if (buffer == NULL) {
21,643!
328
    code = TSDB_CODE_INVALID_JSON_FORMAT;
×
329
    lino = __LINE__;
×
330
    goto _OVER;
×
331
  }
332

333
  code = taosThreadMutexLock(&pMgmt->fileLock);
21,643✔
334
  if (code != 0) {
21,643!
335
    lino = __LINE__;
×
336
    goto _OVER;
×
337
  }
338

339
  pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_WRITE_THROUGH);
21,643✔
340
  if (pFile == NULL) {
21,643!
341
    code = terrno;
×
342
    lino = __LINE__;
×
343
    goto _OVER1;
×
344
  }
345

346
  int32_t len = strlen(buffer);
21,643✔
347
  if (taosWriteFile(pFile, buffer, len) <= 0) {
21,643!
348
    code = terrno;
×
349
    lino = __LINE__;
×
350
    goto _OVER1;
×
351
  }
352
  if (taosFsyncFile(pFile) < 0) {
21,643!
353
    code = TAOS_SYSTEM_ERROR(errno);
×
354
    lino = __LINE__;
×
355
    goto _OVER1;
×
356
  }
357

358
  code = taosCloseFile(&pFile);
21,643✔
359
  if (code != 0) {
21,643!
360
    code = TAOS_SYSTEM_ERROR(errno);
×
361
    lino = __LINE__;
×
362
    goto _OVER1;
×
363
  }
364
  TAOS_CHECK_GOTO(taosRenameFile(file, realfile), &lino, _OVER1);
21,643!
365

366
  dInfo("succeed to write vnodes file:%s, vnodes:%d", realfile, numOfVnodes);
21,643!
367

368
_OVER1:
×
369
  ret = taosThreadMutexUnlock(&pMgmt->fileLock);
21,643✔
370
  if (ret != 0) {
21,643!
371
    dError("failed to unlock since %s", tstrerror(ret));
×
372
  }
373

374
_OVER:
21,643✔
375
  if (pJson != NULL) tjsonDelete(pJson);
21,643!
376
  if (buffer != NULL) taosMemoryFree(buffer);
21,643!
377
  if (pFile != NULL) taosCloseFile(&pFile);
21,643!
378
  if (ppVnodes != NULL) {
21,643!
379
    for (int32_t i = 0; i < numOfVnodes; ++i) {
135,465✔
380
      SVnodeObj *pVnode = ppVnodes[i];
113,822✔
381
      if (pVnode != NULL) {
113,822!
382
        vmReleaseVnode(pMgmt, pVnode);
113,822✔
383
      }
384
    }
385
    taosMemoryFree(ppVnodes);
21,643✔
386
  }
387

388
  if (code != 0) {
21,643!
389
    dError("failed to write vnodes file:%s at line:%d since %s, vnodes:%d", realfile, lino, tstrerror(code),
×
390
           numOfVnodes);
391
  }
392
  return code;
21,643✔
393
}
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

© 2025 Coveralls, Inc