mndOffset.c 10.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 "mndOffset.h"
18
#include "mndPrivilege.h"
L
Liu Jicong 已提交
19 20 21 22 23
#include "mndDb.h"
#include "mndDnode.h"
#include "mndMnode.h"
#include "mndShow.h"
#include "mndStb.h"
L
Liu Jicong 已提交
24
#include "mndTopic.h"
L
Liu Jicong 已提交
25 26 27 28 29 30 31 32 33 34 35
#include "mndTrans.h"
#include "mndUser.h"
#include "mndVgroup.h"
#include "tname.h"

#define MND_OFFSET_VER_NUMBER   1
#define MND_OFFSET_RESERVE_SIZE 64

static int32_t mndOffsetActionInsert(SSdb *pSdb, SMqOffsetObj *pOffset);
static int32_t mndOffsetActionDelete(SSdb *pSdb, SMqOffsetObj *pOffset);
static int32_t mndOffsetActionUpdate(SSdb *pSdb, SMqOffsetObj *pOffset, SMqOffsetObj *pNewOffset);
S
Shengliang Guan 已提交
36
static int32_t mndProcessCommitOffsetReq(SRpcMsg *pReq);
L
Liu Jicong 已提交
37 38

int32_t mndInitOffset(SMnode *pMnode) {
39 40 41 42 43 44 45 46 47
  SSdbTable table = {
      .sdbType = SDB_OFFSET,
      .keyType = SDB_KEY_BINARY,
      .encodeFp = (SdbEncodeFp)mndOffsetActionEncode,
      .decodeFp = (SdbDecodeFp)mndOffsetActionDecode,
      .insertFp = (SdbInsertFp)mndOffsetActionInsert,
      .updateFp = (SdbUpdateFp)mndOffsetActionUpdate,
      .deleteFp = (SdbDeleteFp)mndOffsetActionDelete,
  };
L
Liu Jicong 已提交
48 49 50 51 52 53 54 55

  mndSetMsgHandle(pMnode, TDMT_MND_MQ_COMMIT_OFFSET, mndProcessCommitOffsetReq);

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

void mndCleanupOffset(SMnode *pMnode) {}

L
Liu Jicong 已提交
56 57 58 59 60 61 62 63
bool mndOffsetFromTopic(SMqOffsetObj *pOffset, const char *topic) {
  int32_t i = 0;
  while (pOffset->key[i] != ':') i++;
  while (pOffset->key[i] != ':') i++;
  if (strcmp(&pOffset->key[i + 1], topic) == 0) return true;
  return false;
}

L
Liu Jicong 已提交
64 65 66 67 68 69
bool mndOffsetFromSubKey(SMqOffsetObj *pOffset, const char *subKey) {
  int32_t i = 0;
  while (pOffset->key[i] != ':') i++;
  if (strcmp(&pOffset->key[i + 1], subKey) == 0) return true;
  return false;
}
L
Liu Jicong 已提交
70 71 72 73 74 75 76 77 78
SSdbRaw *mndOffsetActionEncode(SMqOffsetObj *pOffset) {
  terrno = TSDB_CODE_OUT_OF_MEMORY;
  void   *buf = NULL;
  int32_t tlen = tEncodeSMqOffsetObj(NULL, pOffset);
  int32_t size = sizeof(int32_t) + tlen + MND_OFFSET_RESERVE_SIZE;

  SSdbRaw *pRaw = sdbAllocRaw(SDB_OFFSET, MND_OFFSET_VER_NUMBER, size);
  if (pRaw == NULL) goto OFFSET_ENCODE_OVER;

wafwerar's avatar
wafwerar 已提交
79
  buf = taosMemoryMalloc(tlen);
L
Liu Jicong 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93
  if (buf == NULL) goto OFFSET_ENCODE_OVER;

  void *abuf = buf;
  tEncodeSMqOffsetObj(&abuf, pOffset);

  int32_t dataPos = 0;
  SDB_SET_INT32(pRaw, dataPos, tlen, OFFSET_ENCODE_OVER);
  SDB_SET_BINARY(pRaw, dataPos, buf, tlen, OFFSET_ENCODE_OVER);
  SDB_SET_RESERVE(pRaw, dataPos, MND_OFFSET_RESERVE_SIZE, OFFSET_ENCODE_OVER);
  SDB_SET_DATALEN(pRaw, dataPos, OFFSET_ENCODE_OVER);

  terrno = TSDB_CODE_SUCCESS;

OFFSET_ENCODE_OVER:
wafwerar's avatar
wafwerar 已提交
94
  taosMemoryFreeClear(buf);
L
Liu Jicong 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  if (terrno != TSDB_CODE_SUCCESS) {
    mError("offset:%s, failed to encode to raw:%p since %s", pOffset->key, pRaw, terrstr());
    sdbFreeRaw(pRaw);
    return NULL;
  }

  mTrace("offset:%s, encode to raw:%p, row:%p", pOffset->key, pRaw, pOffset);
  return pRaw;
}

SSdbRow *mndOffsetActionDecode(SSdbRaw *pRaw) {
  terrno = TSDB_CODE_OUT_OF_MEMORY;
  void *buf = NULL;

  int8_t sver = 0;
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto OFFSET_DECODE_OVER;

  if (sver != MND_OFFSET_VER_NUMBER) {
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
    goto OFFSET_DECODE_OVER;
  }

  int32_t  size = sizeof(SMqOffsetObj);
  SSdbRow *pRow = sdbAllocRow(size);
  if (pRow == NULL) goto OFFSET_DECODE_OVER;

  SMqOffsetObj *pOffset = sdbGetRowObj(pRow);
  if (pOffset == NULL) goto OFFSET_DECODE_OVER;

  int32_t dataPos = 0;
  int32_t tlen;
  SDB_GET_INT32(pRaw, dataPos, &tlen, OFFSET_DECODE_OVER);
wafwerar's avatar
wafwerar 已提交
127
  buf = taosMemoryMalloc(tlen + 1);
L
Liu Jicong 已提交
128 129 130 131 132 133 134 135 136 137 138
  if (buf == NULL) goto OFFSET_DECODE_OVER;
  SDB_GET_BINARY(pRaw, dataPos, buf, tlen, OFFSET_DECODE_OVER);
  SDB_GET_RESERVE(pRaw, dataPos, MND_OFFSET_RESERVE_SIZE, OFFSET_DECODE_OVER);

  if (tDecodeSMqOffsetObj(buf, pOffset) == NULL) {
    goto OFFSET_DECODE_OVER;
  }

  terrno = TSDB_CODE_SUCCESS;

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

  mTrace("offset:%s, decode from raw:%p, row:%p", pOffset->key, pRaw, pOffset);
  return pRow;
}

L
Liu Jicong 已提交
150
int32_t mndCreateOffsets(STrans *pTrans, const char *cgroup, const char *topicName, const SArray *vgs) {
L
Liu Jicong 已提交
151 152
  int32_t sz = taosArrayGetSize(vgs);
  for (int32_t i = 0; i < sz; i++) {
L
Liu Jicong 已提交
153
    int32_t      vgId = *(int32_t *)taosArrayGet(vgs, i);
L
Liu Jicong 已提交
154
    SMqOffsetObj offsetObj = {0};
L
Liu Jicong 已提交
155
    if (mndMakePartitionKey(offsetObj.key, cgroup, topicName, vgId) < 0) {
L
Liu Jicong 已提交
156 157
      return -1;
    }
L
Liu Jicong 已提交
158
    // TODO assign db
L
Liu Jicong 已提交
159 160 161 162 163 164
    offsetObj.offset = -1;
    SSdbRaw *pOffsetRaw = mndOffsetActionEncode(&offsetObj);
    if (pOffsetRaw == NULL) {
      return -1;
    }
    sdbSetRawStatus(pOffsetRaw, SDB_STATUS_READY);
165
    // commit log or redo log?
L
Liu Jicong 已提交
166 167 168 169 170 171 172
    if (mndTransAppendRedolog(pTrans, pOffsetRaw) < 0) {
      return -1;
    }
  }
  return 0;
}

S
Shengliang Guan 已提交
173
static int32_t mndProcessCommitOffsetReq(SRpcMsg *pMsg) {
L
Liu Jicong 已提交
174 175
  char key[TSDB_PARTITION_KEY_LEN];

S
Shengliang Guan 已提交
176 177
  SMnode              *pMnode = pMsg->info.node;
  char                *msgStr = pMsg->pCont;
L
Liu Jicong 已提交
178
  SMqCMCommitOffsetReq commitOffsetReq;
H
Hongze Cheng 已提交
179
  SDecoder             decoder;
S
Shengliang Guan 已提交
180
  tDecoderInit(&decoder, msgStr, pMsg->contLen);
L
Liu Jicong 已提交
181 182 183

  tDecodeSMqCMCommitOffsetReq(&decoder, &commitOffsetReq);

184
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pMsg);
L
Liu Jicong 已提交
185 186 187

  for (int32_t i = 0; i < commitOffsetReq.num; i++) {
    SMqOffset *pOffset = &commitOffsetReq.offsets[i];
S
Shengliang Guan 已提交
188
    mInfo("commit offset %" PRId64 " to vgId:%d of consumer group %s on topic %s", pOffset->offset, pOffset->vgId,
L
Liu Jicong 已提交
189
          pOffset->cgroup, pOffset->topicName);
L
Liu Jicong 已提交
190
    if (mndMakePartitionKey(key, pOffset->cgroup, pOffset->topicName, pOffset->vgId) < 0) {
L
Liu Jicong 已提交
191
      mError("submit offset to topic %s failed", pOffset->topicName);
L
Liu Jicong 已提交
192 193
      return -1;
    }
L
Liu Jicong 已提交
194
    bool          create = false;
L
Liu Jicong 已提交
195
    SMqOffsetObj *pOffsetObj = mndAcquireOffset(pMnode, key);
L
Liu Jicong 已提交
196
    if (pOffsetObj == NULL) {
L
Liu Jicong 已提交
197 198 199
      SMqTopicObj *pTopic = mndAcquireTopic(pMnode, pOffset->topicName);
      if (pTopic == NULL) {
        terrno = TSDB_CODE_MND_TOPIC_NOT_EXIST;
L
Liu Jicong 已提交
200
        mError("submit offset to topic %s failed since %s", pOffset->topicName, terrstr());
L
Liu Jicong 已提交
201 202
        continue;
      }
L
Liu Jicong 已提交
203
      pOffsetObj = taosMemoryMalloc(sizeof(SMqOffsetObj));
L
Liu Jicong 已提交
204 205
      pOffsetObj->dbUid = pTopic->dbUid;
      mndReleaseTopic(pMnode, pTopic);
L
Liu Jicong 已提交
206 207 208
      memcpy(pOffsetObj->key, key, TSDB_PARTITION_KEY_LEN);
      create = true;
    }
L
Liu Jicong 已提交
209 210 211
    pOffsetObj->offset = pOffset->offset;
    SSdbRaw *pOffsetRaw = mndOffsetActionEncode(pOffsetObj);
    sdbSetRawStatus(pOffsetRaw, SDB_STATUS_READY);
212
    mndTransAppendCommitlog(pTrans, pOffsetRaw);
L
Liu Jicong 已提交
213 214 215 216 217
    if (create) {
      taosMemoryFree(pOffsetObj);
    } else {
      mndReleaseOffset(pMnode, pOffsetObj);
    }
L
Liu Jicong 已提交
218 219
  }

L
Liu Jicong 已提交
220 221
  tDecoderClear(&decoder);

L
Liu Jicong 已提交
222 223 224 225 226 227 228
  if (mndTransPrepare(pMnode, pTrans) != 0) {
    mError("mq-commit-offset-trans:%d, failed to prepare since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }

  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
229
  return TSDB_CODE_ACTION_IN_PROGRESS;
L
Liu Jicong 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243
}

static int32_t mndOffsetActionInsert(SSdb *pSdb, SMqOffsetObj *pOffset) {
  mTrace("offset:%s, perform insert action", pOffset->key);
  return 0;
}

static int32_t mndOffsetActionDelete(SSdb *pSdb, SMqOffsetObj *pOffset) {
  mTrace("offset:%s, perform delete action", pOffset->key);
  return 0;
}

static int32_t mndOffsetActionUpdate(SSdb *pSdb, SMqOffsetObj *pOldOffset, SMqOffsetObj *pNewOffset) {
  mTrace("offset:%s, perform update action", pOldOffset->key);
L
Liu Jicong 已提交
244
  atomic_store_64(&pOldOffset->offset, pNewOffset->offset);
L
Liu Jicong 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
  return 0;
}

SMqOffsetObj *mndAcquireOffset(SMnode *pMnode, const char *key) {
  SSdb         *pSdb = pMnode->pSdb;
  SMqOffsetObj *pOffset = sdbAcquire(pSdb, SDB_OFFSET, key);
  if (pOffset == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
    terrno = TSDB_CODE_MND_OFFSET_NOT_EXIST;
  }
  return pOffset;
}

void mndReleaseOffset(SMnode *pMnode, SMqOffsetObj *pOffset) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pOffset);
}

static void mndCancelGetNextOffset(SMnode *pMnode, void *pIter) {
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
}
L
Liu Jicong 已提交
266 267 268 269 270 271 272 273 274

static int32_t mndSetDropOffsetCommitLogs(SMnode *pMnode, STrans *pTrans, SMqOffsetObj *pOffset) {
  SSdbRaw *pCommitRaw = mndOffsetActionEncode(pOffset);
  if (pCommitRaw == NULL) return -1;
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) return -1;
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED) != 0) return -1;
  return 0;
}

L
Liu Jicong 已提交
275 276 277 278 279 280 281 282
static int32_t mndSetDropOffsetRedoLogs(SMnode *pMnode, STrans *pTrans, SMqOffsetObj *pOffset) {
  SSdbRaw *pRedoRaw = mndOffsetActionEncode(pOffset);
  if (pRedoRaw == NULL) return -1;
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) return -1;
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_DROPPED) != 0) return -1;
  return 0;
}

L
Liu Jicong 已提交
283 284 285 286 287 288 289
int32_t mndDropOffsetByDB(SMnode *pMnode, STrans *pTrans, SDbObj *pDb) {
  int32_t code = -1;
  SSdb   *pSdb = pMnode->pSdb;

  void         *pIter = NULL;
  SMqOffsetObj *pOffset = NULL;
  while (1) {
L
Liu Jicong 已提交
290
    pIter = sdbFetch(pSdb, SDB_OFFSET, pIter, (void **)&pOffset);
L
Liu Jicong 已提交
291 292 293 294 295 296 297 298
    if (pIter == NULL) break;

    if (pOffset->dbUid != pDb->uid) {
      sdbRelease(pSdb, pOffset);
      continue;
    }

    if (mndSetDropOffsetCommitLogs(pMnode, pTrans, pOffset) < 0) {
L
Liu Jicong 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
      sdbRelease(pSdb, pOffset);
      goto END;
    }

    sdbRelease(pSdb, pOffset);
  }

  code = 0;
END:
  return code;
}

int32_t mndDropOffsetByTopic(SMnode *pMnode, STrans *pTrans, const char *topic) {
  int32_t code = -1;
  SSdb   *pSdb = pMnode->pSdb;

  void         *pIter = NULL;
  SMqOffsetObj *pOffset = NULL;
  while (1) {
    pIter = sdbFetch(pSdb, SDB_OFFSET, pIter, (void **)&pOffset);
    if (pIter == NULL) break;

    if (!mndOffsetFromTopic(pOffset, topic)) {
      sdbRelease(pSdb, pOffset);
      continue;
    }

L
Liu Jicong 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
    if (mndSetDropOffsetCommitLogs(pMnode, pTrans, pOffset) < 0) {
      sdbRelease(pSdb, pOffset);
      goto END;
    }

    sdbRelease(pSdb, pOffset);
  }

  code = 0;
END:
  return code;
}

int32_t mndDropOffsetBySubKey(SMnode *pMnode, STrans *pTrans, const char *subKey) {
  int32_t code = -1;
  SSdb   *pSdb = pMnode->pSdb;

  void         *pIter = NULL;
  SMqOffsetObj *pOffset = NULL;
  while (1) {
    pIter = sdbFetch(pSdb, SDB_OFFSET, pIter, (void **)&pOffset);
    if (pIter == NULL) break;

    if (!mndOffsetFromSubKey(pOffset, subKey)) {
      sdbRelease(pSdb, pOffset);
      continue;
    }

    if (mndSetDropOffsetCommitLogs(pMnode, pTrans, pOffset) < 0) {
L
Liu Jicong 已提交
355
      sdbRelease(pSdb, pOffset);
L
Liu Jicong 已提交
356 357
      goto END;
    }
L
Liu Jicong 已提交
358 359

    sdbRelease(pSdb, pOffset);
L
Liu Jicong 已提交
360 361 362 363 364 365
  }

  code = 0;
END:
  return code;
}