mndDef.h 18.1 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
typedef enum {
38 39 40 41 42 43 44 45
  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 已提交
46 47

typedef enum {
48 49 50 51 52 53
  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 已提交
54

S
Shengliang Guan 已提交
55
typedef enum {
56
  TRN_STAGE_PREPARE = 0,
S
Shengliang Guan 已提交
57 58
  TRN_STAGE_REDO_LOG = 1,
  TRN_STAGE_REDO_ACTION = 2,
59 60
  TRN_STAGE_COMMIT = 3,
  TRN_STAGE_COMMIT_LOG = 4,
61 62
  TRN_STAGE_UNDO_ACTION = 5,
  TRN_STAGE_UNDO_LOG = 6,
S
Shengliang Guan 已提交
63 64
  TRN_STAGE_ROLLBACK = 7,
  TRN_STAGE_FINISHED = 8
S
Shengliang Guan 已提交
65 66
} ETrnStage;

S
Shengliang Guan 已提交
67
typedef enum {
S
Shengliang Guan 已提交
68 69 70 71
  TRN_TYPE_BASIC_SCOPE = 1000,
  TRN_TYPE_CREATE_USER = 1001,
  TRN_TYPE_ALTER_USER = 1002,
  TRN_TYPE_DROP_USER = 1003,
S
Shengliang Guan 已提交
72 73
  TRN_TYPE_CREATE_FUNC = 1004,
  TRN_TYPE_DROP_FUNC = 1005,
S
Shengliang Guan 已提交
74 75 76 77 78 79 80 81 82 83 84 85
  TRN_TYPE_CREATE_SNODE = 1006,
  TRN_TYPE_DROP_SNODE = 1007,
  TRN_TYPE_CREATE_QNODE = 1008,
  TRN_TYPE_DROP_QNODE = 1009,
  TRN_TYPE_CREATE_BNODE = 1010,
  TRN_TYPE_DROP_BNODE = 1011,
  TRN_TYPE_CREATE_MNODE = 1012,
  TRN_TYPE_DROP_MNODE = 1013,
  TRN_TYPE_CREATE_TOPIC = 1014,
  TRN_TYPE_DROP_TOPIC = 1015,
  TRN_TYPE_SUBSCRIBE = 1016,
  TRN_TYPE_REBALANCE = 1017,
L
Liu Jicong 已提交
86
  TRN_TYPE_COMMIT_OFFSET = 1018,
L
Liu Jicong 已提交
87
  TRN_TYPE_CREATE_STREAM = 1019,
L
Liu Jicong 已提交
88 89
  TRN_TYPE_DROP_STREAM = 1020,
  TRN_TYPE_ALTER_STREAM = 1021,
S
Shengliang Guan 已提交
90 91 92 93 94 95 96 97 98
  TRN_TYPE_BASIC_SCOPE_END,
  TRN_TYPE_GLOBAL_SCOPE = 2000,
  TRN_TYPE_CREATE_DNODE = 2001,
  TRN_TYPE_DROP_DNODE = 2002,
  TRN_TYPE_GLOBAL_SCOPE_END,
  TRN_TYPE_DB_SCOPE = 3000,
  TRN_TYPE_CREATE_DB = 3001,
  TRN_TYPE_ALTER_DB = 3002,
  TRN_TYPE_DROP_DB = 3003,
S
Shengliang Guan 已提交
99 100
  TRN_TYPE_SPLIT_VGROUP = 3004,
  TRN_TYPE_MERGE_VGROUP = 3015,
S
Shengliang Guan 已提交
101
  TRN_TYPE_DB_SCOPE_END,
S
Shengliang Guan 已提交
102 103 104 105 106
  TRN_TYPE_STB_SCOPE = 4000,
  TRN_TYPE_CREATE_STB = 4001,
  TRN_TYPE_ALTER_STB = 4002,
  TRN_TYPE_DROP_STB = 4003,
  TRN_TYPE_STB_SCOPE_END,
S
Shengliang Guan 已提交
107 108
} ETrnType;

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

S
Shengliang Guan 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124
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 已提交
125
typedef struct {
S
Shengliang Guan 已提交
126 127 128
  int32_t    id;
  ETrnStage  stage;
  ETrnPolicy policy;
S
Shengliang Guan 已提交
129
  ETrnType   transType;
S
Shengliang Guan 已提交
130 131
  int32_t    code;
  int32_t    failedTimes;
L
Liu Jicong 已提交
132 133
  void*      rpcHandle;
  void*      rpcAHandle;
S
Shengliang Guan 已提交
134 135
  void*      rpcRsp;
  int32_t    rpcRspLen;
L
Liu Jicong 已提交
136 137 138 139 140
  SArray*    redoLogs;
  SArray*    undoLogs;
  SArray*    commitLogs;
  SArray*    redoActions;
  SArray*    undoActions;
S
Shengliang Guan 已提交
141 142
  int64_t    createdTime;
  int64_t    lastExecTime;
L
Liu Jicong 已提交
143
  int64_t    dbUid;
S
Shengliang Guan 已提交
144
  char       dbname[TSDB_DB_FNAME_LEN];
S
Shengliang Guan 已提交
145
  char       lastError[TSDB_TRANS_ERROR_LEN];
S
Shengliang Guan 已提交
146
} STrans;
S
Shengliang Guan 已提交
147

S
Shengliang Guan 已提交
148
typedef struct {
149
  int64_t id;
S
Shengliang Guan 已提交
150
  char    name[TSDB_CLUSTER_ID_LEN];
S
Shengliang Guan 已提交
151 152 153 154
  int64_t createdTime;
  int64_t updateTime;
} SClusterObj;

S
Shengliang Guan 已提交
155
typedef struct {
S
Shengliang Guan 已提交
156 157 158 159
  int32_t    id;
  int64_t    createdTime;
  int64_t    updateTime;
  int64_t    rebootTime;
160
  int64_t    lastAccessTime;
S
Shengliang Guan 已提交
161
  int32_t    accessTimes;
S
Shengliang Guan 已提交
162
  int32_t    numOfVnodes;
S
Shengliang Guan 已提交
163 164
  int32_t    numOfSupportVnodes;
  int32_t    numOfCores;
S
Shengliang Guan 已提交
165 166 167 168
  EDndReason offlineReason;
  uint16_t   port;
  char       fqdn[TSDB_FQDN_LEN];
  char       ep[TSDB_EP_LEN];
S
Shengliang Guan 已提交
169 170
} SDnodeObj;

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

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

typedef struct {
  int32_t    id;
  int64_t    createdTime;
  int64_t    updateTime;
L
Liu Jicong 已提交
192
  SDnodeObj* pDnode;
S
Shengliang Guan 已提交
193 194 195 196 197 198
} SSnodeObj;

typedef struct {
  int32_t    id;
  int64_t    createdTime;
  int64_t    updateTime;
L
Liu Jicong 已提交
199
  SDnodeObj* pDnode;
S
Shengliang Guan 已提交
200 201
} SBnodeObj;

S
Shengliang Guan 已提交
202 203 204
typedef struct {
  int32_t maxUsers;
  int32_t maxDbs;
S
Shengliang Guan 已提交
205 206
  int32_t maxStbs;
  int32_t maxTbs;
S
Shengliang Guan 已提交
207 208
  int32_t maxTimeSeries;
  int32_t maxStreams;
S
Shengliang Guan 已提交
209 210 211 212
  int32_t maxFuncs;
  int32_t maxConsumers;
  int32_t maxConns;
  int32_t maxTopics;
S
Shengliang Guan 已提交
213 214
  int64_t maxStorage;   // In unit of GB
  int32_t accessState;  // Configured only by command
S
Shengliang Guan 已提交
215 216 217 218 219 220 221
} SAcctCfg;

typedef struct {
  int32_t numOfUsers;
  int32_t numOfDbs;
  int32_t numOfTimeSeries;
  int32_t numOfStreams;
S
Shengliang Guan 已提交
222 223
  int64_t totalStorage;  // Total storage wrtten from this account
  int64_t compStorage;   // Compressed storage on disk
S
Shengliang Guan 已提交
224 225
} SAcctInfo;

S
Shengliang Guan 已提交
226
typedef struct {
S
Shengliang Guan 已提交
227 228 229 230
  char      acct[TSDB_USER_LEN];
  int64_t   createdTime;
  int64_t   updateTime;
  int32_t   acctId;
S
Shengliang Guan 已提交
231
  int32_t   status;
S
Shengliang Guan 已提交
232 233 234 235
  SAcctCfg  cfg;
  SAcctInfo info;
} SAcctObj;

S
Shengliang Guan 已提交
236
typedef struct {
S
Shengliang Guan 已提交
237
  char      user[TSDB_USER_LEN];
238
  char      pass[TSDB_PASSWORD_LEN];
S
Shengliang Guan 已提交
239 240 241
  char      acct[TSDB_USER_LEN];
  int64_t   createdTime;
  int64_t   updateTime;
242
  int8_t    superUser;
S
Shengliang Guan 已提交
243
  int32_t   acctId;
244 245
  SHashObj* readDbs;
  SHashObj* writeDbs;
S
Shengliang Guan 已提交
246 247 248
} SUserObj;

typedef struct {
249
  int32_t numOfVgroups;
S
Shengliang Guan 已提交
250 251 252 253 254 255
  int32_t cacheBlockSize;
  int32_t totalBlocks;
  int32_t daysPerFile;
  int32_t daysToKeep0;
  int32_t daysToKeep1;
  int32_t daysToKeep2;
S
Shengliang Guan 已提交
256 257
  int32_t minRows;
  int32_t maxRows;
S
Shengliang Guan 已提交
258 259
  int32_t commitTime;
  int32_t fsyncPeriod;
S
Shengliang Guan 已提交
260
  int8_t  walLevel;
S
Shengliang Guan 已提交
261 262 263 264 265 266
  int8_t  precision;
  int8_t  compression;
  int8_t  replications;
  int8_t  quorum;
  int8_t  update;
  int8_t  cacheLastRow;
L
Liu Jicong 已提交
267
  int8_t  streamMode;
S
Shengliang Guan 已提交
268 269
} SDbCfg;

S
Shengliang Guan 已提交
270
typedef struct {
L
Liu Jicong 已提交
271 272 273 274 275 276 277 278 279 280
  char    name[TSDB_DB_FNAME_LEN];
  char    acct[TSDB_USER_LEN];
  char    createUser[TSDB_USER_LEN];
  int64_t createdTime;
  int64_t updateTime;
  int64_t uid;
  int32_t cfgVersion;
  int32_t vgVersion;
  int8_t  hashMethod;  // default is 1
  SDbCfg  cfg;
S
Shengliang Guan 已提交
281 282 283 284
} SDbObj;

typedef struct {
  int32_t    dnodeId;
S
Shengliang Guan 已提交
285
  ESyncState role;
S
Shengliang Guan 已提交
286 287
} SVnodeGid;

S
Shengliang Guan 已提交
288
typedef struct {
S
Shengliang Guan 已提交
289
  int32_t   vgId;
S
Shengliang Guan 已提交
290 291
  int64_t   createdTime;
  int64_t   updateTime;
S
Shengliang Guan 已提交
292
  int32_t   version;
S
Shengliang Guan 已提交
293 294
  uint32_t  hashBegin;
  uint32_t  hashEnd;
295
  char      dbName[TSDB_DB_FNAME_LEN];
S
Shengliang Guan 已提交
296
  int64_t   dbUid;
S
Shengliang Guan 已提交
297 298
  int64_t   numOfTables;
  int64_t   numOfTimeSeries;
S
Shengliang Guan 已提交
299 300 301
  int64_t   totalStorage;
  int64_t   compStorage;
  int64_t   pointsWritten;
S
Shengliang Guan 已提交
302 303
  int8_t    compact;
  int8_t    replica;
L
Liu Jicong 已提交
304
  int8_t    streamMode;
S
Shengliang Guan 已提交
305
  SVnodeGid vnodeGid[TSDB_MAX_REPLICA];
S
Shengliang Guan 已提交
306 307
} SVgObj;

S
Shengliang Guan 已提交
308
typedef struct {
S
Shengliang Guan 已提交
309
  char     name[TSDB_TABLE_FNAME_LEN];
310
  char     db[TSDB_DB_FNAME_LEN];
311 312
  int64_t  createdTime;
  int64_t  updateTime;
L
Liu Jicong 已提交
313 314
  int64_t  uid;
  int64_t  dbUid;
S
Shengliang Guan 已提交
315
  int32_t  version;
S
Shengliang Guan 已提交
316
  int32_t  nextColId;
S
Shengliang Guan 已提交
317 318
  int32_t  numOfColumns;
  int32_t  numOfTags;
S
Shengliang Guan 已提交
319
  SSchema* pColumns;
S
Shengliang Guan 已提交
320
  SSchema* pTags;
S
Shengliang Guan 已提交
321
  SRWLatch lock;
S
Shengliang Guan 已提交
322
  char     comment[TSDB_STB_COMMENT_LEN];
S
Shengliang Guan 已提交
323
} SStbObj;
S
Shengliang Guan 已提交
324

S
Shengliang Guan 已提交
325
typedef struct {
S
Shengliang Guan 已提交
326 327
  char    name[TSDB_FUNC_NAME_LEN];
  int64_t createdTime;
S
Shengliang Guan 已提交
328 329 330 331 332 333
  int8_t  funcType;
  int8_t  scriptType;
  int8_t  align;
  int8_t  outputType;
  int32_t outputLen;
  int32_t bufSize;
S
Shengliang 已提交
334
  int64_t signature;
S
Shengliang Guan 已提交
335 336
  int32_t commentSize;
  int32_t codeSize;
L
Liu Jicong 已提交
337 338
  char*   pComment;
  char*   pCode;
S
Shengliang Guan 已提交
339
  char    pData[];
S
Shengliang Guan 已提交
340 341
} SFuncObj;

S
Shengliang Guan 已提交
342
typedef struct {
343
  int64_t id;
S
Shengliang Guan 已提交
344 345 346 347 348 349 350
  int8_t  type;
  int8_t  replica;
  int16_t numOfColumns;
  int32_t rowSize;
  int32_t numOfRows;
  int32_t numOfReads;
  int32_t payloadLen;
L
Liu Jicong 已提交
351 352
  void*   pIter;
  SMnode* pMnode;
353
  char    db[TSDB_DB_FNAME_LEN];
S
Shengliang Guan 已提交
354 355 356
  int16_t offset[TSDB_MAX_COLUMNS];
  int32_t bytes[TSDB_MAX_COLUMNS];
  char    payload[];
S
Shengliang Guan 已提交
357 358
} SShowObj;

359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
typedef struct {
  int64_t id;
  int8_t  type;
  int8_t  replica;
  int16_t numOfColumns;
  int32_t rowSize;
  int32_t numOfRows;
  int32_t numOfReads;
  int32_t payloadLen;
  void*   pIter;
  SMnode* pMnode;
  char    db[TSDB_DB_FNAME_LEN];
  int16_t offset[TSDB_MAX_COLUMNS];
  int32_t bytes[TSDB_MAX_COLUMNS];
  char    payload[];
} SSysTableRetrieveObj;

L
Liu Jicong 已提交
376 377 378 379 380 381 382
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 已提交
383 384
} SMqConsumerEp;

L
Liu Jicong 已提交
385
static FORCE_INLINE int32_t tEncodeSMqConsumerEp(void** buf, const SMqConsumerEp* pConsumerEp) {
L
Liu Jicong 已提交
386 387
  int32_t tlen = 0;
  tlen += taosEncodeFixedI32(buf, pConsumerEp->vgId);
L
Liu Jicong 已提交
388
  tlen += taosEncodeFixedI32(buf, pConsumerEp->status);
389
  tlen += taosEncodeSEpSet(buf, &pConsumerEp->epSet);
L
Liu Jicong 已提交
390
  tlen += taosEncodeFixedI64(buf, pConsumerEp->oldConsumerId);
L
Liu Jicong 已提交
391
  tlen += taosEncodeFixedI64(buf, pConsumerEp->consumerId);
L
Liu Jicong 已提交
392
  tlen += taosEncodeString(buf, pConsumerEp->qmsg);
L
Liu Jicong 已提交
393 394 395 396 397
  return tlen;
}

static FORCE_INLINE void* tDecodeSMqConsumerEp(void** buf, SMqConsumerEp* pConsumerEp) {
  buf = taosDecodeFixedI32(buf, &pConsumerEp->vgId);
L
Liu Jicong 已提交
398
  buf = taosDecodeFixedI32(buf, &pConsumerEp->status);
399
  buf = taosDecodeSEpSet(buf, &pConsumerEp->epSet);
L
Liu Jicong 已提交
400
  buf = taosDecodeFixedI64(buf, &pConsumerEp->oldConsumerId);
L
Liu Jicong 已提交
401
  buf = taosDecodeFixedI64(buf, &pConsumerEp->consumerId);
L
Liu Jicong 已提交
402
  buf = taosDecodeString(buf, &pConsumerEp->qmsg);
L
Liu Jicong 已提交
403 404 405
  return buf;
}

L
Liu Jicong 已提交
406 407 408 409 410 411
static FORCE_INLINE void tDeleteSMqConsumerEp(SMqConsumerEp* pConsumerEp) {
  if (pConsumerEp) {
    tfree(pConsumerEp->qmsg);
  }
}

L
Liu Jicong 已提交
412 413 414 415
typedef struct {
  int64_t consumerId;
  SArray* vgInfo;  // SArray<SMqConsumerEp>
} SMqSubConsumer;
L
Liu Jicong 已提交
416

L
Liu Jicong 已提交
417 418 419 420
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);
L
Liu Jicong 已提交
421
  tlen += taosEncodeFixedI32(buf, sz);
L
Liu Jicong 已提交
422 423 424
  for (int32_t i = 0; i < sz; i++) {
    SMqConsumerEp* pCEp = taosArrayGet(pConsumer->vgInfo, i);
    tlen += tEncodeSMqConsumerEp(buf, pCEp);
L
Liu Jicong 已提交
425
  }
L
Liu Jicong 已提交
426 427
  return tlen;
}
L
Liu Jicong 已提交
428

L
Liu Jicong 已提交
429 430 431 432 433 434 435 436 437
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 已提交
438
  }
L
Liu Jicong 已提交
439 440 441 442 443 444 445
  return buf;
}

static FORCE_INLINE void tDeleteSMqSubConsumer(SMqSubConsumer* pSubConsumer) {
  if (pSubConsumer->vgInfo) {
    taosArrayDestroyEx(pSubConsumer->vgInfo, (void (*)(void*))tDeleteSMqConsumerEp);
    pSubConsumer->vgInfo = NULL;
L
Liu Jicong 已提交
446
  }
L
Liu Jicong 已提交
447 448
}

L
Liu Jicong 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
typedef struct {
  char    key[TSDB_PARTITION_KEY_LEN];
  int64_t offset;
} SMqOffsetObj;

static FORCE_INLINE int32_t tEncodeSMqOffsetObj(void** buf, const SMqOffsetObj* pOffset) {
  int32_t tlen = 0;
  tlen += taosEncodeString(buf, pOffset->key);
  tlen += taosEncodeFixedI64(buf, pOffset->offset);
  return tlen;
}

static FORCE_INLINE void* tDecodeSMqOffsetObj(void* buf, SMqOffsetObj* pOffset) {
  buf = taosDecodeStringTo(buf, pOffset->key);
  buf = taosDecodeFixedI64(buf, &pOffset->offset);
  return buf;
}

L
Liu Jicong 已提交
467 468 469 470
typedef struct {
  char    key[TSDB_SUBSCRIBE_KEY_LEN];
  int32_t status;
  int32_t vgNum;
L
Liu Jicong 已提交
471 472 473
  SArray* consumers;      // SArray<SMqSubConsumer>
  SArray* lostConsumers;  // SArray<SMqSubConsumer>
  SArray* unassignedVg;   // SArray<SMqConsumerEp>
L
Liu Jicong 已提交
474 475 476 477 478
} SMqSubscribeObj;

static FORCE_INLINE SMqSubscribeObj* tNewSubscribeObj() {
  SMqSubscribeObj* pSub = calloc(1, sizeof(SMqSubscribeObj));
  if (pSub == NULL) {
L
Liu Jicong 已提交
479 480
    return NULL;
  }
L
Liu Jicong 已提交
481

L
Liu Jicong 已提交
482 483 484
  pSub->consumers = taosArrayInit(0, sizeof(SMqSubConsumer));
  if (pSub->consumers == NULL) {
    goto _err;
L
Liu Jicong 已提交
485
  }
L
Liu Jicong 已提交
486 487 488 489 490 491

  pSub->lostConsumers = taosArrayInit(0, sizeof(SMqSubConsumer));
  if (pSub->lostConsumers == NULL) {
    goto _err;
  }

L
Liu Jicong 已提交
492
  pSub->unassignedVg = taosArrayInit(0, sizeof(SMqConsumerEp));
L
Liu Jicong 已提交
493
  if (pSub->unassignedVg == NULL) {
L
Liu Jicong 已提交
494
    goto _err;
L
Liu Jicong 已提交
495
  }
L
Liu Jicong 已提交
496 497 498 499 500

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

L
Liu Jicong 已提交
501
  return pSub;
L
Liu Jicong 已提交
502 503 504

_err:
  tfree(pSub->consumers);
L
Liu Jicong 已提交
505 506
  tfree(pSub->lostConsumers);
  tfree(pSub->unassignedVg);
L
Liu Jicong 已提交
507 508
  tfree(pSub);
  return NULL;
L
Liu Jicong 已提交
509 510 511 512 513
}

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

L
Liu Jicong 已提交
518
  sz = taosArrayGetSize(pSub->consumers);
L
Liu Jicong 已提交
519 520
  tlen += taosEncodeFixedI32(buf, sz);
  for (int32_t i = 0; i < sz; i++) {
L
Liu Jicong 已提交
521 522
    SMqSubConsumer* pSubConsumer = taosArrayGet(pSub->consumers, i);
    tlen += tEncodeSMqSubConsumer(buf, pSubConsumer);
L
Liu Jicong 已提交
523 524
  }

L
Liu Jicong 已提交
525 526 527 528 529 530 531
  sz = taosArrayGetSize(pSub->lostConsumers);
  tlen += taosEncodeFixedI32(buf, sz);
  for (int32_t i = 0; i < sz; i++) {
    SMqSubConsumer* pSubConsumer = taosArrayGet(pSub->lostConsumers, i);
    tlen += tEncodeSMqSubConsumer(buf, pSubConsumer);
  }

L
Liu Jicong 已提交
532 533 534 535 536 537 538 539 540 541 542 543
  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 已提交
544 545
  buf = taosDecodeFixedI32(buf, &pSub->vgNum);
  buf = taosDecodeFixedI32(buf, &pSub->status);
L
Liu Jicong 已提交
546 547 548 549

  int32_t sz;

  buf = taosDecodeFixedI32(buf, &sz);
L
Liu Jicong 已提交
550 551
  pSub->consumers = taosArrayInit(sz, sizeof(SMqSubConsumer));
  if (pSub->consumers == NULL) {
L
Liu Jicong 已提交
552 553 554
    return NULL;
  }
  for (int32_t i = 0; i < sz; i++) {
L
Liu Jicong 已提交
555 556 557
    SMqSubConsumer subConsumer = {0};
    buf = tDecodeSMqSubConsumer(buf, &subConsumer);
    taosArrayPush(pSub->consumers, &subConsumer);
L
Liu Jicong 已提交
558 559
  }

L
Liu Jicong 已提交
560 561 562 563 564 565 566 567 568 569 570
  buf = taosDecodeFixedI32(buf, &sz);
  pSub->lostConsumers = taosArrayInit(sz, sizeof(SMqSubConsumer));
  if (pSub->lostConsumers == NULL) {
    return NULL;
  }
  for (int32_t i = 0; i < sz; i++) {
    SMqSubConsumer subConsumer = {0};
    buf = tDecodeSMqSubConsumer(buf, &subConsumer);
    taosArrayPush(pSub->lostConsumers, &subConsumer);
  }

L
Liu Jicong 已提交
571 572 573 574 575 576
  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 已提交
577 578 579
    SMqConsumerEp consumerEp = {0};
    buf = tDecodeSMqConsumerEp(buf, &consumerEp);
    taosArrayPush(pSub->unassignedVg, &consumerEp);
L
Liu Jicong 已提交
580 581 582
  }
  return buf;
}
L
Liu Jicong 已提交
583

L
Liu Jicong 已提交
584
static FORCE_INLINE void tDeleteSMqSubscribeObj(SMqSubscribeObj* pSub) {
L
Liu Jicong 已提交
585 586
  if (pSub->consumers) {
    taosArrayDestroyEx(pSub->consumers, (void (*)(void*))tDeleteSMqSubConsumer);
L
Liu Jicong 已提交
587
    // taosArrayDestroy(pSub->consumers);
L
Liu Jicong 已提交
588
    pSub->consumers = NULL;
L
Liu Jicong 已提交
589
  }
L
Liu Jicong 已提交
590

L
Liu Jicong 已提交
591
  if (pSub->unassignedVg) {
L
Liu Jicong 已提交
592
    taosArrayDestroyEx(pSub->unassignedVg, (void (*)(void*))tDeleteSMqConsumerEp);
L
Liu Jicong 已提交
593
    // taosArrayDestroy(pSub->unassignedVg);
L
Liu Jicong 已提交
594 595
    pSub->unassignedVg = NULL;
  }
L
Liu Jicong 已提交
596 597
}

L
Liu Jicong 已提交
598
typedef struct {
L
Liu Jicong 已提交
599 600 601 602
  char     name[TSDB_TOPIC_FNAME_LEN];
  char     db[TSDB_DB_FNAME_LEN];
  int64_t  createTime;
  int64_t  updateTime;
L
Liu Jicong 已提交
603
  int64_t  uid;
L
Liu Jicong 已提交
604
  int64_t  dbUid;
L
Liu Jicong 已提交
605 606 607 608 609 610
  int32_t  version;
  SRWLatch lock;
  int32_t  sqlLen;
  char*    sql;
  char*    logicalPlan;
  char*    physicalPlan;
L
Liu Jicong 已提交
611 612
} SMqTopicObj;

L
Liu Jicong 已提交
613
typedef struct {
L
Liu Jicong 已提交
614
  int64_t  consumerId;
L
Liu Jicong 已提交
615
  int64_t  connId;
L
Liu Jicong 已提交
616
  SRWLatch lock;
L
Liu Jicong 已提交
617
  char     cgroup[TSDB_CGROUP_LEN];
L
Liu Jicong 已提交
618 619
  SArray*  currentTopics;        // SArray<char*>
  SArray*  recentRemovedTopics;  // SArray<char*>
L
Liu Jicong 已提交
620
  int32_t  epoch;
L
Liu Jicong 已提交
621
  // stat
L
Liu Jicong 已提交
622 623 624 625 626 627 628
  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 已提交
629 630
} SMqConsumerObj;

L
Liu Jicong 已提交
631
static FORCE_INLINE int32_t tEncodeSMqConsumerObj(void** buf, const SMqConsumerObj* pConsumer) {
L
Liu Jicong 已提交
632
  int32_t sz;
L
Liu Jicong 已提交
633 634
  int32_t tlen = 0;
  tlen += taosEncodeFixedI64(buf, pConsumer->consumerId);
L
Liu Jicong 已提交
635
  tlen += taosEncodeFixedI64(buf, pConsumer->connId);
L
Liu Jicong 已提交
636
  tlen += taosEncodeFixedI32(buf, pConsumer->epoch);
L
Liu Jicong 已提交
637
  tlen += taosEncodeFixedI64(buf, pConsumer->pollCnt);
L
Liu Jicong 已提交
638
  tlen += taosEncodeFixedI32(buf, pConsumer->status);
L
Liu Jicong 已提交
639
  tlen += taosEncodeString(buf, pConsumer->cgroup);
L
Liu Jicong 已提交
640 641 642 643 644 645 646 647 648

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

  sz = taosArrayGetSize(pConsumer->recentRemovedTopics);
L
Liu Jicong 已提交
649 650
  tlen += taosEncodeFixedI32(buf, sz);
  for (int32_t i = 0; i < sz; i++) {
L
Liu Jicong 已提交
651
    char* topic = taosArrayGetP(pConsumer->recentRemovedTopics, i);
L
Liu Jicong 已提交
652
    tlen += taosEncodeString(buf, topic);
L
Liu Jicong 已提交
653 654 655 656 657
  }
  return tlen;
}

static FORCE_INLINE void* tDecodeSMqConsumerObj(void* buf, SMqConsumerObj* pConsumer) {
L
Liu Jicong 已提交
658
  int32_t sz;
L
Liu Jicong 已提交
659
  buf = taosDecodeFixedI64(buf, &pConsumer->consumerId);
L
Liu Jicong 已提交
660
  buf = taosDecodeFixedI64(buf, &pConsumer->connId);
L
Liu Jicong 已提交
661
  buf = taosDecodeFixedI32(buf, &pConsumer->epoch);
L
Liu Jicong 已提交
662
  buf = taosDecodeFixedI64(buf, &pConsumer->pollCnt);
L
Liu Jicong 已提交
663
  buf = taosDecodeFixedI32(buf, &pConsumer->status);
L
Liu Jicong 已提交
664
  buf = taosDecodeStringTo(buf, pConsumer->cgroup);
L
Liu Jicong 已提交
665 666 667 668 669 670 671 672 673

  buf = taosDecodeFixedI32(buf, &sz);
  pConsumer->currentTopics = taosArrayInit(sz, sizeof(SMqConsumerObj));
  for (int32_t i = 0; i < sz; i++) {
    char* topic;
    buf = taosDecodeString(buf, &topic);
    taosArrayPush(pConsumer->currentTopics, &topic);
  }

L
Liu Jicong 已提交
674
  buf = taosDecodeFixedI32(buf, &sz);
L
Liu Jicong 已提交
675
  pConsumer->recentRemovedTopics = taosArrayInit(sz, sizeof(SMqConsumerObj));
L
Liu Jicong 已提交
676
  for (int32_t i = 0; i < sz; i++) {
L
Liu Jicong 已提交
677 678
    char* topic;
    buf = taosDecodeString(buf, &topic);
L
Liu Jicong 已提交
679
    taosArrayPush(pConsumer->recentRemovedTopics, &topic);
L
Liu Jicong 已提交
680 681 682 683
  }
  return buf;
}

L
Liu Jicong 已提交
684
typedef struct {
L
Liu Jicong 已提交
685 686 687 688 689 690 691
  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 已提交
692
  int32_t  vgNum;
L
Liu Jicong 已提交
693 694 695
  SRWLatch lock;
  int8_t   status;
  // int32_t  sqlLen;
L
Liu Jicong 已提交
696 697 698
  char*   sql;
  char*   logicalPlan;
  char*   physicalPlan;
L
Liu Jicong 已提交
699
  SArray* tasks;  // SArray<SArray<SStreamTask>>
L
Liu Jicong 已提交
700
  SArray* outputName;
L
Liu Jicong 已提交
701 702 703 704
} SStreamObj;

int32_t tEncodeSStreamObj(SCoder* pEncoder, const SStreamObj* pObj);
int32_t tDecodeSStreamObj(SCoder* pDecoder, SStreamObj* pObj);
L
Liu Jicong 已提交
705

S
Shengliang Guan 已提交
706 707 708
#ifdef __cplusplus
}
#endif
S
Shengliang Guan 已提交
709

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