mndDef.h 15.5 KB
Newer Older
H
refact  
Hongze Cheng 已提交
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 17
#ifndef _TD_MND_DEF_H_
#define _TD_MND_DEF_H_
S
Shengliang Guan 已提交
18

19
#include "os.h"
S
Shengliang Guan 已提交
20 21

#include "cJSON.h"
L
Liu Jicong 已提交
22
#include "scheduler.h"
S
Shengliang Guan 已提交
23 24
#include "sync.h"
#include "thash.h"
L
Liu Jicong 已提交
25
#include "tlist.h"
S
Shengliang Guan 已提交
26
#include "tlog.h"
L
Liu Jicong 已提交
27
#include "tmsg.h"
S
Shengliang Guan 已提交
28 29
#include "trpc.h"
#include "ttimer.h"
S
Shengliang Guan 已提交
30

S
Shengliang Guan 已提交
31
#include "mnode.h"
S
Shengliang Guan 已提交
32

S
Shengliang Guan 已提交
33 34 35
#ifdef __cplusplus
extern "C" {
#endif
S
Shengliang Guan 已提交
36

S
Shengliang Guan 已提交
37 38 39
extern int32_t mDebugFlag;

// mnode log function
L
Liu Jicong 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#define mFatal(...)                                 \
  {                                                 \
    if (mDebugFlag & DEBUG_FATAL) {                 \
      taosPrintLog("MND FATAL ", 255, __VA_ARGS__); \
    }                                               \
  }
#define mError(...)                                 \
  {                                                 \
    if (mDebugFlag & DEBUG_ERROR) {                 \
      taosPrintLog("MND ERROR ", 255, __VA_ARGS__); \
    }                                               \
  }
#define mWarn(...)                                 \
  {                                                \
    if (mDebugFlag & DEBUG_WARN) {                 \
      taosPrintLog("MND WARN ", 255, __VA_ARGS__); \
    }                                              \
  }
#define mInfo(...)                            \
  {                                           \
    if (mDebugFlag & DEBUG_INFO) {            \
      taosPrintLog("MND ", 255, __VA_ARGS__); \
    }                                         \
  }
#define mDebug(...)                                  \
  {                                                  \
    if (mDebugFlag & DEBUG_DEBUG) {                  \
      taosPrintLog("MND ", mDebugFlag, __VA_ARGS__); \
    }                                                \
  }
#define mTrace(...)                                  \
  {                                                  \
    if (mDebugFlag & DEBUG_TRACE) {                  \
      taosPrintLog("MND ", mDebugFlag, __VA_ARGS__); \
    }                                                \
  }
S
Shengliang Guan 已提交
76 77

typedef enum {
78 79 80 81 82 83 84 85
  MND_AUTH_ACCT_START = 0,
  MND_AUTH_ACCT_USER,
  MND_AUTH_ACCT_DNODE,
  MND_AUTH_ACCT_MNODE,
  MND_AUTH_ACCT_DB,
  MND_AUTH_ACCT_TABLE,
  MND_AUTH_ACCT_MAX
} EAuthAcct;
S
Shengliang Guan 已提交
86 87

typedef enum {
88 89 90 91 92 93
  MND_AUTH_OP_START = 0,
  MND_AUTH_OP_CREATE_USER,
  MND_AUTH_OP_ALTER_USER,
  MND_AUTH_OP_DROP_USER,
  MND_AUTH_MAX
} EAuthOp;
S
Shengliang Guan 已提交
94

S
Shengliang Guan 已提交
95
typedef enum {
96
  TRN_STAGE_PREPARE = 0,
S
Shengliang Guan 已提交
97 98
  TRN_STAGE_REDO_LOG = 1,
  TRN_STAGE_REDO_ACTION = 2,
99 100
  TRN_STAGE_COMMIT = 3,
  TRN_STAGE_COMMIT_LOG = 4,
101 102
  TRN_STAGE_UNDO_ACTION = 5,
  TRN_STAGE_UNDO_LOG = 6,
S
Shengliang Guan 已提交
103 104
  TRN_STAGE_ROLLBACK = 7,
  TRN_STAGE_FINISHED = 8
S
Shengliang Guan 已提交
105 106
} ETrnStage;

107
typedef enum { TRN_POLICY_ROLLBACK = 0, TRN_POLICY_RETRY = 1 } ETrnPolicy;
S
Shengliang Guan 已提交
108

S
Shengliang Guan 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122
typedef enum {
  DND_REASON_ONLINE = 0,
  DND_REASON_STATUS_MSG_TIMEOUT,
  DND_REASON_STATUS_NOT_RECEIVED,
  DND_REASON_VERSION_NOT_MATCH,
  DND_REASON_DNODE_ID_NOT_MATCH,
  DND_REASON_CLUSTER_ID_NOT_MATCH,
  DND_REASON_STATUS_INTERVAL_NOT_MATCH,
  DND_REASON_TIME_ZONE_NOT_MATCH,
  DND_REASON_LOCALE_NOT_MATCH,
  DND_REASON_CHARSET_NOT_MATCH,
  DND_REASON_OTHERS
} EDndReason;

S
Shengliang Guan 已提交
123
typedef struct {
S
Shengliang Guan 已提交
124 125 126
  int32_t    id;
  ETrnStage  stage;
  ETrnPolicy policy;
S
Shengliang Guan 已提交
127 128
  int32_t    code;
  int32_t    failedTimes;
L
Liu Jicong 已提交
129 130
  void*      rpcHandle;
  void*      rpcAHandle;
S
Shengliang Guan 已提交
131 132
  void*      rpcRsp;
  int32_t    rpcRspLen;
L
Liu Jicong 已提交
133 134 135 136 137
  SArray*    redoLogs;
  SArray*    undoLogs;
  SArray*    commitLogs;
  SArray*    redoActions;
  SArray*    undoActions;
S
Shengliang Guan 已提交
138
} STrans;
S
Shengliang Guan 已提交
139

S
Shengliang Guan 已提交
140
typedef struct {
141
  int64_t id;
S
Shengliang Guan 已提交
142
  char    name[TSDB_CLUSTER_ID_LEN];
S
Shengliang Guan 已提交
143 144 145 146
  int64_t createdTime;
  int64_t updateTime;
} SClusterObj;

S
Shengliang Guan 已提交
147
typedef struct {
S
Shengliang Guan 已提交
148 149 150 151
  int32_t    id;
  int64_t    createdTime;
  int64_t    updateTime;
  int64_t    rebootTime;
152
  int64_t    lastAccessTime;
S
Shengliang Guan 已提交
153
  int32_t    accessTimes;
S
Shengliang Guan 已提交
154
  int32_t    numOfVnodes;
S
Shengliang Guan 已提交
155 156
  int32_t    numOfSupportVnodes;
  int32_t    numOfCores;
S
Shengliang Guan 已提交
157 158 159 160
  EDndReason offlineReason;
  uint16_t   port;
  char       fqdn[TSDB_FQDN_LEN];
  char       ep[TSDB_EP_LEN];
S
Shengliang Guan 已提交
161 162
} SDnodeObj;

S
Shengliang Guan 已提交
163
typedef struct {
S
Shengliang Guan 已提交
164
  int32_t    id;
S
Shengliang Guan 已提交
165 166
  int64_t    createdTime;
  int64_t    updateTime;
S
Shengliang Guan 已提交
167
  ESyncState role;
S
Shengliang Guan 已提交
168 169
  int32_t    roleTerm;
  int64_t    roleTime;
L
Liu Jicong 已提交
170
  SDnodeObj* pDnode;
S
Shengliang Guan 已提交
171 172
} SMnodeObj;

S
Shengliang Guan 已提交
173 174 175 176
typedef struct {
  int32_t    id;
  int64_t    createdTime;
  int64_t    updateTime;
L
Liu Jicong 已提交
177
  SDnodeObj* pDnode;
S
Shengliang Guan 已提交
178 179 180 181 182 183
} SQnodeObj;

typedef struct {
  int32_t    id;
  int64_t    createdTime;
  int64_t    updateTime;
L
Liu Jicong 已提交
184
  SDnodeObj* pDnode;
S
Shengliang Guan 已提交
185 186 187 188 189 190
} SSnodeObj;

typedef struct {
  int32_t    id;
  int64_t    createdTime;
  int64_t    updateTime;
L
Liu Jicong 已提交
191
  SDnodeObj* pDnode;
S
Shengliang Guan 已提交
192 193
} SBnodeObj;

S
Shengliang Guan 已提交
194 195 196
typedef struct {
  int32_t maxUsers;
  int32_t maxDbs;
S
Shengliang Guan 已提交
197 198
  int32_t maxStbs;
  int32_t maxTbs;
S
Shengliang Guan 已提交
199 200
  int32_t maxTimeSeries;
  int32_t maxStreams;
S
Shengliang Guan 已提交
201 202 203 204
  int32_t maxFuncs;
  int32_t maxConsumers;
  int32_t maxConns;
  int32_t maxTopics;
S
Shengliang Guan 已提交
205 206
  int64_t maxStorage;   // In unit of GB
  int32_t accessState;  // Configured only by command
S
Shengliang Guan 已提交
207 208 209 210 211 212 213
} SAcctCfg;

typedef struct {
  int32_t numOfUsers;
  int32_t numOfDbs;
  int32_t numOfTimeSeries;
  int32_t numOfStreams;
S
Shengliang Guan 已提交
214 215
  int64_t totalStorage;  // Total storage wrtten from this account
  int64_t compStorage;   // Compressed storage on disk
S
Shengliang Guan 已提交
216 217
} SAcctInfo;

S
Shengliang Guan 已提交
218
typedef struct {
S
Shengliang Guan 已提交
219 220 221 222
  char      acct[TSDB_USER_LEN];
  int64_t   createdTime;
  int64_t   updateTime;
  int32_t   acctId;
S
Shengliang Guan 已提交
223
  int32_t   status;
S
Shengliang Guan 已提交
224 225 226 227
  SAcctCfg  cfg;
  SAcctInfo info;
} SAcctObj;

S
Shengliang Guan 已提交
228
typedef struct {
S
Shengliang Guan 已提交
229
  char      user[TSDB_USER_LEN];
230
  char      pass[TSDB_PASSWORD_LEN];
S
Shengliang Guan 已提交
231 232 233
  char      acct[TSDB_USER_LEN];
  int64_t   createdTime;
  int64_t   updateTime;
234
  int8_t    superUser;
S
Shengliang Guan 已提交
235
  int32_t   acctId;
L
Liu Jicong 已提交
236
  SHashObj* prohibitDbHash;
S
Shengliang Guan 已提交
237 238 239
} SUserObj;

typedef struct {
240
  int32_t numOfVgroups;
S
Shengliang Guan 已提交
241 242 243 244 245 246
  int32_t cacheBlockSize;
  int32_t totalBlocks;
  int32_t daysPerFile;
  int32_t daysToKeep0;
  int32_t daysToKeep1;
  int32_t daysToKeep2;
S
Shengliang Guan 已提交
247 248
  int32_t minRows;
  int32_t maxRows;
S
Shengliang Guan 已提交
249 250
  int32_t commitTime;
  int32_t fsyncPeriod;
S
Shengliang Guan 已提交
251
  int8_t  walLevel;
S
Shengliang Guan 已提交
252 253 254 255 256 257 258 259
  int8_t  precision;
  int8_t  compression;
  int8_t  replications;
  int8_t  quorum;
  int8_t  update;
  int8_t  cacheLastRow;
} SDbCfg;

S
Shengliang Guan 已提交
260
typedef struct {
L
Liu Jicong 已提交
261 262 263 264
  char     name[TSDB_DB_FNAME_LEN];
  char     acct[TSDB_USER_LEN];
  int64_t  createdTime;
  int64_t  updateTime;
H
Haojun Liao 已提交
265
  uint64_t uid;
L
Liu Jicong 已提交
266 267 268 269
  int32_t  cfgVersion;
  int32_t  vgVersion;
  int8_t   hashMethod;  // default is 1
  SDbCfg   cfg;
S
Shengliang Guan 已提交
270 271 272 273
} SDbObj;

typedef struct {
  int32_t    dnodeId;
S
Shengliang Guan 已提交
274
  ESyncState role;
S
Shengliang Guan 已提交
275 276
} SVnodeGid;

S
Shengliang Guan 已提交
277
typedef struct {
S
Shengliang Guan 已提交
278
  int32_t   vgId;
S
Shengliang Guan 已提交
279 280
  int64_t   createdTime;
  int64_t   updateTime;
S
Shengliang Guan 已提交
281
  int32_t   version;
S
Shengliang Guan 已提交
282 283
  uint32_t  hashBegin;
  uint32_t  hashEnd;
284
  char      dbName[TSDB_DB_FNAME_LEN];
S
Shengliang Guan 已提交
285
  int64_t   dbUid;
S
Shengliang Guan 已提交
286
  int32_t   numOfTables;
S
Shengliang Guan 已提交
287
  int32_t   numOfTimeSeries;
S
Shengliang Guan 已提交
288 289 290
  int64_t   totalStorage;
  int64_t   compStorage;
  int64_t   pointsWritten;
S
Shengliang Guan 已提交
291 292 293
  int8_t    compact;
  int8_t    replica;
  SVnodeGid vnodeGid[TSDB_MAX_REPLICA];
S
Shengliang Guan 已提交
294 295
} SVgObj;

S
Shengliang Guan 已提交
296
typedef struct {
S
Shengliang Guan 已提交
297
  char     name[TSDB_TABLE_FNAME_LEN];
298
  char     db[TSDB_DB_FNAME_LEN];
299 300
  int64_t  createdTime;
  int64_t  updateTime;
S
Shengliang Guan 已提交
301
  uint64_t uid;
S
Shengliang Guan 已提交
302
  uint64_t dbUid;
S
Shengliang Guan 已提交
303
  int32_t  version;
S
Shengliang Guan 已提交
304
  int32_t  nextColId;
S
Shengliang Guan 已提交
305 306
  int32_t  numOfColumns;
  int32_t  numOfTags;
S
Shengliang Guan 已提交
307
  SSchema* pColumns;
S
Shengliang Guan 已提交
308
  SSchema* pTags;
S
Shengliang Guan 已提交
309
  SRWLatch lock;
S
Shengliang Guan 已提交
310
} SStbObj;
S
Shengliang Guan 已提交
311

S
Shengliang Guan 已提交
312
typedef struct {
S
Shengliang Guan 已提交
313 314
  char    name[TSDB_FUNC_NAME_LEN];
  int64_t createdTime;
S
Shengliang Guan 已提交
315 316 317 318 319 320
  int8_t  funcType;
  int8_t  scriptType;
  int8_t  align;
  int8_t  outputType;
  int32_t outputLen;
  int32_t bufSize;
S
Shengliang 已提交
321
  int64_t signature;
S
Shengliang Guan 已提交
322 323
  int32_t commentSize;
  int32_t codeSize;
L
Liu Jicong 已提交
324 325
  char*   pComment;
  char*   pCode;
S
Shengliang Guan 已提交
326
  char    pData[];
S
Shengliang Guan 已提交
327 328
} SFuncObj;

S
Shengliang Guan 已提交
329
typedef struct {
330
  int64_t id;
S
Shengliang Guan 已提交
331 332 333 334 335 336 337
  int8_t  type;
  int8_t  replica;
  int16_t numOfColumns;
  int32_t rowSize;
  int32_t numOfRows;
  int32_t numOfReads;
  int32_t payloadLen;
L
Liu Jicong 已提交
338 339
  void*   pIter;
  SMnode* pMnode;
340
  char    db[TSDB_DB_FNAME_LEN];
S
Shengliang Guan 已提交
341 342 343
  int16_t offset[TSDB_MAX_COLUMNS];
  int32_t bytes[TSDB_MAX_COLUMNS];
  char    payload[];
S
Shengliang Guan 已提交
344 345
} SShowObj;

L
Liu Jicong 已提交
346 347 348 349 350 351 352
typedef struct {
  int32_t vgId;  // -1 for unassigned
  int32_t status;
  SEpSet  epSet;
  int64_t oldConsumerId;
  int64_t consumerId;  // -1 for unassigned
  char*   qmsg;
L
Liu Jicong 已提交
353 354
} SMqConsumerEp;

L
Liu Jicong 已提交
355
static FORCE_INLINE int32_t tEncodeSMqConsumerEp(void** buf, const SMqConsumerEp* pConsumerEp) {
L
Liu Jicong 已提交
356 357
  int32_t tlen = 0;
  tlen += taosEncodeFixedI32(buf, pConsumerEp->vgId);
L
Liu Jicong 已提交
358
  tlen += taosEncodeFixedI32(buf, pConsumerEp->status);
359
  tlen += taosEncodeSEpSet(buf, &pConsumerEp->epSet);
L
Liu Jicong 已提交
360
  tlen += taosEncodeFixedI64(buf, pConsumerEp->oldConsumerId);
L
Liu Jicong 已提交
361
  tlen += taosEncodeFixedI64(buf, pConsumerEp->consumerId);
L
Liu Jicong 已提交
362
  tlen += taosEncodeString(buf, pConsumerEp->qmsg);
L
Liu Jicong 已提交
363 364 365 366 367
  return tlen;
}

static FORCE_INLINE void* tDecodeSMqConsumerEp(void** buf, SMqConsumerEp* pConsumerEp) {
  buf = taosDecodeFixedI32(buf, &pConsumerEp->vgId);
L
Liu Jicong 已提交
368
  buf = taosDecodeFixedI32(buf, &pConsumerEp->status);
369
  buf = taosDecodeSEpSet(buf, &pConsumerEp->epSet);
L
Liu Jicong 已提交
370
  buf = taosDecodeFixedI64(buf, &pConsumerEp->oldConsumerId);
L
Liu Jicong 已提交
371
  buf = taosDecodeFixedI64(buf, &pConsumerEp->consumerId);
L
Liu Jicong 已提交
372
  buf = taosDecodeString(buf, &pConsumerEp->qmsg);
L
Liu Jicong 已提交
373 374 375
  return buf;
}

L
Liu Jicong 已提交
376 377 378 379 380 381
static FORCE_INLINE void tDeleteSMqConsumerEp(SMqConsumerEp* pConsumerEp) {
  if (pConsumerEp) {
    tfree(pConsumerEp->qmsg);
  }
}

L
Liu Jicong 已提交
382 383 384 385
typedef struct {
  int64_t consumerId;
  SArray* vgInfo;  // SArray<SMqConsumerEp>
} SMqSubConsumer;
L
Liu Jicong 已提交
386

L
Liu Jicong 已提交
387 388 389 390 391 392 393
static FORCE_INLINE int32_t tEncodeSMqSubConsumer(void** buf, const SMqSubConsumer* pConsumer) {
  int32_t tlen = 0;
  tlen += taosEncodeFixedI64(buf, pConsumer->consumerId);
  int32_t sz = taosArrayGetSize(pConsumer->vgInfo);
  for (int32_t i = 0; i < sz; i++) {
    SMqConsumerEp* pCEp = taosArrayGet(pConsumer->vgInfo, i);
    tlen += tEncodeSMqConsumerEp(buf, pCEp);
L
Liu Jicong 已提交
394
  }
L
Liu Jicong 已提交
395 396
  return tlen;
}
L
Liu Jicong 已提交
397

L
Liu Jicong 已提交
398 399 400 401 402 403 404 405 406
static FORCE_INLINE void* tDecodeSMqSubConsumer(void** buf, SMqSubConsumer* pConsumer) {
  int32_t sz;
  buf = taosDecodeFixedI64(buf, &pConsumer->consumerId);
  buf = taosDecodeFixedI32(buf, &sz);
  pConsumer->vgInfo = taosArrayInit(sz, sizeof(SMqConsumerEp));
  for (int32_t i = 0; i < sz; i++) {
    SMqConsumerEp consumerEp;
    buf = tDecodeSMqConsumerEp(buf, &consumerEp);
    taosArrayPush(pConsumer->vgInfo, &consumerEp);
L
Liu Jicong 已提交
407
  }
L
Liu Jicong 已提交
408 409 410 411 412 413 414
  return buf;
}

static FORCE_INLINE void tDeleteSMqSubConsumer(SMqSubConsumer* pSubConsumer) {
  if (pSubConsumer->vgInfo) {
    taosArrayDestroyEx(pSubConsumer->vgInfo, (void (*)(void*))tDeleteSMqConsumerEp);
    pSubConsumer->vgInfo = NULL;
L
Liu Jicong 已提交
415
  }
L
Liu Jicong 已提交
416 417 418 419 420 421 422 423 424 425 426 427 428
}

typedef struct {
  char    key[TSDB_SUBSCRIBE_KEY_LEN];
  int32_t status;
  int32_t vgNum;
  SArray* consumers;     // SArray<SMqSubConsumer>
  SArray* unassignedVg;  // SArray<SMqConsumerEp>
} SMqSubscribeObj;

static FORCE_INLINE SMqSubscribeObj* tNewSubscribeObj() {
  SMqSubscribeObj* pSub = calloc(1, sizeof(SMqSubscribeObj));
  if (pSub == NULL) {
L
Liu Jicong 已提交
429 430
    return NULL;
  }
L
Liu Jicong 已提交
431 432 433
  pSub->consumers = taosArrayInit(0, sizeof(SMqSubConsumer));
  if (pSub->consumers == NULL) {
    goto _err;
L
Liu Jicong 已提交
434 435
  }
  pSub->unassignedVg = taosArrayInit(0, sizeof(SMqConsumerEp));
L
Liu Jicong 已提交
436
  if (pSub->unassignedVg == NULL) {
L
Liu Jicong 已提交
437
    goto _err;
L
Liu Jicong 已提交
438
  }
L
Liu Jicong 已提交
439 440 441 442 443

  pSub->key[0] = 0;
  pSub->vgNum = 0;
  pSub->status = 0;

L
Liu Jicong 已提交
444
  return pSub;
L
Liu Jicong 已提交
445 446 447 448 449 450

_err:
  tfree(pSub->unassignedVg);
  tfree(pSub->consumers);
  tfree(pSub);
  return NULL;
L
Liu Jicong 已提交
451 452 453 454 455
}

static FORCE_INLINE int32_t tEncodeSubscribeObj(void** buf, const SMqSubscribeObj* pSub) {
  int32_t tlen = 0;
  tlen += taosEncodeString(buf, pSub->key);
L
Liu Jicong 已提交
456 457
  tlen += taosEncodeFixedI32(buf, pSub->vgNum);
  tlen += taosEncodeFixedI32(buf, pSub->status);
L
Liu Jicong 已提交
458 459
  int32_t sz;

L
Liu Jicong 已提交
460
  sz = taosArrayGetSize(pSub->consumers);
L
Liu Jicong 已提交
461 462
  tlen += taosEncodeFixedI32(buf, sz);
  for (int32_t i = 0; i < sz; i++) {
L
Liu Jicong 已提交
463 464
    SMqSubConsumer* pSubConsumer = taosArrayGet(pSub->consumers, i);
    tlen += tEncodeSMqSubConsumer(buf, pSubConsumer);
L
Liu Jicong 已提交
465 466 467 468 469 470 471 472 473 474 475 476 477 478
  }

  sz = taosArrayGetSize(pSub->unassignedVg);
  tlen += taosEncodeFixedI32(buf, sz);
  for (int32_t i = 0; i < sz; i++) {
    SMqConsumerEp* pCEp = taosArrayGet(pSub->unassignedVg, i);
    tlen += tEncodeSMqConsumerEp(buf, pCEp);
  }

  return tlen;
}

static FORCE_INLINE void* tDecodeSubscribeObj(void* buf, SMqSubscribeObj* pSub) {
  buf = taosDecodeStringTo(buf, pSub->key);
L
Liu Jicong 已提交
479 480
  buf = taosDecodeFixedI32(buf, &pSub->vgNum);
  buf = taosDecodeFixedI32(buf, &pSub->status);
L
Liu Jicong 已提交
481 482 483 484

  int32_t sz;

  buf = taosDecodeFixedI32(buf, &sz);
L
Liu Jicong 已提交
485 486
  pSub->consumers = taosArrayInit(sz, sizeof(SMqSubConsumer));
  if (pSub->consumers == NULL) {
L
Liu Jicong 已提交
487 488 489
    return NULL;
  }
  for (int32_t i = 0; i < sz; i++) {
L
Liu Jicong 已提交
490 491 492
    SMqSubConsumer subConsumer = {0};
    buf = tDecodeSMqSubConsumer(buf, &subConsumer);
    taosArrayPush(pSub->consumers, &subConsumer);
L
Liu Jicong 已提交
493 494
  }

L
Liu Jicong 已提交
495 496 497 498 499 500
  buf = taosDecodeFixedI32(buf, &sz);
  pSub->unassignedVg = taosArrayInit(sz, sizeof(SMqConsumerEp));
  if (pSub->unassignedVg == NULL) {
    return NULL;
  }
  for (int32_t i = 0; i < sz; i++) {
L
Liu Jicong 已提交
501
    SMqConsumerEp cEp = {0};
L
Liu Jicong 已提交
502 503 504 505 506
    buf = tDecodeSMqConsumerEp(buf, &cEp);
    taosArrayPush(pSub->unassignedVg, &cEp);
  }
  return buf;
}
L
Liu Jicong 已提交
507

L
Liu Jicong 已提交
508
static FORCE_INLINE void tDeleteSMqSubscribeObj(SMqSubscribeObj* pSub) {
L
Liu Jicong 已提交
509 510 511 512
  if (pSub->consumers) {
    taosArrayDestroyEx(pSub->consumers, (void (*)(void*))tDeleteSMqSubConsumer);
    //taosArrayDestroy(pSub->consumers);
    pSub->consumers = NULL;
L
Liu Jicong 已提交
513
  }
L
Liu Jicong 已提交
514

L
Liu Jicong 已提交
515
  if (pSub->unassignedVg) {
L
Liu Jicong 已提交
516 517
    taosArrayDestroyEx(pSub->unassignedVg, (void (*)(void*))tDeleteSMqConsumerEp);
    //taosArrayDestroy(pSub->unassignedVg);
L
Liu Jicong 已提交
518 519
    pSub->unassignedVg = NULL;
  }
L
Liu Jicong 已提交
520 521
}

L
Liu Jicong 已提交
522
typedef struct {
L
Liu Jicong 已提交
523 524 525 526
  char     name[TSDB_TOPIC_FNAME_LEN];
  char     db[TSDB_DB_FNAME_LEN];
  int64_t  createTime;
  int64_t  updateTime;
L
Liu Jicong 已提交
527
  int64_t  uid;
L
Liu Jicong 已提交
528
  int64_t  dbUid;
L
Liu Jicong 已提交
529 530 531 532 533 534
  int32_t  version;
  SRWLatch lock;
  int32_t  sqlLen;
  char*    sql;
  char*    logicalPlan;
  char*    physicalPlan;
L
Liu Jicong 已提交
535 536
} SMqTopicObj;

L
Liu Jicong 已提交
537
typedef struct {
L
Liu Jicong 已提交
538
  int64_t  consumerId;
L
Liu Jicong 已提交
539
  int64_t  connId;
L
Liu Jicong 已提交
540
  SRWLatch lock;
L
Liu Jicong 已提交
541
  char     cgroup[TSDB_CONSUMER_GROUP_LEN];
L
Liu Jicong 已提交
542
  SArray*  topics;  // SArray<char*>
L
Liu Jicong 已提交
543 544
  int64_t  epoch;
  // stat
L
Liu Jicong 已提交
545 546 547 548 549 550 551
  int64_t pollCnt;
  // status
  int32_t status;
  // heartbeat from the consumer reset hbStatus to 0
  // each checkConsumerAlive msg add hbStatus by 1
  // if checkConsumerAlive > CONSUMER_REBALANCE_CNT, mask to lost
  int32_t hbStatus;
L
Liu Jicong 已提交
552 553
} SMqConsumerObj;

L
Liu Jicong 已提交
554 555 556
static FORCE_INLINE int32_t tEncodeSMqConsumerObj(void** buf, const SMqConsumerObj* pConsumer) {
  int32_t tlen = 0;
  tlen += taosEncodeFixedI64(buf, pConsumer->consumerId);
L
Liu Jicong 已提交
557 558 559
  tlen += taosEncodeFixedI64(buf, pConsumer->connId);
  tlen += taosEncodeFixedI64(buf, pConsumer->epoch);
  tlen += taosEncodeFixedI64(buf, pConsumer->pollCnt);
L
Liu Jicong 已提交
560 561 562 563
  tlen += taosEncodeString(buf, pConsumer->cgroup);
  int32_t sz = taosArrayGetSize(pConsumer->topics);
  tlen += taosEncodeFixedI32(buf, sz);
  for (int32_t i = 0; i < sz; i++) {
L
Liu Jicong 已提交
564 565
    char* topic = taosArrayGetP(pConsumer->topics, i);
    tlen += taosEncodeString(buf, topic);
L
Liu Jicong 已提交
566 567 568 569 570
  }
  return tlen;
}

static FORCE_INLINE void* tDecodeSMqConsumerObj(void* buf, SMqConsumerObj* pConsumer) {
L
Liu Jicong 已提交
571
  int32_t sz;
L
Liu Jicong 已提交
572
  buf = taosDecodeFixedI64(buf, &pConsumer->consumerId);
L
Liu Jicong 已提交
573 574 575
  buf = taosDecodeFixedI64(buf, &pConsumer->connId);
  buf = taosDecodeFixedI64(buf, &pConsumer->epoch);
  buf = taosDecodeFixedI64(buf, &pConsumer->pollCnt);
L
Liu Jicong 已提交
576 577 578 579
  buf = taosDecodeStringTo(buf, pConsumer->cgroup);
  buf = taosDecodeFixedI32(buf, &sz);
  pConsumer->topics = taosArrayInit(sz, sizeof(SMqConsumerObj));
  for (int32_t i = 0; i < sz; i++) {
L
Liu Jicong 已提交
580 581 582
    char* topic;
    buf = taosDecodeString(buf, &topic);
    taosArrayPush(pConsumer->topics, &topic);
L
Liu Jicong 已提交
583 584 585 586
  }
  return buf;
}

S
Shengliang Guan 已提交
587
typedef struct SMnodeMsg {
S
Shengliang Guan 已提交
588
  char    user[TSDB_USER_LEN];
589
  char    db[TSDB_DB_FNAME_LEN];
S
Shengliang Guan 已提交
590
  int32_t acctId;
L
Liu Jicong 已提交
591
  SMnode* pMnode;
S
Shengliang Guan 已提交
592 593 594
  int64_t createdTime;
  SRpcMsg rpcMsg;
  int32_t contLen;
L
Liu Jicong 已提交
595
  void*   pCont;
S
Shengliang Guan 已提交
596
} SMnodeMsg;
S
Shengliang Guan 已提交
597

S
Shengliang Guan 已提交
598 599 600
#ifdef __cplusplus
}
#endif
S
Shengliang Guan 已提交
601

S
Shengliang Guan 已提交
602
#endif /*_TD_MND_DEF_H_*/