mndDef.h 20.4 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 131 132 133 134 135
  void*      rpcHandle;
  void*      rpcAHandle;
  SArray*    redoLogs;
  SArray*    undoLogs;
  SArray*    commitLogs;
  SArray*    redoActions;
  SArray*    undoActions;
S
Shengliang Guan 已提交
136
} STrans;
S
Shengliang Guan 已提交
137

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

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

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

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

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

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

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

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
294
typedef struct {
S
Shengliang Guan 已提交
295
  char     name[TSDB_TABLE_FNAME_LEN];
296
  char     db[TSDB_DB_FNAME_LEN];
297 298
  int64_t  createdTime;
  int64_t  updateTime;
S
Shengliang Guan 已提交
299
  uint64_t uid;
S
Shengliang Guan 已提交
300
  uint64_t dbUid;
S
Shengliang Guan 已提交
301
  int32_t  version;
S
Shengliang Guan 已提交
302 303
  int32_t  numOfColumns;
  int32_t  numOfTags;
S
Shengliang Guan 已提交
304
  SRWLatch lock;
L
Liu Jicong 已提交
305
  SSchema* pSchema;
S
Shengliang Guan 已提交
306
} SStbObj;
S
Shengliang Guan 已提交
307

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

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

L
Liu Jicong 已提交
342
#if 0
L
Liu Jicong 已提交
343
typedef struct SConsumerObj {
L
Liu Jicong 已提交
344
  uint64_t uid;
L
Liu Jicong 已提交
345 346
  int64_t  createTime;
  int64_t  updateTime;
L
Liu Jicong 已提交
347
  //uint64_t dbUid;
L
Liu Jicong 已提交
348
  int32_t  version;
L
Liu Jicong 已提交
349
  SRWLatch lock;
L
Liu Jicong 已提交
350
  SArray*   topics;
L
Liu Jicong 已提交
351 352
} SConsumerObj;

L
Liu Jicong 已提交
353 354 355 356 357 358
typedef struct SMqTopicConsumer {
  int64_t consumerId;
  SList*  topicList;
} SMqTopicConsumer;
#endif

L
Liu Jicong 已提交
359
typedef struct SMqConsumerEp {
L
Liu Jicong 已提交
360 361
  int32_t      vgId;  // -1 for unassigned
  int32_t      status;
362
  SEpSet       epSet;
L
Liu Jicong 已提交
363
  int64_t      consumerId;  // -1 for unassigned
L
Liu Jicong 已提交
364 365
  int64_t      lastConsumerHbTs;
  int64_t      lastVgHbTs;
L
Liu Jicong 已提交
366
  char*        qmsg;
L
Liu Jicong 已提交
367 368
} SMqConsumerEp;

L
Liu Jicong 已提交
369 370 371
static FORCE_INLINE int32_t tEncodeSMqConsumerEp(void** buf, SMqConsumerEp* pConsumerEp) {
  int32_t tlen = 0;
  tlen += taosEncodeFixedI32(buf, pConsumerEp->vgId);
L
Liu Jicong 已提交
372
  tlen += taosEncodeFixedI32(buf, pConsumerEp->status);
373
  tlen += taosEncodeSEpSet(buf, &pConsumerEp->epSet);
L
Liu Jicong 已提交
374
  tlen += taosEncodeFixedI64(buf, pConsumerEp->consumerId);
L
Liu Jicong 已提交
375 376
  tlen += taosEncodeFixedI64(buf, pConsumerEp->lastConsumerHbTs);
  tlen += taosEncodeFixedI64(buf, pConsumerEp->lastVgHbTs);
L
Liu Jicong 已提交
377
  //tlen += tEncodeSSubQueryMsg(buf, &pConsumerEp->qExec);
L
Liu Jicong 已提交
378
  tlen += taosEncodeString(buf, pConsumerEp->qmsg);
L
Liu Jicong 已提交
379 380 381 382 383
  return tlen;
}

static FORCE_INLINE void* tDecodeSMqConsumerEp(void** buf, SMqConsumerEp* pConsumerEp) {
  buf = taosDecodeFixedI32(buf, &pConsumerEp->vgId);
L
Liu Jicong 已提交
384
  buf = taosDecodeFixedI32(buf, &pConsumerEp->status);
385
  buf = taosDecodeSEpSet(buf, &pConsumerEp->epSet);
L
Liu Jicong 已提交
386
  buf = taosDecodeFixedI64(buf, &pConsumerEp->consumerId);
L
Liu Jicong 已提交
387 388
  buf = taosDecodeFixedI64(buf, &pConsumerEp->lastConsumerHbTs);
  buf = taosDecodeFixedI64(buf, &pConsumerEp->lastVgHbTs);
L
Liu Jicong 已提交
389
  //buf = tDecodeSSubQueryMsg(buf, &pConsumerEp->qExec);
L
Liu Jicong 已提交
390
  buf = taosDecodeString(buf, &pConsumerEp->qmsg);
L
Liu Jicong 已提交
391 392 393
  return buf;
}

L
Liu Jicong 已提交
394
// unit for rebalance
L
Liu Jicong 已提交
395 396 397
typedef struct SMqSubscribeObj {
  char    key[TSDB_SUBSCRIBE_KEY_LEN];
  int32_t epoch;
L
Liu Jicong 已提交
398
  // TODO: replace with priority queue
L
Liu Jicong 已提交
399
  int32_t nextConsumerIdx;
L
Liu Jicong 已提交
400 401 402 403 404
  SArray* availConsumer;  // SArray<int64_t> (consumerId)
  SArray* assigned;       // SArray<SMqConsumerEp>
  SArray* idleConsumer;   // SArray<SMqConsumerEp>
  SArray* lostConsumer;   // SArray<SMqConsumerEp>
  SArray* unassignedVg;   // SArray<SMqConsumerEp>
L
Liu Jicong 已提交
405 406 407 408 409 410 411
} SMqSubscribeObj;

static FORCE_INLINE SMqSubscribeObj* tNewSubscribeObj() {
  SMqSubscribeObj* pSub = malloc(sizeof(SMqSubscribeObj));
  if (pSub == NULL) {
    return NULL;
  }
L
Liu Jicong 已提交
412 413 414
  pSub->key[0] = 0;
  pSub->epoch = 0;

L
Liu Jicong 已提交
415 416 417 418 419 420 421 422 423 424 425
  pSub->availConsumer = taosArrayInit(0, sizeof(int64_t));
  if (pSub->availConsumer == NULL) {
    free(pSub);
    return NULL;
  }
  pSub->assigned = taosArrayInit(0, sizeof(SMqConsumerEp));
  if (pSub->assigned == NULL) {
    taosArrayDestroy(pSub->availConsumer);
    free(pSub);
    return NULL;
  }
L
Liu Jicong 已提交
426
  pSub->lostConsumer = taosArrayInit(0, sizeof(SMqConsumerEp));
L
Liu Jicong 已提交
427
  if (pSub->lostConsumer == NULL) {
L
Liu Jicong 已提交
428 429 430 431 432
    taosArrayDestroy(pSub->availConsumer);
    taosArrayDestroy(pSub->assigned);
    free(pSub);
    return NULL;
  }
L
Liu Jicong 已提交
433
  pSub->idleConsumer = taosArrayInit(0, sizeof(SMqConsumerEp));
L
Liu Jicong 已提交
434
  if (pSub->idleConsumer == NULL) {
L
Liu Jicong 已提交
435
    taosArrayDestroy(pSub->availConsumer);
L
Liu Jicong 已提交
436 437
    taosArrayDestroy(pSub->assigned);
    taosArrayDestroy(pSub->lostConsumer);
L
Liu Jicong 已提交
438 439 440 441
    free(pSub);
    return NULL;
  }
  pSub->unassignedVg = taosArrayInit(0, sizeof(SMqConsumerEp));
L
Liu Jicong 已提交
442
  if (pSub->unassignedVg == NULL) {
L
Liu Jicong 已提交
443
    taosArrayDestroy(pSub->availConsumer);
L
Liu Jicong 已提交
444 445
    taosArrayDestroy(pSub->assigned);
    taosArrayDestroy(pSub->lostConsumer);
L
Liu Jicong 已提交
446
    taosArrayDestroy(pSub->idleConsumer);
L
Liu Jicong 已提交
447 448 449
    free(pSub);
    return NULL;
  }
L
Liu Jicong 已提交
450
  return pSub;
L
Liu Jicong 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
}

static FORCE_INLINE int32_t tEncodeSubscribeObj(void** buf, const SMqSubscribeObj* pSub) {
  int32_t tlen = 0;
  tlen += taosEncodeString(buf, pSub->key);
  tlen += taosEncodeFixedI32(buf, pSub->epoch);
  int32_t sz;

  sz = taosArrayGetSize(pSub->availConsumer);
  tlen += taosEncodeFixedI32(buf, sz);
  for (int32_t i = 0; i < sz; i++) {
    int64_t* pConsumerId = taosArrayGet(pSub->availConsumer, i);
    tlen += taosEncodeFixedI64(buf, *pConsumerId);
  }

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

L
Liu Jicong 已提交
473 474 475 476 477 478 479
  sz = taosArrayGetSize(pSub->lostConsumer);
  tlen += taosEncodeFixedI32(buf, sz);
  for (int32_t i = 0; i < sz; i++) {
    SMqConsumerEp* pCEp = taosArrayGet(pSub->lostConsumer, i);
    tlen += tEncodeSMqConsumerEp(buf, pCEp);
  }

L
Liu Jicong 已提交
480
  sz = taosArrayGetSize(pSub->idleConsumer);
L
Liu Jicong 已提交
481 482
  tlen += taosEncodeFixedI32(buf, sz);
  for (int32_t i = 0; i < sz; i++) {
L
Liu Jicong 已提交
483
    SMqConsumerEp* pCEp = taosArrayGet(pSub->idleConsumer, i);
L
Liu Jicong 已提交
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
    tlen += tEncodeSMqConsumerEp(buf, pCEp);
  }

  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);
  buf = taosDecodeFixedI32(buf, &pSub->epoch);

  int32_t sz;

  buf = taosDecodeFixedI32(buf, &sz);
L
Liu Jicong 已提交
504 505
  pSub->availConsumer = taosArrayInit(sz, sizeof(int64_t));
  if (pSub->availConsumer == NULL) {
L
Liu Jicong 已提交
506 507 508 509 510
    return NULL;
  }
  for (int32_t i = 0; i < sz; i++) {
    int64_t consumerId;
    buf = taosDecodeFixedI64(buf, &consumerId);
L
Liu Jicong 已提交
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
    taosArrayPush(pSub->availConsumer, &consumerId);
  }

  buf = taosDecodeFixedI32(buf, &sz);
  pSub->assigned = taosArrayInit(sz, sizeof(SMqConsumerEp));
  if (pSub->assigned == NULL) {
    taosArrayDestroy(pSub->availConsumer);
    return NULL;
  }
  for (int32_t i = 0; i < sz; i++) {
    SMqConsumerEp cEp;
    buf = tDecodeSMqConsumerEp(buf, &cEp);
    taosArrayPush(pSub->assigned, &cEp);
  }

  buf = taosDecodeFixedI32(buf, &sz);
  pSub->lostConsumer = taosArrayInit(sz, sizeof(SMqConsumerEp));
  if (pSub->lostConsumer == NULL) {
    taosArrayDestroy(pSub->availConsumer);
    taosArrayDestroy(pSub->assigned);
    return NULL;
  }
  for (int32_t i = 0; i < sz; i++) {
    SMqConsumerEp cEp;
    buf = tDecodeSMqConsumerEp(buf, &cEp);
    taosArrayPush(pSub->lostConsumer, &cEp);
L
Liu Jicong 已提交
537 538 539
  }

  buf = taosDecodeFixedI32(buf, &sz);
L
Liu Jicong 已提交
540 541
  pSub->idleConsumer = taosArrayInit(sz, sizeof(SMqConsumerEp));
  if (pSub->idleConsumer == NULL) {
L
Liu Jicong 已提交
542
    taosArrayDestroy(pSub->availConsumer);
L
Liu Jicong 已提交
543
    taosArrayDestroy(pSub->assigned);
L
Liu Jicong 已提交
544
    taosArrayDestroy(pSub->lostConsumer);
L
Liu Jicong 已提交
545 546 547 548 549
    return NULL;
  }
  for (int32_t i = 0; i < sz; i++) {
    SMqConsumerEp cEp;
    buf = tDecodeSMqConsumerEp(buf, &cEp);
L
Liu Jicong 已提交
550
    taosArrayPush(pSub->idleConsumer, &cEp);
L
Liu Jicong 已提交
551 552
  }

L
Liu Jicong 已提交
553

L
Liu Jicong 已提交
554 555 556
  buf = taosDecodeFixedI32(buf, &sz);
  pSub->unassignedVg = taosArrayInit(sz, sizeof(SMqConsumerEp));
  if (pSub->unassignedVg == NULL) {
L
Liu Jicong 已提交
557
    taosArrayDestroy(pSub->availConsumer);
L
Liu Jicong 已提交
558
    taosArrayDestroy(pSub->assigned);
L
Liu Jicong 已提交
559
    taosArrayDestroy(pSub->lostConsumer);
L
Liu Jicong 已提交
560
    taosArrayDestroy(pSub->idleConsumer);
L
Liu Jicong 已提交
561 562 563 564 565 566 567 568 569 570
    return NULL;
  }
  for (int32_t i = 0; i < sz; i++) {
    SMqConsumerEp cEp;
    buf = tDecodeSMqConsumerEp(buf, &cEp);
    taosArrayPush(pSub->unassignedVg, &cEp);
  }

  return buf;
}
L
Liu Jicong 已提交
571

L
Liu Jicong 已提交
572 573 574
typedef struct SMqCGroup {
  char    name[TSDB_CONSUMER_GROUP_LEN];
  int32_t status;       // 0 - uninitialized, 1 - wait rebalance, 2- normal
L
Liu Jicong 已提交
575 576
  SList*  consumerIds;  // SList<int64_t>
  SList*  idleVGroups;  // SList<int32_t>
L
Liu Jicong 已提交
577 578 579
} SMqCGroup;

typedef struct SMqTopicObj {
L
Liu Jicong 已提交
580 581 582 583 584 585 586 587 588 589 590 591 592 593
  char     name[TSDB_TOPIC_FNAME_LEN];
  char     db[TSDB_DB_FNAME_LEN];
  int64_t  createTime;
  int64_t  updateTime;
  uint64_t uid;
  uint64_t dbUid;
  int32_t  version;
  SRWLatch lock;
  int32_t  sqlLen;
  char*    sql;
  char*    logicalPlan;
  char*    physicalPlan;
  // SHashObj *cgroups;  // SHashObj<SMqCGroup>
  // SHashObj *consumers;  // SHashObj<SMqConsumerObj>
L
Liu Jicong 已提交
594 595 596 597
} SMqTopicObj;

// TODO: add cache and change name to id
typedef struct SMqConsumerTopic {
L
Liu Jicong 已提交
598
  char    name[TSDB_TOPIC_FNAME_LEN];
L
Liu Jicong 已提交
599
  int32_t epoch;
L
Liu Jicong 已提交
600 601
  // vg assigned to the consumer on the topic
  SArray* pVgInfo;  // SArray<int32_t>
L
Liu Jicong 已提交
602 603
} SMqConsumerTopic;

L
Liu Jicong 已提交
604 605
static FORCE_INLINE SMqConsumerTopic* tNewConsumerTopic(int64_t consumerId, SMqTopicObj* pTopic,
                                                        SMqSubscribeObj* pSub) {
L
Liu Jicong 已提交
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
  SMqConsumerTopic* pCTopic = malloc(sizeof(SMqConsumerTopic));
  if (pCTopic == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
  strcpy(pCTopic->name, pTopic->name);
  pCTopic->epoch = 0;
  pCTopic->pVgInfo = taosArrayInit(0, sizeof(int32_t));

  int32_t unassignedVgSz = taosArrayGetSize(pSub->unassignedVg);
  if (unassignedVgSz > 0) {
    SMqConsumerEp* pCEp = taosArrayPop(pSub->unassignedVg);
    pCEp->consumerId = consumerId;
    taosArrayPush(pCTopic->pVgInfo, &pCEp->vgId);
    taosArrayPush(pSub->assigned, pCEp);
  }
  return pCTopic;
}

static FORCE_INLINE int32_t tEncodeSMqConsumerTopic(void** buf, SMqConsumerTopic* pConsumerTopic) {
  int32_t tlen = 0;
  tlen += taosEncodeString(buf, pConsumerTopic->name);
  tlen += taosEncodeFixedI32(buf, pConsumerTopic->epoch);
L
Liu Jicong 已提交
629 630 631 632
  int32_t sz = 0;
  if (pConsumerTopic->pVgInfo != NULL) {
    sz = taosArrayGetSize(pConsumerTopic->pVgInfo);
  }
L
Liu Jicong 已提交
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
  tlen += taosEncodeFixedI32(buf, sz);
  for (int32_t i = 0; i < sz; i++) {
    int32_t* pVgInfo = taosArrayGet(pConsumerTopic->pVgInfo, i);
    tlen += taosEncodeFixedI32(buf, *pVgInfo);
  }
  return tlen;
}

static FORCE_INLINE void* tDecodeSMqConsumerTopic(void* buf, SMqConsumerTopic* pConsumerTopic) {
  buf = taosDecodeStringTo(buf, pConsumerTopic->name);
  buf = taosDecodeFixedI32(buf, &pConsumerTopic->epoch);
  int32_t sz;
  buf = taosDecodeFixedI32(buf, &sz);
  pConsumerTopic->pVgInfo = taosArrayInit(sz, sizeof(SMqConsumerTopic));
  for (int32_t i = 0; i < sz; i++) {
    int32_t vgInfo;
    buf = taosDecodeFixedI32(buf, &vgInfo);
    taosArrayPush(pConsumerTopic->pVgInfo, &vgInfo);
  }
  return buf;
}

L
Liu Jicong 已提交
655 656
typedef struct SMqConsumerObj {
  int64_t  consumerId;
L
Liu Jicong 已提交
657
  int64_t  connId;
L
Liu Jicong 已提交
658
  SRWLatch lock;
L
Liu Jicong 已提交
659
  char     cgroup[TSDB_CONSUMER_GROUP_LEN];
L
Liu Jicong 已提交
660 661
  SArray*  topics;  // SArray<SMqConsumerTopic>
  // SHashObj *topicHash; //SHashObj<SMqTopicObj>
L
Liu Jicong 已提交
662 663
} SMqConsumerObj;

L
Liu Jicong 已提交
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
static FORCE_INLINE int32_t tEncodeSMqConsumerObj(void** buf, const SMqConsumerObj* pConsumer) {
  int32_t tlen = 0;
  tlen += taosEncodeFixedI64(buf, pConsumer->consumerId);
  tlen += taosEncodeString(buf, pConsumer->cgroup);
  int32_t sz = taosArrayGetSize(pConsumer->topics);
  tlen += taosEncodeFixedI32(buf, sz);
  for (int32_t i = 0; i < sz; i++) {
    SMqConsumerTopic* pConsumerTopic = taosArrayGet(pConsumer->topics, i);
    tlen += tEncodeSMqConsumerTopic(buf, pConsumerTopic);
  }
  return tlen;
}

static FORCE_INLINE void* tDecodeSMqConsumerObj(void* buf, SMqConsumerObj* pConsumer) {
  buf = taosDecodeFixedI64(buf, &pConsumer->consumerId);
  buf = taosDecodeStringTo(buf, pConsumer->cgroup);
  int32_t sz;
  buf = taosDecodeFixedI32(buf, &sz);
  pConsumer->topics = taosArrayInit(sz, sizeof(SMqConsumerObj));
  for (int32_t i = 0; i < sz; i++) {
    SMqConsumerTopic cTopic;
    buf = tDecodeSMqConsumerTopic(buf, &cTopic);
    taosArrayPush(pConsumer->topics, &cTopic);
  }
  return buf;
}

L
Liu Jicong 已提交
691 692
typedef struct SMqSubConsumerObj {
  int64_t consumerUid;  // if -1, unassigned
L
Liu Jicong 已提交
693
  SList*  vgId;         // SList<int32_t>
L
Liu Jicong 已提交
694 695 696 697
} SMqSubConsumerObj;

typedef struct SMqSubCGroupObj {
  char   name[TSDB_CONSUMER_GROUP_LEN];
L
Liu Jicong 已提交
698
  SList* consumers;  // SList<SMqConsumerObj>
L
Liu Jicong 已提交
699 700 701 702 703 704 705 706 707 708
} SMqSubCGroupObj;

typedef struct SMqSubTopicObj {
  char     name[TSDB_TOPIC_FNAME_LEN];
  char     db[TSDB_DB_FNAME_LEN];
  int64_t  createTime;
  int64_t  updateTime;
  int64_t  uid;
  int64_t  dbUid;
  int32_t  version;
L
Liu Jicong 已提交
709 710
  SRWLatch lock;
  int32_t  sqlLen;
L
Liu Jicong 已提交
711 712 713 714
  char*    sql;
  char*    logicalPlan;
  char*    physicalPlan;
  SList*   cgroups;  // SList<SMqSubCGroupObj>
L
Liu Jicong 已提交
715 716 717 718
} SMqSubTopicObj;

typedef struct SMqConsumerSubObj {
  int64_t topicUid;
L
Liu Jicong 已提交
719
  SList*  vgIds;  // SList<int64_t>
L
Liu Jicong 已提交
720 721 722 723
} SMqConsumerSubObj;

typedef struct SMqConsumerHbObj {
  int64_t consumerId;
L
Liu Jicong 已提交
724
  SList*  consumerSubs;  // SList<SMqConsumerSubObj>
L
Liu Jicong 已提交
725 726 727 728
} SMqConsumerHbObj;

typedef struct SMqVGroupSubObj {
  int64_t topicUid;
L
Liu Jicong 已提交
729
  SList*  consumerIds;  // SList<int64_t>
L
Liu Jicong 已提交
730 731 732 733
} SMqVGroupSubObj;

typedef struct SMqVGroupHbObj {
  int64_t vgId;
L
Liu Jicong 已提交
734
  SList*  vgSubs;  // SList<SMqVGroupSubObj>
L
Liu Jicong 已提交
735 736
} SMqVGroupHbObj;

L
Liu Jicong 已提交
737
#if 0
L
Liu Jicong 已提交
738
typedef struct SCGroupObj {
739
  char     name[TSDB_TOPIC_NAME_LEN];
L
Liu Jicong 已提交
740 741
  int64_t  createTime;
  int64_t  updateTime;
L
Liu Jicong 已提交
742 743
  uint64_t uid;
  //uint64_t dbUid;
L
Liu Jicong 已提交
744
  int32_t  version;
L
Liu Jicong 已提交
745
  SRWLatch lock;
L
Liu Jicong 已提交
746
  SList*   consumerIds;
L
Liu Jicong 已提交
747
} SCGroupObj;
L
Liu Jicong 已提交
748
#endif
L
Liu Jicong 已提交
749

S
Shengliang Guan 已提交
750
typedef struct SMnodeMsg {
S
Shengliang Guan 已提交
751
  char    user[TSDB_USER_LEN];
752
  char    db[TSDB_DB_FNAME_LEN];
S
Shengliang Guan 已提交
753
  int32_t acctId;
L
Liu Jicong 已提交
754
  SMnode* pMnode;
S
Shengliang Guan 已提交
755 756 757
  int64_t createdTime;
  SRpcMsg rpcMsg;
  int32_t contLen;
L
Liu Jicong 已提交
758
  void*   pCont;
S
Shengliang Guan 已提交
759
} SMnodeMsg;
S
Shengliang Guan 已提交
760

S
Shengliang Guan 已提交
761 762 763
#ifdef __cplusplus
}
#endif
S
Shengliang Guan 已提交
764

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