syncRaftEntry.c 5.4 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 23 24
  assert(pEntry != NULL);
  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 32 33 34 35 36 37
  SSyncRaftEntry* pEntry = syncEntryBuild3(pMsg, term, index, SYNC_RAFT_ENTRY_DATA);
  assert(pEntry != NULL);

  return pEntry;
}

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

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

  return pEntry;
}

M
Minghao Li 已提交
54 55 56 57 58 59 60 61 62 63
SSyncRaftEntry* syncEntryBuildNoop(SyncTerm term, SyncIndex index) {
  SSyncRaftEntry* pEntry = syncEntryBuild(0);
  assert(pEntry != NULL);
  pEntry->term = term;
  pEntry->index = index;
  pEntry->entryType = SYNC_RAFT_ENTRY_NOOP;

  return pEntry;
}

M
Minghao Li 已提交
64 65
void syncEntryDestory(SSyncRaftEntry* pEntry) {
  if (pEntry != NULL) {
wafwerar's avatar
wafwerar 已提交
66
    taosMemoryFree(pEntry);
M
Minghao Li 已提交
67 68 69
  }
}

M
Minghao Li 已提交
70
// step 5. SSyncRaftEntry => bin, to raft log
M
Minghao Li 已提交
71
char* syncEntrySerialize(const SSyncRaftEntry* pEntry, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
72
  char* buf = taosMemoryMalloc(pEntry->bytes);
M
Minghao Li 已提交
73
  assert(buf != NULL);
M
Minghao Li 已提交
74
  memcpy(buf, pEntry, pEntry->bytes);
M
Minghao Li 已提交
75 76 77 78
  if (len != NULL) {
    *len = pEntry->bytes;
  }
  return buf;
M
Minghao Li 已提交
79 80
}

M
Minghao Li 已提交
81
// step 6. bin => SSyncRaftEntry, from raft log
M
Minghao Li 已提交
82 83
SSyncRaftEntry* syncEntryDeserialize(const char* buf, uint32_t len) {
  uint32_t        bytes = *((uint32_t*)buf);
wafwerar's avatar
wafwerar 已提交
84
  SSyncRaftEntry* pEntry = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
85
  assert(pEntry != NULL);
M
Minghao Li 已提交
86 87
  memcpy(pEntry, buf, len);
  assert(len == pEntry->bytes);
M
Minghao Li 已提交
88
  return pEntry;
M
Minghao Li 已提交
89 90 91
}

cJSON* syncEntry2Json(const SSyncRaftEntry* pEntry) {
M
Minghao Li 已提交
92
  char   u64buf[128];
M
Minghao Li 已提交
93 94
  cJSON* pRoot = cJSON_CreateObject();

M
Minghao Li 已提交
95 96 97 98 99 100 101 102 103 104 105
  if (pEntry != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pEntry->bytes);
    cJSON_AddNumberToObject(pRoot, "msgType", pEntry->msgType);
    cJSON_AddNumberToObject(pRoot, "originalRpcType", pEntry->originalRpcType);
    snprintf(u64buf, sizeof(u64buf), "%lu", pEntry->seqNum);
    cJSON_AddStringToObject(pRoot, "seqNum", u64buf);
    cJSON_AddNumberToObject(pRoot, "isWeak", pEntry->isWeak);
    snprintf(u64buf, sizeof(u64buf), "%lu", pEntry->term);
    cJSON_AddStringToObject(pRoot, "term", u64buf);
    snprintf(u64buf, sizeof(u64buf), "%lu", pEntry->index);
    cJSON_AddStringToObject(pRoot, "index", u64buf);
M
Minghao Li 已提交
106
    cJSON_AddNumberToObject(pRoot, "entryType", pEntry->entryType);
M
Minghao Li 已提交
107 108 109 110 111
    cJSON_AddNumberToObject(pRoot, "dataLen", pEntry->dataLen);

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

M
Minghao Li 已提交
114 115
    s = syncUtilprintBin2((char*)(pEntry->data), pEntry->dataLen);
    cJSON_AddStringToObject(pRoot, "data2", s);
wafwerar's avatar
wafwerar 已提交
116
    taosMemoryFree(s);
M
Minghao Li 已提交
117
  }
M
Minghao Li 已提交
118

M
Minghao Li 已提交
119 120 121 122 123 124 125 126 127 128
  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 已提交
129 130
}

M
Minghao Li 已提交
131 132 133 134 135 136 137 138 139
// 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 已提交
140
// for debug ----------------------
M
Minghao Li 已提交
141 142
void syncEntryPrint(const SSyncRaftEntry* pObj) {
  char* serialized = syncEntry2Str(pObj);
143
  printf("syncEntryPrint | len:%zu | %s \n", strlen(serialized), serialized);
M
Minghao Li 已提交
144
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
145
  taosMemoryFree(serialized);
M
Minghao Li 已提交
146 147
}

M
Minghao Li 已提交
148 149
void syncEntryPrint2(char* s, const SSyncRaftEntry* pObj) {
  char* serialized = syncEntry2Str(pObj);
150
  printf("syncEntryPrint2 | len:%zu | %s | %s \n", strlen(serialized), s, serialized);
M
Minghao Li 已提交
151
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
152
  taosMemoryFree(serialized);
M
Minghao Li 已提交
153 154
}

M
Minghao Li 已提交
155 156
void syncEntryLog(const SSyncRaftEntry* pObj) {
  char* serialized = syncEntry2Str(pObj);
157
  sTrace("syncEntryLog | len:%zu | %s", strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
158
  taosMemoryFree(serialized);
M
Minghao Li 已提交
159 160
}

M
Minghao Li 已提交
161 162
void syncEntryLog2(char* s, const SSyncRaftEntry* pObj) {
  char* serialized = syncEntry2Str(pObj);
163
  sTrace("syncEntryLog2 | len:%zu | %s | %s", strlen(serialized), s, serialized);
wafwerar's avatar
wafwerar 已提交
164
  taosMemoryFree(serialized);
165
}