refact: improve code of tsdbSnapGetDetails as tsdbSnapPrepDescription
This commit is contained in:
parent
ed39c9a572
commit
f68804322c
|
@ -1041,7 +1041,7 @@ typedef enum {
|
||||||
|
|
||||||
// utils
|
// utils
|
||||||
ETsdbFsState tsdbSnapGetFsState(SVnode *pVnode);
|
ETsdbFsState tsdbSnapGetFsState(SVnode *pVnode);
|
||||||
int32_t tsdbSnapGetDetails(SVnode *pVnode, SSnapshot *pSnap);
|
int32_t tsdbSnapPrepDescription(SVnode *pVnode, SSnapshot *pSnap);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
@ -381,69 +381,110 @@ ETsdbFsState tsdbSnapGetFsState(SVnode* pVnode) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// description
|
// description
|
||||||
int32_t tsdbSnapGetDetails(SVnode* pVnode, SSnapshot* pSnap) {
|
typedef struct STsdbPartitionInfo {
|
||||||
int code = -1;
|
int32_t vgId;
|
||||||
int32_t tsdbMaxCnt = (!VND_IS_RSMA(pVnode) ? 1 : TSDB_RETENTION_MAX);
|
int32_t tsdbMaxCnt;
|
||||||
int32_t subTyps[TSDB_RETENTION_MAX] = {SNAP_DATA_TSDB, SNAP_DATA_RSMA1, SNAP_DATA_RSMA2};
|
int32_t subTyps[TSDB_RETENTION_MAX];
|
||||||
STsdbFSetPartList* pLists[TSDB_RETENTION_MAX] = {0};
|
STsdbFSetPartList* pLists[TSDB_RETENTION_MAX];
|
||||||
|
} STsdbPartitionInfo;
|
||||||
|
|
||||||
// get part list
|
static int32_t tsdbPartitionInfoInit(SVnode* pVnode, STsdbPartitionInfo* pInfo) {
|
||||||
for (int32_t j = 0; j < tsdbMaxCnt; ++j) {
|
int32_t subTyps[TSDB_RETENTION_MAX] = {SNAP_DATA_TSDB, SNAP_DATA_RSMA1, SNAP_DATA_RSMA2};
|
||||||
|
pInfo->vgId = TD_VID(pVnode);
|
||||||
|
pInfo->tsdbMaxCnt = (!VND_IS_RSMA(pVnode) ? 1 : TSDB_RETENTION_MAX);
|
||||||
|
|
||||||
|
ASSERT(sizeof(pInfo->subTyps) == sizeof(subTyps));
|
||||||
|
memcpy(pInfo->subTyps, (char*)subTyps, sizeof(subTyps));
|
||||||
|
|
||||||
|
// fset partition list
|
||||||
|
memset(pInfo->pLists, 0, sizeof(pInfo->pLists[0]) * TSDB_RETENTION_MAX);
|
||||||
|
for (int32_t j = 0; j < pInfo->tsdbMaxCnt; ++j) {
|
||||||
STsdb* pTsdb = SMA_RSMA_GET_TSDB(pVnode, j);
|
STsdb* pTsdb = SMA_RSMA_GET_TSDB(pVnode, j);
|
||||||
pLists[j] = tsdbSnapGetFSetPartList(pTsdb->pFS);
|
pInfo->pLists[j] = tsdbSnapGetFSetPartList(pTsdb->pFS);
|
||||||
if (pLists[j] == NULL) goto _out;
|
if (pInfo->pLists[j] == NULL) return -1;
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// estimate bufLen and prepare
|
static void tsdbPartitionInfoClear(STsdbPartitionInfo* pInfo) {
|
||||||
int32_t bufLen = sizeof(SSyncTLV); // typ: TDMT_SYNC_PREP_SNAPSHOT or TDMT_SYNC_PREP_SNAPSOT_REPLY
|
for (int32_t j = 0; j < pInfo->tsdbMaxCnt; ++j) {
|
||||||
for (int32_t j = 0; j < tsdbMaxCnt; ++j) {
|
if (pInfo->pLists[j] == NULL) continue;
|
||||||
bufLen += sizeof(SSyncTLV); // subTyps[j]
|
tsdbFSetPartListDestroy(&pInfo->pLists[j]);
|
||||||
bufLen += tTsdbFSetPartListDataLenCalc(pLists[j]);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
tsdbInfo("vgId:%d, allocate %d bytes for data of snapshot info.", TD_VID(pVnode), bufLen);
|
static int32_t tsdbPartitionInfoEstSize(STsdbPartitionInfo* pInfo) {
|
||||||
|
int32_t dataLen = 0;
|
||||||
|
for (int32_t j = 0; j < pInfo->tsdbMaxCnt; ++j) {
|
||||||
|
dataLen += sizeof(SSyncTLV); // subTyps[j]
|
||||||
|
dataLen += tTsdbFSetPartListDataLenCalc(pInfo->pLists[j]);
|
||||||
|
}
|
||||||
|
return dataLen;
|
||||||
|
}
|
||||||
|
|
||||||
void* data = taosMemoryRealloc(pSnap->data, bufLen);
|
static int32_t tsdbPartitionInfoSerialize(STsdbPartitionInfo* pInfo, uint8_t* buf, int32_t bufLen, int32_t* offset) {
|
||||||
|
int32_t tlen = 0;
|
||||||
|
for (int32_t j = 0; j < pInfo->tsdbMaxCnt; ++j) {
|
||||||
|
SSyncTLV* pSubHead = (void*)((char*)buf + offset[0]);
|
||||||
|
int32_t valOffset = offset[0] + sizeof(*pSubHead);
|
||||||
|
ASSERT(pSubHead->val == (char*)buf + valOffset);
|
||||||
|
if ((tlen = tSerializeTsdbFSetPartList(pSubHead->val, bufLen - valOffset, pInfo->pLists[j])) < 0) {
|
||||||
|
tsdbError("vgId:%d, failed to serialize fset partition list of tsdb %d since %s", pInfo->vgId, j, terrstr());
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
pSubHead->typ = pInfo->subTyps[j];
|
||||||
|
pSubHead->len = tlen;
|
||||||
|
offset[0] += sizeof(*pSubHead) + tlen;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t syncSnapInfoDataRealloc(SSnapshot* pSnap, int32_t size) {
|
||||||
|
void* data = taosMemoryRealloc(pSnap->data, size);
|
||||||
if (data == NULL) {
|
if (data == NULL) {
|
||||||
terrno = TSDB_CODE_OUT_OF_MEMORY;
|
terrno = TSDB_CODE_OUT_OF_MEMORY;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
pSnap->data = data;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t tsdbSnapPrepDescription(SVnode* pVnode, SSnapshot* pSnap) {
|
||||||
|
ASSERT(pSnap->type == TDMT_SYNC_PREP_SNAPSHOT || pSnap->type == TDMT_SYNC_PREP_SNAPSHOT_REPLY);
|
||||||
|
STsdbPartitionInfo partitionInfo = {0};
|
||||||
|
int code = -1;
|
||||||
|
STsdbPartitionInfo* pInfo = &partitionInfo;
|
||||||
|
|
||||||
|
if (tsdbPartitionInfoInit(pVnode, pInfo) != 0) {
|
||||||
|
goto _out;
|
||||||
|
}
|
||||||
|
|
||||||
|
// info data realloc
|
||||||
|
int32_t bufLen = sizeof(SSyncTLV);
|
||||||
|
bufLen += tsdbPartitionInfoEstSize(pInfo);
|
||||||
|
if (syncSnapInfoDataRealloc(pSnap, bufLen) != 0) {
|
||||||
tsdbError("vgId:%d, failed to realloc memory for data of snapshot info. bytes:%d", TD_VID(pVnode), bufLen);
|
tsdbError("vgId:%d, failed to realloc memory for data of snapshot info. bytes:%d", TD_VID(pVnode), bufLen);
|
||||||
goto _out;
|
goto _out;
|
||||||
}
|
}
|
||||||
pSnap->data = data;
|
|
||||||
|
|
||||||
// header
|
// serialization
|
||||||
SSyncTLV* head = data;
|
SSyncTLV* pHead = pSnap->data;
|
||||||
head->len = 0;
|
pHead->typ = pSnap->type;
|
||||||
head->typ = pSnap->type;
|
|
||||||
int32_t offset = sizeof(SSyncTLV);
|
|
||||||
int32_t tlen = 0;
|
|
||||||
|
|
||||||
// fill snapshot info
|
int32_t offset = 0;
|
||||||
for (int32_t j = 0; j < tsdbMaxCnt; ++j) {
|
if (tsdbPartitionInfoSerialize(pInfo, pHead->val, bufLen - sizeof(*pHead), &offset) != 0) {
|
||||||
// subHead
|
tsdbError("vgId:%d, failed to serialize tsdb partition info since %s", TD_VID(pVnode), terrstr());
|
||||||
SSyncTLV* subHead = (void*)((char*)data + offset);
|
goto _out;
|
||||||
subHead->typ = subTyps[j];
|
|
||||||
ASSERT(subHead->val == (char*)data + offset + sizeof(SSyncTLV));
|
|
||||||
|
|
||||||
if ((tlen = tSerializeTsdbFSetPartList(subHead->val, bufLen - offset - sizeof(SSyncTLV), pLists[j])) < 0) {
|
|
||||||
tsdbError("vgId:%d, failed to serialize snap partition list of tsdb %d since %s", TD_VID(pVnode), j, terrstr());
|
|
||||||
goto _out;
|
|
||||||
}
|
|
||||||
subHead->len = tlen;
|
|
||||||
offset += sizeof(SSyncTLV) + tlen;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// total length of subfields
|
// set header of info data
|
||||||
head->len = offset - sizeof(SSyncTLV);
|
ASSERT(sizeof(*pHead) + offset <= bufLen);
|
||||||
ASSERT(offset <= bufLen);
|
pHead->len = offset;
|
||||||
|
|
||||||
|
tsdbInfo("vgId:%d, tsdb snap info prepared. type:%s, val length:%d", TD_VID(pVnode), TMSG_INFO(pHead->typ),
|
||||||
|
pHead->len);
|
||||||
code = 0;
|
code = 0;
|
||||||
|
|
||||||
_out:
|
_out:
|
||||||
for (int32_t j = 0; j < tsdbMaxCnt; ++j) {
|
tsdbPartitionInfoClear(pInfo);
|
||||||
if (pLists[j] == NULL) continue;
|
|
||||||
tsdbFSetPartListDestroy(&pLists[j]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -804,7 +804,7 @@ int32_t vnodeGetSnapshot(SVnode *pVnode, SSnapshot *pSnap) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pSnap->type == TDMT_SYNC_PREP_SNAPSHOT || pSnap->type == TDMT_SYNC_PREP_SNAPSHOT_REPLY) {
|
if (pSnap->type == TDMT_SYNC_PREP_SNAPSHOT || pSnap->type == TDMT_SYNC_PREP_SNAPSHOT_REPLY) {
|
||||||
code = tsdbSnapGetDetails(pVnode, pSnap);
|
code = tsdbSnapPrepDescription(pVnode, pSnap);
|
||||||
}
|
}
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue