clientHb.c 36.5 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"
K
kailixu 已提交
21
#include "tsimplehash.h"
L
Liu Jicong 已提交
22

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

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

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

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
static int32_t hbUpdateUserAuthInfo(SAppHbMgr *pAppHbMgr, SUserAuthBatchRsp *batchRsp) {
K
kailixu 已提交
44 45 46 47 48 49
  SClientHbReq *pReq = NULL;
  while ((pReq = taosHashIterate(pAppHbMgr->activeInfo, pReq))) {
    STscObj *pTscObj = (STscObj *)acquireTscObj(pReq->connKey.tscRid);
    if (!pTscObj) {
      continue;
    }
K
kailixu 已提交
50 51
    for (int32_t i = 0; i < TARRAY_SIZE(batchRsp->pArray); ++i) {
      SGetUserAuthRsp *rsp = taosArrayGet(batchRsp->pArray, i);
K
kailixu 已提交
52

K
kailixu 已提交
53
      if (0 == strncmp(rsp->user, pTscObj->user, TSDB_USER_LEN)) {
K
kailixu 已提交
54 55 56
        
        pTscObj->authVer = rsp->version;

K
kailixu 已提交
57
#if 0 // make jenkins happy temporarily. After PR pass, enable these lines again.
K
kailixu 已提交
58
        if (pTscObj->sysInfo != rsp->sysInfo) {
K
kailixu 已提交
59 60
          tscDebug("update sysInfo of user %s from %" PRIi8 " to %" PRIi8 ", tscRid:%" PRIi64, rsp->user,
                   pTscObj->sysInfo, rsp->sysInfo, pTscObj->id);
K
kailixu 已提交
61 62
          pTscObj->sysInfo = rsp->sysInfo;
        }
K
kailixu 已提交
63
#endif
K
kailixu 已提交
64 65 66
        if (pTscObj->passInfo.fp) {
          SPassInfo *passInfo = &pTscObj->passInfo;
          int32_t    oldVer = atomic_load_32(&passInfo->ver);
K
kailixu 已提交
67 68 69
          if (oldVer < rsp->passVer) {
            atomic_store_32(&passInfo->ver, rsp->passVer);
            if (passInfo->fp) {
K
kailixu 已提交
70
              (*passInfo->fp)(passInfo->param, &rsp->passVer, TAOS_NOTIFY_PASSVER);
K
kailixu 已提交
71
            }
K
kailixu 已提交
72 73
            tscDebug("update passVer of user %s from %d to %d, tscRid:%" PRIi64, rsp->user, oldVer,
                     atomic_load_32(&passInfo->ver), pTscObj->id);
K
kailixu 已提交
74
          }
K
kailixu 已提交
75
        }
K
kailixu 已提交
76

K
kailixu 已提交
77
        break;
K
kailixu 已提交
78 79
      }
    }
K
kailixu 已提交
80
    releaseTscObj(pReq->connKey.tscRid);
K
kailixu 已提交
81
  }
K
kailixu 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  return 0;
}

static int32_t hbProcessUserAuthInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog,
                                        SAppHbMgr *pAppHbMgr) {
  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);
  }

  if (numOfBatchs > 0) hbUpdateUserAuthInfo(pAppHbMgr, &batchRsp);

  atomic_val_compare_exchange_8(&pAppHbMgr->connHbFlag, 1, 2);
K
kailixu 已提交
106 107

  taosArrayDestroy(batchRsp.pArray);
K
kailixu 已提交
108
  return TSDB_CODE_SUCCESS;
K
kailixu 已提交
109 110
}

D
dapan1121 已提交
111
static int32_t hbGenerateVgInfoFromRsp(SDBVgInfo **pInfo, SUseDbRsp *rsp) {
112
  int32_t    code = 0;
D
dapan1121 已提交
113 114 115 116 117
  SDBVgInfo *vgInfo = taosMemoryCalloc(1, sizeof(SDBVgInfo));
  if (NULL == vgInfo) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    return code;
  }
118

D
dapan1121 已提交
119 120 121 122 123 124 125 126 127 128 129 130
  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;
  }
131

D
dapan1121 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
  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 已提交
153 154 155
static int32_t hbProcessDBInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
  int32_t code = 0;

D
dapan1121 已提交
156 157
  SDbHbBatchRsp batchRsp = {0};
  if (tDeserializeSDbHbBatchRsp(value, valueLen, &batchRsp) != 0) {
S
Shengliang Guan 已提交
158
    terrno = TSDB_CODE_INVALID_MSG;
159 160
    code = terrno;
    goto _return;
S
Shengliang Guan 已提交
161
  }
D
dapan1121 已提交
162

D
dapan1121 已提交
163
  int32_t numOfBatchs = taosArrayGetSize(batchRsp.pArray);
S
Shengliang Guan 已提交
164
  for (int32_t i = 0; i < numOfBatchs; ++i) {
D
dapan1121 已提交
165 166
    SDbHbRsp *rsp = taosArrayGet(batchRsp.pArray, i);
    if (rsp->useDbRsp) {
H
Haojun Liao 已提交
167
      tscDebug("hb use db rsp, db:%s, vgVersion:%d, stateTs:%" PRId64 ", uid:%" PRIx64,
D
dapan1121 已提交
168
        rsp->useDbRsp->db, rsp->useDbRsp->vgVersion, rsp->useDbRsp->stateTs, rsp->useDbRsp->uid);
H
Haojun Liao 已提交
169

D
dapan1121 已提交
170 171 172 173 174
      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 已提交
175
        if (TSDB_CODE_SUCCESS != code) {
D
dapan1121 已提交
176
          goto _return;
D
dapan1121 已提交
177
        }
L
fix  
Liu Jicong 已提交
178

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

D
dapan1121 已提交
181 182 183 184 185
        if (IS_SYS_DBNAME(rsp->useDbRsp->db)) {
          code = hbGenerateVgInfoFromRsp(&vgInfo, rsp->useDbRsp);
          if (TSDB_CODE_SUCCESS != code) {
            goto _return;
          }
H
Haojun Liao 已提交
186

D
dapan1121 已提交
187 188
          catalogUpdateDBVgInfo(pCatalog, (rsp->useDbRsp->db[0] == 'i') ? TSDB_PERFORMANCE_SCHEMA_DB : TSDB_INFORMATION_SCHEMA_DB, rsp->useDbRsp->uid, vgInfo);
        }
D
dapan1121 已提交
189
      }
D
dapan1121 已提交
190 191
    }

D
dapan1121 已提交
192 193 194 195
    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 已提交
196 197 198
    }
  }

D
dapan1121 已提交
199 200
_return:

D
dapan1121 已提交
201
  tFreeSDbHbBatchRsp(&batchRsp);
D
dapan1121 已提交
202
  return code;
D
dapan1121 已提交
203 204
}

D
dapan 已提交
205 206 207
static int32_t hbProcessStbInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
  int32_t code = 0;

D
dapan1121 已提交
208 209
  SSTbHbRsp hbRsp = {0};
  if (tDeserializeSSTbHbRsp(value, valueLen, &hbRsp) != 0) {
S
Shengliang Guan 已提交
210 211 212 213
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }

D
dapan1121 已提交
214 215 216
  int32_t numOfMeta = taosArrayGetSize(hbRsp.pMetaRsp);
  for (int32_t i = 0; i < numOfMeta; ++i) {
    STableMetaRsp *rsp = taosArrayGet(hbRsp.pMetaRsp, i);
S
Shengliang Guan 已提交
217

D
dapan 已提交
218 219
    if (rsp->numOfColumns < 0) {
      tscDebug("hb remove stb, db:%s, stb:%s", rsp->dbFName, rsp->stbName);
D
dapan1121 已提交
220
      catalogRemoveStbMeta(pCatalog, rsp->dbFName, rsp->dbId, rsp->stbName, rsp->suid);
D
dapan 已提交
221
    } else {
D
dapan1121 已提交
222
      tscDebug("hb update stb, db:%s, stb:%s", rsp->dbFName, rsp->stbName);
S
Shengliang Guan 已提交
223
      if (rsp->pSchemas[0].colId != PRIMARYKEY_TIMESTAMP_COL_ID) {
224
        tscError("invalid colId[%" PRIi16 "] for the first column in table meta rsp msg", rsp->pSchemas[0].colId);
D
dapan1121 已提交
225
        tFreeSSTbHbRsp(&hbRsp);
D
dapan1121 已提交
226
        return TSDB_CODE_TSC_INVALID_VALUE;
D
dapan 已提交
227 228
      }

D
dapan1121 已提交
229
      catalogAsyncUpdateTableMeta(pCatalog, rsp);
D
dapan 已提交
230 231 232
    }
  }

D
dapan1121 已提交
233 234 235 236 237 238 239 240 241 242
  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 已提交
243
  tFreeSSTbHbRsp(&hbRsp);
D
dapan 已提交
244 245 246
  return TSDB_CODE_SUCCESS;
}

L
Liu Jicong 已提交
247
static int32_t hbQueryHbRspHandle(SAppHbMgr *pAppHbMgr, SClientHbRsp *pRsp) {
D
dapan1121 已提交
248
  SClientHbReq *pReq = taosHashAcquire(pAppHbMgr->activeInfo, &pRsp->connKey, sizeof(SClientHbKey));
D
dapan1121 已提交
249 250
  if (NULL == pReq) {
    tscWarn("pReq to get activeInfo, may be dropped, refId:%" PRIx64 ", type:%d", pRsp->connKey.tscRid,
L
Liu Jicong 已提交
251
            pRsp->connKey.connType);
D
dapan1121 已提交
252 253 254
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
255 256 257 258
  if (pRsp->query) {
    STscObj *pTscObj = (STscObj *)acquireTscObj(pRsp->connKey.tscRid);
    if (NULL == pTscObj) {
      tscDebug("tscObj rid %" PRIx64 " not exist", pRsp->connKey.tscRid);
259
    } else {
D
dapan1121 已提交
260
      if (pRsp->query->totalDnodes > 1 && !isEpsetEqual(&pTscObj->pAppInfo->mgmtEp.epSet, &pRsp->query->epSet)) {
261 262 263 264 265 266 267
        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 已提交
268 269
        updateEpSet_s(&pTscObj->pAppInfo->mgmtEp, &pRsp->query->epSet);
      }
270

D
dapan1121 已提交
271 272
      pTscObj->pAppInfo->totalDnodes = pRsp->query->totalDnodes;
      pTscObj->pAppInfo->onlineDnodes = pRsp->query->onlineDnodes;
D
dapan1121 已提交
273
      pTscObj->connId = pRsp->query->connId;
wmmhello's avatar
wmmhello 已提交
274
      tscTrace("conn %u hb rsp, dnodes %d/%d", pTscObj->connId, pTscObj->pAppInfo->onlineDnodes,
dengyihao's avatar
dengyihao 已提交
275
               pTscObj->pAppInfo->totalDnodes);
L
Liu Jicong 已提交
276

D
dapan1121 已提交
277
      if (pRsp->query->killRid) {
D
dapan1121 已提交
278
        tscDebug("request rid %" PRIx64 " need to be killed now", pRsp->query->killRid);
D
dapan1121 已提交
279 280 281 282 283 284 285 286
        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 已提交
287

D
dapan1121 已提交
288
      if (pRsp->query->killConnection) {
289
        taos_close_internal(pTscObj);
D
dapan1121 已提交
290 291
      }

D
dapan1121 已提交
292 293 294 295
      if (pRsp->query->pQnodeList) {
        updateQnodeList(pTscObj->pAppInfo, pRsp->query->pQnodeList);
      }

D
dapan1121 已提交
296 297 298
      releaseTscObj(pRsp->connKey.tscRid);
    }
  }
L
Liu Jicong 已提交
299

D
dapan1121 已提交
300
  int32_t kvNum = pRsp->info ? taosArrayGetSize(pRsp->info) : 0;
D
dapan1121 已提交
301 302

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

D
dapan1121 已提交
304 305 306
  for (int32_t i = 0; i < kvNum; ++i) {
    SKv *kv = taosArrayGet(pRsp->info, i);
    switch (kv->key) {
D
dapan 已提交
307 308 309 310 311 312 313 314
      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 已提交
315
        int32_t code = catalogGetHandle(pReq->clusterId, &pCatalog);
D
dapan 已提交
316
        if (code != TSDB_CODE_SUCCESS) {
D
dapan1121 已提交
317
          tscWarn("catalogGetHandle failed, clusterId:%" PRIx64 ", error:%s", pReq->clusterId, tstrerror(code));
D
dapan 已提交
318 319 320
          break;
        }

K
kailixu 已提交
321
        hbProcessUserAuthInfoRsp(kv->value, kv->valueLen, pCatalog, pAppHbMgr);
D
dapan 已提交
322 323
        break;
      }
D
dapan1121 已提交
324 325 326 327 328 329 330
      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 已提交
331

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

        hbProcessDBInfoRsp(kv->value, kv->valueLen, pCatalog);
D
dapan1121 已提交
339
        break;
D
dapan1121 已提交
340
      }
L
fix  
Liu Jicong 已提交
341
      case HEARTBEAT_KEY_STBINFO: {
D
dapan 已提交
342 343 344 345
        if (kv->valueLen <= 0 || NULL == kv->value) {
          tscError("invalid hb stb info, len:%d, value:%p", kv->valueLen, kv->value);
          break;
        }
D
dapan1121 已提交
346

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

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

        hbProcessStbInfoRsp(kv->value, kv->valueLen, pCatalog);
D
dapan1121 已提交
356
        break;
D
dapan 已提交
357
      }
D
dapan1121 已提交
358 359 360 361 362 363
      default:
        tscError("invalid hb key type:%d", kv->key);
        break;
    }
  }

D
dapan1121 已提交
364 365
  taosHashRelease(pAppHbMgr->activeInfo, pReq);

D
dapan1121 已提交
366 367 368
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
369
static int32_t hbAsyncCallBack(void *param, SDataBuf *pMsg, int32_t code) {
D
dapan1121 已提交
370 371 372 373
  if (0 == atomic_load_8(&clientHbMgr.inited)) {
    goto _return;
  }

374
  static int32_t    emptyRspNum = 0;
375
  int32_t           idx = *(int32_t *)param;
D
dapan1121 已提交
376
  SClientHbBatchRsp pRsp = {0};
D
dapan1121 已提交
377 378
  if (TSDB_CODE_SUCCESS == code) {
    tDeserializeSClientHbBatchRsp(pMsg->pData, pMsg->len, &pRsp);
379

dengyihao's avatar
dengyihao 已提交
380 381 382 383 384 385
    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 已提交
386 387
  }

D
dapan1121 已提交
388
  int32_t rspNum = taosArrayGetSize(pRsp.rsps);
D
dapan1121 已提交
389

D
dapan1121 已提交
390
  taosThreadMutexLock(&clientHbMgr.lock);
D
dapan1121 已提交
391

D
dapan1121 已提交
392 393 394 395
  SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, idx);
  if (pAppHbMgr == NULL) {
    taosThreadMutexUnlock(&clientHbMgr.lock);
    tscError("appHbMgr not exist, idx:%d", idx);
D
dapan1121 已提交
396
    taosMemoryFree(pMsg->pData);
dengyihao's avatar
dengyihao 已提交
397
    taosMemoryFree(pMsg->pEpSet);
D
dapan1121 已提交
398
    tFreeClientHbBatchRsp(&pRsp);
D
dapan1121 已提交
399 400 401
    return -1;
  }

D
dapan1121 已提交
402 403
  SAppInstInfo *pInst = pAppHbMgr->pAppInstInfo;

D
dapan1121 已提交
404
  if (code != 0) {
D
dapan1121 已提交
405
    pInst->onlineDnodes = pInst->totalDnodes ? 0 : -1;
406
    tscDebug("hb rsp error %s, update server status %d/%d", tstrerror(code), pInst->onlineDnodes, pInst->totalDnodes);
D
dapan1121 已提交
407 408
  }

D
dapan1121 已提交
409
  if (rspNum) {
L
fix  
Liu Jicong 已提交
410 411
    tscDebug("hb got %d rsp, %d empty rsp received before", rspNum,
             atomic_val_compare_exchange_32(&emptyRspNum, emptyRspNum, 0));
D
dapan1121 已提交
412 413 414 415 416
  } else {
    atomic_add_fetch_32(&emptyRspNum, 1);
  }

  for (int32_t i = 0; i < rspNum; ++i) {
L
fix  
Liu Jicong 已提交
417
    SClientHbRsp *rsp = taosArrayGet(pRsp.rsps, i);
D
dapan1121 已提交
418
    code = (*clientHbMgr.rspHandle[rsp->connKey.connType])(pAppHbMgr, rsp);
D
dapan1121 已提交
419 420 421 422
    if (code) {
      break;
    }
  }
D
dapan1121 已提交
423

D
dapan1121 已提交
424
  taosThreadMutexUnlock(&clientHbMgr.lock);
D
dapan1121 已提交
425

D
dapan1121 已提交
426
  tFreeClientHbBatchRsp(&pRsp);
D
dapan1121 已提交
427 428

_return:
H
Haojun Liao 已提交
429
  taosMemoryFree(pMsg->pData);
dengyihao's avatar
dengyihao 已提交
430
  taosMemoryFree(pMsg->pEpSet);
D
dapan1121 已提交
431
  return code;
L
Liu Jicong 已提交
432 433
}

D
dapan1121 已提交
434
int32_t hbBuildQueryDesc(SQueryHbReqBasic *hbBasic, STscObj *pObj) {
L
Liu Jicong 已提交
435
  int64_t    now = taosGetTimestampUs();
D
dapan1121 已提交
436
  SQueryDesc desc = {0};
L
Liu Jicong 已提交
437
  int32_t    code = 0;
D
dapan1121 已提交
438

L
Liu Jicong 已提交
439
  void *pIter = taosHashIterate(pObj->pRequests, NULL);
D
dapan1121 已提交
440
  while (pIter != NULL) {
L
Liu Jicong 已提交
441
    int64_t     *rid = pIter;
D
dapan1121 已提交
442
    SRequestObj *pRequest = acquireRequest(*rid);
D
dapan1121 已提交
443 444 445 446 447
    if (NULL == pRequest) {
      pIter = taosHashIterate(pObj->pRequests, pIter);
      continue;
    }

448
    if (pRequest->killed || 0 == pRequest->body.queryJob) {
D
dapan1121 已提交
449
      releaseRequest(*rid);
D
dapan1121 已提交
450
      pIter = taosHashIterate(pObj->pRequests, pIter);
D
dapan1121 已提交
451 452 453 454
      continue;
    }

    tstrncpy(desc.sql, pRequest->sqlstr, sizeof(desc.sql));
455
    desc.stime = pRequest->metric.start / 1000;
L
Liu Jicong 已提交
456
    desc.queryId = pRequest->requestId;
D
dapan1121 已提交
457
    desc.useconds = now - pRequest->metric.start;
L
Liu Jicong 已提交
458
    desc.reqRid = pRequest->self;
459
    desc.stableQuery = pRequest->stableQuery;
D
dapan1121 已提交
460
    taosGetFqdn(desc.fqdn);
D
dapan1121 已提交
461
    desc.subPlanNum = pRequest->body.subplanNum;
D
dapan1121 已提交
462 463 464 465 466

    if (desc.subPlanNum) {
      desc.subDesc = taosArrayInit(desc.subPlanNum, sizeof(SQuerySubDesc));
      if (NULL == desc.subDesc) {
        releaseRequest(*rid);
S
Shengliang Guan 已提交
467
        return TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
468 469 470 471 472 473
      }

      code = schedulerGetTasksStatus(pRequest->body.queryJob, desc.subDesc);
      if (code) {
        taosArrayDestroy(desc.subDesc);
        desc.subDesc = NULL;
474
        desc.subPlanNum = 0;
D
dapan1121 已提交
475
      }
D
dapan1121 已提交
476
      desc.subPlanNum = taosArrayGetSize(desc.subDesc);
D
dapan1121 已提交
477 478
    } else {
      desc.subDesc = NULL;
D
dapan1121 已提交
479 480
    }

L
Liu Jicong 已提交
481
    releaseRequest(*rid);
D
dapan1121 已提交
482
    taosArrayPush(hbBasic->queryDesc, &desc);
L
Liu Jicong 已提交
483

D
dapan1121 已提交
484 485 486 487 488 489 490 491 492 493
    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 已提交
494
    return TSDB_CODE_APP_ERROR;
D
dapan1121 已提交
495
  }
L
Liu Jicong 已提交
496

D
dapan1121 已提交
497 498 499 500
  SQueryHbReqBasic *hbBasic = (SQueryHbReqBasic *)taosMemoryCalloc(1, sizeof(SQueryHbReqBasic));
  if (NULL == hbBasic) {
    tscError("calloc %d failed", (int32_t)sizeof(SQueryHbReqBasic));
    releaseTscObj(connKey->tscRid);
S
Shengliang Guan 已提交
501
    return TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
502
  }
503

504 505 506 507 508 509 510 511 512
  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 已提交
513 514 515 516 517 518

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

D
dapan1121 已提交
522 523 524
  int32_t code = hbBuildQueryDesc(hbBasic, pTscObj);
  if (code) {
    releaseTscObj(connKey->tscRid);
D
dapan1121 已提交
525 526 527
    if (hbBasic->queryDesc) {
      taosArrayDestroyEx(hbBasic->queryDesc, tFreeClientHbQueryDesc);
    }
D
dapan1121 已提交
528 529 530 531 532 533 534 535 536 537
    taosMemoryFree(hbBasic);
    return code;
  }

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

  return TSDB_CODE_SUCCESS;
}

K
kailixu 已提交
538 539 540 541 542 543 544 545 546 547 548 549 550
static int32_t hbGetUserAuthInfo(SClientHbKey *connKey, SHbParam *param, SClientHbReq *req) {
  STscObj *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
  if (!pTscObj) {
    tscWarn("tscObj rid %" PRIx64 " not exist", connKey->tscRid);
    return TSDB_CODE_APP_ERROR;
  }

  int32_t code = 0;

  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 已提交
551
    SUserAuthVersion *userAuths = (SUserAuthVersion *)pKv->value;
K
kailixu 已提交
552
    for (int32_t i = 0; i < userNum; ++i) {
K
kailixu 已提交
553 554
      SUserAuthVersion *pUserAuth = userAuths + i;
      // both key and user exist, update version
K
kailixu 已提交
555
      if (strncmp(pUserAuth->user, pTscObj->user, TSDB_USER_LEN) == 0) {
K
kailixu 已提交
556
        pUserAuth->version = htonl(-1);  // force get userAuthInfo
K
kailixu 已提交
557 558 559
        goto _return;
      }
    }
K
kailixu 已提交
560
    // key exists, user not exist, append user
K
kailixu 已提交
561 562 563 564
    SUserAuthVersion *qUserAuth =
        (SUserAuthVersion *)taosMemoryRealloc(pKv->value, (userNum + 1) * sizeof(SUserAuthVersion));
    if (qUserAuth) {
      strncpy((qUserAuth + userNum)->user, pTscObj->user, TSDB_USER_LEN);
K
kailixu 已提交
565
      (qUserAuth + userNum)->version = htonl(-1);  // force get userAuthInfo
K
kailixu 已提交
566 567 568 569 570 571 572 573
      pKv->value = qUserAuth;
      pKv->valueLen += sizeof(SUserAuthVersion);
    } else {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
    goto _return;
  }

K
kailixu 已提交
574
  // key/user not exist, add user
K
kailixu 已提交
575 576 577 578 579 580
  SUserAuthVersion *user = taosMemoryMalloc(sizeof(SUserAuthVersion));
  if (!user) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _return;
  }
  strncpy(user->user, pTscObj->user, TSDB_USER_LEN);
K
kailixu 已提交
581
  user->version = htonl(-1);  // force get userAuthInfo
K
kailixu 已提交
582 583 584 585 586 587 588 589 590 591 592
  kv.valueLen = sizeof(SUserAuthVersion);
  kv.value = user;

  tscDebug("hb got user auth info, valueLen:%d, user:%s, authVer:%d, tscRid:%" PRIi64, kv.valueLen, user->user,
           pTscObj->authVer, connKey->tscRid);

  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 已提交
593
    taosMemoryFree(user);
K
kailixu 已提交
594 595 596 597 598 599 600 601 602 603 604 605 606
    code = terrno ? terrno : TSDB_CODE_APP_ERROR;
    goto _return;
  }

_return:
  releaseTscObj(connKey->tscRid);
  if (code) {
    tscError("hb got user auth info failed since %s", terrstr(code));
  }

  return code;
}

D
dapan 已提交
607 608 609 610 611 612 613 614 615
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;
  }
K
kailixu 已提交
616 617 618 619 620
  STscObj *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
  if (NULL == pTscObj) {
    tscWarn("tscObj rid %" PRIx64 " not exist", connKey->tscRid);
    return TSDB_CODE_APP_ERROR;
  }
D
dapan 已提交
621 622

  if (userNum <= 0) {
D
dapan1121 已提交
623
    taosMemoryFree(users);
K
kailixu 已提交
624
    releaseTscObj(connKey->tscRid);
D
dapan 已提交
625
    return TSDB_CODE_SUCCESS;
K
kailixu 已提交
626 627 628
  } 

  releaseTscObj(connKey->tscRid);
D
dapan 已提交
629 630 631 632 633 634 635 636 637 638 639 640 641 642

  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 已提交
643 644 645
  if (NULL == req->info) {
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
  }
646

D
dapan 已提交
647 648 649 650 651
  taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
652
int32_t hbGetExpiredDBInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
D
dapan1121 已提交
653
  SDbCacheInfo *dbs = NULL;
L
fix  
Liu Jicong 已提交
654 655
  uint32_t      dbNum = 0;
  int32_t       code = 0;
D
dapan1121 已提交
656 657 658 659 660 661

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

D
dapan1121 已提交
662
  if (dbNum <= 0) {
D
dapan1121 已提交
663
    taosMemoryFree(dbs);
D
dapan1121 已提交
664 665 666
    return TSDB_CODE_SUCCESS;
  }

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

D
dapan1121 已提交
672 673
    db->dbId = htobe64(db->dbId);
    db->vgVersion = htonl(db->vgVersion);
D
dapan1121 已提交
674
    db->cfgVersion = htonl(db->cfgVersion);
D
dapan 已提交
675
    db->numOfTable = htonl(db->numOfTable);
D
dapan1121 已提交
676
    db->stateTs = htobe64(db->stateTs);
D
dapan1121 已提交
677 678
  }

L
Liu Jicong 已提交
679 680
  SKv kv = {
      .key = HEARTBEAT_KEY_DBINFO,
D
dapan1121 已提交
681
      .valueLen = sizeof(SDbCacheInfo) * dbNum,
L
Liu Jicong 已提交
682 683
      .value = dbs,
  };
D
dapan1121 已提交
684 685 686

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

D
dapan1121 已提交
687 688 689 690
  if (NULL == req->info) {
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
  }

D
dapan1121 已提交
691 692 693 694 695
  taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));

  return TSDB_CODE_SUCCESS;
}

D
dapan 已提交
696
int32_t hbGetExpiredStbInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
D
dapan1121 已提交
697
  SSTableVersion *stbs = NULL;
698 699
  uint32_t        stbNum = 0;
  int32_t         code = 0;
D
dapan 已提交
700 701 702 703 704 705 706

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

  if (stbNum <= 0) {
D
dapan1121 已提交
707
    taosMemoryFree(stbs);
D
dapan 已提交
708 709 710 711
    return TSDB_CODE_SUCCESS;
  }

  for (int32_t i = 0; i < stbNum; ++i) {
D
dapan1121 已提交
712
    SSTableVersion *stb = &stbs[i];
D
dapan 已提交
713 714
    stb->suid = htobe64(stb->suid);
    stb->sversion = htons(stb->sversion);
L
fix  
Liu Jicong 已提交
715
    stb->tversion = htons(stb->tversion);
D
dapan1121 已提交
716
    stb->smaVer = htonl(stb->smaVer);
D
dapan 已提交
717 718
  }

L
Liu Jicong 已提交
719 720
  SKv kv = {
      .key = HEARTBEAT_KEY_STBINFO,
D
dapan1121 已提交
721
      .valueLen = sizeof(SSTableVersion) * stbNum,
L
Liu Jicong 已提交
722 723
      .value = stbs,
  };
D
dapan 已提交
724 725 726

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

D
dapan1121 已提交
727 728 729 730
  if (NULL == req->info) {
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
  }

D
dapan 已提交
731 732 733 734 735
  taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
736
int32_t hbGetAppInfo(int64_t clusterId, SClientHbReq *req) {
737
  SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
D
dapan1121 已提交
738 739 740 741 742 743
  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;
744
    taosGetAppName(req->app.name, NULL);
D
dapan1121 已提交
745 746 747 748 749
  }

  return TSDB_CODE_SUCCESS;
}

K
kailixu 已提交
750
int32_t hbQueryHbReqHandle(SClientHbKey *connKey, void *param, SClientHbReq *req) {
751 752 753 754 755 756 757 758 759 760
  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 已提交
761
  }
L
fix  
Liu Jicong 已提交
762

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

D
dapan1121 已提交
765
  hbGetQueryBasicInfo(connKey, req);
L
Liu Jicong 已提交
766

K
kailixu 已提交
767 768 769 770 771 772 773
  if (hbParam->reqCnt == 0) {
    if(!tSimpleHashGet(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(hbParam->clusterId))) {
      code = hbGetExpiredUserInfo(connKey, pCatalog, req);
      if (TSDB_CODE_SUCCESS != code) {
        return code;
      }
    }
K
kailixu 已提交
774

K
kailixu 已提交
775
    // invoke after hbGetExpiredUserInfo
K
kailixu 已提交
776
    if (2 != atomic_load_8(&hbParam->pAppHbMgr->connHbFlag)) {
K
kailixu 已提交
777 778 779 780
      code = hbGetUserAuthInfo(connKey, hbParam, req);
      if (TSDB_CODE_SUCCESS != code) {
        return code;
      }
K
kailixu 已提交
781
      atomic_store_8(&hbParam->pAppHbMgr->connHbFlag, 1);
782
    }
D
dapan 已提交
783

784 785 786 787
    code = hbGetExpiredDBInfo(connKey, pCatalog, req);
    if (TSDB_CODE_SUCCESS != code) {
      return code;
    }
D
dapan1121 已提交
788

789 790 791 792
    code = hbGetExpiredStbInfo(connKey, pCatalog, req);
    if (TSDB_CODE_SUCCESS != code) {
      return code;
    }
D
dapan 已提交
793
  }
K
kailixu 已提交
794
  ++hbParam->reqCnt;  // success to get catalog info
K
kailixu 已提交
795

D
dapan1121 已提交
796 797 798
  return TSDB_CODE_SUCCESS;
}

799 800
static FORCE_INLINE void hbMgrInitHandle() {
  // init all handle
D
dapan1121 已提交
801 802
  clientHbMgr.reqHandle[CONN_TYPE__QUERY] = hbQueryHbReqHandle;
  clientHbMgr.reqHandle[CONN_TYPE__TMQ] = hbMqHbReqHandle;
L
Liu Jicong 已提交
803

D
dapan1121 已提交
804 805
  clientHbMgr.rspHandle[CONN_TYPE__QUERY] = hbQueryHbRspHandle;
  clientHbMgr.rspHandle[CONN_TYPE__TMQ] = hbMqHbRspHandle;
L
Liu Jicong 已提交
806 807
}

L
fix  
Liu Jicong 已提交
808
SClientHbBatchReq *hbGatherAllInfo(SAppHbMgr *pAppHbMgr) {
wafwerar's avatar
wafwerar 已提交
809
  SClientHbBatchReq *pBatchReq = taosMemoryCalloc(1, sizeof(SClientHbBatchReq));
L
Liu Jicong 已提交
810
  if (pBatchReq == NULL) {
S
Shengliang Guan 已提交
811
    terrno = TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
812 813
    return NULL;
  }
L
Liu Jicong 已提交
814
  int32_t connKeyCnt = atomic_load_32(&pAppHbMgr->connKeyCnt);
L
Liu Jicong 已提交
815
  pBatchReq->reqs = taosArrayInit(connKeyCnt, sizeof(SClientHbReq));
816
  if (!pBatchReq->reqs) {
dengyihao's avatar
dengyihao 已提交
817 818 819
    tFreeClientHbBatchReq(pBatchReq);
    return NULL;
  }
D
dapan1121 已提交
820

K
kailixu 已提交
821
  void    *pIter = NULL;
K
kailixu 已提交
822
  SHbParam param = {.pAppHbMgr = pAppHbMgr};
K
kailixu 已提交
823 824 825 826 827 828 829 830
  while ((pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter))) {
    SClientHbReq *pOneReq = pIter;
    SClientHbKey *connKey = &pOneReq->connKey;
    STscObj      *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);

    if (!pTscObj) {
      continue;
    }
831

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

K
kailixu 已提交
834
    switch (connKey->connType) {
K
kailixu 已提交
835
      case CONN_TYPE__QUERY: {
836 837 838
        if (param.clusterId == 0) {
          // init
          param.clusterId = pOneReq->clusterId;
K
kailixu 已提交
839
          param.connHbFlag = atomic_load_8(&pAppHbMgr->connHbFlag);
840
        }
K
kailixu 已提交
841 842 843 844 845
        break;
      }
      default:
        break;
    }
K
kailixu 已提交
846 847
    if (clientHbMgr.reqHandle[connKey->connType]) {
      int32_t code = (*clientHbMgr.reqHandle[connKey->connType])(connKey, &param, pOneReq);
K
kailixu 已提交
848 849
      if (code) {
        tscWarn("hbGatherAllInfo failed since %s, tscRid:%" PRIi64 ", connType:%" PRIi8, tstrerror(code),
K
kailixu 已提交
850
                connKey->tscRid, connKey->connType);
K
kailixu 已提交
851 852
      }
    }
K
kailixu 已提交
853

K
kailixu 已提交
854
    releaseTscObj(connKey->tscRid);
L
Liu Jicong 已提交
855
  }
L
Liu Jicong 已提交
856

L
Liu Jicong 已提交
857
  return pBatchReq;
L
Liu Jicong 已提交
858 859
}

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

862
void hbMergeSummary(SAppClusterSummary *dst, SAppClusterSummary *src) {
D
dapan1121 已提交
863 864 865 866 867 868 869 870 871 872 873 874 875
  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};
876
  int       sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
D
dapan1121 已提交
877 878 879 880 881
  if (sz > 0) {
    req.pid = taosGetPId();
    req.appId = clientHbMgr.appId;
    taosGetAppName(req.name, NULL);
  }
D
dapan1121 已提交
882 883

  taosHashClear(clientHbMgr.appSummary);
884

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

889 890
    uint64_t   clusterId = pAppHbMgr->pAppInstInfo->clusterId;
    SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
D
dapan1121 已提交
891 892
    if (NULL == pApp) {
      memcpy(&req.summary, &pAppHbMgr->pAppInstInfo->summary, sizeof(req.summary));
D
dapan1121 已提交
893
      req.startTime = pAppHbMgr->startTime;
D
dapan1121 已提交
894 895 896 897 898
      taosHashPut(clientHbMgr.appSummary, &clusterId, sizeof(clusterId), &req, sizeof(req));
    } else {
      if (pAppHbMgr->startTime < pApp->startTime) {
        pApp->startTime = pAppHbMgr->startTime;
      }
899

D
dapan1121 已提交
900 901 902 903 904 905 906
      hbMergeSummary(&pApp->summary, &pAppHbMgr->pAppInstInfo->summary);
    }
  }

  return TSDB_CODE_SUCCESS;
}

L
fix  
Liu Jicong 已提交
907
static void *hbThreadFunc(void *param) {
L
Liu Jicong 已提交
908
  setThreadName("hb");
wafwerar's avatar
wafwerar 已提交
909
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
910 911 912
  if (taosCheckCurrentInDll()) {
    atexit(hbThreadFuncUnexpectedStopped);
  }
wafwerar's avatar
wafwerar 已提交
913
#endif
L
Liu Jicong 已提交
914
  while (1) {
915
    if (1 == clientHbMgr.threadStop) {
L
Liu Jicong 已提交
916 917 918
      break;
    }

wafwerar's avatar
wafwerar 已提交
919
    taosThreadMutexLock(&clientHbMgr.lock);
920

L
Liu Jicong 已提交
921
    int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
D
dapan1121 已提交
922 923
    if (sz > 0) {
      hbGatherAppInfo();
K
kailixu 已提交
924
      if (sz > 1 && !clientHbMgr.appHbHash) {
K
kailixu 已提交
925
        clientHbMgr.appHbHash = tSimpleHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT));
K
kailixu 已提交
926
      }
K
kailixu 已提交
927
      tSimpleHashClear(clientHbMgr.appHbHash);
D
dapan1121 已提交
928
    }
929

L
fix  
Liu Jicong 已提交
930 931
    for (int i = 0; i < sz; i++) {
      SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
dengyihao's avatar
dengyihao 已提交
932 933 934
      if (pAppHbMgr == NULL) {
        continue;
      }
L
Liu Jicong 已提交
935

L
Liu Jicong 已提交
936 937 938 939
      int32_t connCnt = atomic_load_32(&pAppHbMgr->connKeyCnt);
      if (connCnt == 0) {
        continue;
      }
L
fix  
Liu Jicong 已提交
940
      SClientHbBatchReq *pReq = hbGatherAllInfo(pAppHbMgr);
dengyihao's avatar
dengyihao 已提交
941 942
      if (pReq == NULL || taosArrayGetP(clientHbMgr.appHbMgrs, i) == NULL) {
        tFreeClientHbBatchReq(pReq);
L
Liu Jicong 已提交
943 944
        continue;
      }
L
fix  
Liu Jicong 已提交
945
      int   tlen = tSerializeSClientHbBatchReq(NULL, 0, pReq);
wafwerar's avatar
wafwerar 已提交
946
      void *buf = taosMemoryMalloc(tlen);
L
Liu Jicong 已提交
947
      if (buf == NULL) {
S
Shengliang Guan 已提交
948
        terrno = TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
949
        tFreeClientHbBatchReq(pReq);
dengyihao's avatar
dengyihao 已提交
950
        // hbClearReqInfo(pAppHbMgr);
L
Liu Jicong 已提交
951 952
        break;
      }
L
fix  
Liu Jicong 已提交
953

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

L
Liu Jicong 已提交
957
      if (pInfo == NULL) {
S
Shengliang Guan 已提交
958
        terrno = TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
959
        tFreeClientHbBatchReq(pReq);
dengyihao's avatar
dengyihao 已提交
960
        // hbClearReqInfo(pAppHbMgr);
wafwerar's avatar
wafwerar 已提交
961
        taosMemoryFree(buf);
L
Liu Jicong 已提交
962 963
        break;
      }
L
Liu Jicong 已提交
964
      pInfo->fp = hbAsyncCallBack;
L
Liu Jicong 已提交
965 966 967
      pInfo->msgInfo.pData = buf;
      pInfo->msgInfo.len = tlen;
      pInfo->msgType = TDMT_MND_HEARTBEAT;
D
dapan1121 已提交
968 969
      pInfo->param = taosMemoryMalloc(sizeof(int32_t));
      *(int32_t *)pInfo->param = i;
dengyihao's avatar
dengyihao 已提交
970
      pInfo->paramFreeFp = taosMemoryFree;
L
Liu Jicong 已提交
971 972
      pInfo->requestId = generateRequestId();
      pInfo->requestObjRefId = 0;
L
Liu Jicong 已提交
973 974

      SAppInstInfo *pAppInstInfo = pAppHbMgr->pAppInstInfo;
L
fix  
Liu Jicong 已提交
975 976
      int64_t       transporterId = 0;
      SEpSet        epSet = getEpSet_s(&pAppInstInfo->mgmtEp);
L
Liu Jicong 已提交
977
      asyncSendMsgToServer(pAppInstInfo->pTransporter, &epSet, &transporterId, pInfo);
D
dapan1121 已提交
978
      tFreeClientHbBatchReq(pReq);
dengyihao's avatar
dengyihao 已提交
979
      // hbClearReqInfo(pAppHbMgr);
K
kailixu 已提交
980
      tSimpleHashPut(clientHbMgr.appHbHash, &pAppHbMgr->pAppInstInfo->clusterId, sizeof(uint64_t), NULL, 0);
L
Liu Jicong 已提交
981 982
      atomic_add_fetch_32(&pAppHbMgr->reportCnt, 1);
    }
983

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

L
Liu Jicong 已提交
986
    taosMsleep(HEARTBEAT_INTERVAL);
L
Liu Jicong 已提交
987
  }
K
kailixu 已提交
988

L
Liu Jicong 已提交
989 990 991 992
  return NULL;
}

static int32_t hbCreateThread() {
wafwerar's avatar
wafwerar 已提交
993 994 995
  TdThreadAttr thAttr;
  taosThreadAttrInit(&thAttr);
  taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
L
Liu Jicong 已提交
996

D
dapan1121 已提交
997 998 999
  if (taosThreadCreate(&clientHbMgr.thread, &thAttr, hbThreadFunc, NULL) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
L
Liu Jicong 已提交
1000
  }
D
dapan1121 已提交
1001
  taosThreadAttrDestroy(&thAttr);
L
Liu Jicong 已提交
1002 1003 1004
  return 0;
}

L
Liu Jicong 已提交
1005
static void hbStopThread() {
D
dapan1121 已提交
1006 1007 1008
  if (0 == atomic_load_8(&clientHbMgr.inited)) {
    return;
  }
1009
  if (atomic_val_compare_exchange_8(&clientHbMgr.threadStop, 0, 1)) {
D
dapan1121 已提交
1010
    tscDebug("hb thread already stopped");
1011 1012
    return;
  }
L
fix  
Liu Jicong 已提交
1013

1014 1015 1016 1017 1018 1019
  // thread quit mode kill or inner exit from self-thread
  if (clientHbMgr.quitByKill) {
    taosThreadKill(clientHbMgr.thread, 0);
  } else {
    taosThreadJoin(clientHbMgr.thread, NULL);
  }
D
dapan1121 已提交
1020

L
fix  
Liu Jicong 已提交
1021
  tscDebug("hb thread stopped");
L
Liu Jicong 已提交
1022 1023
}

L
fix  
Liu Jicong 已提交
1024
SAppHbMgr *appHbMgrInit(SAppInstInfo *pAppInstInfo, char *key) {
1025
  if (hbMgrInit() != 0) {
1026 1027 1028
    terrno = TSDB_CODE_TSC_INTERNAL_ERROR;
    return NULL;
  }
wafwerar's avatar
wafwerar 已提交
1029
  SAppHbMgr *pAppHbMgr = taosMemoryMalloc(sizeof(SAppHbMgr));
L
Liu Jicong 已提交
1030 1031 1032 1033 1034 1035
  if (pAppHbMgr == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
  // init stat
  pAppHbMgr->startTime = taosGetTimestampMs();
L
Liu Jicong 已提交
1036
  pAppHbMgr->connKeyCnt = 0;
K
kailixu 已提交
1037
  pAppHbMgr->connHbFlag = 0;
L
Liu Jicong 已提交
1038 1039
  pAppHbMgr->reportCnt = 0;
  pAppHbMgr->reportBytes = 0;
1040
  pAppHbMgr->key = taosStrdup(key);
L
Liu Jicong 已提交
1041

L
Liu Jicong 已提交
1042 1043
  // init app info
  pAppHbMgr->pAppInstInfo = pAppInstInfo;
L
Liu Jicong 已提交
1044 1045 1046

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

  if (pAppHbMgr->activeInfo == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
wafwerar's avatar
wafwerar 已提交
1050
    taosMemoryFree(pAppHbMgr);
L
Liu Jicong 已提交
1051 1052
    return NULL;
  }
H
Haojun Liao 已提交
1053

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

wafwerar's avatar
wafwerar 已提交
1056
  taosThreadMutexLock(&clientHbMgr.lock);
L
Liu Jicong 已提交
1057
  taosArrayPush(clientHbMgr.appHbMgrs, &pAppHbMgr);
D
dapan1121 已提交
1058
  pAppHbMgr->idx = taosArrayGetSize(clientHbMgr.appHbMgrs) - 1;
wafwerar's avatar
wafwerar 已提交
1059
  taosThreadMutexUnlock(&clientHbMgr.lock);
L
fix  
Liu Jicong 已提交
1060

L
Liu Jicong 已提交
1061 1062 1063
  return pAppHbMgr;
}

D
dapan1121 已提交
1064 1065 1066 1067 1068 1069 1070 1071 1072
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;
1073

D
dapan1121 已提交
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
  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 已提交
1086
      taosArraySet(clientHbMgr.appHbMgrs, i, pAppHbMgr);
D
dapan1121 已提交
1087 1088 1089 1090 1091 1092
      break;
    }
  }
  taosThreadMutexUnlock(&clientHbMgr.lock);
}

D
dapan1121 已提交
1093
void appHbMgrCleanup(void) {
L
Liu Jicong 已提交
1094 1095
  int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
  for (int i = 0; i < sz; i++) {
L
fix  
Liu Jicong 已提交
1096
    SAppHbMgr *pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i);
dengyihao's avatar
dengyihao 已提交
1097
    if (pTarget == NULL) continue;
D
dapan1121 已提交
1098
    hbFreeAppHbMgr(pTarget);
L
Liu Jicong 已提交
1099
  }
K
kailixu 已提交
1100 1101 1102 1103 1104
  clientHbMgr.appHbMgrs = taosArrayDestroy(clientHbMgr.appHbMgrs);
  tSimpleHashCleanup(clientHbMgr.appHbHash);
  clientHbMgr.appHbHash = NULL;
  taosHashCleanup(clientHbMgr.appSummary);
  clientHbMgr.appSummary = NULL;
L
Liu Jicong 已提交
1105 1106 1107
}

int hbMgrInit() {
L
Liu Jicong 已提交
1108 1109 1110 1111
  // init once
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 0, 1);
  if (old == 1) return 0;

D
dapan1121 已提交
1112 1113
  clientHbMgr.appId = tGenIdPI64();
  tscDebug("app %" PRIx64 " initialized", clientHbMgr.appId);
1114

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

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

dengyihao's avatar
dengyihao 已提交
1120
  int ret = taosThreadMutexAttrInit(&attr);
1121 1122
  if (ret != 0) {
    uError("hbMgrInit:taosThreadMutexAttrInit error") return ret;
1123
  }
dengyihao's avatar
dengyihao 已提交
1124

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

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

  ret = taosThreadMutexAttrDestroy(&attr);
1136 1137
  if (ret != 0) {
    uError("hbMgrInit:taosThreadMutexAttrDestroy error") return ret;
1138
  }
L
Liu Jicong 已提交
1139 1140 1141 1142 1143

  // init handle funcs
  hbMgrInitHandle();

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

L
Liu Jicong 已提交
1146 1147 1148 1149
  return 0;
}

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

L
Liu Jicong 已提交
1152
  // destroy all appHbMgr
L
Liu Jicong 已提交
1153 1154 1155
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 1, 0);
  if (old == 0) return;

wafwerar's avatar
wafwerar 已提交
1156
  taosThreadMutexLock(&clientHbMgr.lock);
D
dapan1121 已提交
1157
  appHbMgrCleanup();
wafwerar's avatar
wafwerar 已提交
1158
  taosThreadMutexUnlock(&clientHbMgr.lock);
L
Liu Jicong 已提交
1159 1160
}

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

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

L
Liu Jicong 已提交
1174
  atomic_add_fetch_32(&pAppHbMgr->connKeyCnt, 1);
L
Liu Jicong 已提交
1175 1176 1177
  return 0;
}

D
dapan1121 已提交
1178
int hbRegisterConn(SAppHbMgr *pAppHbMgr, int64_t tscRefId, int64_t clusterId, int8_t connType) {
L
Liu Jicong 已提交
1179
  SClientHbKey connKey = {
D
dapan1121 已提交
1180
      .tscRid = tscRefId,
D
dapan1121 已提交
1181
      .connType = connType,
L
Liu Jicong 已提交
1182
  };
D
dapan1121 已提交
1183

D
dapan1121 已提交
1184 1185
  switch (connType) {
    case CONN_TYPE__QUERY: {
D
dapan1121 已提交
1186
      return hbRegisterConnImpl(pAppHbMgr, connKey, clusterId);
D
dapan1121 已提交
1187
    }
D
dapan1121 已提交
1188
    case CONN_TYPE__TMQ: {
L
Liu Jicong 已提交
1189
      return 0;
D
dapan1121 已提交
1190 1191
    }
    default:
L
Liu Jicong 已提交
1192
      return 0;
D
dapan1121 已提交
1193 1194 1195
  }
}

K
kailixu 已提交
1196 1197
void hbDeregisterConn(STscObj *pTscObj, SClientHbKey connKey) {
  SAppHbMgr    *pAppHbMgr = pTscObj->pAppInfo->pAppHbMgr;
D
dapan1121 已提交
1198
  SClientHbReq *pReq = taosHashAcquire(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
D
dapan1121 已提交
1199
  if (pReq) {
D
dapan1121 已提交
1200
    tFreeClientHbReq(pReq);
D
dapan1121 已提交
1201
    taosHashRemove(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
D
dapan1121 已提交
1202
    taosHashRelease(pAppHbMgr->activeInfo, pReq);
D
dapan1121 已提交
1203 1204
  }

D
dapan1121 已提交
1205
  if (NULL == pReq) {
D
dapan1121 已提交
1206 1207
    return;
  }
L
Liu Jicong 已提交
1208

L
Liu Jicong 已提交
1209
  atomic_sub_fetch_32(&pAppHbMgr->connKeyCnt, 1);
L
Liu Jicong 已提交
1210
}
1211 1212 1213 1214 1215

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