diff --git a/src/inc/mnode.h b/src/inc/mnode.h index d804c78f7985d83251a543bf1875b6a10e791f82..c7f4b07efa53175a1be1a648b4bb9b2e7ba6c322 100644 --- a/src/inc/mnode.h +++ b/src/inc/mnode.h @@ -59,6 +59,7 @@ typedef struct { char mnodeName[TSDB_DNODE_NAME_LEN + 1]; int8_t reserved[15]; int8_t updateEnd[1]; + int32_t refCount; int syncFd; void *hbTimer; void *pSync; @@ -84,6 +85,7 @@ typedef struct { char dnodeName[TSDB_DNODE_NAME_LEN + 1]; int8_t reserved[15]; int8_t updateEnd[1]; + int32_t refCount; SVnodeLoad vload[TSDB_MAX_VNODES]; int32_t status; uint32_t lastReboot; // time stamp for last reboot @@ -115,6 +117,7 @@ typedef struct SSuperTableObj { int32_t numOfTags; int8_t reserved[15]; int8_t updateEnd[1]; + int32_t refCount; int32_t numOfTables; int16_t nextColId; SSchema * schema; @@ -133,6 +136,7 @@ typedef struct { int8_t reserved[1]; int8_t updateEnd[1]; int16_t nextColId; //used by normal table + int32_t refCount; char* sql; //used by normal table SSchema* schema; //used by normal table SSuperTableObj *superTable; @@ -149,6 +153,7 @@ typedef struct _vg_obj { int8_t lbStatus; int8_t reserved[14]; int8_t updateEnd[1]; + int32_t refCount; struct _vg_obj *prev, *next; struct _db_obj *pDb; int32_t numOfTables; @@ -163,6 +168,7 @@ typedef struct _db_obj { SDbCfg cfg; int8_t reserved[15]; int8_t updateEnd[1]; + int32_t refCount; struct _db_obj *prev, *next; int32_t numOfVgroups; int32_t numOfTables; @@ -181,6 +187,7 @@ typedef struct _user_obj { int8_t writeAuth; int8_t reserved[13]; int8_t updateEnd[1]; + int32_t refCount; struct _acctObj * pAcct; SQqueryList * pQList; // query list SStreamList * pSList; // stream list @@ -213,6 +220,7 @@ typedef struct _acctObj { int8_t dirty; int8_t reserved[14]; int8_t updateEnd[1]; + int32_t refCount; SAcctInfo acctInfo; SDbObj * pHead; pthread_mutex_t mutex; diff --git a/src/mnode/inc/mgmtSdb.h b/src/mnode/inc/mgmtSdb.h index d986170d0ae2fecd326ea9bb94b74654810dd46a..ecf6887b42d105875add725def872ea1cdebb6fc 100644 --- a/src/mnode/inc/mgmtSdb.h +++ b/src/mnode/inc/mgmtSdb.h @@ -44,6 +44,7 @@ typedef struct { char *tableName; int32_t hashSessions; int32_t maxRowSize; + int32_t refCountPos; ESdbKeyType keyType; int32_t (*insertFp)(SSdbOperDesc *pOper); int32_t (*deleteFp)(SSdbOperDesc *pOper); diff --git a/src/mnode/src/mgmtAcct.c b/src/mnode/src/mgmtAcct.c index 5c42e5752ad5cf8a908245693be011e4c447facd..0d863e5249cf5d43e2a8cb448ecb260ffb139c22 100644 --- a/src/mnode/src/mgmtAcct.c +++ b/src/mnode/src/mgmtAcct.c @@ -51,7 +51,7 @@ void acctAddDb(SAcctObj *pAcct, SDbObj *pDb) { pAcct->acctInfo.numOfDbs++; pthread_mutex_unlock(&pAcct->mutex); - mgmtIncDbRef(pDb); + acctIncRef(pAcct); } void acctRemoveDb(SAcctObj *pAcct, SDbObj *pDb) { @@ -71,7 +71,7 @@ void acctRemoveDb(SAcctObj *pAcct, SDbObj *pDb) { pAcct->acctInfo.numOfDbs--; pthread_mutex_unlock(&pAcct->mutex); - mgmtDecDbRef(pDb); + acctDecRef(pAcct); } void acctAddUser(SAcctObj *pAcct, SUserObj *pUser) { @@ -80,7 +80,7 @@ void acctAddUser(SAcctObj *pAcct, SUserObj *pUser) { pUser->pAcct = pAcct; pthread_mutex_unlock(&pAcct->mutex); - mgmtIncUserRef(pUser); + acctIncRef(pAcct); } void acctRemoveUser(SAcctObj *pAcct, SUserObj *pUser) { @@ -89,5 +89,5 @@ void acctRemoveUser(SAcctObj *pAcct, SUserObj *pUser) { pUser->pAcct = NULL; pthread_mutex_unlock(&pAcct->mutex); - mgmtDecUserRef(pUser); + acctDecRef(pAcct); } \ No newline at end of file diff --git a/src/mnode/src/mgmtDb.c b/src/mnode/src/mgmtDb.c index 338eccf0611c15eb3857e70c333d98967b4cc460..69c61531a07f19c5bd16a217702ce7bd5bfac883 100644 --- a/src/mnode/src/mgmtDb.c +++ b/src/mnode/src/mgmtDb.c @@ -117,6 +117,7 @@ int32_t mgmtInitDbs() { .tableName = "dbs", .hashSessions = TSDB_MAX_DBS, .maxRowSize = tsDbUpdateSize, + .refCountPos = (int8_t *)(&tObj.refCount) - (int8_t *)&tObj, .keyType = SDB_KEY_TYPE_STRING, .insertFp = mgmtDbActionInsert, .deleteFp = mgmtDbActionDelete, diff --git a/src/mnode/src/mgmtSdb.c b/src/mnode/src/mgmtSdb.c index f8cd457d81c531c744868ea92e85b36c5dce9e87..0de540ae78b0dfbfdbf48e404c969eee611642ec 100644 --- a/src/mnode/src/mgmtSdb.c +++ b/src/mnode/src/mgmtSdb.c @@ -47,6 +47,7 @@ typedef struct _SSdbTable { int32_t tableId; int32_t hashSessions; int32_t maxRowSize; + int32_t refCountPos; int32_t autoIndex; int32_t fd; int64_t numOfRows; @@ -66,7 +67,6 @@ typedef struct { int64_t version; int64_t offset; int32_t rowSize; - int32_t refCount; void * row; } SRowMeta; @@ -320,11 +320,6 @@ static int32_t sdbInitTableByFile(SSdbTable *pTable) { } } else { if (rowHead->version < 0) { - SSdbOperDesc oper = { - .table = pTable, - .pObj = pMetaRow - }; - sdbDecRef(pTable, pMetaRow); (*sdbDeleteIndexFp[pTable->keyType])(pTable->iHandle, rowHead->data); pTable->numOfRows--; sdbTrace("table:%s, version:%" PRId64 " numOfRows:%d, read deleted record:%s", @@ -340,13 +335,11 @@ static int32_t sdbInitTableByFile(SSdbTable *pTable) { .rowSize = rowHead->rowSize, .pObj = pMetaRow }; - sdbDecRef(pTable, pMetaRow); (*sdbDeleteIndexFp[pTable->keyType])(pTable->iHandle, rowHead->data); int32_t code = (*pTable->decodeFp)(&oper); if (code == TSDB_CODE_SUCCESS) { rowMeta.row = oper.pObj; - sdbIncRef(pTable, pMetaRow); (*sdbAddIndexFp[pTable->keyType])(pTable->iHandle, rowMeta.row, &rowMeta); sdbTrace("table:%s, version:%" PRId64 " numOfRows:%d, read updated record:%s", pTable->tableName, pTable->version, pTable->numOfRows, sdbGetkeyStr(pTable, rowHead->data)); @@ -375,6 +368,7 @@ static int32_t sdbInitTableByFile(SSdbTable *pTable) { .version = pMeta->version, }; + sdbIncRef(pTable, oper.pObj); int32_t code = (*pTable->insertFp)(&oper); if (code != TSDB_CODE_SUCCESS) { sdbError("table:%s, failed to insert record:%s", pTable->tableName, sdbGetkeyStr(pTable, rowHead->data)); @@ -398,6 +392,7 @@ void *sdbOpenTable(SSdbTableDesc *pDesc) { pTable->keyType = pDesc->keyType; pTable->hashSessions = pDesc->hashSessions; pTable->maxRowSize = pDesc->maxRowSize; + pTable->refCountPos = pDesc->refCountPos; pTable->insertFp = pDesc->insertFp; pTable->deleteFp = pDesc->deleteFp; pTable->updateFp = pDesc->updateFp; @@ -438,18 +433,18 @@ static SRowMeta *sdbGetRowMeta(void *handle, void *key) { void sdbIncRef(void *handle, void *pRow) { if (pRow) { SSdbTable *pTable = handle; - SRowMeta *pMeta = (pRow - 4); - atomic_add_fetch_32(&pMeta->refCount, 1); - sdbTrace("table:%s, add ref:%d to record:%s", pTable->tableName, pMeta->refCount, sdbGetkeyStr(pTable, pRow)); + int32_t *pRefCount = (int32_t *)(pRow + pTable->refCountPos); + atomic_add_fetch_32(pRefCount, 1); + sdbTrace("table:%s, add ref:%d to record:%s", pTable->tableName, *pRefCount, sdbGetkeyStr(pTable, pRow)); } } void sdbDecRef(void *handle, void *pRow) { if (pRow) { SSdbTable *pTable = handle; - SRowMeta * pMeta = (pRow - 4); - int32_t refCount = atomic_sub_fetch_32(&pMeta->refCount, 1); - sdbTrace("table:%s, def ref:%d from record:%s", pTable->tableName, pMeta->refCount, sdbGetkeyStr(pTable, pRow)); + int32_t *pRefCount = (int32_t *)(pRow + pTable->refCountPos); + int32_t refCount = atomic_sub_fetch_32(pRefCount, 1); + sdbTrace("table:%s, def ref:%d from record:%s", pTable->tableName, *pRefCount, sdbGetkeyStr(pTable, pRow)); if (refCount <= 0) { SSdbOperDesc oper = {.pObj = pRow}; (*pTable->destroyFp)(&oper); diff --git a/src/mnode/src/mgmtTable.c b/src/mnode/src/mgmtTable.c index eff42ebdb22e5c78492450563281391318212eaf..3e0036571a3df3e04c9f7e5d134c591bcec96542 100644 --- a/src/mnode/src/mgmtTable.c +++ b/src/mnode/src/mgmtTable.c @@ -236,6 +236,7 @@ static int32_t mgmtInitChildTables() { .tableName = "ctables", .hashSessions = tsMaxTables, .maxRowSize = sizeof(SChildTableObj) + sizeof(SSchema) * TSDB_MAX_COLUMNS, + .refCountPos = (int8_t *)(&tObj.refCount) - (int8_t *)&tObj, .keyType = SDB_KEY_TYPE_STRING, .insertFp = mgmtChildTableActionInsert, .deleteFp = mgmtChildTableActionDelete, @@ -411,6 +412,7 @@ static int32_t mgmtInitSuperTables() { .tableName = "stables", .hashSessions = TSDB_MAX_SUPER_TABLES, .maxRowSize = tsSuperTableUpdateSize + sizeof(SSchema) * TSDB_MAX_COLUMNS, + .refCountPos = (int8_t *)(&tObj.refCount) - (int8_t *)&tObj, .keyType = SDB_KEY_TYPE_STRING, .insertFp = mgmtSuperTableActionInsert, .deleteFp = mgmtSuperTableActionDelete, diff --git a/src/mnode/src/mgmtUser.c b/src/mnode/src/mgmtUser.c index 53fce2c2f9fc28418f9f8acf34ed8a4725019b41..cedb32b44d478cb402623f1412ecfb3d768ae95c 100644 --- a/src/mnode/src/mgmtUser.c +++ b/src/mnode/src/mgmtUser.c @@ -99,6 +99,7 @@ int32_t mgmtInitUsers() { .tableName = "users", .hashSessions = TSDB_MAX_USERS, .maxRowSize = tsUserUpdateSize, + .refCountPos = (int8_t *)(&tObj.refCount) - (int8_t *)&tObj, .keyType = SDB_KEY_TYPE_STRING, .insertFp = mgmtUserActionInsert, .deleteFp = mgmtUserActionDelete, diff --git a/src/mnode/src/mgmtVgroup.c b/src/mnode/src/mgmtVgroup.c index 972f1c427cc84907188e0a9cbbeb4a3a45744819..dd12128ccc9c859553c48e73dfea5dad521bea5b 100644 --- a/src/mnode/src/mgmtVgroup.c +++ b/src/mnode/src/mgmtVgroup.c @@ -149,6 +149,7 @@ int32_t mgmtInitVgroups() { .tableName = "vgroups", .hashSessions = TSDB_MAX_VGROUPS, .maxRowSize = tsVgUpdateSize, + .refCountPos = (int8_t *)(&tObj.refCount) - (int8_t *)&tObj, .keyType = SDB_KEY_TYPE_AUTO, .insertFp = mgmtVgroupActionInsert, .deleteFp = mgmtVgroupActionDelete,