mndDef.c 25.0 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 "mndDef.h"
18 19
#include "mndConsumer.h"

L
Liu Jicong 已提交
20
int32_t tEncodeSStreamObj(SEncoder *pEncoder, const SStreamObj *pObj) {
L
Liu Jicong 已提交
21
  if (tStartEncode(pEncoder) < 0) return -1;
L
Liu Jicong 已提交
22
  if (tEncodeCStr(pEncoder, pObj->name) < 0) return -1;
L
Liu Jicong 已提交
23

L
Liu Jicong 已提交
24 25 26
  if (tEncodeI64(pEncoder, pObj->createTime) < 0) return -1;
  if (tEncodeI64(pEncoder, pObj->updateTime) < 0) return -1;
  if (tEncodeI32(pEncoder, pObj->version) < 0) return -1;
L
Liu Jicong 已提交
27
  if (tEncodeI32(pEncoder, pObj->totalLevel) < 0) return -1;
L
Liu Jicong 已提交
28 29 30
  if (tEncodeI64(pEncoder, pObj->smaId) < 0) return -1;

  if (tEncodeI64(pEncoder, pObj->uid) < 0) return -1;
L
Liu Jicong 已提交
31
  if (tEncodeI8(pEncoder, pObj->status) < 0) return -1;
L
Liu Jicong 已提交
32

33
  if (tEncodeI8(pEncoder, pObj->igExpired) < 0) return -1;
L
Liu Jicong 已提交
34
  if (tEncodeI8(pEncoder, pObj->trigger) < 0) return -1;
L
Liu Jicong 已提交
35
  if (tEncodeI8(pEncoder, pObj->fillHistory) < 0) return -1;
36 37
  if (tEncodeI64(pEncoder, pObj->triggerParam) < 0) return -1;
  if (tEncodeI64(pEncoder, pObj->watermark) < 0) return -1;
L
Liu Jicong 已提交
38 39 40 41 42 43 44

  if (tEncodeI64(pEncoder, pObj->sourceDbUid) < 0) return -1;
  if (tEncodeI64(pEncoder, pObj->targetDbUid) < 0) return -1;
  if (tEncodeCStr(pEncoder, pObj->sourceDb) < 0) return -1;
  if (tEncodeCStr(pEncoder, pObj->targetDb) < 0) return -1;
  if (tEncodeCStr(pEncoder, pObj->targetSTbName) < 0) return -1;
  if (tEncodeI64(pEncoder, pObj->targetStbUid) < 0) return -1;
L
Liu Jicong 已提交
45
  if (tEncodeI32(pEncoder, pObj->fixedSinkVgId) < 0) return -1;
L
Liu Jicong 已提交
46

S
Shengliang Guan 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
  if (pObj->sql != NULL) {
    if (tEncodeCStr(pEncoder, pObj->sql) < 0) return -1;
  } else {
    if (tEncodeCStr(pEncoder, "") < 0) return -1;
  }

  if (pObj->ast != NULL) {
    if (tEncodeCStr(pEncoder, pObj->ast) < 0) return -1;
  } else {
    if (tEncodeCStr(pEncoder, "") < 0) return -1;
  }

  if (pObj->physicalPlan != NULL) {
    if (tEncodeCStr(pEncoder, pObj->physicalPlan) < 0) return -1;
  } else {
    if (tEncodeCStr(pEncoder, "") < 0) return -1;
  }
L
Liu Jicong 已提交
64

L
Liu Jicong 已提交
65 66
  int32_t sz = taosArrayGetSize(pObj->tasks);
  if (tEncodeI32(pEncoder, sz) < 0) return -1;
L
Liu Jicong 已提交
67 68 69 70 71 72
  for (int32_t i = 0; i < sz; i++) {
    SArray *pArray = taosArrayGetP(pObj->tasks, i);
    int32_t innerSz = taosArrayGetSize(pArray);
    if (tEncodeI32(pEncoder, innerSz) < 0) return -1;
    for (int32_t j = 0; j < innerSz; j++) {
      SStreamTask *pTask = taosArrayGetP(pArray, j);
73
      if (tEncodeStreamTask(pEncoder, pTask) < 0) return -1;
L
Liu Jicong 已提交
74 75 76 77
    }
  }

  if (tEncodeSSchemaWrapper(pEncoder, &pObj->outputSchema) < 0) return -1;
L
Liu Jicong 已提交
78

79 80
  // 3.0.20
  if (tEncodeI64(pEncoder, pObj->checkpointFreq) < 0) return -1;
81
  if (tEncodeI8(pEncoder, pObj->igCheckUpdate) < 0) return -1;
82

L
Liu Jicong 已提交
83
  tEndEncode(pEncoder);
L
Liu Jicong 已提交
84 85 86
  return pEncoder->pos;
}

87
int32_t tDecodeSStreamObj(SDecoder *pDecoder, SStreamObj *pObj, int32_t sver) {
L
Liu Jicong 已提交
88
  if (tStartDecode(pDecoder) < 0) return -1;
L
Liu Jicong 已提交
89
  if (tDecodeCStrTo(pDecoder, pObj->name) < 0) return -1;
L
Liu Jicong 已提交
90

L
Liu Jicong 已提交
91 92 93
  if (tDecodeI64(pDecoder, &pObj->createTime) < 0) return -1;
  if (tDecodeI64(pDecoder, &pObj->updateTime) < 0) return -1;
  if (tDecodeI32(pDecoder, &pObj->version) < 0) return -1;
L
Liu Jicong 已提交
94
  if (tDecodeI32(pDecoder, &pObj->totalLevel) < 0) return -1;
L
Liu Jicong 已提交
95 96 97
  if (tDecodeI64(pDecoder, &pObj->smaId) < 0) return -1;

  if (tDecodeI64(pDecoder, &pObj->uid) < 0) return -1;
L
Liu Jicong 已提交
98
  if (tDecodeI8(pDecoder, &pObj->status) < 0) return -1;
L
Liu Jicong 已提交
99

100
  if (tDecodeI8(pDecoder, &pObj->igExpired) < 0) return -1;
L
Liu Jicong 已提交
101
  if (tDecodeI8(pDecoder, &pObj->trigger) < 0) return -1;
L
Liu Jicong 已提交
102
  if (tDecodeI8(pDecoder, &pObj->fillHistory) < 0) return -1;
103 104
  if (tDecodeI64(pDecoder, &pObj->triggerParam) < 0) return -1;
  if (tDecodeI64(pDecoder, &pObj->watermark) < 0) return -1;
L
Liu Jicong 已提交
105 106 107 108 109 110 111

  if (tDecodeI64(pDecoder, &pObj->sourceDbUid) < 0) return -1;
  if (tDecodeI64(pDecoder, &pObj->targetDbUid) < 0) return -1;
  if (tDecodeCStrTo(pDecoder, pObj->sourceDb) < 0) return -1;
  if (tDecodeCStrTo(pDecoder, pObj->targetDb) < 0) return -1;
  if (tDecodeCStrTo(pDecoder, pObj->targetSTbName) < 0) return -1;
  if (tDecodeI64(pDecoder, &pObj->targetStbUid) < 0) return -1;
L
Liu Jicong 已提交
112
  if (tDecodeI32(pDecoder, &pObj->fixedSinkVgId) < 0) return -1;
L
Liu Jicong 已提交
113

L
Liu Jicong 已提交
114
  if (tDecodeCStrAlloc(pDecoder, &pObj->sql) < 0) return -1;
L
Liu Jicong 已提交
115
  if (tDecodeCStrAlloc(pDecoder, &pObj->ast) < 0) return -1;
L
Liu Jicong 已提交
116
  if (tDecodeCStrAlloc(pDecoder, &pObj->physicalPlan) < 0) return -1;
L
Liu Jicong 已提交
117

L
Liu Jicong 已提交
118 119 120 121 122 123 124 125 126 127 128
  pObj->tasks = NULL;
  int32_t sz;
  if (tDecodeI32(pDecoder, &sz) < 0) return -1;
  if (sz != 0) {
    pObj->tasks = taosArrayInit(sz, sizeof(void *));
    for (int32_t i = 0; i < sz; i++) {
      int32_t innerSz;
      if (tDecodeI32(pDecoder, &innerSz) < 0) return -1;
      SArray *pArray = taosArrayInit(innerSz, sizeof(void *));
      for (int32_t j = 0; j < innerSz; j++) {
        SStreamTask *pTask = taosMemoryCalloc(1, sizeof(SStreamTask));
129 130 131 132
        if (pTask == NULL) {
          taosArrayDestroy(pArray);
          return -1;
        }
133
        if (tDecodeStreamTask(pDecoder, pTask) < 0) {
L
Liu Jicong 已提交
134
          taosMemoryFree(pTask);
135
          taosArrayDestroy(pArray);
L
Liu Jicong 已提交
136 137
          return -1;
        }
L
Liu Jicong 已提交
138 139 140 141 142 143 144
        taosArrayPush(pArray, &pTask);
      }
      taosArrayPush(pObj->tasks, &pArray);
    }
  }

  if (tDecodeSSchemaWrapper(pDecoder, &pObj->outputSchema) < 0) return -1;
L
Liu Jicong 已提交
145

146
  // 3.0.20
147 148
  if (sver >= 2) {
    if (tDecodeI64(pDecoder, &pObj->checkpointFreq) < 0) return -1;
149 150 151
    if (!tDecodeIsEnd(pDecoder)) {
      if (tDecodeI8(pDecoder, &pObj->igCheckUpdate) < 0) return -1;
    }
152
  }
L
Liu Jicong 已提交
153
  tEndDecode(pDecoder);
L
Liu Jicong 已提交
154 155 156
  return 0;
}

L
Liu Jicong 已提交
157 158 159 160
void tFreeStreamObj(SStreamObj *pStream) {
  taosMemoryFree(pStream->sql);
  taosMemoryFree(pStream->ast);
  taosMemoryFree(pStream->physicalPlan);
161 162 163 164

  if (pStream->outputSchema.nCols) {
    taosMemoryFree(pStream->outputSchema.pSchema);
  }
L
Liu Jicong 已提交
165 166 167 168 169 170 171

  int32_t sz = taosArrayGetSize(pStream->tasks);
  for (int32_t i = 0; i < sz; i++) {
    SArray *pLevel = taosArrayGetP(pStream->tasks, i);
    int32_t taskSz = taosArrayGetSize(pLevel);
    for (int32_t j = 0; j < taskSz; j++) {
      SStreamTask *pTask = taosArrayGetP(pLevel, j);
172
      tFreeStreamTask(pTask);
L
Liu Jicong 已提交
173
    }
174

L
Liu Jicong 已提交
175 176
    taosArrayDestroy(pLevel);
  }
177

L
Liu Jicong 已提交
178
  taosArrayDestroy(pStream->tasks);
179

5
54liuyao 已提交
180 181 182 183
  // tagSchema.pSchema
  if (pStream->tagSchema.nCols > 0) {
    taosMemoryFree(pStream->tagSchema.pSchema);
  }
L
Liu Jicong 已提交
184 185
}

L
Liu Jicong 已提交
186 187 188 189
SMqVgEp *tCloneSMqVgEp(const SMqVgEp *pVgEp) {
  SMqVgEp *pVgEpNew = taosMemoryMalloc(sizeof(SMqVgEp));
  if (pVgEpNew == NULL) return NULL;
  pVgEpNew->vgId = pVgEp->vgId;
190
  pVgEpNew->qmsg = taosStrdup(pVgEp->qmsg);
L
Liu Jicong 已提交
191 192 193 194 195
  pVgEpNew->epSet = pVgEp->epSet;
  return pVgEpNew;
}

void tDeleteSMqVgEp(SMqVgEp *pVgEp) {
L
Liu Jicong 已提交
196 197 198 199
  if (pVgEp) {
    taosMemoryFreeClear(pVgEp->qmsg);
    taosMemoryFree(pVgEp);
  }
L
Liu Jicong 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
}

int32_t tEncodeSMqVgEp(void **buf, const SMqVgEp *pVgEp) {
  int32_t tlen = 0;
  tlen += taosEncodeFixedI32(buf, pVgEp->vgId);
  tlen += taosEncodeString(buf, pVgEp->qmsg);
  tlen += taosEncodeSEpSet(buf, &pVgEp->epSet);
  return tlen;
}

void *tDecodeSMqVgEp(const void *buf, SMqVgEp *pVgEp) {
  buf = taosDecodeFixedI32(buf, &pVgEp->vgId);
  buf = taosDecodeString(buf, &pVgEp->qmsg);
  buf = taosDecodeSEpSet(buf, &pVgEp->epSet);
  return (void *)buf;
}

217 218 219
SMqConsumerObj *tNewSMqConsumerObj(int64_t consumerId, char cgroup[TSDB_CGROUP_LEN]) {
  SMqConsumerObj *pConsumer = taosMemoryCalloc(1, sizeof(SMqConsumerObj));
  if (pConsumer == NULL) {
S
Shengliang Guan 已提交
220
    terrno = TSDB_CODE_OUT_OF_MEMORY;
221 222 223 224 225 226 227
    return NULL;
  }

  pConsumer->consumerId = consumerId;
  memcpy(pConsumer->cgroup, cgroup, TSDB_CGROUP_LEN);

  pConsumer->epoch = 0;
228
  pConsumer->status = MQ_CONSUMER_STATUS_REBALANCE;
229 230 231 232 233 234 235
  pConsumer->hbStatus = 0;

  taosInitRWLatch(&pConsumer->lock);

  pConsumer->currentTopics = taosArrayInit(0, sizeof(void *));
  pConsumer->rebNewTopics = taosArrayInit(0, sizeof(void *));
  pConsumer->rebRemovedTopics = taosArrayInit(0, sizeof(void *));
L
Liu Jicong 已提交
236
  pConsumer->assignedTopics = taosArrayInit(0, sizeof(void *));
237

L
Liu Jicong 已提交
238 239
  if (pConsumer->currentTopics == NULL || pConsumer->rebNewTopics == NULL || pConsumer->rebRemovedTopics == NULL ||
      pConsumer->assignedTopics == NULL) {
240 241 242
    taosArrayDestroy(pConsumer->currentTopics);
    taosArrayDestroy(pConsumer->rebNewTopics);
    taosArrayDestroy(pConsumer->rebRemovedTopics);
L
Liu Jicong 已提交
243
    taosArrayDestroy(pConsumer->assignedTopics);
244 245 246 247
    taosMemoryFree(pConsumer);
    return NULL;
  }

L
Liu Jicong 已提交
248 249
  pConsumer->upTime = taosGetTimestampMs();

250 251 252 253
  return pConsumer;
}

void tDeleteSMqConsumerObj(SMqConsumerObj *pConsumer) {
L
Liu Jicong 已提交
254 255 256 257
  taosArrayDestroyP(pConsumer->currentTopics, (FDelete)taosMemoryFree);
  taosArrayDestroyP(pConsumer->rebNewTopics, (FDelete)taosMemoryFree);
  taosArrayDestroyP(pConsumer->rebRemovedTopics, (FDelete)taosMemoryFree);
  taosArrayDestroyP(pConsumer->assignedTopics, (FDelete)taosMemoryFree);
258 259 260 261 262 263
}

int32_t tEncodeSMqConsumerObj(void **buf, const SMqConsumerObj *pConsumer) {
  int32_t tlen = 0;
  int32_t sz;
  tlen += taosEncodeFixedI64(buf, pConsumer->consumerId);
L
Liu Jicong 已提交
264
  tlen += taosEncodeString(buf, pConsumer->clientId);
265 266 267 268 269
  tlen += taosEncodeString(buf, pConsumer->cgroup);
  tlen += taosEncodeFixedI8(buf, pConsumer->updateType);
  tlen += taosEncodeFixedI32(buf, pConsumer->epoch);
  tlen += taosEncodeFixedI32(buf, pConsumer->status);

L
Liu Jicong 已提交
270 271 272 273 274 275
  tlen += taosEncodeFixedI32(buf, pConsumer->pid);
  tlen += taosEncodeSEpSet(buf, &pConsumer->ep);
  tlen += taosEncodeFixedI64(buf, pConsumer->upTime);
  tlen += taosEncodeFixedI64(buf, pConsumer->subscribeTime);
  tlen += taosEncodeFixedI64(buf, pConsumer->rebalanceTime);

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
  // current topics
  if (pConsumer->currentTopics) {
    sz = taosArrayGetSize(pConsumer->currentTopics);
    tlen += taosEncodeFixedI32(buf, sz);
    for (int32_t i = 0; i < sz; i++) {
      char *topic = taosArrayGetP(pConsumer->currentTopics, i);
      tlen += taosEncodeString(buf, topic);
    }
  } else {
    tlen += taosEncodeFixedI32(buf, 0);
  }

  // reb new topics
  if (pConsumer->rebNewTopics) {
    sz = taosArrayGetSize(pConsumer->rebNewTopics);
    tlen += taosEncodeFixedI32(buf, sz);
    for (int32_t i = 0; i < sz; i++) {
      char *topic = taosArrayGetP(pConsumer->rebNewTopics, i);
      tlen += taosEncodeString(buf, topic);
    }
  } else {
    tlen += taosEncodeFixedI32(buf, 0);
  }

  // reb removed topics
  if (pConsumer->rebRemovedTopics) {
    sz = taosArrayGetSize(pConsumer->rebRemovedTopics);
    tlen += taosEncodeFixedI32(buf, sz);
    for (int32_t i = 0; i < sz; i++) {
      char *topic = taosArrayGetP(pConsumer->rebRemovedTopics, i);
      tlen += taosEncodeString(buf, topic);
    }
  } else {
    tlen += taosEncodeFixedI32(buf, 0);
  }

L
Liu Jicong 已提交
312 313 314 315 316 317 318 319 320 321 322 323
  // lost topics
  if (pConsumer->assignedTopics) {
    sz = taosArrayGetSize(pConsumer->assignedTopics);
    tlen += taosEncodeFixedI32(buf, sz);
    for (int32_t i = 0; i < sz; i++) {
      char *topic = taosArrayGetP(pConsumer->assignedTopics, i);
      tlen += taosEncodeString(buf, topic);
    }
  } else {
    tlen += taosEncodeFixedI32(buf, 0);
  }

324 325 326 327 328
  tlen += taosEncodeFixedI8(buf, pConsumer->withTbName);
  tlen += taosEncodeFixedI8(buf, pConsumer->useSnapshot);
  tlen += taosEncodeFixedI8(buf, pConsumer->autoCommit);
  tlen += taosEncodeFixedI32(buf, pConsumer->autoCommitInterval);
  tlen += taosEncodeFixedI32(buf, pConsumer->resetOffsetCfg);
329 330 331
  return tlen;
}

332
void *tDecodeSMqConsumerObj(const void *buf, SMqConsumerObj *pConsumer, int8_t sver) {
333 334
  int32_t sz;
  buf = taosDecodeFixedI64(buf, &pConsumer->consumerId);
L
Liu Jicong 已提交
335
  buf = taosDecodeStringTo(buf, pConsumer->clientId);
336 337 338 339 340
  buf = taosDecodeStringTo(buf, pConsumer->cgroup);
  buf = taosDecodeFixedI8(buf, &pConsumer->updateType);
  buf = taosDecodeFixedI32(buf, &pConsumer->epoch);
  buf = taosDecodeFixedI32(buf, &pConsumer->status);

L
Liu Jicong 已提交
341 342 343 344 345 346
  buf = taosDecodeFixedI32(buf, &pConsumer->pid);
  buf = taosDecodeSEpSet(buf, &pConsumer->ep);
  buf = taosDecodeFixedI64(buf, &pConsumer->upTime);
  buf = taosDecodeFixedI64(buf, &pConsumer->subscribeTime);
  buf = taosDecodeFixedI64(buf, &pConsumer->rebalanceTime);

347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
  // current topics
  buf = taosDecodeFixedI32(buf, &sz);
  pConsumer->currentTopics = taosArrayInit(sz, sizeof(void *));
  for (int32_t i = 0; i < sz; i++) {
    char *topic;
    buf = taosDecodeString(buf, &topic);
    taosArrayPush(pConsumer->currentTopics, &topic);
  }

  // reb new topics
  buf = taosDecodeFixedI32(buf, &sz);
  pConsumer->rebNewTopics = taosArrayInit(sz, sizeof(void *));
  for (int32_t i = 0; i < sz; i++) {
    char *topic;
    buf = taosDecodeString(buf, &topic);
    taosArrayPush(pConsumer->rebNewTopics, &topic);
  }

  // reb removed topics
  buf = taosDecodeFixedI32(buf, &sz);
  pConsumer->rebRemovedTopics = taosArrayInit(sz, sizeof(void *));
  for (int32_t i = 0; i < sz; i++) {
    char *topic;
    buf = taosDecodeString(buf, &topic);
    taosArrayPush(pConsumer->rebRemovedTopics, &topic);
  }

L
Liu Jicong 已提交
374 375 376 377 378 379 380 381 382
  // reb removed topics
  buf = taosDecodeFixedI32(buf, &sz);
  pConsumer->assignedTopics = taosArrayInit(sz, sizeof(void *));
  for (int32_t i = 0; i < sz; i++) {
    char *topic;
    buf = taosDecodeString(buf, &topic);
    taosArrayPush(pConsumer->assignedTopics, &topic);
  }

383 384 385 386 387 388 389
  if(sver > 1){
    buf = taosDecodeFixedI8(buf, &pConsumer->withTbName);
    buf = taosDecodeFixedI8(buf, &pConsumer->useSnapshot);
    buf = taosDecodeFixedI8(buf, &pConsumer->autoCommit);
    buf = taosDecodeFixedI32(buf, &pConsumer->autoCommitInterval);
    buf = taosDecodeFixedI32(buf, &pConsumer->resetOffsetCfg);
  }
390

391
  return (void *)buf;
392 393
}

394 395 396 397 398 399 400 401 402 403 404 405
//SMqConsumerEp *tCloneSMqConsumerEp(const SMqConsumerEp *pConsumerEpOld) {
//  SMqConsumerEp *pConsumerEpNew = taosMemoryMalloc(sizeof(SMqConsumerEp));
//  if (pConsumerEpNew == NULL) return NULL;
//  pConsumerEpNew->consumerId = pConsumerEpOld->consumerId;
//  pConsumerEpNew->vgs = taosArrayDup(pConsumerEpOld->vgs, (__array_item_dup_fn_t)tCloneSMqVgEp);
//  return pConsumerEpNew;
//}
//
//void tDeleteSMqConsumerEp(void *data) {
//  SMqConsumerEp *pConsumerEp = (SMqConsumerEp *)data;
//  taosArrayDestroyP(pConsumerEp->vgs, (FDelete)tDeleteSMqVgEp);
//}
406

407
int32_t tEncodeSMqConsumerEp(void **buf, const SMqConsumerEp *pConsumerEp) {
408
  int32_t tlen = 0;
409 410
  tlen += taosEncodeFixedI64(buf, pConsumerEp->consumerId);
  tlen += taosEncodeArray(buf, pConsumerEp->vgs, (FEncode)tEncodeSMqVgEp);
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
  int32_t szVgs = taosArrayGetSize(pConsumerEp->offsetRows);
  tlen += taosEncodeFixedI32(buf, szVgs);
  for (int32_t j= 0; j < szVgs; ++j) {
    OffsetRows *offRows = taosArrayGet(pConsumerEp->offsetRows, j);
    tlen += taosEncodeFixedI32(buf, offRows->vgId);
    tlen += taosEncodeFixedI64(buf, offRows->rows);
    tlen += taosEncodeFixedI8(buf, offRows->offset.type);
    if (offRows->offset.type == TMQ_OFFSET__SNAPSHOT_DATA || offRows->offset.type == TMQ_OFFSET__SNAPSHOT_META) {
      tlen += taosEncodeFixedI64(buf, offRows->offset.uid);
      tlen += taosEncodeFixedI64(buf, offRows->offset.ts);
    } else if (offRows->offset.type == TMQ_OFFSET__LOG) {
      tlen += taosEncodeFixedI64(buf, offRows->offset.version);
    } else {
      // do nothing
    }
L
Liu Jicong 已提交
426
  }
427 428 429 430 431 432 433 434
//#if 0
//  int32_t sz = taosArrayGetSize(pConsumerEp->vgs);
//  tlen += taosEncodeFixedI32(buf, sz);
//  for (int32_t i = 0; i < sz; i++) {
//    SMqVgEp *pVgEp = taosArrayGetP(pConsumerEp->vgs, i);
//    tlen += tEncodeSMqVgEp(buf, pVgEp);
//  }
//#endif
435 436 437
  return tlen;
}

438
void *tDecodeSMqConsumerEp(const void *buf, SMqConsumerEp *pConsumerEp, int8_t sver) {
439
  buf = taosDecodeFixedI64(buf, &pConsumerEp->consumerId);
L
Liu Jicong 已提交
440
  buf = taosDecodeArray(buf, &pConsumerEp->vgs, (FDecode)tDecodeSMqVgEp, sizeof(SMqVgEp));
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
  if (sver > 1){
    int32_t szVgs = 0;
    buf = taosDecodeFixedI32(buf, &szVgs);
    if(szVgs > 0){
      pConsumerEp->offsetRows = taosArrayInit(szVgs, sizeof(OffsetRows));
      if (NULL == pConsumerEp->offsetRows) return NULL;
      for (int32_t j= 0; j < szVgs; ++j) {
        OffsetRows* offRows = taosArrayReserve(pConsumerEp->offsetRows, 1);
        buf = taosDecodeFixedI32(buf, &offRows->vgId);
        buf = taosDecodeFixedI64(buf, &offRows->rows);
        buf = taosDecodeFixedI8(buf, &offRows->offset.type);
        if (offRows->offset.type == TMQ_OFFSET__SNAPSHOT_DATA || offRows->offset.type == TMQ_OFFSET__SNAPSHOT_META) {
          buf = taosDecodeFixedI64(buf, &offRows->offset.uid);
          buf = taosDecodeFixedI64(buf, &offRows->offset.ts);
        } else if (offRows->offset.type == TMQ_OFFSET__LOG) {
          buf = taosDecodeFixedI64(buf, &offRows->offset.version);
        } else {
          // do nothing
        }
      }
    }
L
Liu Jicong 已提交
462
  }
463 464 465 466 467 468 469 470 471 472
//#if 0
//  int32_t sz;
//  buf = taosDecodeFixedI32(buf, &sz);
//  pConsumerEp->vgs = taosArrayInit(sz, sizeof(void *));
//  for (int32_t i = 0; i < sz; i++) {
//    SMqVgEp *pVgEp = taosMemoryMalloc(sizeof(SMqVgEp));
//    buf = tDecodeSMqVgEp(buf, pVgEp);
//    taosArrayPush(pConsumerEp->vgs, &pVgEp);
//  }
//#endif
L
Liu Jicong 已提交
473

474 475 476
  return (void *)buf;
}

477 478 479 480 481
SMqSubscribeObj *tNewSubscribeObj(const char* key) {
  SMqSubscribeObj *pSubObj = taosMemoryCalloc(1, sizeof(SMqSubscribeObj));
  if (pSubObj == NULL) {
    return NULL;
  }
482

483 484 485 486
  memcpy(pSubObj->key, key, TSDB_SUBSCRIBE_KEY_LEN);
  taosInitRWLatch(&pSubObj->lock);
  pSubObj->vgNum = 0;
  pSubObj->consumerHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
487

488 489 490 491
  // TODO set hash free fp
  /*taosHashSetFreeFp(pSubObj->consumerHash, tDeleteSMqConsumerEp);*/
  pSubObj->unassignedVgs = taosArrayInit(0, POINTER_BYTES);
  return pSubObj;
492 493 494 495 496 497 498
}

SMqSubscribeObj *tCloneSubscribeObj(const SMqSubscribeObj *pSub) {
  SMqSubscribeObj *pSubNew = taosMemoryMalloc(sizeof(SMqSubscribeObj));
  if (pSubNew == NULL) return NULL;
  memcpy(pSubNew->key, pSub->key, TSDB_SUBSCRIBE_KEY_LEN);
  taosInitRWLatch(&pSubNew->lock);
L
Liu Jicong 已提交
499

L
Liu Jicong 已提交
500
  pSubNew->dbUid = pSub->dbUid;
L
Liu Jicong 已提交
501
  pSubNew->stbUid = pSub->stbUid;
L
Liu Jicong 已提交
502
  pSubNew->subType = pSub->subType;
L
Liu Jicong 已提交
503
  pSubNew->withMeta = pSub->withMeta;
L
Liu Jicong 已提交
504

505 506
  pSubNew->vgNum = pSub->vgNum;
  pSubNew->consumerHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
507 508 509 510
  // TODO set hash free fp
  /*taosHashSetFreeFp(pSubNew->consumerHash, tDeleteSMqConsumerEp);*/
  void          *pIter = NULL;
  SMqConsumerEp *pConsumerEp = NULL;
511
  while (1) {
L
Liu Jicong 已提交
512
    pIter = taosHashIterate(pSub->consumerHash, pIter);
513
    if (pIter == NULL) break;
514 515 516
    pConsumerEp = (SMqConsumerEp *)pIter;
    SMqConsumerEp newEp = {
        .consumerId = pConsumerEp->consumerId,
H
Haojun Liao 已提交
517
        .vgs = taosArrayDup(pConsumerEp->vgs, (__array_item_dup_fn_t)tCloneSMqVgEp),
518
    };
519
    taosHashPut(pSubNew->consumerHash, &newEp.consumerId, sizeof(int64_t), &newEp, sizeof(SMqConsumerEp));
520
  }
H
Haojun Liao 已提交
521
  pSubNew->unassignedVgs = taosArrayDup(pSub->unassignedVgs, (__array_item_dup_fn_t)tCloneSMqVgEp);
L
Liu Jicong 已提交
522
  memcpy(pSubNew->dbName, pSub->dbName, TSDB_DB_FNAME_LEN);
523 524 525 526
  return pSubNew;
}

void tDeleteSubscribeObj(SMqSubscribeObj *pSub) {
L
Liu Jicong 已提交
527 528 529 530 531 532
  void *pIter = NULL;
  while (1) {
    pIter = taosHashIterate(pSub->consumerHash, pIter);
    if (pIter == NULL) break;
    SMqConsumerEp *pConsumerEp = (SMqConsumerEp *)pIter;
    taosArrayDestroyP(pConsumerEp->vgs, (FDelete)tDeleteSMqVgEp);
533
    taosArrayDestroy(pConsumerEp->offsetRows);
L
Liu Jicong 已提交
534
  }
535
  taosHashCleanup(pSub->consumerHash);
536
  taosArrayDestroyP(pSub->unassignedVgs, (FDelete)tDeleteSMqVgEp);
537
  taosArrayDestroy(pSub->offsetRows);
538 539 540 541 542
}

int32_t tEncodeSubscribeObj(void **buf, const SMqSubscribeObj *pSub) {
  int32_t tlen = 0;
  tlen += taosEncodeString(buf, pSub->key);
L
Liu Jicong 已提交
543
  tlen += taosEncodeFixedI64(buf, pSub->dbUid);
544
  tlen += taosEncodeFixedI32(buf, pSub->vgNum);
L
Liu Jicong 已提交
545
  tlen += taosEncodeFixedI8(buf, pSub->subType);
L
Liu Jicong 已提交
546
  tlen += taosEncodeFixedI8(buf, pSub->withMeta);
L
Liu Jicong 已提交
547
  tlen += taosEncodeFixedI64(buf, pSub->stbUid);
548 549 550 551 552 553 554 555 556

  void   *pIter = NULL;
  int32_t sz = taosHashGetSize(pSub->consumerHash);
  tlen += taosEncodeFixedI32(buf, sz);

  int32_t cnt = 0;
  while (1) {
    pIter = taosHashIterate(pSub->consumerHash, pIter);
    if (pIter == NULL) break;
557 558
    SMqConsumerEp *pConsumerEp = (SMqConsumerEp *)pIter;
    tlen += tEncodeSMqConsumerEp(buf, pConsumerEp);
559 560
    cnt++;
  }
561
  if (cnt != sz) return -1;
562
  tlen += taosEncodeArray(buf, pSub->unassignedVgs, (FEncode)tEncodeSMqVgEp);
L
Liu Jicong 已提交
563
  tlen += taosEncodeString(buf, pSub->dbName);
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580

  int32_t szVgs = taosArrayGetSize(pSub->offsetRows);
  tlen += taosEncodeFixedI32(buf, szVgs);
  for (int32_t j= 0; j < szVgs; ++j) {
    OffsetRows *offRows = taosArrayGet(pSub->offsetRows, j);
    tlen += taosEncodeFixedI32(buf, offRows->vgId);
    tlen += taosEncodeFixedI64(buf, offRows->rows);
    tlen += taosEncodeFixedI8(buf, offRows->offset.type);
    if (offRows->offset.type == TMQ_OFFSET__SNAPSHOT_DATA || offRows->offset.type == TMQ_OFFSET__SNAPSHOT_META) {
      tlen += taosEncodeFixedI64(buf, offRows->offset.uid);
      tlen += taosEncodeFixedI64(buf, offRows->offset.ts);
    } else if (offRows->offset.type == TMQ_OFFSET__LOG) {
      tlen += taosEncodeFixedI64(buf, offRows->offset.version);
    } else {
      // do nothing
    }
  }
581 582 583
  return tlen;
}

584
void *tDecodeSubscribeObj(const void *buf, SMqSubscribeObj *pSub, int8_t sver) {
585 586
  //
  buf = taosDecodeStringTo(buf, pSub->key);
L
Liu Jicong 已提交
587
  buf = taosDecodeFixedI64(buf, &pSub->dbUid);
588
  buf = taosDecodeFixedI32(buf, &pSub->vgNum);
L
Liu Jicong 已提交
589
  buf = taosDecodeFixedI8(buf, &pSub->subType);
L
Liu Jicong 已提交
590
  buf = taosDecodeFixedI8(buf, &pSub->withMeta);
L
Liu Jicong 已提交
591
  buf = taosDecodeFixedI64(buf, &pSub->stbUid);
592 593 594 595 596 597

  int32_t sz;
  buf = taosDecodeFixedI32(buf, &sz);

  pSub->consumerHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
  for (int32_t i = 0; i < sz; i++) {
598
    SMqConsumerEp consumerEp = {0};
599
    buf = tDecodeSMqConsumerEp(buf, &consumerEp, sver);
600
    taosHashPut(pSub->consumerHash, &consumerEp.consumerId, sizeof(int64_t), &consumerEp, sizeof(SMqConsumerEp));
601 602
  }

603
  buf = taosDecodeArray(buf, &pSub->unassignedVgs, (FDecode)tDecodeSMqVgEp, sizeof(SMqVgEp));
L
Liu Jicong 已提交
604
  buf = taosDecodeStringTo(buf, pSub->dbName);
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627

  if (sver > 1){
    int32_t szVgs = 0;
    buf = taosDecodeFixedI32(buf, &szVgs);
    if(szVgs > 0){
      pSub->offsetRows = taosArrayInit(szVgs, sizeof(OffsetRows));
      if (NULL == pSub->offsetRows) return NULL;
      for (int32_t j= 0; j < szVgs; ++j) {
        OffsetRows* offRows = taosArrayReserve(pSub->offsetRows, 1);
        buf = taosDecodeFixedI32(buf, &offRows->vgId);
        buf = taosDecodeFixedI64(buf, &offRows->rows);
        buf = taosDecodeFixedI8(buf, &offRows->offset.type);
        if (offRows->offset.type == TMQ_OFFSET__SNAPSHOT_DATA || offRows->offset.type == TMQ_OFFSET__SNAPSHOT_META) {
          buf = taosDecodeFixedI64(buf, &offRows->offset.uid);
          buf = taosDecodeFixedI64(buf, &offRows->offset.ts);
        } else if (offRows->offset.type == TMQ_OFFSET__LOG) {
          buf = taosDecodeFixedI64(buf, &offRows->offset.version);
        } else {
          // do nothing
        }
      }
    }
  }
628 629 630
  return (void *)buf;
}

631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
//SMqSubActionLogEntry *tCloneSMqSubActionLogEntry(SMqSubActionLogEntry *pEntry) {
//  SMqSubActionLogEntry *pEntryNew = taosMemoryMalloc(sizeof(SMqSubActionLogEntry));
//  if (pEntryNew == NULL) return NULL;
//  pEntryNew->epoch = pEntry->epoch;
//  pEntryNew->consumers = taosArrayDup(pEntry->consumers, (__array_item_dup_fn_t)tCloneSMqConsumerEp);
//  return pEntryNew;
//}
//
//void tDeleteSMqSubActionLogEntry(SMqSubActionLogEntry *pEntry) {
//  taosArrayDestroyEx(pEntry->consumers, (FDelete)tDeleteSMqConsumerEp);
//}

//int32_t tEncodeSMqSubActionLogEntry(void **buf, const SMqSubActionLogEntry *pEntry) {
//  int32_t tlen = 0;
//  tlen += taosEncodeFixedI32(buf, pEntry->epoch);
//  tlen += taosEncodeArray(buf, pEntry->consumers, (FEncode)tEncodeSMqSubActionLogEntry);
//  return tlen;
//}
//
//void *tDecodeSMqSubActionLogEntry(const void *buf, SMqSubActionLogEntry *pEntry) {
//  buf = taosDecodeFixedI32(buf, &pEntry->epoch);
//  buf = taosDecodeArray(buf, &pEntry->consumers, (FDecode)tDecodeSMqSubActionLogEntry, sizeof(SMqSubActionLogEntry));
//  return (void *)buf;
//}

//SMqSubActionLogObj *tCloneSMqSubActionLogObj(SMqSubActionLogObj *pLog) {
//  SMqSubActionLogObj *pLogNew = taosMemoryMalloc(sizeof(SMqSubActionLogObj));
//  if (pLogNew == NULL) return pLogNew;
//  memcpy(pLogNew->key, pLog->key, TSDB_SUBSCRIBE_KEY_LEN);
//  pLogNew->logs = taosArrayDup(pLog->logs, (__array_item_dup_fn_t)tCloneSMqConsumerEp);
//  return pLogNew;
//}
//
//void tDeleteSMqSubActionLogObj(SMqSubActionLogObj *pLog) {
//  taosArrayDestroyEx(pLog->logs, (FDelete)tDeleteSMqConsumerEp);
//}

//int32_t tEncodeSMqSubActionLogObj(void **buf, const SMqSubActionLogObj *pLog) {
//  int32_t tlen = 0;
//  tlen += taosEncodeString(buf, pLog->key);
//  tlen += taosEncodeArray(buf, pLog->logs, (FEncode)tEncodeSMqSubActionLogEntry);
//  return tlen;
//}
//
//void *tDecodeSMqSubActionLogObj(const void *buf, SMqSubActionLogObj *pLog) {
//  buf = taosDecodeStringTo(buf, pLog->key);
//  buf = taosDecodeArray(buf, &pLog->logs, (FDecode)tDecodeSMqSubActionLogEntry, sizeof(SMqSubActionLogEntry));
//  return (void *)buf;
//}
//
//int32_t tEncodeSMqOffsetObj(void **buf, const SMqOffsetObj *pOffset) {
//  int32_t tlen = 0;
//  tlen += taosEncodeString(buf, pOffset->key);
//  tlen += taosEncodeFixedI64(buf, pOffset->offset);
//  return tlen;
//}
//
//void *tDecodeSMqOffsetObj(void *buf, SMqOffsetObj *pOffset) {
//  buf = taosDecodeStringTo(buf, pOffset->key);
//  buf = taosDecodeFixedI64(buf, &pOffset->offset);
//  return buf;
//}