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

taosdata / TDengine / #3586

22 Jan 2025 05:37AM UTC coverage: 63.643% (-0.004%) from 63.647%
#3586

push

travis-ci

web-flow
Merge pull request #29612 from taosdata/fix/TD-33556

fix:[TD-33556] tmq close elegantly to avoid invalid read in TD-32585

140971 of 284575 branches covered (49.54%)

Branch coverage included in aggregate %.

19 of 27 new or added lines in 2 files covered. (70.37%)

655 existing lines in 114 files now uncovered.

219468 of 281768 relevant lines covered (77.89%)

19335507.45 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

161
  SyncAppendEntries* pMsg = pRpcMsg->pCont;
3,309,154✔
162
  pMsg->bytes = pRpcMsg->contLen;
3,309,154✔
163
  pMsg->msgType = pRpcMsg->msgType = TDMT_SYNC_APPEND_ENTRIES;
3,309,154✔
164
  pMsg->dataLen = dataLen;
3,309,154✔
165

166
  (void)memcpy(pMsg->data, pEntry, dataLen);
3,309,154✔
167

168
  pMsg->prevLogIndex = pEntry->index - 1;
3,309,154✔
169
  pMsg->prevLogTerm = prevLogTerm;
3,309,154✔
170
  pMsg->vgId = pNode->vgId;
3,309,154✔
171
  pMsg->srcId = pNode->myRaftId;
3,309,154✔
172
  pMsg->term = raftStoreGetTerm(pNode);
3,309,154✔
173
  pMsg->commitIndex = pNode->commitIndex;
3,309,158✔
174
  pMsg->privateTerm = 0;
3,309,158✔
175
  return 0;
3,309,158✔
176
}
177

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

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

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

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

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

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

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

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

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

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

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

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

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

288
const char* syncLocalCmdGetStr(ESyncLocalCmd cmd) {
×
289
  switch (cmd) {
×
290
    case SYNC_LOCAL_CMD_STEP_DOWN:
×
291
      return "step-down";
×
292
    case SYNC_LOCAL_CMD_FOLLOWER_CMT:
×
293
      return "follower-commit";
×
294
    default:
×
295
      return "unknown-local-cmd";
×
296
  }
297
}
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