sync refactor
This commit is contained in:
parent
d3b1d5d8f1
commit
2fcc970d79
|
@ -103,6 +103,9 @@ typedef struct SVotesGranted SVotesGranted;
|
||||||
struct SVotesRespond;
|
struct SVotesRespond;
|
||||||
typedef struct SVotesRespond SVotesRespond;
|
typedef struct SVotesRespond SVotesRespond;
|
||||||
|
|
||||||
|
struct SSyncIndexMgr;
|
||||||
|
typedef struct SSyncIndexMgr SSyncIndexMgr;
|
||||||
|
|
||||||
typedef struct SRaftId {
|
typedef struct SRaftId {
|
||||||
SyncNodeId addr; // typedef uint64_t SyncNodeId;
|
SyncNodeId addr; // typedef uint64_t SyncNodeId;
|
||||||
SyncGroupId vgId; // typedef int32_t SyncGroupId;
|
SyncGroupId vgId; // typedef int32_t SyncGroupId;
|
||||||
|
@ -148,8 +151,8 @@ typedef struct SSyncNode {
|
||||||
SVotesRespond* pVotesRespond;
|
SVotesRespond* pVotesRespond;
|
||||||
|
|
||||||
// tla+ leader vars
|
// tla+ leader vars
|
||||||
SHashObj* pNextIndex;
|
SSyncIndexMgr* pNextIndex;
|
||||||
SHashObj* pMatchIndex;
|
SSyncIndexMgr* pMatchIndex;
|
||||||
|
|
||||||
// tla+ log vars
|
// tla+ log vars
|
||||||
SSyncLogStore* pLogStore;
|
SSyncLogStore* pLogStore;
|
||||||
|
|
|
@ -81,8 +81,9 @@ cJSON *syncIndexMgr2Json(SSyncIndexMgr *pSyncIndexMgr) {
|
||||||
for (int i = 0; i < pSyncIndexMgr->replicaNum; ++i) {
|
for (int i = 0; i < pSyncIndexMgr->replicaNum; ++i) {
|
||||||
arr[i] = pSyncIndexMgr->index[i];
|
arr[i] = pSyncIndexMgr->index[i];
|
||||||
}
|
}
|
||||||
cJSON *pIsRespond = cJSON_CreateIntArray(arr, pSyncIndexMgr->replicaNum);
|
cJSON *pIndex = cJSON_CreateIntArray(arr, pSyncIndexMgr->replicaNum);
|
||||||
free(arr);
|
free(arr);
|
||||||
|
cJSON_AddItemToObject(pRoot, "index", pIndex);
|
||||||
snprintf(u64buf, sizeof(u64buf), "%p", pSyncIndexMgr->pSyncNode);
|
snprintf(u64buf, sizeof(u64buf), "%p", pSyncIndexMgr->pSyncNode);
|
||||||
cJSON_AddStringToObject(pRoot, "pSyncNode", u64buf);
|
cJSON_AddStringToObject(pRoot, "pSyncNode", u64buf);
|
||||||
|
|
||||||
|
@ -93,7 +94,7 @@ cJSON *syncIndexMgr2Json(SSyncIndexMgr *pSyncIndexMgr) {
|
||||||
|
|
||||||
char *syncIndexMgr2Str(SSyncIndexMgr *pSyncIndexMgr) {
|
char *syncIndexMgr2Str(SSyncIndexMgr *pSyncIndexMgr) {
|
||||||
cJSON *pJson = syncIndexMgr2Json(pSyncIndexMgr);
|
cJSON *pJson = syncIndexMgr2Json(pSyncIndexMgr);
|
||||||
char *serialized = cJSON_Print(pJson);
|
char * serialized = cJSON_Print(pJson);
|
||||||
cJSON_Delete(pJson);
|
cJSON_Delete(pJson);
|
||||||
return serialized;
|
return serialized;
|
||||||
}
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
#include <gtest/gtest.h>
|
#include "syncIndexMgr.h"
|
||||||
|
//#include <gtest/gtest.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "syncEnv.h"
|
#include "syncEnv.h"
|
||||||
#include "syncIO.h"
|
#include "syncIO.h"
|
||||||
|
@ -97,60 +98,44 @@ int main(int argc, char** argv) {
|
||||||
|
|
||||||
initRaftId(pSyncNode);
|
initRaftId(pSyncNode);
|
||||||
|
|
||||||
SVotesGranted* pVotesGranted = voteGrantedCreate(pSyncNode);
|
SSyncIndexMgr* pSyncIndexMgr = syncIndexMgrCreate(pSyncNode);
|
||||||
assert(pVotesGranted != NULL);
|
assert(pSyncIndexMgr != NULL);
|
||||||
|
|
||||||
printf("---------------------------------------\n");
|
printf("---------------------------------------\n");
|
||||||
{
|
{
|
||||||
char* serialized = voteGranted2Str(pVotesGranted);
|
char* serialized = syncIndexMgr2Str(pSyncIndexMgr);
|
||||||
assert(serialized != NULL);
|
assert(serialized != NULL);
|
||||||
printf("%s\n", serialized);
|
printf("%s\n", serialized);
|
||||||
free(serialized);
|
free(serialized);
|
||||||
}
|
}
|
||||||
|
|
||||||
SyncTerm term = 1234;
|
syncIndexMgrSetIndex(pSyncIndexMgr, &ids[0], 100);
|
||||||
|
syncIndexMgrSetIndex(pSyncIndexMgr, &ids[1], 200);
|
||||||
|
syncIndexMgrSetIndex(pSyncIndexMgr, &ids[2], 300);
|
||||||
|
|
||||||
printf("---------------------------------------\n");
|
printf("---------------------------------------\n");
|
||||||
voteGrantedReset(pVotesGranted, term);
|
|
||||||
{
|
{
|
||||||
char* serialized = voteGranted2Str(pVotesGranted);
|
char* serialized = syncIndexMgr2Str(pSyncIndexMgr);
|
||||||
assert(serialized != NULL);
|
assert(serialized != NULL);
|
||||||
printf("%s\n", serialized);
|
printf("%s\n", serialized);
|
||||||
free(serialized);
|
free(serialized);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < replicaNum; ++i) {
|
printf("---------------------------------------\n");
|
||||||
SyncRequestVoteReply* reply = SyncRequestVoteReplyBuild();
|
for (int i = 0; i < pSyncIndexMgr->replicaNum; ++i) {
|
||||||
reply->destId = pSyncNode->myRaftId;
|
SyncIndex idx = syncIndexMgrGetIndex(pSyncIndexMgr, &ids[i]);
|
||||||
reply->srcId = ids[i];
|
printf("index %d : %lu \n", i, idx);
|
||||||
reply->term = term;
|
|
||||||
reply->voteGranted = true;
|
|
||||||
|
|
||||||
voteGrantedVote(pVotesGranted, reply);
|
|
||||||
{
|
|
||||||
char* serialized = voteGranted2Str(pVotesGranted);
|
|
||||||
assert(serialized != NULL);
|
|
||||||
printf("%s\n", serialized);
|
|
||||||
free(serialized);
|
|
||||||
}
|
|
||||||
|
|
||||||
voteGrantedVote(pVotesGranted, reply);
|
|
||||||
{
|
|
||||||
char* serialized = voteGranted2Str(pVotesGranted);
|
|
||||||
assert(serialized != NULL);
|
|
||||||
printf("%s\n", serialized);
|
|
||||||
free(serialized);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
syncIndexMgrClear(pSyncIndexMgr);
|
||||||
printf("---------------------------------------\n");
|
printf("---------------------------------------\n");
|
||||||
voteGrantedReset(pVotesGranted, 123456789);
|
|
||||||
{
|
{
|
||||||
char* serialized = voteGranted2Str(pVotesGranted);
|
char* serialized = syncIndexMgr2Str(pSyncIndexMgr);
|
||||||
assert(serialized != NULL);
|
assert(serialized != NULL);
|
||||||
printf("%s\n", serialized);
|
printf("%s\n", serialized);
|
||||||
free(serialized);
|
free(serialized);
|
||||||
}
|
}
|
||||||
|
|
||||||
voteGrantedDestroy(pVotesGranted);
|
syncIndexMgrDestroy(pSyncIndexMgr);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue