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

taosdata / TDengine / #4936

23 Jan 2026 09:40AM UTC coverage: 66.746% (+0.04%) from 66.708%
#4936

push

travis-ci

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

204023 of 305671 relevant lines covered (66.75%)

124768167.97 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) {
412✔
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);
412✔
69
#endif
70
  xndDebug("xnode get xnoded pid path:%s", pipeName);
412✔
71
}
412✔
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() {
412✔
106
  char buf[PATH_MAX] = {0};
412✔
107
  getXnodedPidPath(buf, sizeof(buf));
412✔
108

109
  TdFilePtr pFile = NULL;
412✔
110
  pFile = taosOpenFile(buf, TD_FILE_READ);
412✔
111
  if (pFile == NULL) {
412✔
112
    xndWarn("xnode failed to open xnoded pid file:%s, file may not exist", buf);
412✔
113
    return;
412✔
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) {
412✔
145
  if (tsProcPath == NULL) {
412✔
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);
412✔
155
  }
156

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

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

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

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

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

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

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

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

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

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

217
  options.exit_cb = xnodeMgmtXnodedExit;
412✔
218

219
  killPreXnoded();
412✔
220

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

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

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

238
  options.flags = UV_PROCESS_DETACHED;
412✔
239

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

253
  char xnodedLogLevel[32] = {0};
412✔
254
  if (xndDebugFlag & DEBUG_INFO) {
412✔
255
    snprintf(xnodedLogLevel, 32, "%s=%s", "XNODED_LOG_LEVEL", "info");
412✔
256
  }
257
  if (xndDebugFlag & DEBUG_DEBUG) {
412✔
258
    snprintf(xnodedLogLevel, 32, "%s=%s", "XNODED_LOG_LEVEL", "debug");
×
259
  }
260
  if (xndDebugFlag & DEBUG_TRACE) {
412✔
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,
412✔
265
           xnodePipeSocket, xnodedLogLevel);
266

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

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

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

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

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

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

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

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

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

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

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

338
  return err;
412✔
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) {
206✔
355
  TAOS_XNODED_MGMT_CHECK_PTR_RVOID(args);
412✔
356
  SXnodedData *pData = args;
206✔
357
  TAOS_UV_CHECK_ERRNO(uv_loop_init(&pData->loop));
206✔
358
  TAOS_UV_CHECK_ERRNO(uv_async_init(&pData->loop, &pData->stopAsync, xnodeMgmtXnodedStopAsyncCb));
206✔
359
  pData->stopAsync.data = pData;
206✔
360
  TAOS_UV_CHECK_ERRNO(xnodeMgmtSpawnXnoded(pData));
206✔
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:
206✔
375
  if (terrno != 0) {
206✔
376
    (void)uv_barrier_wait(&pData->barrier);
206✔
377
    atomic_store_32(&pData->spawnErr, terrno);
206✔
378
    if (uv_loop_close(&pData->loop) != 0) {
206✔
379
      xndError("xnoded loop close failed, lino:%d", __LINE__);
206✔
380
    }
381

382
    xndError("xnoded thread exit with code:%d lino:%d", terrno, __LINE__);
206✔
383
    terrno = TSDB_CODE_XNODE_UV_EXEC_FAILURE;
206✔
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) {
206✔
393
  int32_t code = 0, lino = 0;
206✔
394

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

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