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
SMqConsumerObj* mndCreateConsumer(int64_t consumerId, const char* cgroup) {
  SMqConsumerObj* pConsumer = malloc(sizeof(SMqConsumerObj));
  if (pConsumer == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
62
  pConsumer->recentRemovedTopics = taosArrayInit(0, sizeof(char*));
L
Liu Jicong 已提交
63 64
  pConsumer->epoch = 1;
  pConsumer->consumerId = consumerId;
L
Liu Jicong 已提交
65
  atomic_store_32(&pConsumer->status, MQ_CONSUMER_STATUS__INIT);
L
Liu Jicong 已提交
66 67 68 69 70
  strcpy(pConsumer->cgroup, cgroup);
  taosInitRWLatch(&pConsumer->lock);
  return pConsumer;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

  int32_t dataPos = 0;
L
Liu Jicong 已提交
125
  int32_t len;
L
Liu Jicong 已提交
126
  SDB_GET_INT32(pRaw, dataPos, &len, CM_DECODE_OVER);
L
Liu Jicong 已提交
127
  buf = malloc(len);
L
Liu Jicong 已提交
128 129 130
  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 已提交
131

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

  terrno = TSDB_CODE_SUCCESS;

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

  return pRow;
}

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

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

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

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

  return 0;
}

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

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