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

taosdata / TDengine / #3831

02 Apr 2025 01:14AM UTC coverage: 34.081% (-0.02%) from 34.097%
#3831

push

travis-ci

happyguoxy
test:alter gcda dir

148596 of 599532 branches covered (24.79%)

Branch coverage included in aggregate %.

222550 of 489473 relevant lines covered (45.47%)

1589752.67 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

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

167
  (void)memcpy(pMsg->data, pEntry, dataLen);
199,728✔
168

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

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

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

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

204
  SyncHeartbeatReply* pHeartbeatReply = pMsg->pCont;
12,324✔
205
  pHeartbeatReply->bytes = bytes;
12,324✔
206
  pHeartbeatReply->msgType = TDMT_SYNC_HEARTBEAT_REPLY;
12,324✔
207
  pHeartbeatReply->vgId = vgId;
12,324✔
208
  return 0;
12,324✔
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) {
87✔
245
  int32_t bytes = sizeof(SyncLeaderTransfer);
87✔
246
  pMsg->pCont = rpcMallocCont(bytes);
87✔
247
  pMsg->msgType = TDMT_SYNC_LEADER_TRANSFER;
87✔
248
  pMsg->contLen = bytes;
87✔
249
  if (pMsg->pCont == NULL) {
87!
250
    return terrno;
×
251
  }
252

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

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

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

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