mndConsumer.c 5.9 KB
Newer Older
L
Liu Jicong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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
#include "mndConsumer.h"
S
Shengliang Guan 已提交
18
#include "mndAuth.h"
L
Liu Jicong 已提交
19 20 21 22 23 24 25 26 27
#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
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);
S
Shengliang Guan 已提交
37
static int32_t mndProcessConsumerMetaMsg(SNodeMsg *pMsg);
38
static int32_t mndRetrieveConsumer(SNodeMsg *pMsg, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
L
Liu Jicong 已提交
39
static void    mndCancelGetNextConsumer(SMnode *pMnode, void *pIter);
L
Liu Jicong 已提交
40 41 42

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

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

void mndCleanupConsumer(SMnode *pMnode) {}

S
Shengliang Guan 已提交
55
SMqConsumerObj *mndCreateConsumer(int64_t consumerId, const char *cgroup) {
wafwerar's avatar
wafwerar 已提交
56
  SMqConsumerObj *pConsumer = taosMemoryCalloc(1, sizeof(SMqConsumerObj));
L
Liu Jicong 已提交
57 58 59 60
  if (pConsumer == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
S
Shengliang Guan 已提交
61 62

  pConsumer->recentRemovedTopics = taosArrayInit(1, 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;
S
Shengliang Guan 已提交
73 74

  void   *buf = NULL;
L
Liu Jicong 已提交
75
  int32_t tlen = tEncodeSMqConsumerObj(NULL, pConsumer);
L
Liu Jicong 已提交
76 77 78
  int32_t size = sizeof(int32_t) + tlen + MND_CONSUMER_RESERVE_SIZE;

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

wafwerar's avatar
wafwerar 已提交
81
  buf = taosMemoryMalloc(tlen);
L
Liu Jicong 已提交
82 83
  if (buf == NULL) goto CM_ENCODE_OVER;

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

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

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

95
CM_ENCODE_OVER:
wafwerar's avatar
wafwerar 已提交
96
  taosMemoryFreeClear(buf);
97
  if (terrno != 0) {
98
    mError("consumer:%" PRId64 ", failed to encode to raw:%p since %s", pConsumer->consumerId, pRaw, terrstr());
99 100 101 102
    sdbFreeRaw(pRaw);
    return NULL;
  }

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

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

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

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

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

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

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

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

  terrno = TSDB_CODE_SUCCESS;

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

  return pRow;
}

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

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

L
Liu Jicong 已提交
160
static int32_t mndConsumerActionUpdate(SSdb *pSdb, SMqConsumerObj *pOldConsumer, SMqConsumerObj *pNewConsumer) {
161
  mTrace("consumer:%" PRId64 ", perform update action", pOldConsumer->consumerId);
L
Liu Jicong 已提交
162
  /*taosWLockLatch(&pOldConsumer->lock);*/
L
Liu Jicong 已提交
163
  atomic_add_fetch_32(&pOldConsumer->epoch, 1);
L
Liu Jicong 已提交
164
  /*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);
}