mndAcct.c 6.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"
S
Shengliang Guan 已提交
19

S
Shengliang Guan 已提交
20 21
#define TSDB_ACCT_VER_NUMBER 1
#define TSDB_ACCT_RESERVE_SIZE 64
S
Shengliang Guan 已提交
22

S
Shengliang Guan 已提交
23 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);
static int32_t  mndAcctActionUpdate(SSdb *pSdb, SAcctObj *pOldAcct, SAcctObj *pNewAcct);
S
Shengliang Guan 已提交
29 30 31 32 33 34 35
static int32_t  mndProcessCreateAcctMsg(SMnodeMsg *pMnodeMsg);
static int32_t  mndProcessAlterAcctMsg(SMnodeMsg *pMnodeMsg);
static int32_t  mndProcessDropAcctMsg(SMnodeMsg *pMnodeMsg);

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 43 44 45 46 47 48 49 50 51

  mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_CREATE_ACCT, mndProcessCreateAcctMsg);
  mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_ALTER_ACCT, mndProcessAlterAcctMsg);
  mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_DROP_ACCT, mndProcessDropAcctMsg);

  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 58 59 60 61 62 63 64
  SAcctObj acctObj = {0};
  tstrncpy(acctObj.acct, TSDB_DEFAULT_USER, TSDB_USER_LEN);
  acctObj.createdTime = taosGetTimestampMs();
  acctObj.updateTime = acctObj.createdTime;
  acctObj.acctId = 1;
  acctObj.cfg = (SAcctCfg){.maxUsers = 1024,
                           .maxDbs = 1024,
                           .maxTimeSeries = INT32_MAX,
                           .maxStreams = 8092,
                           .maxStorage = INT64_MAX,
                           .accessState = TSDB_VN_ALL_ACCCESS};

S
Shengliang Guan 已提交
65
  SSdbRaw *pRaw = mndAcctActionEncode(&acctObj);
S
Shengliang Guan 已提交
66 67 68
  if (pRaw == NULL) return -1;
  sdbSetRawStatus(pRaw, SDB_STATUS_READY);

S
Shengliang Guan 已提交
69
  mDebug("acct:%s, will be created while deploy sdb", acctObj.acct);
S
Shengliang Guan 已提交
70 71 72
  return sdbWrite(pMnode->pSdb, pRaw);
}

S
Shengliang Guan 已提交
73
static SSdbRaw *mndAcctActionEncode(SAcctObj *pAcct) {
S
Shengliang Guan 已提交
74
  SSdbRaw *pRaw = sdbAllocRaw(SDB_ACCT, TSDB_ACCT_VER_NUMBER, sizeof(SAcctObj) + TSDB_ACCT_RESERVE_SIZE);
S
Shengliang Guan 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88
  if (pRaw == NULL) return NULL;

  int32_t dataPos = 0;
  SDB_SET_BINARY(pRaw, dataPos, pAcct->acct, TSDB_USER_LEN)
  SDB_SET_INT64(pRaw, dataPos, pAcct->createdTime)
  SDB_SET_INT64(pRaw, dataPos, pAcct->updateTime)
  SDB_SET_INT32(pRaw, dataPos, pAcct->acctId)
  SDB_SET_INT32(pRaw, dataPos, pAcct->status)
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.maxUsers)
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.maxDbs)
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.maxTimeSeries)
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.maxStreams)
  SDB_SET_INT64(pRaw, dataPos, pAcct->cfg.maxStorage)
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.accessState)
S
Shengliang Guan 已提交
89
  SDB_SET_RESERVE(pRaw, dataPos, TSDB_ACCT_RESERVE_SIZE)
S
Shengliang Guan 已提交
90
  SDB_SET_DATALEN(pRaw, dataPos);
S
Shengliang Guan 已提交
91

S
Shengliang Guan 已提交
92
  return pRaw;
S
Shengliang Guan 已提交
93 94
}

S
Shengliang Guan 已提交
95
static SSdbRow *mndAcctActionDecode(SSdbRaw *pRaw) {
S
Shengliang Guan 已提交
96 97
  int8_t sver = 0;
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) return NULL;
S
Shengliang Guan 已提交
98

S
Shengliang Guan 已提交
99
  if (sver != TSDB_ACCT_VER_NUMBER) {
S
Shengliang Guan 已提交
100
    mError("failed to decode acct since %s", terrstr());
S
Shengliang Guan 已提交
101
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
S
Shengliang Guan 已提交
102
    return NULL;
S
Shengliang Guan 已提交
103 104
  }

S
Shengliang Guan 已提交
105
  SSdbRow  *pRow = sdbAllocRow(sizeof(SAcctObj));
S
Shengliang Guan 已提交
106 107
  SAcctObj *pAcct = sdbGetRowObj(pRow);
  if (pAcct == NULL) return NULL;
S
Shengliang Guan 已提交
108

S
Shengliang Guan 已提交
109 110 111 112 113 114 115 116 117 118 119 120
  int32_t dataPos = 0;
  SDB_GET_BINARY(pRaw, pRow, dataPos, pAcct->acct, TSDB_USER_LEN)
  SDB_GET_INT64(pRaw, pRow, dataPos, &pAcct->createdTime)
  SDB_GET_INT64(pRaw, pRow, dataPos, &pAcct->updateTime)
  SDB_GET_INT32(pRaw, pRow, dataPos, &pAcct->acctId)
  SDB_GET_INT32(pRaw, pRow, dataPos, &pAcct->status)
  SDB_GET_INT32(pRaw, pRow, dataPos, &pAcct->cfg.maxUsers)
  SDB_GET_INT32(pRaw, pRow, dataPos, &pAcct->cfg.maxDbs)
  SDB_GET_INT32(pRaw, pRow, dataPos, &pAcct->cfg.maxTimeSeries)
  SDB_GET_INT32(pRaw, pRow, dataPos, &pAcct->cfg.maxStreams)
  SDB_GET_INT64(pRaw, pRow, dataPos, &pAcct->cfg.maxStorage)
  SDB_GET_INT32(pRaw, pRow, dataPos, &pAcct->cfg.accessState)
S
Shengliang Guan 已提交
121
  SDB_GET_RESERVE(pRaw, pRow, dataPos, TSDB_ACCT_RESERVE_SIZE)
S
Shengliang Guan 已提交
122 123

  return pRow;
S
Shengliang Guan 已提交
124
}
S
Shengliang Guan 已提交
125

S
Shengliang Guan 已提交
126
static int32_t mndAcctActionInsert(SSdb *pSdb, SAcctObj *pAcct) {
S
Shengliang Guan 已提交
127 128 129
  mTrace("acct:%s, perform insert action", pAcct->acct);
  return 0;
}
S
Shengliang Guan 已提交
130

S
Shengliang Guan 已提交
131
static int32_t mndAcctActionDelete(SSdb *pSdb, SAcctObj *pAcct) {
S
Shengliang Guan 已提交
132 133 134
  mTrace("acct:%s, perform delete action", pAcct->acct);
  return 0;
}
S
Shengliang Guan 已提交
135

S
Shengliang Guan 已提交
136
static int32_t mndAcctActionUpdate(SSdb *pSdb, SAcctObj *pOldAcct, SAcctObj *pNewAcct) {
S
Shengliang Guan 已提交
137
  mTrace("acct:%s, perform update action", pOldAcct->acct);
S
Shengliang Guan 已提交
138

S
Shengliang Guan 已提交
139 140
  pOldAcct->updateTime = pNewAcct->updateTime;
  pOldAcct->status = pNewAcct->status;
S
Shengliang Guan 已提交
141
  memcpy(&pOldAcct->cfg, &pNewAcct->cfg, sizeof(SAcctInfo));
S
Shengliang Guan 已提交
142 143
  return 0;
}
S
Shengliang Guan 已提交
144

S
Shengliang Guan 已提交
145 146 147 148
static int32_t mndProcessCreateAcctMsg(SMnodeMsg *pMnodeMsg) {
  terrno = TSDB_CODE_MND_MSG_NOT_PROCESSED;
  mError("failed to process create acct msg since %s", terrstr());
  return -1;
S
Shengliang Guan 已提交
149 150
}

S
Shengliang Guan 已提交
151 152 153 154
static int32_t mndProcessAlterAcctMsg(SMnodeMsg *pMnodeMsg) {
  terrno = TSDB_CODE_MND_MSG_NOT_PROCESSED;
  mError("failed to process create acct msg since %s", terrstr());
  return -1;
S
Shengliang Guan 已提交
155 156
}

S
Shengliang Guan 已提交
157 158 159 160 161
static int32_t mndProcessDropAcctMsg(SMnodeMsg *pMnodeMsg) {
  terrno = TSDB_CODE_MND_MSG_NOT_PROCESSED;
  mError("failed to process create acct msg since %s", terrstr());
  return -1;
}