fix: send snapshot
This commit is contained in:
parent
ae06cc3696
commit
757a1248dc
|
@ -157,6 +157,7 @@ typedef struct SSyncNode {
|
||||||
// SSnapshot* pSnapshot;
|
// SSnapshot* pSnapshot;
|
||||||
SSyncSnapshotSender* senders[TSDB_MAX_REPLICA];
|
SSyncSnapshotSender* senders[TSDB_MAX_REPLICA];
|
||||||
SSyncSnapshotReceiver* receivers[TSDB_MAX_REPLICA];
|
SSyncSnapshotReceiver* receivers[TSDB_MAX_REPLICA];
|
||||||
|
SSyncSnapshotReceiver* pNewNodeReceiver;
|
||||||
|
|
||||||
} SSyncNode;
|
} SSyncNode;
|
||||||
|
|
||||||
|
|
|
@ -319,6 +319,23 @@ static void *syncIOConsumerFunc(void *param) {
|
||||||
io->FpOnSyncTimeout(io->pSyncNode, pSyncMsg);
|
io->FpOnSyncTimeout(io->pSyncNode, pSyncMsg);
|
||||||
syncTimeoutDestroy(pSyncMsg);
|
syncTimeoutDestroy(pSyncMsg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} else if (pRpcMsg->msgType == TDMT_VND_SYNC_SNAPSHOT_SEND) {
|
||||||
|
if (io->FpOnSyncSnapshotSend != NULL) {
|
||||||
|
SyncSnapshotSend *pSyncMsg = syncSnapshotSendFromRpcMsg2(pRpcMsg);
|
||||||
|
assert(pSyncMsg != NULL);
|
||||||
|
io->FpOnSyncSnapshotSend(io->pSyncNode, pSyncMsg);
|
||||||
|
syncSnapshotSendDestroy(pSyncMsg);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (pRpcMsg->msgType == TDMT_VND_SYNC_SNAPSHOT_RSP) {
|
||||||
|
if (io->FpOnSyncSnapshotRsp != NULL) {
|
||||||
|
SyncSnapshotRsp *pSyncMsg = syncSnapshotRspFromRpcMsg2(pRpcMsg);
|
||||||
|
assert(pSyncMsg != NULL);
|
||||||
|
io->FpOnSyncSnapshotRsp(io->pSyncNode, pSyncMsg);
|
||||||
|
syncSnapshotRspDestroy(pSyncMsg);
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
sTrace("unknown msgType:%d, no operator", pRpcMsg->msgType);
|
sTrace("unknown msgType:%d, no operator", pRpcMsg->msgType);
|
||||||
}
|
}
|
||||||
|
|
|
@ -601,6 +601,8 @@ SSyncNode* syncNodeOpen(const SSyncInfo* pOldSyncInfo) {
|
||||||
(pSyncNode->receivers)[i] = pReceiver;
|
(pSyncNode->receivers)[i] = pReceiver;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pSyncNode->pNewNodeReceiver = snapshotReceiverCreate(pSyncNode, 100);
|
||||||
|
|
||||||
// start in syncNodeStart
|
// start in syncNodeStart
|
||||||
// start raft
|
// start raft
|
||||||
// syncNodeBecomeFollower(pSyncNode);
|
// syncNodeBecomeFollower(pSyncNode);
|
||||||
|
@ -611,6 +613,7 @@ SSyncNode* syncNodeOpen(const SSyncInfo* pOldSyncInfo) {
|
||||||
void syncNodeStart(SSyncNode* pSyncNode) {
|
void syncNodeStart(SSyncNode* pSyncNode) {
|
||||||
// start raft
|
// start raft
|
||||||
if (pSyncNode->replicaNum == 1) {
|
if (pSyncNode->replicaNum == 1) {
|
||||||
|
raftStoreNextTerm(pSyncNode->pRaftStore);
|
||||||
syncNodeBecomeLeader(pSyncNode);
|
syncNodeBecomeLeader(pSyncNode);
|
||||||
|
|
||||||
syncNodeLog2("==state change become leader immediately==", pSyncNode);
|
syncNodeLog2("==state change become leader immediately==", pSyncNode);
|
||||||
|
@ -706,6 +709,11 @@ void syncNodeClose(SSyncNode* pSyncNode) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (pSyncNode->pNewNodeReceiver != NULL) {
|
||||||
|
snapshotReceiverDestroy(pSyncNode->pNewNodeReceiver);
|
||||||
|
pSyncNode->pNewNodeReceiver = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
if (pSyncNode->pSnapshot != NULL) {
|
if (pSyncNode->pSnapshot != NULL) {
|
||||||
taosMemoryFree(pSyncNode->pSnapshot);
|
taosMemoryFree(pSyncNode->pSnapshot);
|
||||||
|
@ -1294,7 +1302,7 @@ int32_t syncNodeGetLastIndexTerm(SSyncNode* pSyncNode, SyncIndex* pLastIndex, Sy
|
||||||
// get pre index and term of "index"
|
// get pre index and term of "index"
|
||||||
int32_t syncNodeGetPreIndexTerm(SSyncNode* pSyncNode, SyncIndex index, SyncIndex* pPreIndex, SyncTerm* pPreTerm) {
|
int32_t syncNodeGetPreIndexTerm(SSyncNode* pSyncNode, SyncIndex index, SyncIndex* pPreIndex, SyncTerm* pPreTerm) {
|
||||||
ASSERT(index >= SYNC_INDEX_BEGIN);
|
ASSERT(index >= SYNC_INDEX_BEGIN);
|
||||||
ASSERT(!syncNodeIsIndexInSnapshot(pSyncNode, index));
|
// ASSERT(!syncNodeIsIndexInSnapshot(pSyncNode, index));
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
SyncIndex preIndex = index - 1;
|
SyncIndex preIndex = index - 1;
|
||||||
|
|
|
@ -65,6 +65,16 @@ cJSON* syncRpcMsg2Json(SRpcMsg* pRpcMsg) {
|
||||||
pRoot = syncAppendEntriesReply2Json(pSyncMsg);
|
pRoot = syncAppendEntriesReply2Json(pSyncMsg);
|
||||||
syncAppendEntriesReplyDestroy(pSyncMsg);
|
syncAppendEntriesReplyDestroy(pSyncMsg);
|
||||||
|
|
||||||
|
} else if (pRpcMsg->msgType == TDMT_VND_SYNC_SNAPSHOT_SEND) {
|
||||||
|
SyncSnapshotSend* pSyncMsg = syncSnapshotSendDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
|
||||||
|
pRoot = syncSnapshotSend2Json(pSyncMsg);
|
||||||
|
syncSnapshotSendDestroy(pSyncMsg);
|
||||||
|
|
||||||
|
} else if (pRpcMsg->msgType == TDMT_VND_SYNC_SNAPSHOT_RSP) {
|
||||||
|
SyncSnapshotRsp* pSyncMsg = syncSnapshotRspDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
|
||||||
|
pRoot = syncSnapshotRsp2Json(pSyncMsg);
|
||||||
|
syncSnapshotRspDestroy(pSyncMsg);
|
||||||
|
|
||||||
} else if (pRpcMsg->msgType == TDMT_VND_SYNC_COMMON_RESPONSE) {
|
} else if (pRpcMsg->msgType == TDMT_VND_SYNC_COMMON_RESPONSE) {
|
||||||
pRoot = cJSON_CreateObject();
|
pRoot = cJSON_CreateObject();
|
||||||
char* s;
|
char* s;
|
||||||
|
|
|
@ -61,6 +61,7 @@ int32_t syncNodeAppendEntriesPeers(SSyncNode* pSyncNode) {
|
||||||
|
|
||||||
// set prevLogIndex
|
// set prevLogIndex
|
||||||
SyncIndex nextIndex = syncIndexMgrGetIndex(pSyncNode->pNextIndex, pDestId);
|
SyncIndex nextIndex = syncIndexMgrGetIndex(pSyncNode->pNextIndex, pDestId);
|
||||||
|
|
||||||
SyncIndex preLogIndex = nextIndex - 1;
|
SyncIndex preLogIndex = nextIndex - 1;
|
||||||
|
|
||||||
// set preLogTerm
|
// set preLogTerm
|
||||||
|
@ -127,13 +128,9 @@ int32_t syncNodeAppendEntriesPeersSnapshot(SSyncNode* pSyncNode) {
|
||||||
SRaftId* pDestId = &(pSyncNode->peersId[i]);
|
SRaftId* pDestId = &(pSyncNode->peersId[i]);
|
||||||
|
|
||||||
SyncIndex nextIndex = syncIndexMgrGetIndex(pSyncNode->pNextIndex, pDestId);
|
SyncIndex nextIndex = syncIndexMgrGetIndex(pSyncNode->pNextIndex, pDestId);
|
||||||
|
|
||||||
SyncIndex preLogIndex;
|
SyncIndex preLogIndex;
|
||||||
SyncTerm preLogTerm;
|
SyncTerm preLogTerm;
|
||||||
|
|
||||||
ret = syncNodeGetPreIndexTerm(pSyncNode, nextIndex, &preLogIndex, &preLogTerm);
|
|
||||||
ASSERT(ret == 0);
|
|
||||||
|
|
||||||
// batch optimized
|
// batch optimized
|
||||||
// SyncIndex lastIndex = syncUtilMinIndex(pSyncNode->pLogStore->getLastIndex(pSyncNode->pLogStore), nextIndex);
|
// SyncIndex lastIndex = syncUtilMinIndex(pSyncNode->pLogStore->getLastIndex(pSyncNode->pLogStore), nextIndex);
|
||||||
|
|
||||||
|
@ -181,6 +178,9 @@ int32_t syncNodeAppendEntriesPeersSnapshot(SSyncNode* pSyncNode) {
|
||||||
snapshotSenderStart(pSender);
|
snapshotSenderStart(pSender);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
ret = syncNodeGetPreIndexTerm(pSyncNode, nextIndex, &preLogIndex, &preLogTerm);
|
||||||
|
ASSERT(ret == 0);
|
||||||
|
|
||||||
SyncAppendEntries* pMsg = NULL;
|
SyncAppendEntries* pMsg = NULL;
|
||||||
SSyncRaftEntry* pEntry = pSyncNode->pLogStore->getEntry(pSyncNode->pLogStore, nextIndex);
|
SSyncRaftEntry* pEntry = pSyncNode->pLogStore->getEntry(pSyncNode->pLogStore, nextIndex);
|
||||||
if (pEntry != NULL) {
|
if (pEntry != NULL) {
|
||||||
|
|
|
@ -407,7 +407,11 @@ int32_t syncNodeOnSnapshotSendCb(SSyncNode *pSyncNode, SyncSnapshotSend *pMsg) {
|
||||||
pReceiver = (pSyncNode->receivers)[i];
|
pReceiver = (pSyncNode->receivers)[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ASSERT(pReceiver != NULL);
|
|
||||||
|
// add new replica
|
||||||
|
if (pReceiver == NULL) {
|
||||||
|
pReceiver = pSyncNode->pNewNodeReceiver;
|
||||||
|
}
|
||||||
|
|
||||||
bool needRsp = false;
|
bool needRsp = false;
|
||||||
|
|
||||||
|
@ -430,7 +434,7 @@ int32_t syncNodeOnSnapshotSendCb(SSyncNode *pSyncNode, SyncSnapshotSend *pMsg) {
|
||||||
pSyncNode->pFsm->FpSnapshotStopWrite(pSyncNode->pFsm, pReceiver->pWriter, true);
|
pSyncNode->pFsm->FpSnapshotStopWrite(pSyncNode->pFsm, pReceiver->pWriter, true);
|
||||||
snapshotReceiverStop(pReceiver);
|
snapshotReceiverStop(pReceiver);
|
||||||
pReceiver->ack = pMsg->seq;
|
pReceiver->ack = pMsg->seq;
|
||||||
needRsp = true;
|
needRsp = false;
|
||||||
|
|
||||||
char *msgStr = syncSnapshotSend2Str(pMsg);
|
char *msgStr = syncSnapshotSend2Str(pMsg);
|
||||||
sTrace("snapshot recv end ack:%d recv msg:%s", pReceiver->ack, msgStr);
|
sTrace("snapshot recv end ack:%d recv msg:%s", pReceiver->ack, msgStr);
|
||||||
|
|
|
@ -20,6 +20,7 @@ uint16_t gPorts[] = {7010, 7110, 7210, 7310, 7410};
|
||||||
const char* gDir = "./syncReplicateTest";
|
const char* gDir = "./syncReplicateTest";
|
||||||
int32_t gVgId = 1234;
|
int32_t gVgId = 1234;
|
||||||
SyncIndex gSnapshotLastApplyIndex;
|
SyncIndex gSnapshotLastApplyIndex;
|
||||||
|
SyncIndex gSnapshotLastApplyTerm;
|
||||||
|
|
||||||
void init() {
|
void init() {
|
||||||
int code = walInit();
|
int code = walInit();
|
||||||
|
@ -44,8 +45,9 @@ void CommitCb(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SFsmCbMeta cbMeta) {
|
||||||
if (cbMeta.index > beginIndex) {
|
if (cbMeta.index > beginIndex) {
|
||||||
char logBuf[256] = {0};
|
char logBuf[256] = {0};
|
||||||
snprintf(logBuf, sizeof(logBuf),
|
snprintf(logBuf, sizeof(logBuf),
|
||||||
"==callback== ==CommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s flag:%lu\n", pFsm,
|
"==callback== ==CommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s, flag:%lu, term:%lu \n",
|
||||||
cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state), cbMeta.flag);
|
pFsm, cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state),
|
||||||
|
cbMeta.flag, cbMeta.term);
|
||||||
syncRpcMsgLog2(logBuf, (SRpcMsg*)pMsg);
|
syncRpcMsgLog2(logBuf, (SRpcMsg*)pMsg);
|
||||||
} else {
|
} else {
|
||||||
sTrace("==callback== ==CommitCb== do not apply again %ld", cbMeta.index);
|
sTrace("==callback== ==CommitCb== do not apply again %ld", cbMeta.index);
|
||||||
|
@ -71,7 +73,7 @@ void RollBackCb(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SFsmCbMeta cbMeta) {
|
||||||
int32_t GetSnapshotCb(struct SSyncFSM* pFsm, SSnapshot* pSnapshot) {
|
int32_t GetSnapshotCb(struct SSyncFSM* pFsm, SSnapshot* pSnapshot) {
|
||||||
pSnapshot->data = NULL;
|
pSnapshot->data = NULL;
|
||||||
pSnapshot->lastApplyIndex = gSnapshotLastApplyIndex;
|
pSnapshot->lastApplyIndex = gSnapshotLastApplyIndex;
|
||||||
pSnapshot->lastApplyTerm = 100;
|
pSnapshot->lastApplyTerm = gSnapshotLastApplyTerm;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,17 +96,18 @@ int32_t SnapshotDoRead(struct SSyncFSM* pFsm, void* pReader, void** ppBuf, int32
|
||||||
static int readIter = 0;
|
static int readIter = 0;
|
||||||
|
|
||||||
if (readIter == 5) {
|
if (readIter == 5) {
|
||||||
|
*len = 0;
|
||||||
|
*ppBuf = NULL;
|
||||||
|
} else if (readIter < 5) {
|
||||||
*len = 20;
|
*len = 20;
|
||||||
*ppBuf = taosMemoryMalloc(*len);
|
*ppBuf = taosMemoryMalloc(*len);
|
||||||
snprintf((char*)*ppBuf, *len, "data iter:%d", readIter);
|
snprintf((char*)*ppBuf, *len, "data iter:%d", readIter);
|
||||||
} else {
|
|
||||||
*len = 0;
|
|
||||||
*ppBuf = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char logBuf[256] = {0};
|
char logBuf[256] = {0};
|
||||||
snprintf(logBuf, sizeof(logBuf), "==callback== ==SnapshotDoRead== pFsm:%p, pReader:%p, *len:%d *ppBuf:%s", pFsm,
|
snprintf(logBuf, sizeof(logBuf),
|
||||||
pReader, *len, (char*)(*ppBuf));
|
"==callback== ==SnapshotDoRead== pFsm:%p, pReader:%p, *len:%d, *ppBuf:%s, readIter:%d", pFsm, pReader, *len,
|
||||||
|
(char*)(*ppBuf), readIter);
|
||||||
sTrace("%s", logBuf);
|
sTrace("%s", logBuf);
|
||||||
|
|
||||||
readIter++;
|
readIter++;
|
||||||
|
@ -249,7 +252,7 @@ void configChange(int64_t rid, int32_t replicaNum, int32_t myIndex) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void usage(char* exe) {
|
void usage(char* exe) {
|
||||||
printf("usage: %s replicaNum myIndex lastApplyIndex writeRecordNum isStandBy isConfigChange \n", exe);
|
printf("usage: %s replicaNum myIndex lastApplyIndex writeRecordNum isStandBy isConfigChange lastApplyTerm \n", exe);
|
||||||
}
|
}
|
||||||
|
|
||||||
SRpcMsg* createRpcMsg(int i, int count, int myIndex) {
|
SRpcMsg* createRpcMsg(int i, int count, int myIndex) {
|
||||||
|
@ -265,7 +268,7 @@ SRpcMsg* createRpcMsg(int i, int count, int myIndex) {
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
tsAsyncLog = 0;
|
tsAsyncLog = 0;
|
||||||
sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE + DEBUG_INFO;
|
sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE + DEBUG_INFO;
|
||||||
if (argc != 7) {
|
if (argc != 8) {
|
||||||
usage(argv[0]);
|
usage(argv[0]);
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
@ -276,7 +279,15 @@ int main(int argc, char** argv) {
|
||||||
int32_t writeRecordNum = atoi(argv[4]);
|
int32_t writeRecordNum = atoi(argv[4]);
|
||||||
bool isStandBy = atoi(argv[5]);
|
bool isStandBy = atoi(argv[5]);
|
||||||
bool isConfigChange = atoi(argv[6]);
|
bool isConfigChange = atoi(argv[6]);
|
||||||
|
int32_t lastApplyTerm = atoi(argv[7]);
|
||||||
|
|
||||||
|
sTrace(
|
||||||
|
"args: replicaNum:%d, myIndex:%d, lastApplyIndex:%d, writeRecordNum:%d, isStandBy:%d, isConfigChange:%d, "
|
||||||
|
"lastApplyTerm:%d",
|
||||||
|
replicaNum, myIndex, lastApplyIndex, writeRecordNum, isStandBy, isConfigChange, lastApplyTerm);
|
||||||
|
|
||||||
gSnapshotLastApplyIndex = lastApplyIndex;
|
gSnapshotLastApplyIndex = lastApplyIndex;
|
||||||
|
gSnapshotLastApplyTerm = lastApplyTerm;
|
||||||
|
|
||||||
if (!isStandBy) {
|
if (!isStandBy) {
|
||||||
assert(replicaNum >= 1 && replicaNum <= 5);
|
assert(replicaNum >= 1 && replicaNum <= 5);
|
||||||
|
|
Loading…
Reference in New Issue