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

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

K
kailixu 已提交
22 23
typedef struct {
  union {
K
kailixu 已提交
24
    struct {
K
kailixu 已提交
25 26 27 28
      SAppHbMgr *pAppHbMgr;
      int64_t    clusterId;
      int32_t    reqCnt;
      int8_t     connHbFlag;
K
kailixu 已提交
29
    };
K
kailixu 已提交
30 31 32
  };
} SHbParam;

K
kailixu 已提交
33 34 35 36
static SClientHbMgr clientHbMgr = {0};

static int32_t hbCreateThread();
static void    hbStopThread();
K
kailixu 已提交
37
static int32_t hbUpdateUserAuthInfo(SAppHbMgr *pAppHbMgr, SUserAuthBatchRsp *batchRsp);
K
kailixu 已提交
38

K
kailixu 已提交
39
static int32_t hbMqHbReqHandle(SClientHbKey *connKey, void *param, SClientHbReq *req) { return 0; }
L
Liu Jicong 已提交
40

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

K
kailixu 已提交
43 44
static int32_t hbProcessUserAuthInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog,
                                        SAppHbMgr *pAppHbMgr) {
D
dapan 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
  int32_t code = 0;

  SUserAuthBatchRsp batchRsp = {0};
  if (tDeserializeSUserAuthBatchRsp(value, valueLen, &batchRsp) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }

  int32_t numOfBatchs = taosArrayGetSize(batchRsp.pArray);
  for (int32_t i = 0; i < numOfBatchs; ++i) {
    SGetUserAuthRsp *rsp = taosArrayGet(batchRsp.pArray, i);
    tscDebug("hb user auth rsp, user:%s, version:%d", rsp->user, rsp->version);

    catalogUpdateUserAuthInfo(pCatalog, rsp);
  }

K
kailixu 已提交
61 62 63 64
  if (numOfBatchs > 0) hbUpdateUserAuthInfo(pAppHbMgr, &batchRsp);

  atomic_val_compare_exchange_8(&pAppHbMgr->connHbFlag, 1, 2);

D
dapan1121 已提交
65
  taosArrayDestroy(batchRsp.pArray);
D
dapan 已提交
66 67 68
  return TSDB_CODE_SUCCESS;
}

K
kailixu 已提交
69
static int32_t hbUpdateUserAuthInfo(SAppHbMgr *pAppHbMgr, SUserAuthBatchRsp *batchRsp) {
K
kailixu 已提交
70 71 72 73
  uint64_t clusterId = pAppHbMgr->pAppInstInfo->clusterId;
  for (int i = 0; i < TARRAY_SIZE(clientHbMgr.appHbMgrs); ++i) {
    SAppHbMgr *hbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
    if (!hbMgr || hbMgr->pAppInstInfo->clusterId != clusterId) {
K
kailixu 已提交
74 75 76
      continue;
    }

K
kailixu 已提交
77 78
    SClientHbReq    *pReq = NULL;
    SGetUserAuthRsp *pRsp = NULL;
K
kailixu 已提交
79 80 81 82 83 84
    while ((pReq = taosHashIterate(hbMgr->activeInfo, pReq))) {
      STscObj *pTscObj = (STscObj *)acquireTscObj(pReq->connKey.tscRid);
      if (!pTscObj) {
        continue;
      }

K
kailixu 已提交
85 86 87 88 89 90
      if (!pRsp) {
        for (int32_t j = 0; j < TARRAY_SIZE(batchRsp->pArray); ++j) {
          SGetUserAuthRsp *rsp = TARRAY_GET_ELEM(batchRsp->pArray, j);
          if (0 == strncmp(rsp->user, pTscObj->user, TSDB_USER_LEN)) {
            pRsp = rsp;
            break;
K
kailixu 已提交
91
          }
K
kailixu 已提交
92 93 94 95
        }
      }
      if (pRsp) {
        pTscObj->authVer = pRsp->version;
K
kailixu 已提交
96

K
kailixu 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109
        if (pTscObj->sysInfo != pRsp->sysInfo) {
          tscDebug("update sysInfo of user %s from %" PRIi8 " to %" PRIi8 ", tscRid:%" PRIi64, pRsp->user,
                   pTscObj->sysInfo, pRsp->sysInfo, pTscObj->id);
          pTscObj->sysInfo = pRsp->sysInfo;
        }

        if (pTscObj->passInfo.fp) {
          SPassInfo *passInfo = &pTscObj->passInfo;
          int32_t    oldVer = atomic_load_32(&passInfo->ver);
          if (oldVer < pRsp->passVer) {
            atomic_store_32(&passInfo->ver, pRsp->passVer);
            if (passInfo->fp) {
              (*passInfo->fp)(passInfo->param, &pRsp->passVer, TAOS_NOTIFY_PASSVER);
K
kailixu 已提交
110
            }
K
kailixu 已提交
111 112
            tscDebug("update passVer of user %s from %d to %d, tscRid:%" PRIi64, pRsp->user, oldVer,
                     atomic_load_32(&passInfo->ver), pTscObj->id);
K
kailixu 已提交
113
          }
K
kailixu 已提交
114
        }
K
kailixu 已提交
115 116 117
      } else {
        releaseTscObj(pReq->connKey.tscRid);
        break;
K
kailixu 已提交
118
      }
K
kailixu 已提交
119

K
kailixu 已提交
120
      releaseTscObj(pReq->connKey.tscRid);
K
kailixu 已提交
121 122
    }
  }
K
kailixu 已提交
123
  return 0;
K
kailixu 已提交
124 125
}

D
dapan1121 已提交
126
static int32_t hbGenerateVgInfoFromRsp(SDBVgInfo **pInfo, SUseDbRsp *rsp) {
127
  int32_t    code = 0;
D
dapan1121 已提交
128 129 130 131 132
  SDBVgInfo *vgInfo = taosMemoryCalloc(1, sizeof(SDBVgInfo));
  if (NULL == vgInfo) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    return code;
  }
133

D
dapan1121 已提交
134 135 136 137 138 139 140 141 142 143 144 145
  vgInfo->vgVersion = rsp->vgVersion;
  vgInfo->stateTs = rsp->stateTs;
  vgInfo->hashMethod = rsp->hashMethod;
  vgInfo->hashPrefix = rsp->hashPrefix;
  vgInfo->hashSuffix = rsp->hashSuffix;
  vgInfo->vgHash = taosHashInit(rsp->vgNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
  if (NULL == vgInfo->vgHash) {
    taosMemoryFree(vgInfo);
    tscError("hash init[%d] failed", rsp->vgNum);
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _return;
  }
146

D
dapan1121 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
  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) {
      tscError("hash push failed, errno:%d", errno);
      taosHashCleanup(vgInfo->vgHash);
      taosMemoryFree(vgInfo);
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _return;
    }
  }

_return:
  if (code) {
    taosHashCleanup(vgInfo->vgHash);
    taosMemoryFreeClear(vgInfo);
  }

  *pInfo = vgInfo;
  return code;
}

D
dapan1121 已提交
168 169 170
static int32_t hbProcessDBInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
  int32_t code = 0;

D
dapan1121 已提交
171 172
  SDbHbBatchRsp batchRsp = {0};
  if (tDeserializeSDbHbBatchRsp(value, valueLen, &batchRsp) != 0) {
S
Shengliang Guan 已提交
173
    terrno = TSDB_CODE_INVALID_MSG;
174 175
    code = terrno;
    goto _return;
S
Shengliang Guan 已提交
176
  }
D
dapan1121 已提交
177

D
dapan1121 已提交
178
  int32_t numOfBatchs = taosArrayGetSize(batchRsp.pArray);
S
Shengliang Guan 已提交
179
  for (int32_t i = 0; i < numOfBatchs; ++i) {
D
dapan1121 已提交
180 181
    SDbHbRsp *rsp = taosArrayGet(batchRsp.pArray, i);
    if (rsp->useDbRsp) {
H
Haojun Liao 已提交
182
      tscDebug("hb use db rsp, db:%s, vgVersion:%d, stateTs:%" PRId64 ", uid:%" PRIx64,
D
dapan1121 已提交
183
        rsp->useDbRsp->db, rsp->useDbRsp->vgVersion, rsp->useDbRsp->stateTs, rsp->useDbRsp->uid);
H
Haojun Liao 已提交
184

D
dapan1121 已提交
185 186 187 188 189
      if (rsp->useDbRsp->vgVersion < 0) {
        code = catalogRemoveDB(pCatalog, rsp->useDbRsp->db, rsp->useDbRsp->uid);
      } else {
        SDBVgInfo *vgInfo = NULL;
        code = hbGenerateVgInfoFromRsp(&vgInfo, rsp->useDbRsp);
D
dapan1121 已提交
190
        if (TSDB_CODE_SUCCESS != code) {
D
dapan1121 已提交
191
          goto _return;
D
dapan1121 已提交
192
        }
L
fix  
Liu Jicong 已提交
193

D
dapan1121 已提交
194
        catalogUpdateDBVgInfo(pCatalog, rsp->useDbRsp->db, rsp->useDbRsp->uid, vgInfo);
H
Haojun Liao 已提交
195

D
dapan1121 已提交
196 197 198 199 200
        if (IS_SYS_DBNAME(rsp->useDbRsp->db)) {
          code = hbGenerateVgInfoFromRsp(&vgInfo, rsp->useDbRsp);
          if (TSDB_CODE_SUCCESS != code) {
            goto _return;
          }
H
Haojun Liao 已提交
201

D
dapan1121 已提交
202 203
          catalogUpdateDBVgInfo(pCatalog, (rsp->useDbRsp->db[0] == 'i') ? TSDB_PERFORMANCE_SCHEMA_DB : TSDB_INFORMATION_SCHEMA_DB, rsp->useDbRsp->uid, vgInfo);
        }
D
dapan1121 已提交
204
      }
D
dapan1121 已提交
205 206
    }

D
dapan1121 已提交
207 208 209 210
    if (rsp->cfgRsp) {
      tscDebug("hb db cfg rsp, db:%s, cfgVersion:%d", rsp->cfgRsp->db, rsp->cfgRsp->cfgVersion);
      catalogUpdateDbCfg(pCatalog, rsp->cfgRsp->db, rsp->cfgRsp->dbId, rsp->cfgRsp);
      rsp->cfgRsp = NULL;
D
dapan1121 已提交
211 212 213
    }
  }

D
dapan1121 已提交
214 215
_return:

D
dapan1121 已提交
216
  tFreeSDbHbBatchRsp(&batchRsp);
D
dapan1121 已提交
217
  return code;
D
dapan1121 已提交
218 219
}

D
dapan 已提交
220 221 222
static int32_t hbProcessStbInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
  int32_t code = 0;

D
dapan1121 已提交
223 224
  SSTbHbRsp hbRsp = {0};
  if (tDeserializeSSTbHbRsp(value, valueLen, &hbRsp) != 0) {
S
Shengliang Guan 已提交
225 226 227 228
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }

D
dapan1121 已提交
229 230 231
  int32_t numOfMeta = taosArrayGetSize(hbRsp.pMetaRsp);
  for (int32_t i = 0; i < numOfMeta; ++i) {
    STableMetaRsp *rsp = taosArrayGet(hbRsp.pMetaRsp, i);
S
Shengliang Guan 已提交
232

D
dapan 已提交
233 234
    if (rsp->numOfColumns < 0) {
      tscDebug("hb remove stb, db:%s, stb:%s", rsp->dbFName, rsp->stbName);
D
dapan1121 已提交
235
      catalogRemoveStbMeta(pCatalog, rsp->dbFName, rsp->dbId, rsp->stbName, rsp->suid);
D
dapan 已提交
236
    } else {
D
dapan1121 已提交
237
      tscDebug("hb update stb, db:%s, stb:%s", rsp->dbFName, rsp->stbName);
S
Shengliang Guan 已提交
238
      if (rsp->pSchemas[0].colId != PRIMARYKEY_TIMESTAMP_COL_ID) {
239
        tscError("invalid colId[%" PRIi16 "] for the first column in table meta rsp msg", rsp->pSchemas[0].colId);
D
dapan1121 已提交
240
        tFreeSSTbHbRsp(&hbRsp);
D
dapan1121 已提交
241
        return TSDB_CODE_TSC_INVALID_VALUE;
D
dapan 已提交
242 243
      }

D
dapan1121 已提交
244
      catalogAsyncUpdateTableMeta(pCatalog, rsp);
D
dapan 已提交
245 246 247
    }
  }

D
dapan1121 已提交
248 249 250 251 252 253 254 255 256 257
  int32_t numOfIndex = taosArrayGetSize(hbRsp.pIndexRsp);
  for (int32_t i = 0; i < numOfIndex; ++i) {
    STableIndexRsp *rsp = taosArrayGet(hbRsp.pIndexRsp, i);

    catalogUpdateTableIndex(pCatalog, rsp);
  }

  taosArrayDestroy(hbRsp.pIndexRsp);
  hbRsp.pIndexRsp = NULL;

D
dapan1121 已提交
258
  tFreeSSTbHbRsp(&hbRsp);
D
dapan 已提交
259 260 261
  return TSDB_CODE_SUCCESS;
}

L
Liu Jicong 已提交
262
static int32_t hbQueryHbRspHandle(SAppHbMgr *pAppHbMgr, SClientHbRsp *pRsp) {
D
dapan1121 已提交
263
  SClientHbReq *pReq = taosHashAcquire(pAppHbMgr->activeInfo, &pRsp->connKey, sizeof(SClientHbKey));
D
dapan1121 已提交
264 265
  if (NULL == pReq) {
    tscWarn("pReq to get activeInfo, may be dropped, refId:%" PRIx64 ", type:%d", pRsp->connKey.tscRid,
L
Liu Jicong 已提交
266
            pRsp->connKey.connType);
D
dapan1121 已提交
267 268 269
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
270 271 272 273
  if (pRsp->query) {
    STscObj *pTscObj = (STscObj *)acquireTscObj(pRsp->connKey.tscRid);
    if (NULL == pTscObj) {
      tscDebug("tscObj rid %" PRIx64 " not exist", pRsp->connKey.tscRid);
274
    } else {
D
dapan1121 已提交
275
      if (pRsp->query->totalDnodes > 1 && !isEpsetEqual(&pTscObj->pAppInfo->mgmtEp.epSet, &pRsp->query->epSet)) {
276 277 278 279 280 281 282
        SEpSet *pOrig = &pTscObj->pAppInfo->mgmtEp.epSet;
        SEp    *pOrigEp = &pOrig->eps[pOrig->inUse];
        SEp    *pNewEp = &pRsp->query->epSet.eps[pRsp->query->epSet.inUse];
        tscDebug("mnode epset updated from %d/%d=>%s:%d to %d/%d=>%s:%d in hb", pOrig->inUse, pOrig->numOfEps,
                 pOrigEp->fqdn, pOrigEp->port, pRsp->query->epSet.inUse, pRsp->query->epSet.numOfEps, pNewEp->fqdn,
                 pNewEp->port);

D
dapan1121 已提交
283 284
        updateEpSet_s(&pTscObj->pAppInfo->mgmtEp, &pRsp->query->epSet);
      }
285

D
dapan1121 已提交
286 287
      pTscObj->pAppInfo->totalDnodes = pRsp->query->totalDnodes;
      pTscObj->pAppInfo->onlineDnodes = pRsp->query->onlineDnodes;
D
dapan1121 已提交
288
      pTscObj->connId = pRsp->query->connId;
wmmhello's avatar
wmmhello 已提交
289
      tscTrace("conn %u hb rsp, dnodes %d/%d", pTscObj->connId, pTscObj->pAppInfo->onlineDnodes,
dengyihao's avatar
dengyihao 已提交
290
               pTscObj->pAppInfo->totalDnodes);
L
Liu Jicong 已提交
291

D
dapan1121 已提交
292
      if (pRsp->query->killRid) {
D
dapan1121 已提交
293
        tscDebug("request rid %" PRIx64 " need to be killed now", pRsp->query->killRid);
D
dapan1121 已提交
294 295 296 297 298 299 300 301
        SRequestObj *pRequest = acquireRequest(pRsp->query->killRid);
        if (NULL == pRequest) {
          tscDebug("request 0x%" PRIx64 " not exist to kill", pRsp->query->killRid);
        } else {
          taos_stop_query((TAOS_RES *)pRequest);
          releaseRequest(pRsp->query->killRid);
        }
      }
L
Liu Jicong 已提交
302

D
dapan1121 已提交
303
      if (pRsp->query->killConnection) {
304
        taos_close_internal(pTscObj);
D
dapan1121 已提交
305 306
      }

D
dapan1121 已提交
307 308 309 310
      if (pRsp->query->pQnodeList) {
        updateQnodeList(pTscObj->pAppInfo, pRsp->query->pQnodeList);
      }

D
dapan1121 已提交
311 312 313
      releaseTscObj(pRsp->connKey.tscRid);
    }
  }
L
Liu Jicong 已提交
314

D
dapan1121 已提交
315
  int32_t kvNum = pRsp->info ? taosArrayGetSize(pRsp->info) : 0;
D
dapan1121 已提交
316 317

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

D
dapan1121 已提交
319 320 321
  for (int32_t i = 0; i < kvNum; ++i) {
    SKv *kv = taosArrayGet(pRsp->info, i);
    switch (kv->key) {
D
dapan 已提交
322 323 324 325 326 327 328 329
      case HEARTBEAT_KEY_USER_AUTHINFO: {
        if (kv->valueLen <= 0 || NULL == kv->value) {
          tscError("invalid hb user auth info, len:%d, value:%p", kv->valueLen, kv->value);
          break;
        }

        struct SCatalog *pCatalog = NULL;

D
dapan1121 已提交
330
        int32_t code = catalogGetHandle(pReq->clusterId, &pCatalog);
D
dapan 已提交
331
        if (code != TSDB_CODE_SUCCESS) {
D
dapan1121 已提交
332
          tscWarn("catalogGetHandle failed, clusterId:%" PRIx64 ", error:%s", pReq->clusterId, tstrerror(code));
D
dapan 已提交
333 334 335
          break;
        }

K
kailixu 已提交
336
        hbProcessUserAuthInfoRsp(kv->value, kv->valueLen, pCatalog, pAppHbMgr);
D
dapan 已提交
337 338
        break;
      }
D
dapan1121 已提交
339 340 341 342 343 344 345
      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;
        }

        struct SCatalog *pCatalog = NULL;
L
fix  
Liu Jicong 已提交
346

D
dapan1121 已提交
347
        int32_t code = catalogGetHandle(pReq->clusterId, &pCatalog);
D
dapan1121 已提交
348
        if (code != TSDB_CODE_SUCCESS) {
D
dapan1121 已提交
349
          tscWarn("catalogGetHandle failed, clusterId:%" PRIx64 ", error:%s", pReq->clusterId, tstrerror(code));
D
dapan1121 已提交
350 351 352 353
          break;
        }

        hbProcessDBInfoRsp(kv->value, kv->valueLen, pCatalog);
D
dapan1121 已提交
354
        break;
D
dapan1121 已提交
355
      }
L
fix  
Liu Jicong 已提交
356
      case HEARTBEAT_KEY_STBINFO: {
D
dapan 已提交
357 358 359 360
        if (kv->valueLen <= 0 || NULL == kv->value) {
          tscError("invalid hb stb info, len:%d, value:%p", kv->valueLen, kv->value);
          break;
        }
D
dapan1121 已提交
361

D
dapan 已提交
362
        struct SCatalog *pCatalog = NULL;
L
fix  
Liu Jicong 已提交
363

D
dapan1121 已提交
364
        int32_t code = catalogGetHandle(pReq->clusterId, &pCatalog);
D
dapan 已提交
365
        if (code != TSDB_CODE_SUCCESS) {
D
dapan1121 已提交
366
          tscWarn("catalogGetHandle failed, clusterId:%" PRIx64 ", error:%s", pReq->clusterId, tstrerror(code));
D
dapan 已提交
367 368 369 370
          break;
        }

        hbProcessStbInfoRsp(kv->value, kv->valueLen, pCatalog);
D
dapan1121 已提交
371
        break;
D
dapan 已提交
372
      }
D
dapan1121 已提交
373 374 375 376 377 378
      default:
        tscError("invalid hb key type:%d", kv->key);
        break;
    }
  }

D
dapan1121 已提交
379 380
  taosHashRelease(pAppHbMgr->activeInfo, pReq);

D
dapan1121 已提交
381 382 383
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
384
static int32_t hbAsyncCallBack(void *param, SDataBuf *pMsg, int32_t code) {
D
dapan1121 已提交
385 386 387 388
  if (0 == atomic_load_8(&clientHbMgr.inited)) {
    goto _return;
  }

389
  static int32_t    emptyRspNum = 0;
390
  int32_t           idx = *(int32_t *)param;
D
dapan1121 已提交
391
  SClientHbBatchRsp pRsp = {0};
D
dapan1121 已提交
392 393
  if (TSDB_CODE_SUCCESS == code) {
    tDeserializeSClientHbBatchRsp(pMsg->pData, pMsg->len, &pRsp);
394

dengyihao's avatar
dengyihao 已提交
395 396 397 398 399 400
    int32_t now = taosGetTimestampSec();
    int32_t delta = abs(now - pRsp.svrTimestamp);
    if (delta > timestampDeltaLimit) {
      code = TSDB_CODE_TIME_UNSYNCED;
      tscError("time diff: %ds is too big", delta);
    }
dengyihao's avatar
dengyihao 已提交
401 402
  }

D
dapan1121 已提交
403
  int32_t rspNum = taosArrayGetSize(pRsp.rsps);
D
dapan1121 已提交
404

D
dapan1121 已提交
405
  taosThreadMutexLock(&clientHbMgr.lock);
D
dapan1121 已提交
406

D
dapan1121 已提交
407 408 409 410
  SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, idx);
  if (pAppHbMgr == NULL) {
    taosThreadMutexUnlock(&clientHbMgr.lock);
    tscError("appHbMgr not exist, idx:%d", idx);
D
dapan1121 已提交
411
    taosMemoryFree(pMsg->pData);
dengyihao's avatar
dengyihao 已提交
412
    taosMemoryFree(pMsg->pEpSet);
D
dapan1121 已提交
413
    tFreeClientHbBatchRsp(&pRsp);
D
dapan1121 已提交
414 415 416
    return -1;
  }

D
dapan1121 已提交
417 418
  SAppInstInfo *pInst = pAppHbMgr->pAppInstInfo;

D
dapan1121 已提交
419
  if (code != 0) {
D
dapan1121 已提交
420
    pInst->onlineDnodes = pInst->totalDnodes ? 0 : -1;
421
    tscDebug("hb rsp error %s, update server status %d/%d", tstrerror(code), pInst->onlineDnodes, pInst->totalDnodes);
D
dapan1121 已提交
422 423
  }

D
dapan1121 已提交
424
  if (rspNum) {
L
fix  
Liu Jicong 已提交
425 426
    tscDebug("hb got %d rsp, %d empty rsp received before", rspNum,
             atomic_val_compare_exchange_32(&emptyRspNum, emptyRspNum, 0));
D
dapan1121 已提交
427 428 429 430 431
  } else {
    atomic_add_fetch_32(&emptyRspNum, 1);
  }

  for (int32_t i = 0; i < rspNum; ++i) {
L
fix  
Liu Jicong 已提交
432
    SClientHbRsp *rsp = taosArrayGet(pRsp.rsps, i);
D
dapan1121 已提交
433
    code = (*clientHbMgr.rspHandle[rsp->connKey.connType])(pAppHbMgr, rsp);
D
dapan1121 已提交
434 435 436 437
    if (code) {
      break;
    }
  }
D
dapan1121 已提交
438

D
dapan1121 已提交
439
  taosThreadMutexUnlock(&clientHbMgr.lock);
D
dapan1121 已提交
440

D
dapan1121 已提交
441
  tFreeClientHbBatchRsp(&pRsp);
D
dapan1121 已提交
442 443

_return:
H
Haojun Liao 已提交
444
  taosMemoryFree(pMsg->pData);
dengyihao's avatar
dengyihao 已提交
445
  taosMemoryFree(pMsg->pEpSet);
D
dapan1121 已提交
446
  return code;
L
Liu Jicong 已提交
447 448
}

D
dapan1121 已提交
449
int32_t hbBuildQueryDesc(SQueryHbReqBasic *hbBasic, STscObj *pObj) {
L
Liu Jicong 已提交
450
  int64_t    now = taosGetTimestampUs();
D
dapan1121 已提交
451
  SQueryDesc desc = {0};
L
Liu Jicong 已提交
452
  int32_t    code = 0;
D
dapan1121 已提交
453

L
Liu Jicong 已提交
454
  void *pIter = taosHashIterate(pObj->pRequests, NULL);
D
dapan1121 已提交
455
  while (pIter != NULL) {
L
Liu Jicong 已提交
456
    int64_t     *rid = pIter;
D
dapan1121 已提交
457
    SRequestObj *pRequest = acquireRequest(*rid);
D
dapan1121 已提交
458 459 460 461 462
    if (NULL == pRequest) {
      pIter = taosHashIterate(pObj->pRequests, pIter);
      continue;
    }

463
    if (pRequest->killed || 0 == pRequest->body.queryJob) {
D
dapan1121 已提交
464
      releaseRequest(*rid);
D
dapan1121 已提交
465
      pIter = taosHashIterate(pObj->pRequests, pIter);
D
dapan1121 已提交
466 467 468 469
      continue;
    }

    tstrncpy(desc.sql, pRequest->sqlstr, sizeof(desc.sql));
470
    desc.stime = pRequest->metric.start / 1000;
L
Liu Jicong 已提交
471
    desc.queryId = pRequest->requestId;
D
dapan1121 已提交
472
    desc.useconds = now - pRequest->metric.start;
L
Liu Jicong 已提交
473
    desc.reqRid = pRequest->self;
474
    desc.stableQuery = pRequest->stableQuery;
475
    desc.isSubQuery = pRequest->isSubReq;
D
dapan1121 已提交
476
    taosGetFqdn(desc.fqdn);
D
dapan1121 已提交
477
    desc.subPlanNum = pRequest->body.subplanNum;
D
dapan1121 已提交
478 479 480 481 482

    if (desc.subPlanNum) {
      desc.subDesc = taosArrayInit(desc.subPlanNum, sizeof(SQuerySubDesc));
      if (NULL == desc.subDesc) {
        releaseRequest(*rid);
S
Shengliang Guan 已提交
483
        return TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
484 485 486 487 488 489
      }

      code = schedulerGetTasksStatus(pRequest->body.queryJob, desc.subDesc);
      if (code) {
        taosArrayDestroy(desc.subDesc);
        desc.subDesc = NULL;
490
        desc.subPlanNum = 0;
D
dapan1121 已提交
491
      }
D
dapan1121 已提交
492
      desc.subPlanNum = taosArrayGetSize(desc.subDesc);
D
dapan1121 已提交
493 494
    } else {
      desc.subDesc = NULL;
D
dapan1121 已提交
495 496
    }

L
Liu Jicong 已提交
497
    releaseRequest(*rid);
D
dapan1121 已提交
498
    taosArrayPush(hbBasic->queryDesc, &desc);
L
Liu Jicong 已提交
499

D
dapan1121 已提交
500 501 502 503 504 505 506 507 508 509
    pIter = taosHashIterate(pObj->pRequests, pIter);
  }

  return TSDB_CODE_SUCCESS;
}

int32_t hbGetQueryBasicInfo(SClientHbKey *connKey, SClientHbReq *req) {
  STscObj *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
  if (NULL == pTscObj) {
    tscWarn("tscObj rid %" PRIx64 " not exist", connKey->tscRid);
S
Shengliang Guan 已提交
510
    return TSDB_CODE_APP_ERROR;
D
dapan1121 已提交
511
  }
L
Liu Jicong 已提交
512

D
dapan1121 已提交
513 514 515 516
  SQueryHbReqBasic *hbBasic = (SQueryHbReqBasic *)taosMemoryCalloc(1, sizeof(SQueryHbReqBasic));
  if (NULL == hbBasic) {
    tscError("calloc %d failed", (int32_t)sizeof(SQueryHbReqBasic));
    releaseTscObj(connKey->tscRid);
S
Shengliang Guan 已提交
517
    return TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
518
  }
519

520 521 522 523 524 525 526 527 528
  hbBasic->connId = pTscObj->connId;

  int32_t numOfQueries = pTscObj->pRequests ? taosHashGetSize(pTscObj->pRequests) : 0;
  if (numOfQueries <= 0) {
    req->query = hbBasic;
    releaseTscObj(connKey->tscRid);
    tscDebug("no queries on connection");
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
529 530 531 532 533 534

  hbBasic->queryDesc = taosArrayInit(numOfQueries, sizeof(SQueryDesc));
  if (NULL == hbBasic->queryDesc) {
    tscWarn("taosArrayInit %d queryDesc failed", numOfQueries);
    releaseTscObj(connKey->tscRid);
    taosMemoryFree(hbBasic);
S
Shengliang Guan 已提交
535
    return TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
536
  }
L
Liu Jicong 已提交
537

D
dapan1121 已提交
538 539 540
  int32_t code = hbBuildQueryDesc(hbBasic, pTscObj);
  if (code) {
    releaseTscObj(connKey->tscRid);
D
dapan1121 已提交
541 542 543
    if (hbBasic->queryDesc) {
      taosArrayDestroyEx(hbBasic->queryDesc, tFreeClientHbQueryDesc);
    }
D
dapan1121 已提交
544 545 546 547 548 549 550 551 552 553
    taosMemoryFree(hbBasic);
    return code;
  }

  req->query = hbBasic;
  releaseTscObj(connKey->tscRid);

  return TSDB_CODE_SUCCESS;
}

K
kailixu 已提交
554
static int32_t hbGetUserAuthInfo(SClientHbKey *connKey, SHbParam *param, SClientHbReq *req) {
K
kailixu 已提交
555 556 557 558 559 560
  STscObj *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
  if (!pTscObj) {
    tscWarn("tscObj rid %" PRIx64 " not exist", connKey->tscRid);
    return TSDB_CODE_APP_ERROR;
  }

561 562
  int32_t code = 0;

K
kailixu 已提交
563 564 565 566
  SKv  kv = {.key = HEARTBEAT_KEY_USER_AUTHINFO};
  SKv *pKv = NULL;
  if ((pKv = taosHashGet(req->info, &kv.key, sizeof(kv.key)))) {
    int32_t           userNum = pKv->valueLen / sizeof(SUserAuthVersion);
K
kailixu 已提交
567
    SUserAuthVersion *userAuths = (SUserAuthVersion *)pKv->value;
K
kailixu 已提交
568
    for (int32_t i = 0; i < userNum; ++i) {
K
kailixu 已提交
569 570
      SUserAuthVersion *pUserAuth = userAuths + i;
      // both key and user exist, update version
K
kailixu 已提交
571
      if (strncmp(pUserAuth->user, pTscObj->user, TSDB_USER_LEN) == 0) {
K
kailixu 已提交
572
        pUserAuth->version = htonl(-1);  // force get userAuthInfo
K
kailixu 已提交
573 574 575
        goto _return;
      }
    }
K
kailixu 已提交
576
    // key exists, user not exist, append user
K
kailixu 已提交
577 578 579 580
    SUserAuthVersion *qUserAuth =
        (SUserAuthVersion *)taosMemoryRealloc(pKv->value, (userNum + 1) * sizeof(SUserAuthVersion));
    if (qUserAuth) {
      strncpy((qUserAuth + userNum)->user, pTscObj->user, TSDB_USER_LEN);
K
kailixu 已提交
581
      (qUserAuth + userNum)->version = htonl(-1);  // force get userAuthInfo
K
kailixu 已提交
582 583 584 585 586
      pKv->value = qUserAuth;
      pKv->valueLen += sizeof(SUserAuthVersion);
    } else {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
587 588 589
    goto _return;
  }

K
kailixu 已提交
590
  // key/user not exist, add user
K
kailixu 已提交
591
  SUserAuthVersion *user = taosMemoryMalloc(sizeof(SUserAuthVersion));
K
kailixu 已提交
592 593 594 595 596
  if (!user) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _return;
  }
  strncpy(user->user, pTscObj->user, TSDB_USER_LEN);
K
kailixu 已提交
597
  user->version = htonl(-1);  // force get userAuthInfo
K
kailixu 已提交
598 599
  kv.valueLen = sizeof(SUserAuthVersion);
  kv.value = user;
K
kailixu 已提交
600

K
kailixu 已提交
601 602
  tscDebug("hb got user auth info, valueLen:%d, user:%s, authVer:%d, tscRid:%" PRIi64, kv.valueLen, user->user,
           pTscObj->authVer, connKey->tscRid);
K
kailixu 已提交
603 604 605 606 607 608

  if (!req->info) {
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
  }

  if (taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)) < 0) {
K
kailixu 已提交
609
    taosMemoryFree(user);
K
kailixu 已提交
610 611 612 613 614 615 616
    code = terrno ? terrno : TSDB_CODE_APP_ERROR;
    goto _return;
  }

_return:
  releaseTscObj(connKey->tscRid);
  if (code) {
K
kailixu 已提交
617
    tscError("hb got user auth info failed since %s", terrstr(code));
K
kailixu 已提交
618 619 620 621 622
  }

  return code;
}

D
dapan 已提交
623 624 625 626 627 628 629 630 631 632 633
int32_t hbGetExpiredUserInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
  SUserAuthVersion *users = NULL;
  uint32_t          userNum = 0;
  int32_t           code = 0;

  code = catalogGetExpiredUsers(pCatalog, &users, &userNum);
  if (TSDB_CODE_SUCCESS != code) {
    return code;
  }

  if (userNum <= 0) {
D
dapan1121 已提交
634
    taosMemoryFree(users);
D
dapan 已提交
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
    return TSDB_CODE_SUCCESS;
  }

  for (int32_t i = 0; i < userNum; ++i) {
    SUserAuthVersion *user = &users[i];
    user->version = htonl(user->version);
  }

  SKv kv = {
      .key = HEARTBEAT_KEY_USER_AUTHINFO,
      .valueLen = sizeof(SUserAuthVersion) * userNum,
      .value = users,
  };

  tscDebug("hb got %d expired users, valueLen:%d", userNum, kv.valueLen);

D
dapan1121 已提交
651 652 653
  if (NULL == req->info) {
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
  }
654

D
dapan 已提交
655 656 657 658 659
  taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
660
int32_t hbGetExpiredDBInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
D
dapan1121 已提交
661
  SDbCacheInfo *dbs = NULL;
L
fix  
Liu Jicong 已提交
662 663
  uint32_t      dbNum = 0;
  int32_t       code = 0;
D
dapan1121 已提交
664 665 666 667 668 669

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

D
dapan1121 已提交
670
  if (dbNum <= 0) {
D
dapan1121 已提交
671
    taosMemoryFree(dbs);
D
dapan1121 已提交
672 673 674
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
675
  for (int32_t i = 0; i < dbNum; ++i) {
D
dapan1121 已提交
676
    SDbCacheInfo *db = &dbs[i];
H
Haojun Liao 已提交
677
    tscDebug("the %dth expired dbFName:%s, dbId:%" PRId64 ", vgVersion:%d, cfgVersion:%d, numOfTable:%d, startTs:%" PRId64,
D
dapan1121 已提交
678
      i, db->dbFName, db->dbId, db->vgVersion, db->cfgVersion, db->numOfTable, db->stateTs);
D
dapan1121 已提交
679

D
dapan1121 已提交
680 681
    db->dbId = htobe64(db->dbId);
    db->vgVersion = htonl(db->vgVersion);
D
dapan1121 已提交
682
    db->cfgVersion = htonl(db->cfgVersion);
D
dapan 已提交
683
    db->numOfTable = htonl(db->numOfTable);
D
dapan1121 已提交
684
    db->stateTs = htobe64(db->stateTs);
D
dapan1121 已提交
685 686
  }

L
Liu Jicong 已提交
687 688
  SKv kv = {
      .key = HEARTBEAT_KEY_DBINFO,
D
dapan1121 已提交
689
      .valueLen = sizeof(SDbCacheInfo) * dbNum,
L
Liu Jicong 已提交
690 691
      .value = dbs,
  };
D
dapan1121 已提交
692 693 694

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

D
dapan1121 已提交
695 696 697 698
  if (NULL == req->info) {
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
  }

D
dapan1121 已提交
699 700 701 702 703
  taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));

  return TSDB_CODE_SUCCESS;
}

D
dapan 已提交
704
int32_t hbGetExpiredStbInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
D
dapan1121 已提交
705
  SSTableVersion *stbs = NULL;
706 707
  uint32_t        stbNum = 0;
  int32_t         code = 0;
D
dapan 已提交
708 709 710 711 712 713 714

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

  if (stbNum <= 0) {
D
dapan1121 已提交
715
    taosMemoryFree(stbs);
D
dapan 已提交
716 717 718 719
    return TSDB_CODE_SUCCESS;
  }

  for (int32_t i = 0; i < stbNum; ++i) {
D
dapan1121 已提交
720
    SSTableVersion *stb = &stbs[i];
D
dapan 已提交
721 722
    stb->suid = htobe64(stb->suid);
    stb->sversion = htons(stb->sversion);
L
fix  
Liu Jicong 已提交
723
    stb->tversion = htons(stb->tversion);
D
dapan1121 已提交
724
    stb->smaVer = htonl(stb->smaVer);
D
dapan 已提交
725 726
  }

L
Liu Jicong 已提交
727 728
  SKv kv = {
      .key = HEARTBEAT_KEY_STBINFO,
D
dapan1121 已提交
729
      .valueLen = sizeof(SSTableVersion) * stbNum,
L
Liu Jicong 已提交
730 731
      .value = stbs,
  };
D
dapan 已提交
732 733 734

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

D
dapan1121 已提交
735 736 737 738
  if (NULL == req->info) {
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
  }

D
dapan 已提交
739 740 741 742 743
  taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
744
int32_t hbGetAppInfo(int64_t clusterId, SClientHbReq *req) {
745
  SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
D
dapan1121 已提交
746 747 748 749 750 751
  if (NULL != pApp) {
    memcpy(&req->app, pApp, sizeof(*pApp));
  } else {
    memset(&req->app.summary, 0, sizeof(req->app.summary));
    req->app.pid = taosGetPId();
    req->app.appId = clientHbMgr.appId;
752
    taosGetAppName(req->app.name, NULL);
D
dapan1121 已提交
753 754 755 756 757
  }

  return TSDB_CODE_SUCCESS;
}

K
kailixu 已提交
758
int32_t hbQueryHbReqHandle(SClientHbKey *connKey, void *param, SClientHbReq *req) {
759 760 761 762 763 764 765 766 767 768
  int32_t   code = 0;
  SHbParam *hbParam = (SHbParam *)param;
  SCatalog *pCatalog = NULL;

  if (hbParam->reqCnt == 0) {
    code = catalogGetHandle(hbParam->clusterId, &pCatalog);
    if (code != TSDB_CODE_SUCCESS) {
      tscWarn("catalogGetHandle failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
      return code;
    }
D
dapan1121 已提交
769
  }
L
fix  
Liu Jicong 已提交
770

K
kailixu 已提交
771
  hbGetAppInfo(hbParam->clusterId, req);
D
dapan1121 已提交
772

D
dapan1121 已提交
773
  hbGetQueryBasicInfo(connKey, req);
L
Liu Jicong 已提交
774

775
  if (hbParam->reqCnt == 0) {
K
kailixu 已提交
776
    if (!taosHashGet(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(hbParam->clusterId))) {
K
kailixu 已提交
777 778 779 780 781
      code = hbGetExpiredUserInfo(connKey, pCatalog, req);
      if (TSDB_CODE_SUCCESS != code) {
        return code;
      }
    }
K
kailixu 已提交
782

K
kailixu 已提交
783
    // invoke after hbGetExpiredUserInfo
K
kailixu 已提交
784
    if (2 != atomic_load_8(&hbParam->pAppHbMgr->connHbFlag)) {
K
kailixu 已提交
785 786 787 788
      code = hbGetUserAuthInfo(connKey, hbParam, req);
      if (TSDB_CODE_SUCCESS != code) {
        return code;
      }
K
kailixu 已提交
789
      atomic_store_8(&hbParam->pAppHbMgr->connHbFlag, 1);
790
    }
D
dapan 已提交
791

792 793 794 795
    code = hbGetExpiredDBInfo(connKey, pCatalog, req);
    if (TSDB_CODE_SUCCESS != code) {
      return code;
    }
D
dapan1121 已提交
796

797 798 799 800
    code = hbGetExpiredStbInfo(connKey, pCatalog, req);
    if (TSDB_CODE_SUCCESS != code) {
      return code;
    }
D
dapan 已提交
801 802
  }

K
kailixu 已提交
803
  ++hbParam->reqCnt;  // success to get catalog info
804

D
dapan1121 已提交
805 806 807
  return TSDB_CODE_SUCCESS;
}

808 809
static FORCE_INLINE void hbMgrInitHandle() {
  // init all handle
D
dapan1121 已提交
810 811
  clientHbMgr.reqHandle[CONN_TYPE__QUERY] = hbQueryHbReqHandle;
  clientHbMgr.reqHandle[CONN_TYPE__TMQ] = hbMqHbReqHandle;
L
Liu Jicong 已提交
812

D
dapan1121 已提交
813 814
  clientHbMgr.rspHandle[CONN_TYPE__QUERY] = hbQueryHbRspHandle;
  clientHbMgr.rspHandle[CONN_TYPE__TMQ] = hbMqHbRspHandle;
L
Liu Jicong 已提交
815 816
}

L
fix  
Liu Jicong 已提交
817
SClientHbBatchReq *hbGatherAllInfo(SAppHbMgr *pAppHbMgr) {
wafwerar's avatar
wafwerar 已提交
818
  SClientHbBatchReq *pBatchReq = taosMemoryCalloc(1, sizeof(SClientHbBatchReq));
L
Liu Jicong 已提交
819
  if (pBatchReq == NULL) {
S
Shengliang Guan 已提交
820
    terrno = TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
821 822
    return NULL;
  }
L
Liu Jicong 已提交
823
  int32_t connKeyCnt = atomic_load_32(&pAppHbMgr->connKeyCnt);
L
Liu Jicong 已提交
824
  pBatchReq->reqs = taosArrayInit(connKeyCnt, sizeof(SClientHbReq));
825
  if (!pBatchReq->reqs) {
dengyihao's avatar
dengyihao 已提交
826 827 828
    tFreeClientHbBatchReq(pBatchReq);
    return NULL;
  }
D
dapan1121 已提交
829

K
kailixu 已提交
830
  void    *pIter = NULL;
831
  SHbParam param = {0};
K
kailixu 已提交
832 833 834 835 836 837 838 839
  while ((pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter))) {
    SClientHbReq *pOneReq = pIter;
    SClientHbKey *connKey = &pOneReq->connKey;
    STscObj      *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);

    if (!pTscObj) {
      continue;
    }
840

dengyihao's avatar
dengyihao 已提交
841
    pOneReq = taosArrayPush(pBatchReq->reqs, pOneReq);
842

K
kailixu 已提交
843
    switch (connKey->connType) {
K
kailixu 已提交
844
      case CONN_TYPE__QUERY: {
845 846 847
        if (param.clusterId == 0) {
          // init
          param.clusterId = pOneReq->clusterId;
K
kailixu 已提交
848
          param.pAppHbMgr = pAppHbMgr;
K
kailixu 已提交
849
          param.connHbFlag = atomic_load_8(&pAppHbMgr->connHbFlag);
850
        }
K
kailixu 已提交
851 852 853 854 855
        break;
      }
      default:
        break;
    }
K
kailixu 已提交
856 857
    if (clientHbMgr.reqHandle[connKey->connType]) {
      int32_t code = (*clientHbMgr.reqHandle[connKey->connType])(connKey, &param, pOneReq);
K
kailixu 已提交
858 859
      if (code) {
        tscWarn("hbGatherAllInfo failed since %s, tscRid:%" PRIi64 ", connType:%" PRIi8, tstrerror(code),
K
kailixu 已提交
860
                connKey->tscRid, connKey->connType);
K
kailixu 已提交
861 862
      }
    }
K
kailixu 已提交
863

K
kailixu 已提交
864
    releaseTscObj(connKey->tscRid);
L
Liu Jicong 已提交
865
  }
L
Liu Jicong 已提交
866

L
Liu Jicong 已提交
867
  return pBatchReq;
L
Liu Jicong 已提交
868 869
}

870
void hbThreadFuncUnexpectedStopped(void) { atomic_store_8(&clientHbMgr.threadStop, 2); }
wafwerar's avatar
wafwerar 已提交
871

872
void hbMergeSummary(SAppClusterSummary *dst, SAppClusterSummary *src) {
D
dapan1121 已提交
873 874 875 876 877 878 879 880 881 882 883 884 885
  dst->numOfInsertsReq += src->numOfInsertsReq;
  dst->numOfInsertRows += src->numOfInsertRows;
  dst->insertElapsedTime += src->insertElapsedTime;
  dst->insertBytes += src->insertBytes;
  dst->fetchBytes += src->fetchBytes;
  dst->queryElapsedTime += src->queryElapsedTime;
  dst->numOfSlowQueries += src->numOfSlowQueries;
  dst->totalRequests += src->totalRequests;
  dst->currentRequests += src->currentRequests;
}

int32_t hbGatherAppInfo(void) {
  SAppHbReq req = {0};
886
  int       sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
D
dapan1121 已提交
887 888 889 890 891
  if (sz > 0) {
    req.pid = taosGetPId();
    req.appId = clientHbMgr.appId;
    taosGetAppName(req.name, NULL);
  }
D
dapan1121 已提交
892 893

  taosHashClear(clientHbMgr.appSummary);
894

D
dapan1121 已提交
895 896
  for (int32_t i = 0; i < sz; ++i) {
    SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
dengyihao's avatar
dengyihao 已提交
897 898
    if (pAppHbMgr == NULL) continue;

899 900
    uint64_t   clusterId = pAppHbMgr->pAppInstInfo->clusterId;
    SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
D
dapan1121 已提交
901 902
    if (NULL == pApp) {
      memcpy(&req.summary, &pAppHbMgr->pAppInstInfo->summary, sizeof(req.summary));
D
dapan1121 已提交
903
      req.startTime = pAppHbMgr->startTime;
D
dapan1121 已提交
904 905 906 907 908
      taosHashPut(clientHbMgr.appSummary, &clusterId, sizeof(clusterId), &req, sizeof(req));
    } else {
      if (pAppHbMgr->startTime < pApp->startTime) {
        pApp->startTime = pAppHbMgr->startTime;
      }
909

D
dapan1121 已提交
910 911 912 913 914 915 916
      hbMergeSummary(&pApp->summary, &pAppHbMgr->pAppInstInfo->summary);
    }
  }

  return TSDB_CODE_SUCCESS;
}

L
fix  
Liu Jicong 已提交
917
static void *hbThreadFunc(void *param) {
L
Liu Jicong 已提交
918
  setThreadName("hb");
wafwerar's avatar
wafwerar 已提交
919
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
920 921 922
  if (taosCheckCurrentInDll()) {
    atexit(hbThreadFuncUnexpectedStopped);
  }
wafwerar's avatar
wafwerar 已提交
923
#endif
L
Liu Jicong 已提交
924
  while (1) {
925
    if (1 == clientHbMgr.threadStop) {
L
Liu Jicong 已提交
926 927 928
      break;
    }

wafwerar's avatar
wafwerar 已提交
929
    taosThreadMutexLock(&clientHbMgr.lock);
930

L
Liu Jicong 已提交
931
    int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
D
dapan1121 已提交
932 933
    if (sz > 0) {
      hbGatherAppInfo();
K
kailixu 已提交
934
      if (sz > 1 && !clientHbMgr.appHbHash) {
K
kailixu 已提交
935
        clientHbMgr.appHbHash = taosHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_NO_LOCK);
K
kailixu 已提交
936
      }
K
kailixu 已提交
937
      taosHashClear(clientHbMgr.appHbHash);
D
dapan1121 已提交
938
    }
939

L
fix  
Liu Jicong 已提交
940 941
    for (int i = 0; i < sz; i++) {
      SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
dengyihao's avatar
dengyihao 已提交
942 943 944
      if (pAppHbMgr == NULL) {
        continue;
      }
L
Liu Jicong 已提交
945

L
Liu Jicong 已提交
946 947 948 949
      int32_t connCnt = atomic_load_32(&pAppHbMgr->connKeyCnt);
      if (connCnt == 0) {
        continue;
      }
L
fix  
Liu Jicong 已提交
950
      SClientHbBatchReq *pReq = hbGatherAllInfo(pAppHbMgr);
dengyihao's avatar
dengyihao 已提交
951 952
      if (pReq == NULL || taosArrayGetP(clientHbMgr.appHbMgrs, i) == NULL) {
        tFreeClientHbBatchReq(pReq);
L
Liu Jicong 已提交
953 954
        continue;
      }
L
fix  
Liu Jicong 已提交
955
      int   tlen = tSerializeSClientHbBatchReq(NULL, 0, pReq);
wafwerar's avatar
wafwerar 已提交
956
      void *buf = taosMemoryMalloc(tlen);
L
Liu Jicong 已提交
957
      if (buf == NULL) {
S
Shengliang Guan 已提交
958
        terrno = TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
959
        tFreeClientHbBatchReq(pReq);
dengyihao's avatar
dengyihao 已提交
960
        // hbClearReqInfo(pAppHbMgr);
L
Liu Jicong 已提交
961 962
        break;
      }
L
fix  
Liu Jicong 已提交
963

S
Shengliang Guan 已提交
964
      tSerializeSClientHbBatchReq(buf, tlen, pReq);
wafwerar's avatar
wafwerar 已提交
965
      SMsgSendInfo *pInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
966

L
Liu Jicong 已提交
967
      if (pInfo == NULL) {
S
Shengliang Guan 已提交
968
        terrno = TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
969
        tFreeClientHbBatchReq(pReq);
dengyihao's avatar
dengyihao 已提交
970
        // hbClearReqInfo(pAppHbMgr);
wafwerar's avatar
wafwerar 已提交
971
        taosMemoryFree(buf);
L
Liu Jicong 已提交
972 973
        break;
      }
L
Liu Jicong 已提交
974
      pInfo->fp = hbAsyncCallBack;
L
Liu Jicong 已提交
975 976 977
      pInfo->msgInfo.pData = buf;
      pInfo->msgInfo.len = tlen;
      pInfo->msgType = TDMT_MND_HEARTBEAT;
D
dapan1121 已提交
978 979
      pInfo->param = taosMemoryMalloc(sizeof(int32_t));
      *(int32_t *)pInfo->param = i;
dengyihao's avatar
dengyihao 已提交
980
      pInfo->paramFreeFp = taosMemoryFree;
L
Liu Jicong 已提交
981 982
      pInfo->requestId = generateRequestId();
      pInfo->requestObjRefId = 0;
L
Liu Jicong 已提交
983 984

      SAppInstInfo *pAppInstInfo = pAppHbMgr->pAppInstInfo;
L
fix  
Liu Jicong 已提交
985 986
      int64_t       transporterId = 0;
      SEpSet        epSet = getEpSet_s(&pAppInstInfo->mgmtEp);
L
Liu Jicong 已提交
987
      asyncSendMsgToServer(pAppInstInfo->pTransporter, &epSet, &transporterId, pInfo);
D
dapan1121 已提交
988
      tFreeClientHbBatchReq(pReq);
dengyihao's avatar
dengyihao 已提交
989
      // hbClearReqInfo(pAppHbMgr);
K
kailixu 已提交
990
      taosHashPut(clientHbMgr.appHbHash, &pAppHbMgr->pAppInstInfo->clusterId, sizeof(uint64_t), NULL, 0);
L
Liu Jicong 已提交
991 992
      atomic_add_fetch_32(&pAppHbMgr->reportCnt, 1);
    }
993

wafwerar's avatar
wafwerar 已提交
994
    taosThreadMutexUnlock(&clientHbMgr.lock);
L
fix  
Liu Jicong 已提交
995

L
Liu Jicong 已提交
996
    taosMsleep(HEARTBEAT_INTERVAL);
L
Liu Jicong 已提交
997
  }
K
kailixu 已提交
998
  taosHashCleanup(clientHbMgr.appHbHash);
L
Liu Jicong 已提交
999 1000 1001 1002
  return NULL;
}

static int32_t hbCreateThread() {
wafwerar's avatar
wafwerar 已提交
1003 1004 1005
  TdThreadAttr thAttr;
  taosThreadAttrInit(&thAttr);
  taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
L
Liu Jicong 已提交
1006

D
dapan1121 已提交
1007 1008 1009
  if (taosThreadCreate(&clientHbMgr.thread, &thAttr, hbThreadFunc, NULL) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
L
Liu Jicong 已提交
1010
  }
D
dapan1121 已提交
1011
  taosThreadAttrDestroy(&thAttr);
L
Liu Jicong 已提交
1012 1013 1014
  return 0;
}

L
Liu Jicong 已提交
1015
static void hbStopThread() {
D
dapan1121 已提交
1016 1017 1018
  if (0 == atomic_load_8(&clientHbMgr.inited)) {
    return;
  }
1019
  if (atomic_val_compare_exchange_8(&clientHbMgr.threadStop, 0, 1)) {
D
dapan1121 已提交
1020
    tscDebug("hb thread already stopped");
1021 1022
    return;
  }
L
fix  
Liu Jicong 已提交
1023

1024 1025 1026 1027 1028 1029
  // thread quit mode kill or inner exit from self-thread
  if (clientHbMgr.quitByKill) {
    taosThreadKill(clientHbMgr.thread, 0);
  } else {
    taosThreadJoin(clientHbMgr.thread, NULL);
  }
D
dapan1121 已提交
1030

L
fix  
Liu Jicong 已提交
1031
  tscDebug("hb thread stopped");
L
Liu Jicong 已提交
1032 1033
}

L
fix  
Liu Jicong 已提交
1034
SAppHbMgr *appHbMgrInit(SAppInstInfo *pAppInstInfo, char *key) {
1035
  if (hbMgrInit() != 0) {
1036 1037 1038
    terrno = TSDB_CODE_TSC_INTERNAL_ERROR;
    return NULL;
  }
wafwerar's avatar
wafwerar 已提交
1039
  SAppHbMgr *pAppHbMgr = taosMemoryMalloc(sizeof(SAppHbMgr));
L
Liu Jicong 已提交
1040 1041 1042 1043 1044 1045
  if (pAppHbMgr == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
  // init stat
  pAppHbMgr->startTime = taosGetTimestampMs();
L
Liu Jicong 已提交
1046
  pAppHbMgr->connKeyCnt = 0;
K
kailixu 已提交
1047
  pAppHbMgr->connHbFlag = 0;
L
Liu Jicong 已提交
1048 1049
  pAppHbMgr->reportCnt = 0;
  pAppHbMgr->reportBytes = 0;
1050
  pAppHbMgr->key = taosStrdup(key);
L
Liu Jicong 已提交
1051

L
Liu Jicong 已提交
1052 1053
  // init app info
  pAppHbMgr->pAppInstInfo = pAppInstInfo;
L
Liu Jicong 已提交
1054 1055 1056

  // init hash info
  pAppHbMgr->activeInfo = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
L
Liu Jicong 已提交
1057 1058 1059

  if (pAppHbMgr->activeInfo == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
wafwerar's avatar
wafwerar 已提交
1060
    taosMemoryFree(pAppHbMgr);
L
Liu Jicong 已提交
1061 1062
    return NULL;
  }
H
Haojun Liao 已提交
1063

1064
  // taosHashSetFreeFp(pAppHbMgr->activeInfo, tFreeClientHbReq);
L
Liu Jicong 已提交
1065

wafwerar's avatar
wafwerar 已提交
1066
  taosThreadMutexLock(&clientHbMgr.lock);
L
Liu Jicong 已提交
1067
  taosArrayPush(clientHbMgr.appHbMgrs, &pAppHbMgr);
D
dapan1121 已提交
1068
  pAppHbMgr->idx = taosArrayGetSize(clientHbMgr.appHbMgrs) - 1;
wafwerar's avatar
wafwerar 已提交
1069
  taosThreadMutexUnlock(&clientHbMgr.lock);
L
fix  
Liu Jicong 已提交
1070

L
Liu Jicong 已提交
1071 1072 1073
  return pAppHbMgr;
}

D
dapan1121 已提交
1074 1075 1076 1077 1078 1079 1080 1081 1082
void hbFreeAppHbMgr(SAppHbMgr *pTarget) {
  void *pIter = taosHashIterate(pTarget->activeInfo, NULL);
  while (pIter != NULL) {
    SClientHbReq *pOneReq = pIter;
    tFreeClientHbReq(pOneReq);
    pIter = taosHashIterate(pTarget->activeInfo, pIter);
  }
  taosHashCleanup(pTarget->activeInfo);
  pTarget->activeInfo = NULL;
1083

D
dapan1121 已提交
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
  taosMemoryFree(pTarget->key);
  taosMemoryFree(pTarget);
}

void hbRemoveAppHbMrg(SAppHbMgr **pAppHbMgr) {
  taosThreadMutexLock(&clientHbMgr.lock);
  int32_t mgrSize = taosArrayGetSize(clientHbMgr.appHbMgrs);
  for (int32_t i = 0; i < mgrSize; ++i) {
    SAppHbMgr *pItem = taosArrayGetP(clientHbMgr.appHbMgrs, i);
    if (pItem == *pAppHbMgr) {
      hbFreeAppHbMgr(*pAppHbMgr);
      *pAppHbMgr = NULL;
dengyihao's avatar
dengyihao 已提交
1096
      taosArraySet(clientHbMgr.appHbMgrs, i, pAppHbMgr);
D
dapan1121 已提交
1097 1098 1099 1100 1101 1102
      break;
    }
  }
  taosThreadMutexUnlock(&clientHbMgr.lock);
}

D
dapan1121 已提交
1103
void appHbMgrCleanup(void) {
L
Liu Jicong 已提交
1104 1105
  int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
  for (int i = 0; i < sz; i++) {
L
fix  
Liu Jicong 已提交
1106
    SAppHbMgr *pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i);
dengyihao's avatar
dengyihao 已提交
1107
    if (pTarget == NULL) continue;
D
dapan1121 已提交
1108
    hbFreeAppHbMgr(pTarget);
L
Liu Jicong 已提交
1109 1110 1111 1112
  }
}

int hbMgrInit() {
L
Liu Jicong 已提交
1113 1114 1115 1116
  // init once
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 0, 1);
  if (old == 1) return 0;

D
dapan1121 已提交
1117 1118
  clientHbMgr.appId = tGenIdPI64();
  tscDebug("app %" PRIx64 " initialized", clientHbMgr.appId);
1119

D
dapan1121 已提交
1120
  clientHbMgr.appSummary = taosHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
L
fix  
Liu Jicong 已提交
1121
  clientHbMgr.appHbMgrs = taosArrayInit(0, sizeof(void *));
dengyihao's avatar
dengyihao 已提交
1122 1123

  TdThreadMutexAttr attr = {0};
dengyihao's avatar
dengyihao 已提交
1124

dengyihao's avatar
dengyihao 已提交
1125
  int ret = taosThreadMutexAttrInit(&attr);
1126 1127
  if (ret != 0) {
    uError("hbMgrInit:taosThreadMutexAttrInit error") return ret;
1128
  }
dengyihao's avatar
dengyihao 已提交
1129

dengyihao's avatar
dengyihao 已提交
1130
  ret = taosThreadMutexAttrSetType(&attr, PTHREAD_MUTEX_RECURSIVE);
1131 1132
  if (ret != 0) {
    uError("hbMgrInit:taosThreadMutexAttrSetType error") return ret;
1133
  }
dengyihao's avatar
dengyihao 已提交
1134 1135

  ret = taosThreadMutexInit(&clientHbMgr.lock, &attr);
1136 1137
  if (ret != 0) {
    uError("hbMgrInit:taosThreadMutexInit error") return ret;
1138
  }
dengyihao's avatar
dengyihao 已提交
1139 1140

  ret = taosThreadMutexAttrDestroy(&attr);
1141 1142
  if (ret != 0) {
    uError("hbMgrInit:taosThreadMutexAttrDestroy error") return ret;
1143
  }
L
Liu Jicong 已提交
1144 1145 1146 1147 1148

  // init handle funcs
  hbMgrInitHandle();

  // init backgroud thread
D
dapan1121 已提交
1149
  hbCreateThread();
L
Liu Jicong 已提交
1150

L
Liu Jicong 已提交
1151 1152 1153 1154
  return 0;
}

void hbMgrCleanUp() {
D
dapan1121 已提交
1155
  hbStopThread();
L
fix  
Liu Jicong 已提交
1156

L
Liu Jicong 已提交
1157
  // destroy all appHbMgr
L
Liu Jicong 已提交
1158 1159 1160
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 1, 0);
  if (old == 0) return;

wafwerar's avatar
wafwerar 已提交
1161
  taosThreadMutexLock(&clientHbMgr.lock);
D
dapan1121 已提交
1162
  appHbMgrCleanup();
L
fix  
Liu Jicong 已提交
1163
  taosArrayDestroy(clientHbMgr.appHbMgrs);
wafwerar's avatar
wafwerar 已提交
1164
  taosThreadMutexUnlock(&clientHbMgr.lock);
1165
  clientHbMgr.appHbMgrs = NULL;
L
Liu Jicong 已提交
1166 1167
}

D
dapan1121 已提交
1168
int hbRegisterConnImpl(SAppHbMgr *pAppHbMgr, SClientHbKey connKey, int64_t clusterId) {
L
Liu Jicong 已提交
1169
  // init hash in activeinfo
L
fix  
Liu Jicong 已提交
1170
  void *data = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
L
Liu Jicong 已提交
1171 1172 1173
  if (data != NULL) {
    return 0;
  }
D
dapan1121 已提交
1174
  SClientHbReq hbReq = {0};
L
Liu Jicong 已提交
1175
  hbReq.connKey = connKey;
D
dapan1121 已提交
1176
  hbReq.clusterId = clusterId;
1177
  // hbReq.info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
L
fix  
Liu Jicong 已提交
1178

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

L
Liu Jicong 已提交
1181
  atomic_add_fetch_32(&pAppHbMgr->connKeyCnt, 1);
L
Liu Jicong 已提交
1182 1183 1184
  return 0;
}

D
dapan1121 已提交
1185
int hbRegisterConn(SAppHbMgr *pAppHbMgr, int64_t tscRefId, int64_t clusterId, int8_t connType) {
L
Liu Jicong 已提交
1186
  SClientHbKey connKey = {
D
dapan1121 已提交
1187
      .tscRid = tscRefId,
D
dapan1121 已提交
1188
      .connType = connType,
L
Liu Jicong 已提交
1189
  };
D
dapan1121 已提交
1190

D
dapan1121 已提交
1191 1192
  switch (connType) {
    case CONN_TYPE__QUERY: {
D
dapan1121 已提交
1193
      return hbRegisterConnImpl(pAppHbMgr, connKey, clusterId);
D
dapan1121 已提交
1194
    }
D
dapan1121 已提交
1195
    case CONN_TYPE__TMQ: {
L
Liu Jicong 已提交
1196
      return 0;
D
dapan1121 已提交
1197 1198
    }
    default:
L
Liu Jicong 已提交
1199
      return 0;
D
dapan1121 已提交
1200 1201 1202
  }
}

K
kailixu 已提交
1203 1204
void hbDeregisterConn(STscObj *pTscObj, SClientHbKey connKey) {
  SAppHbMgr    *pAppHbMgr = pTscObj->pAppInfo->pAppHbMgr;
D
dapan1121 已提交
1205
  SClientHbReq *pReq = taosHashAcquire(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
D
dapan1121 已提交
1206
  if (pReq) {
D
dapan1121 已提交
1207
    tFreeClientHbReq(pReq);
D
dapan1121 已提交
1208
    taosHashRemove(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
D
dapan1121 已提交
1209
    taosHashRelease(pAppHbMgr->activeInfo, pReq);
D
dapan1121 已提交
1210 1211
  }

D
dapan1121 已提交
1212
  if (NULL == pReq) {
D
dapan1121 已提交
1213 1214
    return;
  }
L
Liu Jicong 已提交
1215

L
Liu Jicong 已提交
1216
  atomic_sub_fetch_32(&pAppHbMgr->connKeyCnt, 1);
L
Liu Jicong 已提交
1217
}
1218 1219 1220 1221 1222

// set heart beat thread quit mode , if quicByKill 1 then kill thread else quit from inner
void taos_set_hb_quit(int8_t quitByKill) {
  clientHbMgr.quitByKill = quitByKill;
}