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

taosdata / TDengine / #3534

21 Nov 2024 07:36AM UTC coverage: 60.825% (+2.0%) from 58.848%
#3534

push

travis-ci

web-flow
Merge pull request #28810 from taosdata/ehn/add-sync-heartbeat-sent-time-to-log

ehn:add-sync-heartbeat-sent-time-to-log

120023 of 252376 branches covered (47.56%)

Branch coverage included in aggregate %.

43 of 47 new or added lines in 3 files covered. (91.49%)

2254 existing lines in 162 files now uncovered.

200876 of 275203 relevant lines covered (72.99%)

16110754.39 hits per line

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

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

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

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

46
  pMsg->pCont = rpcMallocCont(bytes);
257,237✔
47
  if (pMsg->pCont == NULL) {
257,238✔
48
    return terrno;
2✔
49
  }
50
  pMsg->msgType = TDMT_SYNC_CLIENT_REQUEST;
257,236✔
51
  pMsg->contLen = bytes;
257,236✔
52

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

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

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

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

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

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

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

161
  SyncAppendEntries* pMsg = pRpcMsg->pCont;
2,845,705✔
162
  pMsg->bytes = pRpcMsg->contLen;
2,845,705✔
163
  pMsg->msgType = pRpcMsg->msgType = TDMT_SYNC_APPEND_ENTRIES;
2,845,705✔
164
  pMsg->dataLen = dataLen;
2,845,705✔
165

166
  (void)memcpy(pMsg->data, pEntry, dataLen);
2,845,705✔
167

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

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

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

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

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

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

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

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

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

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

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

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

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

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

© 2025 Coveralls, Inc