interface for transaction
This commit is contained in:
parent
ff4ad0f43d
commit
d92a150c72
|
@ -76,18 +76,18 @@ extern "C" {
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
SDB_START = 0,
|
SDB_START = 0,
|
||||||
SDB_VERSION = 1,
|
SDB_TRANS = 1,
|
||||||
SDB_CLUSTER = 2,
|
SDB_VERSION = 2,
|
||||||
SDB_DNODE = 3,
|
SDB_CLUSTER = 3,
|
||||||
SDB_MNODE = 4,
|
SDB_DNODE = 4,
|
||||||
SDB_ACCT = 5,
|
SDB_MNODE = 5,
|
||||||
SDB_AUTH = 6,
|
SDB_ACCT = 6,
|
||||||
SDB_USER = 7,
|
SDB_AUTH = 7,
|
||||||
SDB_DB = 8,
|
SDB_USER = 8,
|
||||||
SDB_VGROUP = 9,
|
SDB_DB = 9,
|
||||||
SDB_STABLE = 10,
|
SDB_VGROUP = 10,
|
||||||
SDB_FUNC = 11,
|
SDB_STABLE = 11,
|
||||||
SDB_TRANS = 12,
|
SDB_FUNC = 12,
|
||||||
SDB_MAX = 13
|
SDB_MAX = 13
|
||||||
} ESdbType;
|
} ESdbType;
|
||||||
|
|
||||||
|
@ -104,14 +104,14 @@ typedef struct {
|
||||||
int32_t cksum;
|
int32_t cksum;
|
||||||
int32_t dataLen;
|
int32_t dataLen;
|
||||||
char data[];
|
char data[];
|
||||||
} SSdbRawData;
|
} SSdbRaw;
|
||||||
|
|
||||||
typedef int32_t (*SdbInsertFp)(void *pObj);
|
typedef int32_t (*SdbInsertFp)(void *pObj);
|
||||||
typedef int32_t (*SdbUpdateFp)(void *pSrcObj, void *pDstObj);
|
typedef int32_t (*SdbUpdateFp)(void *pSrcObj, void *pDstObj);
|
||||||
typedef int32_t (*SdbDeleteFp)(void *pObj);
|
typedef int32_t (*SdbDeleteFp)(void *pObj);
|
||||||
typedef int32_t (*SdbDeployFp)();
|
typedef int32_t (*SdbDeployFp)();
|
||||||
typedef void *(*SdbDecodeFp)(SSdbRawData *pRaw);
|
typedef void *(*SdbDecodeFp)(SSdbRaw *pRaw);
|
||||||
typedef SSdbRawData *(*SdbEncodeFp)(void *pObj);
|
typedef SSdbRaw *(*SdbEncodeFp)(void *pObj);
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
ESdbType sdbType;
|
ESdbType sdbType;
|
||||||
|
@ -129,7 +129,7 @@ void sdbCleanup();
|
||||||
void sdbSetHandler(SSdbDesc desc);
|
void sdbSetHandler(SSdbDesc desc);
|
||||||
|
|
||||||
int32_t sdbRead();
|
int32_t sdbRead();
|
||||||
int32_t sdbWrite(SSdbRawData *pRawData);
|
int32_t sdbWrite(SSdbRaw *pRaw);
|
||||||
int32_t sdbCommit();
|
int32_t sdbCommit();
|
||||||
|
|
||||||
int32_t sdbDeploy();
|
int32_t sdbDeploy();
|
||||||
|
|
|
@ -24,21 +24,23 @@ extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef struct STrans STrans;
|
typedef struct STrans STrans;
|
||||||
|
typedef enum { TRN_POLICY_ROLLBACK = 1, TRN_POLICY_RETRY = 2 } ETrnPolicy;
|
||||||
|
|
||||||
int32_t trnInit();
|
int32_t trnInit();
|
||||||
void trnCleanup();
|
void trnCleanup();
|
||||||
|
|
||||||
STrans *trnCreate();
|
STrans *trnCreate(ETrnPolicy);
|
||||||
int32_t trnPrepare(STrans *);
|
void trnDrop(STrans *pTrans);
|
||||||
int32_t trnCommit(STrans *);
|
void trnSetRpcHandle(STrans *pTrans, void *rpcHandle);
|
||||||
int32_t trnExecute(STrans *);
|
int32_t trnAppendRedoLog(STrans *pTrans, SSdbRaw *pRaw);
|
||||||
void trnDrop(STrans *);
|
int32_t trnAppendUndoLog(STrans *pTrans, SSdbRaw *pRaw);
|
||||||
|
int32_t trnAppendCommitLog(STrans *pTrans, SSdbRaw *pRaw);
|
||||||
|
int32_t trnAppendRedoAction(STrans *pTrans, SEpSet *, void *pMsg);
|
||||||
|
int32_t trnAppendUndoAction(STrans *pTrans, SEpSet *, void *pMsg);
|
||||||
|
|
||||||
int32_t trnAppendRedoLog(STrans *, SSdbRawData *);
|
int32_t trnPrepare(STrans *pTrans, int32_t (*syncfp)(SSdbRaw *pRaw, void *pData));
|
||||||
int32_t trnAppendUndoLog(STrans *, SSdbRawData *);
|
int32_t trnApply(SSdbRaw *pRaw, void *pData, int32_t code);
|
||||||
int32_t trnAppendCommitLog(STrans *, SSdbRawData *);
|
int32_t trnExecute(int32_t tranId);
|
||||||
int32_t trnAppendRedoAction(STrans *, SEpSet *, void *pMsg);
|
|
||||||
int32_t trnAppendUndoAction(STrans *, SEpSet *, void *pMsg);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,7 @@ extern "C" {
|
||||||
#define TAOS_FAILED(err) ((err) < 0)
|
#define TAOS_FAILED(err) ((err) < 0)
|
||||||
|
|
||||||
const char* tstrerror(int32_t err);
|
const char* tstrerror(int32_t err);
|
||||||
|
const char* terrstr();
|
||||||
|
|
||||||
int32_t* taosGetErrno();
|
int32_t* taosGetErrno();
|
||||||
#define terrno (*taosGetErrno())
|
#define terrno (*taosGetErrno())
|
||||||
|
|
|
@ -24,6 +24,7 @@ extern "C" {
|
||||||
|
|
||||||
int32_t mnodeInitSync();
|
int32_t mnodeInitSync();
|
||||||
void mnodeCleanUpSync();
|
void mnodeCleanUpSync();
|
||||||
|
int32_t mnodeSyncPropose(SSdbRaw *pRaw, void *pData);
|
||||||
|
|
||||||
bool mnodeIsMaster();
|
bool mnodeIsMaster();
|
||||||
|
|
||||||
|
|
|
@ -18,8 +18,8 @@
|
||||||
|
|
||||||
#define ACCT_VER 1
|
#define ACCT_VER 1
|
||||||
|
|
||||||
static SSdbRawData *mnodeAcctActionEncode(SAcctObj *pAcct) {
|
static SSdbRaw *mnodeAcctActionEncode(SAcctObj *pAcct) {
|
||||||
SSdbRawData *pRaw = calloc(1, sizeof(SAcctObj) + sizeof(SSdbRawData));
|
SSdbRaw *pRaw = calloc(1, sizeof(SAcctObj) + sizeof(SSdbRaw));
|
||||||
if (pRaw == NULL) {
|
if (pRaw == NULL) {
|
||||||
terrno = TSDB_CODE_MND_OUT_OF_MEMORY;
|
terrno = TSDB_CODE_MND_OUT_OF_MEMORY;
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -45,7 +45,7 @@ static SSdbRawData *mnodeAcctActionEncode(SAcctObj *pAcct) {
|
||||||
return pRaw;
|
return pRaw;
|
||||||
}
|
}
|
||||||
|
|
||||||
static SAcctObj *mnodeAcctActionDecode(SSdbRawData *pRaw) {
|
static SAcctObj *mnodeAcctActionDecode(SSdbRaw *pRaw) {
|
||||||
if (pRaw->sver != ACCT_VER) {
|
if (pRaw->sver != ACCT_VER) {
|
||||||
terrno = TSDB_CODE_SDB_INVAID_RAW_DATA_VER;
|
terrno = TSDB_CODE_SDB_INVAID_RAW_DATA_VER;
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -96,23 +96,21 @@ static int32_t mnodeCreateDefaultAcct() {
|
||||||
SAcctObj acctObj = {0};
|
SAcctObj acctObj = {0};
|
||||||
tstrncpy(acctObj.acct, TSDB_DEFAULT_USER, TSDB_USER_LEN);
|
tstrncpy(acctObj.acct, TSDB_DEFAULT_USER, TSDB_USER_LEN);
|
||||||
acctObj.createdTime = taosGetTimestampMs();
|
acctObj.createdTime = taosGetTimestampMs();
|
||||||
acctObj.updateTime = taosGetTimestampMs();
|
acctObj.updateTime = acctObj.createdTime;
|
||||||
acctObj.acctId = 1;
|
acctObj.acctId = 1;
|
||||||
acctObj.cfg = (SAcctCfg){.maxUsers = 128,
|
acctObj.cfg = (SAcctCfg){.maxUsers = 1024,
|
||||||
.maxDbs = 128,
|
.maxDbs = 1024,
|
||||||
.maxTimeSeries = INT32_MAX,
|
.maxTimeSeries = INT32_MAX,
|
||||||
.maxStreams = 1000,
|
.maxStreams = 8092,
|
||||||
.maxStorage = INT64_MAX,
|
.maxStorage = INT64_MAX,
|
||||||
.accessState = TSDB_VN_ALL_ACCCESS};
|
.accessState = TSDB_VN_ALL_ACCCESS};
|
||||||
|
|
||||||
SSdbRawData *pRaw = mnodeAcctActionEncode(&acctObj);
|
SSdbRaw *pRaw = mnodeAcctActionEncode(&acctObj);
|
||||||
if (pRaw != NULL) {
|
if (pRaw != NULL) {
|
||||||
code = sdbWrite(pRaw);
|
return -1;
|
||||||
} else {
|
|
||||||
code = terrno;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return code;
|
return sdbWrite(pRaw);
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t mnodeInitAcct() {
|
int32_t mnodeInitAcct() {
|
||||||
|
|
|
@ -20,4 +20,10 @@
|
||||||
int32_t mnodeInitSync() { return 0; }
|
int32_t mnodeInitSync() { return 0; }
|
||||||
void mnodeCleanUpSync() {}
|
void mnodeCleanUpSync() {}
|
||||||
|
|
||||||
|
int32_t mnodeSyncPropose(SSdbRaw *pRaw, void *pData) {
|
||||||
|
trnApply(pRaw, pData, 0);
|
||||||
|
free(pRaw);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
bool mnodeIsMaster() { return true; }
|
bool mnodeIsMaster() { return true; }
|
|
@ -14,15 +14,15 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define _DEFAULT_SOURCE
|
#define _DEFAULT_SOURCE
|
||||||
|
#include "mnodeSync.h"
|
||||||
#include "os.h"
|
#include "os.h"
|
||||||
#include "tkey.h"
|
|
||||||
#include "tglobal.h"
|
#include "tglobal.h"
|
||||||
#include "mnodeInt.h"
|
#include "tkey.h"
|
||||||
|
|
||||||
#define USER_VER 1
|
#define USER_VER 1
|
||||||
|
|
||||||
static SSdbRawData *mnodeUserActionEncode(SUserObj *pUser) {
|
static SSdbRaw *mnodeUserActionEncode(SUserObj *pUser) {
|
||||||
SSdbRawData *pRaw = calloc(1, sizeof(SUserObj) + sizeof(SSdbRawData));
|
SSdbRaw *pRaw = calloc(1, sizeof(SUserObj) + sizeof(SSdbRaw));
|
||||||
if (pRaw == NULL) {
|
if (pRaw == NULL) {
|
||||||
terrno = TSDB_CODE_MND_OUT_OF_MEMORY;
|
terrno = TSDB_CODE_MND_OUT_OF_MEMORY;
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -43,7 +43,7 @@ static SSdbRawData *mnodeUserActionEncode(SUserObj *pUser) {
|
||||||
return pRaw;
|
return pRaw;
|
||||||
}
|
}
|
||||||
|
|
||||||
static SUserObj *mnodeUserActionDecode(SSdbRawData *pRaw) {
|
static SUserObj *mnodeUserActionDecode(SSdbRaw *pRaw) {
|
||||||
if (pRaw->sver != USER_VER) {
|
if (pRaw->sver != USER_VER) {
|
||||||
terrno = TSDB_CODE_SDB_INVAID_RAW_DATA_VER;
|
terrno = TSDB_CODE_SDB_INVAID_RAW_DATA_VER;
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -77,12 +77,14 @@ static SUserObj *mnodeUserActionDecode(SSdbRawData *pRaw) {
|
||||||
static int32_t mnodeUserActionInsert(SUserObj *pUser) {
|
static int32_t mnodeUserActionInsert(SUserObj *pUser) {
|
||||||
pUser->prohibitDbHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
|
pUser->prohibitDbHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
|
||||||
if (pUser->prohibitDbHash == NULL) {
|
if (pUser->prohibitDbHash == NULL) {
|
||||||
return TSDB_CODE_MND_OUT_OF_MEMORY;
|
terrno = TSDB_CODE_MND_OUT_OF_MEMORY;
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
pUser->pAcct = sdbAcquire(SDB_ACCT, pUser->acct);
|
pUser->pAcct = sdbAcquire(SDB_ACCT, pUser->acct);
|
||||||
if (pUser->pAcct == NULL) {
|
if (pUser->pAcct == NULL) {
|
||||||
return TSDB_CODE_MND_ACCT_NOT_EXIST;
|
terrno = TSDB_CODE_MND_ACCT_NOT_EXIST;
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -108,8 +110,6 @@ static int32_t mnodeUserActionUpdate(SUserObj *pSrcUser, SUserObj *pDstUser) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static int32_t mnodeCreateDefaultUser(char *acct, char *user, char *pass) {
|
static int32_t mnodeCreateDefaultUser(char *acct, char *user, char *pass) {
|
||||||
int32_t code = 0;
|
|
||||||
|
|
||||||
SUserObj userObj = {0};
|
SUserObj userObj = {0};
|
||||||
tstrncpy(userObj.user, user, TSDB_USER_LEN);
|
tstrncpy(userObj.user, user, TSDB_USER_LEN);
|
||||||
tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
|
tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
|
||||||
|
@ -121,35 +121,31 @@ static int32_t mnodeCreateDefaultUser(char *acct, char *user, char *pass) {
|
||||||
userObj.rootAuth = 1;
|
userObj.rootAuth = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
SSdbRawData *pRaw = mnodeUserActionEncode(&userObj);
|
SSdbRaw *pRaw = mnodeUserActionEncode(&userObj);
|
||||||
if (pRaw != NULL) {
|
if (pRaw == NULL) {
|
||||||
code = sdbWrite(pRaw);
|
return -1;
|
||||||
} else {
|
|
||||||
code = terrno;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return code;
|
return sdbWrite(pRaw);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int32_t mnodeCreateDefaultUsers() {
|
static int32_t mnodeCreateDefaultUsers() {
|
||||||
int32_t code = mnodeCreateDefaultUser(TSDB_DEFAULT_USER, TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS);
|
if (mnodeCreateDefaultUser(TSDB_DEFAULT_USER, TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS) != 0) {
|
||||||
if (code != 0) return code;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
code = mnodeCreateDefaultUser(TSDB_DEFAULT_USER, "monitor", tsInternalPass);
|
if (mnodeCreateDefaultUser(TSDB_DEFAULT_USER, "monitor", tsInternalPass) != 0) {
|
||||||
if (code != 0) return code;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
code = mnodeCreateDefaultUser(TSDB_DEFAULT_USER, "_" TSDB_DEFAULT_USER, tsInternalPass);
|
if (mnodeCreateDefaultUser(TSDB_DEFAULT_USER, "_" TSDB_DEFAULT_USER, tsInternalPass) != 0) {
|
||||||
if (code != 0) return code;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
return code;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int32_t mnodeCreateUser(char *acct, char *user, char *pass, SMnMsg *pMsg) {
|
static int32_t mnodeCreateUser(char *acct, char *user, char *pass, SMnMsg *pMsg) {
|
||||||
int32_t code = 0;
|
|
||||||
STrans *pTrans = NULL;
|
|
||||||
SSdbRawData *pUndoRaw = NULL;
|
|
||||||
SSdbRawData *pRedoRaw = NULL;
|
|
||||||
|
|
||||||
SUserObj userObj = {0};
|
SUserObj userObj = {0};
|
||||||
tstrncpy(userObj.user, user, TSDB_USER_LEN);
|
tstrncpy(userObj.user, user, TSDB_USER_LEN);
|
||||||
tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
|
tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
|
||||||
|
@ -158,79 +154,80 @@ static int32_t mnodeCreateUser(char *acct, char *user, char *pass, SMnMsg *pMsg)
|
||||||
userObj.updateTime = userObj.createdTime;
|
userObj.updateTime = userObj.createdTime;
|
||||||
userObj.rootAuth = 0;
|
userObj.rootAuth = 0;
|
||||||
|
|
||||||
pRedoRaw = mnodeUserActionEncode(&userObj);
|
STrans *pTrans = trnCreate(TRN_POLICY_ROLLBACK);
|
||||||
if (pRedoRaw == NULL) {
|
if (pTrans == NULL) return -1;
|
||||||
code = terrno;
|
|
||||||
goto CREATE_USER_OVER;
|
SSdbRaw *pRedoRaw = mnodeUserActionEncode(&userObj);
|
||||||
|
if (pRedoRaw == NULL || trnAppendRedoLog(pTrans, pRedoRaw) != 0) {
|
||||||
|
trnDrop(pTrans);
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
pRedoRaw->status = SDB_STATUS_READY;
|
pRedoRaw->status = SDB_STATUS_CREATING;
|
||||||
pRedoRaw->action = SDB_ACTION_INSERT;
|
pRedoRaw->action = SDB_ACTION_INSERT;
|
||||||
|
|
||||||
pUndoRaw = mnodeUserActionEncode(&userObj);
|
SSdbRaw *pUndoRaw = mnodeUserActionEncode(&userObj);
|
||||||
if (pUndoRaw == NULL) {
|
if (pUndoRaw == NULL || trnAppendUndoLog(pTrans, pUndoRaw) != 0) {
|
||||||
code = terrno;
|
trnDrop(pTrans);
|
||||||
goto CREATE_USER_OVER;
|
return -1;
|
||||||
}
|
}
|
||||||
pUndoRaw->status = SDB_STATUS_DROPPING;
|
pUndoRaw->status = SDB_STATUS_DROPPING;
|
||||||
pUndoRaw->action = SDB_ACTION_DELETE;
|
pUndoRaw->action = SDB_ACTION_DELETE;
|
||||||
|
|
||||||
pTrans = trnCreate();
|
SSdbRaw *pCommitRaw = mnodeUserActionEncode(&userObj);
|
||||||
if (pTrans == NULL) {
|
if (pCommitRaw == NULL || trnAppendCommitLog(pTrans, pCommitRaw) != 0) {
|
||||||
code = TSDB_CODE_MND_OUT_OF_MEMORY;
|
|
||||||
goto CREATE_USER_OVER;
|
|
||||||
}
|
|
||||||
trnAppendRedoLog(pTrans, pRedoRaw);
|
|
||||||
trnAppendUndoLog(pTrans, pUndoRaw);
|
|
||||||
|
|
||||||
code = trnCommit(pTrans);
|
|
||||||
|
|
||||||
CREATE_USER_OVER:
|
|
||||||
if (code != 0) {
|
|
||||||
trnDrop(pTrans);
|
trnDrop(pTrans);
|
||||||
free(pRedoRaw);
|
return -1;
|
||||||
free(pUndoRaw);
|
}
|
||||||
|
pCommitRaw->status = SDB_STATUS_READY;
|
||||||
|
pCommitRaw->action = SDB_ACTION_UPDATE;
|
||||||
|
|
||||||
|
trnSetRpcHandle(pTrans, pMsg->rpcMsg.handle);
|
||||||
|
|
||||||
|
if (trnPrepare(pTrans, mnodeSyncPropose) != 0) {
|
||||||
|
trnDrop(pTrans);
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return code;
|
trnDrop(pTrans);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int32_t mnodeProcessCreateUserMsg(SMnMsg *pMsg) {
|
static int32_t mnodeProcessCreateUserMsg(SMnMsg *pMsg) {
|
||||||
SCreateUserMsg *pCreate = pMsg->rpcMsg.pCont;
|
SCreateUserMsg *pCreate = pMsg->rpcMsg.pCont;
|
||||||
int32_t code = TSDB_CODE_SUCCESS;
|
|
||||||
|
|
||||||
if (pCreate->user[0] == 0) {
|
if (pCreate->user[0] == 0) {
|
||||||
code = TSDB_CODE_MND_INVALID_USER_FORMAT;
|
terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
|
||||||
mError("user:%s, failed to create since %s", pCreate->user, tstrerror(code));
|
mError("user:%s, failed to create since %s", pCreate->user, terrstr());
|
||||||
return code;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pCreate->pass[0] == 0) {
|
if (pCreate->pass[0] == 0) {
|
||||||
code = TSDB_CODE_MND_INVALID_PASS_FORMAT;
|
terrno = TSDB_CODE_MND_INVALID_PASS_FORMAT;
|
||||||
mError("user:%s, failed to create since %s", pCreate->user, tstrerror(code));
|
mError("user:%s, failed to create since %s", pCreate->user, terrstr());
|
||||||
return code;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
SUserObj *pUser = sdbAcquire(SDB_USER, pCreate->user);
|
SUserObj *pUser = sdbAcquire(SDB_USER, pCreate->user);
|
||||||
if (pUser != NULL) {
|
if (pUser != NULL) {
|
||||||
sdbRelease(pUser);
|
sdbRelease(pUser);
|
||||||
code = TSDB_CODE_MND_USER_ALREADY_EXIST;
|
terrno = TSDB_CODE_MND_USER_ALREADY_EXIST;
|
||||||
mError("user:%s, failed to create since %s", pCreate->user, tstrerror(code));
|
mError("user:%s, failed to create since %s", pCreate->user, terrstr());
|
||||||
return code;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
SUserObj *pOperUser = sdbAcquire(SDB_USER, pMsg->user);
|
SUserObj *pOperUser = sdbAcquire(SDB_USER, pMsg->user);
|
||||||
if (pOperUser == NULL) {
|
if (pOperUser == NULL) {
|
||||||
code = TSDB_CODE_MND_NO_USER_FROM_CONN;
|
terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
|
||||||
mError("user:%s, failed to create since %s", pCreate->user, tstrerror(code));
|
mError("user:%s, failed to create since %s", pCreate->user, terrstr());
|
||||||
return code;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
code = mnodeCreateUser(pOperUser->acct, pCreate->user, pCreate->pass, pMsg);
|
int32_t code = mnodeCreateUser(pOperUser->acct, pCreate->user, pCreate->pass, pMsg);
|
||||||
sdbRelease(pOperUser);
|
sdbRelease(pOperUser);
|
||||||
|
|
||||||
if (code != 0) {
|
if (code != 0) {
|
||||||
mError("user:%s, failed to create since %s", pCreate->user, tstrerror(code));
|
mError("user:%s, failed to create since %s", pCreate->user, terrstr());
|
||||||
return code;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return TSDB_CODE_MND_ACTION_IN_PROGRESS;
|
return TSDB_CODE_MND_ACTION_IN_PROGRESS;
|
||||||
|
|
|
@ -61,7 +61,7 @@ static SHashObj *sdbGetHash(int32_t sdb) {
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t sdbWrite(SSdbRawData *pRaw) {
|
int32_t sdbWrite(SSdbRaw *pRaw) {
|
||||||
SHashObj *hash = sdbGetHash(pRaw->type);
|
SHashObj *hash = sdbGetHash(pRaw->type);
|
||||||
switch (pRaw->action) {
|
switch (pRaw->action) {
|
||||||
case SDB_ACTION_INSERT:
|
case SDB_ACTION_INSERT:
|
||||||
|
@ -85,7 +85,7 @@ static int32_t sdbReadVersion(FileFd fd) { return 0; }
|
||||||
static int32_t sdbReadDataFile() {
|
static int32_t sdbReadDataFile() {
|
||||||
int32_t code = 0;
|
int32_t code = 0;
|
||||||
|
|
||||||
SSdbRawData *pRaw = malloc(SDB_MAX_SIZE);
|
SSdbRaw *pRaw = malloc(SDB_MAX_SIZE);
|
||||||
if (pRaw == NULL) {
|
if (pRaw == NULL) {
|
||||||
return TSDB_CODE_MND_OUT_OF_MEMORY;
|
return TSDB_CODE_MND_OUT_OF_MEMORY;
|
||||||
}
|
}
|
||||||
|
@ -101,7 +101,7 @@ static int32_t sdbReadDataFile() {
|
||||||
|
|
||||||
int64_t offset = 0;
|
int64_t offset = 0;
|
||||||
while (1) {
|
while (1) {
|
||||||
int32_t ret = (int32_t)taosReadFile(fd, pRaw, sizeof(SSdbRawData));
|
int32_t ret = (int32_t)taosReadFile(fd, pRaw, sizeof(SSdbRaw));
|
||||||
if (ret == 0) break;
|
if (ret == 0) break;
|
||||||
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
|
@ -110,7 +110,7 @@ static int32_t sdbReadDataFile() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ret < sizeof(SSdbRawData)) {
|
if (ret < sizeof(SSdbRaw)) {
|
||||||
code = TSDB_CODE_SDB_INTERNAL_ERROR;
|
code = TSDB_CODE_SDB_INTERNAL_ERROR;
|
||||||
mError("failed to read file:%s since %s", file, tstrerror(code));
|
mError("failed to read file:%s since %s", file, tstrerror(code));
|
||||||
break;
|
break;
|
||||||
|
@ -143,7 +143,7 @@ static int32_t sdbWriteDataFile() {
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int32_t i = SDB_START; i < SDB_MAX; ++i) {
|
for (int32_t i = SDB_MAX - 1; i > SDB_START; --i) {
|
||||||
SHashObj *hash = tsSdb.hashObjs[i];
|
SHashObj *hash = tsSdb.hashObjs[i];
|
||||||
if (!hash) continue;
|
if (!hash) continue;
|
||||||
|
|
||||||
|
@ -153,9 +153,9 @@ static int32_t sdbWriteDataFile() {
|
||||||
SSdbRow *pRow = taosHashIterate(hash, NULL);
|
SSdbRow *pRow = taosHashIterate(hash, NULL);
|
||||||
while (pRow != NULL) {
|
while (pRow != NULL) {
|
||||||
if (pRow->status == SDB_STATUS_READY) continue;
|
if (pRow->status == SDB_STATUS_READY) continue;
|
||||||
SSdbRawData *pRaw = (*encodeFp)(pRow->data);
|
SSdbRaw *pRaw = (*encodeFp)(pRow->data);
|
||||||
if (pRaw != NULL) {
|
if (pRaw != NULL) {
|
||||||
taosWriteFile(fd, pRaw, sizeof(SSdbRawData) + pRaw->dataLen);
|
taosWriteFile(fd, pRaw, sizeof(SSdbRaw) + pRaw->dataLen);
|
||||||
} else {
|
} else {
|
||||||
taosHashCancelIterate(hash, pRow);
|
taosHashCancelIterate(hash, pRow);
|
||||||
code = TSDB_CODE_SDB_INTERNAL_ERROR;
|
code = TSDB_CODE_SDB_INTERNAL_ERROR;
|
||||||
|
|
|
@ -11,4 +11,5 @@ target_link_libraries(
|
||||||
PRIVATE common
|
PRIVATE common
|
||||||
PRIVATE util
|
PRIVATE util
|
||||||
PRIVATE sdb
|
PRIVATE sdb
|
||||||
|
PRIVATE transport
|
||||||
)
|
)
|
||||||
|
|
|
@ -33,14 +33,35 @@ extern "C" {
|
||||||
#define mDebug(...) { if (mDebugFlag & DEBUG_DEBUG) { taosPrintLog("MND ", mDebugFlag, __VA_ARGS__); }}
|
#define mDebug(...) { if (mDebugFlag & DEBUG_DEBUG) { taosPrintLog("MND ", mDebugFlag, __VA_ARGS__); }}
|
||||||
#define mTrace(...) { if (mDebugFlag & DEBUG_TRACE) { taosPrintLog("MND ", mDebugFlag, __VA_ARGS__); }}
|
#define mTrace(...) { if (mDebugFlag & DEBUG_TRACE) { taosPrintLog("MND ", mDebugFlag, __VA_ARGS__); }}
|
||||||
|
|
||||||
|
#define TRN_VER 1
|
||||||
|
#define TRN_DEFAULT_ARRAY_SIZE 8
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
TRN_STAGE_PREPARE = 1,
|
||||||
|
TRN_STAGE_EXECUTE = 2,
|
||||||
|
TRN_STAGE_COMMIT = 3,
|
||||||
|
TRN_STAGE_ROLLBACK = 4,
|
||||||
|
TRN_STAGE_RETRY = 5
|
||||||
|
} ETrnStage;
|
||||||
|
|
||||||
typedef struct STrans {
|
typedef struct STrans {
|
||||||
SArray *redoLogs;
|
int32_t id;
|
||||||
SArray *undoLogs;
|
ETrnStage stage;
|
||||||
SArray *commitLogs;
|
ETrnPolicy policy;
|
||||||
SArray *redoActions;
|
void *rpcHandle;
|
||||||
SArray *undoActions;
|
SArray *redoLogs;
|
||||||
|
SArray *undoLogs;
|
||||||
|
SArray *commitLogs;
|
||||||
|
SArray *redoActions;
|
||||||
|
SArray *undoActions;
|
||||||
} STrans;
|
} STrans;
|
||||||
|
|
||||||
|
SSdbRaw *trnActionEncode(STrans *pTrans);
|
||||||
|
STrans *trnActionDecode(SSdbRaw *pRaw);
|
||||||
|
int32_t trnActionInsert(STrans *pTrans);
|
||||||
|
int32_t trnActionDelete(STrans *pTrans);
|
||||||
|
int32_t trnActionUpdate(STrans *pSrcTrans, STrans *pDstTrans);
|
||||||
|
int32_t trnGenerateTransId();
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,164 @@
|
||||||
|
/*
|
||||||
|
* 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define _DEFAULT_SOURCE
|
||||||
|
#include "trnInt.h"
|
||||||
|
|
||||||
|
SSdbRaw *trnActionEncode(STrans *pTrans) {
|
||||||
|
int32_t rawDataLen = 5 * sizeof(int32_t);
|
||||||
|
int32_t redoLogNum = taosArrayGetSize(pTrans->redoLogs);
|
||||||
|
int32_t undoLogNum = taosArrayGetSize(pTrans->undoLogs);
|
||||||
|
int32_t commitLogNum = taosArrayGetSize(pTrans->commitLogs);
|
||||||
|
int32_t redoActionNum = taosArrayGetSize(pTrans->redoActions);
|
||||||
|
int32_t undoActionNum = taosArrayGetSize(pTrans->undoActions);
|
||||||
|
|
||||||
|
for (int32_t index = 0; index < redoLogNum; ++index) {
|
||||||
|
SSdbRaw *pRawData = taosArrayGet(pTrans->redoLogs, index);
|
||||||
|
rawDataLen += (sizeof(SSdbRaw) + pRawData->dataLen);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int32_t index = 0; index < undoLogNum; ++index) {
|
||||||
|
SSdbRaw *pRawData = taosArrayGet(pTrans->undoLogs, index);
|
||||||
|
rawDataLen += (sizeof(SSdbRaw) + pRawData->dataLen);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int32_t index = 0; index < commitLogNum; ++index) {
|
||||||
|
SSdbRaw *pRawData = taosArrayGet(pTrans->commitLogs, index);
|
||||||
|
rawDataLen += (sizeof(SSdbRaw) + pRawData->dataLen);
|
||||||
|
}
|
||||||
|
|
||||||
|
SSdbRaw *pRaw = calloc(1, rawDataLen + sizeof(SSdbRaw));
|
||||||
|
if (pRaw == NULL) {
|
||||||
|
terrno = TSDB_CODE_MND_OUT_OF_MEMORY;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t dataLen = 0;
|
||||||
|
char *pData = pRaw->data;
|
||||||
|
SDB_SET_INT32_VAL(pData, dataLen, redoLogNum)
|
||||||
|
SDB_SET_INT32_VAL(pData, dataLen, undoLogNum)
|
||||||
|
SDB_SET_INT32_VAL(pData, dataLen, commitLogNum)
|
||||||
|
SDB_SET_INT32_VAL(pData, dataLen, redoActionNum)
|
||||||
|
SDB_SET_INT32_VAL(pData, dataLen, undoActionNum)
|
||||||
|
|
||||||
|
pRaw->dataLen = dataLen;
|
||||||
|
pRaw->type = SDB_TRANS;
|
||||||
|
pRaw->sver = TRN_VER;
|
||||||
|
return pRaw;
|
||||||
|
}
|
||||||
|
|
||||||
|
STrans *trnActionDecode(SSdbRaw *pRaw) {
|
||||||
|
if (pRaw->sver != TRN_VER) {
|
||||||
|
terrno = TSDB_CODE_SDB_INVAID_RAW_DATA_VER;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
STrans *pTrans = NULL;
|
||||||
|
if (pTrans == NULL) {
|
||||||
|
terrno = TSDB_CODE_MND_OUT_OF_MEMORY;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t redoLogNum = 0;
|
||||||
|
int32_t undoLogNum = 0;
|
||||||
|
int32_t commitLogNum = 0;
|
||||||
|
int32_t redoActionNum = 0;
|
||||||
|
int32_t undoActionNum = 0;
|
||||||
|
SSdbRaw *pTmp = malloc(sizeof(SSdbRaw));
|
||||||
|
|
||||||
|
int32_t code = 0;
|
||||||
|
int32_t dataLen = pRaw->dataLen;
|
||||||
|
char *pData = pRaw->data;
|
||||||
|
SDB_GET_INT32_VAL(pData, dataLen, redoLogNum, code)
|
||||||
|
SDB_GET_INT32_VAL(pData, dataLen, undoLogNum, code)
|
||||||
|
SDB_GET_INT32_VAL(pData, dataLen, commitLogNum, code)
|
||||||
|
SDB_GET_INT32_VAL(pData, dataLen, redoActionNum, code)
|
||||||
|
SDB_GET_INT32_VAL(pData, dataLen, undoActionNum, code)
|
||||||
|
|
||||||
|
for (int32_t index = 0; index < redoLogNum; ++index) {
|
||||||
|
SDB_GET_BINARY_VAL(pData, dataLen, pTmp, sizeof(SSdbRaw), code);
|
||||||
|
if (code == 0 && pTmp->dataLen > 0) {
|
||||||
|
SSdbRaw *pRead = malloc(sizeof(SSdbRaw) + pTmp->dataLen);
|
||||||
|
if (pRead == NULL) {
|
||||||
|
code = TSDB_CODE_MND_OUT_OF_MEMORY;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
memcpy(pRead, pTmp, sizeof(SSdbRaw));
|
||||||
|
SDB_GET_BINARY_VAL(pData, dataLen, pRead->data, pRead->dataLen, code);
|
||||||
|
void *ret = taosArrayPush(pTrans->redoLogs, &pRead);
|
||||||
|
if (ret == NULL) {
|
||||||
|
code = TSDB_CODE_MND_OUT_OF_MEMORY;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (code != 0) {
|
||||||
|
trnDrop(pTrans);
|
||||||
|
terrno = code;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return pTrans;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t trnActionInsert(STrans *pTrans) {
|
||||||
|
SArray *pArray = pTrans->redoLogs;
|
||||||
|
int32_t arraySize = taosArrayGetSize(pArray);
|
||||||
|
|
||||||
|
for (int32_t index = 0; index < arraySize; ++index) {
|
||||||
|
SSdbRaw *pRaw = taosArrayGetP(pArray, index);
|
||||||
|
int32_t code = sdbWrite(pRaw);
|
||||||
|
if (code != 0) {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t trnActionDelete(STrans *pTrans) {
|
||||||
|
SArray *pArray = pTrans->redoLogs;
|
||||||
|
int32_t arraySize = taosArrayGetSize(pArray);
|
||||||
|
|
||||||
|
for (int32_t index = 0; index < arraySize; ++index) {
|
||||||
|
SSdbRaw *pRaw = taosArrayGetP(pArray, index);
|
||||||
|
int32_t code = sdbWrite(pRaw);
|
||||||
|
if (code != 0) {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t trnActionUpdate(STrans *pSrcTrans, STrans *pDstTrans) { return 0; }
|
||||||
|
|
||||||
|
int32_t trnGenerateTransId() { return 1; }
|
||||||
|
|
||||||
|
int32_t trnInit() {
|
||||||
|
SSdbDesc desc = {.sdbType = SDB_TRANS,
|
||||||
|
.keyType = SDB_KEY_INT32,
|
||||||
|
.encodeFp = (SdbEncodeFp)trnActionEncode,
|
||||||
|
.decodeFp = (SdbDecodeFp)trnActionDecode,
|
||||||
|
.insertFp = (SdbInsertFp)trnActionInsert,
|
||||||
|
.updateFp = (SdbUpdateFp)trnActionUpdate,
|
||||||
|
.deleteFp = (SdbDeleteFp)trnActionDelete};
|
||||||
|
sdbSetHandler(desc);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void trnCleanup() {}
|
|
@ -15,17 +15,18 @@
|
||||||
|
|
||||||
#define _DEFAULT_SOURCE
|
#define _DEFAULT_SOURCE
|
||||||
#include "trnInt.h"
|
#include "trnInt.h"
|
||||||
|
#include "trpc.h"
|
||||||
|
|
||||||
#define TRN_VER 1
|
STrans *trnCreate(ETrnPolicy policy) {
|
||||||
#define TRN_DEFAULT_ARRAY_SIZE 8
|
|
||||||
|
|
||||||
STrans *trnCreate() {
|
|
||||||
STrans *pTrans = calloc(1, sizeof(STrans));
|
STrans *pTrans = calloc(1, sizeof(STrans));
|
||||||
if (pTrans == NULL) {
|
if (pTrans == NULL) {
|
||||||
terrno = TSDB_CODE_MND_OUT_OF_MEMORY;
|
terrno = TSDB_CODE_MND_OUT_OF_MEMORY;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pTrans->id = trnGenerateTransId();
|
||||||
|
pTrans->stage = TRN_STAGE_PREPARE;
|
||||||
|
pTrans->policy = policy;
|
||||||
pTrans->redoLogs = taosArrayInit(TRN_DEFAULT_ARRAY_SIZE, sizeof(void *));
|
pTrans->redoLogs = taosArrayInit(TRN_DEFAULT_ARRAY_SIZE, sizeof(void *));
|
||||||
pTrans->undoLogs = taosArrayInit(TRN_DEFAULT_ARRAY_SIZE, sizeof(void *));
|
pTrans->undoLogs = taosArrayInit(TRN_DEFAULT_ARRAY_SIZE, sizeof(void *));
|
||||||
pTrans->commitLogs = taosArrayInit(TRN_DEFAULT_ARRAY_SIZE, sizeof(void *));
|
pTrans->commitLogs = taosArrayInit(TRN_DEFAULT_ARRAY_SIZE, sizeof(void *));
|
||||||
|
@ -34,7 +35,6 @@ STrans *trnCreate() {
|
||||||
|
|
||||||
if (pTrans->redoLogs == NULL || pTrans->undoLogs == NULL || pTrans->commitLogs == NULL ||
|
if (pTrans->redoLogs == NULL || pTrans->undoLogs == NULL || pTrans->commitLogs == NULL ||
|
||||||
pTrans->redoActions == NULL || pTrans->undoActions == NULL) {
|
pTrans->redoActions == NULL || pTrans->undoActions == NULL) {
|
||||||
trnDrop(pTrans);
|
|
||||||
terrno = TSDB_CODE_MND_OUT_OF_MEMORY;
|
terrno = TSDB_CODE_MND_OUT_OF_MEMORY;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -42,192 +42,156 @@ STrans *trnCreate() {
|
||||||
return pTrans;
|
return pTrans;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t trnCommit(STrans *pTrans) { return 0; }
|
static void trnDropArray(SArray *pArray) {
|
||||||
|
|
||||||
static void trnDropLogs(SArray *pArray) {
|
|
||||||
for (int32_t index = 0; index < pArray->size; ++index) {
|
for (int32_t index = 0; index < pArray->size; ++index) {
|
||||||
SSdbRawData *pRaw = taosArrayGetP(pArray, index);
|
SSdbRaw *pRaw = taosArrayGetP(pArray, index);
|
||||||
free(pRaw);
|
tfree(pRaw);
|
||||||
}
|
}
|
||||||
|
|
||||||
taosArrayDestroy(pArray);
|
taosArrayDestroy(pArray);
|
||||||
}
|
}
|
||||||
|
|
||||||
void trnDrop(STrans *pTrans) {
|
void trnDrop(STrans *pTrans) {
|
||||||
trnDropLogs(pTrans->redoLogs);
|
trnDropArray(pTrans->redoLogs);
|
||||||
trnDropLogs(pTrans->undoLogs);
|
trnDropArray(pTrans->undoLogs);
|
||||||
trnDropLogs(pTrans->commitLogs);
|
trnDropArray(pTrans->commitLogs);
|
||||||
free(pTrans);
|
trnDropArray(pTrans->redoActions);
|
||||||
|
trnDropArray(pTrans->undoActions);
|
||||||
|
tfree(pTrans);
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t trnAppendRedoLog(STrans *pTrans, SSdbRawData *pRaw) {
|
void trnSetRpcHandle(STrans *pTrans, void *rpcHandle) {
|
||||||
void *ptr = taosArrayPush(pTrans->redoLogs, &pRaw);
|
if (pTrans != NULL) {
|
||||||
if (ptr == NULL) {
|
pTrans->rpcHandle = rpcHandle;
|
||||||
return TSDB_CODE_MND_OUT_OF_MEMORY;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int32_t trnAppendArray(SArray *pArray, SSdbRaw *pRaw) {
|
||||||
|
if (pArray == NULL || pRaw == NULL) {
|
||||||
|
terrno = TSDB_CODE_MND_OUT_OF_MEMORY;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *ptr = taosArrayPush(pArray, &pRaw);
|
||||||
|
if (ptr == NULL) {
|
||||||
|
terrno = TSDB_CODE_MND_OUT_OF_MEMORY;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t trnAppendUndoLog(STrans *pTrans, SSdbRawData *pRaw) {
|
int32_t trnAppendRedoLog(STrans *pTrans, SSdbRaw *pRaw) { return trnAppendArray(pTrans->redoLogs, pRaw); }
|
||||||
void *ptr = taosArrayPush(pTrans->undoLogs, &pRaw);
|
|
||||||
if (ptr == NULL) {
|
|
||||||
return TSDB_CODE_MND_OUT_OF_MEMORY;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int32_t trnAppendCommitLog(STrans *pTrans, SSdbRawData *pRaw) {
|
int32_t trnAppendUndoLog(STrans *pTrans, SSdbRaw *pRaw) { return trnAppendArray(pTrans->undoLogs, pRaw); }
|
||||||
void *ptr = taosArrayPush(pTrans->commitLogs, &pRaw);
|
|
||||||
if (ptr == NULL) {
|
int32_t trnAppendCommitLog(STrans *pTrans, SSdbRaw *pRaw) { return trnAppendArray(pTrans->commitLogs, pRaw); }
|
||||||
return TSDB_CODE_MND_OUT_OF_MEMORY;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int32_t trnAppendRedoAction(STrans *pTrans, SEpSet *pEpSet, void *pMsg) {
|
int32_t trnAppendRedoAction(STrans *pTrans, SEpSet *pEpSet, void *pMsg) {
|
||||||
void *ptr = taosArrayPush(pTrans->redoActions, &pMsg);
|
return trnAppendArray(pTrans->redoActions, pMsg);
|
||||||
if (ptr == NULL) {
|
|
||||||
return TSDB_CODE_MND_OUT_OF_MEMORY;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t trnAppendUndoAction(STrans *pTrans, SEpSet *pEpSet, void *pMsg) {
|
int32_t trnAppendUndoAction(STrans *pTrans, SEpSet *pEpSet, void *pMsg) {
|
||||||
void *ptr = taosArrayPush(pTrans->undoActions, &pMsg);
|
return trnAppendArray(pTrans->undoActions, pMsg);
|
||||||
if (ptr == NULL) {
|
|
||||||
return TSDB_CODE_MND_OUT_OF_MEMORY;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static SSdbRawData *trnActionEncode(STrans *pTrans) {
|
int32_t trnPrepare(STrans *pTrans, int32_t (*syncfp)(SSdbRaw *pRaw, void *pData)) {
|
||||||
int32_t rawDataLen = 5 * sizeof(int32_t);
|
if (syncfp == NULL) return -1;
|
||||||
int32_t redoLogNum = taosArrayGetSize(pTrans->redoLogs);
|
|
||||||
int32_t undoLogNum = taosArrayGetSize(pTrans->undoLogs);
|
|
||||||
int32_t commitLogNum = taosArrayGetSize(pTrans->commitLogs);
|
|
||||||
int32_t redoActionNum = taosArrayGetSize(pTrans->redoActions);
|
|
||||||
int32_t undoActionNum = taosArrayGetSize(pTrans->undoActions);
|
|
||||||
|
|
||||||
for (int32_t index = 0; index < redoLogNum; ++index) {
|
SSdbRaw *pRaw = trnActionEncode(pTrans);
|
||||||
SSdbRawData *pRawData = taosArrayGet(pTrans->redoLogs, index);
|
|
||||||
rawDataLen += (sizeof(SSdbRawData) + pRawData->dataLen);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int32_t index = 0; index < undoLogNum; ++index) {
|
|
||||||
SSdbRawData *pRawData = taosArrayGet(pTrans->undoLogs, index);
|
|
||||||
rawDataLen += (sizeof(SSdbRawData) + pRawData->dataLen);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int32_t index = 0; index < commitLogNum; ++index) {
|
|
||||||
SSdbRawData *pRawData = taosArrayGet(pTrans->commitLogs, index);
|
|
||||||
rawDataLen += (sizeof(SSdbRawData) + pRawData->dataLen);
|
|
||||||
}
|
|
||||||
|
|
||||||
SSdbRawData *pRaw = calloc(1, rawDataLen + sizeof(SSdbRawData));
|
|
||||||
if (pRaw == NULL) {
|
if (pRaw == NULL) {
|
||||||
terrno = TSDB_CODE_MND_OUT_OF_MEMORY;
|
mError("tranId:%d, failed to decode trans since %s", pTrans->id, terrstr());
|
||||||
return NULL;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t dataLen = 0;
|
if (sdbWrite(pRaw) != 0) {
|
||||||
char *pData = pRaw->data;
|
mError("tranId:%d, failed to write trans since %s", pTrans->id, terrstr());
|
||||||
SDB_SET_INT32_VAL(pData, dataLen, redoLogNum)
|
return -1;
|
||||||
SDB_SET_INT32_VAL(pData, dataLen, undoLogNum)
|
}
|
||||||
SDB_SET_INT32_VAL(pData, dataLen, commitLogNum)
|
|
||||||
SDB_SET_INT32_VAL(pData, dataLen, redoActionNum)
|
|
||||||
SDB_SET_INT32_VAL(pData, dataLen, undoActionNum)
|
|
||||||
|
|
||||||
pRaw->dataLen = dataLen;
|
if ((*syncfp)(pRaw, pTrans->rpcHandle) != 0) {
|
||||||
pRaw->type = SDB_TRANS;
|
mError("tranId:%d, failed to sync trans since %s", pTrans->id, terrstr());
|
||||||
pRaw->sver = TRN_VER;
|
return -1;
|
||||||
return pRaw;
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static STrans *trnActionDecode(SSdbRawData *pRaw) {
|
static void trnSendRpcRsp(void *rpcHandle, int32_t code) {
|
||||||
if (pRaw->sver != TRN_VER) {
|
if (rpcHandle != NULL) {
|
||||||
terrno = TSDB_CODE_SDB_INVAID_RAW_DATA_VER;
|
SRpcMsg rspMsg = {.handle = rpcHandle, .code = terrno};
|
||||||
return NULL;
|
rpcSendResponse(&rspMsg);
|
||||||
}
|
|
||||||
|
|
||||||
STrans *pTrans = trnCreate();
|
|
||||||
if (pTrans == NULL) {
|
|
||||||
terrno = TSDB_CODE_MND_OUT_OF_MEMORY;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
int32_t redoLogNum = 0;
|
|
||||||
int32_t undoLogNum = 0;
|
|
||||||
int32_t commitLogNum = 0;
|
|
||||||
int32_t redoActionNum = 0;
|
|
||||||
int32_t undoActionNum = 0;
|
|
||||||
SSdbRawData *pTmp = malloc(sizeof(SSdbRawData));
|
|
||||||
|
|
||||||
int32_t code = 0;
|
|
||||||
int32_t dataLen = pRaw->dataLen;
|
|
||||||
char *pData = pRaw->data;
|
|
||||||
SDB_GET_INT32_VAL(pData, dataLen, redoLogNum, code)
|
|
||||||
SDB_GET_INT32_VAL(pData, dataLen, undoLogNum, code)
|
|
||||||
SDB_GET_INT32_VAL(pData, dataLen, commitLogNum, code)
|
|
||||||
SDB_GET_INT32_VAL(pData, dataLen, redoActionNum, code)
|
|
||||||
SDB_GET_INT32_VAL(pData, dataLen, undoActionNum, code)
|
|
||||||
|
|
||||||
for (int32_t index = 0; index < redoLogNum; ++index) {
|
|
||||||
SDB_GET_BINARY_VAL(pData, dataLen, pTmp, sizeof(SSdbRawData), code);
|
|
||||||
if (code == 0 && pTmp->dataLen > 0) {
|
|
||||||
SSdbRawData *pRead = malloc(sizeof(SSdbRawData) + pTmp->dataLen);
|
|
||||||
if (pRead == NULL) {
|
|
||||||
code = TSDB_CODE_MND_OUT_OF_MEMORY;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
memcpy(pRead, pTmp, sizeof(SSdbRawData));
|
|
||||||
SDB_GET_BINARY_VAL(pData, dataLen, pRead->data, pRead->dataLen, code);
|
|
||||||
void *ret = taosArrayPush(pTrans->redoLogs, &pRead);
|
|
||||||
if (ret == NULL) {
|
|
||||||
code = TSDB_CODE_MND_OUT_OF_MEMORY;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t trnApply(SSdbRaw *pRaw, void *pData, int32_t code) {
|
||||||
if (code != 0) {
|
if (code != 0) {
|
||||||
trnDrop(pTrans);
|
trnSendRpcRsp(pData, terrno);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sdbWrite(pRaw) != 0) {
|
||||||
|
code = terrno;
|
||||||
|
trnSendRpcRsp(pData, code);
|
||||||
terrno = code;
|
terrno = code;
|
||||||
return NULL;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return pTrans;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int32_t trnActionInsert(STrans *pTrans) {
|
int32_t trnExecuteRedoLogs(STrans *pTrans) {return 0;}
|
||||||
SArray *pArray = pTrans->redoLogs;
|
int32_t trnExecuteUndoLogs(STrans *pTrans) {return 0;}
|
||||||
int32_t arraySize = taosArrayGetSize(pArray);
|
int32_t trnExecuteCommitLogs(STrans *pTrans) {return 0;}
|
||||||
|
int32_t trnExecuteRedoActions(STrans *pTrans) {return 0;}
|
||||||
|
int32_t trnExecuteUndoActions(STrans *pTrans) {return 0;}
|
||||||
|
static int32_t trnPerfomRollbackStage(STrans *pTrans) { return 0; }
|
||||||
|
|
||||||
for (int32_t index = 0; index < arraySize; ++index) {
|
int32_t trnExecute(int32_t tranId) {
|
||||||
SSdbRawData *pRaw = taosArrayGetP(pArray, index);
|
int32_t code = 0;
|
||||||
int32_t code = sdbWrite(pRaw);
|
|
||||||
if (code != 0) {
|
STrans *pTrans = sdbAcquire(SDB_TRANS, &tranId);
|
||||||
return code;
|
if (pTrans == NULL) {
|
||||||
|
code = terrno;
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pTrans->stage == TRN_STAGE_PREPARE) {
|
||||||
|
code = trnExecuteRedoLogs(pTrans);
|
||||||
|
if (code == 0) {
|
||||||
|
pTrans->stage = TRN_STAGE_EXECUTE;
|
||||||
|
} else {
|
||||||
|
pTrans->stage = TRN_STAGE_ROLLBACK;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
if (pTrans->stage == TRN_STAGE_EXECUTE) {
|
||||||
}
|
code = trnExecuteRedoActions(pTrans);
|
||||||
|
if (code == 0) {
|
||||||
static int32_t trnActionDelete(STrans *pTrans) {
|
pTrans->stage = TRN_STAGE_COMMIT;
|
||||||
SArray *pArray = pTrans->redoLogs;
|
} else if (code == TSDB_CODE_MND_ACTION_IN_PROGRESS) {
|
||||||
int32_t arraySize = taosArrayGetSize(pArray);
|
// do nothing
|
||||||
|
} else {
|
||||||
for (int32_t index = 0; index < arraySize; ++index) {
|
if (pTrans->policy == TRN_POLICY_RETRY) {
|
||||||
SSdbRawData *pRaw = taosArrayGetP(pArray, index);
|
pTrans->stage = TRN_STAGE_RETRY;
|
||||||
int32_t code = sdbWrite(pRaw);
|
} else {
|
||||||
if (code != 0) {
|
pTrans->stage = TRN_STAGE_ROLLBACK;
|
||||||
return code;
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
if (pTrans->stage == TRN_STAGE_COMMIT) {
|
||||||
}
|
code = trnExecuteCommitLogs(pTrans);
|
||||||
|
if (code == 0) {
|
||||||
|
trnDrop(pTrans);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pTrans->stage == TRN_STAGE_ROLLBACK) {
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pTrans->stage == TRN_STAGE_RETRY) {
|
||||||
|
}
|
||||||
|
|
||||||
static int32_t trnActionUpdate(STrans *pSrcUser, STrans *pDstUser) {
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
|
@ -1,46 +0,0 @@
|
||||||
/*
|
|
||||||
* 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/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#define _DEFAULT_SOURCE
|
|
||||||
#include "trnInt.h"
|
|
||||||
#include "tthread.h"
|
|
||||||
|
|
||||||
static struct {
|
|
||||||
pthread_t *threadId;
|
|
||||||
bool threadRunning;
|
|
||||||
} tsTrn;
|
|
||||||
|
|
||||||
static void *trnThreadFunc(void *param) {
|
|
||||||
while (1) {
|
|
||||||
pthread_testcancel();
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
int32_t trnInit() {
|
|
||||||
tsTrn.threadId = taosCreateThread(trnThreadFunc, NULL);
|
|
||||||
if (tsTrn.threadId == NULL) {
|
|
||||||
return TSDB_CODE_MND_OUT_OF_MEMORY;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void trnCleanup() {
|
|
||||||
if (tsTrn.threadId) {
|
|
||||||
taosDestoryThread(tsTrn.threadId);
|
|
||||||
tsTrn.threadId = NULL;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -524,3 +524,5 @@ const char* tstrerror(int32_t err) {
|
||||||
|
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char* terrstr() { return tstrerror(terrno); }
|
Loading…
Reference in New Issue