cqMain.c 6.8 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 41 42 43 44 45

typedef struct {
  int      vgId;
  char     user[TSDB_USER_LEN];
  char     pass[TSDB_PASSWORD_LEN];
  FCqWrite cqWrite;
  void    *ahandle;
  int      num;      // number of continuous streams
  struct SCqObj *pHead;
  void    *dbConn;
46
  int      master;
J
jtao1735 已提交
47 48 49 50
  pthread_mutex_t mutex;
} SCqContext;

typedef struct SCqObj {
H
TD-354  
Hongze Cheng 已提交
51 52 53 54 55 56 57 58
  int            tid;      // table ID
  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 已提交
59 60 61 62 63
} SCqObj;

int cqDebugFlag = 135;

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

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

  strcpy(pContext->user, pCfg->user);
  strcpy(pContext->pass, pCfg->pass);
  pContext->vgId = pCfg->vgId;
  pContext->cqWrite = pCfg->cqWrite;
  pContext->ahandle = ahandle;
79
  tscEmbedded = 1;
J
jtao1735 已提交
80 81

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

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

J
jtao1735 已提交
85 86
  return pContext;
}
J
draft  
jtao1735 已提交
87

J
jtao1735 已提交
88 89
void cqClose(void *handle) {
  SCqContext *pContext = handle;
90
  if (handle == NULL) return;
J
draft  
jtao1735 已提交
91

J
jtao1735 已提交
92 93
  // stop all CQs
  cqStop(pContext);
J
draft  
jtao1735 已提交
94

J
jtao1735 已提交
95
  // free all resources
96 97
  pthread_mutex_lock(&pContext->mutex);

J
jtao1735 已提交
98 99 100 101 102 103 104
  SCqObj *pObj = pContext->pHead;
  while (pObj) {
    SCqObj *pTemp = pObj;
    pObj = pObj->next;
    free(pTemp);
  } 
  
105 106
  pthread_mutex_unlock(&pContext->mutex);

J
jtao1735 已提交
107 108 109 110
  pthread_mutex_destroy(&pContext->mutex);

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

J
jtao1735 已提交
113 114
void cqStart(void *handle) {
  SCqContext *pContext = handle;
115
  if (pContext->dbConn || pContext->master) return;
J
jtao1735 已提交
116

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

120
  pContext->master = 1;
J
jtao1735 已提交
121 122 123

  SCqObj *pObj = pContext->pHead;
  while (pObj) {
124
    cqCreateStream(pContext, pObj);
J
jtao1735 已提交
125
    pObj = pObj->next;
J
draft  
jtao1735 已提交
126
  }
J
jtao1735 已提交
127 128

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

J
jtao1735 已提交
131 132 133
void cqStop(void *handle) {
  SCqContext *pContext = handle;
  cTrace("vgId:%d, stop all CQs", pContext->vgId);
134
  if (pContext->dbConn == NULL || pContext->master == 0) return;
J
draft  
jtao1735 已提交
135

J
jtao1735 已提交
136
  pthread_mutex_lock(&pContext->mutex);
J
draft  
jtao1735 已提交
137

138
  pContext->master = 0;
J
jtao1735 已提交
139 140
  SCqObj *pObj = pContext->pHead;
  while (pObj) {
J
jtao1735 已提交
141 142 143 144 145
    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 已提交
146

J
jtao1735 已提交
147
    pObj = pObj->next;
J
draft  
jtao1735 已提交
148
  }
J
jtao1735 已提交
149 150 151 152 153

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

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

H
TD-354  
Hongze Cheng 已提交
156
void *cqCreate(void *handle, int tid, char *sqlStr, STSchema *pSchema) {
J
jtao1735 已提交
157
  SCqContext *pContext = handle;
J
draft  
jtao1735 已提交
158

J
jtao1735 已提交
159
  SCqObj *pObj = calloc(sizeof(SCqObj), 1);
J
jtao1735 已提交
160
  if (pObj == NULL) return NULL;
J
draft  
jtao1735 已提交
161

J
jtao1735 已提交
162
  pObj->tid = tid;
J
jtao1735 已提交
163 164
  pObj->sqlStr = malloc(strlen(sqlStr)+1);
  strcpy(pObj->sqlStr, sqlStr);
J
draft  
jtao1735 已提交
165

H
TD-354  
Hongze Cheng 已提交
166
  pObj->pSchema = tdDupSchema(pSchema);
J
draft  
jtao1735 已提交
167

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

J
jtao1735 已提交
170
  pthread_mutex_lock(&pContext->mutex);
J
draft  
jtao1735 已提交
171

J
jtao1735 已提交
172
  pObj->next = pContext->pHead;
J
jtao1735 已提交
173
  if (pContext->pHead) pContext->pHead->prev = pObj;
J
jtao1735 已提交
174
  pContext->pHead = pObj;
J
draft  
jtao1735 已提交
175

176
  cqCreateStream(pContext, pObj);
J
draft  
jtao1735 已提交
177

J
jtao1735 已提交
178
  pthread_mutex_unlock(&pContext->mutex);
J
jtao1735 已提交
179 180

  return pObj;
J
jtao1735 已提交
181 182
}

J
jtao1735 已提交
183 184 185
void cqDrop(void *handle) {
  SCqObj *pObj = handle;
  SCqContext *pContext = pObj->pContext;
J
jtao1735 已提交
186 187 188

  pthread_mutex_lock(&pContext->mutex);

J
jtao1735 已提交
189 190 191 192 193
  if (pObj->prev) {
    pObj->prev->next = pObj->next;
  } else {
    pContext->pHead = pObj->next;
  }
J
jtao1735 已提交
194

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

J
jtao1735 已提交
199 200 201
  // free the resources associated
  if (pObj->pStream) taos_close_stream(pObj->pStream);
  pObj->pStream = NULL;
J
draft  
jtao1735 已提交
202

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

J
jtao1735 已提交
206
  pthread_mutex_lock(&pContext->mutex);
J
draft  
jtao1735 已提交
207 208
}

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
static void cqCreateStream(SCqContext *pContext, SCqObj *pObj) {

  if (pContext->dbConn == NULL) {
    pContext->dbConn = taos_connect("localhost", pContext->user, pContext->pass, NULL, 0);
    if (pContext->dbConn == NULL) {
      cError("vgId:%d, failed to connect to TDengine(%s)", pContext->vgId, tstrerror(terrno));
    }
    return;
  }

  int64_t lastKey = 0;
  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 已提交
229 230 231 232
static void cqProcessStreamRes(void *param, TAOS_RES *tres, TAOS_ROW row) {
  SCqObj     *pObj = (SCqObj *)param;
  SCqContext *pContext = pObj->pContext;
  if (pObj->pStream == NULL) return;
J
draft  
jtao1735 已提交
233

J
jtao1735 已提交
234
  cTrace("vgId:%d, id:%d CQ:%s stream result is ready", pContext->vgId, pObj->tid, pObj->sqlStr);
J
jtao1735 已提交
235 236 237 238 239 240 241 242 243 244 245 246

  // construct data
  int size = sizeof(SWalHead) + sizeof(SSubmitMsg) + sizeof(SSubmitBlk) + pObj->rowSize;
  char *buffer = calloc(size, 1);

  SWalHead   *pHead = (SWalHead *)buffer;
  pHead->msgType = TSDB_MSG_TYPE_SUBMIT;
  pHead->len = size - sizeof(SWalHead);
  
  SSubmitMsg *pSubmit = (SSubmitMsg *) (buffer + sizeof(SWalHead));
  // to do: fill in the SSubmitMsg structure
  pSubmit->numOfBlocks = 1;
J
draft  
jtao1735 已提交
247 248


J
jtao1735 已提交
249 250
  SSubmitBlk *pBlk = (SSubmitBlk *) (buffer + sizeof(SWalHead) + sizeof(SSubmitMsg));
  // to do: fill in the SSubmitBlk strucuture
J
jtao1735 已提交
251
  pBlk->tid = pObj->tid;
J
jtao1735 已提交
252 253 254 255 256 257


  // write into vnode write queue
  pContext->cqWrite(pContext->ahandle, pHead, TAOS_QTYPE_CQ);
}