clientHb.c 18.7 KB
Newer Older
L
Liu Jicong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

L
Liu Jicong 已提交
16
#include "clientInt.h"
L
Liu Jicong 已提交
17
#include "trpc.h"
D
dapan1121 已提交
18 19
#include "catalog.h"
#include "clientLog.h"
L
Liu Jicong 已提交
20 21 22 23 24 25

static SClientHbMgr clientHbMgr = {0};

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

D
dapan1121 已提交
26
static int32_t hbMqHbRspHandle(struct SAppHbMgr *pAppHbMgr, SClientHbRsp* pRsp) {
L
Liu Jicong 已提交
27 28 29
  return 0;
}

D
dapan1121 已提交
30 31 32 33 34 35 36 37 38 39
static int32_t hbProcessDBInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
  int32_t msgLen = 0;
  int32_t code = 0;
  
  while (msgLen < valueLen) {
    SUseDbRsp *rsp = (SUseDbRsp *)((char *)value + msgLen);

    rsp->vgVersion = ntohl(rsp->vgVersion);
    rsp->vgNum = ntohl(rsp->vgNum);
    rsp->uid = be64toh(rsp->uid);
D
dapan1121 已提交
40 41

    tscDebug("hb db rsp, db:%s, vgVersion:%d, uid:%"PRIx64, rsp->db, rsp->vgVersion, rsp->uid);
D
dapan1121 已提交
42 43
    
    if (rsp->vgVersion < 0) {
D
dapan1121 已提交
44
      code = catalogRemoveDB(pCatalog, rsp->db, rsp->uid);
D
dapan1121 已提交
45 46 47 48
    } else {
      SDBVgroupInfo vgInfo = {0};
      vgInfo.vgVersion = rsp->vgVersion;
      vgInfo.hashMethod = rsp->hashMethod;
D
dapan1121 已提交
49 50
      vgInfo.vgHash = taosHashInit(rsp->vgNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
      if (NULL == vgInfo.vgHash) {
D
dapan1121 已提交
51 52 53 54 55 56 57 58 59
        tscError("hash init[%d] failed", rsp->vgNum);
        return TSDB_CODE_TSC_OUT_OF_MEMORY;
      }

      for (int32_t i = 0; i < rsp->vgNum; ++i) {
        rsp->vgroupInfo[i].vgId = ntohl(rsp->vgroupInfo[i].vgId);
        rsp->vgroupInfo[i].hashBegin = ntohl(rsp->vgroupInfo[i].hashBegin);
        rsp->vgroupInfo[i].hashEnd = ntohl(rsp->vgroupInfo[i].hashEnd);

D
dapan1121 已提交
60 61
        for (int32_t n = 0; n < rsp->vgroupInfo[i].epset.numOfEps; ++n) {
          rsp->vgroupInfo[i].epset.eps[n].port = ntohs(rsp->vgroupInfo[i].epset.eps[n].port);
D
dapan1121 已提交
62 63
        }

D
dapan1121 已提交
64
        if (0 != taosHashPut(vgInfo.vgHash, &rsp->vgroupInfo[i].vgId, sizeof(rsp->vgroupInfo[i].vgId), &rsp->vgroupInfo[i], sizeof(rsp->vgroupInfo[i]))) {
D
dapan1121 已提交
65
          tscError("hash push failed, errno:%d", errno);
D
dapan1121 已提交
66
          taosHashCleanup(vgInfo.vgHash);
D
dapan1121 已提交
67 68 69 70
          return TSDB_CODE_TSC_OUT_OF_MEMORY;
        }
      }  
      
D
dapan1121 已提交
71
      code = catalogUpdateDBVgroup(pCatalog, rsp->db, rsp->uid, &vgInfo);
D
dapan1121 已提交
72
      if (code) {
D
dapan1121 已提交
73
        taosHashCleanup(vgInfo.vgHash);
D
dapan1121 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86
      }
    }

    if (code) {
      return code;
    }

    msgLen += sizeof(SUseDbRsp) + rsp->vgNum * sizeof(SVgroupInfo);
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan 已提交
87 88 89 90 91 92 93 94 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 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 152 153 154 155
static int32_t hbProcessStbInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
  int32_t msgLen = 0;
  int32_t code = 0;
  int32_t schemaNum = 0;
  
  while (msgLen < valueLen) {
    STableMetaRsp *rsp = (STableMetaRsp *)((char *)value + msgLen);

    rsp->numOfColumns = ntohl(rsp->numOfColumns);
    rsp->suid = be64toh(rsp->suid);
    
    if (rsp->numOfColumns < 0) {
      schemaNum = 0;
      
      tscDebug("hb remove stb, db:%s, stb:%s", rsp->dbFName, rsp->stbName);

      code = catalogRemoveSTableMeta(pCatalog, rsp->dbFName, rsp->stbName, rsp->suid);
    } else {
      rsp->numOfTags = ntohl(rsp->numOfTags);
      
      schemaNum = rsp->numOfColumns + rsp->numOfTags;
/*      
      rsp->vgNum = ntohl(rsp->vgNum);
      rsp->uid = be64toh(rsp->uid);

      SDBVgroupInfo vgInfo = {0};
      vgInfo.dbId = rsp->uid;
      vgInfo.vgVersion = rsp->vgVersion;
      vgInfo.hashMethod = rsp->hashMethod;
      vgInfo.vgHash = taosHashInit(rsp->vgNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
      if (NULL == vgInfo.vgHash) {
        tscError("hash init[%d] failed", rsp->vgNum);
        return TSDB_CODE_TSC_OUT_OF_MEMORY;
      }

      for (int32_t i = 0; i < rsp->vgNum; ++i) {
        rsp->vgroupInfo[i].vgId = ntohl(rsp->vgroupInfo[i].vgId);
        rsp->vgroupInfo[i].hashBegin = ntohl(rsp->vgroupInfo[i].hashBegin);
        rsp->vgroupInfo[i].hashEnd = ntohl(rsp->vgroupInfo[i].hashEnd);

        for (int32_t n = 0; n < rsp->vgroupInfo[i].epset.numOfEps; ++n) {
          rsp->vgroupInfo[i].epset.eps[n].port = ntohs(rsp->vgroupInfo[i].epset.eps[n].port);
        }

        if (0 != taosHashPut(vgInfo.vgHash, &rsp->vgroupInfo[i].vgId, sizeof(rsp->vgroupInfo[i].vgId), &rsp->vgroupInfo[i], sizeof(rsp->vgroupInfo[i]))) {
          tscError("hash push failed, errno:%d", errno);
          taosHashCleanup(vgInfo.vgHash);
          return TSDB_CODE_TSC_OUT_OF_MEMORY;
        }
      }  
      
      code = catalogUpdateDBVgroup(pCatalog, rsp->db, &vgInfo);
      if (code) {
        taosHashCleanup(vgInfo.vgHash);
      }
*/      
    }

    if (code) {
      return code;
    }

    msgLen += sizeof(STableMetaRsp) + schemaNum * sizeof(SSchema);
  }

  return TSDB_CODE_SUCCESS;
}


D
dapan1121 已提交
156 157 158 159 160 161 162 163
static int32_t hbQueryHbRspHandle(struct SAppHbMgr *pAppHbMgr, SClientHbRsp* pRsp) {
  SHbConnInfo * info = taosHashGet(pAppHbMgr->connInfo, &pRsp->connKey, sizeof(SClientHbKey));
  if (NULL == info) {
    tscWarn("fail to get connInfo, may be dropped, connId:%d, type:%d", pRsp->connKey.connId, pRsp->connKey.hbType);
    return TSDB_CODE_SUCCESS;
  }

  int32_t kvNum = pRsp->info ? taosArrayGetSize(pRsp->info) : 0;
D
dapan1121 已提交
164 165 166

  tscDebug("hb got %d rsp kv", kvNum);
  
D
dapan1121 已提交
167 168 169
  for (int32_t i = 0; i < kvNum; ++i) {
    SKv *kv = taosArrayGet(pRsp->info, i);
    switch (kv->key) {
D
dapan1121 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
      case HEARTBEAT_KEY_DBINFO: {
        if (kv->valueLen <= 0 || NULL == kv->value) {
          tscError("invalid hb db info, len:%d, value:%p", kv->valueLen, kv->value);
          break;
        }

        int64_t *clusterId = (int64_t *)info->param;
        struct SCatalog *pCatalog = NULL;
        
        int32_t code = catalogGetHandle(*clusterId, &pCatalog);
        if (code != TSDB_CODE_SUCCESS) {
          tscWarn("catalogGetHandle failed, clusterId:%"PRIx64", error:%s", *clusterId, tstrerror(code));
          break;
        }

        hbProcessDBInfoRsp(kv->value, kv->valueLen, pCatalog);
D
dapan1121 已提交
186
        break;
D
dapan1121 已提交
187
      }
D
dapan 已提交
188 189 190 191 192
      case HEARTBEAT_KEY_STBINFO:{
        if (kv->valueLen <= 0 || NULL == kv->value) {
          tscError("invalid hb stb info, len:%d, value:%p", kv->valueLen, kv->value);
          break;
        }
D
dapan1121 已提交
193

D
dapan 已提交
194 195 196 197 198 199 200 201 202 203
        int64_t *clusterId = (int64_t *)info->param;
        struct SCatalog *pCatalog = NULL;
        
        int32_t code = catalogGetHandle(*clusterId, &pCatalog);
        if (code != TSDB_CODE_SUCCESS) {
          tscWarn("catalogGetHandle failed, clusterId:%"PRIx64", error:%s", *clusterId, tstrerror(code));
          break;
        }

        hbProcessStbInfoRsp(kv->value, kv->valueLen, pCatalog);
D
dapan1121 已提交
204
        break;
D
dapan 已提交
205
      }
D
dapan1121 已提交
206 207 208 209 210 211 212 213 214
      default:
        tscError("invalid hb key type:%d", kv->key);
        break;
    }
  }

  return TSDB_CODE_SUCCESS;
}

L
Liu Jicong 已提交
215
static int32_t hbMqAsyncCallBack(void* param, const SDataBuf* pMsg, int32_t code) {
D
dapan1121 已提交
216
  static int32_t emptyRspNum = 0;
L
Liu Jicong 已提交
217
  if (code != 0) {
D
dapan1121 已提交
218
    tfree(param);
L
Liu Jicong 已提交
219 220
    return -1;
  }
D
dapan1121 已提交
221
  char *key = (char *)param;
D
dapan1121 已提交
222 223 224
  SClientHbBatchRsp pRsp = {0};
  tDeserializeSClientHbBatchRsp(pMsg->pData, &pRsp);
  
D
dapan1121 已提交
225
  int32_t rspNum = taosArrayGetSize(pRsp.rsps);
D
dapan1121 已提交
226 227 228

  SAppInstInfo** pInst = taosHashGet(appInfo.pInstMap, key, strlen(key));
  if (pInst == NULL || NULL == *pInst) {
D
dapan1121 已提交
229 230 231
    tscError("cluster not exist, key:%s", key);    
    tfree(param);
    tFreeClientHbBatchRsp(&pRsp);
D
dapan1121 已提交
232 233 234
    return -1;
  }

D
dapan1121 已提交
235 236 237
  tfree(param);

  if (rspNum) {
D
dapan 已提交
238
    tscDebug("hb got %d rsp, %d empty rsp received before", rspNum, atomic_val_compare_exchange_32(&emptyRspNum, emptyRspNum, 0));
D
dapan1121 已提交
239 240 241 242 243
  } else {
    atomic_add_fetch_32(&emptyRspNum, 1);
  }

  for (int32_t i = 0; i < rspNum; ++i) {
D
dapan1121 已提交
244
    SClientHbRsp* rsp = taosArrayGet(pRsp.rsps, i);
D
dapan1121 已提交
245 246 247 248 249
    code = (*clientHbMgr.rspHandle[rsp->connKey.hbType])((*pInst)->pAppHbMgr, rsp);
    if (code) {
      break;
    }
  }
D
dapan1121 已提交
250 251

  tFreeClientHbBatchRsp(&pRsp);
D
dapan1121 已提交
252 253
  
  return code;
L
Liu Jicong 已提交
254 255
}

D
dapan1121 已提交
256 257 258 259 260 261 262 263 264 265
int32_t hbGetExpiredDBInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
  SDbVgVersion *dbs = NULL;
  uint32_t dbNum = 0;
  int32_t code = 0;

  code = catalogGetExpiredDBs(pCatalog, &dbs, &dbNum);
  if (TSDB_CODE_SUCCESS != code) {
    return code;
  }

D
dapan1121 已提交
266 267 268 269
  if (dbNum <= 0) {
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
270 271 272 273 274 275 276
  for (int32_t i = 0; i < dbNum; ++i) {
    SDbVgVersion *db = &dbs[i];
    db->dbId = htobe64(db->dbId);
    db->vgVersion = htonl(db->vgVersion);
  }

  SKv kv = {.key = HEARTBEAT_KEY_DBINFO, .valueLen = sizeof(SDbVgVersion) * dbNum, .value = dbs};
D
dapan1121 已提交
277 278 279

  tscDebug("hb got %d expired db, valueLen:%d", dbNum, kv.valueLen);

D
dapan1121 已提交
280 281 282 283 284
  taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));

  return TSDB_CODE_SUCCESS;
}

D
dapan 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
int32_t hbGetExpiredStbInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
  SSTableMetaVersion *stbs = NULL;
  uint32_t stbNum = 0;
  int32_t code = 0;

  code = catalogGetExpiredSTables(pCatalog, &stbs, &stbNum);
  if (TSDB_CODE_SUCCESS != code) {
    return code;
  }

  if (stbNum <= 0) {
    return TSDB_CODE_SUCCESS;
  }

  for (int32_t i = 0; i < stbNum; ++i) {
    SSTableMetaVersion *stb = &stbs[i];
    stb->suid = htobe64(stb->suid);
    stb->sversion = htons(stb->sversion);
    stb->tversion = htons(stb->tversion);    
  }

  SKv kv = {.key = HEARTBEAT_KEY_STBINFO, .valueLen = sizeof(SSTableMetaVersion) * stbNum, .value = stbs};

  tscDebug("hb got %d expired stb, valueLen:%d", stbNum, kv.valueLen);

  taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));

  return TSDB_CODE_SUCCESS;
}


D
dapan1121 已提交
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
int32_t hbQueryHbReqHandle(SClientHbKey *connKey, void* param, SClientHbReq *req) {
  int64_t *clusterId = (int64_t *)param;
  struct SCatalog *pCatalog = NULL;

  int32_t code = catalogGetHandle(*clusterId, &pCatalog);
  if (code != TSDB_CODE_SUCCESS) {
    tscWarn("catalogGetHandle failed, clusterId:%"PRIx64", error:%s", *clusterId, tstrerror(code));
    return code;
  }
  
  code = hbGetExpiredDBInfo(connKey, pCatalog, req);
  if (TSDB_CODE_SUCCESS != code) {
    return code;
  }

D
dapan 已提交
331 332 333 334 335
  code = hbGetExpiredStbInfo(connKey, pCatalog, req);
  if (TSDB_CODE_SUCCESS != code) {
    return code;
  }

D
dapan1121 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348

  return TSDB_CODE_SUCCESS;
}

int32_t hbMqHbReqHandle(SClientHbKey *connKey, void* param, SClientHbReq *req) {

}

void hbMgrInitMqHbHandle() {
  clientHbMgr.reqHandle[HEARTBEAT_TYPE_QUERY] = hbQueryHbReqHandle;
  clientHbMgr.reqHandle[HEARTBEAT_TYPE_MQ] = hbMqHbReqHandle;
  clientHbMgr.rspHandle[HEARTBEAT_TYPE_QUERY] = hbQueryHbRspHandle;
  clientHbMgr.rspHandle[HEARTBEAT_TYPE_MQ] = hbMqHbRspHandle;
L
Liu Jicong 已提交
349 350
}

L
Liu Jicong 已提交
351 352
static FORCE_INLINE void hbMgrInitHandle() {
  // init all handle
D
dapan1121 已提交
353
  hbMgrInitMqHbHandle();
L
Liu Jicong 已提交
354 355
}

D
dapan1121 已提交
356 357
void hbFreeReq(void *req) {
  SClientHbReq *pReq = (SClientHbReq *)req;
D
dapan1121 已提交
358
  tFreeReqKvHash(pReq->info);
D
dapan1121 已提交
359 360 361 362
}



L
Liu Jicong 已提交
363
SClientHbBatchReq* hbGatherAllInfo(SAppHbMgr *pAppHbMgr) {
L
Liu Jicong 已提交
364 365
  SClientHbBatchReq* pBatchReq = malloc(sizeof(SClientHbBatchReq));
  if (pBatchReq == NULL) {
L
Liu Jicong 已提交
366 367 368
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    return NULL;
  }
L
Liu Jicong 已提交
369
  int32_t connKeyCnt = atomic_load_32(&pAppHbMgr->connKeyCnt);
L
Liu Jicong 已提交
370
  pBatchReq->reqs = taosArrayInit(connKeyCnt, sizeof(SClientHbReq));
L
Liu Jicong 已提交
371

D
dapan1121 已提交
372
  int32_t code = 0;
L
Liu Jicong 已提交
373
  void *pIter = taosHashIterate(pAppHbMgr->activeInfo, NULL);
L
Liu Jicong 已提交
374 375
  while (pIter != NULL) {
    SClientHbReq* pOneReq = pIter;
D
dapan1121 已提交
376 377 378 379 380 381 382 383 384 385

    SHbConnInfo * info = taosHashGet(pAppHbMgr->connInfo, &pOneReq->connKey, sizeof(SClientHbKey));
    if (info) {
      code = (*clientHbMgr.reqHandle[pOneReq->connKey.hbType])(&pOneReq->connKey, info->param, pOneReq);
      if (code) {
        taosHashCancelIterate(pAppHbMgr->activeInfo, pIter);
        break;
      }
    }

L
Liu Jicong 已提交
386
    taosArrayPush(pBatchReq->reqs, pOneReq);
L
Liu Jicong 已提交
387

L
Liu Jicong 已提交
388
    pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter);
L
Liu Jicong 已提交
389 390
  }

D
dapan1121 已提交
391 392 393
  if (code) {
    taosArrayDestroyEx(pBatchReq->reqs, hbFreeReq);
    tfree(pBatchReq);
L
Liu Jicong 已提交
394 395
  }

L
Liu Jicong 已提交
396
  return pBatchReq;
L
Liu Jicong 已提交
397 398
}

D
dapan1121 已提交
399 400 401 402 403 404

void hbClearReqInfo(SAppHbMgr *pAppHbMgr) {
  void *pIter = taosHashIterate(pAppHbMgr->activeInfo, NULL);
  while (pIter != NULL) {
    SClientHbReq* pOneReq = pIter;

D
dapan1121 已提交
405
    tFreeReqKvHash(pOneReq->info);
D
dapan1121 已提交
406 407 408 409 410 411 412 413
    taosHashClear(pOneReq->info);

    pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter);
  }
}



L
Liu Jicong 已提交
414 415 416
static void* hbThreadFunc(void* param) {
  setThreadName("hb");
  while (1) {
417 418
    int8_t threadStop = atomic_val_compare_exchange_8(&clientHbMgr.threadStop, 1, 2);
    if(1 == threadStop) {
L
Liu Jicong 已提交
419 420 421
      break;
    }

422 423
    pthread_mutex_lock(&clientHbMgr.lock);

L
Liu Jicong 已提交
424 425
    int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
    for(int i = 0; i < sz; i++) {
L
Liu Jicong 已提交
426 427
      SAppHbMgr* pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);

L
Liu Jicong 已提交
428 429 430 431
      int32_t connCnt = atomic_load_32(&pAppHbMgr->connKeyCnt);
      if (connCnt == 0) {
        continue;
      }
L
Liu Jicong 已提交
432
      SClientHbBatchReq* pReq = hbGatherAllInfo(pAppHbMgr);
L
Liu Jicong 已提交
433 434 435
      if (pReq == NULL) {
        continue;
      }
L
Liu Jicong 已提交
436 437 438
      int tlen = tSerializeSClientHbBatchReq(NULL, pReq);
      void *buf = malloc(tlen);
      if (buf == NULL) {
D
dapan1121 已提交
439 440 441
        terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
        tFreeClientHbBatchReq(pReq, false);
        hbClearReqInfo(pAppHbMgr);
L
Liu Jicong 已提交
442 443
        break;
      }
L
Liu Jicong 已提交
444 445
      void *abuf = buf;
      tSerializeSClientHbBatchReq(&abuf, pReq);
L
Liu Jicong 已提交
446 447 448
      SMsgSendInfo *pInfo = malloc(sizeof(SMsgSendInfo));
      if (pInfo == NULL) {
        terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
L
Liu Jicong 已提交
449
        tFreeClientHbBatchReq(pReq, false);
D
dapan1121 已提交
450
        hbClearReqInfo(pAppHbMgr);
L
Liu Jicong 已提交
451 452 453 454 455 456 457
        free(buf);
        break;
      }
      pInfo->fp = hbMqAsyncCallBack;
      pInfo->msgInfo.pData = buf;
      pInfo->msgInfo.len = tlen;
      pInfo->msgType = TDMT_MND_HEARTBEAT;
D
dapan1121 已提交
458
      pInfo->param = strdup(pAppHbMgr->key);
L
Liu Jicong 已提交
459 460
      pInfo->requestId = generateRequestId();
      pInfo->requestObjRefId = 0;
L
Liu Jicong 已提交
461 462

      SAppInstInfo *pAppInstInfo = pAppHbMgr->pAppInstInfo;
L
Liu Jicong 已提交
463
      int64_t transporterId = 0;
L
Liu Jicong 已提交
464
      SEpSet epSet = getEpSet_s(&pAppInstInfo->mgmtEp);
L
Liu Jicong 已提交
465
      asyncSendMsgToServer(pAppInstInfo->pTransporter, &epSet, &transporterId, pInfo);
D
dapan1121 已提交
466 467
      tFreeClientHbBatchReq(pReq, false);      
      hbClearReqInfo(pAppHbMgr);
L
Liu Jicong 已提交
468 469 470

      atomic_add_fetch_32(&pAppHbMgr->reportCnt, 1);
    }
471 472 473

    pthread_mutex_unlock(&clientHbMgr.lock);
    
L
Liu Jicong 已提交
474
    taosMsleep(HEARTBEAT_INTERVAL);
L
Liu Jicong 已提交
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
  }
  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 已提交
492
static void hbStopThread() {
493
  if (atomic_val_compare_exchange_8(&clientHbMgr.threadStop, 0, 1)) {
D
dapan1121 已提交
494
    tscDebug("hb thread already stopped");
495 496 497 498 499 500
    return;
  }
  
  while (2 != atomic_load_8(&clientHbMgr.threadStop)) {
    usleep(10);
  }
D
dapan1121 已提交
501 502

  tscDebug("hb thread stopped");  
L
Liu Jicong 已提交
503 504
}

D
dapan1121 已提交
505
SAppHbMgr* appHbMgrInit(SAppInstInfo* pAppInstInfo, char *key) {
L
Liu Jicong 已提交
506
  hbMgrInit();
L
Liu Jicong 已提交
507 508 509 510 511 512 513
  SAppHbMgr* pAppHbMgr = malloc(sizeof(SAppHbMgr)); 
  if (pAppHbMgr == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
  // init stat
  pAppHbMgr->startTime = taosGetTimestampMs();
L
Liu Jicong 已提交
514 515 516
  pAppHbMgr->connKeyCnt = 0;
  pAppHbMgr->reportCnt = 0;
  pAppHbMgr->reportBytes = 0;
D
dapan1121 已提交
517
  pAppHbMgr->key = strdup(key);
L
Liu Jicong 已提交
518

L
Liu Jicong 已提交
519 520
  // init app info
  pAppHbMgr->pAppInstInfo = pAppInstInfo;
L
Liu Jicong 已提交
521 522 523

  // init hash info
  pAppHbMgr->activeInfo = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
L
Liu Jicong 已提交
524 525 526 527 528 529

  if (pAppHbMgr->activeInfo == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    free(pAppHbMgr);
    return NULL;
  }
L
Liu Jicong 已提交
530 531
  pAppHbMgr->activeInfo->freeFp = tFreeClientHbReq;
  // init getInfoFunc
D
dapan1121 已提交
532
  pAppHbMgr->connInfo = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
L
Liu Jicong 已提交
533

D
dapan1121 已提交
534
  if (pAppHbMgr->connInfo == NULL) {
L
Liu Jicong 已提交
535 536 537 538 539
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    free(pAppHbMgr);
    return NULL;
  }

540
  pthread_mutex_lock(&clientHbMgr.lock);
L
Liu Jicong 已提交
541
  taosArrayPush(clientHbMgr.appHbMgrs, &pAppHbMgr);
542 543
  pthread_mutex_unlock(&clientHbMgr.lock);
  
L
Liu Jicong 已提交
544 545 546
  return pAppHbMgr;
}

D
dapan1121 已提交
547
void appHbMgrCleanup(void) {
L
Liu Jicong 已提交
548 549 550 551
  pthread_mutex_lock(&clientHbMgr.lock);

  int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
  for (int i = 0; i < sz; i++) {
L
Liu Jicong 已提交
552
    SAppHbMgr* pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i);
D
dapan1121 已提交
553 554 555 556
    taosHashCleanup(pTarget->activeInfo);
    pTarget->activeInfo = NULL;
    taosHashCleanup(pTarget->connInfo);
    pTarget->connInfo = NULL;
L
Liu Jicong 已提交
557 558 559 560 561 562
  }

  pthread_mutex_unlock(&clientHbMgr.lock);
}

int hbMgrInit() {
L
Liu Jicong 已提交
563 564 565 566
  // init once
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 0, 1);
  if (old == 1) return 0;

L
Liu Jicong 已提交
567 568
  clientHbMgr.appHbMgrs = taosArrayInit(0, sizeof(void*));
  pthread_mutex_init(&clientHbMgr.lock, NULL);
L
Liu Jicong 已提交
569 570 571 572 573

  // init handle funcs
  hbMgrInitHandle();

  // init backgroud thread
L
Liu Jicong 已提交
574 575
  hbCreateThread();

L
Liu Jicong 已提交
576 577 578 579
  return 0;
}

void hbMgrCleanUp() {
L
Liu Jicong 已提交
580
  return;
581 582
  hbStopThread();
  
L
Liu Jicong 已提交
583
  // destroy all appHbMgr
L
Liu Jicong 已提交
584 585 586
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 1, 0);
  if (old == 0) return;

587
  pthread_mutex_lock(&clientHbMgr.lock);
D
dapan1121 已提交
588
  appHbMgrCleanup();
589 590 591 592
  taosArrayDestroy(clientHbMgr.appHbMgrs);  
  pthread_mutex_unlock(&clientHbMgr.lock);
  
  clientHbMgr.appHbMgrs = NULL;
L
Liu Jicong 已提交
593 594
}

D
dapan1121 已提交
595
int hbRegisterConnImpl(SAppHbMgr* pAppHbMgr, SClientHbKey connKey, SHbConnInfo *info) {
L
Liu Jicong 已提交
596
  // init hash in activeinfo
L
Liu Jicong 已提交
597
  void* data = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
L
Liu Jicong 已提交
598 599 600 601 602 603
  if (data != NULL) {
    return 0;
  }
  SClientHbReq hbReq;
  hbReq.connKey = connKey;
  hbReq.info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
D
dapan1121 已提交
604
  
L
Liu Jicong 已提交
605
  taosHashPut(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey), &hbReq, sizeof(SClientHbReq));
D
dapan1121 已提交
606
 
L
Liu Jicong 已提交
607
  // init hash
D
dapan1121 已提交
608 609 610 611
  if (info != NULL) {
    SClientHbReq * pReq = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
    info->req = pReq;
    taosHashPut(pAppHbMgr->connInfo, &connKey, sizeof(SClientHbKey), info, sizeof(SHbConnInfo));
L
Liu Jicong 已提交
612 613
  }

L
Liu Jicong 已提交
614
  atomic_add_fetch_32(&pAppHbMgr->connKeyCnt, 1);
L
Liu Jicong 已提交
615 616 617
  return 0;
}

D
dapan1121 已提交
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
int hbRegisterConn(SAppHbMgr* pAppHbMgr, int32_t connId, int64_t clusterId, int32_t hbType) {
  SClientHbKey connKey = {.connId = connId, .hbType = HEARTBEAT_TYPE_QUERY};
  SHbConnInfo info = {0};

  switch (hbType) {
    case HEARTBEAT_TYPE_QUERY: {
      int64_t *pClusterId = malloc(sizeof(int64_t));
      *pClusterId = clusterId;

      info.param = pClusterId;
      break;
    }
    case HEARTBEAT_TYPE_MQ: {
      break;
    }
    default:
      break;
  }
  
  return hbRegisterConnImpl(pAppHbMgr, connKey, &info);
}

L
Liu Jicong 已提交
640
void hbDeregisterConn(SAppHbMgr* pAppHbMgr, SClientHbKey connKey) {
D
dapan1121 已提交
641 642 643 644 645 646
  int32_t code = 0;
  code = taosHashRemove(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
  code = taosHashRemove(pAppHbMgr->connInfo, &connKey, sizeof(SClientHbKey));
  if (code) {
    return;
  }
L
Liu Jicong 已提交
647
  atomic_sub_fetch_32(&pAppHbMgr->connKeyCnt, 1);
L
Liu Jicong 已提交
648 649
}

L
Liu Jicong 已提交
650
int hbAddConnInfo(SAppHbMgr *pAppHbMgr, SClientHbKey connKey, void* key, void* value, int32_t keyLen, int32_t valueLen) {
L
Liu Jicong 已提交
651
  // find req by connection id
L
Liu Jicong 已提交
652
  SClientHbReq* pReq = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
L
Liu Jicong 已提交
653 654 655 656 657 658
  ASSERT(pReq != NULL);

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

  return 0;
}