You need to sign in or sign up before continuing.
mndConsumer.c 14.9 KB
Newer Older
L
Liu Jicong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
/*
 * 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
#include "mndConsumer.h"
#include "mndDb.h"
#include "mndDnode.h"
#include "mndMnode.h"
#include "mndShow.h"
#include "mndStb.h"
#include "mndTopic.h"
#include "mndTrans.h"
#include "mndUser.h"
#include "mndVgroup.h"
#include "tname.h"

#define MND_CONSUMER_VER_NUMBER 1
#define MND_CONSUMER_RESERVE_SIZE 64

static SSdbRaw *mndConsumerActionEncode(SConsumerObj *pConsumer);
static SSdbRow *mndConsumerActionDecode(SSdbRaw *pRaw);
static int32_t  mndConsumerActionInsert(SSdb *pSdb, SConsumerObj *pConsumer);
static int32_t  mndConsumerActionDelete(SSdb *pSdb, SConsumerObj *pConsumer);
static int32_t  mndConsumerActionUpdate(SSdb *pSdb, SConsumerObj *pConsumer, SConsumerObj *pNewConsumer);
static int32_t  mndProcessCreateConsumerMsg(SMnodeMsg *pMsg);
static int32_t  mndProcessDropConsumerMsg(SMnodeMsg *pMsg);
static int32_t  mndProcessDropConsumerInRsp(SMnodeMsg *pMsg);
static int32_t  mndProcessConsumerMetaMsg(SMnodeMsg *pMsg);
static int32_t  mndGetConsumerMeta(SMnodeMsg *pMsg, SShowObj *pShow, STableMetaMsg *pMeta);
static int32_t  mndRetrieveConsumer(SMnodeMsg *pMsg, SShowObj *pShow, char *data, int32_t rows);
static void     mndCancelGetNextConsumer(SMnode *pMnode, void *pIter);

static int32_t mndProcessSubscribeReq(SMnodeMsg *pMsg);
static int32_t mndProcessSubscribeRsp(SMnodeMsg *pMsg);
static int32_t mndProcessSubscribeInternalReq(SMnodeMsg *pMsg);
static int32_t mndProcessSubscribeInternalRsp(SMnodeMsg *pMsg);

int32_t mndInitConsumer(SMnode *pMnode) {
  SSdbTable table = {.sdbType = SDB_CONSUMER,
                     .keyType = SDB_KEY_BINARY,
                     .encodeFp = (SdbEncodeFp)mndConsumerActionEncode,
                     .decodeFp = (SdbDecodeFp)mndConsumerActionDecode,
                     .insertFp = (SdbInsertFp)mndConsumerActionInsert,
                     .updateFp = (SdbUpdateFp)mndConsumerActionUpdate,
                     .deleteFp = (SdbDeleteFp)mndConsumerActionDelete};

  mndSetMsgHandle(pMnode, TDMT_MND_SUBSCRIBE, mndProcessSubscribeReq);
  mndSetMsgHandle(pMnode, TDMT_MND_SUBSCRIBE_RSP, mndProcessSubscribeRsp);
  mndSetMsgHandle(pMnode, TDMT_VND_SUBSCRIBE, mndProcessSubscribeInternalReq);
  mndSetMsgHandle(pMnode, TDMT_VND_SUBSCRIBE_RSP, mndProcessSubscribeInternalRsp);

  return sdbSetTable(pMnode->pSdb, table);
}

void mndCleanupConsumer(SMnode *pMnode) {}

L
Liu Jicong 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
static SSdbRaw *mndCGroupActionEncode(SCGroupObj *pCGroup) {
  int32_t  size = sizeof(SConsumerObj) + MND_CONSUMER_RESERVE_SIZE;
  SSdbRaw *pRaw = sdbAllocRaw(SDB_CONSUMER, MND_CONSUMER_VER_NUMBER, size);
  if (pRaw == NULL) return NULL;

  int32_t dataPos = 0;
  SDB_SET_BINARY(pRaw, dataPos, pCGroup->name, TSDB_TABLE_FNAME_LEN);
  SDB_SET_INT64(pRaw, dataPos, pCGroup->createTime);
  SDB_SET_INT64(pRaw, dataPos, pCGroup->updateTime);
  SDB_SET_INT64(pRaw, dataPos, pCGroup->uid);
  /*SDB_SET_INT64(pRaw, dataPos, pConsumer->dbUid);*/
  SDB_SET_INT32(pRaw, dataPos, pCGroup->version);

  int32_t sz = listNEles(pCGroup->consumerIds);
  SDB_SET_INT32(pRaw, dataPos, sz);

  SListIter iter;
  tdListInitIter(pCGroup->consumerIds, &iter, TD_LIST_FORWARD);
  SListNode *pn = NULL;
  while ((pn = tdListNext(&iter)) != NULL) {
    int64_t consumerId = *(int64_t *)pn->data;
    SDB_SET_INT64(pRaw, dataPos, consumerId);
  }

  SDB_SET_RESERVE(pRaw, dataPos, MND_CONSUMER_RESERVE_SIZE);
  SDB_SET_DATALEN(pRaw, dataPos);
  return pRaw;
}

static SSdbRow *mndCGroupActionDecode(SSdbRaw *pRaw) {
  int8_t sver = 0;
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) return NULL;

  if (sver != MND_CONSUMER_VER_NUMBER) {
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
    mError("failed to decode cgroup since %s", terrstr());
    return NULL;
  }

  // TODO: maximum size is not known
  int32_t     size = sizeof(SCGroupObj) + 128 * sizeof(int64_t);
  SSdbRow    *pRow = sdbAllocRow(size);
  SCGroupObj *pCGroup = sdbGetRowObj(pRow);
  if (pCGroup == NULL) return NULL;

  int32_t dataPos = 0;
  SDB_GET_BINARY(pRaw, pRow, dataPos, pCGroup->name, TSDB_TABLE_FNAME_LEN);
  SDB_GET_INT64(pRaw, pRow, dataPos, &pCGroup->createTime);
  SDB_GET_INT64(pRaw, pRow, dataPos, &pCGroup->updateTime);
  SDB_GET_INT64(pRaw, pRow, dataPos, &pCGroup->uid);
  /*SDB_GET_INT64(pRaw, pRow, dataPos, &pConsumer->dbUid);*/
  SDB_GET_INT32(pRaw, pRow, dataPos, &pCGroup->version);

  int32_t sz;
  SDB_GET_INT32(pRaw, pRow, dataPos, &sz);
  // TODO: free list when failing
  tdListInit(pCGroup->consumerIds, sizeof(int64_t));
  for (int i = 0; i < sz; i++) {
    int64_t consumerId;
    SDB_GET_INT64(pRaw, pRow, dataPos, &consumerId);
    tdListAppend(pCGroup->consumerIds, &consumerId);
  }

  SDB_GET_RESERVE(pRaw, pRow, dataPos, MND_CONSUMER_RESERVE_SIZE);
  return pRow;
}

L
Liu Jicong 已提交
136 137 138 139 140 141
static SSdbRaw *mndConsumerActionEncode(SConsumerObj *pConsumer) {
  int32_t  size = sizeof(SConsumerObj) + MND_CONSUMER_RESERVE_SIZE;
  SSdbRaw *pRaw = sdbAllocRaw(SDB_CONSUMER, MND_CONSUMER_VER_NUMBER, size);
  if (pRaw == NULL) return NULL;

  int32_t dataPos = 0;
L
Liu Jicong 已提交
142
  SDB_SET_INT64(pRaw, dataPos, pConsumer->uid);
L
Liu Jicong 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
  SDB_SET_INT64(pRaw, dataPos, pConsumer->createTime);
  SDB_SET_INT64(pRaw, dataPos, pConsumer->updateTime);
  /*SDB_SET_INT64(pRaw, dataPos, pConsumer->dbUid);*/
  SDB_SET_INT32(pRaw, dataPos, pConsumer->version);

  SDB_SET_RESERVE(pRaw, dataPos, MND_CONSUMER_RESERVE_SIZE);
  SDB_SET_DATALEN(pRaw, dataPos);

  return pRaw;
}

static SSdbRow *mndConsumerActionDecode(SSdbRaw *pRaw) {
  int8_t sver = 0;
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) return NULL;

  if (sver != MND_CONSUMER_VER_NUMBER) {
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
    mError("failed to decode consumer since %s", terrstr());
    return NULL;
  }

  int32_t       size = sizeof(SConsumerObj) + TSDB_MAX_COLUMNS * sizeof(SSchema);
  SSdbRow      *pRow = sdbAllocRow(size);
  SConsumerObj *pConsumer = sdbGetRowObj(pRow);
  if (pConsumer == NULL) return NULL;

  int32_t dataPos = 0;
L
Liu Jicong 已提交
170
  SDB_GET_INT64(pRaw, pRow, dataPos, &pConsumer->uid);
L
Liu Jicong 已提交
171 172 173 174 175 176 177 178 179 180 181
  SDB_GET_INT64(pRaw, pRow, dataPos, &pConsumer->createTime);
  SDB_GET_INT64(pRaw, pRow, dataPos, &pConsumer->updateTime);
  /*SDB_GET_INT64(pRaw, pRow, dataPos, &pConsumer->dbUid);*/
  SDB_GET_INT32(pRaw, pRow, dataPos, &pConsumer->version);

  SDB_GET_RESERVE(pRaw, pRow, dataPos, MND_CONSUMER_RESERVE_SIZE);

  return pRow;
}

static int32_t mndConsumerActionInsert(SSdb *pSdb, SConsumerObj *pConsumer) {
L
Liu Jicong 已提交
182
  mTrace("consumer:%ld, perform insert action", pConsumer->uid);
L
Liu Jicong 已提交
183 184 185 186
  return 0;
}

static int32_t mndConsumerActionDelete(SSdb *pSdb, SConsumerObj *pConsumer) {
L
Liu Jicong 已提交
187
  mTrace("consumer:%ld, perform delete action", pConsumer->uid);
L
Liu Jicong 已提交
188 189 190 191
  return 0;
}

static int32_t mndConsumerActionUpdate(SSdb *pSdb, SConsumerObj *pOldConsumer, SConsumerObj *pNewConsumer) {
L
Liu Jicong 已提交
192
  mTrace("consumer:%ld, perform update action", pOldConsumer->uid);
L
Liu Jicong 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
  atomic_exchange_32(&pOldConsumer->updateTime, pNewConsumer->updateTime);
  atomic_exchange_32(&pOldConsumer->version, pNewConsumer->version);

  taosWLockLatch(&pOldConsumer->lock);

  // TODO handle update

  taosWUnLockLatch(&pOldConsumer->lock);
  return 0;
}

SConsumerObj *mndAcquireConsumer(SMnode *pMnode, int32_t consumerId) {
  SSdb         *pSdb = pMnode->pSdb;
  SConsumerObj *pConsumer = sdbAcquire(pSdb, SDB_CONSUMER, &consumerId);
  if (pConsumer == NULL) {
    /*terrno = TSDB_CODE_MND_CONSUMER_NOT_EXIST;*/
  }
  return pConsumer;
}

void mndReleaseConsumer(SMnode *pMnode, SConsumerObj *pConsumer) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pConsumer);
}

static int32_t mndProcessSubscribeReq(SMnodeMsg *pMsg) {
  SMnode          *pMnode = pMsg->pMnode;
  char            *msgStr = pMsg->rpcMsg.pCont;
  SCMSubscribeReq *pSubscribe;
  tDeserializeSCMSubscribeReq(msgStr, pSubscribe);
L
Liu Jicong 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
  int     topicNum = pSubscribe->topicNum;
  int64_t consumerId = pSubscribe->consumerId;
  char   *consumerGroup = pSubscribe->consumerGroup;
  // get consumer group and add client into it
  SCGroupObj *pCGroup = sdbAcquire(pMnode->pSdb, SDB_CGROUP, consumerGroup);
  if (pCGroup != NULL) {
    // iterate the list until finding the consumer
    // add consumer to cgroup list if not found
    // put new record
  }

  SConsumerObj *pConsumer = mndAcquireConsumer(pMnode, consumerId);
  if (pConsumer != NULL) {
    //reset topic list
  }

  for (int i = 0; i < topicNum; i++) {
    char *topicName = pSubscribe->topicName[i];
    STopicObj *pTopic = mndAcquireTopic(pMnode, topicName);
    //get 
    // consumer id
    SList *list = pTopic->consumerIds;
    // add the consumer if not in the list
    //
    SList* topicList = pConsumer->topics;
    //add to topic
  }

L
Liu Jicong 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
  return 0;
}

static int32_t mndProcessSubscribeRsp(SMnodeMsg *pMsg) { return 0; }

static int32_t mndProcessSubscribeInternalReq(SMnodeMsg *pMsg) { return 0; }

static int32_t mndProcessSubscribeInternalRsp(SMnodeMsg *pMsg) { return 0; }

static int32_t mndProcessDropConsumerInRsp(SMnodeMsg *pMsg) {
  mndTransProcessRsp(pMsg);
  return 0;
}

static int32_t mndProcessConsumerMetaMsg(SMnodeMsg *pMsg) {
  SMnode        *pMnode = pMsg->pMnode;
  STableInfoMsg *pInfo = pMsg->rpcMsg.pCont;

  mDebug("consumer:%s, start to retrieve meta", pInfo->tableFname);

#if 0
  SDbObj *pDb = mndAcquireDbByConsumer(pMnode, pInfo->tableFname);
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    mError("consumer:%s, failed to retrieve meta since %s", pInfo->tableFname, terrstr());
    return -1;
  }

  SConsumerObj *pConsumer = mndAcquireConsumer(pMnode, pInfo->tableFname);
  if (pConsumer == NULL) {
    mndReleaseDb(pMnode, pDb);
    terrno = TSDB_CODE_MND_INVALID_CONSUMER;
    mError("consumer:%s, failed to get meta since %s", pInfo->tableFname, terrstr());
    return -1;
  }

  taosRLockLatch(&pConsumer->lock);
  int32_t totalCols = pConsumer->numOfColumns + pConsumer->numOfTags;
  int32_t contLen = sizeof(STableMetaMsg) + totalCols * sizeof(SSchema);

  STableMetaMsg *pMeta = rpcMallocCont(contLen);
  if (pMeta == NULL) {
    taosRUnLockLatch(&pConsumer->lock);
    mndReleaseDb(pMnode, pDb);
    mndReleaseConsumer(pMnode, pConsumer);
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    mError("consumer:%s, failed to get meta since %s", pInfo->tableFname, terrstr());
    return -1;
  }

  memcpy(pMeta->consumerFname, pConsumer->name, TSDB_TABLE_FNAME_LEN);
  pMeta->numOfTags = htonl(pConsumer->numOfTags);
  pMeta->numOfColumns = htonl(pConsumer->numOfColumns);
  pMeta->precision = pDb->cfg.precision;
  pMeta->tableType = TSDB_SUPER_TABLE;
  pMeta->update = pDb->cfg.update;
  pMeta->sversion = htonl(pConsumer->version);
  pMeta->tuid = htonl(pConsumer->uid);

  for (int32_t i = 0; i < totalCols; ++i) {
    SSchema *pSchema = &pMeta->pSchema[i];
    SSchema *pSrcSchema = &pConsumer->pSchema[i];
    memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
    pSchema->type = pSrcSchema->type;
    pSchema->colId = htonl(pSrcSchema->colId);
    pSchema->bytes = htonl(pSrcSchema->bytes);
  }
  taosRUnLockLatch(&pConsumer->lock);
  mndReleaseDb(pMnode, pDb);
  mndReleaseConsumer(pMnode, pConsumer);

  pMsg->pCont = pMeta;
  pMsg->contLen = contLen;

  mDebug("consumer:%s, meta is retrieved, cols:%d tags:%d", pInfo->tableFname, pConsumer->numOfColumns, pConsumer->numOfTags);
#endif
  return 0;
}

static int32_t mndGetNumOfConsumers(SMnode *pMnode, char *dbName, int32_t *pNumOfConsumers) {
  SSdb *pSdb = pMnode->pSdb;

  SDbObj *pDb = mndAcquireDb(pMnode, dbName);
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    return -1;
  }

  int32_t numOfConsumers = 0;
  void   *pIter = NULL;
  while (1) {
    SConsumerObj *pConsumer = NULL;
    pIter = sdbFetch(pSdb, SDB_CONSUMER, pIter, (void **)&pConsumer);
    if (pIter == NULL) break;

L
Liu Jicong 已提交
346
    numOfConsumers++;
L
Liu Jicong 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404

    sdbRelease(pSdb, pConsumer);
  }

  *pNumOfConsumers = numOfConsumers;
  return 0;
}

static int32_t mndGetConsumerMeta(SMnodeMsg *pMsg, SShowObj *pShow, STableMetaMsg *pMeta) {
  SMnode *pMnode = pMsg->pMnode;
  SSdb   *pSdb = pMnode->pSdb;

  if (mndGetNumOfConsumers(pMnode, pShow->db, &pShow->numOfRows) != 0) {
    return -1;
  }

  int32_t  cols = 0;
  SSchema *pSchema = pMeta->pSchema;

  pShow->bytes[cols] = TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "name");
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
  strcpy(pSchema[cols].name, "create_time");
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 4;
  pSchema[cols].type = TSDB_DATA_TYPE_INT;
  strcpy(pSchema[cols].name, "columns");
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 4;
  pSchema[cols].type = TSDB_DATA_TYPE_INT;
  strcpy(pSchema[cols].name, "tags");
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
  cols++;

  pMeta->numOfColumns = htonl(cols);
  pShow->numOfColumns = cols;

  pShow->offset[0] = 0;
  for (int32_t i = 1; i < cols; ++i) {
    pShow->offset[i] = pShow->offset[i - 1] + pShow->bytes[i - 1];
  }

  pShow->numOfRows = sdbGetSize(pSdb, SDB_CONSUMER);
  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
  strcpy(pMeta->tbFname, mndShowStr(pShow->type));

  return 0;
}

L
Liu Jicong 已提交
405
static int32_t mndRetrieveCGroup(SMnodeMsg *pMsg, SShowObj *pShow, char *data, int32_t rows) {
L
Liu Jicong 已提交
406 407 408
  SMnode       *pMnode = pMsg->pMnode;
  SSdb         *pSdb = pMnode->pSdb;
  int32_t       numOfRows = 0;
L
Liu Jicong 已提交
409
  SCGroupObj   *pCGroup = NULL;
L
Liu Jicong 已提交
410 411 412 413 414 415 416 417 418
  int32_t       cols = 0;
  char         *pWrite;
  char          prefix[64] = {0};

  tstrncpy(prefix, pShow->db, 64);
  strcat(prefix, TS_PATH_DELIMITER);
  int32_t prefixLen = (int32_t)strlen(prefix);

  while (numOfRows < rows) {
L
Liu Jicong 已提交
419
    pShow->pIter = sdbFetch(pSdb, SDB_CONSUMER, pShow->pIter, (void **)&pCGroup);
L
Liu Jicong 已提交
420 421
    if (pShow->pIter == NULL) break;

L
Liu Jicong 已提交
422 423
    if (strncmp(pCGroup->name, prefix, prefixLen) != 0) {
      sdbRelease(pSdb, pCGroup);
L
Liu Jicong 已提交
424 425 426 427 428 429
      continue;
    }

    cols = 0;

    char consumerName[TSDB_TABLE_NAME_LEN] = {0};
L
Liu Jicong 已提交
430
    tstrncpy(consumerName, pCGroup->name + prefixLen, TSDB_TABLE_NAME_LEN);
L
Liu Jicong 已提交
431 432 433 434 435
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    STR_TO_VARSTR(pWrite, consumerName);
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
L
Liu Jicong 已提交
436
    *(int64_t *)pWrite = pCGroup->createTime;
L
Liu Jicong 已提交
437 438 439
    cols++;

    numOfRows++;
L
Liu Jicong 已提交
440
    sdbRelease(pSdb, pCGroup);
L
Liu Jicong 已提交
441 442 443 444 445 446 447 448 449 450 451
  }

  pShow->numOfReads += numOfRows;
  mndVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
  return numOfRows;
}

static void mndCancelGetNextConsumer(SMnode *pMnode, void *pIter) {
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
}