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

taosdata / TDengine / #3824

01 Apr 2025 12:26PM UTC coverage: 34.064% (-0.001%) from 34.065%
#3824

push

travis-ci

happyguoxy
test:alter gcda dir

148483 of 599532 branches covered (24.77%)

Branch coverage included in aggregate %.

222466 of 489445 relevant lines covered (45.45%)

762427.9 hits per line

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

59.35
/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,
2,360✔
22
                         SSyncNode* pNode) {
23
  int32_t bytes = sizeof(SyncTimeout);
2,360✔
24
  pMsg->pCont = rpcMallocCont(bytes);
2,360✔
25
  pMsg->msgType = (timeoutType == SYNC_TIMEOUT_ELECTION) ? TDMT_SYNC_TIMEOUT_ELECTION : TDMT_SYNC_TIMEOUT;
2,360✔
26
  pMsg->contLen = bytes;
2,360✔
27
  if (pMsg->pCont == NULL) {
2,360!
28
    return terrno;
×
29
  }
30

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

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

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

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

63
  return 0;
34,220✔
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) {
53✔
87
  int32_t bytes = sizeof(SyncRequestVote);
53✔
88
  pMsg->pCont = rpcMallocCont(bytes);
53✔
89
  pMsg->msgType = TDMT_SYNC_REQUEST_VOTE;
53✔
90
  pMsg->contLen = bytes;
53✔
91
  if (pMsg->pCont == NULL) {
53!
92
    return terrno;
×
93
  }
94

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

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

111
  SyncRequestVoteReply* pRequestVoteReply = pMsg->pCont;
82✔
112
  pRequestVoteReply->bytes = bytes;
82✔
113
  pRequestVoteReply->msgType = TDMT_SYNC_REQUEST_VOTE_REPLY;
82✔
114
  pRequestVoteReply->vgId = vgId;
82✔
115
  return 0;
82✔
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) {
64,192✔
136
  int32_t bytes = sizeof(SyncAppendEntriesReply);
64,192✔
137
  pMsg->pCont = rpcMallocCont(bytes);
64,192✔
138
  pMsg->msgType = TDMT_SYNC_APPEND_ENTRIES_REPLY;
64,192✔
139
  pMsg->contLen = bytes;
64,192✔
140
  if (pMsg->pCont == NULL) {
64,192!
141
    return terrno;
×
142
  }
143

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

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

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

167
  (void)memcpy(pMsg->data, pEntry, dataLen);
64,196✔
168

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

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

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

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

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

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

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

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

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

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

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

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

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

276
const char* syncTimerTypeStr(enum ESyncTimeoutType timerType) {
124✔
277
  switch (timerType) {
124!
278
    case SYNC_TIMEOUT_PING:
124✔
279
      return "ping";
124✔
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