• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #4271

10 Jun 2025 09:45AM UTC coverage: 62.985% (+0.002%) from 62.983%
#4271

push

travis-ci

web-flow
Merge pull request #31337 from taosdata/newtest_3.0

fix TD-35057 and TD-35346

158179 of 319671 branches covered (49.48%)

Branch coverage included in aggregate %.

243860 of 318637 relevant lines covered (76.53%)

18624660.26 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.23
/source/libs/sync/src/syncMessage.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#define _DEFAULT_SOURCE
17
#include "syncMessage.h"
18
#include "syncRaftEntry.h"
19
#include "syncRaftStore.h"
20

21
int32_t syncBuildTimeout(SRpcMsg* pMsg, ESyncTimeoutType timeoutType, uint64_t logicClock, int32_t timerMS,
154,214✔
22
                         SSyncNode* pNode) {
23
  int32_t bytes = sizeof(SyncTimeout);
154,214✔
24
  pMsg->pCont = rpcMallocCont(bytes);
154,214✔
25
  pMsg->msgType = (timeoutType == SYNC_TIMEOUT_ELECTION) ? TDMT_SYNC_TIMEOUT_ELECTION : TDMT_SYNC_TIMEOUT;
154,214✔
26
  pMsg->contLen = bytes;
154,214✔
27
  if (pMsg->pCont == NULL) {
154,214!
28
    return terrno;
×
29
  }
30

31
  SyncTimeout* pTimeout = pMsg->pCont;
154,214✔
32
  pTimeout->bytes = bytes;
154,214✔
33
  pTimeout->msgType = pMsg->msgType;
154,214✔
34
  pTimeout->vgId = pNode->vgId;
154,214✔
35
  pTimeout->timeoutType = timeoutType;
154,214✔
36
  pTimeout->logicClock = logicClock;
154,214✔
37
  pTimeout->timerMS = timerMS;
154,214✔
38
  pTimeout->timeStamp = taosGetTimestampMs();
154,214✔
39
  pTimeout->data = pNode;
154,214✔
40
  return 0;
154,214✔
41
}
42

43
int32_t syncBuildClientRequest(SRpcMsg* pMsg, const SRpcMsg* pOriginal, uint64_t seqNum, bool isWeak, int32_t vgId) {
282,573✔
44
  int32_t bytes = sizeof(SyncClientRequest) + pOriginal->contLen;
282,573✔
45

46
  pMsg->pCont = rpcMallocCont(bytes);
282,573✔
47
  if (pMsg->pCont == NULL) {
282,573!
48
    return terrno;
×
49
  }
50
  pMsg->msgType = TDMT_SYNC_CLIENT_REQUEST;
282,575✔
51
  pMsg->contLen = bytes;
282,575✔
52

53
  SyncClientRequest* pClientRequest = pMsg->pCont;
282,575✔
54
  pClientRequest->bytes = bytes;
282,575✔
55
  pClientRequest->vgId = vgId;
282,575✔
56
  pClientRequest->msgType = TDMT_SYNC_CLIENT_REQUEST;
282,575✔
57
  pClientRequest->originalRpcType = pOriginal->msgType;
282,575✔
58
  pClientRequest->seqNum = seqNum;
282,575✔
59
  pClientRequest->isWeak = isWeak;
282,575✔
60
  pClientRequest->dataLen = pOriginal->contLen;
282,575✔
61
  memcpy(pClientRequest->data, (char*)pOriginal->pCont, pOriginal->contLen);
282,575✔
62

63
  return 0;
282,575✔
64
}
65

66
int32_t syncBuildClientRequestFromNoopEntry(SRpcMsg* pMsg, const SSyncRaftEntry* pEntry, int32_t vgId) {
×
67
  int32_t bytes = sizeof(SyncClientRequest) + pEntry->bytes;
×
68
  pMsg->pCont = rpcMallocCont(bytes);
×
69
  pMsg->msgType = TDMT_SYNC_CLIENT_REQUEST;
×
70
  pMsg->contLen = bytes;
×
71
  if (pMsg->pCont == NULL) {
×
72
    return terrno;
×
73
  }
74

75
  SyncClientRequest* pClientRequest = pMsg->pCont;
×
76
  pClientRequest->bytes = bytes;
×
77
  pClientRequest->vgId = vgId;
×
78
  pClientRequest->msgType = TDMT_SYNC_CLIENT_REQUEST;
×
79
  pClientRequest->originalRpcType = TDMT_SYNC_NOOP;
×
80
  pClientRequest->dataLen = pEntry->bytes;
×
81
  memcpy(pClientRequest->data, (char*)pEntry, pEntry->bytes);
×
82

83
  return 0;
×
84
}
85

86
int32_t syncBuildRequestVote(SRpcMsg* pMsg, int32_t vgId) {
3,165✔
87
  int32_t bytes = sizeof(SyncRequestVote);
3,165✔
88
  pMsg->pCont = rpcMallocCont(bytes);
3,165✔
89
  pMsg->msgType = TDMT_SYNC_REQUEST_VOTE;
3,165✔
90
  pMsg->contLen = bytes;
3,165✔
91
  if (pMsg->pCont == NULL) {
3,165!
92
    return terrno;
×
93
  }
94

95
  SyncRequestVote* pRequestVote = pMsg->pCont;
3,165✔
96
  pRequestVote->bytes = bytes;
3,165✔
97
  pRequestVote->msgType = TDMT_SYNC_REQUEST_VOTE;
3,165✔
98
  pRequestVote->vgId = vgId;
3,165✔
99
  return 0;
3,165✔
100
}
101

102
int32_t syncBuildRequestVoteReply(SRpcMsg* pMsg, int32_t vgId) {
4,250✔
103
  int32_t bytes = sizeof(SyncRequestVoteReply);
4,250✔
104
  pMsg->pCont = rpcMallocCont(bytes);
4,250✔
105
  pMsg->msgType = TDMT_SYNC_REQUEST_VOTE_REPLY;
4,250✔
106
  pMsg->contLen = bytes;
4,250✔
107
  if (pMsg->pCont == NULL) {
4,250!
108
    return terrno;
×
109
  }
110

111
  SyncRequestVoteReply* pRequestVoteReply = pMsg->pCont;
4,250✔
112
  pRequestVoteReply->bytes = bytes;
4,250✔
113
  pRequestVoteReply->msgType = TDMT_SYNC_REQUEST_VOTE_REPLY;
4,250✔
114
  pRequestVoteReply->vgId = vgId;
4,250✔
115
  return 0;
4,250✔
116
}
117

118
int32_t syncBuildAppendEntries(SRpcMsg* pMsg, int32_t dataLen, int32_t vgId) {
×
119
  int32_t bytes = sizeof(SyncAppendEntries) + dataLen;
×
120
  pMsg->pCont = rpcMallocCont(bytes);
×
121
  pMsg->msgType = TDMT_SYNC_APPEND_ENTRIES;
×
122
  pMsg->contLen = bytes;
×
123
  if (pMsg->pCont == NULL) {
×
124
    return terrno;
×
125
  }
126

127
  SyncAppendEntries* pAppendEntries = pMsg->pCont;
×
128
  pAppendEntries->bytes = bytes;
×
129
  pAppendEntries->vgId = vgId;
×
130
  pAppendEntries->msgType = TDMT_SYNC_APPEND_ENTRIES;
×
131
  pAppendEntries->dataLen = dataLen;
×
132
  return 0;
×
133
}
134

135
int32_t syncBuildAppendEntriesReply(SRpcMsg* pMsg, int32_t vgId) {
2,793,152✔
136
  int32_t bytes = sizeof(SyncAppendEntriesReply);
2,793,152✔
137
  pMsg->pCont = rpcMallocCont(bytes);
2,793,152✔
138
  pMsg->msgType = TDMT_SYNC_APPEND_ENTRIES_REPLY;
2,793,153✔
139
  pMsg->contLen = bytes;
2,793,153✔
140
  if (pMsg->pCont == NULL) {
2,793,153!
141
    return terrno;
×
142
  }
143

144
  SyncAppendEntriesReply* pAppendEntriesReply = pMsg->pCont;
2,793,154✔
145
  pAppendEntriesReply->bytes = bytes;
2,793,154✔
146
  pAppendEntriesReply->msgType = TDMT_SYNC_APPEND_ENTRIES_REPLY;
2,793,154✔
147
  pAppendEntriesReply->vgId = vgId;
2,793,154✔
148
  return 0;
2,793,154✔
149
}
150

151
int32_t syncBuildAppendEntriesFromRaftEntry(SSyncNode* pNode, SSyncRaftEntry* pEntry, SyncTerm prevLogTerm,
2,816,661✔
152
                                            SRpcMsg* pRpcMsg) {
153
  uint32_t dataLen = pEntry->bytes;
2,816,661✔
154
  uint32_t bytes = sizeof(SyncAppendEntries) + dataLen;
2,816,661✔
155
  pRpcMsg->info.traceId = pEntry->originRpcTraceId;
2,816,661✔
156
  pRpcMsg->contLen = bytes;
2,816,661✔
157
  pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen);
2,816,661✔
158
  if (pRpcMsg->pCont == NULL) {
2,816,680!
159
    return terrno;
×
160
  }
161

162
  SyncAppendEntries* pMsg = pRpcMsg->pCont;
2,816,680✔
163
  pMsg->bytes = pRpcMsg->contLen;
2,816,680✔
164
  pMsg->msgType = pRpcMsg->msgType = TDMT_SYNC_APPEND_ENTRIES;
2,816,680✔
165
  pMsg->dataLen = dataLen;
2,816,680✔
166

167
  (void)memcpy(pMsg->data, pEntry, dataLen);
2,816,680✔
168

169
  pMsg->prevLogIndex = pEntry->index - 1;
2,816,680✔
170
  pMsg->prevLogTerm = prevLogTerm;
2,816,680✔
171
  pMsg->vgId = pNode->vgId;
2,816,680✔
172
  pMsg->srcId = pNode->myRaftId;
2,816,680✔
173
  pMsg->term = raftStoreGetTerm(pNode);
2,816,680✔
174
  pMsg->commitIndex = pNode->commitIndex;
2,816,694✔
175
  pMsg->privateTerm = 0;
2,816,694✔
176
  return 0;
2,816,694✔
177
}
178

179
int32_t syncBuildHeartbeat(SRpcMsg* pMsg, int32_t vgId) {
51,728✔
180
  int32_t bytes = sizeof(SyncHeartbeat);
51,728✔
181
  pMsg->pCont = rpcMallocCont(bytes);
51,728✔
182
  pMsg->msgType = TDMT_SYNC_HEARTBEAT;
51,728✔
183
  pMsg->contLen = bytes;
51,728✔
184
  if (pMsg->pCont == NULL) {
51,728!
185
    return terrno;
×
186
  }
187

188
  SyncHeartbeat* pHeartbeat = pMsg->pCont;
51,728✔
189
  pHeartbeat->bytes = bytes;
51,728✔
190
  pHeartbeat->msgType = TDMT_SYNC_HEARTBEAT;
51,728✔
191
  pHeartbeat->vgId = vgId;
51,728✔
192
  return 0;
51,728✔
193
}
194

195
int32_t syncBuildHeartbeatReply(SRpcMsg* pMsg, int32_t vgId) {
45,173✔
196
  int32_t bytes = sizeof(SyncHeartbeatReply);
45,173✔
197
  pMsg->pCont = rpcMallocCont(bytes);
45,173✔
198
  pMsg->msgType = TDMT_SYNC_HEARTBEAT_REPLY;
45,173✔
199
  pMsg->contLen = bytes;
45,173✔
200
  if (pMsg->pCont == NULL) {
45,173!
201
    return terrno;
×
202
  }
203

204
  SyncHeartbeatReply* pHeartbeatReply = pMsg->pCont;
45,173✔
205
  pHeartbeatReply->bytes = bytes;
45,173✔
206
  pHeartbeatReply->msgType = TDMT_SYNC_HEARTBEAT_REPLY;
45,173✔
207
  pHeartbeatReply->vgId = vgId;
45,173✔
208
  return 0;
45,173✔
209
}
210

211
int32_t syncBuildSnapshotSend(SRpcMsg* pMsg, int32_t dataLen, int32_t vgId) {
68,401✔
212
  int32_t bytes = sizeof(SyncSnapshotSend) + dataLen;
68,401✔
213
  pMsg->pCont = rpcMallocCont(bytes);
68,401✔
214
  pMsg->msgType = TDMT_SYNC_SNAPSHOT_SEND;
68,401✔
215
  pMsg->contLen = bytes;
68,401✔
216
  if (pMsg->pCont == NULL) {
68,401!
217
    return terrno;
×
218
  }
219

220
  SyncSnapshotSend* pSnapshotSend = pMsg->pCont;
68,401✔
221
  pSnapshotSend->bytes = bytes;
68,401✔
222
  pSnapshotSend->vgId = vgId;
68,401✔
223
  pSnapshotSend->msgType = TDMT_SYNC_SNAPSHOT_SEND;
68,401✔
224
  pSnapshotSend->dataLen = dataLen;
68,401✔
225
  return 0;
68,401✔
226
}
227

228
int32_t syncBuildSnapshotSendRsp(SRpcMsg* pMsg, int32_t dataLen, int32_t vgId) {
68,283✔
229
  int32_t bytes = sizeof(SyncSnapshotRsp) + dataLen;
68,283✔
230
  pMsg->pCont = rpcMallocCont(bytes);
68,283✔
231
  pMsg->msgType = TDMT_SYNC_SNAPSHOT_RSP;
68,283✔
232
  pMsg->contLen = bytes;
68,283✔
233
  if (pMsg->pCont == NULL) {
68,283!
234
    return terrno;
×
235
  }
236

237
  SyncSnapshotRsp* pPreSnapshotRsp = pMsg->pCont;
68,283✔
238
  pPreSnapshotRsp->bytes = bytes;
68,283✔
239
  pPreSnapshotRsp->msgType = TDMT_SYNC_SNAPSHOT_RSP;
68,283✔
240
  pPreSnapshotRsp->vgId = vgId;
68,283✔
241
  return 0;
68,283✔
242
}
243

244
int32_t syncBuildLeaderTransfer(SRpcMsg* pMsg, int32_t vgId) {
1,195✔
245
  int32_t bytes = sizeof(SyncLeaderTransfer);
1,195✔
246
  pMsg->pCont = rpcMallocCont(bytes);
1,195✔
247
  pMsg->msgType = TDMT_SYNC_LEADER_TRANSFER;
1,195✔
248
  pMsg->contLen = bytes;
1,195✔
249
  if (pMsg->pCont == NULL) {
1,195!
250
    return terrno;
×
251
  }
252

253
  SyncLeaderTransfer* pLeaderTransfer = pMsg->pCont;
1,195✔
254
  pLeaderTransfer->bytes = bytes;
1,195✔
255
  pLeaderTransfer->msgType = TDMT_SYNC_LEADER_TRANSFER;
1,195✔
256
  pLeaderTransfer->vgId = vgId;
1,195✔
257
  return 0;
1,195✔
258
}
259

260
int32_t syncBuildLocalCmd(SRpcMsg* pMsg, int32_t vgId) {
44,857✔
261
  int32_t bytes = sizeof(SyncLocalCmd);
44,857✔
262
  pMsg->pCont = rpcMallocCont(bytes);
44,857✔
263
  pMsg->msgType = TDMT_SYNC_LOCAL_CMD;
44,857✔
264
  pMsg->contLen = bytes;
44,857✔
265
  if (pMsg->pCont == NULL) {
44,857!
266
    return terrno;
×
267
  }
268

269
  SyncLocalCmd* pLocalCmd = pMsg->pCont;
44,857✔
270
  pLocalCmd->bytes = bytes;
44,857✔
271
  pLocalCmd->msgType = TDMT_SYNC_LOCAL_CMD;
44,857✔
272
  pLocalCmd->vgId = vgId;
44,857✔
273
  return 0;
44,857✔
274
}
275

276
const char* syncTimerTypeStr(enum ESyncTimeoutType timerType) {
4,727✔
277
  switch (timerType) {
4,727!
278
    case SYNC_TIMEOUT_PING:
4,727✔
279
      return "ping";
4,727✔
280
    case SYNC_TIMEOUT_ELECTION:
×
281
      return "elect";
×
282
    case SYNC_TIMEOUT_HEARTBEAT:
×
283
      return "heartbeat";
×
284
    default:
×
285
      return "unknown";
×
286
  }
287
}
288

289
const char* syncLocalCmdGetStr(ESyncLocalCmd cmd) {
×
290
  switch (cmd) {
×
291
    case SYNC_LOCAL_CMD_STEP_DOWN:
×
292
      return "step-down";
×
293
    case SYNC_LOCAL_CMD_FOLLOWER_CMT:
×
294
      return "follower-commit";
×
295
    default:
×
296
      return "unknown-local-cmd";
×
297
  }
298
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc