mndAcct.c 8.1 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
  mDebug("acct:%s, will be created while deploy sdb, raw:%p", acctObj.acct, pRaw);
82
#if 0
S
Shengliang Guan 已提交
83
  return sdbWrite(pMnode->pSdb, pRaw);
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
#else
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_TYPE_CREATE_ACCT, NULL);
  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;
  }
  sdbSetRawStatus(pRaw, SDB_STATUS_READY);

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

  mndTransDrop(pTrans);
  return 0;
#endif
S
Shengliang Guan 已提交
108 109
}

S
Shengliang Guan 已提交
110
static SSdbRaw *mndAcctActionEncode(SAcctObj *pAcct) {
111 112
  terrno = TSDB_CODE_OUT_OF_MEMORY;

113
  SSdbRaw *pRaw = sdbAllocRaw(SDB_ACCT, ACCT_VER_NUMBER, sizeof(SAcctObj) + ACCT_RESERVE_SIZE);
114
  if (pRaw == NULL) goto _OVER;
S
Shengliang Guan 已提交
115 116

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

  terrno = 0;

139
_OVER:
140 141 142 143 144
  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 已提交
145

146
  mTrace("acct:%s, encode to raw:%p, row:%p", pAcct->acct, pRaw, pAcct);
S
Shengliang Guan 已提交
147
  return pRaw;
S
Shengliang Guan 已提交
148 149
}

S
Shengliang Guan 已提交
150
static SSdbRow *mndAcctActionDecode(SSdbRaw *pRaw) {
151 152
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
153
  int8_t sver = 0;
154
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
S
Shengliang Guan 已提交
155

156
  if (sver != ACCT_VER_NUMBER) {
S
Shengliang Guan 已提交
157
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
158
    goto _OVER;
S
Shengliang Guan 已提交
159 160
  }

161
  SSdbRow *pRow = sdbAllocRow(sizeof(SAcctObj));
162
  if (pRow == NULL) goto _OVER;
163

S
Shengliang Guan 已提交
164
  SAcctObj *pAcct = sdbGetRowObj(pRow);
165
  if (pAcct == NULL) goto _OVER;
S
Shengliang Guan 已提交
166

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

  terrno = 0;

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

196
  mTrace("acct:%s, decode from raw:%p, row:%p", pAcct->acct, pRaw, pAcct);
S
Shengliang Guan 已提交
197
  return pRow;
S
Shengliang Guan 已提交
198
}
S
Shengliang Guan 已提交
199

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

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

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

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

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

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