mndAcct.c 8.2 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

S
Shengliang Guan 已提交
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 ACCT_ENCODE_OVER;
S
Shengliang Guan 已提交
84 85

  int32_t dataPos = 0;
86 87 88 89 90 91 92
  SDB_SET_BINARY(pRaw, dataPos, pAcct->acct, TSDB_USER_LEN, ACCT_ENCODE_OVER)
  SDB_SET_INT64(pRaw, dataPos, pAcct->createdTime, ACCT_ENCODE_OVER)
  SDB_SET_INT64(pRaw, dataPos, pAcct->updateTime, ACCT_ENCODE_OVER)
  SDB_SET_INT32(pRaw, dataPos, pAcct->acctId, ACCT_ENCODE_OVER)
  SDB_SET_INT32(pRaw, dataPos, pAcct->status, ACCT_ENCODE_OVER)
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.maxUsers, ACCT_ENCODE_OVER)
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.maxDbs, ACCT_ENCODE_OVER)
S
Shengliang Guan 已提交
93 94
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.maxStbs, ACCT_ENCODE_OVER)
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.maxTbs, ACCT_ENCODE_OVER)
95 96
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.maxTimeSeries, ACCT_ENCODE_OVER)
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.maxStreams, ACCT_ENCODE_OVER)
S
Shengliang Guan 已提交
97 98 99 100
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.maxFuncs, ACCT_ENCODE_OVER)
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.maxConsumers, ACCT_ENCODE_OVER)
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.maxConns, ACCT_ENCODE_OVER)
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.maxTopics, ACCT_ENCODE_OVER)
101 102 103 104 105 106 107 108 109 110 111 112 113
  SDB_SET_INT64(pRaw, dataPos, pAcct->cfg.maxStorage, ACCT_ENCODE_OVER)
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.accessState, ACCT_ENCODE_OVER)
  SDB_SET_RESERVE(pRaw, dataPos, TSDB_ACCT_RESERVE_SIZE, ACCT_ENCODE_OVER)
  SDB_SET_DATALEN(pRaw, dataPos, ACCT_ENCODE_OVER)

  terrno = 0;

ACCT_ENCODE_OVER:
  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 ACCT_DECODE_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 ACCT_DECODE_OVER;
S
Shengliang Guan 已提交
128 129
  }

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

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

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

  terrno = 0;

ACCT_DECODE_OVER:
  if (terrno != 0) {
    mError("acct:%s, failed to decode from raw:%p since %s", pAcct->acct, pRaw, terrstr());
    tfree(pRow);
    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;
}