cqMain.c 9.0 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"
J
jtao1735 已提交
24
#include "taosdef.h"
J
draft  
jtao1735 已提交
25
#include "taosmsg.h"
B
Bomin Zhang 已提交
26
#include "ttimer.h"
H
TD-354  
Hongze Cheng 已提交
27 28
#include "tcq.h"
#include "tdataformat.h"
J
jtao1735 已提交
29
#include "tglobal.h"
J
jtao1735 已提交
30 31 32
#include "tlog.h"
#include "twal.h"

S
Shengliang Guan 已提交
33 34 35 36
#define cError(...) { if (cqDebugFlag & DEBUG_ERROR) { taosPrintLog("ERROR CQ  ", cqDebugFlag, __VA_ARGS__); }}
#define cWarn(...)  { if (cqDebugFlag & DEBUG_WARN)  { taosPrintLog("WARN CQ  ", cqDebugFlag, __VA_ARGS__); }}
#define cTrace(...) { if (cqDebugFlag & DEBUG_TRACE) { taosPrintLog("CQ  ", cqDebugFlag, __VA_ARGS__); }}
#define cPrint(...) { taosPrintLog("CQ  ", 255, __VA_ARGS__); }
J
jtao1735 已提交
37 38 39 40 41

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

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

int cqDebugFlag = 135;

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

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

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

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

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

J
jtao1735 已提交
97
  cTrace("vgId:%d, CQ is opened", pContext->vgId);
J
draft  
jtao1735 已提交
98

J
jtao1735 已提交
99 100
  return pContext;
}
J
draft  
jtao1735 已提交
101

J
jtao1735 已提交
102 103
void cqClose(void *handle) {
  SCqContext *pContext = handle;
104
  if (handle == NULL) return;
J
draft  
jtao1735 已提交
105

B
Bomin Zhang 已提交
106 107 108
  taosTmrCleanUp(pContext->tmrCtrl);
  pContext->tmrCtrl = NULL;

J
jtao1735 已提交
109 110
  // stop all CQs
  cqStop(pContext);
J
draft  
jtao1735 已提交
111

J
jtao1735 已提交
112
  // free all resources
113 114
  pthread_mutex_lock(&pContext->mutex);

J
jtao1735 已提交
115 116 117 118
  SCqObj *pObj = pContext->pHead;
  while (pObj) {
    SCqObj *pTemp = pObj;
    pObj = pObj->next;
B
Bomin Zhang 已提交
119 120
    tdFreeSchema(pTemp->pSchema);
    tfree(pTemp->sqlStr);
J
jtao1735 已提交
121 122 123
    free(pTemp);
  } 
  
124 125
  pthread_mutex_unlock(&pContext->mutex);

J
jtao1735 已提交
126 127 128 129
  pthread_mutex_destroy(&pContext->mutex);

  cTrace("vgId:%d, CQ is closed", pContext->vgId);
  free(pContext);
J
draft  
jtao1735 已提交
130 131
}

J
jtao1735 已提交
132 133
void cqStart(void *handle) {
  SCqContext *pContext = handle;
134
  if (pContext->dbConn || pContext->master) return;
J
jtao1735 已提交
135

136
  cTrace("vgId:%d, start all CQs", pContext->vgId);
J
jtao1735 已提交
137 138
  pthread_mutex_lock(&pContext->mutex);

139
  pContext->master = 1;
J
jtao1735 已提交
140 141 142

  SCqObj *pObj = pContext->pHead;
  while (pObj) {
143
    cqCreateStream(pContext, pObj);
J
jtao1735 已提交
144
    pObj = pObj->next;
J
draft  
jtao1735 已提交
145
  }
J
jtao1735 已提交
146 147

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

J
jtao1735 已提交
150 151 152
void cqStop(void *handle) {
  SCqContext *pContext = handle;
  cTrace("vgId:%d, stop all CQs", pContext->vgId);
153
  if (pContext->dbConn == NULL || pContext->master == 0) return;
J
draft  
jtao1735 已提交
154

J
jtao1735 已提交
155
  pthread_mutex_lock(&pContext->mutex);
J
draft  
jtao1735 已提交
156

157
  pContext->master = 0;
J
jtao1735 已提交
158 159
  SCqObj *pObj = pContext->pHead;
  while (pObj) {
J
jtao1735 已提交
160 161 162 163
    if (pObj->pStream) {
      taos_close_stream(pObj->pStream);
      pObj->pStream = NULL;
      cTrace("vgId:%d, id:%d CQ:%s is closed", pContext->vgId, pObj->tid, pObj->sqlStr);
B
Bomin Zhang 已提交
164 165 166
    } else {
      taosTmrStop(pObj->tmrId);
      pObj->tmrId = 0;
J
jtao1735 已提交
167
    }
J
jtao1735 已提交
168
    pObj = pObj->next;
J
draft  
jtao1735 已提交
169
  }
J
jtao1735 已提交
170 171 172 173 174

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

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

B
Bomin Zhang 已提交
177
void *cqCreate(void *handle, uint64_t uid, int tid, char *sqlStr, STSchema *pSchema) {
J
jtao1735 已提交
178
  SCqContext *pContext = handle;
J
draft  
jtao1735 已提交
179

J
jtao1735 已提交
180
  SCqObj *pObj = calloc(sizeof(SCqObj), 1);
J
jtao1735 已提交
181
  if (pObj == NULL) return NULL;
J
draft  
jtao1735 已提交
182

B
Bomin Zhang 已提交
183
  pObj->uid = uid;
J
jtao1735 已提交
184
  pObj->tid = tid;
J
jtao1735 已提交
185 186
  pObj->sqlStr = malloc(strlen(sqlStr)+1);
  strcpy(pObj->sqlStr, sqlStr);
J
draft  
jtao1735 已提交
187

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

J
jtao1735 已提交
191
  cTrace("vgId:%d, id:%d CQ:%s is created", pContext->vgId, pObj->tid, pObj->sqlStr);
J
draft  
jtao1735 已提交
192

J
jtao1735 已提交
193
  pthread_mutex_lock(&pContext->mutex);
J
draft  
jtao1735 已提交
194

J
jtao1735 已提交
195
  pObj->next = pContext->pHead;
J
jtao1735 已提交
196
  if (pContext->pHead) pContext->pHead->prev = pObj;
J
jtao1735 已提交
197
  pContext->pHead = pObj;
J
draft  
jtao1735 已提交
198

199
  cqCreateStream(pContext, pObj);
J
draft  
jtao1735 已提交
200

J
jtao1735 已提交
201
  pthread_mutex_unlock(&pContext->mutex);
J
jtao1735 已提交
202 203

  return pObj;
J
jtao1735 已提交
204 205
}

J
jtao1735 已提交
206 207 208
void cqDrop(void *handle) {
  SCqObj *pObj = handle;
  SCqContext *pContext = pObj->pContext;
J
jtao1735 已提交
209 210 211

  pthread_mutex_lock(&pContext->mutex);

J
jtao1735 已提交
212 213 214 215 216
  if (pObj->prev) {
    pObj->prev->next = pObj->next;
  } else {
    pContext->pHead = pObj->next;
  }
J
jtao1735 已提交
217

J
jtao1735 已提交
218 219
  if (pObj->next) {
    pObj->next->prev = pObj->prev;
J
draft  
jtao1735 已提交
220 221
  }

J
jtao1735 已提交
222
  // free the resources associated
B
Bomin Zhang 已提交
223 224 225 226 227 228 229
  if (pObj->pStream) {
    taos_close_stream(pObj->pStream);
    pObj->pStream = NULL;
  } else {
    taosTmrStop(pObj->tmrId);
    pObj->tmrId = 0;
  }
J
draft  
jtao1735 已提交
230

J
jtao1735 已提交
231
  cTrace("vgId:%d, id:%d CQ:%s is dropped", pContext->vgId, pObj->tid, pObj->sqlStr); 
232 233
  tdFreeSchema(pObj->pSchema);
  free(pObj->sqlStr);
J
jtao1735 已提交
234
  free(pObj);
J
draft  
jtao1735 已提交
235

B
Bomin Zhang 已提交
236
  pthread_mutex_unlock(&pContext->mutex);
J
draft  
jtao1735 已提交
237 238
}

B
Bomin Zhang 已提交
239 240 241 242
static void cqProcessCreateTimer(void *param, void *tmrId) {
  SCqObj* pObj = (SCqObj*)param;
  SCqContext* pContext = pObj->pContext;

243
  if (pContext->dbConn == NULL) {
B
Bomin Zhang 已提交
244
    pContext->dbConn = taos_connect("localhost", pContext->user, pContext->pass, pContext->db, 0);
245 246 247 248
    if (pContext->dbConn == NULL) {
      cError("vgId:%d, failed to connect to TDengine(%s)", pContext->vgId, tstrerror(terrno));
    }
  }
B
Bomin Zhang 已提交
249 250 251
  
  cqCreateStream(pContext, pObj);
}
252

B
Bomin Zhang 已提交
253
static void cqCreateStream(SCqContext *pContext, SCqObj *pObj) {
B
Bomin Zhang 已提交
254
  pObj->pContext = pContext;
B
Bomin Zhang 已提交
255 256 257 258 259 260 261 262

  if (pContext->dbConn == NULL) {
    pObj->tmrId = taosTmrStart(cqProcessCreateTimer, 1000, pObj, pContext->tmrCtrl);
    return;
  }
  pObj->tmrId = 0;

  pObj->pStream = taos_open_stream(pContext->dbConn, pObj->sqlStr, cqProcessStreamRes, 0, pObj, NULL);
263 264 265 266 267 268 269 270
  if (pObj->pStream) {
    pContext->num++;
    cTrace("vgId:%d, id:%d CQ:%s is openned", pContext->vgId, pObj->tid, pObj->sqlStr);
  } else {
    cError("vgId:%d, id:%d CQ:%s, failed to open", pContext->vgId, pObj->tid, pObj->sqlStr);
  }
}

J
jtao1735 已提交
271 272
static void cqProcessStreamRes(void *param, TAOS_RES *tres, TAOS_ROW row) {
  SCqObj     *pObj = (SCqObj *)param;
B
Bomin Zhang 已提交
273 274 275 276
  if (tres == NULL && row == NULL) {
    pObj->pStream = NULL;
    return;
  }
J
jtao1735 已提交
277
  SCqContext *pContext = pObj->pContext;
B
Bomin Zhang 已提交
278
  STSchema   *pSchema = pObj->pSchema;
J
jtao1735 已提交
279
  if (pObj->pStream == NULL) return;
J
draft  
jtao1735 已提交
280

J
jtao1735 已提交
281
  cTrace("vgId:%d, id:%d CQ:%s stream result is ready", pContext->vgId, pObj->tid, pObj->sqlStr);
J
jtao1735 已提交
282

283
  int size = sizeof(SWalHead) + sizeof(SSubmitMsg) + sizeof(SSubmitBlk) + TD_DATA_ROW_HEAD_SIZE + pObj->rowSize;
J
jtao1735 已提交
284 285 286
  char *buffer = calloc(size, 1);

  SWalHead   *pHead = (SWalHead *)buffer;
B
Bomin Zhang 已提交
287 288
  SSubmitMsg *pMsg = (SSubmitMsg *) (buffer + sizeof(SWalHead));
  SSubmitBlk *pBlk = (SSubmitBlk *) (buffer + sizeof(SWalHead) + sizeof(SSubmitMsg));
J
draft  
jtao1735 已提交
289

B
Bomin Zhang 已提交
290
  SDataRow trow = (SDataRow)pBlk->data;
291
  tdInitDataRow(trow, pSchema);
J
jtao1735 已提交
292

B
Bomin Zhang 已提交
293
  for (int32_t i = 0; i < pSchema->numOfCols; i++) {
294
    STColumn *c = pSchema->columns + i;
295 296 297
    void* val = row[i];
    if (val == NULL) {
      val = getNullValue(c->type);
B
Bomin Zhang 已提交
298
    } else if (c->type == TSDB_DATA_TYPE_BINARY) {
299
      val = ((char*)val) - sizeof(VarDataLenT);
B
Bomin Zhang 已提交
300 301 302 303 304 305
    } else if (c->type == TSDB_DATA_TYPE_NCHAR) {
      char buf[TSDB_MAX_NCHAR_LEN];
      size_t len = taos_fetch_lengths(tres)[i];
      taosMbsToUcs4(val, len, buf, sizeof(buf), &len);
      memcpy(val + sizeof(VarDataLenT), buf, len);
      varDataLen(val) = len;
306 307
    }
    tdAppendColVal(trow, val, c->type, c->bytes, c->offset);
B
Bomin Zhang 已提交
308
  }
H
Haojun Liao 已提交
309 310
  pBlk->dataLen = htonl(dataRowLen(trow));
  pBlk->schemaLen = 0;
B
Bomin Zhang 已提交
311 312 313 314 315 316 317

  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 已提交
318 319
  pHead->len = sizeof(SSubmitMsg) + sizeof(SSubmitBlk) + dataRowLen(trow);

B
Bomin Zhang 已提交
320
  pMsg->header.vgId = htonl(pContext->vgId);
B
Bomin Zhang 已提交
321
  pMsg->header.contLen = htonl(pHead->len);
B
Bomin Zhang 已提交
322 323 324 325 326
  pMsg->length = pMsg->header.contLen;
  pMsg->numOfBlocks = htonl(1);

  pHead->msgType = TSDB_MSG_TYPE_SUBMIT;
  pHead->version = 0;
J
jtao1735 已提交
327 328 329

  // write into vnode write queue
  pContext->cqWrite(pContext->ahandle, pHead, TAOS_QTYPE_CQ);
B
Bomin Zhang 已提交
330
  free(buffer);
J
jtao1735 已提交
331 332
}