cqMain.c 10.3 KB
Newer Older
J
draft  
jtao1735 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#define _DEFAULT_SOURCE
J
jtao1735 已提交
17

H
TD-354  
Hongze Cheng 已提交
18 19
#include <errno.h>
#include <pthread.h>
J
jtao1735 已提交
20 21
#include <stdlib.h>
#include <string.h>
H
TD-354  
Hongze Cheng 已提交
22 23

#include "taos.h"
24
#include "tsclient.h"
J
jtao1735 已提交
25
#include "taosdef.h"
J
draft  
jtao1735 已提交
26
#include "taosmsg.h"
B
Bomin Zhang 已提交
27
#include "ttimer.h"
H
TD-354  
Hongze Cheng 已提交
28 29
#include "tcq.h"
#include "tdataformat.h"
J
jtao1735 已提交
30
#include "tglobal.h"
J
jtao1735 已提交
31 32 33
#include "tlog.h"
#include "twal.h"

S
TD-1520  
Shengliang Guan 已提交
34 35 36 37 38
#define cFatal(...) { if (cqDebugFlag & DEBUG_FATAL) { taosPrintLog("CQ  FATAL ", 255, __VA_ARGS__); }}
#define cError(...) { if (cqDebugFlag & DEBUG_ERROR) { taosPrintLog("CQ  ERROR ", 255, __VA_ARGS__); }}
#define cWarn(...)  { if (cqDebugFlag & DEBUG_WARN)  { taosPrintLog("CQ  WARN ", 255, __VA_ARGS__); }}
#define cInfo(...)  { if (cqDebugFlag & DEBUG_INFO)  { taosPrintLog("CQ  ", 255, __VA_ARGS__); }}
#define cDebug(...) { if (cqDebugFlag & DEBUG_DEBUG) { taosPrintLog("CQ  ", cqDebugFlag, __VA_ARGS__); }}
S
Shengliang Guan 已提交
39
#define cTrace(...) { if (cqDebugFlag & DEBUG_TRACE) { taosPrintLog("CQ  ", cqDebugFlag, __VA_ARGS__); }}
J
jtao1735 已提交
40 41

typedef struct {
S
Shengliang Guan 已提交
42
  int32_t  vgId;
S
TD-2283  
Shengliang Guan 已提交
43 44
  int32_t  master;
  int32_t  num;      // number of continuous streams
J
jtao1735 已提交
45
  char     user[TSDB_USER_LEN];
46
  char     pass[TSDB_KEY_LEN];
B
Bomin Zhang 已提交
47
  char     db[TSDB_DB_NAME_LEN];
J
jtao1735 已提交
48 49 50
  FCqWrite cqWrite;
  struct SCqObj *pHead;
  void    *dbConn;
B
Bomin Zhang 已提交
51
  void    *tmrCtrl;
J
jtao1735 已提交
52 53 54 55
  pthread_mutex_t mutex;
} SCqContext;

typedef struct SCqObj {
B
Bomin Zhang 已提交
56
  tmr_h          tmrId;
B
Bomin Zhang 已提交
57 58
  uint64_t       uid;
  int32_t        tid;      // table ID
S
Shengliang Guan 已提交
59
  int32_t        rowSize;  // bytes of a row
60
  char *         dstTable;
H
TD-354  
Hongze Cheng 已提交
61 62 63 64 65 66
  char *         sqlStr;   // SQL string
  STSchema *     pSchema;  // pointer to schema array
  void *         pStream;
  struct SCqObj *prev;
  struct SCqObj *next;
  SCqContext *   pContext;
J
jtao1735 已提交
67 68 69
} SCqObj;

static void cqProcessStreamRes(void *param, TAOS_RES *tres, TAOS_ROW row); 
70
static void cqCreateStream(SCqContext *pContext, SCqObj *pObj);
J
jtao1735 已提交
71 72

void *cqOpen(void *ahandle, const SCqCfg *pCfg) {
73 74 75
  if (tsEnableStream == 0) {
    return NULL;
  }
J
jtao1735 已提交
76
  SCqContext *pContext = calloc(sizeof(SCqContext), 1);
77 78 79 80
  if (pContext == NULL) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return NULL;
  }
J
jtao1735 已提交
81

B
Bomin Zhang 已提交
82 83
  pContext->tmrCtrl = taosTmrInit(0, 0, 0, "CQ");

B
Bomin Zhang 已提交
84 85
  tstrncpy(pContext->user, pCfg->user, sizeof(pContext->user));
  tstrncpy(pContext->pass, pCfg->pass, sizeof(pContext->pass));
B
Bomin Zhang 已提交
86 87 88 89 90 91 92
  const char* db = pCfg->db;
  for (const char* p = db; *p != 0; p++) {
    if (*p == '.') {
      db = p + 1;
      break;
    }
  }
B
Bomin Zhang 已提交
93
  tstrncpy(pContext->db, db, sizeof(pContext->db));
J
jtao1735 已提交
94 95
  pContext->vgId = pCfg->vgId;
  pContext->cqWrite = pCfg->cqWrite;
96
  tscEmbedded = 1;
J
jtao1735 已提交
97 98

  pthread_mutex_init(&pContext->mutex, NULL);
J
draft  
jtao1735 已提交
99

Z
zyyang 已提交
100
  cDebug("vgId:%d, CQ is opened", pContext->vgId);
J
draft  
jtao1735 已提交
101

J
jtao1735 已提交
102 103
  return pContext;
}
J
draft  
jtao1735 已提交
104

J
jtao1735 已提交
105
void cqClose(void *handle) {
106 107 108
  if (tsEnableStream == 0) {
    return;
  }
J
jtao1735 已提交
109
  SCqContext *pContext = handle;
110
  if (handle == NULL) return;
J
draft  
jtao1735 已提交
111

J
jtao1735 已提交
112 113
  // stop all CQs
  cqStop(pContext);
J
draft  
jtao1735 已提交
114

J
jtao1735 已提交
115
  // free all resources
116 117
  pthread_mutex_lock(&pContext->mutex);

J
jtao1735 已提交
118 119 120 121
  SCqObj *pObj = pContext->pHead;
  while (pObj) {
    SCqObj *pTemp = pObj;
    pObj = pObj->next;
B
Bomin Zhang 已提交
122
    tdFreeSchema(pTemp->pSchema);
S
TD-1848  
Shengliang Guan 已提交
123
    tfree(pTemp->sqlStr);
J
jtao1735 已提交
124 125 126
    free(pTemp);
  } 
  
127 128
  pthread_mutex_unlock(&pContext->mutex);

J
jtao1735 已提交
129 130
  pthread_mutex_destroy(&pContext->mutex);

B
Bomin Zhang 已提交
131 132 133
  taosTmrCleanUp(pContext->tmrCtrl);
  pContext->tmrCtrl = NULL;

Z
zyyang 已提交
134
  cDebug("vgId:%d, CQ is closed", pContext->vgId);
J
jtao1735 已提交
135
  free(pContext);
J
draft  
jtao1735 已提交
136 137
}

J
jtao1735 已提交
138
void cqStart(void *handle) {
139 140 141
  if (tsEnableStream == 0) {
    return;
  }
J
jtao1735 已提交
142
  SCqContext *pContext = handle;
143
  if (pContext->dbConn || pContext->master) return;
J
jtao1735 已提交
144

Z
zyyang 已提交
145
  cDebug("vgId:%d, start all CQs", pContext->vgId);
J
jtao1735 已提交
146 147
  pthread_mutex_lock(&pContext->mutex);

148
  pContext->master = 1;
J
jtao1735 已提交
149 150 151

  SCqObj *pObj = pContext->pHead;
  while (pObj) {
152
    cqCreateStream(pContext, pObj);
J
jtao1735 已提交
153
    pObj = pObj->next;
J
draft  
jtao1735 已提交
154
  }
J
jtao1735 已提交
155 156

  pthread_mutex_unlock(&pContext->mutex);
J
draft  
jtao1735 已提交
157 158
}

J
jtao1735 已提交
159
void cqStop(void *handle) {
160 161 162
  if (tsEnableStream == 0) {
    return;
  }
J
jtao1735 已提交
163
  SCqContext *pContext = handle;
S
TD-2321  
Shengliang Guan 已提交
164
  cDebug("vgId:%d, stop all CQs", pContext->vgId);
165
  if (pContext->dbConn == NULL || pContext->master == 0) return;
J
draft  
jtao1735 已提交
166

J
jtao1735 已提交
167
  pthread_mutex_lock(&pContext->mutex);
J
draft  
jtao1735 已提交
168

169
  pContext->master = 0;
J
jtao1735 已提交
170 171
  SCqObj *pObj = pContext->pHead;
  while (pObj) {
J
jtao1735 已提交
172 173 174
    if (pObj->pStream) {
      taos_close_stream(pObj->pStream);
      pObj->pStream = NULL;
S
TD-1520  
Shengliang Guan 已提交
175
      cInfo("vgId:%d, id:%d CQ:%s is closed", pContext->vgId, pObj->tid, pObj->sqlStr);
B
Bomin Zhang 已提交
176 177 178
    } else {
      taosTmrStop(pObj->tmrId);
      pObj->tmrId = 0;
J
jtao1735 已提交
179
    }
J
jtao1735 已提交
180
    pObj = pObj->next;
J
draft  
jtao1735 已提交
181
  }
J
jtao1735 已提交
182 183 184 185 186

  if (pContext->dbConn) taos_close(pContext->dbConn);
  pContext->dbConn = NULL;

  pthread_mutex_unlock(&pContext->mutex);
J
draft  
jtao1735 已提交
187 188
}

189
void *cqCreate(void *handle, uint64_t uid, int32_t sid, const char* dstTable, char *sqlStr, STSchema *pSchema) {
190 191 192
  if (tsEnableStream == 0) {
    return NULL;
  }
J
jtao1735 已提交
193
  SCqContext *pContext = handle;
J
draft  
jtao1735 已提交
194

J
jtao1735 已提交
195
  SCqObj *pObj = calloc(sizeof(SCqObj), 1);
J
jtao1735 已提交
196
  if (pObj == NULL) return NULL;
J
draft  
jtao1735 已提交
197

B
Bomin Zhang 已提交
198
  pObj->uid = uid;
199 200 201 202 203
  pObj->tid = sid;
  if (dstTable != NULL) {
    pObj->dstTable = strdup(dstTable);
  }
  pObj->sqlStr = strdup(sqlStr);
J
draft  
jtao1735 已提交
204

H
TD-354  
Hongze Cheng 已提交
205
  pObj->pSchema = tdDupSchema(pSchema);
B
Bomin Zhang 已提交
206
  pObj->rowSize = schemaTLen(pSchema);
J
draft  
jtao1735 已提交
207

S
TD-1520  
Shengliang Guan 已提交
208
  cInfo("vgId:%d, id:%d CQ:%s is created", pContext->vgId, pObj->tid, pObj->sqlStr);
J
draft  
jtao1735 已提交
209

J
jtao1735 已提交
210
  pthread_mutex_lock(&pContext->mutex);
J
draft  
jtao1735 已提交
211

J
jtao1735 已提交
212
  pObj->next = pContext->pHead;
J
jtao1735 已提交
213
  if (pContext->pHead) pContext->pHead->prev = pObj;
J
jtao1735 已提交
214
  pContext->pHead = pObj;
J
draft  
jtao1735 已提交
215

216
  cqCreateStream(pContext, pObj);
J
draft  
jtao1735 已提交
217

J
jtao1735 已提交
218
  pthread_mutex_unlock(&pContext->mutex);
J
jtao1735 已提交
219 220

  return pObj;
J
jtao1735 已提交
221 222
}

J
jtao1735 已提交
223
void cqDrop(void *handle) {
224 225 226
  if (tsEnableStream == 0) {
    return;
  }
J
jtao1735 已提交
227 228
  SCqObj *pObj = handle;
  SCqContext *pContext = pObj->pContext;
J
jtao1735 已提交
229 230 231

  pthread_mutex_lock(&pContext->mutex);

J
jtao1735 已提交
232 233 234 235 236
  if (pObj->prev) {
    pObj->prev->next = pObj->next;
  } else {
    pContext->pHead = pObj->next;
  }
J
jtao1735 已提交
237

J
jtao1735 已提交
238 239
  if (pObj->next) {
    pObj->next->prev = pObj->prev;
J
draft  
jtao1735 已提交
240 241
  }

J
jtao1735 已提交
242
  // free the resources associated
B
Bomin Zhang 已提交
243 244 245 246 247 248 249
  if (pObj->pStream) {
    taos_close_stream(pObj->pStream);
    pObj->pStream = NULL;
  } else {
    taosTmrStop(pObj->tmrId);
    pObj->tmrId = 0;
  }
J
draft  
jtao1735 已提交
250

S
TD-1520  
Shengliang Guan 已提交
251
  cInfo("vgId:%d, id:%d CQ:%s is dropped", pContext->vgId, pObj->tid, pObj->sqlStr); 
252
  tdFreeSchema(pObj->pSchema);
253
  free(pObj->dstTable);
254
  free(pObj->sqlStr);
J
jtao1735 已提交
255
  free(pObj);
J
draft  
jtao1735 已提交
256

B
Bomin Zhang 已提交
257
  pthread_mutex_unlock(&pContext->mutex);
J
draft  
jtao1735 已提交
258 259
}

S
Shengliang Guan 已提交
260
static void doCreateStream(void *param, TAOS_RES *result, int32_t code) {
261 262 263
  SCqObj* pObj = (SCqObj*)param;
  SCqContext* pContext = pObj->pContext;
  SSqlObj* pSql = (SSqlObj*)result;
264 265 266 267
  if (atomic_val_compare_exchange_ptr(&(pContext->dbConn), NULL, pSql->pTscObj) != NULL) {
    taos_close(pSql->pTscObj);
  }
  pthread_mutex_lock(&pContext->mutex);
268
  cqCreateStream(pContext, pObj);
269
  pthread_mutex_unlock(&pContext->mutex);
270 271
}

B
Bomin Zhang 已提交
272 273 274 275
static void cqProcessCreateTimer(void *param, void *tmrId) {
  SCqObj* pObj = (SCqObj*)param;
  SCqContext* pContext = pObj->pContext;

276
  if (pContext->dbConn == NULL) {
S
TD-1520  
Shengliang Guan 已提交
277
    cDebug("vgId:%d, try connect to TDengine", pContext->vgId);
278 279
    taos_connect_a(NULL, pContext->user, pContext->pass, pContext->db, 0, doCreateStream, param, NULL);
  } else {
280
    pthread_mutex_lock(&pContext->mutex);
281
    cqCreateStream(pContext, pObj);
282
    pthread_mutex_unlock(&pContext->mutex);
283
  }
B
Bomin Zhang 已提交
284
}
285

B
Bomin Zhang 已提交
286
static void cqCreateStream(SCqContext *pContext, SCqObj *pObj) {
B
Bomin Zhang 已提交
287
  pObj->pContext = pContext;
B
Bomin Zhang 已提交
288 289

  if (pContext->dbConn == NULL) {
S
TD-1520  
Shengliang Guan 已提交
290
    cDebug("vgId:%d, create dbConn after 1000 ms", pContext->vgId);
B
Bomin Zhang 已提交
291 292 293 294 295
    pObj->tmrId = taosTmrStart(cqProcessCreateTimer, 1000, pObj, pContext->tmrCtrl);
    return;
  }
  pObj->tmrId = 0;

296 297
  if (pObj->pStream == NULL) {
    pObj->pStream = taos_open_stream(pContext->dbConn, pObj->sqlStr, cqProcessStreamRes, 0, pObj, NULL);
298 299

    // TODO the pObj->pStream may be released if error happens
300
    if (pObj->pStream) {
301
      tscSetStreamDestTable(pObj->pStream, pObj->dstTable);
302
      pContext->num++;
S
TD-1843  
Shengliang Guan 已提交
303
      cDebug("vgId:%d, id:%d CQ:%s is opened", pContext->vgId, pObj->tid, pObj->sqlStr);
304 305 306
    } else {
      cError("vgId:%d, id:%d CQ:%s, failed to open", pContext->vgId, pObj->tid, pObj->sqlStr);
    }
307 308 309
  }
}

J
jtao1735 已提交
310
static void cqProcessStreamRes(void *param, TAOS_RES *tres, TAOS_ROW row) {
311
  SCqObj *pObj = (SCqObj *)param;
B
Bomin Zhang 已提交
312
  if (tres == NULL && row == NULL) {
313 314
    taos_close_stream(pObj->pStream);

B
Bomin Zhang 已提交
315 316 317
    pObj->pStream = NULL;
    return;
  }
318

J
jtao1735 已提交
319
  SCqContext *pContext = pObj->pContext;
B
Bomin Zhang 已提交
320
  STSchema   *pSchema = pObj->pSchema;
J
jtao1735 已提交
321
  if (pObj->pStream == NULL) return;
J
draft  
jtao1735 已提交
322

S
TD-1520  
Shengliang Guan 已提交
323
  cDebug("vgId:%d, id:%d CQ:%s stream result is ready", pContext->vgId, pObj->tid, pObj->sqlStr);
J
jtao1735 已提交
324

S
Shengliang Guan 已提交
325
  int32_t size = sizeof(SWalHead) + sizeof(SSubmitMsg) + sizeof(SSubmitBlk) + TD_DATA_ROW_HEAD_SIZE + pObj->rowSize;
J
jtao1735 已提交
326 327 328
  char *buffer = calloc(size, 1);

  SWalHead   *pHead = (SWalHead *)buffer;
B
Bomin Zhang 已提交
329 330
  SSubmitMsg *pMsg = (SSubmitMsg *) (buffer + sizeof(SWalHead));
  SSubmitBlk *pBlk = (SSubmitBlk *) (buffer + sizeof(SWalHead) + sizeof(SSubmitMsg));
J
draft  
jtao1735 已提交
331

B
Bomin Zhang 已提交
332
  SDataRow trow = (SDataRow)pBlk->data;
333
  tdInitDataRow(trow, pSchema);
J
jtao1735 已提交
334

B
Bomin Zhang 已提交
335
  for (int32_t i = 0; i < pSchema->numOfCols; i++) {
336
    STColumn *c = pSchema->columns + i;
337 338 339
    void* val = row[i];
    if (val == NULL) {
      val = getNullValue(c->type);
B
Bomin Zhang 已提交
340
    } else if (c->type == TSDB_DATA_TYPE_BINARY) {
341
      val = ((char*)val) - sizeof(VarDataLenT);
B
Bomin Zhang 已提交
342 343
    } else if (c->type == TSDB_DATA_TYPE_NCHAR) {
      char buf[TSDB_MAX_NCHAR_LEN];
344
      int32_t len = taos_fetch_lengths(tres)[i];
B
Bomin Zhang 已提交
345 346 347
      taosMbsToUcs4(val, len, buf, sizeof(buf), &len);
      memcpy(val + sizeof(VarDataLenT), buf, len);
      varDataLen(val) = len;
348 349
    }
    tdAppendColVal(trow, val, c->type, c->bytes, c->offset);
B
Bomin Zhang 已提交
350
  }
H
Haojun Liao 已提交
351 352
  pBlk->dataLen = htonl(dataRowLen(trow));
  pBlk->schemaLen = 0;
B
Bomin Zhang 已提交
353 354 355 356 357 358 359

  pBlk->uid = htobe64(pObj->uid);
  pBlk->tid = htonl(pObj->tid);
  pBlk->numOfRows = htons(1);
  pBlk->sversion = htonl(pSchema->version);
  pBlk->padding = 0;

B
Bomin Zhang 已提交
360 361
  pHead->len = sizeof(SSubmitMsg) + sizeof(SSubmitBlk) + dataRowLen(trow);

B
Bomin Zhang 已提交
362
  pMsg->header.vgId = htonl(pContext->vgId);
B
Bomin Zhang 已提交
363
  pMsg->header.contLen = htonl(pHead->len);
B
Bomin Zhang 已提交
364 365 366 367 368
  pMsg->length = pMsg->header.contLen;
  pMsg->numOfBlocks = htonl(1);

  pHead->msgType = TSDB_MSG_TYPE_SUBMIT;
  pHead->version = 0;
J
jtao1735 已提交
369 370

  // write into vnode write queue
S
TD-2283  
Shengliang Guan 已提交
371
  pContext->cqWrite(pContext->vgId, pHead, TAOS_QTYPE_CQ, NULL);
B
Bomin Zhang 已提交
372
  free(buffer);
J
jtao1735 已提交
373 374
}