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

taosdata / TDengine / #3531

19 Nov 2024 10:42AM UTC coverage: 60.213% (-0.006%) from 60.219%
#3531

push

travis-ci

web-flow
Merge pull request #28777 from taosdata/fix/3.0/TD-32366

fix:TD-32366/stmt add geometry datatype check

118529 of 252344 branches covered (46.97%)

Branch coverage included in aggregate %.

7 of 48 new or added lines in 3 files covered. (14.58%)

2282 existing lines in 115 files now uncovered.

199096 of 275161 relevant lines covered (72.36%)

6067577.83 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) {
18,026✔
23
  (void)taosThreadRwlockRdlock(&pMgmt->lock);
18,026✔
24

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

35
  void *pIter = taosHashIterate(pMgmt->hash, NULL);
18,026✔
36
  while (pIter) {
81,258✔
37
    SVnodeObj **ppVnode = pIter;
63,232✔
38
    SVnodeObj  *pVnode = *ppVnode;
63,232✔
39
    if (pVnode && num < size) {
126,464!
40
      int32_t refCount = atomic_add_fetch_32(&pVnode->refCount, 1);
63,232✔
41
      dTrace("vgId:%d,acquire vnode, vnode:%p, ref:%d", pVnode->vgId, pVnode, refCount);
63,232✔
42
      pVnodes[num++] = (*ppVnode);
63,232✔
43
      pIter = taosHashIterate(pMgmt->hash, pIter);
63,232✔
44
    } else {
45
      taosHashCancelIterate(pMgmt->hash, pIter);
×
46
    }
47
  }
48

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

67
  return 0;
18,026✔
68
}
69

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

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

81
  void *pIter = taosHashIterate(pMgmt->hash, NULL);
16,087✔
82
  while (pIter) {
46,029✔
83
    SVnodeObj **ppVnode = pIter;
29,939✔
84
    SVnodeObj  *pVnode = *ppVnode;
29,939✔
85
    if (pVnode && num < size) {
59,893!
86
      int32_t refCount = atomic_add_fetch_32(&pVnode->refCount, 1);
29,951✔
87
      dTrace("vgId:%d, acquire vnode, vnode:%p, ref:%d", pVnode->vgId, pVnode, refCount);
29,952✔
88
      pVnodes[num++] = (*ppVnode);
29,952✔
89
      pIter = taosHashIterate(pMgmt->hash, pIter);
29,952✔
90
    } else {
91
      taosHashCancelIterate(pMgmt->hash, pIter);
×
92
    }
93
  }
94

95
  (void)taosThreadRwlockUnlock(&pMgmt->lock);
16,090✔
96
  *numOfVnodes = num;
16,151✔
97
  *ppVnodes = pVnodes;
16,151✔
98

99
  return 0;
16,151✔
100
}
101

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

227
  for (int32_t i = 0; i < numOfVnodes; ++i) {
81,257✔
228
    SVnodeObj *pVnode = ppVnodes[i];
63,231✔
229
    if (pVnode == NULL) continue;
63,231!
230

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

243
  return 0;
18,026✔
244
}
245

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

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

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

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

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

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

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

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

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

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

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

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

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