clientHb.h 4.1 KB
Newer Older
L
Liu Jicong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
/*
 * 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/>.
 */

#include "os.h"
#include "tarray.h"
#include "thash.h"
#include "tmsg.h"

typedef enum {
  mq = 0,
  HEARTBEAT_TYPE_MAX
} EHbType;

typedef struct SKlv {
  int32_t keyLen;
  int32_t valueLen;
  void*   key;
  void*   value;
} SKlv;

static FORCE_INLINE int taosEncodeSKlv(void** buf, const SKlv* pKlv) {
  int tlen = 0;
  tlen += taosEncodeFixedI32(buf, pKlv->keyLen);
  tlen += taosEncodeFixedI32(buf, pKlv->valueLen);
  tlen += taosEncodeBinary(buf, pKlv->key, pKlv->keyLen);
  tlen += taosEncodeBinary(buf, pKlv->value, pKlv->valueLen);
  return tlen;
}

static FORCE_INLINE void* taosDecodeSKlv(void* buf, SKlv* pKlv) {
  buf = taosDecodeFixedI32(buf, &pKlv->keyLen); 
  buf = taosDecodeFixedI32(buf, &pKlv->valueLen);
  buf = taosDecodeBinary(buf, &pKlv->key, pKlv->keyLen);
  buf = taosDecodeBinary(buf, &pKlv->value, pKlv->valueLen);
  return buf;
}

typedef struct SClientHbKey {
  int32_t connId;
  int32_t hbType;
} SClientHbKey;

static FORCE_INLINE int taosEncodeSClientHbKey(void** buf, const SClientHbKey* pKey) {
  int tlen = 0;
  tlen += taosEncodeFixedI32(buf, pKey->connId);
  tlen += taosEncodeFixedI32(buf, pKey->hbType);
  return tlen;
}

static FORCE_INLINE void* taosDecodeSClientHbKey(void* buf, SClientHbKey* pKey) {
  buf = taosDecodeFixedI32(buf, &pKey->connId);
  buf = taosDecodeFixedI32(buf, &pKey->hbType);
  return buf;
}

typedef struct SClientHbReq {
  SClientHbKey hbKey;
  SHashObj* info;  // hash<Sklv>
} SClientHbReq;

static FORCE_INLINE int tSerializeSClientHbReq(void** buf, const SClientHbReq* pReq) {
  int tlen = 0;
  tlen += taosEncodeSClientHbKey(buf, &pReq->hbKey);
  
  void* pIter = NULL;
  void* data;
  SKlv klv;
  data = taosHashIterate(pReq->info, pIter);
  while (data != NULL) {
    taosHashGetKey(data, &klv.key, (size_t*)&klv.keyLen);
    klv.valueLen = taosHashGetDataLen(data);
    klv.value = data;
    taosEncodeSKlv(buf, &klv);

    data = taosHashIterate(pReq->info, pIter);
  }
  return tlen;
}

static FORCE_INLINE void* tDeserializeClientHbReq(void* buf, SClientHbReq* pReq) {
  ASSERT(pReq->info != NULL);
  buf = taosDecodeSClientHbKey(buf, &pReq->hbKey);

  //TODO: error handling
  if(pReq->info == NULL) {
    pReq->info = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
  }
  SKlv klv;
  buf = taosDecodeSKlv(buf, &klv);
  taosHashPut(pReq->info, klv.key, klv.keyLen, klv.value, klv.valueLen);
  
  return buf;
}

typedef struct SClientHbBatchReq {
  int64_t reqId;
  SArray* reqs;  // SArray<SClientHbReq>
} SClientHbBatchReq;

typedef struct SClientHbHandleResult {
} SClientHbHandleResult;

typedef struct SClientHbRsp {
  int32_t connId;
  int32_t hbType;
} SClientHbRsp;

typedef struct SClientHbBatchRsp {
  int64_t reqId;
  int64_t rspId;
  SArray* rsps;  // SArray<SClientHbRsp>
} SClientHbBatchRsp;

typedef int32_t (*FHbRspHandle)(SClientHbReq* pReq);
typedef int32_t (*FGetConnInfo)(int32_t conn, void* self);

typedef struct SClientHbMgr {
  int8_t       inited;
  int32_t      reportInterval;  // unit ms
  int32_t      stats;
  SRWLatch     lock;
  SHashObj*    info;   //hash<SClientHbKey, SClientHbReq>
  FHbRspHandle handle[HEARTBEAT_TYPE_MAX];
  // input queue
} SClientHbMgr;

static SClientHbMgr clientHbMgr = {0};

int  hbMgrInit();
void hbMgrCleanUp();

int registerConn(int32_t connId, FGetConnInfo func, FHbRspHandle rspHandle);

int registerHbRspHandle(int32_t connId, int32_t hbType, FHbRspHandle rspHandle);

int HbAddConnInfo(int32_t connId, void* key, void* value, int32_t keyLen, int32_t valueLen);