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

taosdata / TDengine / #3653

14 Mar 2025 08:10AM UTC coverage: 22.565% (-41.0%) from 63.596%
#3653

push

travis-ci

web-flow
feat(keep): support keep on super table level. (#30097)

* Feat: support use keep while create super table.

* Test(keep): add test for create super table with keep option.

* Feat(keep): Add tmsg for create keep.

* Feat(keep): support alter table option keep.

* Fix(keep): Add baisc test for alter table option.

* Fix(keep): memory leek.

* Feat(keep): add keep to metaEntry&metaCache and fix earliestTs with stn keep.

* Test(keep): add some cases for select with stb keep.

* Fix: fix ci core while alter stb.

* Feat(keep): delete expired data in super table level.

* Feat: remove get stb keep while query.

* Fix : build error.

* Revert "Fix : build error."

This reverts commit 0ed66e4e8.

* Revert "Feat(keep): delete expired data in super table level."

This reverts commit 36330f6b4.

* Fix : build errors.

* Feat : support restart taosd.

* Fix : alter table comment problems.

* Test : add tests for super table keep.

* Fix: change sdb stb reserve size.

* Test: add more tests.

* Feat: Disable normal tables and sub tables from setting the keep parameter

* Fix: add more checks to avoid unknown address.

* Docs: Add docs for stable keep.

* Fix: some review changes.

* Fix: review errors.

49248 of 302527 branches covered (16.28%)

Branch coverage included in aggregate %.

53 of 99 new or added lines in 12 files covered. (53.54%)

155872 existing lines in 443 files now uncovered.

87359 of 302857 relevant lines covered (28.84%)

570004.22 hits per line

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

57.14
/source/os/src/osSystem.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 ALLOW_FORBID_FUNC
17
#define _DEFAULT_SOURCE
18
#include "os.h"
19

20
#if defined(WINDOWS)
21
typedef void (*MainWindows)(int argc, char** argv);
22
MainWindows mainWindowsFunc = NULL;
23

24
SERVICE_STATUS        ServiceStatus;
25
SERVICE_STATUS_HANDLE hServiceStatusHandle;
26
void WINAPI           windowsServiceCtrlHandle(DWORD request) {
27
            switch (request) {
28
              case SERVICE_CONTROL_STOP:
29
              case SERVICE_CONTROL_SHUTDOWN:
30
      raise(SIGINT);
31
      ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
32
      if (!SetServiceStatus(hServiceStatusHandle, &ServiceStatus)) {
33
                  DWORD nError = GetLastError();
34
                  fprintf(stderr, "failed to send stopped status to windows service: %d", nError);
35
      }
36
      break;
37
              default:
38
      return;
39
  }
40
}
41
void WINAPI mainWindowsService(int argc, char** argv) {
42
  int ret = 0;
43
  ServiceStatus.dwServiceType = SERVICE_WIN32;
44
  ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_PAUSE_CONTINUE | SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
45
  ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
46
  ServiceStatus.dwWin32ExitCode = 0;
47
  ServiceStatus.dwCheckPoint = 0;
48
  ServiceStatus.dwWaitHint = 0;
49
  ServiceStatus.dwServiceSpecificExitCode = 0;
50
  hServiceStatusHandle = RegisterServiceCtrlHandler("taosd", &windowsServiceCtrlHandle);
51
  if (hServiceStatusHandle == 0) {
52
    DWORD nError = GetLastError();
53
    fprintf(stderr, "failed to register windows service ctrl handler: %d", nError);
54
  }
55

56
  ServiceStatus.dwCurrentState = SERVICE_RUNNING;
57
  if (SetServiceStatus(hServiceStatusHandle, &ServiceStatus)) {
58
    DWORD nError = GetLastError();
59
    fprintf(stderr, "failed to send running status to windows service: %d", nError);
60
  }
61
  if (mainWindowsFunc != NULL) mainWindowsFunc(argc, argv);
62
  ServiceStatus.dwCurrentState = SERVICE_STOPPED;
63
  if (!SetServiceStatus(hServiceStatusHandle, &ServiceStatus)) {
64
    DWORD nError = GetLastError();
65
    fprintf(stderr, "failed to send stopped status to windows service: %d", nError);
66
  }
67
}
68
void stratWindowsService(MainWindows mainWindows) {
69
  mainWindowsFunc = mainWindows;
70
  SERVICE_TABLE_ENTRY ServiceTable[2];
71
  ServiceTable[0].lpServiceName = "taosd";
72
  ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)mainWindowsService;
73
  ServiceTable[1].lpServiceName = NULL;
74
  ServiceTable[1].lpServiceProc = NULL;
75
  StartServiceCtrlDispatcher(ServiceTable);
76
}
77

78
#elif defined(_TD_DARWIN_64) || defined(TD_ASTRA)
79
#else
80
#include <dlfcn.h>
81
#include <termios.h>
82
#include <unistd.h>
83
#endif
84

85
#if !defined(WINDOWS) && !defined(TD_ASTRA)
86
struct termios oldtio;
87
#endif
88

89
typedef struct FILE TdCmd;
90

91
#ifdef BUILD_NO_CALL
92
void* taosLoadDll(const char* filename) {
93
#if defined(WINDOWS)
94
  return NULL;
95
#elif defined(_TD_DARWIN_64)
96
  return NULL;
97
#else
98
  void* handle = dlopen(filename, RTLD_LAZY);
99
  if (!handle) {
100
    // printf("load dll:%s failed, error:%s", filename, dlerror());
101
    return NULL;
102
  }
103

104
  // printf("dll %s loaded", filename);
105

106
  return handle;
107
#endif
108
}
109

110
void taosCloseDll(void* handle) {
111
#if defined(WINDOWS)
112
  return;
113
#elif defined(_TD_DARWIN_64)
114
  return;
115
#else
116
  if (handle) {
117
    dlclose(handle);
118
  }
119
#endif
120
}
121
#endif
122

123
int32_t taosSetConsoleEcho(bool on) {
2✔
124
#if defined(WINDOWS)
125
  HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
126
  if (hStdin == INVALID_HANDLE_VALUE) {
127
    terrno = TAOS_SYSTEM_WINAPI_ERROR(GetLastError());
128
    return terrno;
129
  }
130
  DWORD  mode = 0;
131
  if(!GetConsoleMode(hStdin, &mode)){
132
    terrno = TAOS_SYSTEM_WINAPI_ERROR(GetLastError());
133
    return terrno;
134
  }
135
  if (on) {
136
    mode |= ENABLE_ECHO_INPUT;
137
  } else {
138
    mode &= ~ENABLE_ECHO_INPUT;
139
  }
140
  if(!SetConsoleMode(hStdin, mode)) {
141
    terrno = TAOS_SYSTEM_WINAPI_ERROR(GetLastError());
142
    return terrno;
143
  }
144

145
  return 0;
146
#elif defined(TD_ASTRA) // TD_ASTRA_TODO
147
  return 0;
148
#else
149
#define ECHOFLAGS (ECHO | ECHOE | ECHOK | ECHONL)
150
  int            err;
151
  struct termios term;
152

153
  if (tcgetattr(STDIN_FILENO, &term) == -1) {
2!
154
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
2✔
155
    return terrno;
2✔
156
  }
157

UNCOV
158
  if (on)
×
UNCOV
159
    term.c_lflag |= ECHOFLAGS;
×
160
  else
UNCOV
161
    term.c_lflag &= ~ECHOFLAGS;
×
162

163
  err = tcsetattr(STDIN_FILENO, TCSAFLUSH, &term);
×
164
  if (err == -1) {
×
UNCOV
165
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
UNCOV
166
    return terrno;
×
167
  }
168

UNCOV
169
  return 0;
×
170
#endif
171
}
172

UNCOV
173
int32_t taosSetTerminalMode() {
×
174
#if defined(WINDOWS) || defined(TD_ASTRA) // TD_ASTRA_TODO
175
  return 0;
176
#else
177
  struct termios newtio;
178

179
  /* if (atexit() != 0) { */
180
  /*     fprintf(stderr, "Error register exit function!\n"); */
181
  /*     exit(EXIT_FAILURE); */
182
  /* } */
183

UNCOV
184
  (void)memcpy(&newtio, &oldtio, sizeof(oldtio));
×
185

186
  // Set new terminal attributes.
UNCOV
187
  newtio.c_iflag &= ~(IXON | IXOFF | ICRNL | INLCR | IGNCR | IMAXBEL | ISTRIP);
×
UNCOV
188
  newtio.c_iflag |= IGNBRK;
×
189

190
  // newtio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
UNCOV
191
  newtio.c_oflag |= OPOST;
×
UNCOV
192
  newtio.c_oflag |= ONLCR;
×
UNCOV
193
  newtio.c_oflag &= ~(OCRNL | ONLRET);
×
194

UNCOV
195
  newtio.c_lflag &= ~(IEXTEN | ICANON | ECHO | ECHOE | ECHONL | ECHOCTL | ECHOPRT | ECHOKE | ISIG);
×
UNCOV
196
  newtio.c_cc[VMIN] = 1;
×
UNCOV
197
  newtio.c_cc[VTIME] = 0;
×
198

199
  if (-1 == tcsetattr(0, TCSANOW, &newtio)) {
×
200
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
UNCOV
201
    (void)fprintf(stderr, "Fail to set terminal properties!\n");
×
UNCOV
202
    return terrno;
×
203
  }
204

UNCOV
205
  return 0;
×
206
#endif
207
}
208

209
int32_t taosGetOldTerminalMode() {
1✔
210
#if defined(WINDOWS) || defined(TD_ASTRA) // TD_ASTRA_TODO
211
  return 0;
212
#else
213
  /* Make sure stdin is a terminal. */
214
  if (!isatty(STDIN_FILENO)) {
1!
215
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
1✔
216
    return terrno;
1✔
217
  }
218

219
  // Get the parameter of current terminal
UNCOV
220
  if (-1 == tcgetattr(0, &oldtio)) {
×
UNCOV
221
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
UNCOV
222
    return terrno;
×
223
  }
224

UNCOV
225
  return 0;
×
226
#endif
227
}
228

UNCOV
229
int32_t taosResetTerminalMode() {
×
230
#if defined(WINDOWS) || defined(TD_ASTRA) // TD_ASTRA_TODO
231
  return 0;
232
#else
UNCOV
233
  if (-1 == tcsetattr(0, TCSANOW, &oldtio)) {
×
UNCOV
234
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
UNCOV
235
    (void)fprintf(stderr, "Fail to reset the terminal properties!\n");
×
UNCOV
236
    return terrno;
×
237
  }
238
#endif
UNCOV
239
  return 0;
×
240
}
241

242
TdCmdPtr taosOpenCmd(const char* cmd) {
21✔
243
  if (cmd == NULL) {
21✔
244
    terrno = TSDB_CODE_INVALID_PARA;
1✔
245
    return NULL;
1✔
246
  }
247
  
248
#ifdef WINDOWS
249
  return (TdCmdPtr)_popen(cmd, "r");
250
#elif defined(TD_ASTRA)
251
  return NULL;
252
#else
253
  TdCmdPtr p = (TdCmdPtr)popen(cmd, "r");
20✔
254
  if (NULL == p) {
20!
UNCOV
255
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
256
  }
257
  return p;
20✔
258
#endif
259
}
260

261
int64_t taosGetsCmd(TdCmdPtr pCmd, int32_t maxSize, char* __restrict buf) {
4✔
262
  if (pCmd == NULL || buf == NULL) {
4✔
263
    terrno = TSDB_CODE_INVALID_PARA;
2✔
264
    return terrno;
2✔
265
  }
266
  if (fgets(buf, maxSize, (FILE*)pCmd) == NULL) {
2✔
267
    if (feof((FILE*)pCmd)) {
1!
268
      return 0;
1✔
269
    }
270

UNCOV
271
    terrno = TAOS_SYSTEM_ERROR(ferror((FILE*)pCmd));
×
UNCOV
272
    return terrno;
×
273
  }
274
  
275
  return strlen(buf);
1✔
276
}
277

278
int64_t taosGetLineCmd(TdCmdPtr pCmd, char** __restrict ptrBuf) {
20✔
279
  if (pCmd == NULL || ptrBuf == NULL) {
20!
280
    terrno = TSDB_CODE_INVALID_PARA;
2✔
281
    return terrno;
2✔
282
  }
283
  if (*ptrBuf != NULL) {
18✔
284
    taosMemoryFreeClear(*ptrBuf);
1!
285
  }
286
  
287
#ifdef WINDOWS
288
  *ptrBuf = taosMemoryMalloc(1024);
289
  if (*ptrBuf == NULL) return -1;
290
  if (fgets(*ptrBuf, 1023, (FILE*)pCmd) == NULL) {
291
    taosMemoryFreeClear(*ptrBuf);
292
    return -1;
293
  }
294
  (*ptrBuf)[1023] = 0;
295
  return strlen(*ptrBuf);
296
#elif defined(TD_ASTRA)
297
  size_t bufsize = 128;
298
  size_t pos = 0;
299
  int    c;
300
  if (*ptrBuf == NULL) {
301
    *ptrBuf = (char*)taosMemoryMalloc(bufsize);
302
    if (*ptrBuf == NULL) {
303
      return terrno;
304
    }
305
  }
306
  while ((c = fgetc((FILE*)pCmd)) != EOF) {
307
    if (pos + 1 >= bufsize) {
308
      size_t new_size = bufsize << 1;
309
      char*  new_line = (char*)taosMemoryRealloc(*ptrBuf, new_size);
310
      if (new_line == NULL) {
311
        return terrno;
312
      }
313
      *ptrBuf = new_line;
314
      bufsize = new_size;
315
    }
316
    (*ptrBuf)[pos++] = (char)c;
317
    if (c == '\n') {
318
      break;
319
    }
320
  }
321
  if (pos == 0 && c == EOF) {
322
    return TSDB_CODE_INVALID_PARA;
323
  }
324
  (*ptrBuf)[pos] = '\0';
325
  return (ssize_t)pos;
326
#else
327
  ssize_t len = 0;
18✔
328
  len = getline(ptrBuf, (size_t*)&len, (FILE*)pCmd);
18✔
329
  if (-1 == len) {
18✔
330
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
17✔
331
    return terrno;
17✔
332
  }
333
  return len;
1✔
334
#endif
335
}
336

337
int32_t taosEOFCmd(TdCmdPtr pCmd) {
17✔
338
  if (pCmd == NULL) {
17✔
339
    return 0;
1✔
340
  }
341
  return feof((FILE*)pCmd);
16✔
342
}
343

344
void taosCloseCmd(TdCmdPtr* ppCmd) {
22✔
345
  if (ppCmd == NULL || *ppCmd == NULL) {
22✔
346
    return;
2✔
347
  }
348
#ifdef WINDOWS
349
  _pclose((FILE*)(*ppCmd));
350
#elif defined(TD_ASTRA) // TD_ASTRA_TODO
351
#else
352
  (void)pclose((FILE*)(*ppCmd));
20✔
353
#endif
354
  *ppCmd = NULL;
20✔
355
}
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