transComm.c 9.6 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
// static TdThreadOnce transModuleInit = PTHREAD_ONCE_INIT;

dengyihao's avatar
dengyihao 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34
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 已提交
35

dengyihao's avatar
dengyihao 已提交
36 37 38 39 40 41 42 43 44 45 46
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 已提交
47

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

wafwerar's avatar
wafwerar 已提交
57
  char* buf = taosMemoryMalloc(len + overhead + 8);  // 8 extra bytes
dengyihao's avatar
dengyihao 已提交
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
  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 已提交
83
  taosMemoryFree(buf);
dengyihao's avatar
dengyihao 已提交
84 85 86 87 88 89 90 91 92 93 94 95
  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 已提交
96 97 98 99
void transFreeMsg(void* msg) {
  if (msg == NULL) {
    return;
  }
wafwerar's avatar
wafwerar 已提交
100
  taosMemoryFree((char*)msg - sizeof(STransMsgHead));
dengyihao's avatar
dengyihao 已提交
101
}
dengyihao's avatar
dengyihao 已提交
102 103 104 105 106 107 108

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

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

    uvBuf->base = p->buf;
    uvBuf->len = CAPACITY;
dengyihao's avatar
dengyihao 已提交
130 131 132
  } else if (p->total == -1 && p->len < CAPACITY) {
    uvBuf->base = p->buf + p->len;
    uvBuf->len = CAPACITY - p->len;
dengyihao's avatar
dengyihao 已提交
133
  } else {
dengyihao's avatar
dengyihao 已提交
134
    p->cap = p->total;
wafwerar's avatar
wafwerar 已提交
135
    p->buf = taosMemoryRealloc(p->buf, p->cap);
dengyihao's avatar
dengyihao 已提交
136

dengyihao's avatar
dengyihao 已提交
137 138 139 140 141
    uvBuf->base = p->buf + p->len;
    uvBuf->len = p->cap - p->len;
  }
  return 0;
}
dengyihao's avatar
dengyihao 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154
// 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 已提交
155
int transPackMsg(STransMsgHead* msgHead, bool sercured, bool auth) { return 0; }
dengyihao's avatar
dengyihao 已提交
156

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

  return 0;
dengyihao's avatar
dengyihao 已提交
165
}
dengyihao's avatar
dengyihao 已提交
166

dengyihao's avatar
dengyihao 已提交
167 168 169 170 171 172
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 已提交
173
SAsyncPool* transCreateAsyncPool(uv_loop_t* loop, int sz, void* arg, AsyncCB cb) {
wafwerar's avatar
wafwerar 已提交
174
  SAsyncPool* pool = taosMemoryCalloc(1, sizeof(SAsyncPool));
dengyihao's avatar
dengyihao 已提交
175 176
  pool->index = 0;
  pool->nAsync = sz;
wafwerar's avatar
wafwerar 已提交
177
  pool->asyncs = taosMemoryCalloc(1, sizeof(uv_async_t) * pool->nAsync);
dengyihao's avatar
dengyihao 已提交
178 179 180 181

  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 已提交
182

wafwerar's avatar
wafwerar 已提交
183
    SAsyncItem* item = taosMemoryCalloc(1, sizeof(SAsyncItem));
dengyihao's avatar
dengyihao 已提交
184 185
    item->pThrd = arg;
    QUEUE_INIT(&item->qmsg);
wafwerar's avatar
wafwerar 已提交
186
    taosThreadMutexInit(&item->mtx, NULL);
dengyihao's avatar
dengyihao 已提交
187 188

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

  int64_t st = taosGetTimestampUs();
wafwerar's avatar
wafwerar 已提交
214
  taosThreadMutexLock(&item->mtx);
dengyihao's avatar
dengyihao 已提交
215
  QUEUE_PUSH(&item->qmsg, q);
wafwerar's avatar
wafwerar 已提交
216
  taosThreadMutexUnlock(&item->mtx);
dengyihao's avatar
dengyihao 已提交
217 218 219 220 221
  int64_t el = taosGetTimestampUs() - st;
  if (el > 50) {
    // tInfo("lock and unlock cost: %d", (int)el);
  }
  return uv_async_send(async);
dengyihao's avatar
dengyihao 已提交
222
}
dengyihao's avatar
dengyihao 已提交
223

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

  STransCtxVal* iter = taosHashIterate(ctx->args, NULL);
  while (iter) {
wafwerar's avatar
wafwerar 已提交
235
    iter->freeFunc(iter->val);
dengyihao's avatar
dengyihao 已提交
236 237
    iter = taosHashIterate(ctx->args, iter);
  }
dengyihao's avatar
dengyihao 已提交
238

dengyihao's avatar
dengyihao 已提交
239
  taosHashCleanup(ctx->args);
U
ubuntu 已提交
240
  ctx->args = NULL;
dengyihao's avatar
dengyihao 已提交
241 242 243 244 245
}

void transCtxMerge(STransCtx* dst, STransCtx* src) {
  if (dst->args == NULL) {
    dst->args = src->args;
dengyihao's avatar
dengyihao 已提交
246
    dst->brokenVal = src->brokenVal;
dengyihao's avatar
dengyihao 已提交
247 248 249 250 251 252 253 254 255 256 257 258
    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) {
wafwerar's avatar
wafwerar 已提交
259
      dVal->freeFunc(dVal->val);
dengyihao's avatar
dengyihao 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273
    }
    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 已提交
274
  void* ret = NULL;
D
dapan1121 已提交
275 276
  (*cVal->clone)(cVal->val, &ret);
  return ret;
dengyihao's avatar
dengyihao 已提交
277
}
dengyihao's avatar
dengyihao 已提交
278
void* transCtxDumpBrokenlinkVal(STransCtx* ctx, int32_t* msgType) {
dengyihao's avatar
dengyihao 已提交
279
  void* ret = NULL;
dengyihao's avatar
dengyihao 已提交
280
  if (ctx->brokenVal.clone == NULL) {
dengyihao's avatar
dengyihao 已提交
281
    return ret;
dengyihao's avatar
dengyihao 已提交
282
  }
D
dapan1121 已提交
283
  (*ctx->brokenVal.clone)(ctx->brokenVal.val, &ret);
dengyihao's avatar
dengyihao 已提交
284 285 286

  *msgType = ctx->brokenVal.msgType;

D
dapan1121 已提交
287
  return ret;
dengyihao's avatar
dengyihao 已提交
288
}
dengyihao's avatar
dengyihao 已提交
289

wafwerar's avatar
wafwerar 已提交
290
void transQueueInit(STransQueue* queue, void (*freeFunc)(const void* arg)) {
dengyihao's avatar
dengyihao 已提交
291
  queue->q = taosArrayInit(2, sizeof(void*));
wafwerar's avatar
wafwerar 已提交
292
  queue->freeFunc = (void (*)(const void*))freeFunc;
dengyihao's avatar
dengyihao 已提交
293 294
}
bool transQueuePush(STransQueue* queue, void* arg) {
dengyihao's avatar
dengyihao 已提交
295 296 297
  if (queue->q == NULL) {
    return true;
  }
dengyihao's avatar
dengyihao 已提交
298 299 300 301 302 303 304
  taosArrayPush(queue->q, &arg);
  if (taosArrayGetSize(queue->q) > 1) {
    return false;
  }
  return true;
}
void* transQueuePop(STransQueue* queue) {
dengyihao's avatar
dengyihao 已提交
305
  if (queue->q == NULL || taosArrayGetSize(queue->q) == 0) {
dengyihao's avatar
dengyihao 已提交
306 307 308 309 310 311
    return NULL;
  }
  void* ptr = taosArrayGetP(queue->q, 0);
  taosArrayRemove(queue->q, 0);
  return ptr;
}
dengyihao's avatar
dengyihao 已提交
312
int32_t transQueueSize(STransQueue* queue) {
dengyihao's avatar
dengyihao 已提交
313 314 315
  if (queue->q == NULL) {
    return 0;
  }
dengyihao's avatar
dengyihao 已提交
316 317 318
  return taosArrayGetSize(queue->q);
}
void* transQueueGet(STransQueue* queue, int i) {
dengyihao's avatar
dengyihao 已提交
319
  if (queue->q == NULL || taosArrayGetSize(queue->q) == 0) {
dengyihao's avatar
dengyihao 已提交
320 321 322 323 324
    return NULL;
  }
  if (i >= taosArrayGetSize(queue->q)) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
325

dengyihao's avatar
dengyihao 已提交
326 327 328 329 330
  void* ptr = taosArrayGetP(queue->q, i);
  return ptr;
}

void* transQueueRm(STransQueue* queue, int i) {
dengyihao's avatar
dengyihao 已提交
331
  if (queue->q == NULL || taosArrayGetSize(queue->q) == 0) {
dengyihao's avatar
dengyihao 已提交
332 333
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
334 335 336 337 338
  if (i >= taosArrayGetSize(queue->q)) {
    return NULL;
  }
  void* ptr = taosArrayGetP(queue->q, i);
  taosArrayRemove(queue->q, i);
dengyihao's avatar
dengyihao 已提交
339 340
  return ptr;
}
dengyihao's avatar
dengyihao 已提交
341

dengyihao's avatar
dengyihao 已提交
342
bool transQueueEmpty(STransQueue* queue) {
dengyihao's avatar
dengyihao 已提交
343 344 345
  if (queue->q == NULL) {
    return true;
  }
dengyihao's avatar
dengyihao 已提交
346 347 348
  return taosArrayGetSize(queue->q) == 0;
}
void transQueueClear(STransQueue* queue) {
wafwerar's avatar
wafwerar 已提交
349
  if (queue->freeFunc != NULL) {
dengyihao's avatar
dengyihao 已提交
350 351
    for (int i = 0; i < taosArrayGetSize(queue->q); i++) {
      void* p = taosArrayGetP(queue->q, i);
wafwerar's avatar
wafwerar 已提交
352
      queue->freeFunc(p);
dengyihao's avatar
dengyihao 已提交
353 354 355 356 357 358 359 360
    }
  }
  taosArrayClear(queue->q);
}
void transQueueDestroy(STransQueue* queue) {
  transQueueClear(queue);
  taosArrayDestroy(queue->q);
}
dengyihao's avatar
dengyihao 已提交
361
#endif