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

taosdata / TDengine / #4938

23 Jan 2026 09:40AM UTC coverage: 66.8% (+0.006%) from 66.794%
#4938

push

travis-ci

web-flow
fix: case failuer caused by the modification of the error description (#34391)

204187 of 305671 relevant lines covered (66.8%)

124015580.65 hits per line

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

63.4
/source/libs/txnode/src/txnodeMgmt.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
// clang-format off
17
#include "uv.h"
18
#include "os.h"
19
#include "tarray.h"
20
#include "tglobal.h"
21
#include "txnode.h"
22
#include "txnodeInt.h"
23
#include "osString.h"
24

25
// clang-format on
26

27
extern char **environ;
28

29
#ifdef WINDOWS
30
#define XNODED_DEFAULT_PATH_1    "C:\\TDengine"
31
#define XNODED_DEFAULT_PATH_2    "C:\\TDengine"
32
#define XNODED_DEFAULT_EXEC_NAME "xnoded"
33
#define XNODED_DEFAULT_EXEC      "\\xnoded.exe"
34
#else
35
#define XNODED_DEFAULT_PATH_1    "/usr/bin"
36
#define XNODED_DEFAULT_PATH_2    "/usr/local/taos/bin"
37
#define XNODED_DEFAULT_EXEC_NAME "xnoded"
38
#define XNODED_DEFAULT_EXEC      "/xnoded"
39
#endif
40

41
#define XNODED_XNODED_PID_NAME ".xnoded.pid"
42

43
typedef struct {
44
  bool         isStarted;
45
  bool         needCleanUp;
46
  uv_loop_t    loop;
47
  uv_thread_t  thread;
48
  uv_barrier_t barrier;
49
  uv_process_t process;
50
  int32_t      spawnErr;
51
  uv_pipe_t    ctrlPipe;
52
  uv_async_t   stopAsync;
53
  int32_t      isStopped;
54
  int32_t      dnodeId;
55
  int64_t      clusterId;
56
  char         userPass[XNODE_USER_PASS_LEN];
57
  SEp          leaderEp;
58
} SXnodedData;
59

60
SXnodedData xnodedGlobal = {0};
61

62
static int32_t xnodeMgmtSpawnXnoded(SXnodedData *pData);
63

64
static void getXnodedPidPath(char *pipeName, int32_t size) {
426✔
65
#ifdef _WIN32
66
  snprintf(pipeName, size, "%s%s", tsDataDir, XNODED_XNODED_PID_NAME);
67
#else
68
  snprintf(pipeName, size, "%s%s", tsDataDir, XNODED_XNODED_PID_NAME);
426✔
69
#endif
70
  xndDebug("xnode get xnoded pid path:%s", pipeName);
426✔
71
}
426✔
72

73
static void    xnodeMgmtXnodedExit(uv_process_t *process, int64_t exitStatus, int32_t termSignal) {
×
74
  TAOS_XNODED_MGMT_CHECK_PTR_RVOID(process);
×
75
  xndDebug("xnoded process exited with status %" PRId64 ", signal %d", exitStatus, termSignal);
×
76
  SXnodedData *pData = process->data;
×
77
  if (pData == NULL) {
×
78
    xndError("xnoded process data is NULL");
×
79
    return;
×
80
  }
81
  if ((exitStatus == 0 && termSignal == 0) || atomic_load_32(&pData->isStopped)) {
×
82
    xndInfo("xnoded process exit due to exit status 0 or dnode-mgmt called stop");
×
83
    if (uv_async_send(&pData->stopAsync) != 0) {
×
84
      xndError("stop xnoded: failed to send stop async");
×
85
    }
86
    char xnodedPipeSocket[PATH_MAX] = {0};
×
87
    getXnodedPipeName(xnodedPipeSocket, PATH_MAX);
×
88
    if (0 != unlink(xnodedPipeSocket)) {
×
89
      xndWarn("txnode failed to unlink, socket:%s, err:%s", xnodedPipeSocket, terrstr());
×
90
    }
91

92
    char *pidPath = xnodedPipeSocket;
×
93
    memset(pidPath, 0, PATH_MAX);
×
94
    getXnodedPidPath(pidPath, PATH_MAX);
×
95
    (void)taosRemoveFile(pidPath);
×
96
  } else {
97
    xndInfo("xnoded process restart, exit status %" PRId64 ", signal %d", exitStatus, termSignal);
×
98
    uv_sleep(2000);
×
99
    int32_t code = xnodeMgmtSpawnXnoded(pData);
×
100
    if (code != 0) {
×
101
      xndError("xnoded process restart failed with code:%d", code);
×
102
    }
103
  }
104
}
105
void killPreXnoded() {
426✔
106
  char buf[PATH_MAX] = {0};
426✔
107
  getXnodedPidPath(buf, sizeof(buf));
426✔
108

109
  TdFilePtr pFile = NULL;
426✔
110
  pFile = taosOpenFile(buf, TD_FILE_READ);
426✔
111
  if (pFile == NULL) {
426✔
112
    xndWarn("xnode failed to open xnoded pid file:%s, file may not exist", buf);
426✔
113
    return;
426✔
114
  }
115
  int64_t readSize = taosReadFile(pFile, buf, sizeof(buf));
×
116
  if (readSize <= 0) {
×
117
    if (readSize < 0) {
×
118
      xndError("xnode failed to read len from file:%p since %s", pFile, terrstr());
×
119
    }
120
    (void)taosCloseFile(&pFile);
×
121
    return;
×
122
  }
123
  (void)taosCloseFile(&pFile);
×
124

125
  int32_t pid = taosStr2Int32(buf, NULL, 10);
×
126
  int result = uv_kill((uv_pid_t)pid, SIGTERM);
×
127
  if (result != 0) {
×
128
    if (result != UV_ESRCH) {
×
129
      xndError("xnode failed to kill process %d: %s", pid, uv_strerror(result));
×
130
    }
131
    return;
×
132
  }
133
}
134

135
void saveXnodedPid(int32_t pid) {
×
136
  char buf[PATH_MAX] = {0};
×
137
  getXnodedPidPath(buf, sizeof(buf));
×
138
  TdFilePtr testFilePtr = taosCreateFile(buf, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_READ | TD_FILE_TRUNC);
×
139
  snprintf(buf, PATH_MAX, "%d", pid);
×
140
  (void)taosWriteFile(testFilePtr, buf, strlen(buf));
×
141
  (void)taosCloseFile(&testFilePtr);
×
142
}
×
143

144
static void locateXnodedExecFile(char *path) {
426✔
145
  if (tsProcPath == NULL) {
426✔
146
    path[0] = '.';
×
147
#ifdef WINDOWS
148
    GetModuleFileName(NULL, path, PATH_MAX);
149
#elif defined(_TD_DARWIN_64)
150
    uint32_t pathSize = sizeof(path);
151
    _NSGetExecutablePath(path, &pathSize);
152
#endif
153
  } else {
154
    TAOS_STRNCPY(path, tsProcPath, PATH_MAX);
426✔
155
  }
156

157
  TAOS_DIRNAME(path);
426✔
158
  if (strlen(path) != 0) {
426✔
159
    TAOS_STRCAT(path, XNODED_DEFAULT_EXEC);
426✔
160
    if (taosCheckExistFile(path)) {
426✔
161
      goto _ok;
×
162
    }
163
    xndDebug("can't find xnoded exec file:%s", path);
426✔
164
    path[0] = '\0';
426✔
165
  }
166

167
  TAOS_STRCAT(path, XNODED_DEFAULT_PATH_1);
426✔
168
  TAOS_STRCAT(path, XNODED_DEFAULT_EXEC);
426✔
169
  if (taosCheckExistFile(path)) {
426✔
170
    goto _ok;
×
171
  }
172
  xndDebug("can't find xnoded exec file:%s", path);
426✔
173
  path[0] = '\0';
426✔
174

175
  TAOS_STRCAT(path, XNODED_DEFAULT_PATH_2);
426✔
176
  TAOS_STRCAT(path, XNODED_DEFAULT_EXEC);
426✔
177
  if (taosCheckExistFile(path)) {
426✔
178
    goto _ok;
×
179
  }
180
  xndDebug("can't find xnoded exec file:%s", path);
426✔
181
  path[0] = '\0';
426✔
182

183
  path[0] = '.';
426✔
184
  path[1] = '\0';
426✔
185
  TAOS_STRCAT(path, XNODED_DEFAULT_EXEC);
426✔
186
  if (taosCheckExistFile(path)) {
426✔
187
    goto _ok;
×
188
  }
189
  xndDebug("can't find xnoded exec file:%s", path);
426✔
190
  path[0] = '\0';
426✔
191

192
  TAOS_STRNCPY(path, XNODED_DEFAULT_EXEC_NAME, PATH_MAX);
426✔
193
  xndInfo("can't find xnoded exec file, use default: %s", path);
426✔
194
  return;
426✔
195

196
_ok:
×
197
  xndInfo("find xnoded exec file:%s", path);
×
198
  return;
×
199
}
200

201
static int32_t xnodeMgmtSpawnXnoded(SXnodedData *pData) {
426✔
202
  xndDebug("start to init xnoded");
426✔
203
  TAOS_XNODED_MGMT_CHECK_PTR_RCODE(pData);
852✔
204

205
  int32_t              err = 0;
426✔
206
  uv_process_options_t options = {0};
426✔
207

208
  char path[PATH_MAX] = {0};
426✔
209
  locateXnodedExecFile(path);
426✔
210

211
  xndInfo("xnode mgmt spawn xnoded path: %s", path);
426✔
212
  // char *argsXnoded[] = {path, "-c", configDir, "-d", dnodeId, NULL};
213
  char *argsXnoded[] = {path, NULL};
426✔
214
  options.args = argsXnoded;
426✔
215
  options.file = path;
426✔
216

217
  options.exit_cb = xnodeMgmtXnodedExit;
426✔
218

219
  killPreXnoded();
426✔
220

221
  char xnodedPipeSocket[PATH_MAX] = {0};
426✔
222
  getXnodedPipeName(xnodedPipeSocket, PATH_MAX);
426✔
223
  if (0 != unlink(xnodedPipeSocket)) {
426✔
224
    xndWarn("txnode failed to unlink, ignore if first time, socket:%s, err:%s", xnodedPipeSocket, terrstr());
426✔
225
  }
226

227
  TAOS_UV_LIB_ERROR_RET(uv_pipe_init(&pData->loop, &pData->ctrlPipe, 1));
426✔
228

229
  uv_stdio_container_t child_stdio[3];
426✔
230
  child_stdio[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE;
426✔
231
  child_stdio[0].data.stream = (uv_stream_t *)&pData->ctrlPipe;
426✔
232
  child_stdio[1].flags = UV_IGNORE;
426✔
233
  child_stdio[2].flags = UV_INHERIT_FD;
426✔
234
  child_stdio[2].data.fd = 2;
426✔
235
  options.stdio_count = 3;
426✔
236
  options.stdio = child_stdio;
426✔
237

238
  options.flags = UV_PROCESS_DETACHED;
426✔
239

240
  char xnodedCfgDir[PATH_MAX] = {0};
426✔
241
  snprintf(xnodedCfgDir, PATH_MAX, "%s=%s", "XNODED_CFG_DIR", configDir);
426✔
242
  char xnodedLogDir[PATH_MAX] = {0};
426✔
243
  snprintf(xnodedLogDir, PATH_MAX, "%s=%s", "XNODED_LOG_DIR", tsLogDir);
426✔
244
  char dnodeIdEnvItem[64] = {0};
426✔
245
  snprintf(dnodeIdEnvItem, 64, "%s=%s:%d", "XNODED_LEADER_EP", pData->leaderEp.fqdn, pData->leaderEp.port);
426✔
246
  char xnodedUserPass[XNODE_USER_PASS_LEN] = {0};
426✔
247
  snprintf(xnodedUserPass, XNODE_USER_PASS_LEN, "%s=%s", "XNODED_USER_PASS", pData->userPass);
426✔
248
  char xnodeClusterId[32] = {0};
426✔
249
  snprintf(xnodeClusterId, 32, "%s=%" PRIu64, "XNODED_CLUSTER_ID", pData->clusterId);
426✔
250
  char xnodePipeSocket[PATH_MAX + 64] = {0};
426✔
251
  snprintf(xnodePipeSocket, PATH_MAX + 64, "%s=%s", "XNODED_LISTEN", xnodedPipeSocket);
426✔
252

253
  char xnodedLogLevel[32] = {0};
426✔
254
  if (xndDebugFlag & DEBUG_INFO) {
426✔
255
    snprintf(xnodedLogLevel, 32, "%s=%s", "XNODED_LOG_LEVEL", "info");
426✔
256
  }
257
  if (xndDebugFlag & DEBUG_DEBUG) {
426✔
258
    snprintf(xnodedLogLevel, 32, "%s=%s", "XNODED_LOG_LEVEL", "debug");
×
259
  }
260
  if (xndDebugFlag & DEBUG_TRACE) {
426✔
261
    snprintf(xnodedLogLevel, 32, "%s=%s", "XNODED_LOG_LEVEL", "trace");
×
262
  }
263

264
  xndDebug("txnode env: leader ep: %s, user pass:%s, pipe socket:%s, log level:%s", dnodeIdEnvItem, xnodedUserPass,
426✔
265
           xnodePipeSocket, xnodedLogLevel);
266

267
  char *envXnoded[] = {xnodedCfgDir,   xnodedLogDir,    dnodeIdEnvItem, xnodedUserPass,
426✔
268
                       xnodeClusterId, xnodePipeSocket, xnodedLogLevel, NULL};
269

270
  char **envXnodedWithPEnv = NULL;
426✔
271
  if (environ != NULL) {
426✔
272
    int32_t lenEnvXnoded = ARRAY_SIZE(envXnoded);
426✔
273
    int32_t numEnviron = 0;
426✔
274
    while (environ[numEnviron] != NULL) {
13,206✔
275
      numEnviron++;
12,780✔
276
    }
277

278
    envXnodedWithPEnv = (char **)taosMemoryCalloc(numEnviron + lenEnvXnoded, sizeof(char *));
426✔
279
    if (envXnodedWithPEnv == NULL) {
426✔
280
      err = TSDB_CODE_OUT_OF_MEMORY;
×
281
      goto _OVER;
×
282
    }
283

284
    for (int32_t i = 0; i < numEnviron; i++) {
13,206✔
285
      int32_t len = strlen(environ[i]) + 1;
12,780✔
286
      xndDebug("xnoded exec env: %s", environ[i]);
12,780✔
287
      envXnodedWithPEnv[i] = (char *)taosMemoryCalloc(len, 1);
12,780✔
288
      if (envXnodedWithPEnv[i] == NULL) {
12,780✔
289
        err = TSDB_CODE_OUT_OF_MEMORY;
×
290
        goto _OVER;
×
291
      }
292

293
      tstrncpy(envXnodedWithPEnv[i], environ[i], len);
12,780✔
294
    }
295

296
    for (int32_t i = 0; i < lenEnvXnoded; i++) {
3,834✔
297
      if (envXnoded[i] != NULL) {
3,408✔
298
        int32_t len = strlen(envXnoded[i]) + 1;
2,982✔
299
        envXnodedWithPEnv[numEnviron + i] = (char *)taosMemoryCalloc(len, 1);
2,982✔
300
        if (envXnodedWithPEnv[numEnviron + i] == NULL) {
2,982✔
301
          err = TSDB_CODE_OUT_OF_MEMORY;
×
302
          goto _OVER;
×
303
        }
304

305
        tstrncpy(envXnodedWithPEnv[numEnviron + i], envXnoded[i], len);
2,982✔
306
      }
307
    }
308
    envXnodedWithPEnv[numEnviron + lenEnvXnoded - 1] = NULL;
426✔
309

310
    options.env = envXnodedWithPEnv;
426✔
311
  } else {
312
    options.env = envXnoded;
×
313
  }
314

315
  err = uv_spawn(&pData->loop, &pData->process, &options);
426✔
316
  pData->process.data = (void *)pData;
426✔
317
  if (err != 0) {
426✔
318
    xndError("can not spawn xnoded. path: %s, error: %s", path, uv_strerror(err));
426✔
319
  } else {
320
    xndInfo("xnoded is initialized, xnoded pid: %d", pData->process.pid);
×
321
    saveXnodedPid(pData->process.pid);
×
322
  }
323

324
_OVER:
426✔
325
  // if (taosFqdnEnvItem) {
326
  //   taosMemoryFree(taosFqdnEnvItem);
327
  // }
328

329
  if (envXnodedWithPEnv != NULL) {
426✔
330
    int32_t i = 0;
426✔
331
    while (envXnodedWithPEnv[i] != NULL) {
16,188✔
332
      taosMemoryFree(envXnodedWithPEnv[i]);
15,762✔
333
      i++;
15,762✔
334
    }
335
    taosMemoryFree(envXnodedWithPEnv);
426✔
336
  }
337

338
  return err;
426✔
339
}
340

341
static void xnodeMgmtXnodedCloseWalkCb(uv_handle_t *handle, void *arg) {
×
342
  TAOS_XNODED_MGMT_CHECK_PTR_RVOID(handle);
×
343
  if (!uv_is_closing(handle)) {
×
344
    uv_close(handle, NULL);
×
345
  }
346
}
347

348
static void xnodeMgmtXnodedStopAsyncCb(uv_async_t *async) {
×
349
  TAOS_XNODED_MGMT_CHECK_PTR_RVOID(async);
×
350
  SXnodedData *pData = async->data;
×
351
  uv_stop(&pData->loop);
×
352
}
353

354
static void xnodeMgmtWatchXnoded(void *args) {
213✔
355
  TAOS_XNODED_MGMT_CHECK_PTR_RVOID(args);
426✔
356
  SXnodedData *pData = args;
213✔
357
  TAOS_UV_CHECK_ERRNO(uv_loop_init(&pData->loop));
213✔
358
  TAOS_UV_CHECK_ERRNO(uv_async_init(&pData->loop, &pData->stopAsync, xnodeMgmtXnodedStopAsyncCb));
213✔
359
  pData->stopAsync.data = pData;
213✔
360
  TAOS_UV_CHECK_ERRNO(xnodeMgmtSpawnXnoded(pData));
213✔
361
  atomic_store_32(&pData->spawnErr, 0);
×
362
  (void)uv_barrier_wait(&pData->barrier);
×
363
  int32_t num = uv_run(&pData->loop, UV_RUN_DEFAULT);
×
364
  xndInfo("xnoded loop exit with %d active handles, line:%d", num, __LINE__);
×
365

366
  uv_walk(&pData->loop, xnodeMgmtXnodedCloseWalkCb, NULL);
×
367
  num = uv_run(&pData->loop, UV_RUN_DEFAULT);
×
368
  xndInfo("xnoded loop exit with %d active handles, line:%d", num, __LINE__);
×
369
  if (uv_loop_close(&pData->loop) != 0) {
×
370
    xndError("xnoded loop close failed, lino:%d", __LINE__);
×
371
  }
372
  return;
×
373

374
_exit:
213✔
375
  if (terrno != 0) {
213✔
376
    (void)uv_barrier_wait(&pData->barrier);
213✔
377
    atomic_store_32(&pData->spawnErr, terrno);
213✔
378
    if (uv_loop_close(&pData->loop) != 0) {
213✔
379
      xndError("xnoded loop close failed, lino:%d", __LINE__);
213✔
380
    }
381

382
    xndError("xnoded thread exit with code:%d lino:%d", terrno, __LINE__);
213✔
383
    terrno = TSDB_CODE_XNODE_UV_EXEC_FAILURE;
213✔
384
  }
385
}
386

387
/**
388
 * start xnoded that serves xnode function invocation under dnode startDnodeId
389
 * @param startDnodeId
390
 * @return
391
 */
392
int32_t xnodeMgmtStartXnoded(SXnode *pXnode) {
213✔
393
  int32_t code = 0, lino = 0;
213✔
394

395
  SXnodedData *pData = &xnodedGlobal;
213✔
396
  pData->leaderEp = pXnode->ep;
213✔
397
  if (pData->isStarted) {
213✔
398
    xndInfo("dnode start xnoded already called");
×
399
    return 0;
×
400
  }
401
  pData->isStarted = true;
213✔
402
  char dnodeId[8] = {0};
213✔
403
  snprintf(dnodeId, sizeof(dnodeId), "%d", pXnode->dnodeId);
213✔
404
  TAOS_CHECK_GOTO(uv_os_setenv("DNODE_ID", dnodeId), &lino, _exit);
213✔
405
  pData->dnodeId = pXnode->dnodeId;
213✔
406
  pData->clusterId = pXnode->clusterId;
213✔
407
  memset(pData->userPass, 0, sizeof(pData->userPass));
213✔
408
  memcpy(pData->userPass, pXnode->userPass, pXnode->upLen);
213✔
409

410
  TAOS_CHECK_GOTO(uv_barrier_init(&pData->barrier, 2), &lino, _exit);
213✔
411
  TAOS_CHECK_GOTO(uv_thread_create(&pData->thread, xnodeMgmtWatchXnoded, pData), &lino, _exit);
213✔
412
  (void)uv_barrier_wait(&pData->barrier);
213✔
413
  int32_t err = atomic_load_32(&pData->spawnErr);
213✔
414
  if (err != 0) {
213✔
415
    uv_barrier_destroy(&pData->barrier);
213✔
416
    if (uv_async_send(&pData->stopAsync) != 0) {
213✔
417
      xndError("start xnoded: failed to send stop async");
×
418
    }
419
    if (uv_thread_join(&pData->thread) != 0) {
213✔
420
      xndError("start xnoded: failed to join xnoded thread");
×
421
    }
422
    pData->needCleanUp = false;
213✔
423
    xndInfo("xnoded is cleaned up after spawn err");
213✔
424
    TAOS_CHECK_GOTO(err, &lino, _exit);
213✔
425
  } else {
426
    pData->needCleanUp = true;
×
427
    atomic_store_32(&pData->isStopped, 0);
×
428
  }
429
_exit:
213✔
430
  if (code != 0) {
213✔
431
    xndError("xnoded start failed with lino:%d, code:%d, error: %s", code, lino, uv_strerror(code));
213✔
432
  }
433
  return code;
213✔
434
}
435
/**
436
 * stop xnoded
437
 * @return
438
 */
439
void xnodeMgmtStopXnoded(void) {
626,333✔
440
  SXnodedData *pData = &xnodedGlobal;
626,333✔
441
  xndInfo("stopping xnoded, need cleanup:%d, spawn err:%d", pData->needCleanUp, pData->spawnErr);
626,333✔
442
  if (!pData->needCleanUp || atomic_load_32(&pData->isStopped)) {
626,333✔
443
    return;
626,333✔
444
  }
445
  atomic_store_32(&pData->isStopped, 1);
×
446
  pData->needCleanUp = false;
×
447
  (void)uv_process_kill(&pData->process, SIGTERM);
×
448
  uv_barrier_destroy(&pData->barrier);
×
449

450
  if (uv_thread_join(&pData->thread) != 0) {
×
451
    xndError("stop xnoded: failed to join xnoded thread");
×
452
  }
453
  xndInfo("xnoded is cleaned up");
×
454

455
  pData->isStarted = false;
×
456

457
  return;
×
458
}
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