cqMain.c 7.9 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"
H
TD-354  
Hongze Cheng 已提交
26 27
#include "tcq.h"
#include "tdataformat.h"
J
jtao1735 已提交
28
#include "tglobal.h"
J
jtao1735 已提交
29 30 31
#include "tlog.h"
#include "twal.h"

S
Shengliang Guan 已提交
32 33 34 35
#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 已提交
36 37 38 39 40

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

typedef struct SCqObj {
B
Bomin Zhang 已提交
52 53
  uint64_t       uid;
  int32_t        tid;      // table ID
H
TD-354  
Hongze Cheng 已提交
54 55 56 57 58 59 60
  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 已提交
61 62 63 64 65
} SCqObj;

int cqDebugFlag = 135;

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

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

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

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

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

J
jtao1735 已提交
95 96
  return pContext;
}
J
draft  
jtao1735 已提交
97

J
jtao1735 已提交
98 99
void cqClose(void *handle) {
  SCqContext *pContext = handle;
100
  if (handle == NULL) return;
J
draft  
jtao1735 已提交
101

J
jtao1735 已提交
102 103
  // stop all CQs
  cqStop(pContext);
J
draft  
jtao1735 已提交
104

J
jtao1735 已提交
105
  // free all resources
106 107
  pthread_mutex_lock(&pContext->mutex);

J
jtao1735 已提交
108 109 110 111 112 113 114
  SCqObj *pObj = pContext->pHead;
  while (pObj) {
    SCqObj *pTemp = pObj;
    pObj = pObj->next;
    free(pTemp);
  } 
  
115 116
  pthread_mutex_unlock(&pContext->mutex);

J
jtao1735 已提交
117 118 119 120
  pthread_mutex_destroy(&pContext->mutex);

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

J
jtao1735 已提交
123 124
void cqStart(void *handle) {
  SCqContext *pContext = handle;
125
  if (pContext->dbConn || pContext->master) return;
J
jtao1735 已提交
126

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

130
  pContext->master = 1;
J
jtao1735 已提交
131 132 133

  SCqObj *pObj = pContext->pHead;
  while (pObj) {
134
    cqCreateStream(pContext, pObj);
J
jtao1735 已提交
135
    pObj = pObj->next;
J
draft  
jtao1735 已提交
136
  }
J
jtao1735 已提交
137 138

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

J
jtao1735 已提交
141 142 143
void cqStop(void *handle) {
  SCqContext *pContext = handle;
  cTrace("vgId:%d, stop all CQs", pContext->vgId);
144
  if (pContext->dbConn == NULL || pContext->master == 0) return;
J
draft  
jtao1735 已提交
145

J
jtao1735 已提交
146
  pthread_mutex_lock(&pContext->mutex);
J
draft  
jtao1735 已提交
147

148
  pContext->master = 0;
J
jtao1735 已提交
149 150
  SCqObj *pObj = pContext->pHead;
  while (pObj) {
J
jtao1735 已提交
151 152 153 154 155
    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);
    }
J
draft  
jtao1735 已提交
156

J
jtao1735 已提交
157
    pObj = pObj->next;
J
draft  
jtao1735 已提交
158
  }
J
jtao1735 已提交
159 160 161 162 163

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

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

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

J
jtao1735 已提交
169
  SCqObj *pObj = calloc(sizeof(SCqObj), 1);
J
jtao1735 已提交
170
  if (pObj == NULL) return NULL;
J
draft  
jtao1735 已提交
171

B
Bomin Zhang 已提交
172
  pObj->uid = uid;
J
jtao1735 已提交
173
  pObj->tid = tid;
J
jtao1735 已提交
174 175
  pObj->sqlStr = malloc(strlen(sqlStr)+1);
  strcpy(pObj->sqlStr, sqlStr);
J
draft  
jtao1735 已提交
176

H
TD-354  
Hongze Cheng 已提交
177
  pObj->pSchema = tdDupSchema(pSchema);
B
Bomin Zhang 已提交
178
  pObj->rowSize = pSchema->tlen;
J
draft  
jtao1735 已提交
179

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

J
jtao1735 已提交
182
  pthread_mutex_lock(&pContext->mutex);
J
draft  
jtao1735 已提交
183

J
jtao1735 已提交
184
  pObj->next = pContext->pHead;
J
jtao1735 已提交
185
  if (pContext->pHead) pContext->pHead->prev = pObj;
J
jtao1735 已提交
186
  pContext->pHead = pObj;
J
draft  
jtao1735 已提交
187

188
  cqCreateStream(pContext, pObj);
J
draft  
jtao1735 已提交
189

J
jtao1735 已提交
190
  pthread_mutex_unlock(&pContext->mutex);
J
jtao1735 已提交
191 192

  return pObj;
J
jtao1735 已提交
193 194
}

J
jtao1735 已提交
195 196 197
void cqDrop(void *handle) {
  SCqObj *pObj = handle;
  SCqContext *pContext = pObj->pContext;
J
jtao1735 已提交
198 199 200

  pthread_mutex_lock(&pContext->mutex);

J
jtao1735 已提交
201 202 203 204 205
  if (pObj->prev) {
    pObj->prev->next = pObj->next;
  } else {
    pContext->pHead = pObj->next;
  }
J
jtao1735 已提交
206

J
jtao1735 已提交
207 208
  if (pObj->next) {
    pObj->next->prev = pObj->prev;
J
draft  
jtao1735 已提交
209 210
  }

J
jtao1735 已提交
211 212 213
  // free the resources associated
  if (pObj->pStream) taos_close_stream(pObj->pStream);
  pObj->pStream = NULL;
J
draft  
jtao1735 已提交
214

J
jtao1735 已提交
215 216
  cTrace("vgId:%d, id:%d CQ:%s is dropped", pContext->vgId, pObj->tid, pObj->sqlStr); 
  free(pObj);
J
draft  
jtao1735 已提交
217

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

221 222
static void cqCreateStream(SCqContext *pContext, SCqObj *pObj) {
  if (pContext->dbConn == NULL) {
B
Bomin Zhang 已提交
223
    pContext->dbConn = taos_connect("localhost", pContext->user, pContext->pass, pContext->db, 0);
224 225
    if (pContext->dbConn == NULL) {
      cError("vgId:%d, failed to connect to TDengine(%s)", pContext->vgId, tstrerror(terrno));
B
Bomin Zhang 已提交
226
      return;
227 228 229 230
    }
  }

  int64_t lastKey = 0;
B
Bomin Zhang 已提交
231
  pObj->pContext = pContext;
232 233 234 235 236 237 238 239 240
  pObj->pStream = taos_open_stream(pContext->dbConn, pObj->sqlStr, cqProcessStreamRes, lastKey, pObj, NULL);
  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 已提交
241 242 243
static void cqProcessStreamRes(void *param, TAOS_RES *tres, TAOS_ROW row) {
  SCqObj     *pObj = (SCqObj *)param;
  SCqContext *pContext = pObj->pContext;
B
Bomin Zhang 已提交
244
  STSchema   *pSchema = pObj->pSchema;
J
jtao1735 已提交
245
  if (pObj->pStream == NULL) return;
J
draft  
jtao1735 已提交
246

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

B
Bomin Zhang 已提交
249 250 251 252 253
  int32_t flen = 0;
  for (int32_t i = 0; i < pSchema->numOfCols; i++) {
    flen += TYPE_BYTES[pSchema->columns[i].type];
  }

J
jtao1735 已提交
254
  // construct data
B
Bomin Zhang 已提交
255
  int size = sizeof(SWalHead) + sizeof(SSubmitMsg) + sizeof(SSubmitBlk) + TD_DATA_ROW_HEAD_SIZE + flen;
J
jtao1735 已提交
256 257 258
  char *buffer = calloc(size, 1);

  SWalHead   *pHead = (SWalHead *)buffer;
B
Bomin Zhang 已提交
259 260
  SSubmitMsg *pMsg = (SSubmitMsg *) (buffer + sizeof(SWalHead));
  SSubmitBlk *pBlk = (SSubmitBlk *) (buffer + sizeof(SWalHead) + sizeof(SSubmitMsg));
J
draft  
jtao1735 已提交
261

B
Bomin Zhang 已提交
262 263
  SDataRow trow = (SDataRow)pBlk->data;
  dataRowSetLen(trow, TD_DATA_ROW_HEAD_SIZE + flen);
J
jtao1735 已提交
264

B
Bomin Zhang 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
  int toffset = 0;
  for (int32_t i = 0; i < pSchema->numOfCols; i++) {
    tdAppendColVal(trow, row[i], pSchema->columns[i].type, pSchema->columns[i].bytes, toffset);
    toffset += TYPE_BYTES[pSchema->columns[i].type];
  }
  pBlk->len = htonl(dataRowLen(trow));

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

  pMsg->header.vgId = htonl(pContext->vgId);
  pMsg->header.contLen = htonl(size - sizeof(SWalHead));
  pMsg->length = pMsg->header.contLen;
  pMsg->numOfBlocks = htonl(1);

  pHead->msgType = TSDB_MSG_TYPE_SUBMIT;
  pHead->len = size - sizeof(SWalHead);
  pHead->version = 0;
J
jtao1735 已提交
286 287 288

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