mndConsumer.c 18.1 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
/*
 * 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"
L
Liu Jicong 已提交
27
#include "tcompare.h"
L
Liu Jicong 已提交
28 29 30 31 32
#include "tname.h"

#define MND_CONSUMER_VER_NUMBER 1
#define MND_CONSUMER_RESERVE_SIZE 64

L
Liu Jicong 已提交
33
static SSdbRaw *mndConsumerActionEncode(SMqConsumerObj *pConsumer);
L
Liu Jicong 已提交
34
static SSdbRow *mndConsumerActionDecode(SSdbRaw *pRaw);
L
Liu Jicong 已提交
35 36 37
static int32_t  mndConsumerActionInsert(SSdb *pSdb, SMqConsumerObj *pConsumer);
static int32_t  mndConsumerActionDelete(SSdb *pSdb, SMqConsumerObj *pConsumer);
static int32_t  mndConsumerActionUpdate(SSdb *pSdb, SMqConsumerObj *pConsumer, SMqConsumerObj *pNewConsumer);
L
Liu Jicong 已提交
38 39 40 41
static int32_t  mndProcessCreateConsumerMsg(SMnodeMsg *pMsg);
static int32_t  mndProcessDropConsumerMsg(SMnodeMsg *pMsg);
static int32_t  mndProcessDropConsumerInRsp(SMnodeMsg *pMsg);
static int32_t  mndProcessConsumerMetaMsg(SMnodeMsg *pMsg);
S
Shengliang Guan 已提交
42
static int32_t  mndGetConsumerMeta(SMnodeMsg *pMsg, SShowObj *pShow, STableMetaRsp *pMeta);
L
Liu Jicong 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
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);
L
Liu Jicong 已提交
61 62
  /*mndSetMsgHandle(pMnode, TDMT_MND_SUBSCRIBE_RSP, mndProcessSubscribeRsp);*/
  /*mndSetMsgHandle(pMnode, TDMT_VND_SUBSCRIBE, mndProcessSubscribeInternalReq);*/
L
Liu Jicong 已提交
63 64 65 66 67 68 69
  mndSetMsgHandle(pMnode, TDMT_VND_SUBSCRIBE_RSP, mndProcessSubscribeInternalRsp);

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

void mndCleanupConsumer(SMnode *pMnode) {}

L
Liu Jicong 已提交
70 71
static void *mndBuildMqVGroupSetReq(SMnode *pMnode, char *topicName, int32_t vgId, int64_t consumerId, char *cgroup) {
  return 0;
L
Liu Jicong 已提交
72 73
}

L
Liu Jicong 已提交
74 75
static SSdbRaw *mndConsumerActionEncode(SMqConsumerObj *pConsumer) {
  int32_t  size = sizeof(SMqConsumerObj) + MND_CONSUMER_RESERVE_SIZE;
L
Liu Jicong 已提交
76
  SSdbRaw *pRaw = sdbAllocRaw(SDB_CONSUMER, MND_CONSUMER_VER_NUMBER, size);
77
  if (pRaw == NULL) goto CM_ENCODE_OVER;
L
Liu Jicong 已提交
78 79

  int32_t dataPos = 0;
L
Liu Jicong 已提交
80
  int32_t topicNum = taosArrayGetSize(pConsumer->topics);
L
Liu Jicong 已提交
81
  SDB_SET_INT64(pRaw, dataPos, pConsumer->consumerId, CM_ENCODE_OVER);
L
Liu Jicong 已提交
82
  int32_t len = strlen(pConsumer->cgroup);
L
Liu Jicong 已提交
83 84
  SDB_SET_INT32(pRaw, dataPos, len, CM_ENCODE_OVER);
  SDB_SET_BINARY(pRaw, dataPos, pConsumer->cgroup, len, CM_ENCODE_OVER);
L
Liu Jicong 已提交
85
  SDB_SET_INT32(pRaw, dataPos, topicNum, CM_ENCODE_OVER);
L
Liu Jicong 已提交
86 87 88 89
  for (int i = 0; i < topicNum; i++) {
    int32_t           len;
    SMqConsumerTopic *pConsumerTopic = taosArrayGet(pConsumer->topics, i);
    len = strlen(pConsumerTopic->name);
L
Liu Jicong 已提交
90 91
    SDB_SET_INT32(pRaw, dataPos, len, CM_ENCODE_OVER);
    SDB_SET_BINARY(pRaw, dataPos, pConsumerTopic->name, len, CM_ENCODE_OVER);
L
Liu Jicong 已提交
92 93 94 95 96 97
    int vgSize;
    if (pConsumerTopic->vgroups == NULL) {
      vgSize = 0;
    } else {
      vgSize = listNEles(pConsumerTopic->vgroups);
    }
L
Liu Jicong 已提交
98
    SDB_SET_INT32(pRaw, dataPos, vgSize, CM_ENCODE_OVER);
L
Liu Jicong 已提交
99 100 101 102 103
    for (int j = 0; j < vgSize; j++) {
      // SList* head;
      /*SDB_SET_INT64(pRaw, dataPos, 0[> change to list item <]);*/
    }
  }
L
Liu Jicong 已提交
104

L
Liu Jicong 已提交
105 106
  SDB_SET_RESERVE(pRaw, dataPos, MND_CONSUMER_RESERVE_SIZE, CM_ENCODE_OVER);
  SDB_SET_DATALEN(pRaw, dataPos, CM_ENCODE_OVER);
L
Liu Jicong 已提交
107

108 109
CM_ENCODE_OVER:
  if (terrno != 0) {
L
Liu Jicong 已提交
110
    mError("consumer:%ld, failed to encode to raw:%p since %s", pConsumer->consumerId, pRaw, terrstr());
111 112 113 114
    sdbFreeRaw(pRaw);
    return NULL;
  }

L
Liu Jicong 已提交
115
  mTrace("consumer:%ld, encode to raw:%p, row:%p", pConsumer->consumerId, pRaw, pConsumer);
L
Liu Jicong 已提交
116 117 118 119
  return pRaw;
}

static SSdbRow *mndConsumerActionDecode(SSdbRaw *pRaw) {
120 121
  terrno = TSDB_CODE_OUT_OF_MEMORY;

L
Liu Jicong 已提交
122
  int8_t sver = 0;
123
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto CONSUME_DECODE_OVER;
L
Liu Jicong 已提交
124 125 126

  if (sver != MND_CONSUMER_VER_NUMBER) {
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
127
    goto CONSUME_DECODE_OVER;
L
Liu Jicong 已提交
128 129
  }

L
Liu Jicong 已提交
130 131
  int32_t  size = sizeof(SMqConsumerObj);
  SSdbRow *pRow = sdbAllocRow(size);
L
Liu Jicong 已提交
132
  if (pRow == NULL) goto CONSUME_DECODE_OVER;
133

L
Liu Jicong 已提交
134
  SMqConsumerObj *pConsumer = sdbGetRowObj(pRow);
L
Liu Jicong 已提交
135
  if (pConsumer == NULL) goto CONSUME_DECODE_OVER;
L
Liu Jicong 已提交
136 137

  int32_t dataPos = 0;
L
Liu Jicong 已提交
138
  SDB_GET_INT64(pRaw, dataPos, &pConsumer->consumerId, CONSUME_DECODE_OVER);
L
Liu Jicong 已提交
139
  int32_t len, topicNum;
L
Liu Jicong 已提交
140 141 142
  SDB_GET_INT32(pRaw, dataPos, &len, CONSUME_DECODE_OVER);
  SDB_GET_BINARY(pRaw, dataPos, pConsumer->cgroup, len, CONSUME_DECODE_OVER);
  SDB_GET_INT32(pRaw, dataPos, &topicNum, CONSUME_DECODE_OVER);
L
Liu Jicong 已提交
143 144 145 146 147 148 149 150 151
  for (int i = 0; i < topicNum; i++) {
    int32_t           topicLen;
    SMqConsumerTopic *pConsumerTopic = malloc(sizeof(SMqConsumerTopic));
    if (pConsumerTopic == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      // TODO
      return NULL;
    }
    /*pConsumerTopic->vgroups = taosArrayInit(topicNum, sizeof(SMqConsumerTopic));*/
L
Liu Jicong 已提交
152 153
    SDB_GET_INT32(pRaw, dataPos, &topicLen, CONSUME_DECODE_OVER);
    SDB_GET_BINARY(pRaw, dataPos, pConsumerTopic->name, topicLen, CONSUME_DECODE_OVER);
L
Liu Jicong 已提交
154
    int32_t vgSize;
L
Liu Jicong 已提交
155
    SDB_GET_INT32(pRaw, dataPos, &vgSize, CONSUME_DECODE_OVER);
L
Liu Jicong 已提交
156
  }
L
Liu Jicong 已提交
157

158 159
CONSUME_DECODE_OVER:
  if (terrno != 0) {
L
Liu Jicong 已提交
160
    mError("consumer:%ld, failed to decode from raw:%p since %s", pConsumer->consumerId, pRaw, terrstr());
161 162
    tfree(pRow);
    return NULL;
L
Liu Jicong 已提交
163
  }
L
Liu Jicong 已提交
164

L
Liu Jicong 已提交
165
  /*SDB_GET_RESERVE(pRaw, dataPos, MND_CONSUMER_RESERVE_SIZE);*/
L
Liu Jicong 已提交
166 167 168 169

  return pRow;
}

L
Liu Jicong 已提交
170 171
static int32_t mndConsumerActionInsert(SSdb *pSdb, SMqConsumerObj *pConsumer) {
  mTrace("consumer:%ld, perform insert action", pConsumer->consumerId);
L
Liu Jicong 已提交
172 173 174
  return 0;
}

L
Liu Jicong 已提交
175 176
static int32_t mndConsumerActionDelete(SSdb *pSdb, SMqConsumerObj *pConsumer) {
  mTrace("consumer:%ld, perform delete action", pConsumer->consumerId);
L
Liu Jicong 已提交
177 178 179
  return 0;
}

L
Liu Jicong 已提交
180 181
static int32_t mndConsumerActionUpdate(SSdb *pSdb, SMqConsumerObj *pOldConsumer, SMqConsumerObj *pNewConsumer) {
  mTrace("consumer:%ld, perform update action", pOldConsumer->consumerId);
L
Liu Jicong 已提交
182 183

  // TODO handle update
L
Liu Jicong 已提交
184 185
  /*taosWLockLatch(&pOldConsumer->lock);*/
  /*taosWUnLockLatch(&pOldConsumer->lock);*/
L
Liu Jicong 已提交
186 187 188 189

  return 0;
}

L
Liu Jicong 已提交
190 191 192
SMqConsumerObj *mndAcquireConsumer(SMnode *pMnode, int32_t consumerId) {
  SSdb           *pSdb = pMnode->pSdb;
  SMqConsumerObj *pConsumer = sdbAcquire(pSdb, SDB_CONSUMER, &consumerId);
L
Liu Jicong 已提交
193 194 195 196 197 198
  if (pConsumer == NULL) {
    /*terrno = TSDB_CODE_MND_CONSUMER_NOT_EXIST;*/
  }
  return pConsumer;
}

L
Liu Jicong 已提交
199
void mndReleaseConsumer(SMnode *pMnode, SMqConsumerObj *pConsumer) {
L
Liu Jicong 已提交
200 201 202 203 204 205 206
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pConsumer);
}

static int32_t mndProcessSubscribeReq(SMnodeMsg *pMsg) {
  SMnode          *pMnode = pMsg->pMnode;
  char            *msgStr = pMsg->rpcMsg.pCont;
L
Liu Jicong 已提交
207 208 209 210
  SCMSubscribeReq subscribe;
  tDeserializeSCMSubscribeReq(msgStr, &subscribe);
  int64_t consumerId = subscribe.consumerId;
  char   *consumerGroup = subscribe.consumerGroup;
L
Liu Jicong 已提交
211
  int32_t cgroupLen = strlen(consumerGroup);
L
Liu Jicong 已提交
212

L
Liu Jicong 已提交
213
  SArray *newSub = NULL;
L
Liu Jicong 已提交
214
  int     newTopicNum = subscribe.topicNum;
L
Liu Jicong 已提交
215 216
  if (newTopicNum) {
    newSub = taosArrayInit(newTopicNum, sizeof(SMqConsumerTopic));
L
Liu Jicong 已提交
217
  }
L
Liu Jicong 已提交
218 219 220 221 222
  SMqConsumerTopic *pConsumerTopics = calloc(newTopicNum, sizeof(SMqConsumerTopic));
  if (pConsumerTopics == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
L
Liu Jicong 已提交
223
  for (int i = 0; i < newTopicNum; i++) {
L
Liu Jicong 已提交
224
    char             *newTopicName = taosArrayGetP(newSub, i);
L
Liu Jicong 已提交
225 226
    SMqConsumerTopic *pConsumerTopic = &pConsumerTopics[i];

L
Liu Jicong 已提交
227
    strcpy(pConsumerTopic->name, newTopicName);
L
Liu Jicong 已提交
228
    pConsumerTopic->vgroups = tdListNew(sizeof(int64_t));
L
Liu Jicong 已提交
229
  }
L
Liu Jicong 已提交
230 231 232

  taosArrayAddBatch(newSub, pConsumerTopics, newTopicNum);
  free(pConsumerTopics);
L
Liu Jicong 已提交
233
  taosArraySortString(newSub, taosArrayCompareString);
L
Liu Jicong 已提交
234

L
Liu Jicong 已提交
235 236
  SArray         *oldSub = NULL;
  int             oldTopicNum = 0;
L
Liu Jicong 已提交
237
  // create consumer if not exist
L
Liu Jicong 已提交
238 239 240 241 242 243 244 245
  SMqConsumerObj *pConsumer = mndAcquireConsumer(pMnode, consumerId);
  if (pConsumer == NULL) {
    // create consumer
    pConsumer = malloc(sizeof(SMqConsumerObj));
    if (pConsumer == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
L
Liu Jicong 已提交
246 247
    pConsumer->consumerId = consumerId;
    strcpy(pConsumer->cgroup, consumerGroup);
L
Liu Jicong 已提交
248

L
Liu Jicong 已提交
249 250 251 252 253 254
  } else {
    oldSub = pConsumer->topics;
    oldTopicNum = taosArrayGetSize(oldSub);
  }
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, &pMsg->rpcMsg);
  if (pTrans == NULL) {
L
Liu Jicong 已提交
255
    //TODO: free memory
L
Liu Jicong 已提交
256 257
    return -1;
  }
L
Liu Jicong 已提交
258

L
Liu Jicong 已提交
259 260 261 262 263 264 265 266 267 268
  int i = 0, j = 0;
  while (i < newTopicNum || j < oldTopicNum) {
    SMqConsumerTopic *pOldTopic = NULL;
    SMqConsumerTopic *pNewTopic = NULL;
    if (i >= newTopicNum) {
      // encode unset topic msg to all vnodes related to that topic
      pOldTopic = taosArrayGet(oldSub, j);
      j++;
    } else if (j >= oldTopicNum) {
      pNewTopic = taosArrayGet(newSub, i);
L
Liu Jicong 已提交
269
      i++;
L
Liu Jicong 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
    } else {
      pNewTopic = taosArrayGet(newSub, i);
      pOldTopic = taosArrayGet(oldSub, j);

      char *newName = pNewTopic->name;
      char *oldName = pOldTopic->name;
      int   comp = compareLenPrefixedStr(newName, oldName);
      if (comp == 0) {
        // do nothing
        pOldTopic = pNewTopic = NULL;
        i++;
        j++;
        continue;
      } else if (comp < 0) {
        pOldTopic = NULL;
        i++;
      } else {
        pNewTopic = NULL;
        j++;
      }
    }
L
Liu Jicong 已提交
291

L
Liu Jicong 已提交
292
    if (pOldTopic != NULL) {
L
Liu Jicong 已提交
293
      //cancel subscribe of that old topic
L
Liu Jicong 已提交
294 295 296 297 298 299 300 301 302
      ASSERT(pNewTopic == NULL);
      char     *oldTopicName = pOldTopic->name;
      SList    *vgroups = pOldTopic->vgroups;
      SListIter iter;
      tdListInitIter(vgroups, &iter, TD_LIST_FORWARD);
      SListNode *pn;

      SMqTopicObj *pTopic = mndAcquireTopic(pMnode, oldTopicName);
      ASSERT(pTopic != NULL);
L
Liu Jicong 已提交
303
      SMqCGroup *pGroup = taosHashGet(pTopic->cgroups, consumerGroup, cgroupLen);
L
Liu Jicong 已提交
304 305
      while ((pn = tdListNext(&iter)) != NULL) {
        int32_t vgId = *(int64_t *)pn->data;
L
Liu Jicong 已提交
306
        // acquire and get epset
L
Liu Jicong 已提交
307
        SVgObj *pVgObj = mndAcquireVgroup(pMnode, vgId);
L
Liu Jicong 已提交
308
        // TODO what time to release?
L
Liu Jicong 已提交
309 310 311 312
        if (pVgObj == NULL) {
          // TODO handle error
          continue;
        }
L
Liu Jicong 已提交
313
        //build reset msg
L
Liu Jicong 已提交
314
        void *pMqVgSetReq = mndBuildMqVGroupSetReq(pMnode, oldTopicName, vgId, consumerId, consumerGroup);
L
Liu Jicong 已提交
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
        // TODO:serialize
        if (pMsg == NULL) {
          terrno = TSDB_CODE_OUT_OF_MEMORY;
          return -1;
        }
        STransAction action = {0};
        action.epSet = mndGetVgroupEpset(pMnode, pVgObj);
        action.pCont = pMqVgSetReq;
        action.contLen = 0;  // TODO
        action.msgType = TDMT_VND_MQ_SET_CONN;
        if (mndTransAppendRedoAction(pTrans, &action) != 0) {
          free(pMqVgSetReq);
          mndTransDrop(pTrans);
          // TODO free
          return -1;
        }
      }
L
Liu Jicong 已提交
332
      //delete data in mnode
L
Liu Jicong 已提交
333 334
      taosHashRemove(pTopic->cgroups, consumerGroup, cgroupLen);
      mndReleaseTopic(pMnode, pTopic);
L
Liu Jicong 已提交
335 336

    } else if (pNewTopic != NULL) {
L
Liu Jicong 已提交
337
      // save subscribe info to mnode
L
Liu Jicong 已提交
338 339 340 341 342 343
      ASSERT(pOldTopic == NULL);

      char        *newTopicName = pNewTopic->name;
      SMqTopicObj *pTopic = mndAcquireTopic(pMnode, newTopicName);
      ASSERT(pTopic != NULL);

L
Liu Jicong 已提交
344
      SMqCGroup *pGroup = taosHashGet(pTopic->cgroups, consumerGroup, cgroupLen);
L
Liu Jicong 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
      if (pGroup == NULL) {
        // add new group
        pGroup = malloc(sizeof(SMqCGroup));
        if (pGroup == NULL) {
          terrno = TSDB_CODE_OUT_OF_MEMORY;
          return -1;
        }
        pGroup->consumerIds = tdListNew(sizeof(int64_t));
        if (pGroup->consumerIds == NULL) {
          free(pGroup);
          terrno = TSDB_CODE_OUT_OF_MEMORY;
          return -1;
        }
        pGroup->status = 0;
        // add into cgroups
L
Liu Jicong 已提交
360
        taosHashPut(pTopic->cgroups, consumerGroup, cgroupLen, pGroup, sizeof(SMqCGroup));
L
Liu Jicong 已提交
361
      }
L
Liu Jicong 已提交
362
      /*taosHashPut(pTopic->consumers, &pConsumer->consumerId, sizeof(int64_t), pConsumer, sizeof(SMqConsumerObj));*/
L
Liu Jicong 已提交
363 364 365

      // put the consumer into list
      // rebalance will be triggered by timer
L
Liu Jicong 已提交
366
      tdListAppend(pGroup->consumerIds, &consumerId);
L
Liu Jicong 已提交
367 368 369 370 371

      SSdbRaw *pTopicRaw = mndTopicActionEncode(pTopic);
      sdbSetRawStatus(pTopicRaw, SDB_STATUS_READY);
      // TODO: error handling
      mndTransAppendRedolog(pTrans, pTopicRaw);
L
Liu Jicong 已提交
372 373 374

      mndReleaseTopic(pMnode, pTopic);

L
Liu Jicong 已提交
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
    } else {
      ASSERT(0);
    }
  }
  // destroy old sub
  taosArrayDestroy(oldSub);
  // put new sub into consumerobj
  pConsumer->topics = newSub;

  // persist consumerObj
  SSdbRaw *pConsumerRaw = mndConsumerActionEncode(pConsumer);
  sdbSetRawStatus(pConsumerRaw, SDB_STATUS_READY);
  // TODO: error handling
  mndTransAppendRedolog(pTrans, pConsumerRaw);

  if (mndTransPrepare(pMnode, pTrans) != 0) {
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
L
Liu Jicong 已提交
393
    mndReleaseConsumer(pMnode, pConsumer);
L
Liu Jicong 已提交
394 395
    return -1;
  }
L
Liu Jicong 已提交
396

L
Liu Jicong 已提交
397 398
  // TODO: free memory
  mndTransDrop(pTrans);
L
Liu Jicong 已提交
399
  mndReleaseConsumer(pMnode, pConsumer);
L
Liu Jicong 已提交
400 401 402
  return 0;
}

L
Liu Jicong 已提交
403 404
static int32_t mndProcessSubscribeInternalRsp(SMnodeMsg *pMsg) { return 0; }

L
Liu Jicong 已提交
405 406
static int32_t mndProcessConsumerMetaMsg(SMnodeMsg *pMsg) {
  SMnode        *pMnode = pMsg->pMnode;
S
Shengliang Guan 已提交
407
  STableInfoReq *pInfo = pMsg->rpcMsg.pCont;
L
Liu Jicong 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428

  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;
S
Shengliang Guan 已提交
429
  int32_t contLen = sizeof(STableMetaRsp) + totalCols * sizeof(SSchema);
L
Liu Jicong 已提交
430

S
Shengliang Guan 已提交
431
  STableMetaRsp *pMeta = rpcMallocCont(contLen);
L
Liu Jicong 已提交
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
  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) {
L
Liu Jicong 已提交
482
    SMqConsumerObj *pConsumer = NULL;
L
Liu Jicong 已提交
483 484 485
    pIter = sdbFetch(pSdb, SDB_CONSUMER, pIter, (void **)&pConsumer);
    if (pIter == NULL) break;

L
Liu Jicong 已提交
486
    numOfConsumers++;
L
Liu Jicong 已提交
487 488 489 490 491 492 493 494

    sdbRelease(pSdb, pConsumer);
  }

  *pNumOfConsumers = numOfConsumers;
  return 0;
}

S
Shengliang Guan 已提交
495
static int32_t mndGetConsumerMeta(SMnodeMsg *pMsg, SShowObj *pShow, STableMetaRsp *pMeta) {
L
Liu Jicong 已提交
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
  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;
}

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