tqPush.c 2.6 KB
Newer Older
L
Liu Jicong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
/*
 * 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/>.
 */

#include "tqPush.h"

int32_t tqPushMgrInit() {
  //
  int8_t old = atomic_val_compare_exchange_8(&tqPushMgmt.inited, 0, 1);
  if (old == 1) return 0;

  tqPushMgmt.timer = taosTmrInit(0, 0, 0, "TQ");
  return 0;
}

void tqPushMgrCleanUp() {
  int8_t old = atomic_val_compare_exchange_8(&tqPushMgmt.inited, 1, 0);
  if (old == 0) return;
  taosTmrStop(tqPushMgmt.timer);
  taosTmrCleanUp(tqPushMgmt.timer);
}

STqPushMgr* tqPushMgrOpen() {
  STqPushMgr* mgr = malloc(sizeof(STqPushMgr));
  if (mgr == NULL) {
    return NULL;
  }
  mgr->pHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
  return mgr;
}

void tqPushMgrClose(STqPushMgr* pushMgr) {
  taosHashCleanup(pushMgr->pHash);
  free(pushMgr);
}

STqClientPusher* tqAddClientPusher(STqPushMgr* pushMgr, SRpcMsg* pMsg, int64_t consumerId, int64_t ttl) {
  STqClientPusher* clientPusher = malloc(sizeof(STqClientPusher));
  if (clientPusher == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
  clientPusher->type = TQ_PUSHER_TYPE__CLIENT;
  clientPusher->pMsg = pMsg;
  clientPusher->consumerId = consumerId;
  clientPusher->ttl = ttl;
  if (taosHashPut(pushMgr->pHash, &consumerId, sizeof(int64_t), &clientPusher, sizeof(void*)) < 0) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    free(clientPusher);
    // TODO send rsp back
    return NULL;
  }
  return clientPusher;
}

STqStreamPusher* tqAddStreamPusher(STqPushMgr* pushMgr, int64_t streamId, SEpSet* pEpSet) {
  STqStreamPusher* streamPusher = malloc(sizeof(STqStreamPusher));
  if (streamPusher == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
  streamPusher->type = TQ_PUSHER_TYPE__STREAM;
  streamPusher->nodeType = 0;
  streamPusher->streamId = streamId;
  memcpy(&streamPusher->epSet, pEpSet, sizeof(SEpSet));

  if (taosHashPut(pushMgr->pHash, &streamId, sizeof(int64_t), &streamPusher, sizeof(void*)) < 0) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    free(streamPusher);
    return NULL;
  }
  return streamPusher;
}