2022-02-22 03:28:15 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can use, redistribute, and/or modify
|
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3
|
|
|
|
|
* or later ("AGPL"), as published by the Free Software Foundation.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-02-26 18:24:50 +00:00
|
|
|
#include "syncReplication.h"
|
2022-03-14 06:05:40 +00:00
|
|
|
#include "syncIndexMgr.h"
|
2022-03-07 08:06:07 +00:00
|
|
|
#include "syncMessage.h"
|
2022-05-30 05:14:48 +00:00
|
|
|
#include "syncRaftCfg.h"
|
2022-03-14 06:05:40 +00:00
|
|
|
#include "syncRaftEntry.h"
|
2022-03-14 12:43:35 +00:00
|
|
|
#include "syncRaftLog.h"
|
2022-03-16 08:54:55 +00:00
|
|
|
#include "syncRaftStore.h"
|
2022-05-31 09:53:38 +00:00
|
|
|
#include "syncSnapshot.h"
|
2022-03-14 12:43:35 +00:00
|
|
|
#include "syncUtil.h"
|
2022-03-04 07:48:09 +00:00
|
|
|
|
2022-03-07 08:06:07 +00:00
|
|
|
// TLA+ Spec
|
|
|
|
|
// AppendEntries(i, j) ==
|
|
|
|
|
// /\ i /= j
|
|
|
|
|
// /\ state[i] = Leader
|
|
|
|
|
// /\ LET prevLogIndex == nextIndex[i][j] - 1
|
|
|
|
|
// prevLogTerm == IF prevLogIndex > 0 THEN
|
|
|
|
|
// log[i][prevLogIndex].term
|
|
|
|
|
// ELSE
|
|
|
|
|
// 0
|
|
|
|
|
// \* Send up to 1 entry, constrained by the end of the log.
|
|
|
|
|
// lastEntry == Min({Len(log[i]), nextIndex[i][j]})
|
|
|
|
|
// entries == SubSeq(log[i], nextIndex[i][j], lastEntry)
|
|
|
|
|
// IN Send([mtype |-> AppendEntriesRequest,
|
|
|
|
|
// mterm |-> currentTerm[i],
|
|
|
|
|
// mprevLogIndex |-> prevLogIndex,
|
|
|
|
|
// mprevLogTerm |-> prevLogTerm,
|
|
|
|
|
// mentries |-> entries,
|
|
|
|
|
// \* mlog is used as a history variable for the proof.
|
|
|
|
|
// \* It would not exist in a real implementation.
|
|
|
|
|
// mlog |-> log[i],
|
|
|
|
|
// mcommitIndex |-> Min({commitIndex[i], lastEntry}),
|
|
|
|
|
// msource |-> i,
|
|
|
|
|
// mdest |-> j])
|
|
|
|
|
// /\ UNCHANGED <<serverVars, candidateVars, leaderVars, logVars>>
|
2022-03-07 08:29:21 +00:00
|
|
|
//
|
2022-03-08 06:19:50 +00:00
|
|
|
int32_t syncNodeAppendEntriesPeers(SSyncNode* pSyncNode) {
|
2022-06-21 08:02:36 +00:00
|
|
|
ASSERT(pSyncNode->state == TAOS_SYNC_STATE_LEADER);
|
2022-03-14 06:05:40 +00:00
|
|
|
|
2022-03-23 09:08:07 +00:00
|
|
|
syncIndexMgrLog2("==syncNodeAppendEntriesPeers== pNextIndex", pSyncNode->pNextIndex);
|
|
|
|
|
syncIndexMgrLog2("==syncNodeAppendEntriesPeers== pMatchIndex", pSyncNode->pMatchIndex);
|
|
|
|
|
logStoreSimpleLog2("==syncNodeAppendEntriesPeers==", pSyncNode->pLogStore);
|
|
|
|
|
|
2022-03-08 06:19:50 +00:00
|
|
|
int32_t ret = 0;
|
2022-03-14 06:05:40 +00:00
|
|
|
for (int i = 0; i < pSyncNode->peersNum; ++i) {
|
2022-03-16 08:54:55 +00:00
|
|
|
SRaftId* pDestId = &(pSyncNode->peersId[i]);
|
2022-03-14 12:43:35 +00:00
|
|
|
|
2022-03-16 08:54:55 +00:00
|
|
|
// set prevLogIndex
|
|
|
|
|
SyncIndex nextIndex = syncIndexMgrGetIndex(pSyncNode->pNextIndex, pDestId);
|
2022-06-01 13:23:39 +00:00
|
|
|
|
2022-03-14 06:05:40 +00:00
|
|
|
SyncIndex preLogIndex = nextIndex - 1;
|
2022-03-14 12:43:35 +00:00
|
|
|
|
2022-03-16 08:54:55 +00:00
|
|
|
// set preLogTerm
|
2022-03-14 12:43:35 +00:00
|
|
|
SyncTerm preLogTerm = 0;
|
|
|
|
|
if (preLogIndex >= SYNC_INDEX_BEGIN) {
|
2022-03-14 06:05:40 +00:00
|
|
|
SSyncRaftEntry* pPreEntry = pSyncNode->pLogStore->getEntry(pSyncNode->pLogStore, preLogIndex);
|
2022-06-21 08:02:36 +00:00
|
|
|
ASSERT(pPreEntry != NULL);
|
2022-03-16 08:54:55 +00:00
|
|
|
|
2022-03-14 06:05:40 +00:00
|
|
|
preLogTerm = pPreEntry->term;
|
2022-03-16 08:54:55 +00:00
|
|
|
syncEntryDestory(pPreEntry);
|
2022-03-14 06:05:40 +00:00
|
|
|
}
|
2022-03-14 12:43:35 +00:00
|
|
|
|
2022-03-16 08:54:55 +00:00
|
|
|
// batch optimized
|
|
|
|
|
// SyncIndex lastIndex = syncUtilMinIndex(pSyncNode->pLogStore->getLastIndex(pSyncNode->pLogStore), nextIndex);
|
|
|
|
|
|
|
|
|
|
SyncAppendEntries* pMsg = NULL;
|
2022-05-27 07:41:19 +00:00
|
|
|
SSyncRaftEntry* pEntry = pSyncNode->pLogStore->getEntry(pSyncNode->pLogStore, nextIndex);
|
2022-03-16 08:54:55 +00:00
|
|
|
if (pEntry != NULL) {
|
2022-04-18 13:50:56 +00:00
|
|
|
pMsg = syncAppendEntriesBuild(pEntry->bytes, pSyncNode->vgId);
|
2022-06-21 08:02:36 +00:00
|
|
|
ASSERT(pMsg != NULL);
|
2022-03-14 12:43:35 +00:00
|
|
|
|
2022-03-16 08:54:55 +00:00
|
|
|
// add pEntry into msg
|
|
|
|
|
uint32_t len;
|
|
|
|
|
char* serialized = syncEntrySerialize(pEntry, &len);
|
2022-06-21 08:02:36 +00:00
|
|
|
ASSERT(len == pEntry->bytes);
|
2022-03-16 08:54:55 +00:00
|
|
|
memcpy(pMsg->data, serialized, len);
|
|
|
|
|
|
2022-03-25 16:29:53 +00:00
|
|
|
taosMemoryFree(serialized);
|
2022-03-16 08:54:55 +00:00
|
|
|
syncEntryDestory(pEntry);
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
// maybe overflow, send empty record
|
2022-04-18 13:50:56 +00:00
|
|
|
pMsg = syncAppendEntriesBuild(0, pSyncNode->vgId);
|
2022-06-21 08:02:36 +00:00
|
|
|
ASSERT(pMsg != NULL);
|
2022-03-16 08:54:55 +00:00
|
|
|
}
|
2022-03-14 12:43:35 +00:00
|
|
|
|
2022-06-21 08:02:36 +00:00
|
|
|
ASSERT(pMsg != NULL);
|
2022-03-14 12:43:35 +00:00
|
|
|
pMsg->srcId = pSyncNode->myRaftId;
|
|
|
|
|
pMsg->destId = *pDestId;
|
2022-03-16 08:54:55 +00:00
|
|
|
pMsg->term = pSyncNode->pRaftStore->currentTerm;
|
2022-03-14 12:43:35 +00:00
|
|
|
pMsg->prevLogIndex = preLogIndex;
|
|
|
|
|
pMsg->prevLogTerm = preLogTerm;
|
|
|
|
|
pMsg->commitIndex = pSyncNode->commitIndex;
|
|
|
|
|
|
2022-03-23 09:08:07 +00:00
|
|
|
syncAppendEntriesLog2("==syncNodeAppendEntriesPeers==", pMsg);
|
|
|
|
|
|
2022-03-16 08:54:55 +00:00
|
|
|
// send AppendEntries
|
2022-03-14 12:43:35 +00:00
|
|
|
syncNodeAppendEntries(pSyncNode, pDestId, pMsg);
|
2022-03-16 08:54:55 +00:00
|
|
|
syncAppendEntriesDestroy(pMsg);
|
2022-03-14 06:05:40 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-08 06:19:50 +00:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-17 02:34:53 +00:00
|
|
|
int32_t syncNodeAppendEntriesOnePeer(SSyncNode* pSyncNode, SRaftId* pDestId, SyncIndex nextIndex) {
|
|
|
|
|
int32_t ret = 0;
|
|
|
|
|
|
|
|
|
|
// pre index, pre term
|
|
|
|
|
SyncIndex preLogIndex = syncNodeGetPreIndex(pSyncNode, nextIndex);
|
|
|
|
|
SyncTerm preLogTerm = syncNodeGetPreTerm(pSyncNode, nextIndex);
|
|
|
|
|
if (preLogTerm == SYNC_TERM_INVALID) {
|
|
|
|
|
SyncIndex newNextIndex = syncNodeGetLastIndex(pSyncNode) + 1;
|
|
|
|
|
// SyncIndex newNextIndex = nextIndex + 1;
|
|
|
|
|
|
|
|
|
|
syncIndexMgrSetIndex(pSyncNode->pNextIndex, pDestId, newNextIndex);
|
|
|
|
|
syncIndexMgrSetIndex(pSyncNode->pMatchIndex, pDestId, SYNC_INDEX_INVALID);
|
|
|
|
|
sError("vgId:%d, sync get pre term error, nextIndex:%" PRId64 ", update next-index:%" PRId64
|
|
|
|
|
", match-index:%d, raftid:%" PRId64,
|
|
|
|
|
pSyncNode->vgId, nextIndex, newNextIndex, SYNC_INDEX_INVALID, pDestId->addr);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// entry pointer array
|
|
|
|
|
SSyncRaftEntry* entryPArr[SYNC_MAX_BATCH_SIZE];
|
|
|
|
|
memset(entryPArr, 0, sizeof(entryPArr));
|
|
|
|
|
|
|
|
|
|
// get entry batch
|
|
|
|
|
int32_t getCount = 0;
|
|
|
|
|
SyncIndex getEntryIndex = nextIndex;
|
|
|
|
|
for (int32_t i = 0; i < pSyncNode->pRaftCfg->batchSize; ++i) {
|
|
|
|
|
SSyncRaftEntry* pEntry = NULL;
|
|
|
|
|
int32_t code = pSyncNode->pLogStore->syncLogGetEntry(pSyncNode->pLogStore, getEntryIndex, &pEntry);
|
|
|
|
|
if (code == 0) {
|
|
|
|
|
ASSERT(pEntry != NULL);
|
|
|
|
|
entryPArr[i] = pEntry;
|
|
|
|
|
getCount++;
|
|
|
|
|
getEntryIndex++;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// event log
|
|
|
|
|
do {
|
|
|
|
|
char logBuf[128];
|
|
|
|
|
char host[64];
|
|
|
|
|
uint16_t port;
|
|
|
|
|
syncUtilU642Addr(pDestId->addr, host, sizeof(host), &port);
|
|
|
|
|
snprintf(logBuf, sizeof(logBuf), "build batch:%d for %s:%d", getCount, host, port);
|
|
|
|
|
syncNodeEventLog(pSyncNode, logBuf);
|
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
|
|
// build msg
|
|
|
|
|
SyncAppendEntriesBatch* pMsg = syncAppendEntriesBatchBuild(entryPArr, getCount, pSyncNode->vgId);
|
|
|
|
|
ASSERT(pMsg != NULL);
|
|
|
|
|
|
|
|
|
|
// free entries
|
|
|
|
|
for (int32_t i = 0; i < pSyncNode->pRaftCfg->batchSize; ++i) {
|
|
|
|
|
SSyncRaftEntry* pEntry = entryPArr[i];
|
|
|
|
|
if (pEntry != NULL) {
|
|
|
|
|
syncEntryDestory(pEntry);
|
|
|
|
|
entryPArr[i] = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// prepare msg
|
|
|
|
|
pMsg->srcId = pSyncNode->myRaftId;
|
|
|
|
|
pMsg->destId = *pDestId;
|
|
|
|
|
pMsg->term = pSyncNode->pRaftStore->currentTerm;
|
|
|
|
|
pMsg->prevLogIndex = preLogIndex;
|
|
|
|
|
pMsg->prevLogTerm = preLogTerm;
|
|
|
|
|
pMsg->commitIndex = pSyncNode->commitIndex;
|
|
|
|
|
pMsg->privateTerm = 0;
|
|
|
|
|
pMsg->dataCount = getCount;
|
|
|
|
|
|
|
|
|
|
// send msg
|
|
|
|
|
syncNodeAppendEntriesBatch(pSyncNode, pDestId, pMsg);
|
|
|
|
|
|
|
|
|
|
// speed up
|
|
|
|
|
if (pMsg->dataCount > 0 && pSyncNode->commitIndex - pMsg->prevLogIndex > SYNC_SLOW_DOWN_RANGE) {
|
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
do {
|
|
|
|
|
char logBuf[128];
|
|
|
|
|
char host[64];
|
|
|
|
|
uint16_t port;
|
|
|
|
|
syncUtilU642Addr(pDestId->addr, host, sizeof(host), &port);
|
|
|
|
|
snprintf(logBuf, sizeof(logBuf), "maybe speed up for %s:%d, pre-index:%ld", host, port, pMsg->prevLogIndex);
|
|
|
|
|
syncNodeEventLog(pSyncNode, logBuf);
|
|
|
|
|
} while (0);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
syncAppendEntriesBatchDestroy(pMsg);
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t syncNodeAppendEntriesPeersSnapshot2(SSyncNode* pSyncNode) {
|
|
|
|
|
if (pSyncNode->state != TAOS_SYNC_STATE_LEADER) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t ret = 0;
|
|
|
|
|
for (int i = 0; i < pSyncNode->peersNum; ++i) {
|
|
|
|
|
SRaftId* pDestId = &(pSyncNode->peersId[i]);
|
|
|
|
|
|
|
|
|
|
// next index
|
|
|
|
|
SyncIndex nextIndex = syncIndexMgrGetIndex(pSyncNode->pNextIndex, pDestId);
|
|
|
|
|
ret = syncNodeAppendEntriesOnePeer(pSyncNode, pDestId, nextIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if 0
|
2022-07-01 01:50:20 +00:00
|
|
|
int32_t syncNodeAppendEntriesPeersSnapshot2(SSyncNode* pSyncNode) {
|
|
|
|
|
if (pSyncNode->state != TAOS_SYNC_STATE_LEADER) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t ret = 0;
|
|
|
|
|
for (int i = 0; i < pSyncNode->peersNum; ++i) {
|
|
|
|
|
SRaftId* pDestId = &(pSyncNode->peersId[i]);
|
|
|
|
|
|
|
|
|
|
// next index
|
|
|
|
|
SyncIndex nextIndex = syncIndexMgrGetIndex(pSyncNode->pNextIndex, pDestId);
|
|
|
|
|
|
|
|
|
|
// pre index, pre term
|
2022-07-01 06:22:14 +00:00
|
|
|
SyncIndex preLogIndex = syncNodeGetPreIndex(pSyncNode, nextIndex);
|
|
|
|
|
SyncTerm preLogTerm = syncNodeGetPreTerm(pSyncNode, nextIndex);
|
2022-07-01 01:50:20 +00:00
|
|
|
if (preLogTerm == SYNC_TERM_INVALID) {
|
2022-08-03 06:41:38 +00:00
|
|
|
SyncIndex newNextIndex = syncNodeGetLastIndex(pSyncNode) + 1;
|
|
|
|
|
// SyncIndex newNextIndex = nextIndex + 1;
|
|
|
|
|
|
2022-07-01 01:50:20 +00:00
|
|
|
syncIndexMgrSetIndex(pSyncNode->pNextIndex, pDestId, newNextIndex);
|
|
|
|
|
syncIndexMgrSetIndex(pSyncNode->pMatchIndex, pDestId, SYNC_INDEX_INVALID);
|
2022-08-01 09:23:52 +00:00
|
|
|
sError("vgId:%d, sync get pre term error, nextIndex:%" PRId64 ", update next-index:%" PRId64
|
2022-07-11 02:34:02 +00:00
|
|
|
", match-index:%d, raftid:%" PRId64,
|
2022-07-01 01:50:20 +00:00
|
|
|
pSyncNode->vgId, nextIndex, newNextIndex, SYNC_INDEX_INVALID, pDestId->addr);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-06 03:12:45 +00:00
|
|
|
// entry pointer array
|
2022-07-04 05:33:49 +00:00
|
|
|
SSyncRaftEntry* entryPArr[SYNC_MAX_BATCH_SIZE];
|
|
|
|
|
memset(entryPArr, 0, sizeof(entryPArr));
|
2022-07-01 01:50:20 +00:00
|
|
|
|
2022-07-06 03:12:45 +00:00
|
|
|
// get entry batch
|
2022-07-04 05:33:49 +00:00
|
|
|
int32_t getCount = 0;
|
|
|
|
|
SyncIndex getEntryIndex = nextIndex;
|
2022-07-07 09:00:01 +00:00
|
|
|
for (int32_t i = 0; i < pSyncNode->pRaftCfg->batchSize; ++i) {
|
2022-07-06 03:12:45 +00:00
|
|
|
SSyncRaftEntry* pEntry = NULL;
|
2022-07-04 05:33:49 +00:00
|
|
|
int32_t code = pSyncNode->pLogStore->syncLogGetEntry(pSyncNode->pLogStore, getEntryIndex, &pEntry);
|
2022-07-01 01:50:20 +00:00
|
|
|
if (code == 0) {
|
|
|
|
|
ASSERT(pEntry != NULL);
|
2022-07-04 05:33:49 +00:00
|
|
|
entryPArr[i] = pEntry;
|
2022-07-01 01:50:20 +00:00
|
|
|
getCount++;
|
2022-07-06 03:12:45 +00:00
|
|
|
getEntryIndex++;
|
|
|
|
|
|
2022-07-01 01:50:20 +00:00
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-07 09:00:01 +00:00
|
|
|
// event log
|
|
|
|
|
do {
|
2022-07-08 06:47:00 +00:00
|
|
|
char logBuf[128];
|
|
|
|
|
char host[64];
|
|
|
|
|
uint16_t port;
|
|
|
|
|
syncUtilU642Addr(pDestId->addr, host, sizeof(host), &port);
|
|
|
|
|
snprintf(logBuf, sizeof(logBuf), "build batch:%d for %s:%d", getCount, host, port);
|
2022-07-07 09:00:01 +00:00
|
|
|
syncNodeEventLog(pSyncNode, logBuf);
|
|
|
|
|
} while (0);
|
|
|
|
|
|
2022-07-06 03:12:45 +00:00
|
|
|
// build msg
|
2022-07-04 05:33:49 +00:00
|
|
|
SyncAppendEntriesBatch* pMsg = syncAppendEntriesBatchBuild(entryPArr, getCount, pSyncNode->vgId);
|
2022-07-01 01:50:20 +00:00
|
|
|
ASSERT(pMsg != NULL);
|
|
|
|
|
|
2022-07-06 03:12:45 +00:00
|
|
|
// free entries
|
2022-07-07 09:00:01 +00:00
|
|
|
for (int32_t i = 0; i < pSyncNode->pRaftCfg->batchSize; ++i) {
|
2022-07-04 05:33:49 +00:00
|
|
|
SSyncRaftEntry* pEntry = entryPArr[i];
|
|
|
|
|
if (pEntry != NULL) {
|
|
|
|
|
syncEntryDestory(pEntry);
|
|
|
|
|
entryPArr[i] = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-01 01:50:20 +00:00
|
|
|
// prepare msg
|
|
|
|
|
pMsg->srcId = pSyncNode->myRaftId;
|
|
|
|
|
pMsg->destId = *pDestId;
|
|
|
|
|
pMsg->term = pSyncNode->pRaftStore->currentTerm;
|
|
|
|
|
pMsg->prevLogIndex = preLogIndex;
|
|
|
|
|
pMsg->prevLogTerm = preLogTerm;
|
|
|
|
|
pMsg->commitIndex = pSyncNode->commitIndex;
|
|
|
|
|
pMsg->privateTerm = 0;
|
|
|
|
|
pMsg->dataCount = getCount;
|
|
|
|
|
|
|
|
|
|
// send msg
|
|
|
|
|
syncNodeAppendEntriesBatch(pSyncNode, pDestId, pMsg);
|
2022-08-08 07:10:32 +00:00
|
|
|
|
|
|
|
|
// speed up
|
2022-08-08 11:46:37 +00:00
|
|
|
if (pMsg->dataCount > 0 && pSyncNode->commitIndex - pMsg->prevLogIndex > SYNC_SLOW_DOWN_RANGE) {
|
2022-08-08 07:10:32 +00:00
|
|
|
ret = 1;
|
|
|
|
|
|
2022-08-08 11:46:37 +00:00
|
|
|
#if 0
|
2022-08-08 07:10:32 +00:00
|
|
|
do {
|
|
|
|
|
char logBuf[128];
|
|
|
|
|
char host[64];
|
|
|
|
|
uint16_t port;
|
|
|
|
|
syncUtilU642Addr(pDestId->addr, host, sizeof(host), &port);
|
2022-08-08 11:46:37 +00:00
|
|
|
snprintf(logBuf, sizeof(logBuf), "maybe speed up for %s:%d, pre-index:%ld", host, port, pMsg->prevLogIndex);
|
2022-08-08 07:10:32 +00:00
|
|
|
syncNodeEventLog(pSyncNode, logBuf);
|
|
|
|
|
} while (0);
|
2022-08-08 11:46:37 +00:00
|
|
|
#endif
|
2022-08-08 07:10:32 +00:00
|
|
|
}
|
2022-08-10 03:26:23 +00:00
|
|
|
|
|
|
|
|
syncAppendEntriesBatchDestroy(pMsg);
|
2022-07-01 01:50:20 +00:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 07:10:32 +00:00
|
|
|
return ret;
|
2022-07-01 01:50:20 +00:00
|
|
|
}
|
2022-08-17 02:34:53 +00:00
|
|
|
#endif
|
2022-07-01 01:50:20 +00:00
|
|
|
|
2022-05-31 09:53:38 +00:00
|
|
|
int32_t syncNodeAppendEntriesPeersSnapshot(SSyncNode* pSyncNode) {
|
2022-06-06 08:02:25 +00:00
|
|
|
ASSERT(pSyncNode->state == TAOS_SYNC_STATE_LEADER);
|
2022-05-31 09:53:38 +00:00
|
|
|
|
2022-06-06 08:02:25 +00:00
|
|
|
syncIndexMgrLog2("begin append entries peers pNextIndex:", pSyncNode->pNextIndex);
|
|
|
|
|
syncIndexMgrLog2("begin append entries peers pMatchIndex:", pSyncNode->pMatchIndex);
|
|
|
|
|
logStoreSimpleLog2("begin append entries peers LogStore:", pSyncNode->pLogStore);
|
2022-05-31 09:53:38 +00:00
|
|
|
|
|
|
|
|
int32_t ret = 0;
|
|
|
|
|
for (int i = 0; i < pSyncNode->peersNum; ++i) {
|
|
|
|
|
SRaftId* pDestId = &(pSyncNode->peersId[i]);
|
|
|
|
|
|
2022-06-06 08:02:25 +00:00
|
|
|
// next index
|
2022-05-31 09:53:38 +00:00
|
|
|
SyncIndex nextIndex = syncIndexMgrGetIndex(pSyncNode->pNextIndex, pDestId);
|
2022-06-06 08:02:25 +00:00
|
|
|
|
|
|
|
|
// pre index, pre term
|
|
|
|
|
SyncIndex preLogIndex = syncNodeGetPreIndex(pSyncNode, nextIndex);
|
|
|
|
|
SyncTerm preLogTerm = syncNodeGetPreTerm(pSyncNode, nextIndex);
|
2022-06-23 02:10:57 +00:00
|
|
|
if (preLogTerm == SYNC_TERM_INVALID) {
|
2022-08-03 06:41:38 +00:00
|
|
|
SyncIndex newNextIndex = syncNodeGetLastIndex(pSyncNode) + 1;
|
|
|
|
|
// SyncIndex newNextIndex = nextIndex + 1;
|
|
|
|
|
|
2022-06-23 02:10:57 +00:00
|
|
|
syncIndexMgrSetIndex(pSyncNode->pNextIndex, pDestId, newNextIndex);
|
|
|
|
|
syncIndexMgrSetIndex(pSyncNode->pMatchIndex, pDestId, SYNC_INDEX_INVALID);
|
2022-08-01 09:23:52 +00:00
|
|
|
sError("vgId:%d, sync get pre term error, nextIndex:%" PRId64 ", update next-index:%" PRId64
|
2022-07-11 02:34:02 +00:00
|
|
|
", match-index:%d, raftid:%" PRId64,
|
2022-06-23 02:10:57 +00:00
|
|
|
pSyncNode->vgId, nextIndex, newNextIndex, SYNC_INDEX_INVALID, pDestId->addr);
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2022-05-31 09:53:38 +00:00
|
|
|
|
2022-06-06 08:02:25 +00:00
|
|
|
// prepare entry
|
|
|
|
|
SyncAppendEntries* pMsg = NULL;
|
2022-06-01 08:30:04 +00:00
|
|
|
|
2022-06-06 08:02:25 +00:00
|
|
|
SSyncRaftEntry* pEntry;
|
|
|
|
|
int32_t code = pSyncNode->pLogStore->syncLogGetEntry(pSyncNode->pLogStore, nextIndex, &pEntry);
|
2022-06-02 03:36:26 +00:00
|
|
|
|
2022-06-21 09:45:08 +00:00
|
|
|
if (code == 0) {
|
|
|
|
|
ASSERT(pEntry != NULL);
|
|
|
|
|
|
2022-06-06 08:02:25 +00:00
|
|
|
pMsg = syncAppendEntriesBuild(pEntry->bytes, pSyncNode->vgId);
|
|
|
|
|
ASSERT(pMsg != NULL);
|
2022-06-01 09:07:14 +00:00
|
|
|
|
2022-06-06 08:02:25 +00:00
|
|
|
// add pEntry into msg
|
|
|
|
|
uint32_t len;
|
|
|
|
|
char* serialized = syncEntrySerialize(pEntry, &len);
|
2022-06-21 08:02:36 +00:00
|
|
|
ASSERT(len == pEntry->bytes);
|
2022-06-06 08:02:25 +00:00
|
|
|
memcpy(pMsg->data, serialized, len);
|
2022-06-02 11:47:06 +00:00
|
|
|
|
2022-06-06 08:02:25 +00:00
|
|
|
taosMemoryFree(serialized);
|
|
|
|
|
syncEntryDestory(pEntry);
|
2022-05-31 09:53:38 +00:00
|
|
|
|
|
|
|
|
} else {
|
2022-06-21 09:45:08 +00:00
|
|
|
if (terrno == TSDB_CODE_WAL_LOG_NOT_EXIST) {
|
|
|
|
|
// no entry in log
|
|
|
|
|
pMsg = syncAppendEntriesBuild(0, pSyncNode->vgId);
|
|
|
|
|
ASSERT(pMsg != NULL);
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
syncNodeLog3("", pSyncNode);
|
|
|
|
|
ASSERT(0);
|
|
|
|
|
}
|
2022-05-31 09:53:38 +00:00
|
|
|
}
|
2022-06-06 08:02:25 +00:00
|
|
|
|
|
|
|
|
// prepare msg
|
|
|
|
|
ASSERT(pMsg != NULL);
|
|
|
|
|
pMsg->srcId = pSyncNode->myRaftId;
|
|
|
|
|
pMsg->destId = *pDestId;
|
|
|
|
|
pMsg->term = pSyncNode->pRaftStore->currentTerm;
|
|
|
|
|
pMsg->prevLogIndex = preLogIndex;
|
|
|
|
|
pMsg->prevLogTerm = preLogTerm;
|
|
|
|
|
pMsg->commitIndex = pSyncNode->commitIndex;
|
2022-06-07 11:20:05 +00:00
|
|
|
pMsg->privateTerm = 0;
|
|
|
|
|
// pMsg->privateTerm = syncIndexMgrGetTerm(pSyncNode->pNextIndex, pDestId);
|
2022-06-06 08:02:25 +00:00
|
|
|
|
|
|
|
|
// send msg
|
|
|
|
|
syncNodeAppendEntries(pSyncNode, pDestId, pMsg);
|
|
|
|
|
syncAppendEntriesDestroy(pMsg);
|
2022-05-31 09:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2022-05-30 05:14:48 +00:00
|
|
|
|
2022-08-08 11:46:37 +00:00
|
|
|
int32_t syncNodeReplicate(SSyncNode* pSyncNode, bool isTimer) {
|
2022-03-08 06:19:50 +00:00
|
|
|
// start replicate
|
2022-05-30 05:14:48 +00:00
|
|
|
int32_t ret = 0;
|
|
|
|
|
|
2022-07-06 03:12:45 +00:00
|
|
|
switch (pSyncNode->pRaftCfg->snapshotStrategy) {
|
|
|
|
|
case SYNC_STRATEGY_NO_SNAPSHOT:
|
|
|
|
|
ret = syncNodeAppendEntriesPeers(pSyncNode);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SYNC_STRATEGY_STANDARD_SNAPSHOT:
|
|
|
|
|
ret = syncNodeAppendEntriesPeersSnapshot(pSyncNode);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SYNC_STRATEGY_WAL_FIRST:
|
|
|
|
|
ret = syncNodeAppendEntriesPeersSnapshot2(pSyncNode);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
ret = syncNodeAppendEntriesPeers(pSyncNode);
|
|
|
|
|
break;
|
2022-05-30 05:14:48 +00:00
|
|
|
}
|
2022-07-06 03:12:45 +00:00
|
|
|
|
2022-08-08 11:46:37 +00:00
|
|
|
// start delay
|
|
|
|
|
int64_t timeNow = taosGetTimestampMs();
|
|
|
|
|
int64_t startDelay = timeNow - pSyncNode->startTime;
|
|
|
|
|
|
|
|
|
|
// replicate delay
|
|
|
|
|
int64_t replicateDelay = timeNow - pSyncNode->lastReplicateTime;
|
|
|
|
|
pSyncNode->lastReplicateTime = timeNow;
|
|
|
|
|
|
|
|
|
|
if (ret > 0 && isTimer && startDelay > SYNC_SPEED_UP_AFTER_MS) {
|
2022-08-08 07:10:32 +00:00
|
|
|
// speed up replicate
|
2022-08-08 11:46:37 +00:00
|
|
|
int32_t ms =
|
|
|
|
|
pSyncNode->heartbeatTimerMS < SYNC_SPEED_UP_HB_TIMER ? pSyncNode->heartbeatTimerMS : SYNC_SPEED_UP_HB_TIMER;
|
2022-08-08 07:10:32 +00:00
|
|
|
syncNodeRestartNowHeartbeatTimerMS(pSyncNode, ms);
|
|
|
|
|
|
2022-08-08 11:46:37 +00:00
|
|
|
#if 0
|
|
|
|
|
do {
|
|
|
|
|
char logBuf[128];
|
|
|
|
|
snprintf(logBuf, sizeof(logBuf), "replicate speed up");
|
|
|
|
|
syncNodeEventLog(pSyncNode, logBuf);
|
|
|
|
|
} while (0);
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-08-08 07:10:32 +00:00
|
|
|
} else {
|
|
|
|
|
syncNodeRestartHeartbeatTimer(pSyncNode);
|
2022-08-08 11:46:37 +00:00
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
do {
|
|
|
|
|
char logBuf[128];
|
|
|
|
|
snprintf(logBuf, sizeof(logBuf), "replicate slow down");
|
|
|
|
|
syncNodeEventLog(pSyncNode, logBuf);
|
|
|
|
|
} while (0);
|
|
|
|
|
#endif
|
2022-08-08 07:10:32 +00:00
|
|
|
}
|
2022-07-19 11:04:11 +00:00
|
|
|
|
2022-03-08 06:19:50 +00:00
|
|
|
return ret;
|
|
|
|
|
}
|
2022-03-07 08:06:07 +00:00
|
|
|
|
|
|
|
|
int32_t syncNodeAppendEntries(SSyncNode* pSyncNode, const SRaftId* destRaftId, const SyncAppendEntries* pMsg) {
|
|
|
|
|
int32_t ret = 0;
|
2022-07-20 09:19:42 +00:00
|
|
|
syncLogSendAppendEntries(pSyncNode, pMsg, "");
|
2022-06-23 08:32:06 +00:00
|
|
|
|
2022-03-07 08:06:07 +00:00
|
|
|
SRpcMsg rpcMsg;
|
|
|
|
|
syncAppendEntries2RpcMsg(pMsg, &rpcMsg);
|
|
|
|
|
syncNodeSendMsgById(destRaftId, pSyncNode, &rpcMsg);
|
|
|
|
|
return ret;
|
2022-07-01 01:50:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t syncNodeAppendEntriesBatch(SSyncNode* pSyncNode, const SRaftId* destRaftId,
|
|
|
|
|
const SyncAppendEntriesBatch* pMsg) {
|
2022-07-20 09:19:42 +00:00
|
|
|
syncLogSendAppendEntriesBatch(pSyncNode, pMsg, "");
|
2022-07-01 01:50:20 +00:00
|
|
|
|
|
|
|
|
SRpcMsg rpcMsg;
|
|
|
|
|
syncAppendEntriesBatch2RpcMsg(pMsg, &rpcMsg);
|
|
|
|
|
syncNodeSendMsgById(destRaftId, pSyncNode, &rpcMsg);
|
|
|
|
|
return 0;
|
2022-03-07 08:06:07 +00:00
|
|
|
}
|