mndAcct.c 8.0 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"
19
#include "mndTrans.h"
S
Shengliang Guan 已提交
20

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

S
Shengliang Guan 已提交
24 25 26 27 28
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 已提交
29
static int32_t  mndAcctActionUpdate(SSdb *pSdb, SAcctObj *pOld, SAcctObj *pNew);
S
Shengliang Guan 已提交
30 31 32
static int32_t  mndProcessCreateAcctReq(SRpcMsg *pReq);
static int32_t  mndProcessAlterAcctReq(SRpcMsg *pReq);
static int32_t  mndProcessDropAcctReq(SRpcMsg *pReq);
S
Shengliang Guan 已提交
33 34

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

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

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

void mndCleanupAcct(SMnode *pMnode) {}

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

S
Shengliang Guan 已提交
77
  SSdbRaw *pRaw = mndAcctActionEncode(&acctObj);
S
Shengliang Guan 已提交
78 79 80
  if (pRaw == NULL) return -1;
  sdbSetRawStatus(pRaw, SDB_STATUS_READY);

81 82
  mDebug("acct:%s, will be created when deploying, raw:%p", acctObj.acct, pRaw);

83
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, NULL);
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
  if (pTrans == NULL) {
    mError("acct:%s, failed to create since %s", acctObj.acct, terrstr());
    return -1;
  }
  mDebug("trans:%d, used to create acct:%s", pTrans->id, acctObj.acct);

  if (mndTransAppendCommitlog(pTrans, pRaw) != 0) {
    mError("trans:%d, failed to commit redo log since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }

  if (mndTransPrepare(pMnode, pTrans) != 0) {
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }

  mndTransDrop(pTrans);
  return 0;
S
Shengliang Guan 已提交
104 105
}

S
Shengliang Guan 已提交
106
static SSdbRaw *mndAcctActionEncode(SAcctObj *pAcct) {
107 108
  terrno = TSDB_CODE_OUT_OF_MEMORY;

109
  SSdbRaw *pRaw = sdbAllocRaw(SDB_ACCT, ACCT_VER_NUMBER, sizeof(SAcctObj) + ACCT_RESERVE_SIZE);
110
  if (pRaw == NULL) goto _OVER;
S
Shengliang Guan 已提交
111 112

  int32_t dataPos = 0;
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
  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)
130
  SDB_SET_RESERVE(pRaw, dataPos, ACCT_RESERVE_SIZE, _OVER)
131
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
132 133 134

  terrno = 0;

135
_OVER:
136 137 138 139 140
  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 已提交
141

142
  mTrace("acct:%s, encode to raw:%p, row:%p", pAcct->acct, pRaw, pAcct);
S
Shengliang Guan 已提交
143
  return pRaw;
S
Shengliang Guan 已提交
144 145
}

S
Shengliang Guan 已提交
146
static SSdbRow *mndAcctActionDecode(SSdbRaw *pRaw) {
147 148
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
149
  int8_t sver = 0;
150
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
S
Shengliang Guan 已提交
151

152
  if (sver != ACCT_VER_NUMBER) {
S
Shengliang Guan 已提交
153
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
154
    goto _OVER;
S
Shengliang Guan 已提交
155 156
  }

157
  SSdbRow *pRow = sdbAllocRow(sizeof(SAcctObj));
158
  if (pRow == NULL) goto _OVER;
159

S
Shengliang Guan 已提交
160
  SAcctObj *pAcct = sdbGetRowObj(pRow);
161
  if (pAcct == NULL) goto _OVER;
S
Shengliang Guan 已提交
162

S
Shengliang Guan 已提交
163
  int32_t dataPos = 0;
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  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)
181
  SDB_GET_RESERVE(pRaw, dataPos, ACCT_RESERVE_SIZE, _OVER)
182 183 184

  terrno = 0;

185
_OVER:
186 187
  if (terrno != 0) {
    mError("acct:%s, failed to decode from raw:%p since %s", pAcct->acct, pRaw, terrstr());
wafwerar's avatar
wafwerar 已提交
188
    taosMemoryFreeClear(pRow);
189 190
    return NULL;
  }
S
Shengliang Guan 已提交
191

192
  mTrace("acct:%s, decode from raw:%p, row:%p", pAcct->acct, pRaw, pAcct);
S
Shengliang Guan 已提交
193
  return pRow;
S
Shengliang Guan 已提交
194
}
S
Shengliang Guan 已提交
195

S
Shengliang Guan 已提交
196
static int32_t mndAcctActionInsert(SSdb *pSdb, SAcctObj *pAcct) {
197
  mTrace("acct:%s, perform insert action, row:%p", pAcct->acct, pAcct);
S
Shengliang Guan 已提交
198 199
  return 0;
}
S
Shengliang Guan 已提交
200

S
Shengliang Guan 已提交
201
static int32_t mndAcctActionDelete(SSdb *pSdb, SAcctObj *pAcct) {
202
  mTrace("acct:%s, perform delete action, row:%p", pAcct->acct, pAcct);
S
Shengliang Guan 已提交
203 204
  return 0;
}
S
Shengliang Guan 已提交
205

S
Shengliang Guan 已提交
206
static int32_t mndAcctActionUpdate(SSdb *pSdb, SAcctObj *pOld, SAcctObj *pNew) {
S
Shengliang Guan 已提交
207
  mTrace("acct:%s, perform update action, old row:%p new row:%p", pOld->acct, pOld, pNew);
S
Shengliang Guan 已提交
208 209 210
  pOld->updateTime = pNew->updateTime;
  pOld->status = pNew->status;
  memcpy(&pOld->cfg, &pNew->cfg, sizeof(SAcctCfg));
S
Shengliang Guan 已提交
211 212
  return 0;
}
S
Shengliang Guan 已提交
213

S
Shengliang Guan 已提交
214
static int32_t mndProcessCreateAcctReq(SRpcMsg *pReq) {
215
  terrno = TSDB_CODE_MSG_NOT_PROCESSED;
S
Shengliang Guan 已提交
216
  mError("failed to process create acct request since %s", terrstr());
S
Shengliang Guan 已提交
217
  return -1;
S
Shengliang Guan 已提交
218 219
}

S
Shengliang Guan 已提交
220
static int32_t mndProcessAlterAcctReq(SRpcMsg *pReq) {
221
  terrno = TSDB_CODE_MSG_NOT_PROCESSED;
S
Shengliang Guan 已提交
222
  mError("failed to process create acct request since %s", terrstr());
S
Shengliang Guan 已提交
223
  return -1;
S
Shengliang Guan 已提交
224 225
}

S
Shengliang Guan 已提交
226
static int32_t mndProcessDropAcctReq(SRpcMsg *pReq) {
227
  terrno = TSDB_CODE_MSG_NOT_PROCESSED;
S
Shengliang Guan 已提交
228
  mError("failed to process create acct request since %s", terrstr());
S
Shengliang Guan 已提交
229 230
  return -1;
}