clientHb.c 22.0 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 22 23 24 25 26

static SClientHbMgr clientHbMgr = {0};

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

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

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

D
dapan1121 已提交
31 32 33
static int32_t hbProcessDBInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
  int32_t code = 0;

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

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

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

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

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

    if (code) {
      return code;
    }
  }

S
Shengliang Guan 已提交
74
  tFreeSUseDbBatchRsp(&batchUseRsp);
D
dapan1121 已提交
75 76 77
  return TSDB_CODE_SUCCESS;
}

D
dapan 已提交
78 79 80
static int32_t hbProcessStbInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
  int32_t code = 0;

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

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

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

D
dapan1121 已提交
102
      catalogUpdateSTableMeta(pCatalog, rsp);
D
dapan 已提交
103 104 105
    }
  }

S
Shengliang Guan 已提交
106
  tFreeSTableMetaBatchRsp(&batchMetaRsp);
D
dapan 已提交
107 108 109
  return TSDB_CODE_SUCCESS;
}

L
Liu Jicong 已提交
110
static int32_t hbQueryHbRspHandle(SAppHbMgr *pAppHbMgr, SClientHbRsp *pRsp) {
L
fix  
Liu Jicong 已提交
111
  SHbConnInfo *info = taosHashGet(pAppHbMgr->connInfo, &pRsp->connKey, sizeof(SClientHbKey));
D
dapan1121 已提交
112
  if (NULL == info) {
L
Liu Jicong 已提交
113 114
    tscWarn("fail to get connInfo, may be dropped, refId:%" PRIx64 ", type:%d", pRsp->connKey.tscRid,
            pRsp->connKey.connType);
D
dapan1121 已提交
115 116 117
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
118 119 120 121 122 123 124
  if (pRsp->query) {
    STscObj *pTscObj = (STscObj *)acquireTscObj(pRsp->connKey.tscRid);
    if (NULL == pTscObj) {
      tscDebug("tscObj rid %" PRIx64 " not exist", pRsp->connKey.tscRid);
    } else {
      updateEpSet_s(&pTscObj->pAppInfo->mgmtEp, &pRsp->query->epSet);
      pTscObj->connId = pRsp->query->connId;
L
Liu Jicong 已提交
125

D
dapan1121 已提交
126 127 128 129 130 131 132 133 134
      if (pRsp->query->killRid) {
        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 已提交
135

D
dapan1121 已提交
136 137 138 139 140 141 142
      if (pRsp->query->killConnection) {
        taos_close(pTscObj);
      }

      releaseTscObj(pRsp->connKey.tscRid);
    }
  }
L
Liu Jicong 已提交
143

D
dapan1121 已提交
144
  int32_t kvNum = pRsp->info ? taosArrayGetSize(pRsp->info) : 0;
D
dapan1121 已提交
145 146

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

D
dapan1121 已提交
148 149 150
  for (int32_t i = 0; i < kvNum; ++i) {
    SKv *kv = taosArrayGet(pRsp->info, i);
    switch (kv->key) {
D
dapan1121 已提交
151 152 153 154 155 156
      case HEARTBEAT_KEY_DBINFO: {
        if (kv->valueLen <= 0 || NULL == kv->value) {
          tscError("invalid hb db info, len:%d, value:%p", kv->valueLen, kv->value);
          break;
        }

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

D
dapan1121 已提交
160 161
        int32_t code = catalogGetHandle(*clusterId, &pCatalog);
        if (code != TSDB_CODE_SUCCESS) {
L
fix  
Liu Jicong 已提交
162
          tscWarn("catalogGetHandle failed, clusterId:%" PRIx64 ", error:%s", *clusterId, tstrerror(code));
D
dapan1121 已提交
163 164 165 166
          break;
        }

        hbProcessDBInfoRsp(kv->value, kv->valueLen, pCatalog);
D
dapan1121 已提交
167
        break;
D
dapan1121 已提交
168
      }
L
fix  
Liu Jicong 已提交
169
      case HEARTBEAT_KEY_STBINFO: {
D
dapan 已提交
170 171 172 173
        if (kv->valueLen <= 0 || NULL == kv->value) {
          tscError("invalid hb stb info, len:%d, value:%p", kv->valueLen, kv->value);
          break;
        }
D
dapan1121 已提交
174

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

D
dapan 已提交
178 179
        int32_t code = catalogGetHandle(*clusterId, &pCatalog);
        if (code != TSDB_CODE_SUCCESS) {
L
fix  
Liu Jicong 已提交
180
          tscWarn("catalogGetHandle failed, clusterId:%" PRIx64 ", error:%s", *clusterId, tstrerror(code));
D
dapan 已提交
181 182 183 184
          break;
        }

        hbProcessStbInfoRsp(kv->value, kv->valueLen, pCatalog);
D
dapan1121 已提交
185
        break;
D
dapan 已提交
186
      }
D
dapan1121 已提交
187 188 189 190 191 192 193 194 195
      default:
        tscError("invalid hb key type:%d", kv->key);
        break;
    }
  }

  return TSDB_CODE_SUCCESS;
}

L
Liu Jicong 已提交
196
static int32_t hbAsyncCallBack(void *param, const SDataBuf *pMsg, int32_t code) {
D
dapan1121 已提交
197
  static int32_t emptyRspNum = 0;
L
Liu Jicong 已提交
198
  if (code != 0) {
wafwerar's avatar
wafwerar 已提交
199
    taosMemoryFreeClear(param);
L
Liu Jicong 已提交
200 201
    return -1;
  }
S
Shengliang Guan 已提交
202

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

D
dapan1121 已提交
207
  int32_t rspNum = taosArrayGetSize(pRsp.rsps);
D
dapan1121 已提交
208

L
fix  
Liu Jicong 已提交
209
  SAppInstInfo **pInst = taosHashGet(appInfo.pInstMap, key, strlen(key));
D
dapan1121 已提交
210
  if (pInst == NULL || NULL == *pInst) {
L
fix  
Liu Jicong 已提交
211
    tscError("cluster not exist, key:%s", key);
wafwerar's avatar
wafwerar 已提交
212
    taosMemoryFreeClear(param);
D
dapan1121 已提交
213
    tFreeClientHbBatchRsp(&pRsp);
D
dapan1121 已提交
214 215 216
    return -1;
  }

wafwerar's avatar
wafwerar 已提交
217
  taosMemoryFreeClear(param);
D
dapan1121 已提交
218 219

  if (rspNum) {
L
fix  
Liu Jicong 已提交
220 221
    tscDebug("hb got %d rsp, %d empty rsp received before", rspNum,
             atomic_val_compare_exchange_32(&emptyRspNum, emptyRspNum, 0));
D
dapan1121 已提交
222 223 224 225 226
  } else {
    atomic_add_fetch_32(&emptyRspNum, 1);
  }

  for (int32_t i = 0; i < rspNum; ++i) {
L
fix  
Liu Jicong 已提交
227
    SClientHbRsp *rsp = taosArrayGet(pRsp.rsps, i);
D
dapan1121 已提交
228
    code = (*clientHbMgr.rspHandle[rsp->connKey.connType])((*pInst)->pAppHbMgr, rsp);
D
dapan1121 已提交
229 230 231 232
    if (code) {
      break;
    }
  }
D
dapan1121 已提交
233 234

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

D
dapan1121 已提交
236
  return code;
L
Liu Jicong 已提交
237 238
}

D
dapan1121 已提交
239
int32_t hbBuildQueryDesc(SQueryHbReqBasic *hbBasic, STscObj *pObj) {
L
Liu Jicong 已提交
240
  int64_t    now = taosGetTimestampUs();
D
dapan1121 已提交
241
  SQueryDesc desc = {0};
L
Liu Jicong 已提交
242
  int32_t    code = 0;
D
dapan1121 已提交
243

L
Liu Jicong 已提交
244
  void *pIter = taosHashIterate(pObj->pRequests, NULL);
D
dapan1121 已提交
245
  while (pIter != NULL) {
L
Liu Jicong 已提交
246
    int64_t     *rid = pIter;
D
dapan1121 已提交
247 248 249 250 251 252
    SRequestObj *pRequest = acquireRequest(*rid);
    if (NULL == pRequest) {
      continue;
    }

    tstrncpy(desc.sql, pRequest->sqlstr, sizeof(desc.sql));
L
Liu Jicong 已提交
253 254
    desc.stime = pRequest->metric.start;
    desc.queryId = pRequest->requestId;
D
dapan1121 已提交
255
    desc.useconds = now - pRequest->metric.start;
L
Liu Jicong 已提交
256 257
    desc.reqRid = pRequest->self;
    desc.pid = hbBasic->pid;
D
dapan1121 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
    taosGetFqdn(desc.fqdn);
    desc.subPlanNum = pRequest->body.pDag ? pRequest->body.pDag->numOfSubplans : 0;

    if (desc.subPlanNum) {
      desc.subDesc = taosArrayInit(desc.subPlanNum, sizeof(SQuerySubDesc));
      if (NULL == desc.subDesc) {
        releaseRequest(*rid);
        return TSDB_CODE_QRY_OUT_OF_MEMORY;
      }

      code = schedulerGetTasksStatus(pRequest->body.queryJob, desc.subDesc);
      if (code) {
        taosArrayDestroy(desc.subDesc);
        desc.subDesc = NULL;
      }
    }

L
Liu Jicong 已提交
275
    releaseRequest(*rid);
D
dapan1121 已提交
276
    taosArrayPush(hbBasic->queryDesc, &desc);
L
Liu Jicong 已提交
277

D
dapan1121 已提交
278 279 280 281 282 283 284 285 286 287 288 289
    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);
    return TSDB_CODE_QRY_APP_ERROR;
  }
L
Liu Jicong 已提交
290

D
dapan1121 已提交
291 292 293 294 295 296
  int32_t numOfQueries = pTscObj->pRequests ? taosHashGetSize(pTscObj->pRequests) : 0;
  if (numOfQueries <= 0) {
    releaseTscObj(connKey->tscRid);
    tscDebug("no queries on connection");
    return TSDB_CODE_QRY_APP_ERROR;
  }
L
Liu Jicong 已提交
297

D
dapan1121 已提交
298 299 300 301 302 303 304 305 306 307 308 309 310 311
  SQueryHbReqBasic *hbBasic = (SQueryHbReqBasic *)taosMemoryCalloc(1, sizeof(SQueryHbReqBasic));
  if (NULL == hbBasic) {
    tscError("calloc %d failed", (int32_t)sizeof(SQueryHbReqBasic));
    releaseTscObj(connKey->tscRid);
    return TSDB_CODE_QRY_OUT_OF_MEMORY;
  }

  hbBasic->queryDesc = taosArrayInit(numOfQueries, sizeof(SQueryDesc));
  if (NULL == hbBasic->queryDesc) {
    tscWarn("taosArrayInit %d queryDesc failed", numOfQueries);
    releaseTscObj(connKey->tscRid);
    taosMemoryFree(hbBasic);
    return TSDB_CODE_QRY_OUT_OF_MEMORY;
  }
L
Liu Jicong 已提交
312

D
dapan1121 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
  hbBasic->connId = pTscObj->connId;
  hbBasic->pid = taosGetPId();
  taosGetAppName(hbBasic->app, NULL);

  int32_t code = hbBuildQueryDesc(hbBasic, pTscObj);
  if (code) {
    releaseTscObj(connKey->tscRid);
    taosMemoryFree(hbBasic);
    return code;
  }

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

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
330 331
int32_t hbGetExpiredDBInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
  SDbVgVersion *dbs = NULL;
L
fix  
Liu Jicong 已提交
332 333
  uint32_t      dbNum = 0;
  int32_t       code = 0;
D
dapan1121 已提交
334 335 336 337 338 339

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

D
dapan1121 已提交
340 341 342 343
  if (dbNum <= 0) {
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
344 345 346 347
  for (int32_t i = 0; i < dbNum; ++i) {
    SDbVgVersion *db = &dbs[i];
    db->dbId = htobe64(db->dbId);
    db->vgVersion = htonl(db->vgVersion);
D
dapan 已提交
348
    db->numOfTable = htonl(db->numOfTable);
D
dapan1121 已提交
349 350
  }

L
Liu Jicong 已提交
351 352 353 354 355
  SKv kv = {
      .key = HEARTBEAT_KEY_DBINFO,
      .valueLen = sizeof(SDbVgVersion) * dbNum,
      .value = dbs,
  };
D
dapan1121 已提交
356 357 358

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

D
dapan1121 已提交
359 360 361 362 363
  taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));

  return TSDB_CODE_SUCCESS;
}

D
dapan 已提交
364 365
int32_t hbGetExpiredStbInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
  SSTableMetaVersion *stbs = NULL;
L
fix  
Liu Jicong 已提交
366 367
  uint32_t            stbNum = 0;
  int32_t             code = 0;
D
dapan 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381

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

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

  for (int32_t i = 0; i < stbNum; ++i) {
    SSTableMetaVersion *stb = &stbs[i];
    stb->suid = htobe64(stb->suid);
    stb->sversion = htons(stb->sversion);
L
fix  
Liu Jicong 已提交
382
    stb->tversion = htons(stb->tversion);
D
dapan 已提交
383 384
  }

L
Liu Jicong 已提交
385 386 387 388 389
  SKv kv = {
      .key = HEARTBEAT_KEY_STBINFO,
      .valueLen = sizeof(SSTableMetaVersion) * stbNum,
      .value = stbs,
  };
D
dapan 已提交
390 391 392 393 394 395 396 397

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

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

  return TSDB_CODE_SUCCESS;
}

L
fix  
Liu Jicong 已提交
398 399
int32_t hbQueryHbReqHandle(SClientHbKey *connKey, void *param, SClientHbReq *req) {
  int64_t         *clusterId = (int64_t *)param;
D
dapan1121 已提交
400 401 402 403
  struct SCatalog *pCatalog = NULL;

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

D
dapan1121 已提交
408
  hbGetQueryBasicInfo(connKey, req);
L
Liu Jicong 已提交
409

D
dapan1121 已提交
410 411 412 413 414
  code = hbGetExpiredDBInfo(connKey, pCatalog, req);
  if (TSDB_CODE_SUCCESS != code) {
    return code;
  }

D
dapan 已提交
415 416 417 418 419
  code = hbGetExpiredStbInfo(connKey, pCatalog, req);
  if (TSDB_CODE_SUCCESS != code) {
    return code;
  }

D
dapan1121 已提交
420 421 422 423
  return TSDB_CODE_SUCCESS;
}

void hbMgrInitMqHbHandle() {
D
dapan1121 已提交
424 425
  clientHbMgr.reqHandle[CONN_TYPE__QUERY] = hbQueryHbReqHandle;
  clientHbMgr.reqHandle[CONN_TYPE__TMQ] = hbMqHbReqHandle;
L
Liu Jicong 已提交
426

D
dapan1121 已提交
427 428
  clientHbMgr.rspHandle[CONN_TYPE__QUERY] = hbQueryHbRspHandle;
  clientHbMgr.rspHandle[CONN_TYPE__TMQ] = hbMqHbRspHandle;
L
Liu Jicong 已提交
429 430
}

L
Liu Jicong 已提交
431 432
static FORCE_INLINE void hbMgrInitHandle() {
  // init all handle
D
dapan1121 已提交
433
  hbMgrInitMqHbHandle();
L
Liu Jicong 已提交
434 435
}

D
dapan1121 已提交
436 437
void hbFreeReq(void *req) {
  SClientHbReq *pReq = (SClientHbReq *)req;
D
dapan1121 已提交
438
  tFreeReqKvHash(pReq->info);
D
dapan1121 已提交
439 440
}

D
dapan1121 已提交
441 442 443 444 445
void hbClearClientHbReq(SClientHbReq *pReq) {
  pReq->query = NULL;
  pReq->info = NULL;
}

L
fix  
Liu Jicong 已提交
446
SClientHbBatchReq *hbGatherAllInfo(SAppHbMgr *pAppHbMgr) {
wafwerar's avatar
wafwerar 已提交
447
  SClientHbBatchReq *pBatchReq = taosMemoryCalloc(1, sizeof(SClientHbBatchReq));
L
Liu Jicong 已提交
448
  if (pBatchReq == NULL) {
L
Liu Jicong 已提交
449 450 451
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    return NULL;
  }
L
Liu Jicong 已提交
452
  int32_t connKeyCnt = atomic_load_32(&pAppHbMgr->connKeyCnt);
L
Liu Jicong 已提交
453
  pBatchReq->reqs = taosArrayInit(connKeyCnt, sizeof(SClientHbReq));
L
Liu Jicong 已提交
454

D
dapan1121 已提交
455
  int32_t code = 0;
L
fix  
Liu Jicong 已提交
456
  void   *pIter = taosHashIterate(pAppHbMgr->activeInfo, NULL);
L
Liu Jicong 已提交
457
  while (pIter != NULL) {
L
fix  
Liu Jicong 已提交
458
    SClientHbReq *pOneReq = pIter;
D
dapan1121 已提交
459

L
fix  
Liu Jicong 已提交
460
    SHbConnInfo *info = taosHashGet(pAppHbMgr->connInfo, &pOneReq->connKey, sizeof(SClientHbKey));
D
dapan1121 已提交
461
    if (info) {
D
dapan1121 已提交
462
      code = (*clientHbMgr.reqHandle[pOneReq->connKey.connType])(&pOneReq->connKey, info->param, pOneReq);
D
dapan1121 已提交
463
      if (code) {
D
dapan1121 已提交
464 465
        pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter);
        continue;
D
dapan1121 已提交
466 467 468
      }
    }

L
Liu Jicong 已提交
469
    taosArrayPush(pBatchReq->reqs, pOneReq);
D
dapan1121 已提交
470
    hbClearClientHbReq(pOneReq);
L
Liu Jicong 已提交
471

L
Liu Jicong 已提交
472
    pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter);
L
Liu Jicong 已提交
473 474
  }

L
Liu Jicong 已提交
475 476 477 478
  //  if (code) {
  //    taosArrayDestroyEx(pBatchReq->reqs, hbFreeReq);
  //    taosMemoryFreeClear(pBatchReq);
  //  }
L
Liu Jicong 已提交
479

L
Liu Jicong 已提交
480
  return pBatchReq;
L
Liu Jicong 已提交
481 482
}

D
dapan1121 已提交
483 484 485
void hbClearReqInfo(SAppHbMgr *pAppHbMgr) {
  void *pIter = taosHashIterate(pAppHbMgr->activeInfo, NULL);
  while (pIter != NULL) {
L
fix  
Liu Jicong 已提交
486
    SClientHbReq *pOneReq = pIter;
D
dapan1121 已提交
487

D
dapan1121 已提交
488
    tFreeReqKvHash(pOneReq->info);
D
dapan1121 已提交
489 490 491 492 493 494
    taosHashClear(pOneReq->info);

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

L
fix  
Liu Jicong 已提交
495
static void *hbThreadFunc(void *param) {
L
Liu Jicong 已提交
496 497
  setThreadName("hb");
  while (1) {
498
    int8_t threadStop = atomic_val_compare_exchange_8(&clientHbMgr.threadStop, 1, 2);
L
fix  
Liu Jicong 已提交
499
    if (1 == threadStop) {
L
Liu Jicong 已提交
500 501 502
      break;
    }

wafwerar's avatar
wafwerar 已提交
503
    taosThreadMutexLock(&clientHbMgr.lock);
504

L
Liu Jicong 已提交
505
    int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
L
fix  
Liu Jicong 已提交
506 507
    for (int i = 0; i < sz; i++) {
      SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
L
Liu Jicong 已提交
508

L
Liu Jicong 已提交
509 510 511 512
      int32_t connCnt = atomic_load_32(&pAppHbMgr->connKeyCnt);
      if (connCnt == 0) {
        continue;
      }
L
fix  
Liu Jicong 已提交
513
      SClientHbBatchReq *pReq = hbGatherAllInfo(pAppHbMgr);
L
Liu Jicong 已提交
514 515 516
      if (pReq == NULL) {
        continue;
      }
L
fix  
Liu Jicong 已提交
517
      int   tlen = tSerializeSClientHbBatchReq(NULL, 0, pReq);
wafwerar's avatar
wafwerar 已提交
518
      void *buf = taosMemoryMalloc(tlen);
L
Liu Jicong 已提交
519
      if (buf == NULL) {
D
dapan1121 已提交
520 521 522
        terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
        tFreeClientHbBatchReq(pReq, false);
        hbClearReqInfo(pAppHbMgr);
L
Liu Jicong 已提交
523 524
        break;
      }
L
fix  
Liu Jicong 已提交
525

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

L
Liu Jicong 已提交
529 530
      if (pInfo == NULL) {
        terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
L
Liu Jicong 已提交
531
        tFreeClientHbBatchReq(pReq, false);
D
dapan1121 已提交
532
        hbClearReqInfo(pAppHbMgr);
wafwerar's avatar
wafwerar 已提交
533
        taosMemoryFree(buf);
L
Liu Jicong 已提交
534 535
        break;
      }
L
Liu Jicong 已提交
536
      pInfo->fp = hbAsyncCallBack;
L
Liu Jicong 已提交
537 538 539
      pInfo->msgInfo.pData = buf;
      pInfo->msgInfo.len = tlen;
      pInfo->msgType = TDMT_MND_HEARTBEAT;
D
dapan1121 已提交
540
      pInfo->param = strdup(pAppHbMgr->key);
L
Liu Jicong 已提交
541 542
      pInfo->requestId = generateRequestId();
      pInfo->requestObjRefId = 0;
L
Liu Jicong 已提交
543 544

      SAppInstInfo *pAppInstInfo = pAppHbMgr->pAppInstInfo;
L
fix  
Liu Jicong 已提交
545 546
      int64_t       transporterId = 0;
      SEpSet        epSet = getEpSet_s(&pAppInstInfo->mgmtEp);
L
Liu Jicong 已提交
547
      asyncSendMsgToServer(pAppInstInfo->pTransporter, &epSet, &transporterId, pInfo);
L
fix  
Liu Jicong 已提交
548
      tFreeClientHbBatchReq(pReq, false);
D
dapan1121 已提交
549
      hbClearReqInfo(pAppHbMgr);
L
Liu Jicong 已提交
550 551 552

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

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

L
Liu Jicong 已提交
556
    taosMsleep(HEARTBEAT_INTERVAL);
L
Liu Jicong 已提交
557 558 559 560 561
  }
  return NULL;
}

static int32_t hbCreateThread() {
wafwerar's avatar
wafwerar 已提交
562 563 564
  TdThreadAttr thAttr;
  taosThreadAttrInit(&thAttr);
  taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
L
Liu Jicong 已提交
565

D
dapan1121 已提交
566 567 568
  if (taosThreadCreate(&clientHbMgr.thread, &thAttr, hbThreadFunc, NULL) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
L
Liu Jicong 已提交
569
  }
D
dapan1121 已提交
570
  taosThreadAttrDestroy(&thAttr);
L
Liu Jicong 已提交
571 572 573
  return 0;
}

L
Liu Jicong 已提交
574
static void hbStopThread() {
575
  if (atomic_val_compare_exchange_8(&clientHbMgr.threadStop, 0, 1)) {
D
dapan1121 已提交
576
    tscDebug("hb thread already stopped");
577 578
    return;
  }
L
fix  
Liu Jicong 已提交
579

580
  while (2 != atomic_load_8(&clientHbMgr.threadStop)) {
wafwerar's avatar
wafwerar 已提交
581
    taosUsleep(10);
582
  }
D
dapan1121 已提交
583

L
fix  
Liu Jicong 已提交
584
  tscDebug("hb thread stopped");
L
Liu Jicong 已提交
585 586
}

L
fix  
Liu Jicong 已提交
587
SAppHbMgr *appHbMgrInit(SAppInstInfo *pAppInstInfo, char *key) {
L
Liu Jicong 已提交
588
  hbMgrInit();
wafwerar's avatar
wafwerar 已提交
589
  SAppHbMgr *pAppHbMgr = taosMemoryMalloc(sizeof(SAppHbMgr));
L
Liu Jicong 已提交
590 591 592 593 594 595
  if (pAppHbMgr == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
  // init stat
  pAppHbMgr->startTime = taosGetTimestampMs();
L
Liu Jicong 已提交
596 597 598
  pAppHbMgr->connKeyCnt = 0;
  pAppHbMgr->reportCnt = 0;
  pAppHbMgr->reportBytes = 0;
D
dapan1121 已提交
599
  pAppHbMgr->key = strdup(key);
L
Liu Jicong 已提交
600

L
Liu Jicong 已提交
601 602
  // init app info
  pAppHbMgr->pAppInstInfo = pAppInstInfo;
L
Liu Jicong 已提交
603 604 605

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

  if (pAppHbMgr->activeInfo == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
wafwerar's avatar
wafwerar 已提交
609
    taosMemoryFree(pAppHbMgr);
L
Liu Jicong 已提交
610 611
    return NULL;
  }
H
Haojun Liao 已提交
612 613

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

D
dapan1121 已提交
617
  if (pAppHbMgr->connInfo == NULL) {
L
Liu Jicong 已提交
618
    terrno = TSDB_CODE_OUT_OF_MEMORY;
wafwerar's avatar
wafwerar 已提交
619
    taosMemoryFree(pAppHbMgr);
L
Liu Jicong 已提交
620 621 622
    return NULL;
  }

wafwerar's avatar
wafwerar 已提交
623
  taosThreadMutexLock(&clientHbMgr.lock);
L
Liu Jicong 已提交
624
  taosArrayPush(clientHbMgr.appHbMgrs, &pAppHbMgr);
wafwerar's avatar
wafwerar 已提交
625
  taosThreadMutexUnlock(&clientHbMgr.lock);
L
fix  
Liu Jicong 已提交
626

L
Liu Jicong 已提交
627 628 629
  return pAppHbMgr;
}

D
dapan1121 已提交
630
void appHbMgrCleanup(void) {
L
Liu Jicong 已提交
631 632
  int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
  for (int i = 0; i < sz; i++) {
L
fix  
Liu Jicong 已提交
633
    SAppHbMgr *pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i);
L
Liu Jicong 已提交
634 635

    void *pIter = taosHashIterate(pTarget->activeInfo, NULL);
D
dapan1121 已提交
636 637 638 639 640
    while (pIter != NULL) {
      SClientHbReq *pOneReq = pIter;
      hbFreeReq(pOneReq);
      taosHashCleanup(pOneReq->info);
      pIter = taosHashIterate(pTarget->activeInfo, pIter);
L
Liu Jicong 已提交
641
    }
D
dapan1121 已提交
642 643
    taosHashCleanup(pTarget->activeInfo);
    pTarget->activeInfo = NULL;
D
dapan1121 已提交
644 645 646 647 648 649

    pIter = taosHashIterate(pTarget->connInfo, NULL);
    while (pIter != NULL) {
      SHbConnInfo *info = pIter;
      taosMemoryFree(info->param);
      pIter = taosHashIterate(pTarget->connInfo, pIter);
L
Liu Jicong 已提交
650
    }
D
dapan1121 已提交
651 652
    taosHashCleanup(pTarget->connInfo);
    pTarget->connInfo = NULL;
D
dapan1121 已提交
653 654 655

    taosMemoryFree(pTarget->key);
    taosMemoryFree(pTarget);
L
Liu Jicong 已提交
656 657 658 659
  }
}

int hbMgrInit() {
L
Liu Jicong 已提交
660 661 662 663
  // init once
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 0, 1);
  if (old == 1) return 0;

L
fix  
Liu Jicong 已提交
664
  clientHbMgr.appHbMgrs = taosArrayInit(0, sizeof(void *));
wafwerar's avatar
wafwerar 已提交
665
  taosThreadMutexInit(&clientHbMgr.lock, NULL);
L
Liu Jicong 已提交
666 667 668 669 670

  // init handle funcs
  hbMgrInitHandle();

  // init backgroud thread
L
Liu Jicong 已提交
671
  hbCreateThread();
L
Liu Jicong 已提交
672

L
Liu Jicong 已提交
673 674 675 676
  return 0;
}

void hbMgrCleanUp() {
L
Liu Jicong 已提交
677
  // hbStopThread();
L
fix  
Liu Jicong 已提交
678

L
Liu Jicong 已提交
679
  // destroy all appHbMgr
L
Liu Jicong 已提交
680 681 682
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 1, 0);
  if (old == 0) return;

wafwerar's avatar
wafwerar 已提交
683
  taosThreadMutexLock(&clientHbMgr.lock);
D
dapan1121 已提交
684
  appHbMgrCleanup();
L
fix  
Liu Jicong 已提交
685
  taosArrayDestroy(clientHbMgr.appHbMgrs);
wafwerar's avatar
wafwerar 已提交
686
  taosThreadMutexUnlock(&clientHbMgr.lock);
L
fix  
Liu Jicong 已提交
687

688
  clientHbMgr.appHbMgrs = NULL;
L
Liu Jicong 已提交
689 690
}

L
fix  
Liu Jicong 已提交
691
int hbRegisterConnImpl(SAppHbMgr *pAppHbMgr, SClientHbKey connKey, SHbConnInfo *info) {
L
Liu Jicong 已提交
692
  // init hash in activeinfo
L
fix  
Liu Jicong 已提交
693
  void *data = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
L
Liu Jicong 已提交
694 695 696
  if (data != NULL) {
    return 0;
  }
D
dapan1121 已提交
697
  SClientHbReq hbReq = {0};
L
Liu Jicong 已提交
698 699
  hbReq.connKey = connKey;
  hbReq.info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
L
fix  
Liu Jicong 已提交
700

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

L
Liu Jicong 已提交
703
  // init hash
D
dapan1121 已提交
704
  if (info != NULL) {
L
fix  
Liu Jicong 已提交
705
    SClientHbReq *pReq = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
D
dapan1121 已提交
706 707
    info->req = pReq;
    taosHashPut(pAppHbMgr->connInfo, &connKey, sizeof(SClientHbKey), info, sizeof(SHbConnInfo));
L
Liu Jicong 已提交
708 709
  }

L
Liu Jicong 已提交
710
  atomic_add_fetch_32(&pAppHbMgr->connKeyCnt, 1);
L
Liu Jicong 已提交
711 712 713
  return 0;
}

D
dapan1121 已提交
714
int hbRegisterConn(SAppHbMgr *pAppHbMgr, int64_t tscRefId, int64_t clusterId, int8_t connType) {
L
Liu Jicong 已提交
715
  SClientHbKey connKey = {
D
dapan1121 已提交
716
      .tscRid = tscRefId,
D
dapan1121 已提交
717
      .connType = connType,
L
Liu Jicong 已提交
718 719
  };
  SHbConnInfo info = {0};
D
dapan1121 已提交
720

D
dapan1121 已提交
721 722
  switch (connType) {
    case CONN_TYPE__QUERY: {
wafwerar's avatar
wafwerar 已提交
723
      int64_t *pClusterId = taosMemoryMalloc(sizeof(int64_t));
D
dapan1121 已提交
724 725 726
      *pClusterId = clusterId;

      info.param = pClusterId;
L
Liu Jicong 已提交
727
      return hbRegisterConnImpl(pAppHbMgr, connKey, &info);
D
dapan1121 已提交
728
    }
D
dapan1121 已提交
729
    case CONN_TYPE__TMQ: {
L
Liu Jicong 已提交
730
      return 0;
D
dapan1121 已提交
731 732
    }
    default:
L
Liu Jicong 已提交
733
      return 0;
D
dapan1121 已提交
734 735 736
  }
}

L
fix  
Liu Jicong 已提交
737
void hbDeregisterConn(SAppHbMgr *pAppHbMgr, SClientHbKey connKey) {
D
dapan1121 已提交
738 739 740 741 742 743 744 745 746 747 748 749
  SClientHbReq *pReq = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
  if (pReq) {
    hbFreeReq(pReq);
    taosHashCleanup(pReq->info);
    taosHashRemove(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
  }

  SHbConnInfo *info = taosHashGet(pAppHbMgr->connInfo, &connKey, sizeof(SClientHbKey));
  if (info) {
    taosMemoryFree(info->param);
    taosHashRemove(pAppHbMgr->connInfo, &connKey, sizeof(SClientHbKey));
  }
L
Liu Jicong 已提交
750

D
dapan1121 已提交
751
  if (NULL == pReq || NULL == info) {
D
dapan1121 已提交
752 753
    return;
  }
L
Liu Jicong 已提交
754

L
Liu Jicong 已提交
755
  atomic_sub_fetch_32(&pAppHbMgr->connKeyCnt, 1);
L
Liu Jicong 已提交
756 757
}

L
fix  
Liu Jicong 已提交
758 759
int hbAddConnInfo(SAppHbMgr *pAppHbMgr, SClientHbKey connKey, void *key, void *value, int32_t keyLen,
                  int32_t valueLen) {
L
Liu Jicong 已提交
760
  // find req by connection id
L
fix  
Liu Jicong 已提交
761
  SClientHbReq *pReq = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
L
Liu Jicong 已提交
762 763 764 765 766 767
  ASSERT(pReq != NULL);

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

  return 0;
}