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

S
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
L
Liu Jicong 已提交
17
#include "mndSubscribe.h"
L
Liu Jicong 已提交
18
#include "mndConsumer.h"
L
Liu Jicong 已提交
19
#include "mndScheduler.h"
L
Liu Jicong 已提交
20 21 22 23 24 25 26
#include "mndShow.h"
#include "mndTopic.h"
#include "mndTrans.h"
#include "mndVgroup.h"
#include "tcompare.h"
#include "tname.h"

L
Liu Jicong 已提交
27
#define MND_SUBSCRIBE_VER_NUMBER   1
L
Liu Jicong 已提交
28 29
#define MND_SUBSCRIBE_RESERVE_SIZE 64

L
Liu Jicong 已提交
30
#define MND_SUBSCRIBE_REBALANCE_CNT 3
L
Liu Jicong 已提交
31

L
Liu Jicong 已提交
32 33 34 35 36
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);
37 38 39 40
static int32_t  mndProcessRebalanceReq(SRpcMsg *pMsg);
static int32_t  mndProcessDropCgroupReq(SRpcMsg *pMsg);
static int32_t  mndRetrieveSubscribe(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
static void     mndCancelGetNextSubscribe(SMnode *pMnode, void *pIter);
L
Liu Jicong 已提交
41

42 43 44 45 46 47 48
static int32_t mndSetSubRedoLogs(SMnode *pMnode, STrans *pTrans, SMqSubscribeObj *pSub) {
  SSdbRaw *pRedoRaw = mndSubActionEncode(pSub);
  if (pRedoRaw == NULL) return -1;
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) return -1;
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_READY) != 0) return -1;
  return 0;
}
L
Liu Jicong 已提交
49

50 51 52 53 54 55 56
static int32_t mndSetSubCommitLogs(SMnode *pMnode, STrans *pTrans, SMqSubscribeObj *pSub) {
  SSdbRaw *pCommitRaw = mndSubActionEncode(pSub);
  if (pCommitRaw == NULL) return -1;
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) return -1;
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY) != 0) return -1;
  return 0;
}
L
Liu Jicong 已提交
57

L
Liu Jicong 已提交
58
int32_t mndInitSubscribe(SMnode *pMnode) {
59 60 61 62 63 64 65 66 67 68
  SSdbTable table = {
      .sdbType = SDB_SUBSCRIBE,
      .keyType = SDB_KEY_BINARY,
      .encodeFp = (SdbEncodeFp)mndSubActionEncode,
      .decodeFp = (SdbDecodeFp)mndSubActionDecode,
      .insertFp = (SdbInsertFp)mndSubActionInsert,
      .updateFp = (SdbUpdateFp)mndSubActionUpdate,
      .deleteFp = (SdbDeleteFp)mndSubActionDelete,
  };

L
Liu Jicong 已提交
69 70 71 72 73
  mndSetMsgHandle(pMnode, TDMT_VND_TMQ_SUBSCRIBE_RSP, mndTransProcessRsp);
  mndSetMsgHandle(pMnode, TDMT_VND_TMQ_DELETE_SUB_RSP, mndTransProcessRsp);
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_DO_REBALANCE, mndProcessRebalanceReq);
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_DROP_CGROUP, mndProcessDropCgroupReq);
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_DROP_CGROUP_RSP, mndTransProcessRsp);
L
Liu Jicong 已提交
74 75 76 77

  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_SUBSCRIPTIONS, mndRetrieveSubscribe);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_TOPICS, mndCancelGetNextSubscribe);

L
Liu Jicong 已提交
78 79 80
  return sdbSetTable(pMnode->pSdb, table);
}

81
static SMqSubscribeObj *mndCreateSubscription(SMnode *pMnode, const SMqTopicObj *pTopic, const char *subKey) {
82 83 84 85 86
  SMqSubscribeObj *pSub = tNewSubscribeObj(subKey);
  if (pSub == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
87

L
Liu Jicong 已提交
88
  pSub->dbUid = pTopic->dbUid;
L
Liu Jicong 已提交
89
  pSub->stbUid = pTopic->stbUid;
L
Liu Jicong 已提交
90
  pSub->subType = pTopic->subType;
L
Liu Jicong 已提交
91
  pSub->withMeta = pTopic->withMeta;
L
Liu Jicong 已提交
92

93 94 95 96 97 98 99 100 101
  if (mndSchedInitSubEp(pMnode, pTopic, pSub) < 0) {
    tDeleteSubscribeObj(pSub);
    taosMemoryFree(pSub);
    return NULL;
  }

  return pSub;
}

L
Liu Jicong 已提交
102 103
static int32_t mndBuildSubChangeReq(void **pBuf, int32_t *pLen, const SMqSubscribeObj *pSub,
                                    const SMqRebOutputVg *pRebVg) {
104 105 106 107
  SMqRebVgReq req = {0};
  req.oldConsumerId = pRebVg->oldConsumerId;
  req.newConsumerId = pRebVg->newConsumerId;
  req.vgId = pRebVg->pVgEp->vgId;
L
Liu Jicong 已提交
108
  req.qmsg = pRebVg->pVgEp->qmsg;
L
Liu Jicong 已提交
109
  req.subType = pSub->subType;
L
Liu Jicong 已提交
110
  req.withMeta = pSub->withMeta;
L
Liu Jicong 已提交
111
  req.suid = pSub->stbUid;
S
Shengliang Guan 已提交
112
  tstrncpy(req.subKey, pSub->key, TSDB_SUBSCRIBE_KEY_LEN);
113 114 115 116 117 118 119

  int32_t tlen = sizeof(SMsgHead) + tEncodeSMqRebVgReq(NULL, &req);
  void   *buf = taosMemoryMalloc(tlen);
  if (buf == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
120

121 122 123 124 125 126 127 128 129 130 131 132 133
  SMsgHead *pMsgHead = (SMsgHead *)buf;

  pMsgHead->contLen = htonl(tlen);
  pMsgHead->vgId = htonl(pRebVg->pVgEp->vgId);

  void *abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
  tEncodeSMqRebVgReq(&abuf, &req);
  *pBuf = buf;
  *pLen = tlen;

  return 0;
}

L
Liu Jicong 已提交
134
static int32_t mndPersistSubChangeVgReq(SMnode *pMnode, STrans *pTrans, const SMqSubscribeObj *pSub,
135
                                        const SMqRebOutputVg *pRebVg) {
136 137 138 139
  if (pRebVg->oldConsumerId == pRebVg->newConsumerId) {
    terrno = TSDB_CODE_MND_INVALID_SUB_OPTION;
    return -1;
  }
140 141 142

  void   *buf;
  int32_t tlen;
L
Liu Jicong 已提交
143
  if (mndBuildSubChangeReq(&buf, &tlen, pSub, pRebVg) < 0) {
144 145 146 147 148
    return -1;
  }

  int32_t vgId = pRebVg->pVgEp->vgId;
  SVgObj *pVgObj = mndAcquireVgroup(pMnode, vgId);
L
Liu Jicong 已提交
149 150
  if (pVgObj == NULL) {
    taosMemoryFree(buf);
151
    terrno = TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
152 153
    return -1;
  }
154 155 156 157 158

  STransAction action = {0};
  action.epSet = mndGetVgroupEpset(pMnode, pVgObj);
  action.pCont = buf;
  action.contLen = tlen;
L
Liu Jicong 已提交
159
  action.msgType = TDMT_VND_TMQ_SUBSCRIBE;
160 161 162 163 164 165 166 167

  mndReleaseVgroup(pMnode, pVgObj);
  if (mndTransAppendRedoAction(pTrans, &action) != 0) {
    taosMemoryFree(buf);
    return -1;
  }
  return 0;
}
L
Liu Jicong 已提交
168

L
Liu Jicong 已提交
169
static int32_t mndSplitSubscribeKey(const char *key, char *topic, char *cgroup, bool fullName) {
S
Shengliang Guan 已提交
170
  int32_t i = 0;
L
Liu Jicong 已提交
171
  while (key[i] != TMQ_SEPARATOR) {
L
Liu Jicong 已提交
172 173
    i++;
  }
L
Liu Jicong 已提交
174 175
  memcpy(cgroup, key, i);
  cgroup[i] = 0;
L
Liu Jicong 已提交
176 177 178 179 180 181 182 183
  if (fullName) {
    strcpy(topic, &key[i + 1]);
  } else {
    while (key[i] != '.') {
      i++;
    }
    strcpy(topic, &key[i + 1]);
  }
L
Liu Jicong 已提交
184 185 186
  return 0;
}

L
Liu Jicong 已提交
187 188
static SMqRebInfo *mndGetOrCreateRebSub(SHashObj *pHash, const char *key) {
  SMqRebInfo *pRebSub = taosHashGet(pHash, key, strlen(key) + 1);
L
Liu Jicong 已提交
189 190 191
  if (pRebSub == NULL) {
    pRebSub = tNewSMqRebSubscribe(key);
    if (pRebSub == NULL) {
L
Liu Jicong 已提交
192
      terrno = TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
193 194
      return NULL;
    }
L
Liu Jicong 已提交
195
    taosHashPut(pHash, key, strlen(key) + 1, pRebSub, sizeof(SMqRebInfo));
L
Liu Jicong 已提交
196 197 198 199
  }
  return pRebSub;
}

200 201 202
static void doRemoveExistedConsumers(SMqRebOutputObj *pOutput, SHashObj *pHash, const SMqRebInputObj *pInput) {
  int32_t     numOfRemoved = taosArrayGetSize(pInput->pRebInfo->removedConsumers);
  const char *pSubKey = pOutput->pSub->key;
203 204

  int32_t actualRemoved = 0;
205
  for (int32_t i = 0; i < numOfRemoved; i++) {
206
    uint64_t consumerId = *(uint64_t *)taosArrayGet(pInput->pRebInfo->removedConsumers, i);
207

208
    SMqConsumerEp *pConsumerEp = taosHashGet(pOutput->pSub->consumerHash, &consumerId, sizeof(int64_t));
209

210
    // consumer exists till now
211
    if (pConsumerEp) {
212
      actualRemoved++;
213

214
      int32_t consumerVgNum = taosArrayGetSize(pConsumerEp->vgs);
215
      for (int32_t j = 0; j < consumerVgNum; j++) {
216
        SMqVgEp       *pVgEp = taosArrayGetP(pConsumerEp->vgs, j);
217 218 219 220 221
        SMqRebOutputVg outputVg = {
            .oldConsumerId = consumerId,
            .newConsumerId = -1,
            .pVgEp = pVgEp,
        };
222

223
        taosHashPut(pHash, &pVgEp->vgId, sizeof(int32_t), &outputVg, sizeof(SMqRebOutputVg));
224
        mInfo("sub:%s mq re-balance remove vgId:%d from consumer:%" PRIx64, pSubKey, pVgEp->vgId, consumerId);
225
      }
226

L
Liu Jicong 已提交
227
      taosArrayDestroy(pConsumerEp->vgs);
228
      taosHashRemove(pOutput->pSub->consumerHash, &consumerId, sizeof(int64_t));
229

230 231 232 233
      // put into removed
      taosArrayPush(pOutput->removedConsumers, &consumerId);
    }
  }
234

235 236 237 238
  if (numOfRemoved != actualRemoved) {
    mError("sub:%s mq re-balance removedNum:%d not matched with actual:%d", pSubKey, numOfRemoved, actualRemoved);
  } else {
    mInfo("sub:%s removed %d consumers", pSubKey, numOfRemoved);
239
  }
240
}
241

242 243 244 245 246 247 248 249 250 251 252 253 254 255
static void doAddNewConsumers(SMqRebOutputObj *pOutput, const SMqRebInputObj *pInput) {
  int32_t numOfNewConsumers = taosArrayGetSize(pInput->pRebInfo->newConsumers);
  const char *pSubKey = pOutput->pSub->key;

  for (int32_t i = 0; i < numOfNewConsumers; i++) {
    int64_t consumerId = *(int64_t *)taosArrayGet(pInput->pRebInfo->newConsumers, i);

    SMqConsumerEp newConsumerEp;
    newConsumerEp.consumerId = consumerId;
    newConsumerEp.vgs = taosArrayInit(0, sizeof(void *));

    taosHashPut(pOutput->pSub->consumerHash, &consumerId, sizeof(int64_t), &newConsumerEp, sizeof(SMqConsumerEp));
    taosArrayPush(pOutput->newConsumers, &consumerId);
    mInfo("sub:%s mq rebalance add new consumer:%" PRIx64, pSubKey, consumerId);
256
  }
257
}
258

259 260 261 262 263 264 265 266 267 268 269 270 271 272
static void addUnassignedVgroups(SMqRebOutputObj *pOutput, SHashObj* pHash) {
  const char *pSubKey = pOutput->pSub->key;
  int32_t numOfVgroups = taosArrayGetSize(pOutput->pSub->unassignedVgs);

  for (int32_t i = 0; i < numOfVgroups; i++) {
    SMqVgEp       *pVgEp = *(SMqVgEp **)taosArrayPop(pOutput->pSub->unassignedVgs);
    SMqRebOutputVg rebOutput = {
        .oldConsumerId = -1,
        .newConsumerId = -1,
        .pVgEp = pVgEp,
    };

    taosHashPut(pHash, &pVgEp->vgId, sizeof(int32_t), &rebOutput, sizeof(SMqRebOutputVg));
    mInfo("sub:%s mq re-balance remove vgId:%d from unassigned", pSubKey, pVgEp->vgId);
L
Liu Jicong 已提交
273
  }
274
}
275

276 277
static void transferVgroupsForConsumers(SMqRebOutputObj *pOutput, SHashObj* pHash, int32_t minVgCnt, int32_t imbConsumerNum) {
  const char *pSubKey = pOutput->pSub->key;
278 279 280

  int32_t imbCnt = 0;
  void   *pIter = NULL;
281

282 283
  while (1) {
    pIter = taosHashIterate(pOutput->pSub->consumerHash, pIter);
284 285 286 287
    if (pIter == NULL) {
      break;
    }

288
    SMqConsumerEp *pConsumerEp = (SMqConsumerEp *)pIter;
289
    int32_t        consumerVgNum = taosArrayGetSize(pConsumerEp->vgs);
290

291 292
    // all old consumers still existing are touched
    // TODO optimize: touch only consumer whose vgs changed
293
    taosArrayPush(pOutput->touchedConsumers, &pConsumerEp->consumerId);
294 295 296
    if (consumerVgNum > minVgCnt) {
      if (imbCnt < imbConsumerNum) {
        if (consumerVgNum == minVgCnt + 1) {
L
Liu Jicong 已提交
297
          imbCnt++;
298 299 300
          continue;
        } else {
          // pop until equal minVg + 1
301 302
          while (taosArrayGetSize(pConsumerEp->vgs) > minVgCnt + 1) {
            SMqVgEp       *pVgEp = *(SMqVgEp **)taosArrayPop(pConsumerEp->vgs);
303
            SMqRebOutputVg outputVg = {
304
                .oldConsumerId = pConsumerEp->consumerId,
305 306 307 308
                .newConsumerId = -1,
                .pVgEp = pVgEp,
            };
            taosHashPut(pHash, &pVgEp->vgId, sizeof(int32_t), &outputVg, sizeof(SMqRebOutputVg));
309
            mInfo("sub:%s mq rebalance remove vgId:%d from consumer:0x%" PRIx64 ",(first scan)", pSubKey, pVgEp->vgId,
L
Liu Jicong 已提交
310
                  pConsumerEp->consumerId);
311 312 313 314
          }
          imbCnt++;
        }
      } else {
315
        // all the remain consumers should only have the number of vgroups, which is equalled to the value of minVg
316 317
        while (taosArrayGetSize(pConsumerEp->vgs) > minVgCnt) {
          SMqVgEp       *pVgEp = *(SMqVgEp **)taosArrayPop(pConsumerEp->vgs);
318
          SMqRebOutputVg outputVg = {
319
              .oldConsumerId = pConsumerEp->consumerId,
320 321 322 323
              .newConsumerId = -1,
              .pVgEp = pVgEp,
          };
          taosHashPut(pHash, &pVgEp->vgId, sizeof(int32_t), &outputVg, sizeof(SMqRebOutputVg));
324
          mInfo("sub:%s mq rebalance remove vgId:%d from consumer:0x%" PRIx64 ",(first scan)", pSubKey, pVgEp->vgId,
L
Liu Jicong 已提交
325
                pConsumerEp->consumerId);
326 327 328 329
        }
      }
    }
  }
330
}
331

332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
static int32_t mndDoRebalance(SMnode *pMnode, const SMqRebInputObj *pInput, SMqRebOutputObj *pOutput) {
  int32_t     totalVgNum = pOutput->pSub->vgNum;
  const char *pSubKey = pOutput->pSub->key;

  int32_t numOfRemoved = taosArrayGetSize(pInput->pRebInfo->removedConsumers);
  int32_t numOfAdded = taosArrayGetSize(pInput->pRebInfo->newConsumers);
  mInfo("sub:%s mq re-balance %d vgroups, existed consumers:%d, added:%d, removed:%d", pSubKey, totalVgNum,
        pInput->oldConsumerNum, numOfAdded, numOfRemoved);

  // 1. build temporary hash(vgId -> SMqRebOutputVg) to store modified vg
  SHashObj *pHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK);

  // 2. check and get actual removed consumers, put their vg into hash
  doRemoveExistedConsumers(pOutput, pHash, pInput);

  // 3. if previously no consumer, there are vgs not assigned
  addUnassignedVgroups(pOutput, pHash);

  // 4. calc vg number of each consumer
  int32_t numOfFinal = pInput->oldConsumerNum + numOfAdded - numOfRemoved;
H
Haojun Liao 已提交
352

353 354
  int32_t minVgCnt = 0;
  int32_t imbConsumerNum = 0;
H
Haojun Liao 已提交
355

356 357 358 359
  // calc num
  if (numOfFinal) {
    minVgCnt = totalVgNum / numOfFinal;
    imbConsumerNum = totalVgNum % numOfFinal;
360 361 362 363
    mInfo("sub:%s mq re-balance %d consumers: at least %d vgs each, %d consumers has 1 more vgroups than avg value",
          pSubKey, numOfFinal, minVgCnt, imbConsumerNum);
  } else {
    mInfo("sub:%s no consumer subscribe this topic", pSubKey);
364 365
  }

366 367 368 369 370 371 372 373
  // 5. first scan: remove vgroups from te consumers, who have more vgroups than the threashold value that is
  // minVgCnt, and then put them into the recycled hash list
  transferVgroupsForConsumers(pOutput, pHash, minVgCnt, imbConsumerNum);

  // 6. add new consumer into sub
  doAddNewConsumers(pOutput, pInput);

  // 7. second scan: find consumer do not have enough vgroups, extract from temporary hash and assign to them
374 375 376
  // All related vg should be put into rebVgs
  SMqRebOutputVg *pRebVg = NULL;
  void           *pRemovedIter = NULL;
377 378
  void           *pIter = NULL;

379 380
  while (1) {
    pIter = taosHashIterate(pOutput->pSub->consumerHash, pIter);
381 382 383 384
    if (pIter == NULL) {
      break;
    }

385
    SMqConsumerEp *pConsumerEp = (SMqConsumerEp *)pIter;
L
Liu Jicong 已提交
386 387

    // push until equal minVg
388
    while (taosArrayGetSize(pConsumerEp->vgs) < minVgCnt) {
L
Liu Jicong 已提交
389 390
      // iter hash and find one vg
      pRemovedIter = taosHashIterate(pHash, pRemovedIter);
S
Shengliang Guan 已提交
391
      if (pRemovedIter == NULL) {
392 393
        mError("sub:%s removed iter is null", pSubKey);
        break;
S
Shengliang Guan 已提交
394
      }
395

L
Liu Jicong 已提交
396 397
      pRebVg = (SMqRebOutputVg *)pRemovedIter;
      // push
398 399
      taosArrayPush(pConsumerEp->vgs, &pRebVg->pVgEp);
      pRebVg->newConsumerId = pConsumerEp->consumerId;
L
Liu Jicong 已提交
400
      taosArrayPush(pOutput->rebVgs, pRebVg);
H
Haojun Liao 已提交
401
      mInfo("mq rebalance: add vgId:%d to consumer:0x%" PRIx64 " (second scan) (not enough)", pRebVg->pVgEp->vgId,
L
Liu Jicong 已提交
402
            pConsumerEp->consumerId);
L
Liu Jicong 已提交
403
    }
404 405 406
  }

  // 7. handle unassigned vg
407
  if (taosHashGetSize(pOutput->pSub->consumerHash) != 0) {
L
Liu Jicong 已提交
408 409
    // if has consumer, assign all left vg
    while (1) {
L
Liu Jicong 已提交
410
      SMqConsumerEp *pConsumerEp = NULL;
L
Liu Jicong 已提交
411
      pRemovedIter = taosHashIterate(pHash, pRemovedIter);
L
Liu Jicong 已提交
412 413 414 415 416 417 418 419 420 421
      if (pRemovedIter == NULL) {
        if (pIter != NULL) {
          taosHashCancelIterate(pOutput->pSub->consumerHash, pIter);
          pIter = NULL;
        }
        break;
      }
      while (1) {
        pIter = taosHashIterate(pOutput->pSub->consumerHash, pIter);
        pConsumerEp = (SMqConsumerEp *)pIter;
422

423 424
        if (taosArrayGetSize(pConsumerEp->vgs) == minVgCnt) {
          break;
L
Liu Jicong 已提交
425 426
        }
      }
L
Liu Jicong 已提交
427
      pRebVg = (SMqRebOutputVg *)pRemovedIter;
428 429
      taosArrayPush(pConsumerEp->vgs, &pRebVg->pVgEp);
      pRebVg->newConsumerId = pConsumerEp->consumerId;
L
Liu Jicong 已提交
430
      if (pRebVg->newConsumerId == pRebVg->oldConsumerId) {
H
Haojun Liao 已提交
431
        mInfo("mq rebalance: skip vg %d for same consumer:0x%" PRIx64 " (second scan)", pRebVg->pVgEp->vgId,
L
Liu Jicong 已提交
432 433 434
              pConsumerEp->consumerId);
        continue;
      }
L
Liu Jicong 已提交
435
      taosArrayPush(pOutput->rebVgs, pRebVg);
H
Haojun Liao 已提交
436
      mInfo("mq rebalance: add vgId:%d to consumer:0x%" PRIx64 " (second scan) (unassigned)", pRebVg->pVgEp->vgId,
L
Liu Jicong 已提交
437
            pConsumerEp->consumerId);
L
Liu Jicong 已提交
438
    }
439 440 441 442 443 444
  } else {
    // if all consumer is removed, put all vg into unassigned
    pIter = NULL;
    SMqRebOutputVg *pRebOutput = NULL;
    while (1) {
      pIter = taosHashIterate(pHash, pIter);
445 446 447 448
      if (pIter == NULL) {
        break;
      }

449
      pRebOutput = (SMqRebOutputVg *)pIter;
450

451
      taosArrayPush(pOutput->pSub->unassignedVgs, &pRebOutput->pVgEp);
L
Liu Jicong 已提交
452
      taosArrayPush(pOutput->rebVgs, pRebOutput);
453
      mInfo("sub:%s mq re-balance unassign vgId:%d (second scan)", pSubKey, pRebOutput->pVgEp->vgId);
454 455 456
    }
  }

L
Liu Jicong 已提交
457
  // 8. generate logs
458
  mInfo("sub:%s mq re-balance calculation completed, re-balanced vg", pSubKey);
L
Liu Jicong 已提交
459 460
  for (int32_t i = 0; i < taosArrayGetSize(pOutput->rebVgs); i++) {
    SMqRebOutputVg *pOutputRebVg = taosArrayGet(pOutput->rebVgs, i);
461
    mInfo("sub:%s mq re-balance vgId:%d, moved from consumer:0x%" PRIx64 ", to consumer:0x%" PRIx64, pSubKey,
462
          pOutputRebVg->pVgEp->vgId, pOutputRebVg->oldConsumerId, pOutputRebVg->newConsumerId);
L
Liu Jicong 已提交
463
  }
L
Liu Jicong 已提交
464
  {
465
    pIter = NULL;
L
Liu Jicong 已提交
466 467 468 469 470
    while (1) {
      pIter = taosHashIterate(pOutput->pSub->consumerHash, pIter);
      if (pIter == NULL) break;
      SMqConsumerEp *pConsumerEp = (SMqConsumerEp *)pIter;
      int32_t        sz = taosArrayGetSize(pConsumerEp->vgs);
471
      mInfo("sub:%s mq re-balance final cfg: consumer:0x%" PRIx64 " has %d vg", pSubKey, pConsumerEp->consumerId, sz);
L
Liu Jicong 已提交
472 473
      for (int32_t i = 0; i < sz; i++) {
        SMqVgEp *pVgEp = taosArrayGetP(pConsumerEp->vgs, i);
474
        mInfo("sub:%s mq re-balance final cfg: vg %d to consumer:0x%" PRIx64, pSubKey, pVgEp->vgId,
475
              pConsumerEp->consumerId);
L
Liu Jicong 已提交
476 477 478
      }
    }
  }
479 480 481 482 483 484 485

  // 9. clear
  taosHashCleanup(pHash);

  return 0;
}

S
Shengliang Guan 已提交
486
static int32_t mndPersistRebResult(SMnode *pMnode, SRpcMsg *pMsg, const SMqRebOutputObj *pOutput) {
487
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_DB_INSIDE, pMsg, "tmq-reb");
488 489 490
  if (pTrans == NULL) {
    return -1;
  }
491

492
  mndTransSetDbName(pTrans, pOutput->pSub->dbName, NULL);
493 494 495 496
  if (mndTrancCheckConflict(pMnode, pTrans) != 0) {
    mndTransDrop(pTrans);
    return -1;
  }
497

498 499 500 501 502 503
  // make txn:
  // 1. redo action: action to all vg
  const SArray *rebVgs = pOutput->rebVgs;
  int32_t       vgNum = taosArrayGetSize(rebVgs);
  for (int32_t i = 0; i < vgNum; i++) {
    SMqRebOutputVg *pRebVg = taosArrayGet(rebVgs, i);
L
Liu Jicong 已提交
504
    if (mndPersistSubChangeVgReq(pMnode, pTrans, pOutput->pSub, pRebVg) < 0) {
505 506 507 508 509 510
      goto REB_FAIL;
    }
  }

  // 2. redo log: subscribe and vg assignment
  // subscribe
L
Liu Jicong 已提交
511
  if (mndSetSubCommitLogs(pMnode, pTrans, pOutput->pSub) != 0) {
512 513 514 515 516 517 518 519 520 521 522 523 524
    goto REB_FAIL;
  }

  // 3. commit log: consumer to update status and epoch
  // 3.1 set touched consumer
  int32_t consumerNum = taosArrayGetSize(pOutput->touchedConsumers);
  for (int32_t i = 0; i < consumerNum; i++) {
    int64_t         consumerId = *(int64_t *)taosArrayGet(pOutput->touchedConsumers, i);
    SMqConsumerObj *pConsumerOld = mndAcquireConsumer(pMnode, consumerId);
    SMqConsumerObj *pConsumerNew = tNewSMqConsumerObj(pConsumerOld->consumerId, pConsumerOld->cgroup);
    pConsumerNew->updateType = CONSUMER_UPDATE__TOUCH;
    mndReleaseConsumer(pMnode, pConsumerOld);
    if (mndSetConsumerCommitLogs(pMnode, pTrans, pConsumerNew) != 0) {
L
Liu Jicong 已提交
525 526
      tDeleteSMqConsumerObj(pConsumerNew);
      taosMemoryFree(pConsumerNew);
527 528
      goto REB_FAIL;
    }
L
Liu Jicong 已提交
529 530
    tDeleteSMqConsumerObj(pConsumerNew);
    taosMemoryFree(pConsumerNew);
531 532 533 534
  }
  // 3.2 set new consumer
  consumerNum = taosArrayGetSize(pOutput->newConsumers);
  for (int32_t i = 0; i < consumerNum; i++) {
L
Liu Jicong 已提交
535
    int64_t consumerId = *(int64_t *)taosArrayGet(pOutput->newConsumers, i);
536

537 538 539 540 541
    SMqConsumerObj *pConsumerOld = mndAcquireConsumer(pMnode, consumerId);
    SMqConsumerObj *pConsumerNew = tNewSMqConsumerObj(pConsumerOld->consumerId, pConsumerOld->cgroup);
    pConsumerNew->updateType = CONSUMER_UPDATE__ADD;
    char *topic = taosMemoryCalloc(1, TSDB_TOPIC_FNAME_LEN);
    char  cgroup[TSDB_CGROUP_LEN];
L
Liu Jicong 已提交
542
    mndSplitSubscribeKey(pOutput->pSub->key, topic, cgroup, true);
543 544 545
    taosArrayPush(pConsumerNew->rebNewTopics, &topic);
    mndReleaseConsumer(pMnode, pConsumerOld);
    if (mndSetConsumerCommitLogs(pMnode, pTrans, pConsumerNew) != 0) {
L
Liu Jicong 已提交
546 547
      tDeleteSMqConsumerObj(pConsumerNew);
      taosMemoryFree(pConsumerNew);
548 549
      goto REB_FAIL;
    }
L
Liu Jicong 已提交
550 551
    tDeleteSMqConsumerObj(pConsumerNew);
    taosMemoryFree(pConsumerNew);
552 553 554 555 556 557
  }

  // 3.3 set removed consumer
  consumerNum = taosArrayGetSize(pOutput->removedConsumers);
  for (int32_t i = 0; i < consumerNum; i++) {
    int64_t consumerId = *(int64_t *)taosArrayGet(pOutput->removedConsumers, i);
558

559 560 561 562 563
    SMqConsumerObj *pConsumerOld = mndAcquireConsumer(pMnode, consumerId);
    SMqConsumerObj *pConsumerNew = tNewSMqConsumerObj(pConsumerOld->consumerId, pConsumerOld->cgroup);
    pConsumerNew->updateType = CONSUMER_UPDATE__REMOVE;
    char *topic = taosMemoryCalloc(1, TSDB_TOPIC_FNAME_LEN);
    char  cgroup[TSDB_CGROUP_LEN];
L
Liu Jicong 已提交
564
    mndSplitSubscribeKey(pOutput->pSub->key, topic, cgroup, true);
565 566 567
    taosArrayPush(pConsumerNew->rebRemovedTopics, &topic);
    mndReleaseConsumer(pMnode, pConsumerOld);
    if (mndSetConsumerCommitLogs(pMnode, pTrans, pConsumerNew) != 0) {
L
Liu Jicong 已提交
568 569
      tDeleteSMqConsumerObj(pConsumerNew);
      taosMemoryFree(pConsumerNew);
570 571
      goto REB_FAIL;
    }
L
Liu Jicong 已提交
572 573
    tDeleteSMqConsumerObj(pConsumerNew);
    taosMemoryFree(pConsumerNew);
574
  }
L
Liu Jicong 已提交
575

L
Liu Jicong 已提交
576 577
  // 4. TODO commit log: modification log

L
Liu Jicong 已提交
578
  // 5. set cb
579
  mndTransSetCb(pTrans, TRANS_START_FUNC_MQ_REB, TRANS_STOP_FUNC_MQ_REB, NULL, 0);
L
Liu Jicong 已提交
580 581

  // 6. execution
L
Liu Jicong 已提交
582
  if (mndTransPrepare(pMnode, pTrans) != 0) {
L
Liu Jicong 已提交
583
    mError("failed to prepare trans rebalance since %s", terrstr());
L
Liu Jicong 已提交
584 585
    goto REB_FAIL;
  }
586 587 588 589 590 591 592 593 594

  mndTransDrop(pTrans);
  return 0;

REB_FAIL:
  mndTransDrop(pTrans);
  return -1;
}

S
Shengliang Guan 已提交
595 596 597
static int32_t mndProcessRebalanceReq(SRpcMsg *pMsg) {
  SMnode            *pMnode = pMsg->info.node;
  SMqDoRebalanceMsg *pReq = pMsg->pCont;
598
  void              *pIter = NULL;
H
Haojun Liao 已提交
599
  bool               rebalanceOnce = false;  // to ensure only once.
600

601
  mInfo("mq re-balance start, total required re-balanced trans:%d", taosHashGetSize(pReq->rebSubHash));
602

603
  // here we only handle one topic rebalance requirement to ensure the atomic execution of this transaction.
604
  while (1) {
H
Haojun Liao 已提交
605
    if (rebalanceOnce) {
606 607 608
      break;
    }

609
    pIter = taosHashIterate(pReq->rebSubHash, pIter);
610 611 612 613
    if (pIter == NULL) {
      break;
    }

H
Haojun Liao 已提交
614
    // todo handle the malloc failure
615
    SMqRebInputObj rebInput = {0};
L
Liu Jicong 已提交
616
    SMqRebOutputObj rebOutput = {0};
L
Liu Jicong 已提交
617 618 619
    rebOutput.newConsumers = taosArrayInit(0, sizeof(int64_t));
    rebOutput.removedConsumers = taosArrayInit(0, sizeof(int64_t));
    rebOutput.touchedConsumers = taosArrayInit(0, sizeof(int64_t));
L
Liu Jicong 已提交
620 621
    rebOutput.rebVgs = taosArrayInit(0, sizeof(SMqRebOutputVg));

L
Liu Jicong 已提交
622 623 624 625
    SMqRebInfo      *pRebInfo = (SMqRebInfo *)pIter;
    SMqSubscribeObj *pSub = mndAcquireSubscribeByKey(pMnode, pRebInfo->key);

    rebInput.pRebInfo = pRebInfo;
626 627 628 629 630

    if (pSub == NULL) {
      // split sub key and extract topic
      char topic[TSDB_TOPIC_FNAME_LEN];
      char cgroup[TSDB_CGROUP_LEN];
L
Liu Jicong 已提交
631
      mndSplitSubscribeKey(pRebInfo->key, topic, cgroup, true);
632

633
      SMqTopicObj *pTopic = mndAcquireTopic(pMnode, topic);
L
Liu Jicong 已提交
634
      if (pTopic == NULL) {
635
        mError("mq re-balance %s ignored since topic %s doesn't exist", pRebInfo->key, topic);
L
Liu Jicong 已提交
636 637
        continue;
      }
638

639 640
      taosRLockLatch(&pTopic->lock);

641
      rebOutput.pSub = mndCreateSubscription(pMnode, pTopic, pRebInfo->key);
L
Liu Jicong 已提交
642 643

      if (rebOutput.pSub == NULL) {
644
        mError("mq rebalance %s failed create sub since %s, ignore", pRebInfo->key, terrstr());
L
Liu Jicong 已提交
645 646 647 648
        taosRUnLockLatch(&pTopic->lock);
        mndReleaseTopic(pMnode, pTopic);
        continue;
      }
L
Liu Jicong 已提交
649

650
      memcpy(rebOutput.pSub->dbName, pTopic->db, TSDB_DB_FNAME_LEN);
L
Liu Jicong 已提交
651 652 653 654
      taosRUnLockLatch(&pTopic->lock);
      mndReleaseTopic(pMnode, pTopic);

      rebInput.oldConsumerNum = 0;
655
      mInfo("topic:%s has no consumers sub yet", topic);
L
Liu Jicong 已提交
656 657 658 659 660
    } else {
      taosRLockLatch(&pSub->lock);
      rebInput.oldConsumerNum = taosHashGetSize(pSub->consumerHash);
      rebOutput.pSub = tCloneSubscribeObj(pSub);
      taosRUnLockLatch(&pSub->lock);
661
      mInfo("topic:%s has %d consumers sub till now", pRebInfo->key, rebInput.oldConsumerNum);
L
Liu Jicong 已提交
662 663
      mndReleaseSubscribe(pMnode, pSub);
    }
664

665
    if (mndDoRebalance(pMnode, &rebInput, &rebOutput) < 0) {
666
      mError("mq re-balance internal error");
667
    }
668

L
Liu Jicong 已提交
669 670
    // if add more consumer to balanced subscribe,
    // possibly no vg is changed
671
    // when each topic is re-balanced, issue an trans to save the results in sdb.
L
Liu Jicong 已提交
672
    if (mndPersistRebResult(pMnode, pMsg, &rebOutput) < 0) {
673
      mError("mq re-balance persist output error, possibly vnode splitted or dropped");
L
Liu Jicong 已提交
674
    }
675

L
Liu Jicong 已提交
676 677 678 679 680 681
    taosArrayDestroy(rebOutput.newConsumers);
    taosArrayDestroy(rebOutput.touchedConsumers);
    taosArrayDestroy(rebOutput.removedConsumers);
    taosArrayDestroy(rebOutput.rebVgs);
    tDeleteSubscribeObj(rebOutput.pSub);
    taosMemoryFree(rebOutput.pSub);
682

H
Haojun Liao 已提交
683
    rebalanceOnce = true;
684 685 686
  }

  // reset flag
687
  mInfo("mq re-balance completed successfully");
688
  taosHashCleanup(pReq->rebSubHash);
L
Liu Jicong 已提交
689
  mndRebEnd();
690 691 692

  return 0;
}
L
Liu Jicong 已提交
693

694 695
static int32_t mndProcessDropCgroupReq(SRpcMsg *pMsg) {
  SMnode         *pMnode = pMsg->info.node;
L
Liu Jicong 已提交
696 697
  SMDropCgroupReq dropReq = {0};

698
  if (tDeserializeSMDropCgroupReq(pMsg->pCont, pMsg->contLen, &dropReq) != 0) {
L
Liu Jicong 已提交
699 700 701 702 703 704 705
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }

  SMqSubscribeObj *pSub = mndAcquireSubscribe(pMnode, dropReq.cgroup, dropReq.topic);
  if (pSub == NULL) {
    if (dropReq.igNotExists) {
706
      mInfo("cgroup:%s on topic:%s, not exist, ignore not exist is set", dropReq.cgroup, dropReq.topic);
L
Liu Jicong 已提交
707 708 709 710 711 712 713 714
      return 0;
    } else {
      terrno = TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
      mError("topic:%s, cgroup:%s, failed to drop since %s", dropReq.topic, dropReq.cgroup, terrstr());
      return -1;
    }
  }

L
Liu Jicong 已提交
715
  if (taosHashGetSize(pSub->consumerHash) != 0) {
L
Liu Jicong 已提交
716 717
    terrno = TSDB_CODE_MND_CGROUP_USED;
    mError("cgroup:%s on topic:%s, failed to drop since %s", dropReq.cgroup, dropReq.topic, terrstr());
L
Liu Jicong 已提交
718
    mndReleaseSubscribe(pMnode, pSub);
L
Liu Jicong 已提交
719 720 721
    return -1;
  }

722
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pMsg, "drop-cgroup");
L
Liu Jicong 已提交
723 724
  if (pTrans == NULL) {
    mError("cgroup: %s on topic:%s, failed to drop since %s", dropReq.cgroup, dropReq.topic, terrstr());
L
Liu Jicong 已提交
725
    mndReleaseSubscribe(pMnode, pSub);
L
Liu Jicong 已提交
726
    mndTransDrop(pTrans);
L
Liu Jicong 已提交
727 728 729
    return -1;
  }

730
  mInfo("trans:%d, used to drop cgroup:%s on topic %s", pTrans->id, dropReq.cgroup, dropReq.topic);
L
Liu Jicong 已提交
731 732 733

  if (mndSetDropSubCommitLogs(pMnode, pTrans, pSub) < 0) {
    mError("cgroup %s on topic:%s, failed to drop since %s", dropReq.cgroup, dropReq.topic, terrstr());
L
Liu Jicong 已提交
734
    mndReleaseSubscribe(pMnode, pSub);
L
Liu Jicong 已提交
735
    mndTransDrop(pTrans);
L
Liu Jicong 已提交
736 737 738
    return -1;
  }

L
Liu Jicong 已提交
739 740 741 742 743
  if (mndTransPrepare(pMnode, pTrans) < 0) {
    mndReleaseSubscribe(pMnode, pSub);
    mndTransDrop(pTrans);
    return -1;
  }
L
Liu Jicong 已提交
744 745 746 747 748
  mndReleaseSubscribe(pMnode, pSub);

  return TSDB_CODE_ACTION_IN_PROGRESS;
}

L
Liu Jicong 已提交
749 750 751
void mndCleanupSubscribe(SMnode *pMnode) {}

static SSdbRaw *mndSubActionEncode(SMqSubscribeObj *pSub) {
L
Liu Jicong 已提交
752
  terrno = TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
753
  void   *buf = NULL;
L
Liu Jicong 已提交
754
  int32_t tlen = tEncodeSubscribeObj(NULL, pSub);
S
Shengliang Guan 已提交
755
  if (tlen <= 0) goto SUB_ENCODE_OVER;
L
Liu Jicong 已提交
756
  int32_t size = sizeof(int32_t) + tlen + MND_SUBSCRIBE_RESERVE_SIZE;
L
Liu Jicong 已提交
757 758 759 760

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

wafwerar's avatar
wafwerar 已提交
761
  buf = taosMemoryMalloc(tlen);
L
Liu Jicong 已提交
762
  if (buf == NULL) goto SUB_ENCODE_OVER;
L
Liu Jicong 已提交
763

L
Liu Jicong 已提交
764 765
  void *abuf = buf;
  tEncodeSubscribeObj(&abuf, pSub);
L
Liu Jicong 已提交
766 767 768 769 770 771 772

  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 已提交
773 774
  terrno = TSDB_CODE_SUCCESS;

L
Liu Jicong 已提交
775
SUB_ENCODE_OVER:
wafwerar's avatar
wafwerar 已提交
776
  taosMemoryFreeClear(buf);
L
Liu Jicong 已提交
777
  if (terrno != TSDB_CODE_SUCCESS) {
L
Liu Jicong 已提交
778 779 780 781 782 783 784 785 786 787 788
    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;
789 790 791
  SSdbRow         *pRow = NULL;
  SMqSubscribeObj *pSub = NULL;
  void            *buf = NULL;
L
Liu Jicong 已提交
792 793 794 795 796 797 798 799 800

  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;
  }

801
  pRow = sdbAllocRow(sizeof(SMqSubscribeObj));
L
Liu Jicong 已提交
802 803
  if (pRow == NULL) goto SUB_DECODE_OVER;

804
  pSub = sdbGetRowObj(pRow);
L
Liu Jicong 已提交
805 806 807 808
  if (pSub == NULL) goto SUB_DECODE_OVER;

  int32_t dataPos = 0;
  int32_t tlen;
L
Liu Jicong 已提交
809
  SDB_GET_INT32(pRaw, dataPos, &tlen, SUB_DECODE_OVER);
L
Liu Jicong 已提交
810
  buf = taosMemoryMalloc(tlen);
L
Liu Jicong 已提交
811 812 813 814 815 816 817 818
  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;
  }

819 820 821 822
  // update epset saved in mnode
  if (pSub->unassignedVgs != NULL) {
    int32_t size = (int32_t)taosArrayGetSize(pSub->unassignedVgs);
    for (int32_t i = 0; i < size; ++i) {
wmmhello's avatar
wmmhello 已提交
823
      SMqVgEp *pMqVgEp = (SMqVgEp *)taosArrayGetP(pSub->unassignedVgs, i);
824 825 826 827 828 829 830 831 832
      tmsgUpdateDnodeEpSet(&pMqVgEp->epSet);
    }
  }
  if (pSub->consumerHash != NULL) {
    void *pIter = taosHashIterate(pSub->consumerHash, NULL);
    while (pIter) {
      SMqConsumerEp *pConsumerEp = pIter;
      int32_t        size = (int32_t)taosArrayGetSize(pConsumerEp->vgs);
      for (int32_t i = 0; i < size; ++i) {
wmmhello's avatar
wmmhello 已提交
833
        SMqVgEp *pMqVgEp = (SMqVgEp *)taosArrayGetP(pConsumerEp->vgs, i);
834 835 836 837 838 839
        tmsgUpdateDnodeEpSet(&pMqVgEp->epSet);
      }
      pIter = taosHashIterate(pSub->consumerHash, pIter);
    }
  }

L
Liu Jicong 已提交
840 841
  terrno = TSDB_CODE_SUCCESS;

L
Liu Jicong 已提交
842
SUB_DECODE_OVER:
wafwerar's avatar
wafwerar 已提交
843
  taosMemoryFreeClear(buf);
L
Liu Jicong 已提交
844
  if (terrno != TSDB_CODE_SUCCESS) {
845
    mError("subscribe:%s, failed to decode from raw:%p since %s", pSub == NULL ? "null" : pSub->key, pRaw, terrstr());
wafwerar's avatar
wafwerar 已提交
846
    taosMemoryFreeClear(pRow);
L
Liu Jicong 已提交
847 848 849
    return NULL;
  }

S
Shengliang Guan 已提交
850
  mTrace("subscribe:%s, decode from raw:%p, row:%p", pSub->key, pRaw, pSub);
L
Liu Jicong 已提交
851 852 853 854 855 856 857 858 859 860
  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);
861
  tDeleteSubscribeObj(pSub);
L
Liu Jicong 已提交
862 863 864 865 866
  return 0;
}

static int32_t mndSubActionUpdate(SSdb *pSdb, SMqSubscribeObj *pOldSub, SMqSubscribeObj *pNewSub) {
  mTrace("subscribe:%s, perform update action", pOldSub->key);
867 868 869 870 871 872
  taosWLockLatch(&pOldSub->lock);

  SHashObj *tmp = pOldSub->consumerHash;
  pOldSub->consumerHash = pNewSub->consumerHash;
  pNewSub->consumerHash = tmp;

873 874 875 876
  SArray *tmp1 = pOldSub->unassignedVgs;
  pOldSub->unassignedVgs = pNewSub->unassignedVgs;
  pNewSub->unassignedVgs = tmp1;

877
  taosWUnLockLatch(&pOldSub->lock);
L
Liu Jicong 已提交
878 879 880
  return 0;
}

881
int32_t mndMakeSubscribeKey(char *key, const char *cgroup, const char *topicName) {
S
Shengliang Guan 已提交
882
  int32_t tlen = strlen(cgroup);
L
Liu Jicong 已提交
883
  memcpy(key, cgroup, tlen);
L
Liu Jicong 已提交
884
  key[tlen] = TMQ_SEPARATOR;
L
Liu Jicong 已提交
885
  strcpy(key + tlen + 1, topicName);
L
Liu Jicong 已提交
886
  return 0;
L
Liu Jicong 已提交
887 888
}

L
Liu Jicong 已提交
889
SMqSubscribeObj *mndAcquireSubscribe(SMnode *pMnode, const char *cgroup, const char *topicName) {
L
Liu Jicong 已提交
890 891 892
  SSdb *pSdb = pMnode->pSdb;
  char  key[TSDB_SUBSCRIBE_KEY_LEN];
  mndMakeSubscribeKey(key, cgroup, topicName);
L
Liu Jicong 已提交
893 894
  SMqSubscribeObj *pSub = sdbAcquire(pSdb, SDB_SUBSCRIBE, key);
  if (pSub == NULL) {
895
    terrno = TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
L
Liu Jicong 已提交
896 897 898 899
  }
  return pSub;
}

L
Liu Jicong 已提交
900 901 902 903
SMqSubscribeObj *mndAcquireSubscribeByKey(SMnode *pMnode, const char *key) {
  SSdb            *pSdb = pMnode->pSdb;
  SMqSubscribeObj *pSub = sdbAcquire(pSdb, SDB_SUBSCRIBE, key);
  if (pSub == NULL) {
904
    terrno = TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
L
Liu Jicong 已提交
905 906 907 908
  }
  return pSub;
}

L
Liu Jicong 已提交
909 910 911 912 913
void mndReleaseSubscribe(SMnode *pMnode, SMqSubscribeObj *pSub) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pSub);
}

L
Liu Jicong 已提交
914 915 916 917 918 919 920 921
static int32_t mndSetDropSubRedoLogs(SMnode *pMnode, STrans *pTrans, SMqSubscribeObj *pSub) {
  SSdbRaw *pRedoRaw = mndSubActionEncode(pSub);
  if (pRedoRaw == NULL) return -1;
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) return -1;
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_DROPPED) != 0) return -1;
  return 0;
}

L
Liu Jicong 已提交
922
int32_t mndSetDropSubCommitLogs(SMnode *pMnode, STrans *pTrans, SMqSubscribeObj *pSub) {
L
Liu Jicong 已提交
923 924 925 926 927 928 929 930
  SSdbRaw *pCommitRaw = mndSubActionEncode(pSub);
  if (pCommitRaw == NULL) return -1;
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) return -1;
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED) != 0) return -1;
  return 0;
}

int32_t mndDropSubByDB(SMnode *pMnode, STrans *pTrans, SDbObj *pDb) {
931
  int32_t code = 0;
L
Liu Jicong 已提交
932 933 934 935 936 937 938 939 940 941 942 943 944 945
  SSdb   *pSdb = pMnode->pSdb;

  void            *pIter = NULL;
  SMqSubscribeObj *pSub = NULL;
  while (1) {
    pIter = sdbFetch(pSdb, SDB_SUBSCRIBE, pIter, (void **)&pSub);
    if (pIter == NULL) break;

    if (pSub->dbUid != pDb->uid) {
      sdbRelease(pSdb, pSub);
      continue;
    }

    if (mndSetDropSubCommitLogs(pMnode, pTrans, pSub) < 0) {
L
Liu Jicong 已提交
946
      sdbRelease(pSdb, pSub);
947 948 949
      sdbCancelFetch(pSdb, pIter);
      code = -1;
      break;
L
Liu Jicong 已提交
950
    }
951 952

    sdbRelease(pSdb, pSub);
L
Liu Jicong 已提交
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
  }

  return code;
}

int32_t mndDropSubByTopic(SMnode *pMnode, STrans *pTrans, const char *topicName) {
  int32_t code = -1;
  SSdb   *pSdb = pMnode->pSdb;

  void            *pIter = NULL;
  SMqSubscribeObj *pSub = NULL;
  while (1) {
    pIter = sdbFetch(pSdb, SDB_SUBSCRIBE, pIter, (void **)&pSub);
    if (pIter == NULL) break;

    char topic[TSDB_TOPIC_FNAME_LEN];
    char cgroup[TSDB_CGROUP_LEN];
    mndSplitSubscribeKey(pSub->key, topic, cgroup, true);
    if (strcmp(topic, topicName) != 0) {
      sdbRelease(pSdb, pSub);
      continue;
    }

    // iter all vnode to delete handle
977 978
    if (taosHashGetSize(pSub->consumerHash) != 0) {
      sdbRelease(pSdb, pSub);
L
Liu Jicong 已提交
979
      terrno = TSDB_CODE_MND_IN_REBALANCE;
980 981
      return -1;
    }
L
Liu Jicong 已提交
982 983 984 985 986 987 988 989 990 991 992 993
    int32_t sz = taosArrayGetSize(pSub->unassignedVgs);
    for (int32_t i = 0; i < sz; i++) {
      SMqVgEp       *pVgEp = taosArrayGetP(pSub->unassignedVgs, i);
      SMqVDeleteReq *pReq = taosMemoryCalloc(1, sizeof(SMqVDeleteReq));
      pReq->head.vgId = htonl(pVgEp->vgId);
      pReq->vgId = pVgEp->vgId;
      pReq->consumerId = -1;
      memcpy(pReq->subKey, pSub->key, TSDB_SUBSCRIBE_KEY_LEN);
      STransAction action = {0};
      action.epSet = pVgEp->epSet;
      action.pCont = pReq;
      action.contLen = sizeof(SMqVDeleteReq);
L
Liu Jicong 已提交
994
      action.msgType = TDMT_VND_TMQ_DELETE_SUB;
L
Liu Jicong 已提交
995 996
      if (mndTransAppendRedoAction(pTrans, &action) != 0) {
        taosMemoryFree(pReq);
S
Shengliang Guan 已提交
997
        sdbRelease(pSdb, pSub);
L
Liu Jicong 已提交
998 999 1000
        return -1;
      }
    }
L
Liu Jicong 已提交
1001 1002 1003

    if (mndSetDropSubRedoLogs(pMnode, pTrans, pSub) < 0) {
      sdbRelease(pSdb, pSub);
L
Liu Jicong 已提交
1004 1005
      goto END;
    }
S
Shengliang Guan 已提交
1006 1007

    sdbRelease(pSdb, pSub);
L
Liu Jicong 已提交
1008 1009 1010 1011 1012 1013
  }

  code = 0;
END:
  return code;
}
L
Liu Jicong 已提交
1014

1015
int32_t mndRetrieveSubscribe(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rowsCapacity) {
S
Shengliang Guan 已提交
1016
  SMnode          *pMnode = pReq->info.node;
L
Liu Jicong 已提交
1017 1018 1019 1020
  SSdb            *pSdb = pMnode->pSdb;
  int32_t          numOfRows = 0;
  SMqSubscribeObj *pSub = NULL;

L
Liu Jicong 已提交
1021 1022
  mDebug("mnd show subscriptions begin");

L
Liu Jicong 已提交
1023 1024
  while (numOfRows < rowsCapacity) {
    pShow->pIter = sdbFetch(pSdb, SDB_SUBSCRIBE, pShow->pIter, (void **)&pSub);
1025 1026 1027
    if (pShow->pIter == NULL) {
      break;
    }
L
Liu Jicong 已提交
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051

    taosRLockLatch(&pSub->lock);

    if (numOfRows + pSub->vgNum > rowsCapacity) {
      blockDataEnsureCapacity(pBlock, numOfRows + pSub->vgNum);
    }

    SMqConsumerEp *pConsumerEp = NULL;
    void          *pIter = NULL;
    while (1) {
      pIter = taosHashIterate(pSub->consumerHash, pIter);
      if (pIter == NULL) break;
      pConsumerEp = (SMqConsumerEp *)pIter;

      int32_t sz = taosArrayGetSize(pConsumerEp->vgs);
      for (int32_t j = 0; j < sz; j++) {
        SMqVgEp *pVgEp = taosArrayGetP(pConsumerEp->vgs, j);

        SColumnInfoData *pColInfo;
        int32_t          cols = 0;

        // topic and cgroup
        char topic[TSDB_TOPIC_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
        char cgroup[TSDB_CGROUP_LEN + VARSTR_HEADER_SIZE] = {0};
L
Liu Jicong 已提交
1052
        mndSplitSubscribeKey(pSub->key, varDataVal(topic), varDataVal(cgroup), false);
L
Liu Jicong 已提交
1053 1054 1055 1056
        varDataSetLen(topic, strlen(varDataVal(topic)));
        varDataSetLen(cgroup, strlen(varDataVal(cgroup)));

        pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1057
        colDataSetVal(pColInfo, numOfRows, (const char *)topic, false);
L
Liu Jicong 已提交
1058 1059

        pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1060
        colDataSetVal(pColInfo, numOfRows, (const char *)cgroup, false);
L
Liu Jicong 已提交
1061 1062 1063

        // vg id
        pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1064
        colDataSetVal(pColInfo, numOfRows, (const char *)&pVgEp->vgId, false);
L
Liu Jicong 已提交
1065 1066 1067

        // consumer id
        pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1068
        colDataSetVal(pColInfo, numOfRows, (const char *)&pConsumerEp->consumerId, false);
L
Liu Jicong 已提交
1069

H
Haojun Liao 已提交
1070
        mDebug("mnd show subscriptions: topic %s, consumer:0x%" PRIx64 " cgroup %s vgid %d", varDataVal(topic),
L
Liu Jicong 已提交
1071 1072
               pConsumerEp->consumerId, varDataVal(cgroup), pVgEp->vgId);

L
Liu Jicong 已提交
1073 1074 1075 1076
        // offset
#if 0
      // subscribe time
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1077
      colDataSetVal(pColInfo, numOfRows, (const char *)&pSub->subscribeTime, false);
L
Liu Jicong 已提交
1078 1079 1080

      // rebalance time
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1081
      colDataSetVal(pColInfo, numOfRows, (const char *)&pSub->rebalanceTime, pConsumer->rebalanceTime == 0);
L
Liu Jicong 已提交
1082 1083 1084 1085 1086 1087
#endif

        numOfRows++;
      }
    }

L
Liu Jicong 已提交
1088
    // do not show for cleared subscription
L
Liu Jicong 已提交
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
    int32_t sz = taosArrayGetSize(pSub->unassignedVgs);
    for (int32_t i = 0; i < sz; i++) {
      SMqVgEp *pVgEp = taosArrayGetP(pSub->unassignedVgs, i);

      SColumnInfoData *pColInfo;
      int32_t          cols = 0;

      // topic and cgroup
      char topic[TSDB_TOPIC_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
      char cgroup[TSDB_CGROUP_LEN + VARSTR_HEADER_SIZE] = {0};
L
Liu Jicong 已提交
1099
      mndSplitSubscribeKey(pSub->key, varDataVal(topic), varDataVal(cgroup), false);
L
Liu Jicong 已提交
1100 1101 1102 1103
      varDataSetLen(topic, strlen(varDataVal(topic)));
      varDataSetLen(cgroup, strlen(varDataVal(cgroup)));

      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1104
      colDataSetVal(pColInfo, numOfRows, (const char *)topic, false);
L
Liu Jicong 已提交
1105 1106

      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1107
      colDataSetVal(pColInfo, numOfRows, (const char *)cgroup, false);
L
Liu Jicong 已提交
1108 1109 1110

      // vg id
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1111
      colDataSetVal(pColInfo, numOfRows, (const char *)&pVgEp->vgId, false);
L
Liu Jicong 已提交
1112 1113 1114

      // consumer id
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1115
      colDataSetVal(pColInfo, numOfRows, NULL, true);
L
Liu Jicong 已提交
1116

1117
      mDebug("mnd show subscriptions(unassigned): topic %s, cgroup %s vgid %d", varDataVal(topic), varDataVal(cgroup),
L
Liu Jicong 已提交
1118 1119
             pVgEp->vgId);

L
Liu Jicong 已提交
1120 1121 1122 1123
      // offset
#if 0
      // subscribe time
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1124
      colDataSetVal(pColInfo, numOfRows, (const char *)&pSub->subscribeTime, false);
L
Liu Jicong 已提交
1125 1126 1127

      // rebalance time
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1128
      colDataSetVal(pColInfo, numOfRows, (const char *)&pSub->rebalanceTime, pConsumer->rebalanceTime == 0);
L
Liu Jicong 已提交
1129 1130 1131 1132 1133
#endif

      numOfRows++;
    }

1134 1135
    pBlock->info.rows = numOfRows;

L
Liu Jicong 已提交
1136 1137 1138 1139
    taosRUnLockLatch(&pSub->lock);
    sdbRelease(pSdb, pSub);
  }

L
Liu Jicong 已提交
1140 1141
  mDebug("mnd end show subscriptions");

L
Liu Jicong 已提交
1142 1143 1144 1145
  pShow->numOfRows += numOfRows;
  return numOfRows;
}

1146
void mndCancelGetNextSubscribe(SMnode *pMnode, void *pIter) {
L
Liu Jicong 已提交
1147 1148 1149
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
}