mndConsumer.c 5.8 KB
Newer Older
L
Liu Jicong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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/>.
 */

#define _DEFAULT_SOURCE
L
Liu Jicong 已提交
17

L
Liu Jicong 已提交
18 19 20 21 22 23 24 25 26 27
#include "mndConsumer.h"
#include "mndDb.h"
#include "mndDnode.h"
#include "mndMnode.h"
#include "mndShow.h"
#include "mndStb.h"
#include "mndTopic.h"
#include "mndTrans.h"
#include "mndUser.h"
#include "mndVgroup.h"
L
Liu Jicong 已提交
28
#include "tcompare.h"
L
Liu Jicong 已提交
29 30 31 32 33
#include "tname.h"

#define MND_CONSUMER_VER_NUMBER 1
#define MND_CONSUMER_RESERVE_SIZE 64

L
Liu Jicong 已提交
34 35 36 37 38 39 40
static int32_t mndConsumerActionInsert(SSdb *pSdb, SMqConsumerObj *pConsumer);
static int32_t mndConsumerActionDelete(SSdb *pSdb, SMqConsumerObj *pConsumer);
static int32_t mndConsumerActionUpdate(SSdb *pSdb, SMqConsumerObj *pConsumer, SMqConsumerObj *pNewConsumer);
static int32_t mndProcessConsumerMetaMsg(SMnodeMsg *pMsg);
static int32_t mndGetConsumerMeta(SMnodeMsg *pMsg, SShowObj *pShow, STableMetaRsp *pMeta);
static int32_t mndRetrieveConsumer(SMnodeMsg *pMsg, SShowObj *pShow, char *data, int32_t rows);
static void    mndCancelGetNextConsumer(SMnode *pMnode, void *pIter);
L
Liu Jicong 已提交
41 42 43

int32_t mndInitConsumer(SMnode *pMnode) {
  SSdbTable table = {.sdbType = SDB_CONSUMER,
L
Liu Jicong 已提交
44
                     .keyType = SDB_KEY_INT64,
L
Liu Jicong 已提交
45 46 47 48 49 50 51 52 53 54 55
                     .encodeFp = (SdbEncodeFp)mndConsumerActionEncode,
                     .decodeFp = (SdbDecodeFp)mndConsumerActionDecode,
                     .insertFp = (SdbInsertFp)mndConsumerActionInsert,
                     .updateFp = (SdbUpdateFp)mndConsumerActionUpdate,
                     .deleteFp = (SdbDeleteFp)mndConsumerActionDelete};

  return sdbSetTable(pMnode->pSdb, table);
}

void mndCleanupConsumer(SMnode *pMnode) {}

L
Liu Jicong 已提交
56 57 58 59 60 61 62 63
SMqConsumerObj* mndCreateConsumer(int64_t consumerId, const char* cgroup) {
  SMqConsumerObj* pConsumer = malloc(sizeof(SMqConsumerObj));
  if (pConsumer == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
  pConsumer->epoch = 1;
  pConsumer->consumerId = consumerId;
L
Liu Jicong 已提交
64
  pConsumer->status = MQ_CONSUMER_STATUS__INIT;
L
Liu Jicong 已提交
65 66 67 68 69
  strcpy(pConsumer->cgroup, cgroup);
  taosInitRWLatch(&pConsumer->lock);
  return pConsumer;
}

L
Liu Jicong 已提交
70 71
SSdbRaw *mndConsumerActionEncode(SMqConsumerObj *pConsumer) {
  terrno = TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
72
  void* buf = NULL;
L
Liu Jicong 已提交
73
  int32_t tlen = tEncodeSMqConsumerObj(NULL, pConsumer);
L
Liu Jicong 已提交
74 75 76
  int32_t size = sizeof(int32_t) + tlen + MND_CONSUMER_RESERVE_SIZE;

  SSdbRaw *pRaw = sdbAllocRaw(SDB_CONSUMER, MND_CONSUMER_VER_NUMBER, size);
77
  if (pRaw == NULL) goto CM_ENCODE_OVER;
L
Liu Jicong 已提交
78

L
Liu Jicong 已提交
79
  buf = malloc(tlen);
L
Liu Jicong 已提交
80 81
  if (buf == NULL) goto CM_ENCODE_OVER;

L
Liu Jicong 已提交
82
  void *abuf = buf;
L
Liu Jicong 已提交
83 84
  tEncodeSMqConsumerObj(&abuf, pConsumer);

L
Liu Jicong 已提交
85
  int32_t dataPos = 0;
L
Liu Jicong 已提交
86 87
  SDB_SET_INT32(pRaw, dataPos, tlen, CM_ENCODE_OVER);
  SDB_SET_BINARY(pRaw, dataPos, buf, tlen, CM_ENCODE_OVER);
L
Liu Jicong 已提交
88 89
  SDB_SET_RESERVE(pRaw, dataPos, MND_CONSUMER_RESERVE_SIZE, CM_ENCODE_OVER);
  SDB_SET_DATALEN(pRaw, dataPos, CM_ENCODE_OVER);
L
Liu Jicong 已提交
90

L
Liu Jicong 已提交
91 92
  terrno = TSDB_CODE_SUCCESS;

93
CM_ENCODE_OVER:
L
Liu Jicong 已提交
94
  tfree(buf);
95
  if (terrno != 0) {
L
Liu Jicong 已提交
96
    mError("consumer:%ld, failed to encode to raw:%p since %s", pConsumer->consumerId, pRaw, terrstr());
97 98 99 100
    sdbFreeRaw(pRaw);
    return NULL;
  }

L
Liu Jicong 已提交
101
  mTrace("consumer:%ld, encode to raw:%p, row:%p", pConsumer->consumerId, pRaw, pConsumer);
L
Liu Jicong 已提交
102 103 104
  return pRaw;
}

L
Liu Jicong 已提交
105
SSdbRow *mndConsumerActionDecode(SSdbRaw *pRaw) {
106
  terrno = TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
107
  void* buf = NULL;
108

L
Liu Jicong 已提交
109
  int8_t sver = 0;
L
Liu Jicong 已提交
110
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto CM_DECODE_OVER;
L
Liu Jicong 已提交
111 112 113

  if (sver != MND_CONSUMER_VER_NUMBER) {
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
L
Liu Jicong 已提交
114
    goto CM_DECODE_OVER;
L
Liu Jicong 已提交
115 116
  }

L
Liu Jicong 已提交
117
  SSdbRow *pRow = sdbAllocRow(sizeof(SMqConsumerObj));
L
Liu Jicong 已提交
118
  if (pRow == NULL) goto CM_DECODE_OVER;
119

L
Liu Jicong 已提交
120
  SMqConsumerObj *pConsumer = sdbGetRowObj(pRow);
L
Liu Jicong 已提交
121
  if (pConsumer == NULL) goto CM_DECODE_OVER;
L
Liu Jicong 已提交
122 123

  int32_t dataPos = 0;
L
Liu Jicong 已提交
124
  int32_t len;
L
Liu Jicong 已提交
125
  SDB_GET_INT32(pRaw, dataPos, &len, CM_DECODE_OVER);
L
Liu Jicong 已提交
126
  buf = malloc(len);
L
Liu Jicong 已提交
127 128 129
  if (buf == NULL) goto CM_DECODE_OVER;
  SDB_GET_BINARY(pRaw, dataPos, buf, len, CM_DECODE_OVER);
  SDB_GET_RESERVE(pRaw, dataPos, MND_CONSUMER_RESERVE_SIZE, CM_DECODE_OVER);
L
Liu Jicong 已提交
130

L
Liu Jicong 已提交
131 132 133
  if (tDecodeSMqConsumerObj(buf, pConsumer) == NULL) {
    goto CM_DECODE_OVER;
  }
L
Liu Jicong 已提交
134 135 136

  terrno = TSDB_CODE_SUCCESS;

L
Liu Jicong 已提交
137
CM_DECODE_OVER:
L
Liu Jicong 已提交
138
  tfree(buf);
L
Liu Jicong 已提交
139
  if (terrno != TSDB_CODE_SUCCESS) {
L
Liu Jicong 已提交
140
    mError("consumer:%ld, failed to decode from raw:%p since %s", pConsumer->consumerId, pRaw, terrstr());
141 142
    tfree(pRow);
    return NULL;
L
Liu Jicong 已提交
143
  }
L
Liu Jicong 已提交
144 145 146 147

  return pRow;
}

L
Liu Jicong 已提交
148 149
static int32_t mndConsumerActionInsert(SSdb *pSdb, SMqConsumerObj *pConsumer) {
  mTrace("consumer:%ld, perform insert action", pConsumer->consumerId);
L
Liu Jicong 已提交
150 151 152
  return 0;
}

L
Liu Jicong 已提交
153 154
static int32_t mndConsumerActionDelete(SSdb *pSdb, SMqConsumerObj *pConsumer) {
  mTrace("consumer:%ld, perform delete action", pConsumer->consumerId);
L
Liu Jicong 已提交
155 156 157
  return 0;
}

L
Liu Jicong 已提交
158 159
static int32_t mndConsumerActionUpdate(SSdb *pSdb, SMqConsumerObj *pOldConsumer, SMqConsumerObj *pNewConsumer) {
  mTrace("consumer:%ld, perform update action", pOldConsumer->consumerId);
L
Liu Jicong 已提交
160 161

  // TODO handle update
L
Liu Jicong 已提交
162 163
  /*taosWLockLatch(&pOldConsumer->lock);*/
  /*taosWUnLockLatch(&pOldConsumer->lock);*/
L
Liu Jicong 已提交
164 165 166 167

  return 0;
}

L
Liu Jicong 已提交
168
SMqConsumerObj *mndAcquireConsumer(SMnode *pMnode, int64_t consumerId) {
L
Liu Jicong 已提交
169 170
  SSdb           *pSdb = pMnode->pSdb;
  SMqConsumerObj *pConsumer = sdbAcquire(pSdb, SDB_CONSUMER, &consumerId);
L
Liu Jicong 已提交
171 172 173 174 175 176
  if (pConsumer == NULL) {
    /*terrno = TSDB_CODE_MND_CONSUMER_NOT_EXIST;*/
  }
  return pConsumer;
}

L
Liu Jicong 已提交
177
void mndReleaseConsumer(SMnode *pMnode, SMqConsumerObj *pConsumer) {
L
Liu Jicong 已提交
178 179 180
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pConsumer);
}