clientHb.c 11.9 KB
Newer Older
L
Liu Jicong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

L
Liu Jicong 已提交
16
#include "clientInt.h"
L
Liu Jicong 已提交
17
#include "trpc.h"
L
Liu Jicong 已提交
18 19 20 21 22 23

static SClientHbMgr clientHbMgr = {0};

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

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

D
dapan1121 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
static int32_t hbQueryHbRspHandle(struct SAppHbMgr *pAppHbMgr, SClientHbRsp* pRsp) {
  SHbConnInfo * info = taosHashGet(pAppHbMgr->connInfo, &pRsp->connKey, sizeof(SClientHbKey));
  if (NULL == info) {
    tscWarn("fail to get connInfo, may be dropped, connId:%d, type:%d", pRsp->connKey.connId, pRsp->connKey.hbType);
    return TSDB_CODE_SUCCESS;
  }

  int32_t kvNum = pRsp->info ? taosArrayGetSize(pRsp->info) : 0;
  for (int32_t i = 0; i < kvNum; ++i) {
    SKv *kv = taosArrayGet(pRsp->info, i);
    switch (kv->key) {
      case HEARTBEAT_KEY_DBINFO:

        break;
      case HEARTBEAT_KEY_STBINFO:

        break;
      default:
        tscError("invalid hb key type:%d", kv->key);
        break;
    }
  }

  return TSDB_CODE_SUCCESS;
}

L
Liu Jicong 已提交
54 55 56 57
static int32_t hbMqAsyncCallBack(void* param, const SDataBuf* pMsg, int32_t code) {
  if (code != 0) {
    return -1;
  }
D
dapan1121 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  char *key = (char *)param;
  SClientHbBatchRsp* pRsp = (SClientHbBatchRsp*) pMsg->pData;
  int32_t reqNum = taosArrayGetSize(pRsp->rsps);

  SAppInstInfo** pInst = taosHashGet(appInfo.pInstMap, key, strlen(key));
  if (pInst == NULL || NULL == *pInst) {
    tscError("cluster not exist, key:%s", key);
    return -1;
  }

  for (int32_t i = 0; i < reqNum; ++i) {
    SClientHbRsp* rsp = taosArrayGet(pRsp->rsps, i);
    code = (*clientHbMgr.rspHandle[rsp->connKey.hbType])((*pInst)->pAppHbMgr, rsp);
    if (code) {
      break;
    }
  }
  
  return code;
L
Liu Jicong 已提交
77 78
}

D
dapan1121 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
int32_t hbGetExpiredDBInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
  SDbVgVersion *dbs = NULL;
  uint32_t dbNum = 0;
  int32_t code = 0;

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

  for (int32_t i = 0; i < dbNum; ++i) {
    SDbVgVersion *db = &dbs[i];
    db->dbId = htobe64(db->dbId);
    db->vgVersion = htonl(db->vgVersion);
  }

  SKv kv = {.key = HEARTBEAT_KEY_DBINFO, .valueLen = sizeof(SDbVgVersion) * dbNum, .value = dbs};
  taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));

  return TSDB_CODE_SUCCESS;
}

int32_t hbQueryHbReqHandle(SClientHbKey *connKey, void* param, SClientHbReq *req) {
  int64_t *clusterId = (int64_t *)param;
  struct SCatalog *pCatalog = NULL;

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


  return TSDB_CODE_SUCCESS;
}

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

}

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

L
Liu Jicong 已提交
131 132
static FORCE_INLINE void hbMgrInitHandle() {
  // init all handle
D
dapan1121 已提交
133
  hbMgrInitMqHbHandle();
L
Liu Jicong 已提交
134 135
}

D
dapan1121 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

void hbFreeReqKvHash(SHashObj*      info) {
  void *pIter = taosHashIterate(info, NULL);
  while (pIter != NULL) {
    SKv* kv = pIter;

    tfree(kv->value);

    pIter = taosHashIterate(info, pIter);
  }
}

void hbFreeReq(SClientHbReq *req) {
  hbFreeReqKvHash(req->info);
}



L
Liu Jicong 已提交
154
SClientHbBatchReq* hbGatherAllInfo(SAppHbMgr *pAppHbMgr) {
L
Liu Jicong 已提交
155 156
  SClientHbBatchReq* pBatchReq = malloc(sizeof(SClientHbBatchReq));
  if (pBatchReq == NULL) {
L
Liu Jicong 已提交
157 158 159
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    return NULL;
  }
L
Liu Jicong 已提交
160
  int32_t connKeyCnt = atomic_load_32(&pAppHbMgr->connKeyCnt);
L
Liu Jicong 已提交
161
  pBatchReq->reqs = taosArrayInit(connKeyCnt, sizeof(SClientHbReq));
L
Liu Jicong 已提交
162

D
dapan1121 已提交
163
  int32_t code = 0;
L
Liu Jicong 已提交
164
  void *pIter = taosHashIterate(pAppHbMgr->activeInfo, NULL);
L
Liu Jicong 已提交
165 166
  while (pIter != NULL) {
    SClientHbReq* pOneReq = pIter;
D
dapan1121 已提交
167 168 169 170 171 172 173 174 175 176

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

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

L
Liu Jicong 已提交
179
    pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter);
L
Liu Jicong 已提交
180 181
  }

D
dapan1121 已提交
182 183 184
  if (code) {
    taosArrayDestroyEx(pBatchReq->reqs, hbFreeReq);
    tfree(pBatchReq);
L
Liu Jicong 已提交
185 186
  }

L
Liu Jicong 已提交
187
  return pBatchReq;
L
Liu Jicong 已提交
188 189
}

D
dapan1121 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

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

    hbFreeReqKvHash(pOneReq->info);
    taosHashClear(pOneReq->info);

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



L
Liu Jicong 已提交
205 206 207
static void* hbThreadFunc(void* param) {
  setThreadName("hb");
  while (1) {
L
Liu Jicong 已提交
208 209 210 211 212
    int8_t threadStop = atomic_load_8(&clientHbMgr.threadStop);
    if(threadStop) {
      break;
    }

L
Liu Jicong 已提交
213 214
    int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
    for(int i = 0; i < sz; i++) {
L
Liu Jicong 已提交
215 216
      SAppHbMgr* pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);

L
Liu Jicong 已提交
217 218 219 220
      int32_t connCnt = atomic_load_32(&pAppHbMgr->connKeyCnt);
      if (connCnt == 0) {
        continue;
      }
L
Liu Jicong 已提交
221
      SClientHbBatchReq* pReq = hbGatherAllInfo(pAppHbMgr);
L
Liu Jicong 已提交
222 223 224
      if (pReq == NULL) {
        continue;
      }
L
Liu Jicong 已提交
225 226 227
      int tlen = tSerializeSClientHbBatchReq(NULL, pReq);
      void *buf = malloc(tlen);
      if (buf == NULL) {
D
dapan1121 已提交
228 229 230
        terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
        tFreeClientHbBatchReq(pReq, false);
        hbClearReqInfo(pAppHbMgr);
L
Liu Jicong 已提交
231 232
        break;
      }
L
Liu Jicong 已提交
233 234
      void *abuf = buf;
      tSerializeSClientHbBatchReq(&abuf, pReq);
L
Liu Jicong 已提交
235 236 237
      SMsgSendInfo *pInfo = malloc(sizeof(SMsgSendInfo));
      if (pInfo == NULL) {
        terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
L
Liu Jicong 已提交
238
        tFreeClientHbBatchReq(pReq, false);
D
dapan1121 已提交
239
        hbClearReqInfo(pAppHbMgr);
L
Liu Jicong 已提交
240 241 242 243 244 245 246
        free(buf);
        break;
      }
      pInfo->fp = hbMqAsyncCallBack;
      pInfo->msgInfo.pData = buf;
      pInfo->msgInfo.len = tlen;
      pInfo->msgType = TDMT_MND_HEARTBEAT;
D
dapan1121 已提交
247
      pInfo->param = strdup(pAppHbMgr->key);
L
Liu Jicong 已提交
248 249
      pInfo->requestId = generateRequestId();
      pInfo->requestObjRefId = 0;
L
Liu Jicong 已提交
250 251

      SAppInstInfo *pAppInstInfo = pAppHbMgr->pAppInstInfo;
L
Liu Jicong 已提交
252
      int64_t transporterId = 0;
L
Liu Jicong 已提交
253
      SEpSet epSet = getEpSet_s(&pAppInstInfo->mgmtEp);
L
Liu Jicong 已提交
254
      asyncSendMsgToServer(pAppInstInfo->pTransporter, &epSet, &transporterId, pInfo);
L
Liu Jicong 已提交
255
      tFreeClientHbBatchReq(pReq, false);
L
Liu Jicong 已提交
256 257 258

      atomic_add_fetch_32(&pAppHbMgr->reportCnt, 1);
    }
L
Liu Jicong 已提交
259
    taosMsleep(HEARTBEAT_INTERVAL);
L
Liu Jicong 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
  }
  return NULL;
}

static int32_t hbCreateThread() {
  pthread_attr_t thAttr;
  pthread_attr_init(&thAttr);
  pthread_attr_setdetachstate(&thAttr, PTHREAD_CREATE_JOINABLE);

  if (pthread_create(&clientHbMgr.thread, &thAttr, hbThreadFunc, NULL) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }
  pthread_attr_destroy(&thAttr);
  return 0;
}

L
Liu Jicong 已提交
277 278 279 280
static void hbStopThread() {
  atomic_store_8(&clientHbMgr.threadStop, 1);
}

D
dapan1121 已提交
281
SAppHbMgr* appHbMgrInit(SAppInstInfo* pAppInstInfo, char *key) {
L
Liu Jicong 已提交
282
  hbMgrInit();
L
Liu Jicong 已提交
283 284 285 286 287 288 289
  SAppHbMgr* pAppHbMgr = malloc(sizeof(SAppHbMgr)); 
  if (pAppHbMgr == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
  // init stat
  pAppHbMgr->startTime = taosGetTimestampMs();
L
Liu Jicong 已提交
290 291 292
  pAppHbMgr->connKeyCnt = 0;
  pAppHbMgr->reportCnt = 0;
  pAppHbMgr->reportBytes = 0;
D
dapan1121 已提交
293
  pAppHbMgr->key = strdup(key);
L
Liu Jicong 已提交
294

L
Liu Jicong 已提交
295 296
  // init app info
  pAppHbMgr->pAppInstInfo = pAppInstInfo;
L
Liu Jicong 已提交
297 298 299

  // init hash info
  pAppHbMgr->activeInfo = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
L
Liu Jicong 已提交
300 301 302 303 304 305

  if (pAppHbMgr->activeInfo == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    free(pAppHbMgr);
    return NULL;
  }
L
Liu Jicong 已提交
306 307 308 309
  pAppHbMgr->activeInfo->freeFp = tFreeClientHbReq;
  // init getInfoFunc
  pAppHbMgr->getInfoFuncs = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);

L
Liu Jicong 已提交
310 311 312 313 314 315
  if (pAppHbMgr->getInfoFuncs == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    free(pAppHbMgr);
    return NULL;
  }

L
Liu Jicong 已提交
316 317 318 319 320 321 322 323 324
  taosArrayPush(clientHbMgr.appHbMgrs, &pAppHbMgr);
  return pAppHbMgr;
}

void appHbMgrCleanup(SAppHbMgr* pAppHbMgr) {
  pthread_mutex_lock(&clientHbMgr.lock);

  int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
  for (int i = 0; i < sz; i++) {
L
Liu Jicong 已提交
325
    SAppHbMgr* pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i);
L
Liu Jicong 已提交
326 327 328 329 330 331 332 333 334 335
    if (pAppHbMgr == pTarget) {
      taosHashCleanup(pTarget->activeInfo);
      taosHashCleanup(pTarget->getInfoFuncs);
    }
  }

  pthread_mutex_unlock(&clientHbMgr.lock);
}

int hbMgrInit() {
L
Liu Jicong 已提交
336 337 338 339
  // init once
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 0, 1);
  if (old == 1) return 0;

L
Liu Jicong 已提交
340 341
  clientHbMgr.appHbMgrs = taosArrayInit(0, sizeof(void*));
  pthread_mutex_init(&clientHbMgr.lock, NULL);
L
Liu Jicong 已提交
342 343 344 345 346

  // init handle funcs
  hbMgrInitHandle();

  // init backgroud thread
L
Liu Jicong 已提交
347 348
  hbCreateThread();

L
Liu Jicong 已提交
349 350 351 352
  return 0;
}

void hbMgrCleanUp() {
L
Liu Jicong 已提交
353
  // destroy all appHbMgr
L
Liu Jicong 已提交
354 355 356
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 1, 0);
  if (old == 0) return;

L
Liu Jicong 已提交
357
  taosArrayDestroy(clientHbMgr.appHbMgrs);
L
Liu Jicong 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
}

int hbHandleRsp(SClientHbBatchRsp* hbRsp) {
  int64_t reqId = hbRsp->reqId;
  int64_t rspId = hbRsp->rspId;

  SArray* rsps = hbRsp->rsps;
  int32_t sz = taosArrayGetSize(rsps);
  for (int i = 0; i < sz; i++) {
    SClientHbRsp* pRsp = taosArrayGet(rsps, i);
    if (pRsp->connKey.hbType < HEARTBEAT_TYPE_MAX) {
      clientHbMgr.handle[pRsp->connKey.hbType](pRsp);
    } else {
      // discard rsp
    }
  }
  return 0;
}

D
dapan1121 已提交
377
int hbRegisterConnImpl(SAppHbMgr* pAppHbMgr, SClientHbKey connKey, SHbConnInfo *info) {
L
Liu Jicong 已提交
378
  // init hash in activeinfo
L
Liu Jicong 已提交
379
  void* data = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
L
Liu Jicong 已提交
380 381 382 383 384 385
  if (data != NULL) {
    return 0;
  }
  SClientHbReq hbReq;
  hbReq.connKey = connKey;
  hbReq.info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
D
dapan1121 已提交
386
  
L
Liu Jicong 已提交
387
  taosHashPut(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey), &hbReq, sizeof(SClientHbReq));
D
dapan1121 已提交
388
 
L
Liu Jicong 已提交
389
  // init hash
D
dapan1121 已提交
390 391 392 393
  if (info != NULL) {
    SClientHbReq * pReq = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
    info->req = pReq;
    taosHashPut(pAppHbMgr->connInfo, &connKey, sizeof(SClientHbKey), info, sizeof(SHbConnInfo));
L
Liu Jicong 已提交
394 395
  }

L
Liu Jicong 已提交
396
  atomic_add_fetch_32(&pAppHbMgr->connKeyCnt, 1);
L
Liu Jicong 已提交
397 398 399
  return 0;
}

D
dapan1121 已提交
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
int hbRegisterConn(SAppHbMgr* pAppHbMgr, int32_t connId, int64_t clusterId, int32_t hbType) {
  SClientHbKey connKey = {.connId = connId, .hbType = HEARTBEAT_TYPE_QUERY};
  SHbConnInfo info = {0};

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

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

L
Liu Jicong 已提交
422 423 424 425
void hbDeregisterConn(SAppHbMgr* pAppHbMgr, SClientHbKey connKey) {
  taosHashRemove(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
  taosHashRemove(pAppHbMgr->getInfoFuncs, &connKey, sizeof(SClientHbKey));
  atomic_sub_fetch_32(&pAppHbMgr->connKeyCnt, 1);
L
Liu Jicong 已提交
426 427
}

L
Liu Jicong 已提交
428
int hbAddConnInfo(SAppHbMgr *pAppHbMgr, SClientHbKey connKey, void* key, void* value, int32_t keyLen, int32_t valueLen) {
L
Liu Jicong 已提交
429
  // find req by connection id
L
Liu Jicong 已提交
430
  SClientHbReq* pReq = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
L
Liu Jicong 已提交
431 432 433 434 435 436
  ASSERT(pReq != NULL);

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

  return 0;
}