clientHb.c 6.4 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/>.
 */

#include "clientHb.h"
L
Liu Jicong 已提交
17
#include "trpc.h"
L
Liu Jicong 已提交
18 19 20 21 22 23

static SClientHbMgr clientHbMgr = {0};

static int32_t hbCreateThread();
static void    hbStopThread();

L
Liu Jicong 已提交
24 25 26 27 28 29 30 31
static int32_t hbMqHbRspHandle(SClientHbRsp* pReq) {
  return 0;
}

void hbMgrInitMqHbRspHandle() {
  clientHbMgr.handle[HEARTBEAT_TYPE_MQ] = hbMqHbRspHandle;
}

L
Liu Jicong 已提交
32 33 34 35 36
static FORCE_INLINE void hbMgrInitHandle() {
  // init all handle
  hbMgrInitMqHbRspHandle();
}

L
Liu Jicong 已提交
37
SClientHbBatchReq* hbGatherAllInfo(SAppHbMgr *pAppHbMgr) {
L
Liu Jicong 已提交
38
  SClientHbBatchReq* pReq = malloc(sizeof(SClientHbBatchReq));
L
Liu Jicong 已提交
39
  if (pReq == NULL) {
L
Liu Jicong 已提交
40 41 42
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    return NULL;
  }
L
Liu Jicong 已提交
43
  int32_t connKeyCnt = atomic_load_32(&pAppHbMgr->connKeyCnt);
L
Liu Jicong 已提交
44 45
  pReq->reqs = taosArrayInit(connKeyCnt, sizeof(SClientHbReq));

L
Liu Jicong 已提交
46
  void *pIter = taosHashIterate(pAppHbMgr->activeInfo, NULL);
L
Liu Jicong 已提交
47 48 49 50
  while (pIter != NULL) {
    taosArrayPush(pReq->reqs, pIter);
    SClientHbReq* pOneReq = pIter;
    taosHashClear(pOneReq->info);
L
Liu Jicong 已提交
51

L
Liu Jicong 已提交
52
    pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter);
L
Liu Jicong 已提交
53 54
  }

L
Liu Jicong 已提交
55
  pIter = taosHashIterate(pAppHbMgr->getInfoFuncs, NULL);
L
Liu Jicong 已提交
56 57 58 59 60 61
  while (pIter != NULL) {
    FGetConnInfo getConnInfoFp = (FGetConnInfo)pIter;
    SClientHbKey connKey;
    taosHashCopyKey(pIter, &connKey);
    getConnInfoFp(connKey, NULL);

L
Liu Jicong 已提交
62
    pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter);
L
Liu Jicong 已提交
63 64 65 66 67 68 69 70
  }

  return pReq;
}

static void* hbThreadFunc(void* param) {
  setThreadName("hb");
  while (1) {
L
Liu Jicong 已提交
71 72 73 74 75
    int8_t threadStop = atomic_load_8(&clientHbMgr.threadStop);
    if(threadStop) {
      break;
    }

L
Liu Jicong 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
    for(int i = 0; i < sz; i++) {
      SAppHbMgr* pAppHbMgr = taosArrayGet(clientHbMgr.appHbMgrs, i);
      SClientHbBatchReq* pReq = hbGatherAllInfo(pAppHbMgr);
      void* reqStr = NULL;
      int tlen = tSerializeSClientHbBatchReq(&reqStr, pReq);
      SMsgSendInfo info;
      /*info.fp = hbHandleRsp;*/

      int64_t transporterId = 0;
      asyncSendMsgToServer(pAppHbMgr->transporter, &pAppHbMgr->epSet, &transporterId, &info);
      tFreeClientHbBatchReq(pReq);

      atomic_add_fetch_32(&pAppHbMgr->reportCnt, 1);
      taosMsleep(HEARTBEAT_INTERVAL);
    }
L
Liu Jicong 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
  }
  return NULL;
}

static int32_t hbCreateThread() {
  pthread_attr_t thAttr;
  pthread_attr_init(&thAttr);
  pthread_attr_setdetachstate(&thAttr, PTHREAD_CREATE_JOINABLE);

  if (pthread_create(&clientHbMgr.thread, &thAttr, hbThreadFunc, NULL) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }
  pthread_attr_destroy(&thAttr);
  return 0;
}

L
Liu Jicong 已提交
109 110 111 112
static void hbStopThread() {
  atomic_store_8(&clientHbMgr.threadStop, 1);
}

L
Liu Jicong 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
SAppHbMgr* appHbMgrInit(void* transporter, SEpSet epSet) {
  SAppHbMgr* pAppHbMgr = malloc(sizeof(SAppHbMgr)); 
  if (pAppHbMgr == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
  // init stat
  pAppHbMgr->startTime = taosGetTimestampMs();

  // init connection info
  pAppHbMgr->transporter = transporter;
  pAppHbMgr->epSet = epSet;

  // init hash info
  pAppHbMgr->activeInfo = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
  pAppHbMgr->activeInfo->freeFp = tFreeClientHbReq;
  // init getInfoFunc
  pAppHbMgr->getInfoFuncs = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);

  taosArrayPush(clientHbMgr.appHbMgrs, &pAppHbMgr);
  return pAppHbMgr;
}

void appHbMgrCleanup(SAppHbMgr* pAppHbMgr) {
  pthread_mutex_lock(&clientHbMgr.lock);

  int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
  for (int i = 0; i < sz; i++) {
    SAppHbMgr* pTarget = taosArrayGet(clientHbMgr.appHbMgrs, i);
    if (pAppHbMgr == pTarget) {
      taosHashCleanup(pTarget->activeInfo);
      taosHashCleanup(pTarget->getInfoFuncs);
    }
  }

  pthread_mutex_unlock(&clientHbMgr.lock);
}

int hbMgrInit() {
L
Liu Jicong 已提交
152 153 154 155
  // init once
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 0, 1);
  if (old == 1) return 0;

L
Liu Jicong 已提交
156 157
  clientHbMgr.appHbMgrs = taosArrayInit(0, sizeof(void*));
  pthread_mutex_init(&clientHbMgr.lock, NULL);
L
Liu Jicong 已提交
158 159 160 161 162

  // init handle funcs
  hbMgrInitHandle();

  // init backgroud thread
L
Liu Jicong 已提交
163 164
  hbCreateThread();

L
Liu Jicong 已提交
165 166 167 168
  return 0;
}

void hbMgrCleanUp() {
L
Liu Jicong 已提交
169
  // destroy all appHbMgr
L
Liu Jicong 已提交
170 171 172
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 1, 0);
  if (old == 0) return;

L
Liu Jicong 已提交
173 174
  taosArrayDestroy(clientHbMgr.appHbMgrs);

L
Liu Jicong 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
}

int hbHandleRsp(SClientHbBatchRsp* hbRsp) {
  int64_t reqId = hbRsp->reqId;
  int64_t rspId = hbRsp->rspId;

  SArray* rsps = hbRsp->rsps;
  int32_t sz = taosArrayGetSize(rsps);
  for (int i = 0; i < sz; i++) {
    SClientHbRsp* pRsp = taosArrayGet(rsps, i);
    if (pRsp->connKey.hbType < HEARTBEAT_TYPE_MAX) {
      clientHbMgr.handle[pRsp->connKey.hbType](pRsp);
    } else {
      // discard rsp
    }
  }
  return 0;
}

L
Liu Jicong 已提交
194
int hbRegisterConn(SAppHbMgr* pAppHbMgr, SClientHbKey connKey, FGetConnInfo func) {
L
Liu Jicong 已提交
195
  // init hash in activeinfo
L
Liu Jicong 已提交
196
  void* data = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
L
Liu Jicong 已提交
197 198 199 200 201 202
  if (data != NULL) {
    return 0;
  }
  SClientHbReq hbReq;
  hbReq.connKey = connKey;
  hbReq.info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
L
Liu Jicong 已提交
203
  taosHashPut(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey), &hbReq, sizeof(SClientHbReq));
L
Liu Jicong 已提交
204 205
  // init hash
  if (func != NULL) {
L
Liu Jicong 已提交
206
    taosHashPut(pAppHbMgr->getInfoFuncs, &connKey, sizeof(SClientHbKey), func, sizeof(FGetConnInfo));
L
Liu Jicong 已提交
207 208
  }

L
Liu Jicong 已提交
209
  atomic_add_fetch_32(&pAppHbMgr->connKeyCnt, 1);
L
Liu Jicong 已提交
210 211 212
  return 0;
}

L
Liu Jicong 已提交
213 214 215 216
void hbDeregisterConn(SAppHbMgr* pAppHbMgr, SClientHbKey connKey) {
  taosHashRemove(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
  taosHashRemove(pAppHbMgr->getInfoFuncs, &connKey, sizeof(SClientHbKey));
  atomic_sub_fetch_32(&pAppHbMgr->connKeyCnt, 1);
L
Liu Jicong 已提交
217 218
}

L
Liu Jicong 已提交
219
int hbAddConnInfo(SAppHbMgr *pAppHbMgr, SClientHbKey connKey, void* key, void* value, int32_t keyLen, int32_t valueLen) {
L
Liu Jicong 已提交
220
  // find req by connection id
L
Liu Jicong 已提交
221
  SClientHbReq* pReq = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
L
Liu Jicong 已提交
222 223 224 225 226 227
  ASSERT(pReq != NULL);

  taosHashPut(pReq->info, key, keyLen, value, valueLen);

  return 0;
}