mndAcct.c 7.7 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

S
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
17 18
#include "mndAcct.h"
#include "mndShow.h"
S
Shengliang Guan 已提交
19

20 21
#define ACCT_VER_NUMBER   1
#define ACCT_RESERVE_SIZE 128
S
Shengliang Guan 已提交
22

S
Shengliang Guan 已提交
23 24 25 26 27
static int32_t  mndCreateDefaultAcct(SMnode *pMnode);
static SSdbRaw *mndAcctActionEncode(SAcctObj *pAcct);
static SSdbRow *mndAcctActionDecode(SSdbRaw *pRaw);
static int32_t  mndAcctActionInsert(SSdb *pSdb, SAcctObj *pAcct);
static int32_t  mndAcctActionDelete(SSdb *pSdb, SAcctObj *pAcct);
S
Shengliang Guan 已提交
28
static int32_t  mndAcctActionUpdate(SSdb *pSdb, SAcctObj *pOld, SAcctObj *pNew);
S
Shengliang Guan 已提交
29 30 31
static int32_t  mndProcessCreateAcctReq(SNodeMsg *pReq);
static int32_t  mndProcessAlterAcctReq(SNodeMsg *pReq);
static int32_t  mndProcessDropAcctReq(SNodeMsg *pReq);
S
Shengliang Guan 已提交
32 33 34 35

int32_t mndInitAcct(SMnode *pMnode) {
  SSdbTable table = {.sdbType = SDB_ACCT,
                     .keyType = SDB_KEY_BINARY,
S
Shengliang Guan 已提交
36 37 38 39 40 41
                     .deployFp = mndCreateDefaultAcct,
                     .encodeFp = (SdbEncodeFp)mndAcctActionEncode,
                     .decodeFp = (SdbDecodeFp)mndAcctActionDecode,
                     .insertFp = (SdbInsertFp)mndAcctActionInsert,
                     .updateFp = (SdbUpdateFp)mndAcctActionUpdate,
                     .deleteFp = (SdbDeleteFp)mndAcctActionDelete};
S
Shengliang Guan 已提交
42

S
Shengliang Guan 已提交
43 44 45
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_ACCT, mndProcessCreateAcctReq);
  mndSetMsgHandle(pMnode, TDMT_MND_ALTER_ACCT, mndProcessAlterAcctReq);
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_ACCT, mndProcessDropAcctReq);
S
Shengliang Guan 已提交
46 47 48 49 50 51

  return sdbSetTable(pMnode->pSdb, table);
}

void mndCleanupAcct(SMnode *pMnode) {}

S
Shengliang Guan 已提交
52
static int32_t mndCreateDefaultAcct(SMnode *pMnode) {
S
Shengliang Guan 已提交
53 54 55 56 57
  SAcctObj acctObj = {0};
  tstrncpy(acctObj.acct, TSDB_DEFAULT_USER, TSDB_USER_LEN);
  acctObj.createdTime = taosGetTimestampMs();
  acctObj.updateTime = acctObj.createdTime;
  acctObj.acctId = 1;
58
  acctObj.status = 0;
S
Shengliang Guan 已提交
59 60 61 62
  acctObj.cfg = (SAcctCfg){.maxUsers = INT32_MAX,
                           .maxDbs = INT32_MAX,
                           .maxStbs = INT32_MAX,
                           .maxTbs = INT32_MAX,
S
Shengliang Guan 已提交
63
                           .maxTimeSeries = INT32_MAX,
S
Shengliang Guan 已提交
64 65 66 67 68
                           .maxStreams = INT32_MAX,
                           .maxFuncs = INT32_MAX,
                           .maxConsumers = INT32_MAX,
                           .maxConns = INT32_MAX,
                           .maxTopics = INT32_MAX,
S
Shengliang Guan 已提交
69 70 71
                           .maxStorage = INT64_MAX,
                           .accessState = TSDB_VN_ALL_ACCCESS};

S
Shengliang Guan 已提交
72
  SSdbRaw *pRaw = mndAcctActionEncode(&acctObj);
S
Shengliang Guan 已提交
73 74 75
  if (pRaw == NULL) return -1;
  sdbSetRawStatus(pRaw, SDB_STATUS_READY);

76
  mDebug("acct:%s, will be created while deploy sdb, raw:%p", acctObj.acct, pRaw);
S
Shengliang Guan 已提交
77 78 79
  return sdbWrite(pMnode->pSdb, pRaw);
}

S
Shengliang Guan 已提交
80
static SSdbRaw *mndAcctActionEncode(SAcctObj *pAcct) {
81 82
  terrno = TSDB_CODE_OUT_OF_MEMORY;

83
  SSdbRaw *pRaw = sdbAllocRaw(SDB_ACCT, ACCT_VER_NUMBER, sizeof(SAcctObj) + ACCT_RESERVE_SIZE);
84
  if (pRaw == NULL) goto _OVER;
S
Shengliang Guan 已提交
85 86

  int32_t dataPos = 0;
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
  SDB_SET_BINARY(pRaw, dataPos, pAcct->acct, TSDB_USER_LEN, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pAcct->createdTime, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pAcct->updateTime, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pAcct->acctId, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pAcct->status, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.maxUsers, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.maxDbs, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.maxStbs, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.maxTbs, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.maxTimeSeries, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.maxStreams, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.maxFuncs, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.maxConsumers, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.maxConns, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.maxTopics, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pAcct->cfg.maxStorage, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.accessState, _OVER)
104
  SDB_SET_RESERVE(pRaw, dataPos, ACCT_RESERVE_SIZE, _OVER)
105
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
106 107 108

  terrno = 0;

109
_OVER:
110 111 112 113 114
  if (terrno != 0) {
    mError("acct:%s, failed to encode to raw:%p since %s", pAcct->acct, pRaw, terrstr());
    sdbFreeRaw(pRaw);
    return NULL;
  }
S
Shengliang Guan 已提交
115

116
  mTrace("acct:%s, encode to raw:%p, row:%p", pAcct->acct, pRaw, pAcct);
S
Shengliang Guan 已提交
117
  return pRaw;
S
Shengliang Guan 已提交
118 119
}

S
Shengliang Guan 已提交
120
static SSdbRow *mndAcctActionDecode(SSdbRaw *pRaw) {
121 122
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
123
  int8_t sver = 0;
124
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
S
Shengliang Guan 已提交
125

126
  if (sver != ACCT_VER_NUMBER) {
S
Shengliang Guan 已提交
127
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
128
    goto _OVER;
S
Shengliang Guan 已提交
129 130
  }

131
  SSdbRow *pRow = sdbAllocRow(sizeof(SAcctObj));
132
  if (pRow == NULL) goto _OVER;
133

S
Shengliang Guan 已提交
134
  SAcctObj *pAcct = sdbGetRowObj(pRow);
135
  if (pAcct == NULL) goto _OVER;
S
Shengliang Guan 已提交
136

S
Shengliang Guan 已提交
137
  int32_t dataPos = 0;
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
  SDB_GET_BINARY(pRaw, dataPos, pAcct->acct, TSDB_USER_LEN, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pAcct->createdTime, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pAcct->updateTime, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pAcct->acctId, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pAcct->status, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pAcct->cfg.maxUsers, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pAcct->cfg.maxDbs, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pAcct->cfg.maxStbs, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pAcct->cfg.maxTbs, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pAcct->cfg.maxTimeSeries, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pAcct->cfg.maxStreams, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pAcct->cfg.maxFuncs, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pAcct->cfg.maxConsumers, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pAcct->cfg.maxConns, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pAcct->cfg.maxTopics, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pAcct->cfg.maxStorage, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pAcct->cfg.accessState, _OVER)
155
  SDB_GET_RESERVE(pRaw, dataPos, ACCT_RESERVE_SIZE, _OVER)
156 157 158

  terrno = 0;

159
_OVER:
160 161
  if (terrno != 0) {
    mError("acct:%s, failed to decode from raw:%p since %s", pAcct->acct, pRaw, terrstr());
wafwerar's avatar
wafwerar 已提交
162
    taosMemoryFreeClear(pRow);
163 164
    return NULL;
  }
S
Shengliang Guan 已提交
165

166
  mTrace("acct:%s, decode from raw:%p, row:%p", pAcct->acct, pRaw, pAcct);
S
Shengliang Guan 已提交
167
  return pRow;
S
Shengliang Guan 已提交
168
}
S
Shengliang Guan 已提交
169

S
Shengliang Guan 已提交
170
static int32_t mndAcctActionInsert(SSdb *pSdb, SAcctObj *pAcct) {
171
  mTrace("acct:%s, perform insert action, row:%p", pAcct->acct, pAcct);
S
Shengliang Guan 已提交
172 173
  return 0;
}
S
Shengliang Guan 已提交
174

S
Shengliang Guan 已提交
175
static int32_t mndAcctActionDelete(SSdb *pSdb, SAcctObj *pAcct) {
176
  mTrace("acct:%s, perform delete action, row:%p", pAcct->acct, pAcct);
S
Shengliang Guan 已提交
177 178
  return 0;
}
S
Shengliang Guan 已提交
179

S
Shengliang Guan 已提交
180
static int32_t mndAcctActionUpdate(SSdb *pSdb, SAcctObj *pOld, SAcctObj *pNew) {
S
Shengliang Guan 已提交
181
  mTrace("acct:%s, perform update action, old row:%p new row:%p", pOld->acct, pOld, pNew);
S
Shengliang Guan 已提交
182 183 184
  pOld->updateTime = pNew->updateTime;
  pOld->status = pNew->status;
  memcpy(&pOld->cfg, &pNew->cfg, sizeof(SAcctCfg));
S
Shengliang Guan 已提交
185 186
  return 0;
}
S
Shengliang Guan 已提交
187

S
Shengliang Guan 已提交
188
static int32_t mndProcessCreateAcctReq(SNodeMsg *pReq) {
189
  terrno = TSDB_CODE_MSG_NOT_PROCESSED;
S
Shengliang Guan 已提交
190
  mError("failed to process create acct request since %s", terrstr());
S
Shengliang Guan 已提交
191
  return -1;
S
Shengliang Guan 已提交
192 193
}

S
Shengliang Guan 已提交
194
static int32_t mndProcessAlterAcctReq(SNodeMsg *pReq) {
195
  terrno = TSDB_CODE_MSG_NOT_PROCESSED;
S
Shengliang Guan 已提交
196
  mError("failed to process create acct request since %s", terrstr());
S
Shengliang Guan 已提交
197
  return -1;
S
Shengliang Guan 已提交
198 199
}

S
Shengliang Guan 已提交
200
static int32_t mndProcessDropAcctReq(SNodeMsg *pReq) {
201
  terrno = TSDB_CODE_MSG_NOT_PROCESSED;
S
Shengliang Guan 已提交
202
  mError("failed to process create acct request since %s", terrstr());
S
Shengliang Guan 已提交
203 204
  return -1;
}