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

taosdata / TDengine / #3524

08 Nov 2024 04:27AM UTC coverage: 60.898% (+5.0%) from 55.861%
#3524

push

travis-ci

web-flow
Merge pull request #28647 from taosdata/fix/3.0/TD-32519_drop_ctb

fix TD-32519 drop child table with tsma caused crash

118687 of 248552 branches covered (47.75%)

Branch coverage included in aggregate %.

286 of 337 new or added lines in 18 files covered. (84.87%)

9647 existing lines in 190 files now uncovered.

199106 of 273291 relevant lines covered (72.85%)

15236719.35 hits per line

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

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

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

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

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

67
  return 0;
21,406✔
68
}
69

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

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

81
  void *pIter = taosHashIterate(pMgmt->hash, NULL);
18,452✔
82
  while (pIter) {
95,442✔
83
    SVnodeObj **ppVnode = pIter;
76,995✔
84
    SVnodeObj  *pVnode = *ppVnode;
76,995✔
85
    if (pVnode && num < size) {
154,007!
86
      int32_t refCount = atomic_add_fetch_32(&pVnode->refCount, 1);
77,009✔
87
      dTrace("vgId:%d, acquire vnode, vnode:%p, ref:%d", pVnode->vgId, pVnode, refCount);
77,008✔
88
      pVnodes[num++] = (*ppVnode);
77,008✔
89
      pIter = taosHashIterate(pMgmt->hash, pIter);
77,008✔
90
    } else {
UNCOV
91
      taosHashCancelIterate(pMgmt->hash, pIter);
×
92
    }
93
  }
94

95
  (void)taosThreadRwlockUnlock(&pMgmt->lock);
18,447✔
96
  *numOfVnodes = num;
18,515✔
97
  *ppVnodes = pVnodes;
18,515✔
98

99
  return 0;
18,515✔
100
}
101

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

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

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

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

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

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

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

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

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

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

163
  pFile = taosOpenFile(file, TD_FILE_READ);
405✔
164
  if (pFile == NULL) {
405!
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;
405✔
171
  code = taosFStatFile(pFile, &size, NULL);
405✔
172
  if (code != 0) {
405!
173
    dError("failed to fstat mnode file:%s since %s", file, tstrerror(code));
×
174
    goto _OVER;
×
175
  }
176

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

183
  if (taosReadFile(pFile, pData, size) != size) {
405!
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';
405✔
190

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

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

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

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

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

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

227
  for (int32_t i = 0; i < numOfVnodes; ++i) {
138,572✔
228
    SVnodeObj *pVnode = ppVnodes[i];
117,166✔
229
    if (pVnode == NULL) continue;
117,166!
230

231
    SJson *vnode = tjsonCreateObject();
117,166✔
232
    if (vnode == NULL) return terrno;
117,167!
233
    if ((code = tjsonAddDoubleToObject(vnode, "vgId", pVnode->vgId)) < 0) return code;
117,167!
234
    if ((code = tjsonAddDoubleToObject(vnode, "dropped", pVnode->dropped)) < 0) return code;
117,167!
235
    if ((code = tjsonAddDoubleToObject(vnode, "vgVersion", pVnode->vgVersion)) < 0) return code;
117,163!
236
    if ((code = tjsonAddDoubleToObject(vnode, "diskPrimary", pVnode->diskPrimary)) < 0) return code;
117,167!
237
    if (pVnode->toVgId) {
117,166✔
238
      if ((code = tjsonAddDoubleToObject(vnode, "toVgId", pVnode->toVgId)) < 0) return code;
86!
239
    }
240
    if ((code = tjsonAddItemToArray(vnodes, vnode)) < 0) return code;
117,166!
241
  }
242

243
  return 0;
21,406✔
244
}
245

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

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

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

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

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

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

285
  code = taosThreadMutexLock(&pMgmt->fileLock);
21,406✔
286
  if (code != 0) {
21,406!
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);
21,406✔
292
  if (pFile == NULL) {
21,406!
293
    code = terrno;
×
294
    lino = __LINE__;
×
295
    goto _OVER1;
×
296
  }
297

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

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

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

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

326
_OVER:
21,406✔
327
  if (pJson != NULL) tjsonDelete(pJson);
21,406!
328
  if (buffer != NULL) taosMemoryFree(buffer);
21,406!
329
  if (pFile != NULL) taosCloseFile(&pFile);
21,406!
330
  if (ppVnodes != NULL) {
21,406!
331
    for (int32_t i = 0; i < numOfVnodes; ++i) {
138,575✔
332
      SVnodeObj *pVnode = ppVnodes[i];
117,169✔
333
      if (pVnode != NULL) {
117,169!
334
        vmReleaseVnode(pMgmt, pVnode);
117,169✔
335
      }
336
    }
337
    taosMemoryFree(ppVnodes);
21,406✔
338
  }
339

340
  if (code != 0) {
21,406!
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;
21,406✔
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