syncRaftEntry.c 14.0 KB
Newer Older
M
Minghao Li 已提交
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/>.
 */

M
Minghao Li 已提交
16
#include "syncRaftEntry.h"
M
Minghao Li 已提交
17
#include "syncUtil.h"
M
Minghao Li 已提交
18

M
Minghao Li 已提交
19 20
SSyncRaftEntry* syncEntryBuild(uint32_t dataLen) {
  uint32_t        bytes = sizeof(SSyncRaftEntry) + dataLen;
wafwerar's avatar
wafwerar 已提交
21
  SSyncRaftEntry* pEntry = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
22
  ASSERT(pEntry != NULL);
M
Minghao Li 已提交
23 24
  memset(pEntry, 0, bytes);
  pEntry->bytes = bytes;
M
Minghao Li 已提交
25 26 27 28
  pEntry->dataLen = dataLen;
  return pEntry;
}

M
Minghao Li 已提交
29
// step 4. SyncClientRequest => SSyncRaftEntry, add term, index
M
Minghao Li 已提交
30
SSyncRaftEntry* syncEntryBuild2(SyncClientRequest* pMsg, SyncTerm term, SyncIndex index) {
M
Minghao Li 已提交
31
  SSyncRaftEntry* pEntry = syncEntryBuild3(pMsg, term, index);
M
Minghao Li 已提交
32
  ASSERT(pEntry != NULL);
M
Minghao Li 已提交
33 34 35 36

  return pEntry;
}

M
Minghao Li 已提交
37
SSyncRaftEntry* syncEntryBuild3(SyncClientRequest* pMsg, SyncTerm term, SyncIndex index) {
M
Minghao Li 已提交
38
  SSyncRaftEntry* pEntry = syncEntryBuild(pMsg->dataLen);
M
Minghao Li 已提交
39
  ASSERT(pEntry != NULL);
M
Minghao Li 已提交
40

M
Minghao Li 已提交
41 42 43 44 45 46 47 48 49 50 51 52
  pEntry->msgType = pMsg->msgType;
  pEntry->originalRpcType = pMsg->originalRpcType;
  pEntry->seqNum = pMsg->seqNum;
  pEntry->isWeak = pMsg->isWeak;
  pEntry->term = term;
  pEntry->index = index;
  pEntry->dataLen = pMsg->dataLen;
  memcpy(pEntry->data, pMsg->data, pMsg->dataLen);

  return pEntry;
}

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
SSyncRaftEntry* syncEntryBuild4(SRpcMsg* pOriginalMsg, SyncTerm term, SyncIndex index) {
  SSyncRaftEntry* pEntry = syncEntryBuild(pOriginalMsg->contLen);
  ASSERT(pEntry != NULL);

  pEntry->msgType = TDMT_SYNC_CLIENT_REQUEST;
  pEntry->originalRpcType = pOriginalMsg->msgType;
  pEntry->seqNum = 0;
  pEntry->isWeak = 0;
  pEntry->term = term;
  pEntry->index = index;
  pEntry->dataLen = pOriginalMsg->contLen;
  memcpy(pEntry->data, pOriginalMsg->pCont, pOriginalMsg->contLen);

  return pEntry;
}

M
Minghao Li 已提交
69 70 71 72 73 74 75 76 77
SSyncRaftEntry* syncEntryBuildNoop(SyncTerm term, SyncIndex index, int32_t vgId) {
  // init rpcMsg
  SMsgHead head;
  head.vgId = vgId;
  head.contLen = sizeof(SMsgHead);
  SRpcMsg rpcMsg;
  memset(&rpcMsg, 0, sizeof(SRpcMsg));
  rpcMsg.contLen = head.contLen;
  rpcMsg.pCont = rpcMallocCont(rpcMsg.contLen);
78
  rpcMsg.msgType = TDMT_SYNC_NOOP;
M
Minghao Li 已提交
79 80 81
  memcpy(rpcMsg.pCont, &head, sizeof(head));

  SSyncRaftEntry* pEntry = syncEntryBuild(rpcMsg.contLen);
M
Minghao Li 已提交
82
  ASSERT(pEntry != NULL);
M
Minghao Li 已提交
83

84 85
  pEntry->msgType = TDMT_SYNC_CLIENT_REQUEST;
  pEntry->originalRpcType = TDMT_SYNC_NOOP;
M
Minghao Li 已提交
86 87
  pEntry->seqNum = 0;
  pEntry->isWeak = 0;
M
Minghao Li 已提交
88 89
  pEntry->term = term;
  pEntry->index = index;
M
Minghao Li 已提交
90

M
Minghao Li 已提交
91
  ASSERT(pEntry->dataLen == rpcMsg.contLen);
M
Minghao Li 已提交
92 93
  memcpy(pEntry->data, rpcMsg.pCont, rpcMsg.contLen);
  rpcFreeCont(rpcMsg.pCont);
M
Minghao Li 已提交
94 95 96 97

  return pEntry;
}

M
Minghao Li 已提交
98 99
void syncEntryDestory(SSyncRaftEntry* pEntry) {
  if (pEntry != NULL) {
wafwerar's avatar
wafwerar 已提交
100
    taosMemoryFree(pEntry);
M
Minghao Li 已提交
101 102 103
  }
}

M
Minghao Li 已提交
104
// step 5. SSyncRaftEntry => bin, to raft log
M
Minghao Li 已提交
105
char* syncEntrySerialize(const SSyncRaftEntry* pEntry, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
106
  char* buf = taosMemoryMalloc(pEntry->bytes);
M
Minghao Li 已提交
107
  ASSERT(buf != NULL);
M
Minghao Li 已提交
108
  memcpy(buf, pEntry, pEntry->bytes);
M
Minghao Li 已提交
109 110 111 112
  if (len != NULL) {
    *len = pEntry->bytes;
  }
  return buf;
M
Minghao Li 已提交
113 114
}

M
Minghao Li 已提交
115
// step 6. bin => SSyncRaftEntry, from raft log
M
Minghao Li 已提交
116 117
SSyncRaftEntry* syncEntryDeserialize(const char* buf, uint32_t len) {
  uint32_t        bytes = *((uint32_t*)buf);
wafwerar's avatar
wafwerar 已提交
118
  SSyncRaftEntry* pEntry = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
119
  ASSERT(pEntry != NULL);
M
Minghao Li 已提交
120
  memcpy(pEntry, buf, len);
M
Minghao Li 已提交
121
  ASSERT(len == pEntry->bytes);
M
Minghao Li 已提交
122
  return pEntry;
M
Minghao Li 已提交
123 124 125
}

cJSON* syncEntry2Json(const SSyncRaftEntry* pEntry) {
126
  char   u64buf[128] = {0};
M
Minghao Li 已提交
127 128
  cJSON* pRoot = cJSON_CreateObject();

M
Minghao Li 已提交
129 130 131 132
  if (pEntry != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pEntry->bytes);
    cJSON_AddNumberToObject(pRoot, "msgType", pEntry->msgType);
    cJSON_AddNumberToObject(pRoot, "originalRpcType", pEntry->originalRpcType);
S
Shengliang Guan 已提交
133
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pEntry->seqNum);
M
Minghao Li 已提交
134 135
    cJSON_AddStringToObject(pRoot, "seqNum", u64buf);
    cJSON_AddNumberToObject(pRoot, "isWeak", pEntry->isWeak);
S
Shengliang Guan 已提交
136
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pEntry->term);
M
Minghao Li 已提交
137
    cJSON_AddStringToObject(pRoot, "term", u64buf);
S
Shengliang Guan 已提交
138
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pEntry->index);
M
Minghao Li 已提交
139 140 141 142 143 144
    cJSON_AddStringToObject(pRoot, "index", u64buf);
    cJSON_AddNumberToObject(pRoot, "dataLen", pEntry->dataLen);

    char* s;
    s = syncUtilprintBin((char*)(pEntry->data), pEntry->dataLen);
    cJSON_AddStringToObject(pRoot, "data", s);
wafwerar's avatar
wafwerar 已提交
145
    taosMemoryFree(s);
M
Minghao Li 已提交
146

M
Minghao Li 已提交
147 148
    s = syncUtilprintBin2((char*)(pEntry->data), pEntry->dataLen);
    cJSON_AddStringToObject(pRoot, "data2", s);
wafwerar's avatar
wafwerar 已提交
149
    taosMemoryFree(s);
M
Minghao Li 已提交
150
  }
M
Minghao Li 已提交
151

M
Minghao Li 已提交
152 153 154 155 156 157 158 159 160 161
  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SSyncRaftEntry", pRoot);
  return pJson;
}

char* syncEntry2Str(const SSyncRaftEntry* pEntry) {
  cJSON* pJson = syncEntry2Json(pEntry);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
M
Minghao Li 已提交
162 163
}

M
Minghao Li 已提交
164 165 166 167 168 169 170 171 172
// step 7. SSyncRaftEntry => original SRpcMsg, commit to user, delete seqNum, isWeak, term, index
void syncEntry2OriginalRpc(const SSyncRaftEntry* pEntry, SRpcMsg* pRpcMsg) {
  memset(pRpcMsg, 0, sizeof(*pRpcMsg));
  pRpcMsg->msgType = pEntry->originalRpcType;
  pRpcMsg->contLen = pEntry->dataLen;
  pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen);
  memcpy(pRpcMsg->pCont, pEntry->data, pRpcMsg->contLen);
}

M
Minghao Li 已提交
173
// for debug ----------------------
M
Minghao Li 已提交
174 175
void syncEntryPrint(const SSyncRaftEntry* pObj) {
  char* serialized = syncEntry2Str(pObj);
176
  printf("syncEntryPrint | len:%zu | %s \n", strlen(serialized), serialized);
M
Minghao Li 已提交
177
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
178
  taosMemoryFree(serialized);
M
Minghao Li 已提交
179 180
}

M
Minghao Li 已提交
181 182
void syncEntryPrint2(char* s, const SSyncRaftEntry* pObj) {
  char* serialized = syncEntry2Str(pObj);
183
  printf("syncEntryPrint2 | len:%zu | %s | %s \n", strlen(serialized), s, serialized);
M
Minghao Li 已提交
184
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
185
  taosMemoryFree(serialized);
M
Minghao Li 已提交
186 187
}

M
Minghao Li 已提交
188 189
void syncEntryLog(const SSyncRaftEntry* pObj) {
  char* serialized = syncEntry2Str(pObj);
190
  sTrace("syncEntryLog | len:%zu | %s", strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
191
  taosMemoryFree(serialized);
M
Minghao Li 已提交
192 193
}

M
Minghao Li 已提交
194 195
void syncEntryLog2(char* s, const SSyncRaftEntry* pObj) {
  char* serialized = syncEntry2Str(pObj);
196
  sTrace("syncEntryLog2 | len:%zu | %s | %s", strlen(serialized), s, serialized);
wafwerar's avatar
wafwerar 已提交
197
  taosMemoryFree(serialized);
198
}
M
Minghao Li 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248

//-----------------------------------
SRaftEntryCache* raftCacheCreate(SSyncNode* pSyncNode, int32_t maxCount) {
  SRaftEntryCache* pCache = taosMemoryMalloc(sizeof(SRaftEntryCache));
  if (pCache == NULL) {
    sError("vgId:%d raft cache create error", pSyncNode->vgId);
    return NULL;
  }

  pCache->pEntryHash =
      taosHashInit(sizeof(SyncIndex), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
  if (pCache->pEntryHash == NULL) {
    sError("vgId:%d raft cache create hash error", pSyncNode->vgId);
    return NULL;
  }

  taosThreadMutexInit(&(pCache->mutex), NULL);
  pCache->maxCount = maxCount;
  pCache->currentCount = 0;
  pCache->pSyncNode = pSyncNode;

  return pCache;
}

void raftCacheDestroy(SRaftEntryCache* pCache) {
  if (pCache != NULL) {
    taosThreadMutexLock(&(pCache->mutex));
    taosHashCleanup(pCache->pEntryHash);
    taosThreadMutexUnlock(&(pCache->mutex));
    taosThreadMutexDestroy(&(pCache->mutex));
    taosMemoryFree(pCache);
  }
}

// success, return 1
// max count, return 0
// error, return -1
int32_t raftCachePutEntry(struct SRaftEntryCache* pCache, SSyncRaftEntry* pEntry) {
  taosThreadMutexLock(&(pCache->mutex));

  if (pCache->currentCount >= pCache->maxCount) {
    taosThreadMutexUnlock(&(pCache->mutex));
    return 0;
  }

  taosHashPut(pCache->pEntryHash, &(pEntry->index), sizeof(pEntry->index), pEntry, pEntry->bytes);
  ++(pCache->currentCount);

  do {
    char eventLog[128];
S
Shengliang Guan 已提交
249
    snprintf(eventLog, sizeof(eventLog), "raft cache add, type:%s,%d, type2:%s,%d, index:%" PRId64 ", bytes:%d",
M
Minghao Li 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
             TMSG_INFO(pEntry->msgType), pEntry->msgType, TMSG_INFO(pEntry->originalRpcType), pEntry->originalRpcType,
             pEntry->index, pEntry->bytes);
    syncNodeEventLog(pCache->pSyncNode, eventLog);
  } while (0);

  taosThreadMutexUnlock(&(pCache->mutex));
  return 1;
}

// success, return 0
// error, return -1
// not exist, return -1, terrno = TSDB_CODE_WAL_LOG_NOT_EXIST
int32_t raftCacheGetEntry(struct SRaftEntryCache* pCache, SyncIndex index, SSyncRaftEntry** ppEntry) {
  if (ppEntry == NULL) {
    return -1;
  }
  *ppEntry = NULL;

  taosThreadMutexLock(&(pCache->mutex));
  void* pTmp = taosHashGet(pCache->pEntryHash, &index, sizeof(index));
  if (pTmp != NULL) {
    SSyncRaftEntry* pEntry = pTmp;
    *ppEntry = taosMemoryMalloc(pEntry->bytes);
    memcpy(*ppEntry, pTmp, pEntry->bytes);

    do {
      char eventLog[128];
S
Shengliang Guan 已提交
277
      snprintf(eventLog, sizeof(eventLog), "raft cache get, type:%s,%d, type2:%s,%d, index:%" PRId64,
M
Minghao Li 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
               TMSG_INFO((*ppEntry)->msgType), (*ppEntry)->msgType, TMSG_INFO((*ppEntry)->originalRpcType),
               (*ppEntry)->originalRpcType, (*ppEntry)->index);
      syncNodeEventLog(pCache->pSyncNode, eventLog);
    } while (0);

    taosThreadMutexUnlock(&(pCache->mutex));
    return 0;
  }

  taosThreadMutexUnlock(&(pCache->mutex));
  terrno = TSDB_CODE_WAL_LOG_NOT_EXIST;
  return -1;
}

// success, return 0
// error, return -1
// not exist, return -1, terrno = TSDB_CODE_WAL_LOG_NOT_EXIST
int32_t raftCacheGetEntryP(struct SRaftEntryCache* pCache, SyncIndex index, SSyncRaftEntry** ppEntry) {
  if (ppEntry == NULL) {
    return -1;
  }
  *ppEntry = NULL;

  taosThreadMutexLock(&(pCache->mutex));
  void* pTmp = taosHashGet(pCache->pEntryHash, &index, sizeof(index));
  if (pTmp != NULL) {
    SSyncRaftEntry* pEntry = pTmp;
    *ppEntry = pEntry;

    do {
      char eventLog[128];
S
Shengliang Guan 已提交
309
      snprintf(eventLog, sizeof(eventLog), "raft cache get, type:%s,%d, type2:%s,%d, index:%" PRId64,
M
Minghao Li 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
               TMSG_INFO((*ppEntry)->msgType), (*ppEntry)->msgType, TMSG_INFO((*ppEntry)->originalRpcType),
               (*ppEntry)->originalRpcType, (*ppEntry)->index);
      syncNodeEventLog(pCache->pSyncNode, eventLog);
    } while (0);

    taosThreadMutexUnlock(&(pCache->mutex));
    return 0;
  }

  taosThreadMutexUnlock(&(pCache->mutex));
  terrno = TSDB_CODE_WAL_LOG_NOT_EXIST;
  return -1;
}

int32_t raftCacheDelEntry(struct SRaftEntryCache* pCache, SyncIndex index) {
  taosThreadMutexLock(&(pCache->mutex));
  taosHashRemove(pCache->pEntryHash, &index, sizeof(index));
M
Minghao Li 已提交
327
  --(pCache->currentCount);
M
Minghao Li 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
  taosThreadMutexUnlock(&(pCache->mutex));
  return 0;
}

int32_t raftCacheGetAndDel(struct SRaftEntryCache* pCache, SyncIndex index, SSyncRaftEntry** ppEntry) {
  if (ppEntry == NULL) {
    return -1;
  }
  *ppEntry = NULL;

  taosThreadMutexLock(&(pCache->mutex));
  void* pTmp = taosHashGet(pCache->pEntryHash, &index, sizeof(index));
  if (pTmp != NULL) {
    SSyncRaftEntry* pEntry = pTmp;
    *ppEntry = taosMemoryMalloc(pEntry->bytes);
    memcpy(*ppEntry, pTmp, pEntry->bytes);

    do {
      char eventLog[128];
S
Shengliang Guan 已提交
347
      snprintf(eventLog, sizeof(eventLog), "raft cache get-and-del, type:%s,%d, type2:%s,%d, index:%" PRId64,
M
Minghao Li 已提交
348 349 350 351 352 353
               TMSG_INFO((*ppEntry)->msgType), (*ppEntry)->msgType, TMSG_INFO((*ppEntry)->originalRpcType),
               (*ppEntry)->originalRpcType, (*ppEntry)->index);
      syncNodeEventLog(pCache->pSyncNode, eventLog);
    } while (0);

    taosHashRemove(pCache->pEntryHash, &index, sizeof(index));
M
Minghao Li 已提交
354 355
    --(pCache->currentCount);

M
Minghao Li 已提交
356 357 358 359 360 361 362 363 364 365 366 367
    taosThreadMutexUnlock(&(pCache->mutex));
    return 0;
  }

  taosThreadMutexUnlock(&(pCache->mutex));
  terrno = TSDB_CODE_WAL_LOG_NOT_EXIST;
  return -1;
}

int32_t raftCacheClear(struct SRaftEntryCache* pCache) {
  taosThreadMutexLock(&(pCache->mutex));
  taosHashClear(pCache->pEntryHash);
M
Minghao Li 已提交
368
  pCache->currentCount = 0;
M
Minghao Li 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
  taosThreadMutexUnlock(&(pCache->mutex));
  return 0;
}

//-----------------------------------
cJSON* raftCache2Json(SRaftEntryCache* pCache) {
  char   u64buf[128] = {0};
  cJSON* pRoot = cJSON_CreateObject();

  if (pCache != NULL) {
    taosThreadMutexLock(&(pCache->mutex));

    snprintf(u64buf, sizeof(u64buf), "%p", pCache->pSyncNode);
    cJSON_AddStringToObject(pRoot, "pSyncNode", u64buf);
    cJSON_AddNumberToObject(pRoot, "currentCount", pCache->currentCount);
    cJSON_AddNumberToObject(pRoot, "maxCount", pCache->maxCount);
    cJSON* pEntries = cJSON_CreateArray();
    cJSON_AddItemToObject(pRoot, "entries", pEntries);

    SSyncRaftEntry* pIter = (SSyncRaftEntry*)taosHashIterate(pCache->pEntryHash, NULL);
    if (pIter != NULL) {
      SSyncRaftEntry* pEntry = (SSyncRaftEntry*)pIter;
      cJSON_AddItemToArray(pEntries, syncEntry2Json(pEntry));
    }
    while (pIter) {
      pIter = taosHashIterate(pCache->pEntryHash, pIter);
      if (pIter != NULL) {
        SSyncRaftEntry* pEntry = (SSyncRaftEntry*)pIter;
        cJSON_AddItemToArray(pEntries, syncEntry2Json(pEntry));
      }
    }

    taosThreadMutexUnlock(&(pCache->mutex));
  }

  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SRaftEntryCache", pRoot);
  return pJson;
}

char* raftCache2Str(SRaftEntryCache* pCache) {
  cJSON* pJson = raftCache2Json(pCache);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

void raftCachePrint(SRaftEntryCache* pCache) {
  char* serialized = raftCache2Str(pCache);
S
Shengliang Guan 已提交
418
  printf("raftCachePrint | len:%" PRIu64 " | %s \n", strlen(serialized), serialized);
M
Minghao Li 已提交
419 420 421 422 423 424
  fflush(NULL);
  taosMemoryFree(serialized);
}

void raftCachePrint2(char* s, SRaftEntryCache* pCache) {
  char* serialized = raftCache2Str(pCache);
S
Shengliang Guan 已提交
425
  printf("raftCachePrint2 | len:%" PRIu64 " | %s | %s \n", strlen(serialized), s, serialized);
M
Minghao Li 已提交
426 427 428 429 430 431
  fflush(NULL);
  taosMemoryFree(serialized);
}

void raftCacheLog(SRaftEntryCache* pCache) {
  char* serialized = raftCache2Str(pCache);
S
Shengliang Guan 已提交
432
  sTrace("raftCacheLog | len:%" PRIu64 " | %s", strlen(serialized), serialized);
M
Minghao Li 已提交
433 434 435 436 437 438
  taosMemoryFree(serialized);
}

void raftCacheLog2(char* s, SRaftEntryCache* pCache) {
  if (gRaftDetailLog) {
    char* serialized = raftCache2Str(pCache);
S
Shengliang Guan 已提交
439
    sTraceLong("raftCacheLog2 | len:%" PRIu64 " | %s | %s", strlen(serialized), s, serialized);
M
Minghao Li 已提交
440 441 442
    taosMemoryFree(serialized);
  }
}