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
#define TSDB_ACCT_VER_NUMBER   1
S
Shengliang Guan 已提交
21
#define TSDB_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;
S
Shengliang Guan 已提交
58 59 60 61
  acctObj.cfg = (SAcctCfg){.maxUsers = INT32_MAX,
                           .maxDbs = INT32_MAX,
                           .maxStbs = INT32_MAX,
                           .maxTbs = INT32_MAX,
S
Shengliang Guan 已提交
62
                           .maxTimeSeries = INT32_MAX,
S
Shengliang Guan 已提交
63 64 65 66 67
                           .maxStreams = INT32_MAX,
                           .maxFuncs = INT32_MAX,
                           .maxConsumers = INT32_MAX,
                           .maxConns = INT32_MAX,
                           .maxTopics = INT32_MAX,
S
Shengliang Guan 已提交
68 69 70
                           .maxStorage = INT64_MAX,
                           .accessState = TSDB_VN_ALL_ACCCESS};

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

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

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

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

  int32_t dataPos = 0;
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  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)
  SDB_SET_RESERVE(pRaw, dataPos, TSDB_ACCT_RESERVE_SIZE, _OVER)
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
105 106 107

  terrno = 0;

108
_OVER:
109 110 111 112 113
  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 已提交
114

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

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

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

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

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

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

S
Shengliang Guan 已提交
136
  int32_t dataPos = 0;
137 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)
  SDB_GET_RESERVE(pRaw, dataPos, TSDB_ACCT_RESERVE_SIZE, _OVER)
155 156 157

  terrno = 0;

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

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

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

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

S
Shengliang Guan 已提交
179
static int32_t mndAcctActionUpdate(SSdb *pSdb, SAcctObj *pOld, SAcctObj *pNew) {
S
Shengliang Guan 已提交
180
  mTrace("acct:%s, perform update action, old row:%p new row:%p", pOld->acct, pOld, pNew);
S
Shengliang Guan 已提交
181

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) {
S
Shengliang Guan 已提交
189
  terrno = TSDB_CODE_MND_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) {
S
Shengliang Guan 已提交
195
  terrno = TSDB_CODE_MND_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) {
S
Shengliang Guan 已提交
201
  terrno = TSDB_CODE_MND_MSG_NOT_PROCESSED;
S
Shengliang Guan 已提交
202
  mError("failed to process create acct request since %s", terrstr());
S
Shengliang Guan 已提交
203 204
  return -1;
}