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

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

static SClientHbMgr clientHbMgr = {0};

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

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

D
dapan1121 已提交
28 29 30
static int32_t hbProcessDBInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
  int32_t code = 0;

S
Shengliang Guan 已提交
31 32 33 34 35
  SUseDbBatchRsp batchUseRsp = {0};
  if (tDeserializeSUseDbBatchRsp(value, valueLen, &batchUseRsp) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }
D
dapan1121 已提交
36

S
Shengliang Guan 已提交
37 38 39
  int32_t numOfBatchs = taosArrayGetSize(batchUseRsp.pArray);
  for (int32_t i = 0; i < numOfBatchs; ++i) {
    SUseDbRsp *rsp = taosArrayGet(batchUseRsp.pArray, i);
L
fix  
Liu Jicong 已提交
40 41
    tscDebug("hb db rsp, db:%s, vgVersion:%d, uid:%" PRIx64, rsp->db, rsp->vgVersion, rsp->uid);

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

S
Shengliang Guan 已提交
54 55 56
      for (int32_t j = 0; j < rsp->vgNum; ++j) {
        SVgroupInfo *pInfo = taosArrayGet(rsp->pVgroupInfos, j);
        if (taosHashPut(vgInfo.vgHash, &pInfo->vgId, sizeof(int32_t), pInfo, sizeof(SVgroupInfo)) != 0) {
D
dapan1121 已提交
57
          tscError("hash push failed, errno:%d", errno);
D
dapan1121 已提交
58
          taosHashCleanup(vgInfo.vgHash);
D
dapan1121 已提交
59 60
          return TSDB_CODE_TSC_OUT_OF_MEMORY;
        }
L
fix  
Liu Jicong 已提交
61 62
      }

D
dapan1121 已提交
63
      catalogUpdateDBVgInfo(pCatalog, rsp->db, rsp->uid, &vgInfo);
D
dapan1121 已提交
64 65 66 67 68 69 70
    }

    if (code) {
      return code;
    }
  }

S
Shengliang Guan 已提交
71
  tFreeSUseDbBatchRsp(&batchUseRsp);
D
dapan1121 已提交
72 73 74
  return TSDB_CODE_SUCCESS;
}

D
dapan 已提交
75 76 77
static int32_t hbProcessStbInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
  int32_t code = 0;

S
Shengliang Guan 已提交
78 79 80 81 82 83 84 85 86 87
  STableMetaBatchRsp batchMetaRsp = {0};
  if (tDeserializeSTableMetaBatchRsp(value, valueLen, &batchMetaRsp) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }

  int32_t numOfBatchs = taosArrayGetSize(batchMetaRsp.pArray);
  for (int32_t i = 0; i < numOfBatchs; ++i) {
    STableMetaRsp *rsp = taosArrayGet(batchMetaRsp.pArray, i);

D
dapan 已提交
88 89
    if (rsp->numOfColumns < 0) {
      tscDebug("hb remove stb, db:%s, stb:%s", rsp->dbFName, rsp->stbName);
D
dapan1121 已提交
90
      catalogRemoveStbMeta(pCatalog, rsp->dbFName, rsp->dbId, rsp->stbName, rsp->suid);
D
dapan 已提交
91
    } else {
D
dapan1121 已提交
92
      tscDebug("hb update stb, db:%s, stb:%s", rsp->dbFName, rsp->stbName);
S
Shengliang Guan 已提交
93 94 95
      if (rsp->pSchemas[0].colId != PRIMARYKEY_TIMESTAMP_COL_ID) {
        tscError("invalid colId[%d] for the first column in table meta rsp msg", rsp->pSchemas[0].colId);
        tFreeSTableMetaBatchRsp(&batchMetaRsp);
D
dapan1121 已提交
96
        return TSDB_CODE_TSC_INVALID_VALUE;
D
dapan 已提交
97 98
      }

D
dapan1121 已提交
99
      catalogUpdateSTableMeta(pCatalog, rsp);
D
dapan 已提交
100 101 102
    }
  }

S
Shengliang Guan 已提交
103
  tFreeSTableMetaBatchRsp(&batchMetaRsp);
D
dapan 已提交
104 105 106
  return TSDB_CODE_SUCCESS;
}

L
Liu Jicong 已提交
107
static int32_t hbQueryHbRspHandle(SAppHbMgr *pAppHbMgr, SClientHbRsp *pRsp) {
L
fix  
Liu Jicong 已提交
108
  SHbConnInfo *info = taosHashGet(pAppHbMgr->connInfo, &pRsp->connKey, sizeof(SClientHbKey));
D
dapan1121 已提交
109 110 111 112 113 114
  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 已提交
115 116

  tscDebug("hb got %d rsp kv", kvNum);
L
fix  
Liu Jicong 已提交
117

D
dapan1121 已提交
118 119 120
  for (int32_t i = 0; i < kvNum; ++i) {
    SKv *kv = taosArrayGet(pRsp->info, i);
    switch (kv->key) {
D
dapan1121 已提交
121 122 123 124 125 126
      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;
        }

L
fix  
Liu Jicong 已提交
127
        int64_t         *clusterId = (int64_t *)info->param;
D
dapan1121 已提交
128
        struct SCatalog *pCatalog = NULL;
L
fix  
Liu Jicong 已提交
129

D
dapan1121 已提交
130 131
        int32_t code = catalogGetHandle(*clusterId, &pCatalog);
        if (code != TSDB_CODE_SUCCESS) {
L
fix  
Liu Jicong 已提交
132
          tscWarn("catalogGetHandle failed, clusterId:%" PRIx64 ", error:%s", *clusterId, tstrerror(code));
D
dapan1121 已提交
133 134 135 136
          break;
        }

        hbProcessDBInfoRsp(kv->value, kv->valueLen, pCatalog);
D
dapan1121 已提交
137
        break;
D
dapan1121 已提交
138
      }
L
fix  
Liu Jicong 已提交
139
      case HEARTBEAT_KEY_STBINFO: {
D
dapan 已提交
140 141 142 143
        if (kv->valueLen <= 0 || NULL == kv->value) {
          tscError("invalid hb stb info, len:%d, value:%p", kv->valueLen, kv->value);
          break;
        }
D
dapan1121 已提交
144

L
fix  
Liu Jicong 已提交
145
        int64_t         *clusterId = (int64_t *)info->param;
D
dapan 已提交
146
        struct SCatalog *pCatalog = NULL;
L
fix  
Liu Jicong 已提交
147

D
dapan 已提交
148 149
        int32_t code = catalogGetHandle(*clusterId, &pCatalog);
        if (code != TSDB_CODE_SUCCESS) {
L
fix  
Liu Jicong 已提交
150
          tscWarn("catalogGetHandle failed, clusterId:%" PRIx64 ", error:%s", *clusterId, tstrerror(code));
D
dapan 已提交
151 152 153 154
          break;
        }

        hbProcessStbInfoRsp(kv->value, kv->valueLen, pCatalog);
D
dapan1121 已提交
155
        break;
D
dapan 已提交
156
      }
D
dapan1121 已提交
157 158 159 160 161 162 163 164 165
      default:
        tscError("invalid hb key type:%d", kv->key);
        break;
    }
  }

  return TSDB_CODE_SUCCESS;
}

L
Liu Jicong 已提交
166
static int32_t hbAsyncCallBack(void *param, const SDataBuf *pMsg, int32_t code) {
D
dapan1121 已提交
167
  static int32_t emptyRspNum = 0;
L
Liu Jicong 已提交
168
  if (code != 0) {
D
dapan1121 已提交
169
    tfree(param);
L
Liu Jicong 已提交
170 171
    return -1;
  }
S
Shengliang Guan 已提交
172

L
fix  
Liu Jicong 已提交
173
  char             *key = (char *)param;
D
dapan1121 已提交
174
  SClientHbBatchRsp pRsp = {0};
S
Shengliang Guan 已提交
175
  tDeserializeSClientHbBatchRsp(pMsg->pData, pMsg->len, &pRsp);
L
fix  
Liu Jicong 已提交
176

D
dapan1121 已提交
177
  int32_t rspNum = taosArrayGetSize(pRsp.rsps);
D
dapan1121 已提交
178

L
fix  
Liu Jicong 已提交
179
  SAppInstInfo **pInst = taosHashGet(appInfo.pInstMap, key, strlen(key));
D
dapan1121 已提交
180
  if (pInst == NULL || NULL == *pInst) {
L
fix  
Liu Jicong 已提交
181
    tscError("cluster not exist, key:%s", key);
D
dapan1121 已提交
182 183
    tfree(param);
    tFreeClientHbBatchRsp(&pRsp);
D
dapan1121 已提交
184 185 186
    return -1;
  }

D
dapan1121 已提交
187 188 189
  tfree(param);

  if (rspNum) {
L
fix  
Liu Jicong 已提交
190 191
    tscDebug("hb got %d rsp, %d empty rsp received before", rspNum,
             atomic_val_compare_exchange_32(&emptyRspNum, emptyRspNum, 0));
D
dapan1121 已提交
192 193 194 195 196
  } else {
    atomic_add_fetch_32(&emptyRspNum, 1);
  }

  for (int32_t i = 0; i < rspNum; ++i) {
L
fix  
Liu Jicong 已提交
197
    SClientHbRsp *rsp = taosArrayGet(pRsp.rsps, i);
D
dapan1121 已提交
198 199 200 201 202
    code = (*clientHbMgr.rspHandle[rsp->connKey.hbType])((*pInst)->pAppHbMgr, rsp);
    if (code) {
      break;
    }
  }
D
dapan1121 已提交
203 204

  tFreeClientHbBatchRsp(&pRsp);
L
fix  
Liu Jicong 已提交
205

D
dapan1121 已提交
206
  return code;
L
Liu Jicong 已提交
207 208
}

D
dapan1121 已提交
209 210
int32_t hbGetExpiredDBInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
  SDbVgVersion *dbs = NULL;
L
fix  
Liu Jicong 已提交
211 212
  uint32_t      dbNum = 0;
  int32_t       code = 0;
D
dapan1121 已提交
213 214 215 216 217 218

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

D
dapan1121 已提交
219 220 221 222
  if (dbNum <= 0) {
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
223 224 225 226 227 228
  for (int32_t i = 0; i < dbNum; ++i) {
    SDbVgVersion *db = &dbs[i];
    db->dbId = htobe64(db->dbId);
    db->vgVersion = htonl(db->vgVersion);
  }

L
Liu Jicong 已提交
229 230 231 232 233
  SKv kv = {
      .key = HEARTBEAT_KEY_DBINFO,
      .valueLen = sizeof(SDbVgVersion) * dbNum,
      .value = dbs,
  };
D
dapan1121 已提交
234 235 236

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

D
dapan1121 已提交
237 238 239 240 241
  taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));

  return TSDB_CODE_SUCCESS;
}

D
dapan 已提交
242 243
int32_t hbGetExpiredStbInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
  SSTableMetaVersion *stbs = NULL;
L
fix  
Liu Jicong 已提交
244 245
  uint32_t            stbNum = 0;
  int32_t             code = 0;
D
dapan 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259

  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);
L
fix  
Liu Jicong 已提交
260
    stb->tversion = htons(stb->tversion);
D
dapan 已提交
261 262
  }

L
Liu Jicong 已提交
263 264 265 266 267
  SKv kv = {
      .key = HEARTBEAT_KEY_STBINFO,
      .valueLen = sizeof(SSTableMetaVersion) * stbNum,
      .value = stbs,
  };
D
dapan 已提交
268 269 270 271 272 273 274 275

  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;
}

L
fix  
Liu Jicong 已提交
276 277
int32_t hbQueryHbReqHandle(SClientHbKey *connKey, void *param, SClientHbReq *req) {
  int64_t         *clusterId = (int64_t *)param;
D
dapan1121 已提交
278 279 280 281
  struct SCatalog *pCatalog = NULL;

  int32_t code = catalogGetHandle(*clusterId, &pCatalog);
  if (code != TSDB_CODE_SUCCESS) {
L
fix  
Liu Jicong 已提交
282
    tscWarn("catalogGetHandle failed, clusterId:%" PRIx64 ", error:%s", *clusterId, tstrerror(code));
D
dapan1121 已提交
283 284
    return code;
  }
L
fix  
Liu Jicong 已提交
285

D
dapan1121 已提交
286 287 288 289 290
  code = hbGetExpiredDBInfo(connKey, pCatalog, req);
  if (TSDB_CODE_SUCCESS != code) {
    return code;
  }

D
dapan 已提交
291 292 293 294 295
  code = hbGetExpiredStbInfo(connKey, pCatalog, req);
  if (TSDB_CODE_SUCCESS != code) {
    return code;
  }

D
dapan1121 已提交
296 297 298
  return TSDB_CODE_SUCCESS;
}

L
Liu Jicong 已提交
299
int32_t hbMqHbReqHandle(SClientHbKey *connKey, void *param, SClientHbReq *req) { return 0; }
D
dapan1121 已提交
300 301 302 303 304 305

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 已提交
306 307
}

L
Liu Jicong 已提交
308 309
static FORCE_INLINE void hbMgrInitHandle() {
  // init all handle
D
dapan1121 已提交
310
  hbMgrInitMqHbHandle();
L
Liu Jicong 已提交
311 312
}

D
dapan1121 已提交
313 314
void hbFreeReq(void *req) {
  SClientHbReq *pReq = (SClientHbReq *)req;
D
dapan1121 已提交
315
  tFreeReqKvHash(pReq->info);
D
dapan1121 已提交
316 317
}

L
fix  
Liu Jicong 已提交
318 319
SClientHbBatchReq *hbGatherAllInfo(SAppHbMgr *pAppHbMgr) {
  SClientHbBatchReq *pBatchReq = calloc(1, sizeof(SClientHbBatchReq));
L
Liu Jicong 已提交
320
  if (pBatchReq == NULL) {
L
Liu Jicong 已提交
321 322 323
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    return NULL;
  }
L
Liu Jicong 已提交
324
  int32_t connKeyCnt = atomic_load_32(&pAppHbMgr->connKeyCnt);
L
Liu Jicong 已提交
325
  pBatchReq->reqs = taosArrayInit(connKeyCnt, sizeof(SClientHbReq));
L
Liu Jicong 已提交
326

D
dapan1121 已提交
327
  int32_t code = 0;
L
fix  
Liu Jicong 已提交
328
  void   *pIter = taosHashIterate(pAppHbMgr->activeInfo, NULL);
L
Liu Jicong 已提交
329
  while (pIter != NULL) {
L
fix  
Liu Jicong 已提交
330
    SClientHbReq *pOneReq = pIter;
D
dapan1121 已提交
331

L
fix  
Liu Jicong 已提交
332
    SHbConnInfo *info = taosHashGet(pAppHbMgr->connInfo, &pOneReq->connKey, sizeof(SClientHbKey));
D
dapan1121 已提交
333 334 335 336 337 338 339 340
    if (info) {
      code = (*clientHbMgr.reqHandle[pOneReq->connKey.hbType])(&pOneReq->connKey, info->param, pOneReq);
      if (code) {
        taosHashCancelIterate(pAppHbMgr->activeInfo, pIter);
        break;
      }
    }

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

L
Liu Jicong 已提交
343
    pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter);
L
Liu Jicong 已提交
344 345
  }

D
dapan1121 已提交
346 347 348
  if (code) {
    taosArrayDestroyEx(pBatchReq->reqs, hbFreeReq);
    tfree(pBatchReq);
L
Liu Jicong 已提交
349 350
  }

L
Liu Jicong 已提交
351
  return pBatchReq;
L
Liu Jicong 已提交
352 353
}

D
dapan1121 已提交
354 355 356
void hbClearReqInfo(SAppHbMgr *pAppHbMgr) {
  void *pIter = taosHashIterate(pAppHbMgr->activeInfo, NULL);
  while (pIter != NULL) {
L
fix  
Liu Jicong 已提交
357
    SClientHbReq *pOneReq = pIter;
D
dapan1121 已提交
358

D
dapan1121 已提交
359
    tFreeReqKvHash(pOneReq->info);
D
dapan1121 已提交
360 361 362 363 364 365
    taosHashClear(pOneReq->info);

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

L
fix  
Liu Jicong 已提交
366
static void *hbThreadFunc(void *param) {
L
Liu Jicong 已提交
367 368
  setThreadName("hb");
  while (1) {
369
    int8_t threadStop = atomic_val_compare_exchange_8(&clientHbMgr.threadStop, 1, 2);
L
fix  
Liu Jicong 已提交
370
    if (1 == threadStop) {
L
Liu Jicong 已提交
371 372 373
      break;
    }

374 375
    pthread_mutex_lock(&clientHbMgr.lock);

L
Liu Jicong 已提交
376
    int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
L
fix  
Liu Jicong 已提交
377 378
    for (int i = 0; i < sz; i++) {
      SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
L
Liu Jicong 已提交
379

L
Liu Jicong 已提交
380 381 382 383
      int32_t connCnt = atomic_load_32(&pAppHbMgr->connKeyCnt);
      if (connCnt == 0) {
        continue;
      }
L
fix  
Liu Jicong 已提交
384
      SClientHbBatchReq *pReq = hbGatherAllInfo(pAppHbMgr);
L
Liu Jicong 已提交
385 386 387
      if (pReq == NULL) {
        continue;
      }
L
fix  
Liu Jicong 已提交
388
      int   tlen = tSerializeSClientHbBatchReq(NULL, 0, pReq);
L
Liu Jicong 已提交
389 390
      void *buf = malloc(tlen);
      if (buf == NULL) {
D
dapan1121 已提交
391 392 393
        terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
        tFreeClientHbBatchReq(pReq, false);
        hbClearReqInfo(pAppHbMgr);
L
Liu Jicong 已提交
394 395
        break;
      }
L
fix  
Liu Jicong 已提交
396

S
Shengliang Guan 已提交
397
      tSerializeSClientHbBatchReq(buf, tlen, pReq);
dengyihao's avatar
dengyihao 已提交
398
      SMsgSendInfo *pInfo = calloc(1, sizeof(SMsgSendInfo));
399

L
Liu Jicong 已提交
400 401
      if (pInfo == NULL) {
        terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
L
Liu Jicong 已提交
402
        tFreeClientHbBatchReq(pReq, false);
D
dapan1121 已提交
403
        hbClearReqInfo(pAppHbMgr);
L
Liu Jicong 已提交
404 405 406
        free(buf);
        break;
      }
L
Liu Jicong 已提交
407
      pInfo->fp = hbAsyncCallBack;
L
Liu Jicong 已提交
408 409 410
      pInfo->msgInfo.pData = buf;
      pInfo->msgInfo.len = tlen;
      pInfo->msgType = TDMT_MND_HEARTBEAT;
D
dapan1121 已提交
411
      pInfo->param = strdup(pAppHbMgr->key);
L
Liu Jicong 已提交
412 413
      pInfo->requestId = generateRequestId();
      pInfo->requestObjRefId = 0;
L
Liu Jicong 已提交
414 415

      SAppInstInfo *pAppInstInfo = pAppHbMgr->pAppInstInfo;
L
fix  
Liu Jicong 已提交
416 417
      int64_t       transporterId = 0;
      SEpSet        epSet = getEpSet_s(&pAppInstInfo->mgmtEp);
L
Liu Jicong 已提交
418
      asyncSendMsgToServer(pAppInstInfo->pTransporter, &epSet, &transporterId, pInfo);
L
fix  
Liu Jicong 已提交
419
      tFreeClientHbBatchReq(pReq, false);
D
dapan1121 已提交
420
      hbClearReqInfo(pAppHbMgr);
L
Liu Jicong 已提交
421 422 423

      atomic_add_fetch_32(&pAppHbMgr->reportCnt, 1);
    }
424 425

    pthread_mutex_unlock(&clientHbMgr.lock);
L
fix  
Liu Jicong 已提交
426

L
Liu Jicong 已提交
427
    taosMsleep(HEARTBEAT_INTERVAL);
L
Liu Jicong 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
  }
  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 已提交
445
static void hbStopThread() {
446
  if (atomic_val_compare_exchange_8(&clientHbMgr.threadStop, 0, 1)) {
D
dapan1121 已提交
447
    tscDebug("hb thread already stopped");
448 449
    return;
  }
L
fix  
Liu Jicong 已提交
450

451 452 453
  while (2 != atomic_load_8(&clientHbMgr.threadStop)) {
    usleep(10);
  }
D
dapan1121 已提交
454

L
fix  
Liu Jicong 已提交
455
  tscDebug("hb thread stopped");
L
Liu Jicong 已提交
456 457
}

L
fix  
Liu Jicong 已提交
458
SAppHbMgr *appHbMgrInit(SAppInstInfo *pAppInstInfo, char *key) {
L
Liu Jicong 已提交
459
  hbMgrInit();
L
fix  
Liu Jicong 已提交
460
  SAppHbMgr *pAppHbMgr = malloc(sizeof(SAppHbMgr));
L
Liu Jicong 已提交
461 462 463 464 465 466
  if (pAppHbMgr == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
  // init stat
  pAppHbMgr->startTime = taosGetTimestampMs();
L
Liu Jicong 已提交
467 468 469
  pAppHbMgr->connKeyCnt = 0;
  pAppHbMgr->reportCnt = 0;
  pAppHbMgr->reportBytes = 0;
D
dapan1121 已提交
470
  pAppHbMgr->key = strdup(key);
L
Liu Jicong 已提交
471

L
Liu Jicong 已提交
472 473
  // init app info
  pAppHbMgr->pAppInstInfo = pAppInstInfo;
L
Liu Jicong 已提交
474 475 476

  // init hash info
  pAppHbMgr->activeInfo = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
L
Liu Jicong 已提交
477 478 479 480 481 482

  if (pAppHbMgr->activeInfo == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    free(pAppHbMgr);
    return NULL;
  }
H
Haojun Liao 已提交
483 484

  taosHashSetFreeFp(pAppHbMgr->activeInfo, tFreeClientHbReq);
L
Liu Jicong 已提交
485
  // init getInfoFunc
D
dapan1121 已提交
486
  pAppHbMgr->connInfo = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
L
Liu Jicong 已提交
487

D
dapan1121 已提交
488
  if (pAppHbMgr->connInfo == NULL) {
L
Liu Jicong 已提交
489 490 491 492 493
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    free(pAppHbMgr);
    return NULL;
  }

494
  pthread_mutex_lock(&clientHbMgr.lock);
L
Liu Jicong 已提交
495
  taosArrayPush(clientHbMgr.appHbMgrs, &pAppHbMgr);
496
  pthread_mutex_unlock(&clientHbMgr.lock);
L
fix  
Liu Jicong 已提交
497

L
Liu Jicong 已提交
498 499 500
  return pAppHbMgr;
}

D
dapan1121 已提交
501
void appHbMgrCleanup(void) {
L
Liu Jicong 已提交
502 503 504 505
  pthread_mutex_lock(&clientHbMgr.lock);

  int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
  for (int i = 0; i < sz; i++) {
L
fix  
Liu Jicong 已提交
506
    SAppHbMgr *pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i);
D
dapan1121 已提交
507 508 509 510
    taosHashCleanup(pTarget->activeInfo);
    pTarget->activeInfo = NULL;
    taosHashCleanup(pTarget->connInfo);
    pTarget->connInfo = NULL;
L
Liu Jicong 已提交
511 512 513 514 515 516
  }

  pthread_mutex_unlock(&clientHbMgr.lock);
}

int hbMgrInit() {
L
Liu Jicong 已提交
517 518 519 520
  // init once
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 0, 1);
  if (old == 1) return 0;

L
fix  
Liu Jicong 已提交
521
  clientHbMgr.appHbMgrs = taosArrayInit(0, sizeof(void *));
L
Liu Jicong 已提交
522
  pthread_mutex_init(&clientHbMgr.lock, NULL);
L
Liu Jicong 已提交
523 524 525 526 527

  // init handle funcs
  hbMgrInitHandle();

  // init backgroud thread
L
Liu Jicong 已提交
528 529
  hbCreateThread();

L
Liu Jicong 已提交
530 531 532 533
  return 0;
}

void hbMgrCleanUp() {
L
Liu Jicong 已提交
534
#if 0
535
  hbStopThread();
L
fix  
Liu Jicong 已提交
536

L
Liu Jicong 已提交
537
  // destroy all appHbMgr
L
Liu Jicong 已提交
538 539 540
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 1, 0);
  if (old == 0) return;

541
  pthread_mutex_lock(&clientHbMgr.lock);
D
dapan1121 已提交
542
  appHbMgrCleanup();
L
fix  
Liu Jicong 已提交
543
  taosArrayDestroy(clientHbMgr.appHbMgrs);
544
  pthread_mutex_unlock(&clientHbMgr.lock);
L
fix  
Liu Jicong 已提交
545

546
  clientHbMgr.appHbMgrs = NULL;
L
Liu Jicong 已提交
547
#endif
L
Liu Jicong 已提交
548 549
}

L
fix  
Liu Jicong 已提交
550
int hbRegisterConnImpl(SAppHbMgr *pAppHbMgr, SClientHbKey connKey, SHbConnInfo *info) {
L
Liu Jicong 已提交
551
  // init hash in activeinfo
L
fix  
Liu Jicong 已提交
552
  void *data = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
L
Liu Jicong 已提交
553 554 555 556 557 558
  if (data != NULL) {
    return 0;
  }
  SClientHbReq hbReq;
  hbReq.connKey = connKey;
  hbReq.info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
L
fix  
Liu Jicong 已提交
559

L
Liu Jicong 已提交
560
  taosHashPut(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey), &hbReq, sizeof(SClientHbReq));
L
fix  
Liu Jicong 已提交
561

L
Liu Jicong 已提交
562
  // init hash
D
dapan1121 已提交
563
  if (info != NULL) {
L
fix  
Liu Jicong 已提交
564
    SClientHbReq *pReq = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
D
dapan1121 已提交
565 566
    info->req = pReq;
    taosHashPut(pAppHbMgr->connInfo, &connKey, sizeof(SClientHbKey), info, sizeof(SHbConnInfo));
L
Liu Jicong 已提交
567 568
  }

L
Liu Jicong 已提交
569
  atomic_add_fetch_32(&pAppHbMgr->connKeyCnt, 1);
L
Liu Jicong 已提交
570 571 572
  return 0;
}

L
fix  
Liu Jicong 已提交
573
int hbRegisterConn(SAppHbMgr *pAppHbMgr, int32_t connId, int64_t clusterId, int32_t hbType) {
L
Liu Jicong 已提交
574 575 576 577 578
  SClientHbKey connKey = {
      .connId = connId,
      .hbType = HEARTBEAT_TYPE_QUERY,
  };
  SHbConnInfo info = {0};
D
dapan1121 已提交
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593

  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;
  }
L
fix  
Liu Jicong 已提交
594

D
dapan1121 已提交
595 596 597
  return hbRegisterConnImpl(pAppHbMgr, connKey, &info);
}

L
fix  
Liu Jicong 已提交
598
void hbDeregisterConn(SAppHbMgr *pAppHbMgr, SClientHbKey connKey) {
D
dapan1121 已提交
599 600 601 602 603 604
  int32_t code = 0;
  code = taosHashRemove(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
  code = taosHashRemove(pAppHbMgr->connInfo, &connKey, sizeof(SClientHbKey));
  if (code) {
    return;
  }
L
Liu Jicong 已提交
605
  atomic_sub_fetch_32(&pAppHbMgr->connKeyCnt, 1);
L
Liu Jicong 已提交
606 607
}

L
fix  
Liu Jicong 已提交
608 609
int hbAddConnInfo(SAppHbMgr *pAppHbMgr, SClientHbKey connKey, void *key, void *value, int32_t keyLen,
                  int32_t valueLen) {
L
Liu Jicong 已提交
610
  // find req by connection id
L
fix  
Liu Jicong 已提交
611
  SClientHbReq *pReq = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
L
Liu Jicong 已提交
612 613 614 615 616 617
  ASSERT(pReq != NULL);

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

  return 0;
}