transComm.c 15.4 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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/>.
 */
#ifdef USE_UV

#include "transComm.h"

dengyihao's avatar
dengyihao 已提交
19 20 21
static TdThreadOnce transModuleInit = PTHREAD_ONCE_INIT;

static int32_t refMgt;
dengyihao's avatar
dengyihao 已提交
22
static int32_t instMgt;
dengyihao's avatar
dengyihao 已提交
23

dengyihao's avatar
dengyihao 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37
int transAuthenticateMsg(void* pMsg, int msgLen, void* pAuth, void* pKey) {
  T_MD5_CTX context;
  int       ret = -1;

  tMD5Init(&context);
  tMD5Update(&context, (uint8_t*)pKey, TSDB_PASSWORD_LEN);
  tMD5Update(&context, (uint8_t*)pMsg, msgLen);
  tMD5Update(&context, (uint8_t*)pKey, TSDB_PASSWORD_LEN);
  tMD5Final(&context);

  if (memcmp(context.digest, pAuth, sizeof(context.digest)) == 0) ret = 0;

  return ret;
}
dengyihao's avatar
dengyihao 已提交
38

dengyihao's avatar
dengyihao 已提交
39 40 41 42 43 44 45 46 47 48 49
void transBuildAuthHead(void* pMsg, int msgLen, void* pAuth, void* pKey) {
  T_MD5_CTX context;

  tMD5Init(&context);
  tMD5Update(&context, (uint8_t*)pKey, TSDB_PASSWORD_LEN);
  tMD5Update(&context, (uint8_t*)pMsg, msgLen);
  tMD5Update(&context, (uint8_t*)pKey, TSDB_PASSWORD_LEN);
  tMD5Final(&context);

  memcpy(pAuth, context.digest, sizeof(context.digest));
}
dengyihao's avatar
dengyihao 已提交
50

dengyihao's avatar
dengyihao 已提交
51
bool transCompressMsg(char* msg, int32_t len, int32_t* flen) {
dengyihao's avatar
dengyihao 已提交
52
  return false;
dengyihao's avatar
dengyihao 已提交
53 54 55 56 57 58 59
  // SRpcHead* pHead = rpcHeadFromCont(pCont);
  bool succ = false;
  int  overhead = sizeof(STransCompMsg);
  if (!NEEDTO_COMPRESSS_MSG(len)) {
    return succ;
  }

wafwerar's avatar
wafwerar 已提交
60
  char* buf = taosMemoryMalloc(len + overhead + 8);  // 8 extra bytes
dengyihao's avatar
dengyihao 已提交
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
  if (buf == NULL) {
    tError("failed to allocate memory for rpc msg compression, contLen:%d", len);
    *flen = len;
    return succ;
  }

  int32_t clen = LZ4_compress_default(msg, buf, len, len + overhead);
  tDebug("compress rpc msg, before:%d, after:%d, overhead:%d", len, clen, overhead);
  /*
   * only the compressed size is less than the value of contLen - overhead, the compression is applied
   * The first four bytes is set to 0, the second four bytes are utilized to keep the original length of message
   */
  if (clen > 0 && clen < len - overhead) {
    STransCompMsg* pComp = (STransCompMsg*)msg;
    pComp->reserved = 0;
    pComp->contLen = htonl(len);
    memcpy(msg + overhead, buf, clen);

    tDebug("compress rpc msg, before:%d, after:%d", len, clen);
    *flen = clen + overhead;
    succ = true;
  } else {
    *flen = len;
    succ = false;
  }
wafwerar's avatar
wafwerar 已提交
86
  taosMemoryFree(buf);
dengyihao's avatar
dengyihao 已提交
87 88 89 90 91 92 93 94 95 96 97 98
  return succ;
}
bool transDecompressMsg(char* msg, int32_t len, int32_t* flen) {
  // impl later
  return false;
  STransCompMsg* pComp = (STransCompMsg*)msg;

  int overhead = sizeof(STransCompMsg);
  int clen = 0;
  return false;
}

dengyihao's avatar
dengyihao 已提交
99 100 101 102
void transFreeMsg(void* msg) {
  if (msg == NULL) {
    return;
  }
wafwerar's avatar
wafwerar 已提交
103
  taosMemoryFree((char*)msg - sizeof(STransMsgHead));
dengyihao's avatar
dengyihao 已提交
104
}
dengyihao's avatar
dengyihao 已提交
105 106 107 108 109 110 111

int transInitBuffer(SConnBuffer* buf) {
  transClearBuffer(buf);
  return 0;
}
int transClearBuffer(SConnBuffer* buf) {
  memset(buf, 0, sizeof(*buf));
dengyihao's avatar
fix bug  
dengyihao 已提交
112
  buf->total = -1;
dengyihao's avatar
dengyihao 已提交
113 114 115 116 117 118
  return 0;
}
int transAllocBuffer(SConnBuffer* connBuf, uv_buf_t* uvBuf) {
  /*
   * formate of data buffer:
   * |<--------------------------data from socket------------------------------->|
dengyihao's avatar
dengyihao 已提交
119 120
   * |<------STransMsgHead------->|<-------------------userdata--------------->|<-----auth data----->|<----user
   * info--->|
dengyihao's avatar
dengyihao 已提交
121
   */
dengyihao's avatar
fix bug  
dengyihao 已提交
122
  static const int CAPACITY = sizeof(STransMsgHead);
dengyihao's avatar
dengyihao 已提交
123 124 125

  SConnBuffer* p = connBuf;
  if (p->cap == 0) {
wafwerar's avatar
wafwerar 已提交
126
    p->buf = (char*)taosMemoryCalloc(CAPACITY, sizeof(char));
dengyihao's avatar
dengyihao 已提交
127 128
    p->len = 0;
    p->cap = CAPACITY;
dengyihao's avatar
dengyihao 已提交
129
    p->total = -1;
dengyihao's avatar
dengyihao 已提交
130 131 132

    uvBuf->base = p->buf;
    uvBuf->len = CAPACITY;
dengyihao's avatar
dengyihao 已提交
133 134 135
  } else if (p->total == -1 && p->len < CAPACITY) {
    uvBuf->base = p->buf + p->len;
    uvBuf->len = CAPACITY - p->len;
dengyihao's avatar
dengyihao 已提交
136
  } else {
dengyihao's avatar
dengyihao 已提交
137
    p->cap = p->total;
wafwerar's avatar
wafwerar 已提交
138
    p->buf = taosMemoryRealloc(p->buf, p->cap);
S
Shengliang Guan 已提交
139
    tTrace("internal malloc mem:%p, size:%d", p->buf, p->cap);
dengyihao's avatar
dengyihao 已提交
140

dengyihao's avatar
dengyihao 已提交
141 142 143 144 145
    uvBuf->base = p->buf + p->len;
    uvBuf->len = p->cap - p->len;
  }
  return 0;
}
dengyihao's avatar
dengyihao 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158
// check whether already read complete
bool transReadComplete(SConnBuffer* connBuf) {
  if (connBuf->total == -1 && connBuf->len >= sizeof(STransMsgHead)) {
    STransMsgHead head;
    memcpy((char*)&head, connBuf->buf, sizeof(head));
    int32_t msgLen = (int32_t)htonl(head.msgLen);
    connBuf->total = msgLen;
  }
  if (connBuf->len == connBuf->cap && connBuf->total == connBuf->cap) {
    return true;
  }
  return false;
}
dengyihao's avatar
dengyihao 已提交
159
int transPackMsg(STransMsgHead* msgHead, bool sercured, bool auth) { return 0; }
dengyihao's avatar
dengyihao 已提交
160

dengyihao's avatar
dengyihao 已提交
161
int transUnpackMsg(STransMsgHead* msgHead) { return 0; }
dengyihao's avatar
dengyihao 已提交
162 163
int transDestroyBuffer(SConnBuffer* buf) {
  if (buf->cap > 0) {
wafwerar's avatar
wafwerar 已提交
164
    taosMemoryFreeClear(buf->buf);
dengyihao's avatar
dengyihao 已提交
165 166
  }
  transClearBuffer(buf);
167 168

  return 0;
dengyihao's avatar
dengyihao 已提交
169
}
dengyihao's avatar
dengyihao 已提交
170

dengyihao's avatar
dengyihao 已提交
171 172 173 174 175 176
int transSetConnOption(uv_tcp_t* stream) {
  uv_tcp_nodelay(stream, 1);
  int ret = uv_tcp_keepalive(stream, 5, 5);
  return ret;
}

dengyihao's avatar
dengyihao 已提交
177
SAsyncPool* transCreateAsyncPool(uv_loop_t* loop, int sz, void* arg, AsyncCB cb) {
wafwerar's avatar
wafwerar 已提交
178
  SAsyncPool* pool = taosMemoryCalloc(1, sizeof(SAsyncPool));
dengyihao's avatar
dengyihao 已提交
179 180
  pool->index = 0;
  pool->nAsync = sz;
wafwerar's avatar
wafwerar 已提交
181
  pool->asyncs = taosMemoryCalloc(1, sizeof(uv_async_t) * pool->nAsync);
dengyihao's avatar
dengyihao 已提交
182 183 184 185

  for (int i = 0; i < pool->nAsync; i++) {
    uv_async_t* async = &(pool->asyncs[i]);
    uv_async_init(loop, async, cb);
dengyihao's avatar
dengyihao 已提交
186

wafwerar's avatar
wafwerar 已提交
187
    SAsyncItem* item = taosMemoryCalloc(1, sizeof(SAsyncItem));
dengyihao's avatar
dengyihao 已提交
188 189
    item->pThrd = arg;
    QUEUE_INIT(&item->qmsg);
wafwerar's avatar
wafwerar 已提交
190
    taosThreadMutexInit(&item->mtx, NULL);
dengyihao's avatar
dengyihao 已提交
191 192

    async->data = item;
dengyihao's avatar
dengyihao 已提交
193 194 195
  }
  return pool;
}
dengyihao's avatar
dengyihao 已提交
196

dengyihao's avatar
dengyihao 已提交
197 198 199
void transDestroyAsyncPool(SAsyncPool* pool) {
  for (int i = 0; i < pool->nAsync; i++) {
    uv_async_t* async = &(pool->asyncs[i]);
dengyihao's avatar
dengyihao 已提交
200
    // uv_close((uv_handle_t*)async, NULL);
dengyihao's avatar
dengyihao 已提交
201
    SAsyncItem* item = async->data;
wafwerar's avatar
wafwerar 已提交
202
    taosThreadMutexDestroy(&item->mtx);
wafwerar's avatar
wafwerar 已提交
203
    taosMemoryFree(item);
dengyihao's avatar
dengyihao 已提交
204
  }
wafwerar's avatar
wafwerar 已提交
205 206
  taosMemoryFree(pool->asyncs);
  taosMemoryFree(pool);
dengyihao's avatar
dengyihao 已提交
207
}
dengyihao's avatar
dengyihao 已提交
208
int transAsyncSend(SAsyncPool* pool, queue* q) {
dengyihao's avatar
dengyihao 已提交
209 210 211 212 213 214
  int idx = pool->index;
  idx = idx % pool->nAsync;
  // no need mutex here
  if (pool->index++ > pool->nAsync) {
    pool->index = 0;
  }
dengyihao's avatar
dengyihao 已提交
215 216 217 218
  uv_async_t* async = &(pool->asyncs[idx]);
  SAsyncItem* item = async->data;

  int64_t st = taosGetTimestampUs();
wafwerar's avatar
wafwerar 已提交
219
  taosThreadMutexLock(&item->mtx);
dengyihao's avatar
dengyihao 已提交
220
  QUEUE_PUSH(&item->qmsg, q);
wafwerar's avatar
wafwerar 已提交
221
  taosThreadMutexUnlock(&item->mtx);
dengyihao's avatar
dengyihao 已提交
222 223
  int64_t el = taosGetTimestampUs() - st;
  if (el > 50) {
S
Shengliang Guan 已提交
224
    // tInfo("lock and unlock cost:%d", (int)el);
dengyihao's avatar
dengyihao 已提交
225 226
  }
  return uv_async_send(async);
dengyihao's avatar
dengyihao 已提交
227
}
dengyihao's avatar
dengyihao 已提交
228

dengyihao's avatar
dengyihao 已提交
229 230 231 232
void transCtxInit(STransCtx* ctx) {
  // init transCtx
  ctx->args = taosHashInit(2, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UINT), true, HASH_NO_LOCK);
}
dengyihao's avatar
dengyihao 已提交
233
void transCtxCleanup(STransCtx* ctx) {
dengyihao's avatar
dengyihao 已提交
234 235 236 237 238 239
  if (ctx->args == NULL) {
    return;
  }

  STransCtxVal* iter = taosHashIterate(ctx->args, NULL);
  while (iter) {
dengyihao's avatar
dengyihao 已提交
240
    ctx->freeFunc(iter->val);
dengyihao's avatar
dengyihao 已提交
241 242
    iter = taosHashIterate(ctx->args, iter);
  }
dengyihao's avatar
dengyihao 已提交
243

dengyihao's avatar
dengyihao 已提交
244
  taosHashCleanup(ctx->args);
U
ubuntu 已提交
245
  ctx->args = NULL;
dengyihao's avatar
dengyihao 已提交
246 247 248 249 250
}

void transCtxMerge(STransCtx* dst, STransCtx* src) {
  if (dst->args == NULL) {
    dst->args = src->args;
dengyihao's avatar
dengyihao 已提交
251
    dst->brokenVal = src->brokenVal;
dengyihao's avatar
dengyihao 已提交
252
    dst->freeFunc = src->freeFunc;
dengyihao's avatar
dengyihao 已提交
253 254 255 256 257 258 259 260 261 262 263 264
    src->args = NULL;
    return;
  }
  void*  key = NULL;
  size_t klen = 0;
  void*  iter = taosHashIterate(src->args, NULL);
  while (iter) {
    STransCtxVal* sVal = (STransCtxVal*)iter;
    key = taosHashGetKey(sVal, &klen);

    STransCtxVal* dVal = taosHashGet(dst->args, key, klen);
    if (dVal) {
dengyihao's avatar
dengyihao 已提交
265
      dst->freeFunc(dVal->val);
dengyihao's avatar
dengyihao 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279
    }
    taosHashPut(dst->args, key, klen, sVal, sizeof(*sVal));
    iter = taosHashIterate(src->args, iter);
  }
  taosHashCleanup(src->args);
}
void* transCtxDumpVal(STransCtx* ctx, int32_t key) {
  if (ctx->args == NULL) {
    return NULL;
  }
  STransCtxVal* cVal = taosHashGet(ctx->args, (const void*)&key, sizeof(key));
  if (cVal == NULL) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
280
  void* ret = NULL;
D
dapan1121 已提交
281 282
  (*cVal->clone)(cVal->val, &ret);
  return ret;
dengyihao's avatar
dengyihao 已提交
283
}
dengyihao's avatar
dengyihao 已提交
284
void* transCtxDumpBrokenlinkVal(STransCtx* ctx, int32_t* msgType) {
dengyihao's avatar
dengyihao 已提交
285
  void* ret = NULL;
dengyihao's avatar
dengyihao 已提交
286
  if (ctx->brokenVal.clone == NULL) {
dengyihao's avatar
dengyihao 已提交
287
    return ret;
dengyihao's avatar
dengyihao 已提交
288
  }
D
dapan1121 已提交
289
  (*ctx->brokenVal.clone)(ctx->brokenVal.val, &ret);
dengyihao's avatar
dengyihao 已提交
290 291 292

  *msgType = ctx->brokenVal.msgType;

D
dapan1121 已提交
293
  return ret;
dengyihao's avatar
dengyihao 已提交
294
}
dengyihao's avatar
dengyihao 已提交
295

dengyihao's avatar
dengyihao 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
void transReqQueueInit(queue* q) {
  // init  req queue
  QUEUE_INIT(q);
}
void* transReqQueuePushReq(queue* q) {
  uv_write_t* req = taosMemoryCalloc(1, sizeof(uv_write_t));
  STransReq*  wreq = taosMemoryCalloc(1, sizeof(STransReq));
  wreq->data = req;
  req->data = wreq;
  QUEUE_PUSH(q, &wreq->q);
  return req;
}
void* transReqQueueRemove(void* arg) {
  void*       ret = NULL;
  uv_write_t* req = arg;
  STransReq*  wreq = req && req->data ? req->data : NULL;

  assert(wreq->data == req);
  if (wreq == NULL || wreq->data == NULL) {
    taosMemoryFree(wreq->data);
    taosMemoryFree(wreq);
    return req;
  }

  QUEUE_REMOVE(&wreq->q);

  ret = req && req->handle ? req->handle->data : NULL;
  taosMemoryFree(wreq->data);
  taosMemoryFree(wreq);

  return ret;
}
void transReqQueueClear(queue* q) {
  while (!QUEUE_IS_EMPTY(q)) {
    queue* h = QUEUE_HEAD(q);
    QUEUE_REMOVE(h);
    STransReq* req = QUEUE_DATA(h, STransReq, q);
    taosMemoryFree(req->data);
    taosMemoryFree(req);
  }
}

wafwerar's avatar
wafwerar 已提交
338
void transQueueInit(STransQueue* queue, void (*freeFunc)(const void* arg)) {
dengyihao's avatar
dengyihao 已提交
339
  queue->q = taosArrayInit(2, sizeof(void*));
wafwerar's avatar
wafwerar 已提交
340
  queue->freeFunc = (void (*)(const void*))freeFunc;
dengyihao's avatar
dengyihao 已提交
341 342
}
bool transQueuePush(STransQueue* queue, void* arg) {
dengyihao's avatar
dengyihao 已提交
343 344 345
  if (queue->q == NULL) {
    return true;
  }
dengyihao's avatar
dengyihao 已提交
346 347 348 349 350 351 352
  taosArrayPush(queue->q, &arg);
  if (taosArrayGetSize(queue->q) > 1) {
    return false;
  }
  return true;
}
void* transQueuePop(STransQueue* queue) {
dengyihao's avatar
dengyihao 已提交
353
  if (queue->q == NULL || taosArrayGetSize(queue->q) == 0) {
dengyihao's avatar
dengyihao 已提交
354 355 356 357 358 359
    return NULL;
  }
  void* ptr = taosArrayGetP(queue->q, 0);
  taosArrayRemove(queue->q, 0);
  return ptr;
}
dengyihao's avatar
dengyihao 已提交
360
int32_t transQueueSize(STransQueue* queue) {
dengyihao's avatar
dengyihao 已提交
361 362 363
  if (queue->q == NULL) {
    return 0;
  }
dengyihao's avatar
dengyihao 已提交
364 365 366
  return taosArrayGetSize(queue->q);
}
void* transQueueGet(STransQueue* queue, int i) {
dengyihao's avatar
dengyihao 已提交
367
  if (queue->q == NULL || taosArrayGetSize(queue->q) == 0) {
dengyihao's avatar
dengyihao 已提交
368 369 370 371 372
    return NULL;
  }
  if (i >= taosArrayGetSize(queue->q)) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
373

dengyihao's avatar
dengyihao 已提交
374 375 376 377 378
  void* ptr = taosArrayGetP(queue->q, i);
  return ptr;
}

void* transQueueRm(STransQueue* queue, int i) {
dengyihao's avatar
dengyihao 已提交
379
  if (queue->q == NULL || taosArrayGetSize(queue->q) == 0) {
dengyihao's avatar
dengyihao 已提交
380 381
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
382 383 384 385 386
  if (i >= taosArrayGetSize(queue->q)) {
    return NULL;
  }
  void* ptr = taosArrayGetP(queue->q, i);
  taosArrayRemove(queue->q, i);
dengyihao's avatar
dengyihao 已提交
387 388
  return ptr;
}
dengyihao's avatar
dengyihao 已提交
389

dengyihao's avatar
dengyihao 已提交
390
bool transQueueEmpty(STransQueue* queue) {
dengyihao's avatar
dengyihao 已提交
391 392 393
  if (queue->q == NULL) {
    return true;
  }
dengyihao's avatar
dengyihao 已提交
394 395 396
  return taosArrayGetSize(queue->q) == 0;
}
void transQueueClear(STransQueue* queue) {
wafwerar's avatar
wafwerar 已提交
397
  if (queue->freeFunc != NULL) {
dengyihao's avatar
dengyihao 已提交
398 399
    for (int i = 0; i < taosArrayGetSize(queue->q); i++) {
      void* p = taosArrayGetP(queue->q, i);
wafwerar's avatar
wafwerar 已提交
400
      queue->freeFunc(p);
dengyihao's avatar
dengyihao 已提交
401 402 403 404 405 406 407 408
    }
  }
  taosArrayClear(queue->q);
}
void transQueueDestroy(STransQueue* queue) {
  transQueueClear(queue);
  taosArrayDestroy(queue->q);
}
dengyihao's avatar
dengyihao 已提交
409 410 411 412 413

static int32_t timeCompare(const HeapNode* a, const HeapNode* b) {
  SDelayTask* arg1 = container_of(a, SDelayTask, node);
  SDelayTask* arg2 = container_of(b, SDelayTask, node);
  if (arg1->execTime > arg2->execTime) {
dengyihao's avatar
dengyihao 已提交
414
    return 0;
dengyihao's avatar
dengyihao 已提交
415 416 417 418 419
  } else {
    return 1;
  }
}

dengyihao's avatar
dengyihao 已提交
420
static void transDQTimeout(uv_timer_t* timer) {
dengyihao's avatar
dengyihao 已提交
421
  SDelayQueue* queue = timer->data;
dengyihao's avatar
dengyihao 已提交
422 423
  tTrace("timer %p timeout", timer);
  uint64_t timeout = 0;
dengyihao's avatar
dengyihao 已提交
424
  int64_t  current = taosGetTimestampMs();
dengyihao's avatar
dengyihao 已提交
425 426 427 428
  do {
    HeapNode* minNode = heapMin(queue->heap);
    if (minNode == NULL) break;
    SDelayTask* task = container_of(minNode, SDelayTask, node);
dengyihao's avatar
dengyihao 已提交
429
    if (task->execTime <= current) {
dengyihao's avatar
dengyihao 已提交
430 431 432 433 434
      heapRemove(queue->heap, minNode);
      task->func(task->arg);
      taosMemoryFree(task);
      timeout = 0;
    } else {
dengyihao's avatar
dengyihao 已提交
435
      timeout = task->execTime - current;
dengyihao's avatar
dengyihao 已提交
436 437 438 439
      break;
    }
  } while (1);
  if (timeout != 0) {
dengyihao's avatar
dengyihao 已提交
440
    uv_timer_start(queue->timer, transDQTimeout, timeout, 0);
dengyihao's avatar
dengyihao 已提交
441 442
  }
}
dengyihao's avatar
dengyihao 已提交
443
int transDQCreate(uv_loop_t* loop, SDelayQueue** queue) {
dengyihao's avatar
dengyihao 已提交
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
  uv_timer_t* timer = taosMemoryCalloc(1, sizeof(uv_timer_t));
  uv_timer_init(loop, timer);

  Heap* heap = heapCreate(timeCompare);

  SDelayQueue* q = taosMemoryCalloc(1, sizeof(SDelayQueue));
  q->heap = heap;
  q->timer = timer;
  q->loop = loop;
  q->timer->data = q;

  *queue = q;
  return 0;
}

dengyihao's avatar
dengyihao 已提交
459
void transDQDestroy(SDelayQueue* queue) {
dengyihao's avatar
dengyihao 已提交
460
  taosMemoryFree(queue->timer);
dengyihao's avatar
dengyihao 已提交
461 462 463 464 465 466 467 468 469 470 471

  while (heapSize(queue->heap) > 0) {
    HeapNode* minNode = heapMin(queue->heap);
    if (minNode == NULL) {
      return;
    }
    heapRemove(queue->heap, minNode);

    SDelayTask* task = container_of(minNode, SDelayTask, node);
    taosMemoryFree(task);
  }
dengyihao's avatar
dengyihao 已提交
472 473 474 475
  heapDestroy(queue->heap);
  taosMemoryFree(queue);
}

dengyihao's avatar
dengyihao 已提交
476
int transDQSched(SDelayQueue* queue, void (*func)(void* arg), void* arg, uint64_t timeoutMs) {
dengyihao's avatar
dengyihao 已提交
477
  uint64_t    now = taosGetTimestampMs();
dengyihao's avatar
dengyihao 已提交
478 479 480
  SDelayTask* task = taosMemoryCalloc(1, sizeof(SDelayTask));
  task->func = func;
  task->arg = arg;
dengyihao's avatar
dengyihao 已提交
481 482 483 484 485 486
  task->execTime = now + timeoutMs;

  HeapNode* minNode = heapMin(queue->heap);
  if (minNode) {
    SDelayTask* minTask = container_of(minNode, SDelayTask, node);
    if (minTask->execTime < task->execTime) {
dengyihao's avatar
dengyihao 已提交
487
      timeoutMs = minTask->execTime <= now ? 0 : minTask->execTime - now;
dengyihao's avatar
dengyihao 已提交
488 489
    }
  }
dengyihao's avatar
dengyihao 已提交
490

S
Shengliang Guan 已提交
491
  tTrace("timer %p put task into delay queue, timeoutMs:%" PRIu64, queue->timer, timeoutMs);
dengyihao's avatar
dengyihao 已提交
492
  heapInsert(queue->heap, &task->node);
dengyihao's avatar
dengyihao 已提交
493
  uv_timer_start(queue->timer, transDQTimeout, timeoutMs, 0);
dengyihao's avatar
dengyihao 已提交
494 495
  return 0;
}
dengyihao's avatar
dengyihao 已提交
496 497 498 499 500 501

void transPrintEpSet(SEpSet* pEpSet) {
  if (pEpSet == NULL) {
    tTrace("NULL epset");
    return;
  }
dengyihao's avatar
dengyihao 已提交
502
  char buf[512] = {0};
dengyihao's avatar
dengyihao 已提交
503
  int  len = snprintf(buf, sizeof(buf), "epset:{");
dengyihao's avatar
dengyihao 已提交
504
  for (int i = 0; i < pEpSet->numOfEps; i++) {
dengyihao's avatar
dengyihao 已提交
505
    if (i == pEpSet->numOfEps - 1) {
dengyihao's avatar
dengyihao 已提交
506
      len += snprintf(buf + len, sizeof(buf) - len, "%d. %s:%d", i, pEpSet->eps[i].fqdn, pEpSet->eps[i].port);
dengyihao's avatar
dengyihao 已提交
507 508 509
    } else {
      len += snprintf(buf + len, sizeof(buf) - len, "%d. %s:%d, ", i, pEpSet->eps[i].fqdn, pEpSet->eps[i].port);
    }
dengyihao's avatar
dengyihao 已提交
510
  }
dengyihao's avatar
dengyihao 已提交
511
  len += snprintf(buf + len, sizeof(buf) - len, "}");
dengyihao's avatar
dengyihao 已提交
512
  tTrace("%s, inUse:%d", buf, pEpSet->inUse);
dengyihao's avatar
dengyihao 已提交
513
}
514 515 516 517 518 519 520 521 522 523 524
bool transEpSetIsEqual(SEpSet* a, SEpSet* b) {
  if (a->numOfEps != b->numOfEps || a->inUse != b->inUse) {
    return false;
  }
  for (int i = 0; i < a->numOfEps; i++) {
    if (strncmp(a->eps[i].fqdn, b->eps[i].fqdn, TSDB_FQDN_LEN) != 0 || a->eps[i].port != b->eps[i].port) {
      return false;
    }
  }
  return true;
}
dengyihao's avatar
dengyihao 已提交
525

dengyihao's avatar
dengyihao 已提交
526
static void transInitEnv() {
dengyihao's avatar
dengyihao 已提交
527 528
  refMgt = transOpenRefMgt(50000, transDestoryExHandle);
  instMgt = taosOpenRef(50, rpcCloseImpl);
dengyihao's avatar
dengyihao 已提交
529 530
  uv_os_setenv("UV_TCP_SINGLE_ACCEPT", "1");
}
dengyihao's avatar
dengyihao 已提交
531
static void transDestroyEnv() {
dengyihao's avatar
dengyihao 已提交
532 533
  transCloseRefMgt(refMgt);
  transCloseRefMgt(instMgt);
dengyihao's avatar
dengyihao 已提交
534
}
dengyihao's avatar
dengyihao 已提交
535

dengyihao's avatar
dengyihao 已提交
536 537 538 539
void transInit() {
  // init env
  taosThreadOnce(&transModuleInit, transInitEnv);
}
dengyihao's avatar
dengyihao 已提交
540 541 542 543

int32_t transGetRefMgt() { return refMgt; }
int32_t transGetInstMgt() { return instMgt; }

dengyihao's avatar
dengyihao 已提交
544 545 546 547
void transCleanup() {
  // clean env
  transDestroyEnv();
}
dengyihao's avatar
dengyihao 已提交
548
int32_t transOpenRefMgt(int size, void (*func)(void*)) {
dengyihao's avatar
dengyihao 已提交
549
  // added into once later
dengyihao's avatar
dengyihao 已提交
550
  return taosOpenRef(size, func);
dengyihao's avatar
dengyihao 已提交
551
}
dengyihao's avatar
dengyihao 已提交
552
void transCloseRefMgt(int32_t mgt) {
dengyihao's avatar
dengyihao 已提交
553
  // close ref
dengyihao's avatar
dengyihao 已提交
554
  taosCloseRef(mgt);
dengyihao's avatar
dengyihao 已提交
555
}
dengyihao's avatar
dengyihao 已提交
556
int64_t transAddExHandle(int32_t refMgt, void* p) {
dengyihao's avatar
dengyihao 已提交
557
  // acquire extern handle
dengyihao's avatar
dengyihao 已提交
558
  return taosAddRef(refMgt, p);
dengyihao's avatar
dengyihao 已提交
559
}
dengyihao's avatar
dengyihao 已提交
560
int32_t transRemoveExHandle(int32_t refMgt, int64_t refId) {
dengyihao's avatar
dengyihao 已提交
561
  // acquire extern handle
dengyihao's avatar
dengyihao 已提交
562
  return taosRemoveRef(refMgt, refId);
dengyihao's avatar
dengyihao 已提交
563 564
}

dengyihao's avatar
dengyihao 已提交
565
void* transAcquireExHandle(int32_t refMgt, int64_t refId) {
dengyihao's avatar
dengyihao 已提交
566
  // acquire extern handle
dengyihao's avatar
dengyihao 已提交
567
  return (void*)taosAcquireRef(refMgt, refId);
dengyihao's avatar
dengyihao 已提交
568 569
}

dengyihao's avatar
dengyihao 已提交
570
int32_t transReleaseExHandle(int32_t refMgt, int64_t refId) {
dengyihao's avatar
dengyihao 已提交
571
  // release extern handle
dengyihao's avatar
dengyihao 已提交
572
  return taosReleaseRef(refMgt, refId);
dengyihao's avatar
dengyihao 已提交
573 574 575 576 577 578 579
}
void transDestoryExHandle(void* handle) {
  if (handle == NULL) {
    return;
  }
  taosMemoryFree(handle);
}
dengyihao's avatar
dengyihao 已提交
580
#endif