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
#define SDB_ACCT_VER 1
S
Shengliang Guan 已提交
21

S
Shengliang Guan 已提交
22 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);
static int32_t  mndAcctActionUpdate(SSdb *pSdb, SAcctObj *pOldAcct, SAcctObj *pNewAcct);
S
Shengliang Guan 已提交
28 29 30 31 32 33 34
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 已提交
35 36 37 38 39 40
                     .deployFp = mndCreateDefaultAcct,
                     .encodeFp = (SdbEncodeFp)mndAcctActionEncode,
                     .decodeFp = (SdbDecodeFp)mndAcctActionDecode,
                     .insertFp = (SdbInsertFp)mndAcctActionInsert,
                     .updateFp = (SdbUpdateFp)mndAcctActionUpdate,
                     .deleteFp = (SdbDeleteFp)mndAcctActionDelete};
S
Shengliang Guan 已提交
41 42 43 44 45 46 47 48 49 50

  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 已提交
51
static int32_t mndCreateDefaultAcct(SMnode *pMnode) {
S
Shengliang Guan 已提交
52 53 54 55 56 57 58 59 60 61 62 63
  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 已提交
64
  SSdbRaw *pRaw = mndAcctActionEncode(&acctObj);
S
Shengliang Guan 已提交
65 66 67
  if (pRaw == NULL) return -1;
  sdbSetRawStatus(pRaw, SDB_STATUS_READY);

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

S
Shengliang Guan 已提交
72
static SSdbRaw *mndAcctActionEncode(SAcctObj *pAcct) {
S
Shengliang Guan 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
  SSdbRaw *pRaw = sdbAllocRaw(SDB_ACCT, SDB_ACCT_VER, sizeof(SAcctObj));
  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)
  SDB_SET_DATALEN(pRaw, dataPos);
S
Shengliang Guan 已提交
89

S
Shengliang Guan 已提交
90
  return pRaw;
S
Shengliang Guan 已提交
91 92
}

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

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

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

S
Shengliang Guan 已提交
107 108 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)

  return pRow;
S
Shengliang Guan 已提交
121
}
S
Shengliang Guan 已提交
122

S
Shengliang Guan 已提交
123
static int32_t mndAcctActionInsert(SSdb *pSdb, SAcctObj *pAcct) {
S
Shengliang Guan 已提交
124 125 126 127
  mTrace("acct:%s, perform insert action", pAcct->acct);
  memset(&pAcct->info, 0, sizeof(SAcctInfo));
  return 0;
}
S
Shengliang Guan 已提交
128

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

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

S
Shengliang Guan 已提交
137 138 139 140 141 142
  memcpy(pOldAcct->acct, pNewAcct->acct, TSDB_USER_LEN);
  pOldAcct->createdTime = pNewAcct->createdTime;
  pOldAcct->updateTime = pNewAcct->updateTime;
  pOldAcct->acctId = pNewAcct->acctId;
  pOldAcct->status = pNewAcct->status;
  pOldAcct->cfg = pNewAcct->cfg;
S
Shengliang Guan 已提交
143 144
  return 0;
}
S
Shengliang Guan 已提交
145

S
Shengliang Guan 已提交
146 147 148 149
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 已提交
150 151
}

S
Shengliang Guan 已提交
152 153 154 155
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 已提交
156 157
}

S
Shengliang Guan 已提交
158 159 160 161 162
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;
}