tmsg.h 65.9 KB
Newer Older
D
dapan1121 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * 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/>.
 */

#ifndef _TD_COMMON_TAOS_MSG_H_
#define _TD_COMMON_TAOS_MSG_H_

#include "taosdef.h"
#include "taoserror.h"
H
more  
Hongze Cheng 已提交
21
#include "tarray.h"
H
more  
Hongze Cheng 已提交
22
#include "tcoding.h"
S
Shengliang Guan 已提交
23
#include "tencode.h"
L
Liu Jicong 已提交
24
#include "thash.h"
H
Hongze Cheng 已提交
25
#include "tlist.h"
D
dapan1121 已提交
26
#include "tname.h"
L
Liu Jicong 已提交
27
#include "trow.h"
L
Liu Jicong 已提交
28
#include "tuuid.h"
D
dapan1121 已提交
29

S
Shengliang Guan 已提交
30 31 32 33
#ifdef __cplusplus
extern "C" {
#endif

H
Hongze Cheng 已提交
34
/* ------------------------ MESSAGE DEFINITIONS ------------------------ */
H
Hongze Cheng 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#define TD_MSG_NUMBER_
#undef TD_MSG_DICT_
#undef TD_MSG_INFO_
#undef TD_MSG_SEG_CODE_
#include "tmsgdef.h"

#undef TD_MSG_NUMBER_
#undef TD_MSG_DICT_
#undef TD_MSG_INFO_
#define TD_MSG_SEG_CODE_
#include "tmsgdef.h"

#undef TD_MSG_NUMBER_
#undef TD_MSG_DICT_
#undef TD_MSG_INFO_
#undef TD_MSG_SEG_CODE_
#include "tmsgdef.h"

S
Shengliang Guan 已提交
53 54
extern char*   tMsgInfo[];
extern int32_t tMsgDict[];
H
Hongze Cheng 已提交
55 56

#define TMSG_SEG_CODE(TYPE) (((TYPE)&0xff00) >> 8)
L
Liu Jicong 已提交
57 58 59
#define TMSG_SEG_SEQ(TYPE)  ((TYPE)&0xff)
#define TMSG_INFO(TYPE)     tMsgInfo[tMsgDict[TMSG_SEG_CODE(TYPE)] + TMSG_SEG_SEQ(TYPE)]
#define TMSG_INDEX(TYPE)    (tMsgDict[TMSG_SEG_CODE(TYPE)] + TMSG_SEG_SEQ(TYPE))
D
dapan1121 已提交
60

H
Hongze Cheng 已提交
61 62
typedef uint16_t tmsg_t;

H
Hongze Cheng 已提交
63
/* ------------------------ OTHER DEFINITIONS ------------------------ */
D
dapan1121 已提交
64
// IE type
L
Liu Jicong 已提交
65 66 67 68
#define TSDB_IE_TYPE_SEC         1
#define TSDB_IE_TYPE_META        2
#define TSDB_IE_TYPE_MGMT_IP     3
#define TSDB_IE_TYPE_DNODE_CFG   4
D
dapan1121 已提交
69
#define TSDB_IE_TYPE_NEW_VERSION 5
L
Liu Jicong 已提交
70
#define TSDB_IE_TYPE_DNODE_EXT   6
D
dapan1121 已提交
71 72
#define TSDB_IE_TYPE_DNODE_STATE 7

L
Liu Jicong 已提交
73
typedef enum {
74
  HEARTBEAT_TYPE_MQ = 0,
L
Liu Jicong 已提交
75
  HEARTBEAT_TYPE_QUERY,
L
Liu Jicong 已提交
76 77 78 79 80
  // types can be added here
  //
  HEARTBEAT_TYPE_MAX
} EHbType;

D
dapan1121 已提交
81
enum {
D
dapan1121 已提交
82 83
  HEARTBEAT_KEY_DBINFO = 1,
  HEARTBEAT_KEY_STBINFO,
D
dapan1121 已提交
84
  HEARTBEAT_KEY_MQ_TMP,
D
dapan1121 已提交
85 86
};

S
Shengliang Guan 已提交
87 88
typedef enum _mgmt_table {
  TSDB_MGMT_TABLE_START,
D
dapan1121 已提交
89 90 91 92 93 94
  TSDB_MGMT_TABLE_ACCT,
  TSDB_MGMT_TABLE_USER,
  TSDB_MGMT_TABLE_DB,
  TSDB_MGMT_TABLE_TABLE,
  TSDB_MGMT_TABLE_DNODE,
  TSDB_MGMT_TABLE_MNODE,
S
Shengliang Guan 已提交
95 96 97
  TSDB_MGMT_TABLE_QNODE,
  TSDB_MGMT_TABLE_SNODE,
  TSDB_MGMT_TABLE_BNODE,
D
dapan1121 已提交
98
  TSDB_MGMT_TABLE_VGROUP,
S
Shengliang Guan 已提交
99
  TSDB_MGMT_TABLE_STB,
D
dapan1121 已提交
100 101 102 103 104
  TSDB_MGMT_TABLE_MODULE,
  TSDB_MGMT_TABLE_QUERIES,
  TSDB_MGMT_TABLE_STREAMS,
  TSDB_MGMT_TABLE_VARIABLES,
  TSDB_MGMT_TABLE_CONNS,
S
Shengliang Guan 已提交
105
  TSDB_MGMT_TABLE_TRANS,
D
dapan1121 已提交
106 107 108
  TSDB_MGMT_TABLE_GRANTS,
  TSDB_MGMT_TABLE_VNODES,
  TSDB_MGMT_TABLE_CLUSTER,
S
Shengliang Guan 已提交
109
  TSDB_MGMT_TABLE_STREAMTABLES,
D
dapan1121 已提交
110
  TSDB_MGMT_TABLE_TP,
S
Shengliang 已提交
111
  TSDB_MGMT_TABLE_FUNC,
S
sma  
Shengliang Guan 已提交
112
  TSDB_MGMT_TABLE_INDEX,
D
dapan1121 已提交
113
  TSDB_MGMT_TABLE_MAX,
S
Shengliang Guan 已提交
114
} EShowType;
D
dapan1121 已提交
115

116 117 118 119
#define TSDB_ALTER_TABLE_ADD_TAG             1
#define TSDB_ALTER_TABLE_DROP_TAG            2
#define TSDB_ALTER_TABLE_UPDATE_TAG_NAME     3
#define TSDB_ALTER_TABLE_UPDATE_TAG_VAL      4
L
Liu Jicong 已提交
120 121
#define TSDB_ALTER_TABLE_ADD_COLUMN          5
#define TSDB_ALTER_TABLE_DROP_COLUMN         6
S
Shengliang Guan 已提交
122
#define TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES 7
L
Liu Jicong 已提交
123
#define TSDB_ALTER_TABLE_UPDATE_TAG_BYTES    8
124 125
#define TSDB_ALTER_TABLE_UPDATE_OPTIONS      9
#define TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME  10
D
dapan1121 已提交
126

L
Liu Jicong 已提交
127 128
#define TSDB_FILL_NONE      0
#define TSDB_FILL_NULL      1
H
Hongze Cheng 已提交
129
#define TSDB_FILL_SET_VALUE 2
L
Liu Jicong 已提交
130 131 132 133 134 135 136 137 138 139
#define TSDB_FILL_LINEAR    3
#define TSDB_FILL_PREV      4
#define TSDB_FILL_NEXT      5

#define TSDB_ALTER_USER_PASSWD          0x1
#define TSDB_ALTER_USER_SUPERUSER       0x2
#define TSDB_ALTER_USER_ADD_READ_DB     0x3
#define TSDB_ALTER_USER_REMOVE_READ_DB  0x4
#define TSDB_ALTER_USER_CLEAR_READ_DB   0x5
#define TSDB_ALTER_USER_ADD_WRITE_DB    0x6
S
Shengliang Guan 已提交
140
#define TSDB_ALTER_USER_REMOVE_WRITE_DB 0x7
L
Liu Jicong 已提交
141
#define TSDB_ALTER_USER_CLEAR_WRITE_DB  0x8
S
Shengliang Guan 已提交
142

D
dapan1121 已提交
143 144
#define TSDB_ALTER_USER_PRIVILEGES 0x2

H
Hongze Cheng 已提交
145
#define TSDB_KILL_MSG_LEN 30
D
dapan1121 已提交
146

D
dapan1121 已提交
147 148
#define TSDB_TABLE_NUM_UNIT 100000

L
Liu Jicong 已提交
149
#define TSDB_VN_READ_ACCCESS  ((char)0x1)
H
Hongze Cheng 已提交
150
#define TSDB_VN_WRITE_ACCCESS ((char)0x2)
L
Liu Jicong 已提交
151
#define TSDB_VN_ALL_ACCCESS   (TSDB_VN_READ_ACCCESS | TSDB_VN_WRITE_ACCCESS)
D
dapan1121 已提交
152

H
Hongze Cheng 已提交
153
#define TSDB_COL_NORMAL 0x0u  // the normal column of the table
L
Liu Jicong 已提交
154 155 156 157
#define TSDB_COL_TAG    0x1u  // the tag column type
#define TSDB_COL_UDC    0x2u  // the user specified normal string column, it is a dummy column
#define TSDB_COL_TMP    0x4u  // internal column generated by the previous operators
#define TSDB_COL_NULL   0x8u  // the column filter NULL or not
D
dapan1121 已提交
158

L
Liu Jicong 已提交
159
#define TSDB_COL_IS_TAG(f)        (((f & (~(TSDB_COL_NULL))) & TSDB_COL_TAG) != 0)
H
Hongze Cheng 已提交
160
#define TSDB_COL_IS_NORMAL_COL(f) ((f & (~(TSDB_COL_NULL))) == TSDB_COL_NORMAL)
L
Liu Jicong 已提交
161 162
#define TSDB_COL_IS_UD_COL(f)     ((f & (~(TSDB_COL_NULL))) == TSDB_COL_UDC)
#define TSDB_COL_REQ_NULL(f)      (((f)&TSDB_COL_NULL) != 0)
S
Shengliang Guan 已提交
163

L
Liu Jicong 已提交
164 165
#define TD_SUPER_TABLE  TSDB_SUPER_TABLE
#define TD_CHILD_TABLE  TSDB_CHILD_TABLE
S
Shengliang Guan 已提交
166 167
#define TD_NORMAL_TABLE TSDB_NORMAL_TABLE

S
Shengliang Guan 已提交
168
typedef struct {
S
Shengliang Guan 已提交
169
  int32_t vgId;
D
dapan1121 已提交
170 171
  char*   dbFName;
  char*   tbName;
D
dapan1121 已提交
172 173
} SBuildTableMetaInput;

S
Shengliang Guan 已提交
174
typedef struct {
D
dapan1121 已提交
175
  char    db[TSDB_DB_FNAME_LEN];
D
dapan1121 已提交
176
  int64_t dbId;
177
  int32_t vgVersion;
L
Liu Jicong 已提交
178
  int32_t numOfTable;  // unit is TSDB_TABLE_NUM_UNIT
D
dapan1121 已提交
179 180
} SBuildUseDBInput;

S
Shengliang Guan 已提交
181 182 183 184 185 186
typedef struct SField {
  char    name[TSDB_COL_NAME_LEN];
  uint8_t type;
  int32_t bytes;
} SField;

S
Shengliang Guan 已提交
187
typedef struct SRetention {
S
sma  
Shengliang Guan 已提交
188 189 190 191
  int32_t freq;
  int32_t keep;
  int8_t  freqUnit;
  int8_t  keepUnit;
S
Shengliang Guan 已提交
192 193
} SRetention;

D
dapan1121 已提交
194 195 196
#pragma pack(push, 1)

// null-terminated string instead of char array to avoid too many memory consumption in case of more than 1M tableMeta
H
Haojun Liao 已提交
197
typedef struct SEp {
D
dapan1121 已提交
198
  char     fqdn[TSDB_FQDN_LEN];
D
dapan1121 已提交
199
  uint16_t port;
H
Haojun Liao 已提交
200
} SEp;
D
dapan1121 已提交
201

S
Shengliang Guan 已提交
202
typedef struct {
D
dapan1121 已提交
203
  int32_t contLen;
L
Liu Jicong 已提交
204
  int32_t vgId;
D
dapan1121 已提交
205 206 207 208
} SMsgHead;

// Submit message for one table
typedef struct SSubmitBlk {
209
  int64_t uid;        // table unique id
L
Liu Jicong 已提交
210
  int64_t suid;       // stable id
211 212 213 214
  int32_t sversion;   // data schema version
  int32_t dataLen;    // data part length, not including the SSubmitBlk head
  int32_t schemaLen;  // schema length, if length is 0, no schema exists
  int16_t numOfRows;  // total number of rows in current submit block
C
Cary Xu 已提交
215
  int16_t padding;    // TODO just for padding here
216
  char    data[];
D
dapan1121 已提交
217 218 219
} SSubmitBlk;

// Submit message for this TSDB
S
Shengliang Guan 已提交
220
typedef struct {
S
Shengliang Guan 已提交
221
  SMsgHead header;
H
Hongze Cheng 已提交
222
  int64_t  version;
S
Shengliang Guan 已提交
223 224 225
  int32_t  length;
  int32_t  numOfBlocks;
  char     blocks[];
S
Shengliang Guan 已提交
226
} SSubmitReq;
D
dapan1121 已提交
227

H
Hongze Cheng 已提交
228 229 230
typedef struct {
  int32_t totalLen;
  int32_t len;
231
  STSRow* row;
H
Hongze Cheng 已提交
232 233 234
} SSubmitBlkIter;

typedef struct {
C
Cary Xu 已提交
235 236 237
  int32_t     totalLen;
  int32_t     len;
  const void* pMsg;
H
Hongze Cheng 已提交
238 239
} SSubmitMsgIter;

C
Cary Xu 已提交
240
int32_t tInitSubmitMsgIter(const SSubmitReq* pMsg, SSubmitMsgIter* pIter);
S
Shengliang Guan 已提交
241 242
int32_t tGetSubmitMsgNext(SSubmitMsgIter* pIter, SSubmitBlk** pPBlock);
int32_t tInitSubmitBlkIter(SSubmitBlk* pBlock, SSubmitBlkIter* pIter);
C
Cary Xu 已提交
243
STSRow* tGetSubmitBlkNext(SSubmitBlkIter* pIter);
H
Hongze Cheng 已提交
244

D
dapan1121 已提交
245 246 247 248 249
typedef struct {
  int32_t index;  // index of failed block in submit blocks
  int32_t vnode;  // vnode index of failed block
  int32_t sid;    // table index of failed block
  int32_t code;   // errorcode while write data to vnode, such as not created, dropped, no space, invalid table
S
Shengliang Guan 已提交
250
} SSubmitRspBlock;
D
dapan1121 已提交
251 252

typedef struct {
S
Shengliang Guan 已提交
253 254 255 256 257 258 259
  int32_t         code;          // 0-success, > 0 error code
  int32_t         numOfRows;     // number of records the client is trying to write
  int32_t         affectedRows;  // number of records actually written
  int32_t         failedRows;    // number of failed records (exclude duplicate records)
  int32_t         numOfFailedBlocks;
  SSubmitRspBlock failedBlocks[];
} SSubmitRsp;
D
dapan1121 已提交
260 261

typedef struct SSchema {
262 263 264 265
  int8_t   type;
  col_id_t colId;
  int32_t  bytes;
  char     name[TSDB_COL_NAME_LEN];
D
dapan1121 已提交
266 267 268
} SSchema;

typedef struct {
S
Shengliang Guan 已提交
269 270
  char    name[TSDB_TABLE_FNAME_LEN];
  int8_t  igExists;
S
sma  
Shengliang Guan 已提交
271 272 273
  float   xFilesFactor;
  int32_t aggregationMethod;
  int32_t delay;
S
sma  
Shengliang Guan 已提交
274
  int32_t ttl;
S
Shengliang Guan 已提交
275
  int32_t numOfColumns;
S
Shengliang Guan 已提交
276
  int32_t numOfTags;
S
sma  
Shengliang Guan 已提交
277
  int32_t numOfSmas;
S
sma  
Shengliang Guan 已提交
278
  int32_t commentLen;
S
sma  
Shengliang Guan 已提交
279 280 281
  SArray* pColumns;  // array of SField
  SArray* pTags;     // array of SField
  SArray* pSmas;     // array of SField
S
sma  
Shengliang Guan 已提交
282
  char*   comment;
S
Shengliang Guan 已提交
283
} SMCreateStbReq;
D
dapan1121 已提交
284

S
Shengliang Guan 已提交
285 286
int32_t tSerializeSMCreateStbReq(void* buf, int32_t bufLen, SMCreateStbReq* pReq);
int32_t tDeserializeSMCreateStbReq(void* buf, int32_t bufLen, SMCreateStbReq* pReq);
S
Shengliang Guan 已提交
287
void    tFreeSMCreateStbReq(SMCreateStbReq* pReq);
S
Shengliang Guan 已提交
288

D
dapan1121 已提交
289 290 291
typedef struct {
  char   name[TSDB_TABLE_FNAME_LEN];
  int8_t igNotExists;
S
Shengliang Guan 已提交
292
} SMDropStbReq;
S
Shengliang Guan 已提交
293

S
Shengliang Guan 已提交
294 295
int32_t tSerializeSMDropStbReq(void* buf, int32_t bufLen, SMDropStbReq* pReq);
int32_t tDeserializeSMDropStbReq(void* buf, int32_t bufLen, SMDropStbReq* pReq);
S
Shengliang Guan 已提交
296

S
Shengliang Guan 已提交
297 298 299
typedef struct {
  char    name[TSDB_TABLE_FNAME_LEN];
  int8_t  alterType;
S
Shengliang Guan 已提交
300 301
  int32_t numOfFields;
  SArray* pFields;
S
Shengliang Guan 已提交
302
} SMAltertbReq;
D
dapan1121 已提交
303

S
Shengliang Guan 已提交
304 305 306
int32_t tSerializeSMAlterStbReq(void* buf, int32_t bufLen, SMAltertbReq* pReq);
int32_t tDeserializeSMAlterStbReq(void* buf, int32_t bufLen, SMAltertbReq* pReq);
void    tFreeSMAltertbReq(SMAltertbReq* pReq);
D
dapan1121 已提交
307 308

typedef struct SEpSet {
309 310 311
  int8_t inUse;
  int8_t numOfEps;
  SEp    eps[TSDB_MAX_REPLICA];
D
dapan1121 已提交
312 313
} SEpSet;

S
Shengliang Guan 已提交
314 315 316 317
int32_t tEncodeSEpSet(SCoder* pEncoder, const SEpSet* pEp);
int32_t tDecodeSEpSet(SCoder* pDecoder, SEpSet* pEp);
int32_t taosEncodeSEpSet(void** buf, const SEpSet* pEp);
void*   taosDecodeSEpSet(void* buf, SEpSet* pEp);
L
Liu Jicong 已提交
318

D
dapan1121 已提交
319 320
typedef struct {
  int32_t pid;
S
Shengliang Guan 已提交
321 322
  char    app[TSDB_APP_NAME_LEN];
  char    db[TSDB_DB_NAME_LEN];
323
  int64_t startTime;
S
Shengliang Guan 已提交
324
} SConnectReq;
L
Liu Jicong 已提交
325

S
Shengliang Guan 已提交
326 327
int32_t tSerializeSConnectReq(void* buf, int32_t bufLen, SConnectReq* pReq);
int32_t tDeserializeSConnectReq(void* buf, int32_t bufLen, SConnectReq* pReq);
328

L
Liu Jicong 已提交
329 330 331 332 333 334
typedef struct {
  int32_t acctId;
  int64_t clusterId;
  int32_t connId;
  int8_t  superUser;
  SEpSet  epSet;
335
  char    sVersion[128];
L
Liu Jicong 已提交
336
} SConnectRsp;
L
Liu Jicong 已提交
337

S
Shengliang Guan 已提交
338 339 340
int32_t tSerializeSConnectRsp(void* buf, int32_t bufLen, SConnectRsp* pRsp);
int32_t tDeserializeSConnectRsp(void* buf, int32_t bufLen, SConnectRsp* pRsp);

L
Liu Jicong 已提交
341 342 343 344 345 346 347 348 349 350
typedef struct {
  char    user[TSDB_USER_LEN];
  char    pass[TSDB_PASSWORD_LEN];
  int32_t maxUsers;
  int32_t maxDbs;
  int32_t maxTimeSeries;
  int32_t maxStreams;
  int32_t accessState;  // Configured only by command
  int64_t maxStorage;   // In unit of GB
} SCreateAcctReq, SAlterAcctReq;
L
Liu Jicong 已提交
351

S
Shengliang Guan 已提交
352 353
int32_t tSerializeSCreateAcctReq(void* buf, int32_t bufLen, SCreateAcctReq* pReq);
int32_t tDeserializeSCreateAcctReq(void* buf, int32_t bufLen, SCreateAcctReq* pReq);
S
Shengliang Guan 已提交
354

L
Liu Jicong 已提交
355 356 357
typedef struct {
  char user[TSDB_USER_LEN];
} SDropUserReq, SDropAcctReq;
D
dapan1121 已提交
358

S
Shengliang Guan 已提交
359 360
int32_t tSerializeSDropUserReq(void* buf, int32_t bufLen, SDropUserReq* pReq);
int32_t tDeserializeSDropUserReq(void* buf, int32_t bufLen, SDropUserReq* pReq);
S
Shengliang Guan 已提交
361

D
dapan1121 已提交
362
typedef struct {
S
Shengliang Guan 已提交
363 364
  int8_t createType;
  int8_t superUser;  // denote if it is a super user or not
S
Shengliang Guan 已提交
365 366
  char   user[TSDB_USER_LEN];
  char   pass[TSDB_PASSWORD_LEN];
S
Shengliang Guan 已提交
367 368
} SCreateUserReq;

S
Shengliang Guan 已提交
369 370
int32_t tSerializeSCreateUserReq(void* buf, int32_t bufLen, SCreateUserReq* pReq);
int32_t tDeserializeSCreateUserReq(void* buf, int32_t bufLen, SCreateUserReq* pReq);
S
Shengliang Guan 已提交
371 372 373

typedef struct {
  int8_t alterType;
S
Shengliang Guan 已提交
374
  int8_t superUser;
S
Shengliang Guan 已提交
375 376 377 378 379
  char   user[TSDB_USER_LEN];
  char   pass[TSDB_PASSWORD_LEN];
  char   dbname[TSDB_DB_FNAME_LEN];
} SAlterUserReq;

S
Shengliang Guan 已提交
380 381
int32_t tSerializeSAlterUserReq(void* buf, int32_t bufLen, SAlterUserReq* pReq);
int32_t tDeserializeSAlterUserReq(void* buf, int32_t bufLen, SAlterUserReq* pReq);
D
dapan1121 已提交
382

S
Shengliang Guan 已提交
383 384 385 386
typedef struct {
  char user[TSDB_USER_LEN];
} SGetUserAuthReq;

S
Shengliang Guan 已提交
387 388
int32_t tSerializeSGetUserAuthReq(void* buf, int32_t bufLen, SGetUserAuthReq* pReq);
int32_t tDeserializeSGetUserAuthReq(void* buf, int32_t bufLen, SGetUserAuthReq* pReq);
S
Shengliang Guan 已提交
389 390 391 392 393 394 395 396

typedef struct {
  char      user[TSDB_USER_LEN];
  int8_t    superAuth;
  SHashObj* readDbs;
  SHashObj* writeDbs;
} SGetUserAuthRsp;

S
Shengliang Guan 已提交
397 398
int32_t tSerializeSGetUserAuthRsp(void* buf, int32_t bufLen, SGetUserAuthRsp* pRsp);
int32_t tDeserializeSGetUserAuthRsp(void* buf, int32_t bufLen, SGetUserAuthRsp* pRsp);
S
Shengliang Guan 已提交
399

D
dapan1121 已提交
400
typedef struct {
S
Shengliang Guan 已提交
401 402 403
  int16_t colId;     // column id
  int16_t colIndex;  // column index in colList if it is a normal column or index in tagColList if a tag
  int16_t flag;      // denote if it is a tag or a normal column
404
  char    name[TSDB_DB_FNAME_LEN];
D
dapan1121 已提交
405 406
} SColIndex;

S
Shengliang Guan 已提交
407
typedef struct {
D
dapan1121 已提交
408 409
  int16_t lowerRelOptr;
  int16_t upperRelOptr;
S
Shengliang Guan 已提交
410
  int16_t filterstr;  // denote if current column is char(binary/nchar)
D
dapan1121 已提交
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427

  union {
    struct {
      int64_t lowerBndi;
      int64_t upperBndi;
    };
    struct {
      double lowerBndd;
      double upperBndd;
    };
    struct {
      int64_t pz;
      int64_t len;
    };
  };
} SColumnFilterInfo;

S
Shengliang Guan 已提交
428
typedef struct {
S
Shengliang Guan 已提交
429 430 431
  int16_t numOfFilters;
  union {
    int64_t            placeholder;
H
Hongze Cheng 已提交
432
    SColumnFilterInfo* filterInfo;
D
dapan1121 已提交
433 434 435
  };
} SColumnFilterList;
/*
L
Liu Jicong 已提交
436
 * for client side struct, only column id, type, bytes are necessary
D
dapan1121 已提交
437 438
 * But for data in vnode side, we need all the following information.
 */
S
Shengliang Guan 已提交
439
typedef struct {
H
Haojun Liao 已提交
440
  union {
441 442
    col_id_t colId;
    int16_t  slotId;
H
Haojun Liao 已提交
443 444
  };

L
Liu Jicong 已提交
445 446 447 448
  int16_t type;
  int32_t bytes;
  uint8_t precision;
  uint8_t scale;
D
dapan1121 已提交
449 450
} SColumnInfo;

S
Shengliang Guan 已提交
451
typedef struct {
L
Liu Jicong 已提交
452 453
  int64_t uid;
  TSKEY   key;  // last accessed ts, for subscription
D
dapan1121 已提交
454 455 456 457 458 459 460 461
} STableIdInfo;

typedef struct STimeWindow {
  TSKEY skey;
  TSKEY ekey;
} STimeWindow;

typedef struct {
S
Shengliang Guan 已提交
462 463 464 465
  int32_t tsOffset;       // offset value in current msg body, NOTE: ts list is compressed
  int32_t tsLen;          // total length of ts comp block
  int32_t tsNumOfBlocks;  // ts comp block numbers
  int32_t tsOrder;        // ts comp block order
D
dapan1121 已提交
466 467
} STsBufInfo;

S
Shengliang Guan 已提交
468
typedef struct {
S
Shengliang Guan 已提交
469
  int32_t tz;  // query client timezone
D
dapan1121 已提交
470 471
  char    intervalUnit;
  char    slidingUnit;
L
Liu Jicong 已提交
472 473
  char
      offsetUnit;  // TODO Remove it, the offset is the number of precision tickle, and it must be a immutable duration.
H
Haojun Liao 已提交
474
  int8_t  precision;
D
dapan1121 已提交
475 476 477 478 479 480
  int64_t interval;
  int64_t sliding;
  int64_t offset;
} SInterval;

typedef struct {
S
Shengliang Guan 已提交
481
  int32_t code;
D
dapan1121 已提交
482 483
} SQueryTableRsp;

L
Liu Jicong 已提交
484
int32_t tSerializeSQueryTableRsp(void* buf, int32_t bufLen, SQueryTableRsp* pRsp);
D
dapan1121 已提交
485

L
Liu Jicong 已提交
486
int32_t tDeserializeSQueryTableRsp(void* buf, int32_t bufLen, SQueryTableRsp* pRsp);
D
dapan1121 已提交
487

D
dapan1121 已提交
488
typedef struct {
489
  char    db[TSDB_DB_FNAME_LEN];
490
  int32_t numOfVgroups;
S
Shengliang Guan 已提交
491 492 493 494 495 496
  int32_t cacheBlockSize;  // MB
  int32_t totalBlocks;
  int32_t daysPerFile;
  int32_t daysToKeep0;
  int32_t daysToKeep1;
  int32_t daysToKeep2;
S
Shengliang Guan 已提交
497 498
  int32_t minRows;
  int32_t maxRows;
S
Shengliang Guan 已提交
499 500
  int32_t commitTime;
  int32_t fsyncPeriod;
S
Shengliang Guan 已提交
501
  int8_t  walLevel;
S
Shengliang Guan 已提交
502 503 504 505 506 507
  int8_t  precision;  // time resolution
  int8_t  compression;
  int8_t  replications;
  int8_t  quorum;
  int8_t  update;
  int8_t  cacheLastRow;
S
Shengliang Guan 已提交
508
  int8_t  ignoreExist;
X
Xiaoyu Wang 已提交
509
  int8_t  streamMode;
S
Shengliang Guan 已提交
510 511
  int32_t numOfRetensions;
  SArray* pRetensions;  // SRetention
S
Shengliang Guan 已提交
512
} SCreateDbReq;
S
Shengliang Guan 已提交
513

S
Shengliang Guan 已提交
514 515
int32_t tSerializeSCreateDbReq(void* buf, int32_t bufLen, SCreateDbReq* pReq);
int32_t tDeserializeSCreateDbReq(void* buf, int32_t bufLen, SCreateDbReq* pReq);
S
Shengliang Guan 已提交
516
void    tFreeSCreateDbReq(SCreateDbReq* pReq);
S
Shengliang Guan 已提交
517

S
Shengliang Guan 已提交
518
typedef struct {
519
  char    db[TSDB_DB_FNAME_LEN];
S
Shengliang Guan 已提交
520 521 522 523 524 525 526 527
  int32_t totalBlocks;
  int32_t daysToKeep0;
  int32_t daysToKeep1;
  int32_t daysToKeep2;
  int32_t fsyncPeriod;
  int8_t  walLevel;
  int8_t  quorum;
  int8_t  cacheLastRow;
S
Shengliang Guan 已提交
528
} SAlterDbReq;
S
Shengliang Guan 已提交
529

S
Shengliang Guan 已提交
530 531 532
int32_t tSerializeSAlterDbReq(void* buf, int32_t bufLen, SAlterDbReq* pReq);
int32_t tDeserializeSAlterDbReq(void* buf, int32_t bufLen, SAlterDbReq* pReq);

S
Shengliang Guan 已提交
533
typedef struct {
S
Shengliang Guan 已提交
534
  char   db[TSDB_DB_FNAME_LEN];
S
Shengliang Guan 已提交
535
  int8_t ignoreNotExists;
S
Shengliang Guan 已提交
536
} SDropDbReq;
S
Shengliang Guan 已提交
537

S
Shengliang Guan 已提交
538 539 540
int32_t tSerializeSDropDbReq(void* buf, int32_t bufLen, SDropDbReq* pReq);
int32_t tDeserializeSDropDbReq(void* buf, int32_t bufLen, SDropDbReq* pReq);

S
Shengliang Guan 已提交
541
typedef struct {
L
Liu Jicong 已提交
542 543
  char    db[TSDB_DB_FNAME_LEN];
  int64_t uid;
S
Shengliang Guan 已提交
544 545
} SDropDbRsp;

S
Shengliang Guan 已提交
546 547 548
int32_t tSerializeSDropDbRsp(void* buf, int32_t bufLen, SDropDbRsp* pRsp);
int32_t tDeserializeSDropDbRsp(void* buf, int32_t bufLen, SDropDbRsp* pRsp);

S
Shengliang Guan 已提交
549 550
typedef struct {
  char    db[TSDB_DB_FNAME_LEN];
D
dapan1121 已提交
551
  int64_t dbId;
552
  int32_t vgVersion;
L
Liu Jicong 已提交
553
  int32_t numOfTable;  // unit is TSDB_TABLE_NUM_UNIT
S
Shengliang Guan 已提交
554
} SUseDbReq;
S
Shengliang Guan 已提交
555

S
Shengliang Guan 已提交
556 557
int32_t tSerializeSUseDbReq(void* buf, int32_t bufLen, SUseDbReq* pReq);
int32_t tDeserializeSUseDbReq(void* buf, int32_t bufLen, SUseDbReq* pReq);
S
Shengliang Guan 已提交
558 559

typedef struct {
L
Liu Jicong 已提交
560 561 562 563 564 565
  char    db[TSDB_DB_FNAME_LEN];
  int64_t uid;
  int32_t vgVersion;
  int32_t vgNum;
  int8_t  hashMethod;
  SArray* pVgroupInfos;  // Array of SVgroupInfo
S
Shengliang Guan 已提交
566 567 568 569 570 571
} SUseDbRsp;

int32_t tSerializeSUseDbRsp(void* buf, int32_t bufLen, SUseDbRsp* pRsp);
int32_t tDeserializeSUseDbRsp(void* buf, int32_t bufLen, SUseDbRsp* pRsp);
void    tFreeSUsedbRsp(SUseDbRsp* pRsp);

D
dapan1121 已提交
572 573 574 575 576 577 578 579
typedef struct {
  int32_t rowNum;
} SQnodeListReq;

int32_t tSerializeSQnodeListReq(void* buf, int32_t bufLen, SQnodeListReq* pReq);
int32_t tDeserializeSQnodeListReq(void* buf, int32_t bufLen, SQnodeListReq* pReq);

typedef struct {
L
Liu Jicong 已提交
580
  SArray* epSetList;  // SArray<SEpSet>
D
dapan1121 已提交
581 582 583 584 585 586
} SQnodeListRsp;

int32_t tSerializeSQnodeListRsp(void* buf, int32_t bufLen, SQnodeListRsp* pRsp);
int32_t tDeserializeSQnodeListRsp(void* buf, int32_t bufLen, SQnodeListRsp* pRsp);
void    tFreeSQnodeListRsp(SQnodeListRsp* pRsp);

S
Shengliang Guan 已提交
587 588 589 590 591 592 593
typedef struct {
  SArray* pArray;  // Array of SUseDbRsp
} SUseDbBatchRsp;

int32_t tSerializeSUseDbBatchRsp(void* buf, int32_t bufLen, SUseDbBatchRsp* pRsp);
int32_t tDeserializeSUseDbBatchRsp(void* buf, int32_t bufLen, SUseDbBatchRsp* pRsp);
void    tFreeSUseDbBatchRsp(SUseDbBatchRsp* pRsp);
S
Shengliang Guan 已提交
594 595

typedef struct {
S
Shengliang Guan 已提交
596
  char db[TSDB_DB_FNAME_LEN];
S
Shengliang Guan 已提交
597 598 599 600
} SSyncDbReq, SCompactDbReq;

int32_t tSerializeSSyncDbReq(void* buf, int32_t bufLen, SSyncDbReq* pReq);
int32_t tDeserializeSSyncDbReq(void* buf, int32_t bufLen, SSyncDbReq* pReq);
D
dapan1121 已提交
601 602

typedef struct {
S
Shengliang Guan 已提交
603
  char    name[TSDB_FUNC_NAME_LEN];
S
Shengliang 已提交
604
  int8_t  igExists;
S
Shengliang Guan 已提交
605 606
  int8_t  funcType;
  int8_t  scriptType;
S
Shengliang Guan 已提交
607
  int8_t  outputType;
S
Shengliang Guan 已提交
608
  int32_t outputLen;
S
Shengliang Guan 已提交
609
  int32_t bufSize;
S
Shengliang 已提交
610
  int64_t signature;
S
Shengliang Guan 已提交
611 612
  int32_t commentSize;
  int32_t codeSize;
S
Shengliang Guan 已提交
613 614
  char    pComment[TSDB_FUNC_COMMENT_LEN];
  char    pCode[TSDB_FUNC_CODE_LEN];
S
Shengliang Guan 已提交
615
} SCreateFuncReq;
D
dapan1121 已提交
616

S
Shengliang Guan 已提交
617 618 619
int32_t tSerializeSCreateFuncReq(void* buf, int32_t bufLen, SCreateFuncReq* pReq);
int32_t tDeserializeSCreateFuncReq(void* buf, int32_t bufLen, SCreateFuncReq* pReq);

D
dapan1121 已提交
620
typedef struct {
S
Shengliang 已提交
621 622
  char   name[TSDB_FUNC_NAME_LEN];
  int8_t igNotExists;
S
Shengliang Guan 已提交
623
} SDropFuncReq;
S
Shengliang Guan 已提交
624

S
Shengliang Guan 已提交
625 626 627
int32_t tSerializeSDropFuncReq(void* buf, int32_t bufLen, SDropFuncReq* pReq);
int32_t tDeserializeSDropFuncReq(void* buf, int32_t bufLen, SDropFuncReq* pReq);

S
Shengliang Guan 已提交
628 629
typedef struct {
  int32_t numOfFuncs;
S
Shengliang Guan 已提交
630
  SArray* pFuncNames;
S
Shengliang Guan 已提交
631
} SRetrieveFuncReq;
D
dapan1121 已提交
632

S
Shengliang Guan 已提交
633 634 635
int32_t tSerializeSRetrieveFuncReq(void* buf, int32_t bufLen, SRetrieveFuncReq* pReq);
int32_t tDeserializeSRetrieveFuncReq(void* buf, int32_t bufLen, SRetrieveFuncReq* pReq);

D
dapan1121 已提交
636 637
typedef struct {
  char    name[TSDB_FUNC_NAME_LEN];
S
Shengliang Guan 已提交
638 639 640 641
  int8_t  funcType;
  int8_t  scriptType;
  int8_t  outputType;
  int32_t outputLen;
D
dapan1121 已提交
642
  int32_t bufSize;
S
Shengliang 已提交
643
  int64_t signature;
S
Shengliang Guan 已提交
644 645
  int32_t commentSize;
  int32_t codeSize;
S
Shengliang Guan 已提交
646 647
  char    pComment[TSDB_FUNC_COMMENT_LEN];
  char    pCode[TSDB_FUNC_CODE_LEN];
S
Shengliang Guan 已提交
648
} SFuncInfo;
D
dapan1121 已提交
649 650

typedef struct {
S
Shengliang Guan 已提交
651
  int32_t numOfFuncs;
S
Shengliang Guan 已提交
652
  SArray* pFuncInfos;
S
Shengliang Guan 已提交
653
} SRetrieveFuncRsp;
D
dapan1121 已提交
654

S
Shengliang Guan 已提交
655 656 657
int32_t tSerializeSRetrieveFuncRsp(void* buf, int32_t bufLen, SRetrieveFuncRsp* pRsp);
int32_t tDeserializeSRetrieveFuncRsp(void* buf, int32_t bufLen, SRetrieveFuncRsp* pRsp);

D
dapan1121 已提交
658
typedef struct {
S
Shengliang Guan 已提交
659
  int32_t statusInterval;
S
Shengliang Guan 已提交
660
  int64_t checkTime;                  // 1970-01-01 00:00:00.000
S
Shengliang Guan 已提交
661 662 663
  char    timezone[TD_TIMEZONE_LEN];  // tsTimezone
  char    locale[TD_LOCALE_LEN];      // tsLocale
  char    charset[TD_LOCALE_LEN];     // tsCharset
S
Shengliang Guan 已提交
664
} SClusterCfg;
D
dapan1121 已提交
665

S
Shengliang Guan 已提交
666 667 668
typedef struct {
  int32_t vgId;
  int8_t  role;
S
Shengliang Guan 已提交
669 670
  int64_t numOfTables;
  int64_t numOfTimeSeries;
S
Shengliang Guan 已提交
671 672 673
  int64_t totalStorage;
  int64_t compStorage;
  int64_t pointsWritten;
S
Shengliang Guan 已提交
674 675 676 677 678
  int64_t numOfSelectReqs;
  int64_t numOfInsertReqs;
  int64_t numOfInsertSuccessReqs;
  int64_t numOfBatchInsertReqs;
  int64_t numOfBatchInsertSuccessReqs;
S
Shengliang Guan 已提交
679 680 681
} SVnodeLoad;

typedef struct {
S
Shengliang Guan 已提交
682 683
  int32_t     sver;  // software version
  int64_t     dver;  // dnode table version in sdb
S
Shengliang Guan 已提交
684
  int32_t     dnodeId;
685
  int64_t     clusterId;
S
Shengliang Guan 已提交
686 687
  int64_t     rebootTime;
  int64_t     updateTime;
S
Shengliang Guan 已提交
688 689
  int32_t     numOfCores;
  int32_t     numOfSupportVnodes;
S
Shengliang Guan 已提交
690 691
  char        dnodeEp[TSDB_EP_LEN];
  SClusterCfg clusterCfg;
S
Shengliang Guan 已提交
692
  SArray*     pVloads;  // array of SVnodeLoad
S
Shengliang Guan 已提交
693
} SStatusReq;
D
dapan1121 已提交
694

S
Shengliang Guan 已提交
695 696
int32_t tSerializeSStatusReq(void* buf, int32_t bufLen, SStatusReq* pReq);
int32_t tDeserializeSStatusReq(void* buf, int32_t bufLen, SStatusReq* pReq);
S
Shengliang Guan 已提交
697

D
dapan1121 已提交
698
typedef struct {
S
Shengliang Guan 已提交
699
  int32_t dnodeId;
700
  int64_t clusterId;
D
dapan1121 已提交
701 702 703
} SDnodeCfg;

typedef struct {
704 705 706
  int32_t id;
  int8_t  isMnode;
  SEp     ep;
D
dapan1121 已提交
707 708 709
} SDnodeEp;

typedef struct {
710
  int64_t   dver;
S
Shengliang Guan 已提交
711
  SDnodeCfg dnodeCfg;
S
Shengliang Guan 已提交
712
  SArray*   pDnodeEps;  // Array of SDnodeEp
D
dapan1121 已提交
713 714
} SStatusRsp;

S
Shengliang Guan 已提交
715 716
int32_t tSerializeSStatusRsp(void* buf, int32_t bufLen, SStatusRsp* pRsp);
int32_t tDeserializeSStatusRsp(void* buf, int32_t bufLen, SStatusRsp* pRsp);
S
shm  
Shengliang Guan 已提交
717
void    tFreeSStatusRsp(SStatusRsp* pRsp);
S
Shengliang Guan 已提交
718 719

typedef struct {
S
Shengliang Guan 已提交
720 721 722 723 724
  int32_t reserved;
} SMTimerReq;

int32_t tSerializeSMTimerMsg(void* buf, int32_t bufLen, SMTimerReq* pReq);
int32_t tDeserializeSMTimerMsg(void* buf, int32_t bufLen, SMTimerReq* pReq);
S
Shengliang Guan 已提交
725

D
dapan1121 已提交
726
typedef struct {
S
Shengliang Guan 已提交
727 728 729
  int32_t  id;
  uint16_t port;                 // node sync Port
  char     fqdn[TSDB_FQDN_LEN];  // node FQDN
S
Shengliang Guan 已提交
730 731 732
} SReplica;

typedef struct {
S
Shengliang Guan 已提交
733
  int32_t  vgId;
S
Shengliang Guan 已提交
734
  int32_t  dnodeId;
735
  char     db[TSDB_DB_FNAME_LEN];
L
Liu Jicong 已提交
736
  int64_t  dbUid;
737
  int32_t  vgVersion;
S
Shengliang Guan 已提交
738 739 740 741 742 743
  int32_t  cacheBlockSize;
  int32_t  totalBlocks;
  int32_t  daysPerFile;
  int32_t  daysToKeep0;
  int32_t  daysToKeep1;
  int32_t  daysToKeep2;
S
Shengliang Guan 已提交
744 745 746
  int32_t  minRows;
  int32_t  maxRows;
  int32_t  commitTime;
S
Shengliang Guan 已提交
747
  int32_t  fsyncPeriod;
D
dapan1121 已提交
748 749
  uint32_t hashBegin;
  uint32_t hashEnd;
L
Liu Jicong 已提交
750
  int8_t   hashMethod;
S
Shengliang Guan 已提交
751
  int8_t   walLevel;
S
Shengliang Guan 已提交
752 753 754
  int8_t   precision;
  int8_t   compression;
  int8_t   quorum;
S
Shengliang Guan 已提交
755 756
  int8_t   update;
  int8_t   cacheLastRow;
S
Shengliang Guan 已提交
757
  int8_t   replica;
S
Shengliang Guan 已提交
758
  int8_t   selfIndex;
L
Liu Jicong 已提交
759
  int8_t   streamMode;
S
Shengliang Guan 已提交
760
  SReplica replicas[TSDB_MAX_REPLICA];
S
sma  
Shengliang Guan 已提交
761 762
  int32_t  numOfRetensions;
  SArray*  pRetensions;  // SRetention
S
Shengliang Guan 已提交
763
} SCreateVnodeReq, SAlterVnodeReq;
D
dapan1121 已提交
764

S
Shengliang Guan 已提交
765 766
int32_t tSerializeSCreateVnodeReq(void* buf, int32_t bufLen, SCreateVnodeReq* pReq);
int32_t tDeserializeSCreateVnodeReq(void* buf, int32_t bufLen, SCreateVnodeReq* pReq);
S
sma  
Shengliang Guan 已提交
767
int32_t tFreeSCreateVnodeReq(SCreateVnodeReq* pReq);
S
Shengliang Guan 已提交
768

S
Shengliang Guan 已提交
769
typedef struct {
L
Liu Jicong 已提交
770 771 772 773
  int32_t vgId;
  int32_t dnodeId;
  int64_t dbUid;
  char    db[TSDB_DB_FNAME_LEN];
S
Shengliang Guan 已提交
774
} SDropVnodeReq, SSyncVnodeReq, SCompactVnodeReq;
S
Shengliang Guan 已提交
775

S
Shengliang Guan 已提交
776 777
int32_t tSerializeSDropVnodeReq(void* buf, int32_t bufLen, SDropVnodeReq* pReq);
int32_t tDeserializeSDropVnodeReq(void* buf, int32_t bufLen, SDropVnodeReq* pReq);
S
Shengliang Guan 已提交
778

D
dapan1121 已提交
779
typedef struct {
D
dapan1121 已提交
780
  SMsgHead header;
D
dapan1121 已提交
781 782
  char     dbFName[TSDB_DB_FNAME_LEN];
  char     tbName[TSDB_TABLE_NAME_LEN];
S
Shengliang Guan 已提交
783
} STableInfoReq;
D
dapan1121 已提交
784

S
Shengliang Guan 已提交
785 786 787
int32_t tSerializeSTableInfoReq(void* buf, int32_t bufLen, STableInfoReq* pReq);
int32_t tDeserializeSTableInfoReq(void* buf, int32_t bufLen, STableInfoReq* pReq);

D
dapan1121 已提交
788
typedef struct {
S
Shengliang Guan 已提交
789
  int8_t  metaClone;  // create local clone of the cached table meta
D
dapan1121 已提交
790 791 792 793
  int32_t numOfVgroups;
  int32_t numOfTables;
  int32_t numOfUdfs;
  char    tableNames[];
S
Shengliang Guan 已提交
794
} SMultiTableInfoReq;
D
dapan1121 已提交
795

H
Haojun Liao 已提交
796
// todo refactor
D
dapan1121 已提交
797
typedef struct SVgroupInfo {
798 799 800
  int32_t  vgId;
  uint32_t hashBegin;
  uint32_t hashEnd;
L
Liu Jicong 已提交
801
  SEpSet   epSet;
D
dapan1121 已提交
802
  int32_t  numOfTable;  // unit is TSDB_TABLE_NUM_UNIT
D
dapan1121 已提交
803 804
} SVgroupInfo;

D
dapan1121 已提交
805
typedef struct {
H
Haojun Liao 已提交
806 807
  int32_t     numOfVgroups;
  SVgroupInfo vgroups[];
S
Shengliang Guan 已提交
808
} SVgroupsInfo;
D
dapan1121 已提交
809

S
Shengliang Guan 已提交
810
typedef struct {
D
dapan1121 已提交
811 812 813
  char     tbName[TSDB_TABLE_NAME_LEN];
  char     stbName[TSDB_TABLE_NAME_LEN];
  char     dbFName[TSDB_DB_FNAME_LEN];
L
Liu Jicong 已提交
814
  int64_t  dbId;
S
Shengliang Guan 已提交
815 816 817 818 819 820 821
  int32_t  numOfTags;
  int32_t  numOfColumns;
  int8_t   precision;
  int8_t   tableType;
  int8_t   update;
  int32_t  sversion;
  int32_t  tversion;
L
Liu Jicong 已提交
822 823
  uint64_t suid;
  uint64_t tuid;
S
Shengliang Guan 已提交
824
  int32_t  vgId;
S
Shengliang Guan 已提交
825
  SSchema* pSchemas;
S
Shengliang Guan 已提交
826
} STableMetaRsp;
D
dapan1121 已提交
827

S
Shengliang Guan 已提交
828 829 830 831 832 833 834 835 836 837 838 839
int32_t tSerializeSTableMetaRsp(void* buf, int32_t bufLen, STableMetaRsp* pRsp);
int32_t tDeserializeSTableMetaRsp(void* buf, int32_t bufLen, STableMetaRsp* pRsp);
void    tFreeSTableMetaRsp(STableMetaRsp* pRsp);

typedef struct {
  SArray* pArray;  // Array of STableMetaRsp
} STableMetaBatchRsp;

int32_t tSerializeSTableMetaBatchRsp(void* buf, int32_t bufLen, STableMetaBatchRsp* pRsp);
int32_t tDeserializeSTableMetaBatchRsp(void* buf, int32_t bufLen, STableMetaBatchRsp* pRsp);
void    tFreeSTableMetaBatchRsp(STableMetaBatchRsp* pRsp);

S
Shengliang Guan 已提交
840
typedef struct {
S
Shengliang Guan 已提交
841 842 843 844 845 846 847 848
  int32_t numOfTables;
  int32_t numOfVgroup;
  int32_t numOfUdf;
  int32_t contLen;
  int8_t  compressed;  // denote if compressed or not
  int32_t rawLen;      // size before compress
  uint8_t metaClone;   // make meta clone after retrieve meta from mnode
  char    meta[];
D
dapan1121 已提交
849 850 851 852 853
} SMultiTableMeta;

typedef struct {
  int32_t dataLen;
  char    name[TSDB_TABLE_FNAME_LEN];
H
Hongze Cheng 已提交
854
  char*   data;
D
dapan1121 已提交
855 856 857 858 859 860 861 862
} STagData;

/*
 * sql: show tables like '%a_%'
 * payload is the query condition, e.g., '%a_%'
 * payloadLen is the length of payload
 */
typedef struct {
L
Liu Jicong 已提交
863
  int32_t type;
864
  char    db[TSDB_DB_FNAME_LEN];
S
Shengliang Guan 已提交
865 866
  int32_t payloadLen;
  char*   payload;
S
Shengliang Guan 已提交
867
} SShowReq;
D
dapan1121 已提交
868

S
Shengliang Guan 已提交
869 870 871 872
int32_t tSerializeSShowReq(void* buf, int32_t bufLen, SShowReq* pReq);
int32_t tDeserializeSShowReq(void* buf, int32_t bufLen, SShowReq* pReq);
void    tFreeSShowReq(SShowReq* pReq);

S
Shengliang Guan 已提交
873
typedef struct {
874
  int64_t       showId;
S
Shengliang Guan 已提交
875
  STableMetaRsp tableMeta;
S
Shengliang Guan 已提交
876
} SShowRsp, SVShowTablesRsp;
D
dapan1121 已提交
877

S
Shengliang Guan 已提交
878 879 880
int32_t tSerializeSShowRsp(void* buf, int32_t bufLen, SShowRsp* pRsp);
int32_t tDeserializeSShowRsp(void* buf, int32_t bufLen, SShowRsp* pRsp);
void    tFreeSShowRsp(SShowRsp* pRsp);
D
dapan1121 已提交
881

882
typedef struct {
883 884
  int32_t type;
  char    db[TSDB_DB_FNAME_LEN];
H
Haojun Liao 已提交
885
  char    tb[TSDB_TABLE_NAME_LEN];
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
  int64_t showId;
  int8_t  free;
} SRetrieveTableReq;

int32_t tSerializeSRetrieveTableReq(void* buf, int32_t bufLen, SRetrieveTableReq* pReq);
int32_t tDeserializeSRetrieveTableReq(void* buf, int32_t bufLen, SRetrieveTableReq* pReq);

typedef struct {
  int64_t useconds;
  int8_t  completed;  // all results are returned to client
  int8_t  precision;
  int8_t  compressed;
  int32_t compLen;
  int32_t numOfRows;
  char    data[];
} SRetrieveTableRsp;

H
Haojun Liao 已提交
903
typedef struct {
L
Liu Jicong 已提交
904 905 906 907 908 909 910 911
  int64_t handle;
  int64_t useconds;
  int8_t  completed;  // all results are returned to client
  int8_t  precision;
  int8_t  compressed;
  int32_t compLen;
  int32_t numOfRows;
  char    data[];
H
Haojun Liao 已提交
912 913
} SRetrieveMetaTableRsp;

D
dapan1121 已提交
914
typedef struct {
S
Shengliang Guan 已提交
915
  char    fqdn[TSDB_FQDN_LEN];  // end point, hostname:port
916
  int32_t port;
S
Shengliang Guan 已提交
917
} SCreateDnodeReq;
S
Shengliang Guan 已提交
918

S
Shengliang Guan 已提交
919 920 921
int32_t tSerializeSCreateDnodeReq(void* buf, int32_t bufLen, SCreateDnodeReq* pReq);
int32_t tDeserializeSCreateDnodeReq(void* buf, int32_t bufLen, SCreateDnodeReq* pReq);

S
Shengliang Guan 已提交
922 923
typedef struct {
  int32_t dnodeId;
924 925
  char    fqdn[TSDB_FQDN_LEN];
  int32_t port;
S
Shengliang Guan 已提交
926
} SDropDnodeReq;
S
Shengliang Guan 已提交
927

S
Shengliang Guan 已提交
928 929 930
int32_t tSerializeSDropDnodeReq(void* buf, int32_t bufLen, SDropDnodeReq* pReq);
int32_t tDeserializeSDropDnodeReq(void* buf, int32_t bufLen, SDropDnodeReq* pReq);

S
Shengliang Guan 已提交
931 932
typedef struct {
  int32_t dnodeId;
933
  char    config[TSDB_DNODE_CONFIG_LEN];
S
Shengliang Guan 已提交
934
  char    value[TSDB_DNODE_VALUE_LEN];
S
Shengliang Guan 已提交
935
} SMCfgDnodeReq, SDCfgDnodeReq;
D
dapan1121 已提交
936

S
Shengliang Guan 已提交
937 938 939
int32_t tSerializeSMCfgDnodeReq(void* buf, int32_t bufLen, SMCfgDnodeReq* pReq);
int32_t tDeserializeSMCfgDnodeReq(void* buf, int32_t bufLen, SMCfgDnodeReq* pReq);

S
Shengliang Guan 已提交
940 941
typedef struct {
  int32_t dnodeId;
S
Shengliang Guan 已提交
942
} SMCreateMnodeReq, SMDropMnodeReq, SDDropMnodeReq;
S
Shengliang Guan 已提交
943

S
Shengliang Guan 已提交
944 945 946
int32_t tSerializeSMCreateDropMnodeReq(void* buf, int32_t bufLen, SMCreateMnodeReq* pReq);
int32_t tDeserializeSMCreateDropMnodeReq(void* buf, int32_t bufLen, SMCreateMnodeReq* pReq);

D
dapan1121 已提交
947
typedef struct {
S
Shengliang Guan 已提交
948 949
  int32_t  dnodeId;
  int8_t   replica;
S
Shengliang Guan 已提交
950
  SReplica replicas[TSDB_MAX_REPLICA];
S
Shengliang Guan 已提交
951
} SDCreateMnodeReq, SDAlterMnodeReq;
D
dapan1121 已提交
952

S
Shengliang Guan 已提交
953 954 955
int32_t tSerializeSDCreateMnodeReq(void* buf, int32_t bufLen, SDCreateMnodeReq* pReq);
int32_t tDeserializeSDCreateMnodeReq(void* buf, int32_t bufLen, SDCreateMnodeReq* pReq);

S
Shengliang Guan 已提交
956 957
typedef struct {
  int32_t dnodeId;
S
Shengliang Guan 已提交
958 959
} SMCreateQnodeReq, SMDropQnodeReq, SDCreateQnodeReq, SDDropQnodeReq, SMCreateSnodeReq, SMDropSnodeReq,
    SDCreateSnodeReq, SDDropSnodeReq, SMCreateBnodeReq, SMDropBnodeReq, SDCreateBnodeReq, SDDropBnodeReq;
S
Shengliang Guan 已提交
960

S
Shengliang Guan 已提交
961 962
int32_t tSerializeSMCreateDropQSBNodeReq(void* buf, int32_t bufLen, SMCreateQnodeReq* pReq);
int32_t tDeserializeSMCreateDropQSBNodeReq(void* buf, int32_t bufLen, SMCreateQnodeReq* pReq);
S
Shengliang Guan 已提交
963

D
dapan1121 已提交
964
typedef struct {
S
Shengliang Guan 已提交
965 966 967 968 969 970 971 972 973 974 975
  char    sql[TSDB_SHOW_SQL_LEN];
  int32_t queryId;
  int64_t useconds;
  int64_t stime;
  int64_t qId;
  int64_t sqlObjId;
  int32_t pid;
  char    fqdn[TSDB_FQDN_LEN];
  int8_t  stableQuery;
  int32_t numOfSub;
  char    subSqlInfo[TSDB_SHOW_SUBQUERY_LEN];  // include subqueries' index, Obj IDs and states(C-complete/I-imcomplete)
D
dapan1121 已提交
976 977 978
} SQueryDesc;

typedef struct {
S
Shengliang Guan 已提交
979 980 981 982 983 984
  int32_t connId;
  int32_t pid;
  int32_t numOfQueries;
  int32_t numOfStreams;
  char    app[TSDB_APP_NAME_LEN];
  char    pData[];
S
Shengliang Guan 已提交
985
} SHeartBeatReq;
D
dapan1121 已提交
986 987

typedef struct {
S
Shengliang Guan 已提交
988 989 990 991 992 993
  int32_t connId;
  int32_t queryId;
  int32_t streamId;
  int32_t totalDnodes;
  int32_t onlineDnodes;
  int8_t  killConnection;
S
Shengliang Guan 已提交
994
  int8_t  align[3];
S
Shengliang Guan 已提交
995
  SEpSet  epSet;
D
dapan1121 已提交
996 997
} SHeartBeatRsp;

S
Shengliang Guan 已提交
998 999 1000
typedef struct {
  int32_t connId;
  int32_t queryId;
S
Shengliang Guan 已提交
1001
} SKillQueryReq;
S
Shengliang Guan 已提交
1002

S
Shengliang Guan 已提交
1003 1004 1005
int32_t tSerializeSKillQueryReq(void* buf, int32_t bufLen, SKillQueryReq* pReq);
int32_t tDeserializeSKillQueryReq(void* buf, int32_t bufLen, SKillQueryReq* pReq);

S
Shengliang Guan 已提交
1006 1007
typedef struct {
  int32_t connId;
S
Shengliang Guan 已提交
1008
} SKillConnReq;
D
dapan1121 已提交
1009

S
Shengliang Guan 已提交
1010 1011 1012
int32_t tSerializeSKillConnReq(void* buf, int32_t bufLen, SKillConnReq* pReq);
int32_t tDeserializeSKillConnReq(void* buf, int32_t bufLen, SKillConnReq* pReq);

S
Shengliang Guan 已提交
1013 1014 1015 1016 1017 1018 1019
typedef struct {
  int32_t transId;
} SKillTransReq;

int32_t tSerializeSKillTransReq(void* buf, int32_t bufLen, SKillTransReq* pReq);
int32_t tDeserializeSKillTransReq(void* buf, int32_t bufLen, SKillTransReq* pReq);

D
dapan1121 已提交
1020 1021 1022 1023
typedef struct {
  char user[TSDB_USER_LEN];
  char spi;
  char encrypt;
1024 1025
  char secret[TSDB_PASSWORD_LEN];
  char ckey[TSDB_PASSWORD_LEN];
S
Shengliang Guan 已提交
1026
} SAuthReq, SAuthRsp;
D
dapan1121 已提交
1027

S
Shengliang Guan 已提交
1028 1029 1030
int32_t tSerializeSAuthReq(void* buf, int32_t bufLen, SAuthReq* pReq);
int32_t tDeserializeSAuthReq(void* buf, int32_t bufLen, SAuthReq* pReq);

D
dapan1121 已提交
1031
typedef struct {
S
Shengliang Guan 已提交
1032 1033 1034
  int8_t finished;
  char   name[TSDB_STEP_NAME_LEN];
  char   desc[TSDB_STEP_DESC_LEN];
S
Shengliang Guan 已提交
1035
} SStartupReq;
D
dapan1121 已提交
1036

1037 1038 1039 1040 1041 1042 1043
/**
 * The layout of the query message payload is as following:
 * +--------------------+---------------------------------+
 * |Sql statement       | Physical plan                   |
 * |(denoted by sqlLen) |(In JSON, denoted by contentLen) |
 * +--------------------+---------------------------------+
 */
L
Liu Jicong 已提交
1044
typedef struct SSubQueryMsg {
D
dapan1121 已提交
1045
  SMsgHead header;
D
dapan1121 已提交
1046
  uint64_t sId;
H
Hongze Cheng 已提交
1047 1048
  uint64_t queryId;
  uint64_t taskId;
D
dapan1121 已提交
1049
  int64_t  refId;
D
dapan1121 已提交
1050
  int8_t   taskType;
1051
  uint32_t sqlLen;  // the query sql,
1052
  uint32_t phyLen;
H
Hongze Cheng 已提交
1053
  char     msg[];
D
dapan1121 已提交
1054
} SSubQueryMsg;
D
dapan 已提交
1055

1056 1057 1058 1059 1060 1061 1062
typedef struct {
  SMsgHead header;
  uint64_t sId;
  uint64_t queryId;
  uint64_t taskId;
} SSinkDataReq;

D
dapan1121 已提交
1063 1064 1065 1066 1067 1068 1069
typedef struct {
  SMsgHead header;
  uint64_t sId;
  uint64_t queryId;
  uint64_t taskId;
} SQueryContinueReq;

S
Shengliang Guan 已提交
1070
typedef struct {
D
dapan1121 已提交
1071
  SMsgHead header;
D
dapan1121 已提交
1072
  uint64_t sId;
H
Hongze Cheng 已提交
1073 1074
  uint64_t queryId;
  uint64_t taskId;
S
Shengliang Guan 已提交
1075
} SResReadyReq;
D
dapan 已提交
1076

S
Shengliang Guan 已提交
1077
typedef struct {
D
dapan1121 已提交
1078 1079 1080
  int32_t code;
} SResReadyRsp;

S
Shengliang Guan 已提交
1081
typedef struct {
D
dapan1121 已提交
1082
  SMsgHead header;
D
dapan1121 已提交
1083
  uint64_t sId;
H
Hongze Cheng 已提交
1084 1085
  uint64_t queryId;
  uint64_t taskId;
S
Shengliang Guan 已提交
1086
} SResFetchReq;
D
dapan 已提交
1087

S
Shengliang Guan 已提交
1088
typedef struct {
D
dapan1121 已提交
1089
  SMsgHead header;
D
dapan1121 已提交
1090
  uint64_t sId;
S
Shengliang Guan 已提交
1091
} SSchTasksStatusReq;
D
dapan1121 已提交
1092

S
Shengliang Guan 已提交
1093
typedef struct {
H
Hongze Cheng 已提交
1094 1095
  uint64_t queryId;
  uint64_t taskId;
D
dapan1121 已提交
1096
  int64_t  refId;
H
Hongze Cheng 已提交
1097
  int8_t   status;
D
dapan1121 已提交
1098 1099
} STaskStatus;

S
Shengliang Guan 已提交
1100
typedef struct {
L
Liu Jicong 已提交
1101 1102
  int64_t refId;
  SArray* taskStatus;  // SArray<STaskStatus>
D
dapan1121 已提交
1103 1104
} SSchedulerStatusRsp;

D
dapan1121 已提交
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
typedef struct {
  uint64_t queryId;
  uint64_t taskId;
  int8_t   action;
} STaskAction;

typedef struct SQueryNodeEpId {
  int32_t nodeId;  // vgId or qnodeId
  SEp     ep;
} SQueryNodeEpId;

typedef struct {
  SMsgHead       header;
  uint64_t       sId;
  SQueryNodeEpId epId;
L
Liu Jicong 已提交
1120
  SArray*        taskAction;  // SArray<STaskAction>
D
dapan1121 已提交
1121 1122
} SSchedulerHbReq;

L
Liu Jicong 已提交
1123 1124 1125
int32_t tSerializeSSchedulerHbReq(void* buf, int32_t bufLen, SSchedulerHbReq* pReq);
int32_t tDeserializeSSchedulerHbReq(void* buf, int32_t bufLen, SSchedulerHbReq* pReq);
void    tFreeSSchedulerHbReq(SSchedulerHbReq* pReq);
D
dapan1121 已提交
1126 1127 1128

typedef struct {
  SQueryNodeEpId epId;
L
Liu Jicong 已提交
1129
  SArray*        taskStatus;  // SArray<STaskStatus>
D
dapan1121 已提交
1130 1131
} SSchedulerHbRsp;

L
Liu Jicong 已提交
1132 1133 1134
int32_t tSerializeSSchedulerHbRsp(void* buf, int32_t bufLen, SSchedulerHbRsp* pRsp);
int32_t tDeserializeSSchedulerHbRsp(void* buf, int32_t bufLen, SSchedulerHbRsp* pRsp);
void    tFreeSSchedulerHbRsp(SSchedulerHbRsp* pRsp);
D
dapan1121 已提交
1135

S
Shengliang Guan 已提交
1136
typedef struct {
D
dapan1121 已提交
1137
  SMsgHead header;
D
dapan1121 已提交
1138
  uint64_t sId;
H
Hongze Cheng 已提交
1139 1140
  uint64_t queryId;
  uint64_t taskId;
D
dapan1121 已提交
1141
  int64_t  refId;
S
Shengliang Guan 已提交
1142
} STaskCancelReq;
D
dapan1121 已提交
1143

S
Shengliang Guan 已提交
1144
typedef struct {
D
dapan1121 已提交
1145 1146 1147
  int32_t code;
} STaskCancelRsp;

S
Shengliang Guan 已提交
1148
typedef struct {
D
dapan1121 已提交
1149
  SMsgHead header;
D
dapan1121 已提交
1150
  uint64_t sId;
H
Hongze Cheng 已提交
1151 1152
  uint64_t queryId;
  uint64_t taskId;
D
dapan1121 已提交
1153
  int64_t  refId;
S
Shengliang Guan 已提交
1154
} STaskDropReq;
D
dapan1121 已提交
1155

S
Shengliang Guan 已提交
1156
typedef struct {
D
dapan1121 已提交
1157 1158 1159
  int32_t code;
} STaskDropRsp;

L
Liu Jicong 已提交
1160
typedef struct {
L
Liu Jicong 已提交
1161
  char   name[TSDB_TOPIC_FNAME_LEN];
L
Liu Jicong 已提交
1162
  char   outputSTbName[TSDB_TABLE_FNAME_LEN];
L
Liu Jicong 已提交
1163 1164
  int8_t igExists;
  char*  sql;
L
Liu Jicong 已提交
1165
  char*  ast;
L
Liu Jicong 已提交
1166
} SCMCreateStreamReq;
L
Liu Jicong 已提交
1167

L
Liu Jicong 已提交
1168 1169 1170 1171 1172 1173 1174 1175
typedef struct {
  int64_t streamId;
} SCMCreateStreamRsp;

int32_t tSerializeSCMCreateStreamReq(void* buf, int32_t bufLen, const SCMCreateStreamReq* pReq);
int32_t tDeserializeSCMCreateStreamReq(void* buf, int32_t bufLen, SCMCreateStreamReq* pReq);
void    tFreeSCMCreateStreamReq(SCMCreateStreamReq* pReq);

L
Liu Jicong 已提交
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
typedef struct {
  char    name[TSDB_TOPIC_FNAME_LEN];
  int64_t streamId;
  char*   sql;
  char*   executorMsg;
} SMVCreateStreamReq, SMSCreateStreamReq;

typedef struct {
  int64_t streamId;
} SMVCreateStreamRsp, SMSCreateStreamRsp;

L
Liu Jicong 已提交
1187 1188 1189 1190
typedef struct {
  char   name[TSDB_TOPIC_FNAME_LEN];
  int8_t igExists;
  char*  sql;
1191 1192
  char*  ast;
  char   subscribeDbName[TSDB_DB_NAME_LEN];
L
Liu Jicong 已提交
1193 1194 1195 1196 1197
} SCMCreateTopicReq;

int32_t tSerializeSCMCreateTopicReq(void* buf, int32_t bufLen, const SCMCreateTopicReq* pReq);
int32_t tDeserializeSCMCreateTopicReq(void* buf, int32_t bufLen, SCMCreateTopicReq* pReq);
void    tFreeSCMCreateTopicReq(SCMCreateTopicReq* pReq);
L
Liu Jicong 已提交
1198 1199 1200

typedef struct {
  int64_t topicId;
L
Liu Jicong 已提交
1201
} SCMCreateTopicRsp;
L
Liu Jicong 已提交
1202

L
Liu Jicong 已提交
1203 1204
int32_t tSerializeSCMCreateTopicRsp(void* buf, int32_t bufLen, const SCMCreateTopicRsp* pRsp);
int32_t tDeserializeSCMCreateTopicRsp(void* buf, int32_t bufLen, SCMCreateTopicRsp* pRsp);
L
Liu Jicong 已提交
1205 1206

typedef struct {
L
Liu Jicong 已提交
1207
  int32_t topicNum;
L
Liu Jicong 已提交
1208
  int64_t consumerId;
L
Liu Jicong 已提交
1209
  char*   consumerGroup;
L
Liu Jicong 已提交
1210
  SArray* topicNames;  // SArray<char*>
L
Liu Jicong 已提交
1211 1212
} SCMSubscribeReq;

S
Shengliang Guan 已提交
1213 1214
static FORCE_INLINE int32_t tSerializeSCMSubscribeReq(void** buf, const SCMSubscribeReq* pReq) {
  int32_t tlen = 0;
L
Liu Jicong 已提交
1215
  tlen += taosEncodeFixedI32(buf, pReq->topicNum);
L
Liu Jicong 已提交
1216
  tlen += taosEncodeFixedI64(buf, pReq->consumerId);
L
Liu Jicong 已提交
1217
  tlen += taosEncodeString(buf, pReq->consumerGroup);
L
Liu Jicong 已提交
1218

S
Shengliang Guan 已提交
1219
  for (int32_t i = 0; i < pReq->topicNum; i++) {
L
Liu Jicong 已提交
1220
    tlen += taosEncodeString(buf, (char*)taosArrayGetP(pReq->topicNames, i));
L
Liu Jicong 已提交
1221
  }
L
Liu Jicong 已提交
1222 1223 1224 1225
  return tlen;
}

static FORCE_INLINE void* tDeserializeSCMSubscribeReq(void* buf, SCMSubscribeReq* pReq) {
L
Liu Jicong 已提交
1226
  buf = taosDecodeFixedI32(buf, &pReq->topicNum);
L
Liu Jicong 已提交
1227
  buf = taosDecodeFixedI64(buf, &pReq->consumerId);
L
Liu Jicong 已提交
1228
  buf = taosDecodeString(buf, &pReq->consumerGroup);
L
Liu Jicong 已提交
1229
  pReq->topicNames = taosArrayInit(pReq->topicNum, sizeof(void*));
S
Shengliang Guan 已提交
1230
  for (int32_t i = 0; i < pReq->topicNum; i++) {
L
Liu Jicong 已提交
1231
    char* name;
L
Liu Jicong 已提交
1232 1233
    buf = taosDecodeString(buf, &name);
    taosArrayPush(pReq->topicNames, &name);
L
Liu Jicong 已提交
1234
  }
L
Liu Jicong 已提交
1235 1236 1237
  return buf;
}

L
Liu Jicong 已提交
1238
typedef struct SMqSubTopic {
L
Liu Jicong 已提交
1239
  int32_t vgId;
L
Liu Jicong 已提交
1240 1241 1242 1243 1244
  int64_t topicId;
  SEpSet  epSet;
} SMqSubTopic;

typedef struct {
L
Liu Jicong 已提交
1245
  int32_t     topicNum;
L
Liu Jicong 已提交
1246
  SMqSubTopic topics[];
L
Liu Jicong 已提交
1247 1248
} SCMSubscribeRsp;

S
Shengliang Guan 已提交
1249 1250
static FORCE_INLINE int32_t tSerializeSCMSubscribeRsp(void** buf, const SCMSubscribeRsp* pRsp) {
  int32_t tlen = 0;
L
Liu Jicong 已提交
1251
  tlen += taosEncodeFixedI32(buf, pRsp->topicNum);
S
Shengliang Guan 已提交
1252
  for (int32_t i = 0; i < pRsp->topicNum; i++) {
L
Liu Jicong 已提交
1253 1254 1255 1256
    tlen += taosEncodeFixedI32(buf, pRsp->topics[i].vgId);
    tlen += taosEncodeFixedI64(buf, pRsp->topics[i].topicId);
    tlen += taosEncodeSEpSet(buf, &pRsp->topics[i].epSet);
  }
L
Liu Jicong 已提交
1257 1258 1259 1260
  return tlen;
}

static FORCE_INLINE void* tDeserializeSCMSubscribeRsp(void* buf, SCMSubscribeRsp* pRsp) {
L
Liu Jicong 已提交
1261
  buf = taosDecodeFixedI32(buf, &pRsp->topicNum);
S
Shengliang Guan 已提交
1262
  for (int32_t i = 0; i < pRsp->topicNum; i++) {
L
Liu Jicong 已提交
1263 1264 1265 1266
    buf = taosDecodeFixedI32(buf, &pRsp->topics[i].vgId);
    buf = taosDecodeFixedI64(buf, &pRsp->topics[i].topicId);
    buf = taosDecodeSEpSet(buf, &pRsp->topics[i].epSet);
  }
L
Liu Jicong 已提交
1267 1268 1269 1270 1271 1272 1273 1274
  return buf;
}

typedef struct {
  int64_t topicId;
  int64_t consumerId;
  int64_t consumerGroupId;
  int64_t offset;
L
Liu Jicong 已提交
1275 1276 1277
  char*   sql;
  char*   logicalPlan;
  char*   physicalPlan;
L
Liu Jicong 已提交
1278 1279
} SMVSubscribeReq;

S
Shengliang Guan 已提交
1280 1281
static FORCE_INLINE int32_t tSerializeSMVSubscribeReq(void** buf, SMVSubscribeReq* pReq) {
  int32_t tlen = 0;
L
Liu Jicong 已提交
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
  tlen += taosEncodeFixedI64(buf, pReq->topicId);
  tlen += taosEncodeFixedI64(buf, pReq->consumerId);
  tlen += taosEncodeFixedI64(buf, pReq->consumerGroupId);
  tlen += taosEncodeFixedI64(buf, pReq->offset);
  tlen += taosEncodeString(buf, pReq->sql);
  tlen += taosEncodeString(buf, pReq->logicalPlan);
  tlen += taosEncodeString(buf, pReq->physicalPlan);
  return tlen;
}

static FORCE_INLINE void* tDeserializeSMVSubscribeReq(void* buf, SMVSubscribeReq* pReq) {
  buf = taosDecodeFixedI64(buf, &pReq->topicId);
  buf = taosDecodeFixedI64(buf, &pReq->consumerId);
  buf = taosDecodeFixedI64(buf, &pReq->consumerGroupId);
  buf = taosDecodeFixedI64(buf, &pReq->offset);
  buf = taosDecodeString(buf, &pReq->sql);
  buf = taosDecodeString(buf, &pReq->logicalPlan);
  buf = taosDecodeString(buf, &pReq->physicalPlan);
  return buf;
}

L
Liu Jicong 已提交
1303
typedef struct {
1304 1305 1306 1307
  const char* key;
  SArray*     lostConsumers;     // SArray<int64_t>
  SArray*     removedConsumers;  // SArray<int64_t>
  SArray*     newConsumers;      // SArray<int64_t>
L
Liu Jicong 已提交
1308 1309 1310
} SMqRebSubscribe;

static FORCE_INLINE SMqRebSubscribe* tNewSMqRebSubscribe(const char* key) {
wafwerar's avatar
wafwerar 已提交
1311
  SMqRebSubscribe* pRebSub = (SMqRebSubscribe*)taosMemoryCalloc(1, sizeof(SMqRebSubscribe));
L
Liu Jicong 已提交
1312 1313 1314
  if (pRebSub == NULL) {
    goto _err;
  }
L
Liu Jicong 已提交
1315
  pRebSub->key = strdup(key);
L
Liu Jicong 已提交
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
  pRebSub->lostConsumers = taosArrayInit(0, sizeof(int64_t));
  if (pRebSub->lostConsumers == NULL) {
    goto _err;
  }
  pRebSub->removedConsumers = taosArrayInit(0, sizeof(int64_t));
  if (pRebSub->removedConsumers == NULL) {
    goto _err;
  }
  pRebSub->newConsumers = taosArrayInit(0, sizeof(int64_t));
  if (pRebSub->newConsumers == NULL) {
    goto _err;
  }
  return pRebSub;
_err:
  taosArrayDestroy(pRebSub->lostConsumers);
  taosArrayDestroy(pRebSub->removedConsumers);
  taosArrayDestroy(pRebSub->newConsumers);
wafwerar's avatar
wafwerar 已提交
1333
  taosMemoryFreeClear(pRebSub);
L
Liu Jicong 已提交
1334 1335 1336
  return NULL;
}

L
Liu Jicong 已提交
1337
// this message is sent from mnode to mnode(read thread to write thread), so there is no need for serialization or
1338
// deserialization
L
Liu Jicong 已提交
1339
typedef struct {
1340
  SHashObj* rebSubHash;  // SHashObj<key, SMqRebSubscribe>
L
Liu Jicong 已提交
1341 1342
} SMqDoRebalanceMsg;

L
Liu Jicong 已提交
1343
typedef struct {
L
Liu Jicong 已提交
1344
  int64_t status;
L
Liu Jicong 已提交
1345 1346
} SMVSubscribeRsp;

L
Liu Jicong 已提交
1347 1348 1349
typedef struct {
  char   name[TSDB_TABLE_FNAME_LEN];
  int8_t igNotExists;
S
Shengliang Guan 已提交
1350 1351
} SMDropTopicReq;

S
Shengliang Guan 已提交
1352
int32_t tSerializeSMDropTopicReq(void* buf, int32_t bufLen, SMDropTopicReq* pReq);
S
Shengliang Guan 已提交
1353
int32_t tDeserializeSMDropTopicReq(void* buf, int32_t bufLen, SMDropTopicReq* pReq);
L
Liu Jicong 已提交
1354 1355 1356 1357 1358

typedef struct {
  char    name[TSDB_TABLE_FNAME_LEN];
  int8_t  alterType;
  SSchema schema;
S
Shengliang Guan 已提交
1359
} SAlterTopicReq;
L
Liu Jicong 已提交
1360 1361 1362 1363

typedef struct {
  SMsgHead head;
  char     name[TSDB_TABLE_FNAME_LEN];
L
Liu Jicong 已提交
1364
  int64_t  tuid;
L
Liu Jicong 已提交
1365 1366 1367 1368 1369
  int32_t  sverson;
  int32_t  execLen;
  char*    executor;
  int32_t  sqlLen;
  char*    sql;
S
Shengliang Guan 已提交
1370
} SDCreateTopicReq;
L
Liu Jicong 已提交
1371 1372 1373 1374

typedef struct {
  SMsgHead head;
  char     name[TSDB_TABLE_FNAME_LEN];
L
Liu Jicong 已提交
1375
  int64_t  tuid;
S
Shengliang Guan 已提交
1376
} SDDropTopicReq;
S
Shengliang Guan 已提交
1377

C
Cary Xu 已提交
1378 1379 1380 1381 1382 1383 1384 1385
typedef struct {
  float    xFilesFactor;
  int8_t   delayUnit;
  int8_t   nFuncIds;
  int32_t* pFuncIds;
  int64_t  delay;
} SRSmaParam;

H
more  
Hongze Cheng 已提交
1386
typedef struct SVCreateTbReq {
L
Liu Jicong 已提交
1387
  int64_t  ver;  // use a general definition
D
dapan1121 已提交
1388
  char*    dbFName;
H
more  
Hongze Cheng 已提交
1389 1390 1391 1392
  char*    name;
  uint32_t ttl;
  uint32_t keep;
  union {
C
Cary Xu 已提交
1393
    uint8_t info;
H
more  
Hongze Cheng 已提交
1394
    struct {
C
Cary Xu 已提交
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
      uint8_t rollup : 1;  // 1 means rollup sma
      uint8_t type : 7;
    };
  };
  union {
    struct {
      tb_uid_t    suid;
      uint32_t    nCols;
      SSchema*    pSchema;
      uint32_t    nTagCols;
      SSchema*    pTagSchema;
      col_id_t    nBSmaCols;
      col_id_t*   pBSmaCols;
      SRSmaParam* pRSmaParam;
H
more  
Hongze Cheng 已提交
1409 1410 1411 1412 1413 1414
    } stbCfg;
    struct {
      tb_uid_t suid;
      SKVRow   pTag;
    } ctbCfg;
    struct {
C
Cary Xu 已提交
1415 1416 1417 1418 1419
      uint32_t    nCols;
      SSchema*    pSchema;
      col_id_t    nBSmaCols;
      col_id_t*   pBSmaCols;
      SRSmaParam* pRSmaParam;
H
more  
Hongze Cheng 已提交
1420 1421
    } ntbCfg;
  };
S
Shengliang Guan 已提交
1422
} SVCreateTbReq, SVUpdateTbReq;
H
more  
Hongze Cheng 已提交
1423

H
more  
Hongze Cheng 已提交
1424
typedef struct {
D
dapan1121 已提交
1425
  int32_t code;
S
Shengliang Guan 已提交
1426
} SVCreateTbRsp, SVUpdateTbRsp;
H
more  
Hongze Cheng 已提交
1427

S
Shengliang Guan 已提交
1428 1429
int32_t tSerializeSVCreateTbReq(void** buf, SVCreateTbReq* pReq);
void*   tDeserializeSVCreateTbReq(void* buf, SVCreateTbReq* pReq);
H
more  
Hongze Cheng 已提交
1430

H
more  
Hongze Cheng 已提交
1431
typedef struct {
L
Liu Jicong 已提交
1432 1433
  int64_t ver;  // use a general definition
  SArray* pArray;
H
more  
Hongze Cheng 已提交
1434
} SVCreateTbBatchReq;
H
more  
Hongze Cheng 已提交
1435

D
dapan 已提交
1436 1437
int32_t tSerializeSVCreateTbBatchReq(void** buf, SVCreateTbBatchReq* pReq);
void*   tDeserializeSVCreateTbBatchReq(void* buf, SVCreateTbBatchReq* pReq);
D
dapan1121 已提交
1438

S
Shengliang Guan 已提交
1439
typedef struct {
L
Liu Jicong 已提交
1440
  SArray* rspList;  // SArray<SVCreateTbRsp>
S
Shengliang Guan 已提交
1441
} SVCreateTbBatchRsp;
H
more  
Hongze Cheng 已提交
1442

L
Liu Jicong 已提交
1443 1444
int32_t tSerializeSVCreateTbBatchRsp(void* buf, int32_t bufLen, SVCreateTbBatchRsp* pRsp);
int32_t tDeserializeSVCreateTbBatchRsp(void* buf, int32_t bufLen, SVCreateTbBatchRsp* pRsp);
S
Shengliang Guan 已提交
1445 1446

typedef struct {
L
Liu Jicong 已提交
1447
  int64_t  ver;
S
Shengliang Guan 已提交
1448 1449 1450
  char*    name;
  uint8_t  type;
  tb_uid_t suid;
S
Shengliang Guan 已提交
1451 1452 1453
} SVDropTbReq;

typedef struct {
L
Liu Jicong 已提交
1454
  int tmp;  // TODO: to avoid compile error
S
Shengliang Guan 已提交
1455 1456
} SVDropTbRsp;

S
Shengliang Guan 已提交
1457 1458 1459
int32_t tSerializeSVDropTbReq(void** buf, SVDropTbReq* pReq);
void*   tDeserializeSVDropTbReq(void* buf, SVDropTbReq* pReq);

S
Shengliang Guan 已提交
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
typedef struct {
  SMsgHead head;
  int64_t  uid;
  int32_t  tid;
  int16_t  tversion;
  int16_t  colId;
  int8_t   type;
  int16_t  bytes;
  int32_t  tagValLen;
  int16_t  numOfTags;
  int32_t  schemaLen;
  char     data[];
} SUpdateTagValReq;

typedef struct {
  SMsgHead head;
} SUpdateTagValRsp;

typedef struct {
H
Hongze Cheng 已提交
1479 1480 1481
  SMsgHead head;
} SVShowTablesReq;

S
Shengliang Guan 已提交
1482
typedef struct {
H
Hongze Cheng 已提交
1483
  SMsgHead head;
H
Haojun Liao 已提交
1484
  int32_t  id;
H
Hongze Cheng 已提交
1485 1486
} SVShowTablesFetchReq;

S
Shengliang Guan 已提交
1487
typedef struct {
H
Hongze Cheng 已提交
1488 1489 1490 1491 1492 1493 1494 1495 1496
  int64_t useconds;
  int8_t  completed;  // all results are returned to client
  int8_t  precision;
  int8_t  compressed;
  int32_t compLen;
  int32_t numOfRows;
  char    data[];
} SVShowTablesFetchRsp;

L
Liu Jicong 已提交
1497 1498 1499
typedef struct SMqCMGetSubEpReq {
  int64_t consumerId;
  int32_t epoch;
L
Liu Jicong 已提交
1500
  char    cgroup[TSDB_CGROUP_LEN];
L
Liu Jicong 已提交
1501 1502
} SMqCMGetSubEpReq;

L
Liu Jicong 已提交
1503 1504 1505 1506 1507 1508 1509 1510
static FORCE_INLINE int32_t tEncodeSMsgHead(void** buf, const SMsgHead* pMsg) {
  int32_t tlen = 0;
  tlen += taosEncodeFixedI32(buf, pMsg->contLen);
  tlen += taosEncodeFixedI32(buf, pMsg->vgId);
  return tlen;
}

typedef struct SMqHbRsp {
1511
  int8_t status;  // idle or not
L
Liu Jicong 已提交
1512
  int8_t vnodeChanged;
1513
  int8_t epChanged;  // should use new epset
L
Liu Jicong 已提交
1514 1515 1516 1517
  int8_t reserved;
  SEpSet epSet;
} SMqHbRsp;

S
Shengliang Guan 已提交
1518 1519
static FORCE_INLINE int32_t taosEncodeSMqHbRsp(void** buf, const SMqHbRsp* pRsp) {
  int32_t tlen = 0;
L
Liu Jicong 已提交
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
  tlen += taosEncodeFixedI8(buf, pRsp->status);
  tlen += taosEncodeFixedI8(buf, pRsp->vnodeChanged);
  tlen += taosEncodeFixedI8(buf, pRsp->epChanged);
  tlen += taosEncodeSEpSet(buf, &pRsp->epSet);
  return tlen;
}

static FORCE_INLINE void* taosDecodeSMqHbRsp(void* buf, SMqHbRsp* pRsp) {
  buf = taosDecodeFixedI8(buf, &pRsp->status);
  buf = taosDecodeFixedI8(buf, &pRsp->vnodeChanged);
  buf = taosDecodeFixedI8(buf, &pRsp->epChanged);
  buf = taosDecodeSEpSet(buf, &pRsp->epSet);
  return buf;
}

typedef struct SMqHbOneTopicBatchRsp {
1536
  char    topicName[TSDB_TOPIC_FNAME_LEN];
L
Liu Jicong 已提交
1537 1538 1539
  SArray* rsps;  // SArray<SMqHbRsp>
} SMqHbOneTopicBatchRsp;

S
Shengliang Guan 已提交
1540 1541
static FORCE_INLINE int32_t taosEncodeSMqHbOneTopicBatchRsp(void** buf, const SMqHbOneTopicBatchRsp* pBatchRsp) {
  int32_t tlen = 0;
L
Liu Jicong 已提交
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565
  tlen += taosEncodeString(buf, pBatchRsp->topicName);
  int32_t sz = taosArrayGetSize(pBatchRsp->rsps);
  tlen += taosEncodeFixedI32(buf, sz);
  for (int32_t i = 0; i < sz; i++) {
    SMqHbRsp* pRsp = (SMqHbRsp*)taosArrayGet(pBatchRsp->rsps, i);
    tlen += taosEncodeSMqHbRsp(buf, pRsp);
  }
  return tlen;
}

static FORCE_INLINE void* taosDecodeSMqHbOneTopicBatchRsp(void* buf, SMqHbOneTopicBatchRsp* pBatchRsp) {
  int32_t sz;
  buf = taosDecodeStringTo(buf, pBatchRsp->topicName);
  buf = taosDecodeFixedI32(buf, &sz);
  pBatchRsp->rsps = taosArrayInit(sz, sizeof(SMqHbRsp));
  for (int32_t i = 0; i < sz; i++) {
    SMqHbRsp rsp;
    buf = taosDecodeSMqHbRsp(buf, &rsp);
    buf = taosArrayPush(pBatchRsp->rsps, &rsp);
  }
  return buf;
}

typedef struct SMqHbBatchRsp {
1566 1567
  int64_t consumerId;
  SArray* batchRsps;  // SArray<SMqHbOneTopicBatchRsp>
L
Liu Jicong 已提交
1568 1569
} SMqHbBatchRsp;

S
Shengliang Guan 已提交
1570 1571
static FORCE_INLINE int32_t taosEncodeSMqHbBatchRsp(void** buf, const SMqHbBatchRsp* pBatchRsp) {
  int32_t tlen = 0;
L
Liu Jicong 已提交
1572 1573 1574 1575
  tlen += taosEncodeFixedI64(buf, pBatchRsp->consumerId);
  int32_t sz;
  tlen += taosEncodeFixedI32(buf, sz);
  for (int32_t i = 0; i < sz; i++) {
1576
    SMqHbOneTopicBatchRsp* pRsp = (SMqHbOneTopicBatchRsp*)taosArrayGet(pBatchRsp->batchRsps, i);
L
Liu Jicong 已提交
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
    tlen += taosEncodeSMqHbOneTopicBatchRsp(buf, pRsp);
  }
  return tlen;
}

static FORCE_INLINE void* taosDecodeSMqHbBatchRsp(void* buf, SMqHbBatchRsp* pBatchRsp) {
  buf = taosDecodeFixedI64(buf, &pBatchRsp->consumerId);
  int32_t sz;
  buf = taosDecodeFixedI32(buf, &sz);
  pBatchRsp->batchRsps = taosArrayInit(sz, sizeof(SMqHbOneTopicBatchRsp));
  for (int32_t i = 0; i < sz; i++) {
1588
    SMqHbOneTopicBatchRsp rsp;
L
Liu Jicong 已提交
1589 1590 1591 1592 1593 1594 1595
    buf = taosDecodeSMqHbOneTopicBatchRsp(buf, &rsp);
    buf = taosArrayPush(pBatchRsp->batchRsps, &rsp);
  }
  return buf;
}

typedef struct {
D
dapan1121 已提交
1596
  int32_t key;
L
Liu Jicong 已提交
1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618
  int32_t valueLen;
  void*   value;
} SKv;

typedef struct {
  int32_t connId;
  int32_t hbType;
} SClientHbKey;

typedef struct {
  SClientHbKey connKey;
  SHashObj*    info;  // hash<Skv.key, Skv>
} SClientHbReq;

typedef struct {
  int64_t reqId;
  SArray* reqs;  // SArray<SClientHbReq>
} SClientHbBatchReq;

typedef struct {
  SClientHbKey connKey;
  int32_t      status;
D
dapan1121 已提交
1619
  SArray*      info;  // Array<Skv>
L
Liu Jicong 已提交
1620 1621 1622 1623 1624 1625 1626 1627
} SClientHbRsp;

typedef struct {
  int64_t reqId;
  int64_t rspId;
  SArray* rsps;  // SArray<SClientHbRsp>
} SClientHbBatchRsp;

1628
static FORCE_INLINE uint32_t hbKeyHashFunc(const char* key, uint32_t keyLen) { return taosIntHash_64(key, keyLen); }
L
Liu Jicong 已提交
1629

1630 1631
static FORCE_INLINE void tFreeReqKvHash(SHashObj* info) {
  void* pIter = taosHashIterate(info, NULL);
D
dapan1121 已提交
1632 1633
  while (pIter != NULL) {
    SKv* kv = (SKv*)pIter;
wafwerar's avatar
wafwerar 已提交
1634
    taosMemoryFreeClear(kv->value);
D
dapan1121 已提交
1635 1636 1637 1638
    pIter = taosHashIterate(info, pIter);
  }
}

1639
static FORCE_INLINE void tFreeClientHbReq(void* pReq) {
L
Liu Jicong 已提交
1640
  SClientHbReq* req = (SClientHbReq*)pReq;
D
dapan1121 已提交
1641 1642 1643 1644
  if (req->info) {
    tFreeReqKvHash(req->info);
    taosHashCleanup(req->info);
  }
L
Liu Jicong 已提交
1645 1646
}

S
Shengliang Guan 已提交
1647 1648
int32_t tSerializeSClientHbBatchReq(void* buf, int32_t bufLen, const SClientHbBatchReq* pReq);
int32_t tDeserializeSClientHbBatchReq(void* buf, int32_t bufLen, SClientHbBatchReq* pReq);
L
Liu Jicong 已提交
1649 1650

static FORCE_INLINE void tFreeClientHbBatchReq(void* pReq, bool deep) {
1651
  SClientHbBatchReq* req = (SClientHbBatchReq*)pReq;
L
Liu Jicong 已提交
1652 1653 1654 1655 1656
  if (deep) {
    taosArrayDestroyEx(req->reqs, tFreeClientHbReq);
  } else {
    taosArrayDestroy(req->reqs);
  }
wafwerar's avatar
wafwerar 已提交
1657
  taosMemoryFree(pReq);
L
Liu Jicong 已提交
1658 1659
}

1660 1661
static FORCE_INLINE void tFreeClientKv(void* pKv) {
  SKv* kv = (SKv*)pKv;
D
dapan1121 已提交
1662
  if (kv) {
wafwerar's avatar
wafwerar 已提交
1663
    taosMemoryFreeClear(kv->value);
D
dapan1121 已提交
1664 1665 1666
  }
}

1667
static FORCE_INLINE void tFreeClientHbRsp(void* pRsp) {
D
dapan1121 已提交
1668 1669 1670 1671 1672
  SClientHbRsp* rsp = (SClientHbRsp*)pRsp;
  if (rsp->info) taosArrayDestroyEx(rsp->info, tFreeClientKv);
}

static FORCE_INLINE void tFreeClientHbBatchRsp(void* pRsp) {
1673
  SClientHbBatchRsp* rsp = (SClientHbBatchRsp*)pRsp;
D
dapan1121 已提交
1674 1675 1676
  taosArrayDestroyEx(rsp->rsps, tFreeClientHbRsp);
}

S
Shengliang Guan 已提交
1677 1678
int32_t tSerializeSClientHbBatchRsp(void* buf, int32_t bufLen, const SClientHbBatchRsp* pBatchRsp);
int32_t tDeserializeSClientHbBatchRsp(void* buf, int32_t bufLen, SClientHbBatchRsp* pBatchRsp);
L
Liu Jicong 已提交
1679

S
Shengliang Guan 已提交
1680 1681 1682
static FORCE_INLINE int32_t tEncodeSKv(SCoder* pEncoder, const SKv* pKv) {
  if (tEncodeI32(pEncoder, pKv->key) < 0) return -1;
  if (tEncodeI32(pEncoder, pKv->valueLen) < 0) return -1;
S
Shengliang Guan 已提交
1683
  if (tEncodeBinary(pEncoder, (const char*)pKv->value, pKv->valueLen) < 0) return -1;
S
Shengliang Guan 已提交
1684
  return 0;
L
Liu Jicong 已提交
1685 1686
}

S
Shengliang Guan 已提交
1687 1688 1689
static FORCE_INLINE int32_t tDecodeSKv(SCoder* pDecoder, SKv* pKv) {
  if (tDecodeI32(pDecoder, &pKv->key) < 0) return -1;
  if (tDecodeI32(pDecoder, &pKv->valueLen) < 0) return -1;
wafwerar's avatar
wafwerar 已提交
1690
  pKv->value = taosMemoryMalloc(pKv->valueLen + 1);
S
Shengliang Guan 已提交
1691 1692 1693
  if (pKv->value == NULL) return -1;
  if (tDecodeCStrTo(pDecoder, (char*)pKv->value) < 0) return -1;
  return 0;
L
Liu Jicong 已提交
1694 1695
}

S
Shengliang Guan 已提交
1696 1697 1698 1699
static FORCE_INLINE int32_t tEncodeSClientHbKey(SCoder* pEncoder, const SClientHbKey* pKey) {
  if (tEncodeI32(pEncoder, pKey->connId) < 0) return -1;
  if (tEncodeI32(pEncoder, pKey->hbType) < 0) return -1;
  return 0;
L
Liu Jicong 已提交
1700 1701
}

S
Shengliang Guan 已提交
1702 1703 1704 1705
static FORCE_INLINE int32_t tDecodeSClientHbKey(SCoder* pDecoder, SClientHbKey* pKey) {
  if (tDecodeI32(pDecoder, &pKey->connId) < 0) return -1;
  if (tDecodeI32(pDecoder, &pKey->hbType) < 0) return -1;
  return 0;
L
Liu Jicong 已提交
1706 1707 1708 1709 1710 1711
}

typedef struct SMqHbVgInfo {
  int32_t vgId;
} SMqHbVgInfo;

S
Shengliang Guan 已提交
1712 1713
static FORCE_INLINE int32_t taosEncodeSMqVgInfo(void** buf, const SMqHbVgInfo* pVgInfo) {
  int32_t tlen = 0;
L
Liu Jicong 已提交
1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729
  tlen += taosEncodeFixedI32(buf, pVgInfo->vgId);
  return tlen;
}

static FORCE_INLINE void* taosDecodeSMqVgInfo(void* buf, SMqHbVgInfo* pVgInfo) {
  buf = taosDecodeFixedI32(buf, &pVgInfo->vgId);
  return buf;
}

typedef struct SMqHbTopicInfo {
  int32_t epoch;
  int64_t topicUid;
  char    name[TSDB_TOPIC_FNAME_LEN];
  SArray* pVgInfo;
} SMqHbTopicInfo;

S
Shengliang Guan 已提交
1730 1731
static FORCE_INLINE int32_t taosEncodeSMqHbTopicInfoMsg(void** buf, const SMqHbTopicInfo* pTopicInfo) {
  int32_t tlen = 0;
L
Liu Jicong 已提交
1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759
  tlen += taosEncodeFixedI32(buf, pTopicInfo->epoch);
  tlen += taosEncodeFixedI64(buf, pTopicInfo->topicUid);
  tlen += taosEncodeString(buf, pTopicInfo->name);
  int32_t sz = taosArrayGetSize(pTopicInfo->pVgInfo);
  tlen += taosEncodeFixedI32(buf, sz);
  for (int32_t i = 0; i < sz; i++) {
    SMqHbVgInfo* pVgInfo = (SMqHbVgInfo*)taosArrayGet(pTopicInfo->pVgInfo, i);
    tlen += taosEncodeSMqVgInfo(buf, pVgInfo);
  }
  return tlen;
}

static FORCE_INLINE void* taosDecodeSMqHbTopicInfoMsg(void* buf, SMqHbTopicInfo* pTopicInfo) {
  buf = taosDecodeFixedI32(buf, &pTopicInfo->epoch);
  buf = taosDecodeFixedI64(buf, &pTopicInfo->topicUid);
  buf = taosDecodeStringTo(buf, pTopicInfo->name);
  int32_t sz;
  buf = taosDecodeFixedI32(buf, &sz);
  pTopicInfo->pVgInfo = taosArrayInit(sz, sizeof(SMqHbVgInfo));
  for (int32_t i = 0; i < sz; i++) {
    SMqHbVgInfo vgInfo;
    buf = taosDecodeSMqVgInfo(buf, &vgInfo);
    taosArrayPush(pTopicInfo->pVgInfo, &vgInfo);
  }
  return buf;
}

typedef struct SMqHbMsg {
1760 1761 1762 1763
  int32_t status;  // ask hb endpoint
  int32_t epoch;
  int64_t consumerId;
  SArray* pTopics;  // SArray<SMqHbTopicInfo>
L
Liu Jicong 已提交
1764 1765
} SMqHbMsg;

S
Shengliang Guan 已提交
1766 1767
static FORCE_INLINE int32_t taosEncodeSMqMsg(void** buf, const SMqHbMsg* pMsg) {
  int32_t tlen = 0;
L
Liu Jicong 已提交
1768 1769 1770 1771 1772
  tlen += taosEncodeFixedI32(buf, pMsg->status);
  tlen += taosEncodeFixedI32(buf, pMsg->epoch);
  tlen += taosEncodeFixedI64(buf, pMsg->consumerId);
  int32_t sz = taosArrayGetSize(pMsg->pTopics);
  tlen += taosEncodeFixedI32(buf, sz);
S
Shengliang Guan 已提交
1773
  for (int32_t i = 0; i < sz; i++) {
L
Liu Jicong 已提交
1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786
    SMqHbTopicInfo* topicInfo = (SMqHbTopicInfo*)taosArrayGet(pMsg->pTopics, i);
    tlen += taosEncodeSMqHbTopicInfoMsg(buf, topicInfo);
  }
  return tlen;
}

static FORCE_INLINE void* taosDecodeSMqMsg(void* buf, SMqHbMsg* pMsg) {
  buf = taosDecodeFixedI32(buf, &pMsg->status);
  buf = taosDecodeFixedI32(buf, &pMsg->epoch);
  buf = taosDecodeFixedI64(buf, &pMsg->consumerId);
  int32_t sz;
  buf = taosDecodeFixedI32(buf, &sz);
  pMsg->pTopics = taosArrayInit(sz, sizeof(SMqHbTopicInfo));
S
Shengliang Guan 已提交
1787
  for (int32_t i = 0; i < sz; i++) {
L
Liu Jicong 已提交
1788 1789 1790 1791 1792 1793 1794
    SMqHbTopicInfo topicInfo;
    buf = taosDecodeSMqHbTopicInfoMsg(buf, &topicInfo);
    taosArrayPush(pMsg->pTopics, &topicInfo);
  }
  return buf;
}

L
Liu Jicong 已提交
1795
typedef struct {
1796 1797 1798 1799
  int64_t leftForVer;
  int32_t vgId;
  int64_t consumerId;
  char    topicName[TSDB_TOPIC_FNAME_LEN];
L
Liu Jicong 已提交
1800
  char    cgroup[TSDB_CGROUP_LEN];
1801 1802 1803 1804
  char*   sql;
  char*   logicalPlan;
  char*   physicalPlan;
  char*   qmsg;
L
Liu Jicong 已提交
1805 1806 1807 1808
} SMqSetCVgReq;

static FORCE_INLINE int32_t tEncodeSMqSetCVgReq(void** buf, const SMqSetCVgReq* pReq) {
  int32_t tlen = 0;
L
Liu Jicong 已提交
1809
  tlen += taosEncodeFixedI64(buf, pReq->leftForVer);
L
Liu Jicong 已提交
1810
  tlen += taosEncodeFixedI32(buf, pReq->vgId);
L
Liu Jicong 已提交
1811
  tlen += taosEncodeFixedI64(buf, pReq->consumerId);
L
Liu Jicong 已提交
1812
  tlen += taosEncodeString(buf, pReq->topicName);
L
Liu Jicong 已提交
1813
  tlen += taosEncodeString(buf, pReq->cgroup);
L
Liu Jicong 已提交
1814 1815 1816
  tlen += taosEncodeString(buf, pReq->sql);
  tlen += taosEncodeString(buf, pReq->logicalPlan);
  tlen += taosEncodeString(buf, pReq->physicalPlan);
L
Liu Jicong 已提交
1817
  tlen += taosEncodeString(buf, pReq->qmsg);
L
Liu Jicong 已提交
1818 1819 1820 1821
  return tlen;
}

static FORCE_INLINE void* tDecodeSMqSetCVgReq(void* buf, SMqSetCVgReq* pReq) {
L
Liu Jicong 已提交
1822
  buf = taosDecodeFixedI64(buf, &pReq->leftForVer);
L
Liu Jicong 已提交
1823
  buf = taosDecodeFixedI32(buf, &pReq->vgId);
L
Liu Jicong 已提交
1824
  buf = taosDecodeFixedI64(buf, &pReq->consumerId);
L
Liu Jicong 已提交
1825
  buf = taosDecodeStringTo(buf, pReq->topicName);
L
Liu Jicong 已提交
1826
  buf = taosDecodeStringTo(buf, pReq->cgroup);
L
Liu Jicong 已提交
1827 1828 1829
  buf = taosDecodeString(buf, &pReq->sql);
  buf = taosDecodeString(buf, &pReq->logicalPlan);
  buf = taosDecodeString(buf, &pReq->physicalPlan);
L
Liu Jicong 已提交
1830
  buf = taosDecodeString(buf, &pReq->qmsg);
L
Liu Jicong 已提交
1831 1832 1833
  return buf;
}

L
Liu Jicong 已提交
1834
typedef struct {
1835 1836 1837 1838
  int64_t leftForVer;
  int32_t vgId;
  int64_t oldConsumerId;
  int64_t newConsumerId;
L
Liu Jicong 已提交
1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858
} SMqMVRebReq;

static FORCE_INLINE int32_t tEncodeSMqMVRebReq(void** buf, const SMqMVRebReq* pReq) {
  int32_t tlen = 0;
  tlen += taosEncodeFixedI64(buf, pReq->leftForVer);
  tlen += taosEncodeFixedI32(buf, pReq->vgId);
  tlen += taosEncodeFixedI64(buf, pReq->oldConsumerId);
  tlen += taosEncodeFixedI64(buf, pReq->newConsumerId);
  return tlen;
}

static FORCE_INLINE void* tDecodeSMqMVRebReq(void* buf, SMqMVRebReq* pReq) {
  buf = taosDecodeFixedI64(buf, &pReq->leftForVer);
  buf = taosDecodeFixedI32(buf, &pReq->vgId);
  buf = taosDecodeFixedI64(buf, &pReq->oldConsumerId);
  buf = taosDecodeFixedI64(buf, &pReq->newConsumerId);
  return buf;
}

typedef struct {
L
Liu Jicong 已提交
1859 1860 1861 1862
  SMsgHead header;
  int32_t  vgId;
  int64_t  consumerId;
  char     topicName[TSDB_TOPIC_FNAME_LEN];
L
Liu Jicong 已提交
1863
  char     cgroup[TSDB_CGROUP_LEN];
L
Liu Jicong 已提交
1864 1865
} SMqSetCVgRsp;

L
Liu Jicong 已提交
1866 1867 1868 1869 1870
typedef struct {
  SMsgHead header;
  int32_t  vgId;
  int64_t  consumerId;
  char     topicName[TSDB_TOPIC_FNAME_LEN];
L
Liu Jicong 已提交
1871
  char     cgroup[TSDB_CGROUP_LEN];
L
Liu Jicong 已提交
1872 1873
} SMqMVRebRsp;

L
Liu Jicong 已提交
1874 1875 1876 1877
typedef struct {
  int32_t vgId;
  int64_t offset;
  char    topicName[TSDB_TOPIC_FNAME_LEN];
L
Liu Jicong 已提交
1878
  char    cgroup[TSDB_CGROUP_LEN];
L
Liu Jicong 已提交
1879 1880 1881 1882 1883
} SMqOffset;

typedef struct {
  int32_t    num;
  SMqOffset* offsets;
L
Liu Jicong 已提交
1884
} SMqCMCommitOffsetReq;
L
Liu Jicong 已提交
1885 1886 1887

typedef struct {
  int32_t reserved;
L
Liu Jicong 已提交
1888
} SMqCMCommitOffsetRsp;
L
Liu Jicong 已提交
1889 1890 1891

int32_t tEncodeSMqOffset(SCoder* encoder, const SMqOffset* pOffset);
int32_t tDecodeSMqOffset(SCoder* decoder, SMqOffset* pOffset);
L
Liu Jicong 已提交
1892 1893
int32_t tEncodeSMqCMCommitOffsetReq(SCoder* encoder, const SMqCMCommitOffsetReq* pReq);
int32_t tDecodeSMqCMCommitOffsetReq(SCoder* decoder, SMqCMCommitOffsetReq* pReq);
L
Liu Jicong 已提交
1894

L
Liu Jicong 已提交
1895 1896
typedef struct {
  uint32_t nCols;
1897
  SSchema* pSchema;
L
Liu Jicong 已提交
1898 1899
} SSchemaWrapper;

S
Shengliang Guan 已提交
1900
static FORCE_INLINE int32_t taosEncodeSSchema(void** buf, const SSchema* pSchema) {
L
Liu Jicong 已提交
1901 1902 1903
  int32_t tlen = 0;
  tlen += taosEncodeFixedI8(buf, pSchema->type);
  tlen += taosEncodeFixedI32(buf, pSchema->bytes);
1904
  tlen += taosEncodeFixedI16(buf, pSchema->colId);
L
Liu Jicong 已提交
1905 1906 1907 1908
  tlen += taosEncodeString(buf, pSchema->name);
  return tlen;
}

S
Shengliang Guan 已提交
1909
static FORCE_INLINE void* taosDecodeSSchema(void* buf, SSchema* pSchema) {
L
Liu Jicong 已提交
1910 1911
  buf = taosDecodeFixedI8(buf, &pSchema->type);
  buf = taosDecodeFixedI32(buf, &pSchema->bytes);
1912
  buf = taosDecodeFixedI16(buf, &pSchema->colId);
L
Liu Jicong 已提交
1913 1914 1915 1916
  buf = taosDecodeStringTo(buf, pSchema->name);
  return buf;
}

S
Shengliang Guan 已提交
1917 1918 1919
static FORCE_INLINE int32_t tEncodeSSchema(SCoder* pEncoder, const SSchema* pSchema) {
  if (tEncodeI8(pEncoder, pSchema->type) < 0) return -1;
  if (tEncodeI32(pEncoder, pSchema->bytes) < 0) return -1;
1920
  if (tEncodeI16(pEncoder, pSchema->colId) < 0) return -1;
S
Shengliang Guan 已提交
1921 1922 1923 1924 1925 1926 1927
  if (tEncodeCStr(pEncoder, pSchema->name) < 0) return -1;
  return 0;
}

static FORCE_INLINE int32_t tDecodeSSchema(SCoder* pDecoder, SSchema* pSchema) {
  if (tDecodeI8(pDecoder, &pSchema->type) < 0) return -1;
  if (tDecodeI32(pDecoder, &pSchema->bytes) < 0) return -1;
1928
  if (tDecodeI16(pDecoder, &pSchema->colId) < 0) return -1;
S
Shengliang Guan 已提交
1929 1930 1931 1932
  if (tDecodeCStrTo(pDecoder, pSchema->name) < 0) return -1;
  return 0;
}

L
Liu Jicong 已提交
1933
static FORCE_INLINE int32_t taosEncodeSSchemaWrapper(void** buf, const SSchemaWrapper* pSW) {
L
Liu Jicong 已提交
1934 1935
  int32_t tlen = 0;
  tlen += taosEncodeFixedU32(buf, pSW->nCols);
1936
  for (int32_t i = 0; i < pSW->nCols; i++) {
S
Shengliang Guan 已提交
1937
    tlen += taosEncodeSSchema(buf, &pSW->pSchema[i]);
L
Liu Jicong 已提交
1938 1939 1940 1941
  }
  return tlen;
}

L
Liu Jicong 已提交
1942
static FORCE_INLINE void* taosDecodeSSchemaWrapper(void* buf, SSchemaWrapper* pSW) {
L
Liu Jicong 已提交
1943
  buf = taosDecodeFixedU32(buf, &pSW->nCols);
wafwerar's avatar
wafwerar 已提交
1944
  pSW->pSchema = (SSchema*)taosMemoryCalloc(pSW->nCols, sizeof(SSchema));
L
Liu Jicong 已提交
1945 1946 1947
  if (pSW->pSchema == NULL) {
    return NULL;
  }
1948

1949
  for (int32_t i = 0; i < pSW->nCols; i++) {
S
Shengliang Guan 已提交
1950
    buf = taosDecodeSSchema(buf, &pSW->pSchema[i]);
L
Liu Jicong 已提交
1951 1952 1953
  }
  return buf;
}
S
Shengliang Guan 已提交
1954

L
Liu Jicong 已提交
1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975
static FORCE_INLINE int32_t tEncodeSSchemaWrapper(SCoder* pEncoder, const SSchemaWrapper* pSW) {
  if (tEncodeU32(pEncoder, pSW->nCols) < 0) return -1;
  for (int32_t i = 0; i < pSW->nCols; i++) {
    if (tEncodeSSchema(pEncoder, &pSW->pSchema[i]) < 0) return -1;
  }
  return pEncoder->pos;
}

static FORCE_INLINE int32_t tDecodeSSchemaWrapper(SCoder* pDecoder, SSchemaWrapper* pSW) {
  if (tDecodeU32(pDecoder, &pSW->nCols) < 0) return -1;
  void* ptr = taosMemoryRealloc(pSW->pSchema, pSW->nCols * sizeof(SSchema));
  if (ptr == NULL) {
    return -1;
  }
  pSW->pSchema = (SSchema*)ptr;
  for (int32_t i = 0; i < pSW->nCols; i++) {
    if (tDecodeSSchema(pDecoder, &pSW->pSchema[i]) < 0) return -1;
  }
  return 0;
}

S
Shengliang Guan 已提交
1976 1977 1978 1979 1980 1981 1982
typedef struct {
  char    name[TSDB_TABLE_FNAME_LEN];
  char    stb[TSDB_TABLE_FNAME_LEN];
  int8_t  igExists;
  int8_t  intervalUnit;
  int8_t  slidingUnit;
  int8_t  timezone;
S
sma  
Shengliang Guan 已提交
1983
  int32_t dstVgId;  // for stream
S
Shengliang Guan 已提交
1984 1985 1986
  int64_t interval;
  int64_t offset;
  int64_t sliding;
S
sma  
Shengliang Guan 已提交
1987 1988 1989 1990
  int32_t exprLen;        // strlen + 1
  int32_t tagsFilterLen;  // strlen + 1
  int32_t sqlLen;         // strlen + 1
  int32_t astLen;         // strlen + 1
L
Liu Jicong 已提交
1991
  char*   expr;
S
Shengliang Guan 已提交
1992
  char*   tagsFilter;
S
sma  
Shengliang Guan 已提交
1993 1994
  char*   sql;
  char*   ast;
S
Shengliang Guan 已提交
1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008
} SMCreateSmaReq;

int32_t tSerializeSMCreateSmaReq(void* buf, int32_t bufLen, SMCreateSmaReq* pReq);
int32_t tDeserializeSMCreateSmaReq(void* buf, int32_t bufLen, SMCreateSmaReq* pReq);
void    tFreeSMCreateSmaReq(SMCreateSmaReq* pReq);

typedef struct {
  char   name[TSDB_TABLE_FNAME_LEN];
  int8_t igNotExists;
} SMDropSmaReq;

int32_t tSerializeSMDropSmaReq(void* buf, int32_t bufLen, SMDropSmaReq* pReq);
int32_t tDeserializeSMDropSmaReq(void* buf, int32_t bufLen, SMDropSmaReq* pReq);

C
Cary Xu 已提交
2009
typedef struct {
L
Liu Jicong 已提交
2010 2011 2012
  int8_t   version;       // for compatibility(default 0)
  int8_t   intervalUnit;  // MACRO: TIME_UNIT_XXX
  int8_t   slidingUnit;   // MACRO: TIME_UNIT_XXX
L
Liu Jicong 已提交
2013
  int8_t   timezoneInt;   // sma data expired if timezone changes.
C
Cary Xu 已提交
2014
  char     indexName[TSDB_INDEX_NAME_LEN];
L
Liu Jicong 已提交
2015
  char     timezone[TD_TIMEZONE_LEN];
X
Xiaoyu Wang 已提交
2016 2017
  int32_t  exprLen;
  int32_t  tagsFilterLen;
C
Cary Xu 已提交
2018 2019 2020
  int64_t  indexUid;
  tb_uid_t tableUid;  // super/child/common table uid
  int64_t  interval;
L
Liu Jicong 已提交
2021
  int64_t  offset;  // use unit by precision of DB
C
Cary Xu 已提交
2022 2023 2024
  int64_t  sliding;
  char*    expr;  // sma expression
  char*    tagsFilter;
C
Cary Xu 已提交
2025
} STSma;  // Time-range-wise SMA
C
Cary Xu 已提交
2026

C
Cary Xu 已提交
2027
typedef struct {
C
Cary Xu 已提交
2028 2029 2030
  int64_t ver;  // use a general definition
  STSma   tSma;
} SVCreateTSmaReq;
C
Cary Xu 已提交
2031 2032

typedef struct {
C
Cary Xu 已提交
2033 2034 2035
  int8_t  type;  // 0 status report, 1 update data
  int64_t indexUid;
  int64_t skey;  // start TS key of interval/sliding window
C
Cary Xu 已提交
2036 2037 2038 2039
} STSmaMsg;

typedef struct {
  int64_t ver;  // use a general definition
C
Cary Xu 已提交
2040
  int64_t indexUid;
C
Cary Xu 已提交
2041
  char    indexName[TSDB_INDEX_NAME_LEN];
C
Cary Xu 已提交
2042
} SVDropTSmaReq;
2043

C
Cary Xu 已提交
2044
typedef struct {
L
Liu Jicong 已提交
2045
  int tmp;  // TODO: to avoid compile error
C
Cary Xu 已提交
2046 2047 2048 2049 2050 2051
} SVCreateTSmaRsp, SVDropTSmaRsp;

int32_t tSerializeSVCreateTSmaReq(void** buf, SVCreateTSmaReq* pReq);
void*   tDeserializeSVCreateTSmaReq(void* buf, SVCreateTSmaReq* pReq);
int32_t tSerializeSVDropTSmaReq(void** buf, SVDropTSmaReq* pReq);
void*   tDeserializeSVDropTSmaReq(void* buf, SVDropTSmaReq* pReq);
C
Cary Xu 已提交
2052 2053

typedef struct {
C
Cary Xu 已提交
2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066
  col_id_t colId;
  uint16_t blockSize;  // sma data block size
  char     data[];
} STSmaColData;

typedef struct {
  tb_uid_t tableUid;  // super/child/normal table uid
  int32_t  dataLen;   // not including head
  char     data[];
} STSmaTbData;

typedef struct {
  int64_t indexUid;
C
Cary Xu 已提交
2067
  TSKEY   skey;  // startKey of one interval/sliding window
C
Cary Xu 已提交
2068 2069 2070 2071 2072 2073 2074 2075 2076 2077
  int64_t interval;
  int32_t dataLen;  // not including head
  int8_t  intervalUnit;
  char    data[];
} STSmaDataWrapper;  // sma data for a interval/sliding window

// interval/sliding => window

// => window->table->colId
// => 当一个window下所有的表均计算完成时,流计算告知tsdb清除window的过期标记
C
Cary Xu 已提交
2078

C
Cary Xu 已提交
2079
// RSma: Rollup SMA
C
Cary Xu 已提交
2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092
typedef struct {
  int64_t  interval;
  int32_t  retention;  // unit: day
  uint16_t days;       // unit: day
  int8_t   intervalUnit;
} SSmaParams;

typedef struct {
  STSma   tsma;
  float   xFilesFactor;
  SArray* smaParams;  // SSmaParams
} SRSma;

C
Cary Xu 已提交
2093 2094 2095 2096 2097
typedef struct {
  uint32_t number;
  STSma*   tSma;
} STSmaWrapper;

C
Cary Xu 已提交
2098
static FORCE_INLINE void tdDestroyTSma(STSma* pSma) {
C
Cary Xu 已提交
2099
  if (pSma) {
wafwerar's avatar
wafwerar 已提交
2100 2101
    taosMemoryFreeClear(pSma->expr);
    taosMemoryFreeClear(pSma->tagsFilter);
C
Cary Xu 已提交
2102 2103 2104
  }
}

C
Cary Xu 已提交
2105
static FORCE_INLINE void tdDestroyTSmaWrapper(STSmaWrapper* pSW) {
C
Cary Xu 已提交
2106 2107 2108
  if (pSW) {
    if (pSW->tSma) {
      for (uint32_t i = 0; i < pSW->number; ++i) {
C
Cary Xu 已提交
2109
        tdDestroyTSma(pSW->tSma + i);
C
Cary Xu 已提交
2110
      }
wafwerar's avatar
wafwerar 已提交
2111
      taosMemoryFreeClear(pSW->tSma);
C
Cary Xu 已提交
2112
    }
C
Cary Xu 已提交
2113 2114 2115
  }
}

C
Cary Xu 已提交
2116 2117
static FORCE_INLINE void tdFreeTSmaWrapper(STSmaWrapper* pSW) {
  tdDestroyTSmaWrapper(pSW);
wafwerar's avatar
wafwerar 已提交
2118
  taosMemoryFreeClear(pSW);
C
Cary Xu 已提交
2119 2120
}

C
Cary Xu 已提交
2121 2122 2123
static FORCE_INLINE int32_t tEncodeTSma(void** buf, const STSma* pSma) {
  int32_t tlen = 0;

C
Cary Xu 已提交
2124 2125 2126
  tlen += taosEncodeFixedI8(buf, pSma->version);
  tlen += taosEncodeFixedI8(buf, pSma->intervalUnit);
  tlen += taosEncodeFixedI8(buf, pSma->slidingUnit);
S
sma  
Shengliang Guan 已提交
2127
  tlen += taosEncodeFixedI8(buf, pSma->timezoneInt);
C
Cary Xu 已提交
2128
  tlen += taosEncodeString(buf, pSma->indexName);
X
Xiaoyu Wang 已提交
2129 2130
  tlen += taosEncodeFixedI32(buf, pSma->exprLen);
  tlen += taosEncodeFixedI32(buf, pSma->tagsFilterLen);
C
Cary Xu 已提交
2131
  tlen += taosEncodeFixedI64(buf, pSma->indexUid);
C
Cary Xu 已提交
2132
  tlen += taosEncodeFixedI64(buf, pSma->tableUid);
C
Cary Xu 已提交
2133
  tlen += taosEncodeFixedI64(buf, pSma->interval);
X
Xiaoyu Wang 已提交
2134
  tlen += taosEncodeFixedI64(buf, pSma->offset);
C
Cary Xu 已提交
2135
  tlen += taosEncodeFixedI64(buf, pSma->sliding);
L
Liu Jicong 已提交
2136

C
Cary Xu 已提交
2137 2138
  if (pSma->exprLen > 0) {
    tlen += taosEncodeString(buf, pSma->expr);
C
Cary Xu 已提交
2139 2140
  }

C
Cary Xu 已提交
2141 2142
  if (pSma->tagsFilterLen > 0) {
    tlen += taosEncodeString(buf, pSma->tagsFilter);
C
Cary Xu 已提交
2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158
  }

  return tlen;
}

static FORCE_INLINE int32_t tEncodeTSmaWrapper(void** buf, const STSmaWrapper* pSW) {
  int32_t tlen = 0;

  tlen += taosEncodeFixedU32(buf, pSW->number);
  for (uint32_t i = 0; i < pSW->number; ++i) {
    tlen += tEncodeTSma(buf, pSW->tSma + i);
  }
  return tlen;
}

static FORCE_INLINE void* tDecodeTSma(void* buf, STSma* pSma) {
C
Cary Xu 已提交
2159 2160 2161
  buf = taosDecodeFixedI8(buf, &pSma->version);
  buf = taosDecodeFixedI8(buf, &pSma->intervalUnit);
  buf = taosDecodeFixedI8(buf, &pSma->slidingUnit);
S
sma  
Shengliang Guan 已提交
2162
  buf = taosDecodeFixedI8(buf, &pSma->timezoneInt);
C
Cary Xu 已提交
2163
  buf = taosDecodeStringTo(buf, pSma->indexName);
X
Xiaoyu Wang 已提交
2164 2165
  buf = taosDecodeFixedI32(buf, &pSma->exprLen);
  buf = taosDecodeFixedI32(buf, &pSma->tagsFilterLen);
C
Cary Xu 已提交
2166
  buf = taosDecodeFixedI64(buf, &pSma->indexUid);
C
Cary Xu 已提交
2167
  buf = taosDecodeFixedI64(buf, &pSma->tableUid);
C
Cary Xu 已提交
2168
  buf = taosDecodeFixedI64(buf, &pSma->interval);
X
Xiaoyu Wang 已提交
2169
  buf = taosDecodeFixedI64(buf, &pSma->offset);
C
Cary Xu 已提交
2170 2171
  buf = taosDecodeFixedI64(buf, &pSma->sliding);

C
Cary Xu 已提交
2172
  if (pSma->exprLen > 0) {
2173
    if ((buf = taosDecodeString(buf, &pSma->expr)) == NULL) {
C
Cary Xu 已提交
2174
      tdDestroyTSma(pSma);
C
Cary Xu 已提交
2175 2176 2177
      return NULL;
    }
  } else {
C
Cary Xu 已提交
2178
    pSma->expr = NULL;
C
Cary Xu 已提交
2179 2180
  }

C
Cary Xu 已提交
2181
  if (pSma->tagsFilterLen > 0) {
2182
    if ((buf = taosDecodeString(buf, &pSma->tagsFilter)) == NULL) {
C
Cary Xu 已提交
2183
      tdDestroyTSma(pSma);
C
Cary Xu 已提交
2184 2185 2186
      return NULL;
    }
  } else {
C
Cary Xu 已提交
2187
    pSma->tagsFilter = NULL;
C
Cary Xu 已提交
2188 2189 2190 2191 2192 2193 2194 2195
  }

  return buf;
}

static FORCE_INLINE void* tDecodeTSmaWrapper(void* buf, STSmaWrapper* pSW) {
  buf = taosDecodeFixedU32(buf, &pSW->number);

wafwerar's avatar
wafwerar 已提交
2196
  pSW->tSma = (STSma*)taosMemoryCalloc(pSW->number, sizeof(STSma));
C
Cary Xu 已提交
2197 2198 2199 2200 2201 2202 2203
  if (pSW->tSma == NULL) {
    return NULL;
  }

  for (uint32_t i = 0; i < pSW->number; ++i) {
    if ((buf = tDecodeTSma(buf, pSW->tSma + i)) == NULL) {
      for (uint32_t j = i; j >= 0; --i) {
C
Cary Xu 已提交
2204
        tdDestroyTSma(pSW->tSma + j);
C
Cary Xu 已提交
2205
      }
wafwerar's avatar
wafwerar 已提交
2206
      taosMemoryFree(pSW->tSma);
C
Cary Xu 已提交
2207 2208 2209 2210 2211
      return NULL;
    }
  }
  return buf;
}
L
Liu Jicong 已提交
2212

L
Liu Jicong 已提交
2213
typedef struct {
2214 2215 2216
  int64_t uid;
  int32_t numOfRows;
  char*   colData;
L
Liu Jicong 已提交
2217 2218
} SMqTbData;

L
Liu Jicong 已提交
2219
typedef struct {
L
Liu Jicong 已提交
2220 2221 2222 2223 2224 2225 2226 2227
  char       topicName[TSDB_TOPIC_FNAME_LEN];
  int64_t    committedOffset;
  int64_t    reqOffset;
  int64_t    rspOffset;
  int32_t    skipLogNum;
  int32_t    bodyLen;
  int32_t    numOfTb;
  SMqTbData* tbData;
L
Liu Jicong 已提交
2228 2229
} SMqTopicData;

L
Liu Jicong 已提交
2230 2231 2232 2233
typedef struct {
  int8_t  mqMsgType;
  int32_t code;
  int32_t epoch;
L
Liu Jicong 已提交
2234
  int64_t consumerId;
L
Liu Jicong 已提交
2235 2236
} SMqRspHead;

L
Liu Jicong 已提交
2237
typedef struct {
2238
  SMsgHead head;
L
Liu Jicong 已提交
2239

2240 2241
  int64_t consumerId;
  int64_t blockingTime;
L
Liu Jicong 已提交
2242
  int32_t epoch;
L
Liu Jicong 已提交
2243
  int8_t  withSchema;
L
Liu Jicong 已提交
2244
  char    cgroup[TSDB_CGROUP_LEN];
L
Liu Jicong 已提交
2245

L
Liu Jicong 已提交
2246
  int64_t currentOffset;
2247
  char    topic[TSDB_TOPIC_FNAME_LEN];
L
Liu Jicong 已提交
2248
} SMqPollReq;
L
Liu Jicong 已提交
2249

L
Liu Jicong 已提交
2250
typedef struct {
L
Liu Jicong 已提交
2251
  int32_t vgId;
L
Liu Jicong 已提交
2252
  int64_t offset;
L
Liu Jicong 已提交
2253 2254 2255
  SEpSet  epSet;
} SMqSubVgEp;

L
Liu Jicong 已提交
2256
typedef struct {
L
Liu Jicong 已提交
2257
  char    topic[TSDB_TOPIC_FNAME_LEN];
2258
  SArray* vgs;  // SArray<SMqSubVgEp>
L
Liu Jicong 已提交
2259 2260
} SMqSubTopicEp;

L
Liu Jicong 已提交
2261
typedef struct {
L
Liu Jicong 已提交
2262
  SMqRspHead head;
L
Liu Jicong 已提交
2263 2264 2265 2266 2267
  int64_t    reqOffset;
  int64_t    rspOffset;
  int32_t    skipLogNum;
  // TODO: replace with topic name
  int32_t numOfTopics;
L
Liu Jicong 已提交
2268 2269 2270 2271
  // TODO: remove from msg
  SSchemaWrapper* schema;
  SArray*         pBlockData;  // SArray<SSDataBlock>
} SMqPollRsp;
L
Liu Jicong 已提交
2272

L
Liu Jicong 已提交
2273
typedef struct {
L
Liu Jicong 已提交
2274
  SMqRspHead head;
L
Liu Jicong 已提交
2275 2276 2277
  char       cgroup[TSDB_CGROUP_LEN];
  SArray*    topics;  // SArray<SMqSubTopicEp>
} SMqCMGetSubEpRsp;
L
Liu Jicong 已提交
2278 2279 2280 2281 2282 2283 2284

typedef struct {
  int32_t curBlock;
  int32_t curRow;
  void**  uData;
} SMqRowIter;

L
Liu Jicong 已提交
2285 2286 2287
struct tmq_message_t {
  SMqPollRsp msg;
  void*      vg;
L
Liu Jicong 已提交
2288 2289 2290
  SMqRowIter iter;
};

L
Liu Jicong 已提交
2291
#if 0
L
Liu Jicong 已提交
2292 2293 2294 2295 2296 2297 2298 2299 2300 2301
struct tmq_message_t {
  SMqRspHead head;
  union {
    SMqPollRsp       consumeRsp;
    SMqCMGetSubEpRsp getEpRsp;
  };
  void*   extra;
  int32_t curBlock;
  int32_t curRow;
  void**  uData;
L
Liu Jicong 已提交
2302
};
L
Liu Jicong 已提交
2303
#endif
L
Liu Jicong 已提交
2304

2305
static FORCE_INLINE void tDeleteSMqSubTopicEp(SMqSubTopicEp* pSubTopicEp) { taosArrayDestroy(pSubTopicEp->vgs); }
L
Liu Jicong 已提交
2306

L
Liu Jicong 已提交
2307 2308
static FORCE_INLINE int32_t tEncodeSMqSubVgEp(void** buf, const SMqSubVgEp* pVgEp) {
  int32_t tlen = 0;
L
Liu Jicong 已提交
2309
  tlen += taosEncodeFixedI32(buf, pVgEp->vgId);
L
Liu Jicong 已提交
2310
  tlen += taosEncodeFixedI64(buf, pVgEp->offset);
L
Liu Jicong 已提交
2311 2312 2313 2314 2315 2316
  tlen += taosEncodeSEpSet(buf, &pVgEp->epSet);
  return tlen;
}

static FORCE_INLINE void* tDecodeSMqSubVgEp(void* buf, SMqSubVgEp* pVgEp) {
  buf = taosDecodeFixedI32(buf, &pVgEp->vgId);
L
Liu Jicong 已提交
2317
  buf = taosDecodeFixedI64(buf, &pVgEp->offset);
L
Liu Jicong 已提交
2318 2319 2320 2321
  buf = taosDecodeSEpSet(buf, &pVgEp->epSet);
  return buf;
}

L
Liu Jicong 已提交
2322
static FORCE_INLINE void tDeleteSMqCMGetSubEpRsp(SMqCMGetSubEpRsp* pRsp) {
2323
  taosArrayDestroyEx(pRsp->topics, (void (*)(void*))tDeleteSMqSubTopicEp);
L
Liu Jicong 已提交
2324 2325
}

L
Liu Jicong 已提交
2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355
static FORCE_INLINE int32_t tEncodeSMqSubTopicEp(void** buf, const SMqSubTopicEp* pTopicEp) {
  int32_t tlen = 0;
  tlen += taosEncodeString(buf, pTopicEp->topic);
  int32_t sz = taosArrayGetSize(pTopicEp->vgs);
  tlen += taosEncodeFixedI32(buf, sz);
  for (int32_t i = 0; i < sz; i++) {
    SMqSubVgEp* pVgEp = (SMqSubVgEp*)taosArrayGet(pTopicEp->vgs, i);
    tlen += tEncodeSMqSubVgEp(buf, pVgEp);
  }
  return tlen;
}

static FORCE_INLINE void* tDecodeSMqSubTopicEp(void* buf, SMqSubTopicEp* pTopicEp) {
  buf = taosDecodeStringTo(buf, pTopicEp->topic);
  int32_t sz;
  buf = taosDecodeFixedI32(buf, &sz);
  pTopicEp->vgs = taosArrayInit(sz, sizeof(SMqSubVgEp));
  if (pTopicEp->vgs == NULL) {
    return NULL;
  }
  for (int32_t i = 0; i < sz; i++) {
    SMqSubVgEp vgEp;
    buf = tDecodeSMqSubVgEp(buf, &vgEp);
    taosArrayPush(pTopicEp->vgs, &vgEp);
  }
  return buf;
}

static FORCE_INLINE int32_t tEncodeSMqCMGetSubEpRsp(void** buf, const SMqCMGetSubEpRsp* pRsp) {
  int32_t tlen = 0;
L
Liu Jicong 已提交
2356
  // tlen += taosEncodeString(buf, pRsp->cgroup);
L
Liu Jicong 已提交
2357 2358 2359 2360 2361 2362 2363 2364 2365 2366
  int32_t sz = taosArrayGetSize(pRsp->topics);
  tlen += taosEncodeFixedI32(buf, sz);
  for (int32_t i = 0; i < sz; i++) {
    SMqSubTopicEp* pVgEp = (SMqSubTopicEp*)taosArrayGet(pRsp->topics, i);
    tlen += tEncodeSMqSubTopicEp(buf, pVgEp);
  }
  return tlen;
}

static FORCE_INLINE void* tDecodeSMqCMGetSubEpRsp(void* buf, SMqCMGetSubEpRsp* pRsp) {
L
Liu Jicong 已提交
2367
  // buf = taosDecodeStringTo(buf, pRsp->cgroup);
L
Liu Jicong 已提交
2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380
  int32_t sz;
  buf = taosDecodeFixedI32(buf, &sz);
  pRsp->topics = taosArrayInit(sz, sizeof(SMqSubTopicEp));
  if (pRsp->topics == NULL) {
    return NULL;
  }
  for (int32_t i = 0; i < sz; i++) {
    SMqSubTopicEp topicEp;
    buf = tDecodeSMqSubTopicEp(buf, &topicEp);
    taosArrayPush(pRsp->topics, &topicEp);
  }
  return buf;
}
L
Liu Jicong 已提交
2381 2382
#pragma pack(pop)

D
dapan1121 已提交
2383 2384 2385 2386 2387
#ifdef __cplusplus
}
#endif

#endif /*_TD_COMMON_TAOS_MSG_H_*/