mndSubscribe.c 29.0 KB
Newer Older
L
Liu Jicong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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/>.
 */
L
Liu Jicong 已提交
15
#define _DEFAULT_SOURCE
L
Liu Jicong 已提交
16

L
Liu Jicong 已提交
17
#include "mndSubscribe.h"
L
Liu Jicong 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#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 "tcompare.h"
#include "tname.h"

#define MND_SUBSCRIBE_VER_NUMBER 1
#define MND_SUBSCRIBE_RESERVE_SIZE 64

L
Liu Jicong 已提交
34 35
#define MND_SUBSCRIBE_REBALANCE_MS 5000

L
Liu Jicong 已提交
36 37
static char *mndMakeSubscribeKey(char *cgroup, char *topicName);

L
Liu Jicong 已提交
38 39 40 41 42 43 44 45 46 47
static SSdbRaw *mndSubActionEncode(SMqSubscribeObj *);
static SSdbRow *mndSubActionDecode(SSdbRaw *pRaw);
static int32_t  mndSubActionInsert(SSdb *pSdb, SMqSubscribeObj *);
static int32_t  mndSubActionDelete(SSdb *pSdb, SMqSubscribeObj *);
static int32_t  mndSubActionUpdate(SSdb *pSdb, SMqSubscribeObj *pOldSub, SMqSubscribeObj *pNewSub);

static int32_t mndProcessSubscribeReq(SMnodeMsg *pMsg);
static int32_t mndProcessSubscribeRsp(SMnodeMsg *pMsg);
static int32_t mndProcessSubscribeInternalReq(SMnodeMsg *pMsg);
static int32_t mndProcessSubscribeInternalRsp(SMnodeMsg *pMsg);
L
Liu Jicong 已提交
48
static int32_t mndProcessMqTimerMsg(SMnodeMsg *pMsg);
L
Liu Jicong 已提交
49
static int32_t mndProcessGetSubEpReq(SMnodeMsg *pMsg);
L
Liu Jicong 已提交
50 51

static int mndBuildMqSetConsumerVgReq(SMnode *pMnode, STrans *pTrans, SMqConsumerObj *pConsumer,
L
Liu Jicong 已提交
52 53
                                      SMqConsumerTopic *pConsumerTopic, SMqTopicObj *pTopic, SMqConsumerEp *pSub,
                                      int64_t oldConsumerId);
L
Liu Jicong 已提交
54 55 56 57 58 59 60 61 62 63 64

int32_t mndInitSubscribe(SMnode *pMnode) {
  SSdbTable table = {.sdbType = SDB_SUBSCRIBE,
                     .keyType = SDB_KEY_BINARY,
                     .encodeFp = (SdbEncodeFp)mndSubActionEncode,
                     .decodeFp = (SdbDecodeFp)mndSubActionDecode,
                     .insertFp = (SdbInsertFp)mndSubActionInsert,
                     .updateFp = (SdbUpdateFp)mndSubActionUpdate,
                     .deleteFp = (SdbDeleteFp)mndSubActionDelete};

  mndSetMsgHandle(pMnode, TDMT_MND_SUBSCRIBE, mndProcessSubscribeReq);
L
Liu Jicong 已提交
65
  mndSetMsgHandle(pMnode, TDMT_VND_MQ_SET_CONN_RSP, mndProcessSubscribeInternalRsp);
L
Liu Jicong 已提交
66
  mndSetMsgHandle(pMnode, TDMT_MND_MQ_TIMER, mndProcessMqTimerMsg);
L
Liu Jicong 已提交
67
  mndSetMsgHandle(pMnode, TDMT_MND_GET_SUB_EP, mndProcessGetSubEpReq);
L
Liu Jicong 已提交
68 69 70
  return sdbSetTable(pMnode->pSdb, table);
}

L
Liu Jicong 已提交
71 72
static int32_t mndProcessGetSubEpReq(SMnodeMsg *pMsg) {
  SMnode           *pMnode = pMsg->pMnode;
L
Liu Jicong 已提交
73
  SMqCMGetSubEpReq *pReq = (SMqCMGetSubEpReq *)pMsg->rpcMsg.pCont;
L
Liu Jicong 已提交
74
  SMqCMGetSubEpRsp  rsp = {0};
L
Liu Jicong 已提交
75
  int64_t           consumerId = be64toh(pReq->consumerId);
L
Liu Jicong 已提交
76
  int64_t           currentTs = taosGetTimestampMs();
L
Liu Jicong 已提交
77 78 79

  SMqConsumerObj *pConsumer = mndAcquireConsumer(pMsg->pMnode, consumerId);
  if (pConsumer == NULL) {
L
Liu Jicong 已提交
80
    terrno = TSDB_CODE_MND_CONSUMER_NOT_EXIST;
L
Liu Jicong 已提交
81 82 83 84 85 86
    return -1;
  }
  ASSERT(strcmp(pReq->cgroup, pConsumer->cgroup) == 0);

  strcpy(rsp.cgroup, pReq->cgroup);
  rsp.consumerId = consumerId;
L
Liu Jicong 已提交
87
  rsp.epoch = pConsumer->epoch;
L
Liu Jicong 已提交
88 89 90 91 92 93 94 95 96
  SArray *pTopics = pConsumer->topics;
  int32_t sz = taosArrayGetSize(pTopics);
  rsp.topics = taosArrayInit(sz, sizeof(SMqSubTopicEp));
  for (int32_t i = 0; i < sz; i++) {
    SMqSubTopicEp     topicEp;
    SMqConsumerTopic *pConsumerTopic = taosArrayGet(pTopics, i);
    strcpy(topicEp.topic, pConsumerTopic->name);

    SMqSubscribeObj *pSub = mndAcquireSubscribe(pMnode, pConsumer->cgroup, pConsumerTopic->name);
L
Liu Jicong 已提交
97 98 99 100
    ASSERT(pSub);
    bool found = 0;
    bool changed = 0;
    for (int32_t j = 0; j < taosArrayGetSize(pSub->availConsumer); j++) {
L
Liu Jicong 已提交
101
      if (*(int64_t *)taosArrayGet(pSub->availConsumer, j) == consumerId) {
L
Liu Jicong 已提交
102 103 104 105 106 107 108 109
        found = 1;
        break;
      }
    }
    if (found == 0) {
      taosArrayPush(pSub->availConsumer, &consumerId);
    }

L
Liu Jicong 已提交
110
    int32_t assignedSz = taosArrayGetSize(pSub->assigned);
L
Liu Jicong 已提交
111 112
    topicEp.vgs = taosArrayInit(assignedSz, sizeof(SMqSubVgEp));
    for (int32_t j = 0; j < assignedSz; j++) {
L
Liu Jicong 已提交
113
      SMqConsumerEp *pCEp = taosArrayGet(pSub->assigned, j);
L
Liu Jicong 已提交
114
      if (pCEp->consumerId == consumerId) {
L
Liu Jicong 已提交
115
        pCEp->lastConsumerHbTs = currentTs;
L
Liu Jicong 已提交
116
        SMqSubVgEp vgEp = {.epSet = pCEp->epSet, .vgId = pCEp->vgId};
L
Liu Jicong 已提交
117
        taosArrayPush(topicEp.vgs, &vgEp);
L
Liu Jicong 已提交
118
        changed = 1;
L
Liu Jicong 已提交
119 120 121 122 123
      }
    }
    if (taosArrayGetSize(topicEp.vgs) != 0) {
      taosArrayPush(rsp.topics, &topicEp);
    }
L
Liu Jicong 已提交
124
    if (changed || found) {
L
Liu Jicong 已提交
125
      SSdbRaw *pRaw = mndSubActionEncode(pSub);
L
Liu Jicong 已提交
126
      sdbSetRawStatus(pRaw, SDB_STATUS_READY);
L
Liu Jicong 已提交
127
      sdbWrite(pMnode->pSdb, pRaw);
L
Liu Jicong 已提交
128 129
    }
    mndReleaseSubscribe(pMnode, pSub);
L
Liu Jicong 已提交
130 131
  }
  int32_t tlen = tEncodeSMqCMGetSubEpRsp(NULL, &rsp);
L
Liu Jicong 已提交
132
  void   *buf = rpcMallocCont(tlen);
L
Liu Jicong 已提交
133 134 135 136 137 138
  if (buf == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  void *abuf = buf;
  tEncodeSMqCMGetSubEpRsp(&abuf, &rsp);
L
Liu Jicong 已提交
139
  tDeleteSMqCMGetSubEpRsp(&rsp);
L
Liu Jicong 已提交
140 141 142 143 144
  pMsg->pCont = buf;
  pMsg->contLen = tlen;
  return 0;
}

L
Liu Jicong 已提交
145 146 147 148 149 150
static int32_t mndSplitSubscribeKey(char *key, char **topic, char **cgroup) {
  int i = 0;
  while (key[i] != ':') {
    i++;
  }
  key[i] = 0;
L
Liu Jicong 已提交
151
  *cgroup = strdup(key);
L
Liu Jicong 已提交
152
  key[i] = ':';
L
Liu Jicong 已提交
153
  *topic = strdup(&key[i + 1]);
L
Liu Jicong 已提交
154 155 156 157 158 159 160 161
  return 0;
}

static int32_t mndProcessMqTimerMsg(SMnodeMsg *pMsg) {
  SMnode          *pMnode = pMsg->pMnode;
  SSdb            *pSdb = pMnode->pSdb;
  SMqSubscribeObj *pSub = NULL;
  void            *pIter = sdbFetch(pSdb, SDB_SUBSCRIBE, NULL, (void **)&pSub);
L
Liu Jicong 已提交
162 163
  int64_t          currentTs = taosGetTimestampMs();
  int32_t          sz;
L
Liu Jicong 已提交
164
  while (pIter != NULL) {
L
Liu Jicong 已提交
165
    for (int i = 0; i < taosArrayGetSize(pSub->assigned); i++) {
L
Liu Jicong 已提交
166 167 168
      SMqConsumerEp *pCEp = taosArrayGet(pSub->assigned, i);
      int64_t        consumerId = pCEp->consumerId;
      if (pCEp->lastConsumerHbTs != -1 && currentTs - pCEp->lastConsumerHbTs > MND_SUBSCRIBE_REBALANCE_MS) {
L
Liu Jicong 已提交
169 170
        // put consumer into lostConsumer
        taosArrayPush(pSub->lostConsumer, pCEp);
L
Liu Jicong 已提交
171
        // put vg into unassigned
L
Liu Jicong 已提交
172 173 174 175 176 177
        taosArrayPush(pSub->unassignedVg, pCEp);
        // remove from assigned
        // TODO: swap with last one, reduce size and reset i
        taosArrayRemove(pSub->assigned, i);
        // remove from available consumer
        for (int j = 0; j < taosArrayGetSize(pSub->availConsumer); j++) {
L
Liu Jicong 已提交
178
          if (*(int64_t *)taosArrayGet(pSub->availConsumer, i) == pCEp->consumerId) {
L
Liu Jicong 已提交
179 180 181 182 183
            taosArrayRemove(pSub->availConsumer, j);
            break;
          }
          // TODO: acquire consumer, set status to unavail
        }
L
Liu Jicong 已提交
184
#if 0
L
Liu Jicong 已提交
185 186
        SMqConsumerObj* pConsumer = mndAcquireConsumer(pMnode, consumerId);
        pConsumer->epoch++;
L
Liu Jicong 已提交
187
        printf("current epoch %ld size %ld", pConsumer->epoch, pConsumer->topics->size);
L
Liu Jicong 已提交
188 189 190 191
        SSdbRaw* pRaw = mndConsumerActionEncode(pConsumer);
        sdbSetRawStatus(pRaw, SDB_STATUS_READY);
        sdbWriteNotFree(pMnode->pSdb, pRaw);
        mndReleaseConsumer(pMnode, pConsumer);
L
Liu Jicong 已提交
192
#endif
L
Liu Jicong 已提交
193 194 195
      }
    }
    if ((sz = taosArrayGetSize(pSub->unassignedVg)) > 0 && taosArrayGetSize(pSub->availConsumer) > 0) {
L
Liu Jicong 已提交
196 197 198 199 200 201 202 203
      char *topic = NULL;
      char *cgroup = NULL;
      mndSplitSubscribeKey(pSub->key, &topic, &cgroup);

      SMqTopicObj *pTopic = mndAcquireTopic(pMnode, topic);

      // create trans
      STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, &pMsg->rpcMsg);
L
Liu Jicong 已提交
204
      for (int32_t i = 0; i < sz; i++) {
L
Liu Jicong 已提交
205 206
        int64_t        consumerId = *(int64_t *)taosArrayGet(pSub->availConsumer, pSub->nextConsumerIdx);
        SMqConsumerEp *pCEp = taosArrayPop(pSub->unassignedVg);
L
Liu Jicong 已提交
207
        int64_t        oldConsumerId = pCEp->consumerId;
L
Liu Jicong 已提交
208 209
        pCEp->consumerId = consumerId;
        taosArrayPush(pSub->assigned, pCEp);
L
Liu Jicong 已提交
210
        pSub->nextConsumerIdx = (pSub->nextConsumerIdx + 1) % taosArrayGetSize(pSub->availConsumer);
L
Liu Jicong 已提交
211

L
Liu Jicong 已提交
212
        SMqConsumerObj *pConsumer = mndAcquireConsumer(pMnode, consumerId);
L
Liu Jicong 已提交
213 214 215 216 217 218
        pConsumer->epoch++;
        /*SSdbRaw* pConsumerRaw = mndConsumerActionEncode(pConsumer);*/
        /*sdbSetRawStatus(pConsumerRaw, SDB_STATUS_READY);*/
        /*sdbWriteNotFree(pMnode->pSdb, pConsumerRaw);*/
        mndReleaseConsumer(pMnode, pConsumer);

L
Liu Jicong 已提交
219
        // build msg
L
Liu Jicong 已提交
220

L
Liu Jicong 已提交
221 222 223 224 225 226 227
        SMqSetCVgReq req = {0};
        strcpy(req.cgroup, cgroup);
        strcpy(req.topicName, topic);
        req.sql = pTopic->sql;
        req.logicalPlan = pTopic->logicalPlan;
        req.physicalPlan = pTopic->physicalPlan;
        req.qmsg = pCEp->qmsg;
L
Liu Jicong 已提交
228
        req.oldConsumerId = oldConsumerId;
L
Liu Jicong 已提交
229
        req.newConsumerId = consumerId;
L
Liu Jicong 已提交
230
        req.vgId = pCEp->vgId;
L
Liu Jicong 已提交
231
        int32_t tlen = tEncodeSMqSetCVgReq(NULL, &req);
L
Liu Jicong 已提交
232
        void   *buf = malloc(sizeof(SMsgHead) + tlen);
L
Liu Jicong 已提交
233
        if (buf == NULL) {
L
Liu Jicong 已提交
234 235 236
          terrno = TSDB_CODE_OUT_OF_MEMORY;
          return -1;
        }
L
Liu Jicong 已提交
237 238 239 240 241 242
        SMsgHead *pMsgHead = (SMsgHead *)buf;

        pMsgHead->contLen = htonl(sizeof(SMsgHead) + tlen);
        pMsgHead->vgId = htonl(pCEp->vgId);
        void *abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
        tEncodeSMqSetCVgReq(&abuf, &req);
L
Liu Jicong 已提交
243 244

        // persist msg
L
Liu Jicong 已提交
245
        // TODO: no need for txn
L
Liu Jicong 已提交
246
        STransAction action = {0};
247
        action.epSet = pCEp->epSet;
L
Liu Jicong 已提交
248
        action.pCont = buf;
L
Liu Jicong 已提交
249
        action.contLen = sizeof(SMsgHead) + tlen;
L
Liu Jicong 已提交
250 251 252 253 254
        action.msgType = TDMT_VND_MQ_SET_CONN;
        mndTransAppendRedoAction(pTrans, &action);

        // persist raw
        SSdbRaw *pRaw = mndSubActionEncode(pSub);
L
Liu Jicong 已提交
255
        sdbSetRawStatus(pRaw, SDB_STATUS_READY);
L
Liu Jicong 已提交
256 257 258 259 260 261 262 263
        mndTransAppendRedolog(pTrans, pRaw);

        tfree(topic);
        tfree(cgroup);
      }
      if (mndTransPrepare(pMnode, pTrans) != 0) {
        mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
      }
L
Liu Jicong 已提交
264
      mndReleaseTopic(pMnode, pTopic);
L
Liu Jicong 已提交
265 266
      mndTransDrop(pTrans);
    }
L
Liu Jicong 已提交
267
    pIter = sdbFetch(pSdb, SDB_SUBSCRIBE, pIter, (void **)&pSub);
L
Liu Jicong 已提交
268 269 270 271
  }
  return 0;
}

L
Liu Jicong 已提交
272
static int mndInitUnassignedVg(SMnode *pMnode, SMqTopicObj *pTopic, SArray *unassignedVg) {
L
Liu Jicong 已提交
273
  // convert phyplan to dag
L
Liu Jicong 已提交
274
  SQueryDag *pDag = qStringToDag(pTopic->physicalPlan);
L
Liu Jicong 已提交
275
  SArray    *pArray = NULL;
L
Liu Jicong 已提交
276 277
  SArray    *inner = taosArrayGet(pDag->pSubplans, 0);
  SSubplan  *plan = taosArrayGetP(inner, 0);
L
Liu Jicong 已提交
278 279
  SSdb      *pSdb = pMnode->pSdb;
  SVgObj    *pVgroup = NULL;
H
Haojun Liao 已提交
280

L
Liu Jicong 已提交
281 282 283 284 285
  void *pIter = NULL;
  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
    if (pVgroup->dbUid != pTopic->dbUid) continue;
H
Haojun Liao 已提交
286

L
Liu Jicong 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299
    plan->execNode.nodeId = pVgroup->vgId;
    plan->execNode.epset = mndGetVgroupEpset(pMnode, pVgroup);

    if (schedulerConvertDagToTaskList(pDag, &pArray) < 0) {
      terrno = TSDB_CODE_MND_UNSUPPORTED_TOPIC;
      mError("unsupport topic: %s, sql: %s", pTopic->name, pTopic->sql);
      return -1;
    }
    if (pArray && taosArrayGetSize(pArray) != 1) {
      terrno = TSDB_CODE_MND_UNSUPPORTED_TOPIC;
      mError("unsupport topic: %s, sql: %s, plan level: %ld", pTopic->name, pTopic->sql, taosArrayGetSize(pArray));
      return -1;
    }
L
Liu Jicong 已提交
300

L
Liu Jicong 已提交
301
    SMqConsumerEp CEp = {0};
L
Liu Jicong 已提交
302
    CEp.status = 0;
L
Liu Jicong 已提交
303
    CEp.consumerId = -1;
304
    CEp.lastConsumerHbTs = CEp.lastVgHbTs = -1;
L
Liu Jicong 已提交
305
    STaskInfo *pTaskInfo = taosArrayGet(pArray, 0);
H
Haojun Liao 已提交
306
    CEp.epSet = pTaskInfo->addr.epset;
307
    CEp.vgId = pTaskInfo->addr.nodeId;
L
Liu Jicong 已提交
308 309

    ASSERT(CEp.vgId == pVgroup->vgId);
L
Liu Jicong 已提交
310
    CEp.qmsg = strdup(pTaskInfo->msg->msg);
311
    taosArrayPush(unassignedVg, &CEp);
L
Liu Jicong 已提交
312
    // TODO: free taskInfo
L
Liu Jicong 已提交
313 314 315 316 317
    taosArrayDestroy(pArray);

    /*SEpSet *pEpSet = &plan->execNode.epset;*/
    /*pEpSet->inUse = 0;*/
    /*addEpIntoEpSet(pEpSet, "localhost", 6030);*/
L
Liu Jicong 已提交
318
  }
319

L
Liu Jicong 已提交
320
  /*qDestroyQueryDag(pDag);*/
321
  return 0;
L
Liu Jicong 已提交
322 323 324
}

static int mndBuildMqSetConsumerVgReq(SMnode *pMnode, STrans *pTrans, SMqConsumerObj *pConsumer,
L
Liu Jicong 已提交
325 326
                                      SMqConsumerTopic *pConsumerTopic, SMqTopicObj *pTopic, SMqConsumerEp *pCEp,
                                      int64_t oldConsumerId) {
L
Liu Jicong 已提交
327 328 329 330 331 332
  int32_t sz = taosArrayGetSize(pConsumerTopic->pVgInfo);
  for (int32_t i = 0; i < sz; i++) {
    int32_t      vgId = *(int32_t *)taosArrayGet(pConsumerTopic->pVgInfo, i);
    SVgObj      *pVgObj = mndAcquireVgroup(pMnode, vgId);
    SMqSetCVgReq req = {
        .vgId = vgId,
L
Liu Jicong 已提交
333
        .oldConsumerId = oldConsumerId,
L
Liu Jicong 已提交
334
        .newConsumerId = pConsumer->consumerId,
L
Liu Jicong 已提交
335
    };
L
Liu Jicong 已提交
336
    strcpy(req.cgroup, pConsumer->cgroup);
L
Liu Jicong 已提交
337
    strcpy(req.topicName, pTopic->name);
L
Liu Jicong 已提交
338 339 340
    req.sql = pTopic->sql;
    req.logicalPlan = pTopic->logicalPlan;
    req.physicalPlan = pTopic->physicalPlan;
L
Liu Jicong 已提交
341
    req.qmsg = pCEp->qmsg;
L
Liu Jicong 已提交
342
    int32_t tlen = tEncodeSMqSetCVgReq(NULL, &req);
L
Liu Jicong 已提交
343 344
    void   *buf = malloc(sizeof(SMsgHead) + tlen);
    if (buf == NULL) {
L
Liu Jicong 已提交
345 346 347
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
L
Liu Jicong 已提交
348

L
Liu Jicong 已提交
349
    SMsgHead *pMsgHead = (SMsgHead *)buf;
L
Liu Jicong 已提交
350 351 352 353

    pMsgHead->contLen = htonl(sizeof(SMsgHead) + tlen);
    pMsgHead->vgId = htonl(vgId);

L
Liu Jicong 已提交
354
    void *abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
L
Liu Jicong 已提交
355
    tEncodeSMqSetCVgReq(&abuf, &req);
L
Liu Jicong 已提交
356 357 358

    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgObj);
L
Liu Jicong 已提交
359
    action.pCont = buf;
L
Liu Jicong 已提交
360
    action.contLen = sizeof(SMsgHead) + tlen;
L
Liu Jicong 已提交
361 362 363 364
    action.msgType = TDMT_VND_MQ_SET_CONN;

    mndReleaseVgroup(pMnode, pVgObj);
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
L
Liu Jicong 已提交
365
      free(buf);
L
Liu Jicong 已提交
366 367 368 369 370 371 372 373 374
      return -1;
    }
  }
  return 0;
}

void mndCleanupSubscribe(SMnode *pMnode) {}

static SSdbRaw *mndSubActionEncode(SMqSubscribeObj *pSub) {
L
Liu Jicong 已提交
375
  terrno = TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
376
  void* buf = NULL;
L
Liu Jicong 已提交
377
  int32_t tlen = tEncodeSubscribeObj(NULL, pSub);
L
Liu Jicong 已提交
378
  int32_t size = sizeof(int32_t) + tlen + MND_SUBSCRIBE_RESERVE_SIZE;
L
Liu Jicong 已提交
379 380 381 382

  SSdbRaw *pRaw = sdbAllocRaw(SDB_SUBSCRIBE, MND_SUBSCRIBE_VER_NUMBER, size);
  if (pRaw == NULL) goto SUB_ENCODE_OVER;

L
Liu Jicong 已提交
383
  buf = malloc(tlen);
L
Liu Jicong 已提交
384
  if (buf == NULL) goto SUB_ENCODE_OVER;
L
Liu Jicong 已提交
385

L
Liu Jicong 已提交
386 387
  void *abuf = buf;
  tEncodeSubscribeObj(&abuf, pSub);
L
Liu Jicong 已提交
388 389 390 391 392 393 394

  int32_t dataPos = 0;
  SDB_SET_INT32(pRaw, dataPos, tlen, SUB_ENCODE_OVER);
  SDB_SET_BINARY(pRaw, dataPos, buf, tlen, SUB_ENCODE_OVER);
  SDB_SET_RESERVE(pRaw, dataPos, MND_SUBSCRIBE_RESERVE_SIZE, SUB_ENCODE_OVER);
  SDB_SET_DATALEN(pRaw, dataPos, SUB_ENCODE_OVER);

L
Liu Jicong 已提交
395 396
  terrno = TSDB_CODE_SUCCESS;

L
Liu Jicong 已提交
397
SUB_ENCODE_OVER:
L
Liu Jicong 已提交
398
  tfree(buf);
L
Liu Jicong 已提交
399 400 401 402 403 404 405 406 407 408 409 410
  if (terrno != 0) {
    mError("subscribe:%s, failed to encode to raw:%p since %s", pSub->key, pRaw, terrstr());
    sdbFreeRaw(pRaw);
    return NULL;
  }

  mTrace("subscribe:%s, encode to raw:%p, row:%p", pSub->key, pRaw, pSub);
  return pRaw;
}

static SSdbRow *mndSubActionDecode(SSdbRaw *pRaw) {
  terrno = TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
411
  void* buf = NULL;
L
Liu Jicong 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429

  int8_t sver = 0;
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto SUB_DECODE_OVER;

  if (sver != MND_SUBSCRIBE_VER_NUMBER) {
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
    goto SUB_DECODE_OVER;
  }

  int32_t  size = sizeof(SMqSubscribeObj);
  SSdbRow *pRow = sdbAllocRow(size);
  if (pRow == NULL) goto SUB_DECODE_OVER;

  SMqSubscribeObj *pSub = sdbGetRowObj(pRow);
  if (pSub == NULL) goto SUB_DECODE_OVER;

  int32_t dataPos = 0;
  int32_t tlen;
L
Liu Jicong 已提交
430
  SDB_GET_INT32(pRaw, dataPos, &tlen, SUB_DECODE_OVER);
L
Liu Jicong 已提交
431
  buf = malloc(tlen + 1);
L
Liu Jicong 已提交
432 433 434 435 436 437 438 439
  if (buf == NULL) goto SUB_DECODE_OVER;
  SDB_GET_BINARY(pRaw, dataPos, buf, tlen, SUB_DECODE_OVER);
  SDB_GET_RESERVE(pRaw, dataPos, MND_SUBSCRIBE_RESERVE_SIZE, SUB_DECODE_OVER);

  if (tDecodeSubscribeObj(buf, pSub) == NULL) {
    goto SUB_DECODE_OVER;
  }

L
Liu Jicong 已提交
440 441
  terrno = TSDB_CODE_SUCCESS;

L
Liu Jicong 已提交
442
SUB_DECODE_OVER:
L
Liu Jicong 已提交
443
  tfree(buf);
L
Liu Jicong 已提交
444
  if (terrno != TSDB_CODE_SUCCESS) {
L
Liu Jicong 已提交
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
    mError("subscribe:%s, failed to decode from raw:%p since %s", pSub->key, pRaw, terrstr());
    // TODO free subscribeobj
    tfree(pRow);
    return NULL;
  }

  return pRow;
}

static int32_t mndSubActionInsert(SSdb *pSdb, SMqSubscribeObj *pSub) {
  mTrace("subscribe:%s, perform insert action", pSub->key);
  return 0;
}

static int32_t mndSubActionDelete(SSdb *pSdb, SMqSubscribeObj *pSub) {
  mTrace("subscribe:%s, perform delete action", pSub->key);
L
Liu Jicong 已提交
461
  tDeleteSMqSubscribeObj(pSub);
L
Liu Jicong 已提交
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 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
  return 0;
}

static int32_t mndSubActionUpdate(SSdb *pSdb, SMqSubscribeObj *pOldSub, SMqSubscribeObj *pNewSub) {
  mTrace("subscribe:%s, perform update action", pOldSub->key);
  return 0;
}

static char *mndMakeSubscribeKey(char *cgroup, char *topicName) {
  char *key = malloc(TSDB_SHOW_SUBQUERY_LEN);
  if (key == NULL) {
    return NULL;
  }
  int tlen = strlen(cgroup);
  memcpy(key, cgroup, tlen);
  key[tlen] = ':';
  strcpy(key + tlen + 1, topicName);
  return key;
}

SMqSubscribeObj *mndAcquireSubscribe(SMnode *pMnode, char *cgroup, char *topicName) {
  SSdb            *pSdb = pMnode->pSdb;
  char            *key = mndMakeSubscribeKey(cgroup, topicName);
  SMqSubscribeObj *pSub = sdbAcquire(pSdb, SDB_SUBSCRIBE, key);
  free(key);
  if (pSub == NULL) {
    /*terrno = TSDB_CODE_MND_CONSUMER_NOT_EXIST;*/
  }
  return pSub;
}

void mndReleaseSubscribe(SMnode *pMnode, SMqSubscribeObj *pSub) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pSub);
}

static int32_t mndProcessSubscribeReq(SMnodeMsg *pMsg) {
  SMnode         *pMnode = pMsg->pMnode;
  char           *msgStr = pMsg->rpcMsg.pCont;
  SCMSubscribeReq subscribe;
  tDeserializeSCMSubscribeReq(msgStr, &subscribe);
  int64_t consumerId = subscribe.consumerId;
  char   *consumerGroup = subscribe.consumerGroup;
  int32_t cgroupLen = strlen(consumerGroup);

  SArray *newSub = subscribe.topicNames;
  int     newTopicNum = subscribe.topicNum;

  taosArraySortString(newSub, taosArrayCompareString);

  SArray *oldSub = NULL;
  int     oldTopicNum = 0;
  // create consumer if not exist
  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 已提交
523
    pConsumer->epoch = 1;
L
Liu Jicong 已提交
524 525 526 527
    pConsumer->consumerId = consumerId;
    strcpy(pConsumer->cgroup, consumerGroup);
    taosInitRWLatch(&pConsumer->lock);
  } else {
L
Liu Jicong 已提交
528
    pConsumer->epoch++;
L
Liu Jicong 已提交
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
    oldSub = pConsumer->topics;
  }
  pConsumer->topics = taosArrayInit(newTopicNum, sizeof(SMqConsumerTopic));

  if (oldSub != NULL) {
    oldTopicNum = taosArrayGetSize(oldSub);
  }

  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, &pMsg->rpcMsg);
  if (pTrans == NULL) {
    // TODO: free memory
    return -1;
  }

  int i = 0, j = 0;
  while (i < newTopicNum || j < oldTopicNum) {
L
Liu Jicong 已提交
545 546
    char *newTopicName = NULL;
    char *oldTopicName = NULL;
L
Liu Jicong 已提交
547 548
    if (i >= newTopicNum) {
      // encode unset topic msg to all vnodes related to that topic
L
Liu Jicong 已提交
549
      oldTopicName = ((SMqConsumerTopic *)taosArrayGet(oldSub, j))->name;
L
Liu Jicong 已提交
550 551
      j++;
    } else if (j >= oldTopicNum) {
L
Liu Jicong 已提交
552
      newTopicName = taosArrayGetP(newSub, i);
L
Liu Jicong 已提交
553 554
      i++;
    } else {
L
Liu Jicong 已提交
555
      newTopicName = taosArrayGetP(newSub, i);
L
Liu Jicong 已提交
556
      oldTopicName = ((SMqConsumerTopic *)taosArrayGet(oldSub, j))->name;
L
Liu Jicong 已提交
557

L
Liu Jicong 已提交
558
      int comp = compareLenPrefixedStr(newTopicName, oldTopicName);
L
Liu Jicong 已提交
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
      if (comp == 0) {
        // do nothing
        oldTopicName = newTopicName = NULL;
        i++;
        j++;
        continue;
      } else if (comp < 0) {
        oldTopicName = NULL;
        i++;
      } else {
        newTopicName = NULL;
        j++;
      }
    }

    if (oldTopicName != NULL) {
#if 0
      // cancel subscribe of that old topic
      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);
      SMqSubscribeObj *pSub = mndAcquireSubscribe(pMnode, consumerGroup, oldTopicName);
      SMqCGroup *pGroup = taosHashGet(pTopic->cgroups, consumerGroup, cgroupLen);
      while ((pn = tdListNext(&iter)) != NULL) {
        int32_t vgId = *(int64_t *)pn->data;
        // acquire and get epset
        SVgObj *pVgObj = mndAcquireVgroup(pMnode, vgId);
        // TODO what time to release?
        if (pVgObj == NULL) {
          // TODO handle error
          continue;
        }
        // build reset msg
        void *pMqVgSetReq = mndBuildMqVGroupSetReq(pMnode, oldTopicName, vgId, consumerId, consumerGroup);
        // 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;
        }
      }
      // delete data in mnode
      taosHashRemove(pTopic->cgroups, consumerGroup, cgroupLen);
      mndReleaseSubscribe(pMnode, pSub);
      mndReleaseTopic(pMnode, pTopic);
#endif
    } else if (newTopicName != NULL) {
      // save subscribe info to mnode
      ASSERT(oldTopicName == NULL);

      SMqTopicObj *pTopic = mndAcquireTopic(pMnode, newTopicName);
      if (pTopic == NULL) {
L
Liu Jicong 已提交
627
        mError("topic being subscribed not exist: %s", newTopicName);
L
Liu Jicong 已提交
628 629 630 631
        continue;
      }

      SMqSubscribeObj *pSub = mndAcquireSubscribe(pMnode, consumerGroup, newTopicName);
L
Liu Jicong 已提交
632
      bool             create = false;
L
Liu Jicong 已提交
633
      if (pSub == NULL) {
L
Liu Jicong 已提交
634
        mDebug("create new subscription, group: %s, topic %s", consumerGroup, newTopicName);
L
Liu Jicong 已提交
635 636 637 638 639
        pSub = tNewSubscribeObj();
        if (pSub == NULL) {
          terrno = TSDB_CODE_OUT_OF_MEMORY;
          return -1;
        }
L
Liu Jicong 已提交
640
        char *key = mndMakeSubscribeKey(consumerGroup, newTopicName);
L
Liu Jicong 已提交
641 642 643 644
        if (key == NULL) {
          terrno = TSDB_CODE_OUT_OF_MEMORY;
          return -1;
        }
L
Liu Jicong 已提交
645
        strcpy(pSub->key, key);
L
Liu Jicong 已提交
646
        free(key);
L
Liu Jicong 已提交
647
        // set unassigned vg
L
Liu Jicong 已提交
648
        if (mndInitUnassignedVg(pMnode, pTopic, pSub->unassignedVg) < 0) {
L
Liu Jicong 已提交
649
          // TODO: free memory
L
Liu Jicong 已提交
650 651
          return -1;
        }
L
Liu Jicong 已提交
652
        // TODO: disable alter
L
Liu Jicong 已提交
653
        create = true;
L
Liu Jicong 已提交
654
      }
L
Liu Jicong 已提交
655 656
      taosArrayPush(pSub->availConsumer, &consumerId);

L
Liu Jicong 已提交
657 658
      int64_t           oldConsumerId;
      SMqConsumerTopic *pConsumerTopic = tNewConsumerTopic(consumerId, pTopic, pSub, &oldConsumerId);
L
Liu Jicong 已提交
659 660 661
      taosArrayPush(pConsumer->topics, pConsumerTopic);

      if (taosArrayGetSize(pConsumerTopic->pVgInfo) > 0) {
L
Liu Jicong 已提交
662
        ASSERT(taosArrayGetSize(pConsumerTopic->pVgInfo) == 1);
L
Liu Jicong 已提交
663 664
        int32_t        vgId = *(int32_t *)taosArrayGetLast(pConsumerTopic->pVgInfo);
        SMqConsumerEp *pCEp = taosArrayGetLast(pSub->assigned);
L
fix tq  
Liu Jicong 已提交
665
        if (pCEp->vgId == vgId) {
L
Liu Jicong 已提交
666
          if (mndBuildMqSetConsumerVgReq(pMnode, pTrans, pConsumer, pConsumerTopic, pTopic, pCEp, oldConsumerId) < 0) {
L
fix tq  
Liu Jicong 已提交
667 668 669
            // TODO
            return -1;
          }
L
Liu Jicong 已提交
670
        }
L
fix tq  
Liu Jicong 已提交
671
        // send setmsg to vnode
L
Liu Jicong 已提交
672
      }
L
Liu Jicong 已提交
673

L
Liu Jicong 已提交
674
      SSdbRaw *pRaw = mndSubActionEncode(pSub);
L
Liu Jicong 已提交
675
      sdbSetRawStatus(pRaw, SDB_STATUS_READY);
L
Liu Jicong 已提交
676
      mndTransAppendRedolog(pTrans, pRaw);
L
Liu Jicong 已提交
677
      if (!create) mndReleaseSubscribe(pMnode, pSub);
L
Liu Jicong 已提交
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
#if 0
      SMqCGroup *pGroup = taosHashGet(pTopic->cgroups, consumerGroup, cgroupLen);
      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
        taosHashPut(pTopic->cgroups, consumerGroup, cgroupLen, pGroup, sizeof(SMqCGroup));
      }
      /*taosHashPut(pTopic->consumers, &pConsumer->consumerId, sizeof(int64_t), pConsumer, sizeof(SMqConsumerObj));*/

      // put the consumer into list
      // rebalance will be triggered by timer
      tdListAppend(pGroup->consumerIds, &consumerId);

      SSdbRaw *pTopicRaw = mndTopicActionEncode(pTopic);
      sdbSetRawStatus(pTopicRaw, SDB_STATUS_READY);
      // TODO: error handling
      mndTransAppendRedolog(pTrans, pTopicRaw);

#endif
L
Liu Jicong 已提交
709 710
      /*mndReleaseTopic(pMnode, pTopic);*/
      /*mndReleaseSubscribe(pMnode, pSub);*/
L
Liu Jicong 已提交
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
    }
  }
  // part3. persist consumerObj

  // destroy old sub
  if (oldSub) taosArrayDestroy(oldSub);
  // put new sub into consumerobj

  // 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());
L
Liu Jicong 已提交
727
    if (newSub) taosArrayDestroy(newSub);
L
Liu Jicong 已提交
728
    mndTransDrop(pTrans);
L
Liu Jicong 已提交
729
    /*mndReleaseConsumer(pMnode, pConsumer);*/
L
Liu Jicong 已提交
730 731 732 733 734
    return -1;
  }

  if (newSub) taosArrayDestroy(newSub);
  mndTransDrop(pTrans);
L
Liu Jicong 已提交
735 736
  /*mndReleaseConsumer(pMnode, pConsumer);*/
  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
L
Liu Jicong 已提交
737 738
}

L
Liu Jicong 已提交
739 740 741 742
static int32_t mndProcessSubscribeInternalRsp(SMnodeMsg *pRsp) {
  mndTransProcessRsp(pRsp);
  return 0;
}
L
Liu Jicong 已提交
743 744 745 746 747

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

D
dapan1121 已提交
748
  mDebug("subscribe:%s, start to retrieve meta", pInfo->tbName);
L
Liu Jicong 已提交
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878

#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(STableMetaRsp) + totalCols * sizeof(SSchema);

  STableMetaRsp *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) {
    SMqConsumerObj *pConsumer = NULL;
    pIter = sdbFetch(pSdb, SDB_CONSUMER, pIter, (void **)&pConsumer);
    if (pIter == NULL) break;

    numOfConsumers++;

    sdbRelease(pSdb, pConsumer);
  }

  *pNumOfConsumers = numOfConsumers;
  return 0;
}

static int32_t mndGetConsumerMeta(SMnodeMsg *pMsg, SShowObj *pShow, STableMetaRsp *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];
D
dapan1121 已提交
879
  strcpy(pMeta->tbName, mndShowStr(pShow->type));
L
Liu Jicong 已提交
880 881 882 883 884 885 886 887

  return 0;
}

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