syncUtil.c 8.0 KB
Newer Older
M
Minghao Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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 "syncUtil.h"
17 18
#include <stdio.h>

M
Minghao Li 已提交
19
#include "syncEnv.h"
M
Minghao Li 已提交
20

M
Minghao Li 已提交
21 22
void addEpIntoEpSet(SEpSet* pEpSet, const char* fqdn, uint16_t port);

M
Minghao Li 已提交
23 24 25
// ---- encode / decode
uint64_t syncUtilAddr2U64(const char* host, uint16_t port) {
  uint64_t u64;
26 27 28 29 30 31 32 33

  uint32_t hostU32 = taosGetIpv4FromFqdn(host);
  if (hostU32 == (uint32_t)-1) {
    sError("Get IP address error");
    return -1;
  }

  /*
34
  uint32_t hostU32 = (uint32_t)taosInetAddr(host);
35 36 37 38 39 40 41 42 43 44 45 46 47 48
  if (hostU32 == (uint32_t)-1) {
    struct hostent* hostEnt = gethostbyname(host);
    if (hostEnt == NULL) {
      sError("Get IP address error");
      return -1;
    }

    const char* newHost = taosInetNtoa(*(struct in_addr*)(hostEnt->h_addr_list[0]));
    hostU32 = (uint32_t)taosInetAddr(newHost);
    if (hostU32 == (uint32_t)-1) {
      sError("change %s to id, error", newHost);
    }
    // ASSERT(hostU32 != (uint32_t)-1);
  }
49 50
  */

M
Minghao Li 已提交
51 52 53
  u64 = (((uint64_t)hostU32) << 32) | (((uint32_t)port) << 16);
  return u64;
}
M
Minghao Li 已提交
54

M
Minghao Li 已提交
55 56
void syncUtilU642Addr(uint64_t u64, char* host, size_t len, uint16_t* port) {
  uint32_t hostU32 = (uint32_t)((u64 >> 32) & 0x00000000FFFFFFFF);
M
Minghao Li 已提交
57

M
Minghao Li 已提交
58 59
  struct in_addr addr;
  addr.s_addr = hostU32;
wafwerar's avatar
wafwerar 已提交
60
  taosInetNtoa(addr, host, len);
M
Minghao Li 已提交
61 62 63 64 65
  *port = (uint16_t)((u64 & 0x00000000FFFF0000) >> 16);
}

void syncUtilnodeInfo2EpSet(const SNodeInfo* pNodeInfo, SEpSet* pEpSet) {
  pEpSet->inUse = 0;
M
Minghao Li 已提交
66
  pEpSet->numOfEps = 0;
M
Minghao Li 已提交
67 68 69 70
  addEpIntoEpSet(pEpSet, pNodeInfo->nodeFqdn, pNodeInfo->nodePort);
}

void syncUtilraftId2EpSet(const SRaftId* raftId, SEpSet* pEpSet) {
71
  char     host[TSDB_FQDN_LEN] = {0};
M
Minghao Li 已提交
72 73 74
  uint16_t port;

  syncUtilU642Addr(raftId->addr, host, sizeof(host), &port);
M
sync io  
Minghao Li 已提交
75 76 77 78 79

  /*
    pEpSet->numOfEps = 1;
    pEpSet->inUse = 0;
    pEpSet->eps[0].port = port;
M
Minghao Li 已提交
80
    snprintf(pEpSet->eps[0].fqdn, sizeof(pEpSet->eps[0].fqdn), "%s", host);
M
sync io  
Minghao Li 已提交
81
  */
M
Minghao Li 已提交
82
  pEpSet->inUse = 0;
M
Minghao Li 已提交
83
  pEpSet->numOfEps = 0;
M
Minghao Li 已提交
84 85 86 87
  addEpIntoEpSet(pEpSet, host, port);
}

void syncUtilnodeInfo2raftId(const SNodeInfo* pNodeInfo, SyncGroupId vgId, SRaftId* raftId) {
M
sync io  
Minghao Li 已提交
88
  uint32_t ipv4 = taosGetIpv4FromFqdn(pNodeInfo->nodeFqdn);
M
Minghao Li 已提交
89
  ASSERT(ipv4 != 0xFFFFFFFF);
90
  char ipbuf[128] = {0};
M
sync io  
Minghao Li 已提交
91 92
  tinet_ntoa(ipbuf, ipv4);
  raftId->addr = syncUtilAddr2U64(ipbuf, pNodeInfo->nodePort);
M
Minghao Li 已提交
93 94 95
  raftId->vgId = vgId;
}

M
Minghao Li 已提交
96 97 98 99
bool syncUtilSameId(const SRaftId* pId1, const SRaftId* pId2) {
  bool ret = pId1->addr == pId2->addr && pId1->vgId == pId2->vgId;
  return ret;
}
M
Minghao Li 已提交
100 101

bool syncUtilEmptyId(const SRaftId* pId) { return (pId->addr == 0 && pId->vgId == 0); }
M
Minghao Li 已提交
102

M
Minghao Li 已提交
103 104 105
// ---- SSyncBuffer -----
void syncUtilbufBuild(SSyncBuffer* syncBuf, size_t len) {
  syncBuf->len = len;
wafwerar's avatar
wafwerar 已提交
106
  syncBuf->data = taosMemoryMalloc(syncBuf->len);
M
Minghao Li 已提交
107 108
}

wafwerar's avatar
wafwerar 已提交
109
void syncUtilbufDestroy(SSyncBuffer* syncBuf) { taosMemoryFree(syncBuf->data); }
M
Minghao Li 已提交
110 111 112 113 114 115 116 117

void syncUtilbufCopy(const SSyncBuffer* src, SSyncBuffer* dest) {
  dest->len = src->len;
  dest->data = src->data;
}

void syncUtilbufCopyDeep(const SSyncBuffer* src, SSyncBuffer* dest) {
  dest->len = src->len;
wafwerar's avatar
wafwerar 已提交
118
  dest->data = taosMemoryMalloc(dest->len);
M
Minghao Li 已提交
119 120
  memcpy(dest->data, src->data, dest->len);
}
M
Minghao Li 已提交
121 122 123

// ---- misc ----

wafwerar's avatar
wafwerar 已提交
124
int32_t syncUtilRand(int32_t max) { return taosRand() % max; }
M
Minghao Li 已提交
125

M
Minghao Li 已提交
126
int32_t syncUtilElectRandomMS(int32_t min, int32_t max) {
M
Minghao Li 已提交
127
  ASSERT(min > 0 && max > 0 && max >= min);
128 129 130 131
  int32_t rdm = min + syncUtilRand(max - min);

  // sDebug("random min:%d, max:%d, rdm:%d", min, max, rdm);
  return rdm;
M
Minghao Li 已提交
132
}
M
Minghao Li 已提交
133

M
Minghao Li 已提交
134 135 136
int32_t syncUtilQuorum(int32_t replicaNum) { return replicaNum / 2 + 1; }

cJSON* syncUtilNodeInfo2Json(const SNodeInfo* p) {
137
  char   u64buf[128] = {0};
M
Minghao Li 已提交
138 139 140 141 142 143 144 145 146 147 148
  cJSON* pRoot = cJSON_CreateObject();

  cJSON_AddStringToObject(pRoot, "nodeFqdn", p->nodeFqdn);
  cJSON_AddNumberToObject(pRoot, "nodePort", p->nodePort);

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

cJSON* syncUtilRaftId2Json(const SRaftId* p) {
149
  char   u64buf[128] = {0};
M
Minghao Li 已提交
150 151
  cJSON* pRoot = cJSON_CreateObject();

152
  snprintf(u64buf, sizeof(u64buf), "%" PRIu64 "", p->addr);
M
Minghao Li 已提交
153
  cJSON_AddStringToObject(pRoot, "addr", u64buf);
154
  char     host[128] = {0};
M
Minghao Li 已提交
155 156 157 158 159 160 161
  uint16_t port;
  syncUtilU642Addr(p->addr, host, sizeof(host), &port);
  cJSON_AddStringToObject(pRoot, "host", host);
  cJSON_AddNumberToObject(pRoot, "port", port);
  cJSON_AddNumberToObject(pRoot, "vgId", p->vgId);

  cJSON* pJson = cJSON_CreateObject();
M
Minghao Li 已提交
162
  cJSON_AddItemToObject(pJson, "SRaftId", pRoot);
M
Minghao Li 已提交
163 164 165
  return pJson;
}

M
Minghao Li 已提交
166 167 168 169 170 171 172
char* syncUtilRaftId2Str(const SRaftId* p) {
  cJSON* pJson = syncUtilRaftId2Json(p);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

M
Minghao Li 已提交
173
const char* syncUtilState2String(ESyncState state) {
M
Minghao Li 已提交
174 175 176 177 178 179 180 181 182 183 184 185
  /*
    if (state == TAOS_SYNC_STATE_FOLLOWER) {
      return "TAOS_SYNC_STATE_FOLLOWER";
    } else if (state == TAOS_SYNC_STATE_CANDIDATE) {
      return "TAOS_SYNC_STATE_CANDIDATE";
    } else if (state == TAOS_SYNC_STATE_LEADER) {
      return "TAOS_SYNC_STATE_LEADER";
    } else {
      return "TAOS_SYNC_STATE_UNKNOWN";
    }
  */

M
Minghao Li 已提交
186
  if (state == TAOS_SYNC_STATE_FOLLOWER) {
M
Minghao Li 已提交
187
    return "follower";
M
Minghao Li 已提交
188
  } else if (state == TAOS_SYNC_STATE_CANDIDATE) {
M
Minghao Li 已提交
189
    return "candidate";
M
Minghao Li 已提交
190
  } else if (state == TAOS_SYNC_STATE_LEADER) {
M
Minghao Li 已提交
191
    return "leader";
M
Minghao Li 已提交
192
  } else {
M
Minghao Li 已提交
193
    return "state_error";
M
Minghao Li 已提交
194
  }
M
Minghao Li 已提交
195 196 197 198 199 200 201 202 203 204 205
}

bool syncUtilCanPrint(char c) {
  if (c >= 32 && c <= 126) {
    return true;
  } else {
    return false;
  }
}

char* syncUtilprintBin(char* ptr, uint32_t len) {
wafwerar's avatar
wafwerar 已提交
206
  char* s = taosMemoryMalloc(len + 1);
M
Minghao Li 已提交
207
  ASSERT(s != NULL);
M
Minghao Li 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220
  memset(s, 0, len + 1);
  memcpy(s, ptr, len);

  for (int i = 0; i < len; ++i) {
    if (!syncUtilCanPrint(s[i])) {
      s[i] = '.';
    }
  }
  return s;
}

char* syncUtilprintBin2(char* ptr, uint32_t len) {
  uint32_t len2 = len * 4 + 1;
wafwerar's avatar
wafwerar 已提交
221
  char*    s = taosMemoryMalloc(len2);
M
Minghao Li 已提交
222
  ASSERT(s != NULL);
M
Minghao Li 已提交
223 224 225 226 227 228 229 230
  memset(s, 0, len2);

  char* p = s;
  for (int i = 0; i < len; ++i) {
    int n = sprintf(p, "%d,", ptr[i]);
    p += n;
  }
  return s;
M
Minghao Li 已提交
231 232 233 234 235 236 237 238 239 240
}

SyncIndex syncUtilMinIndex(SyncIndex a, SyncIndex b) {
  SyncIndex r = a < b ? a : b;
  return r;
}

SyncIndex syncUtilMaxIndex(SyncIndex a, SyncIndex b) {
  SyncIndex r = a > b ? a : b;
  return r;
241
}
M
Minghao Li 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254

void syncUtilMsgHtoN(void* msg) {
  // htonl
  SMsgHead* pHead = msg;
  pHead->contLen = htonl(pHead->contLen);
  pHead->vgId = htonl(pHead->vgId);
}

void syncUtilMsgNtoH(void* msg) {
  // ntohl
  SMsgHead* pHead = msg;
  pHead->contLen = ntohl(pHead->contLen);
  pHead->vgId = ntohl(pHead->vgId);
M
Minghao Li 已提交
255 256
}

257
#if 0
M
Minghao Li 已提交
258
bool syncUtilIsData(tmsg_t msgType) {
259
  if (msgType == TDMT_SYNC_NOOP || msgType == TDMT_SYNC_CONFIG_CHANGE) {
M
Minghao Li 已提交
260 261 262 263
    return false;
  }
  return true;
}
264
#endif
M
Minghao Li 已提交
265 266

bool syncUtilUserPreCommit(tmsg_t msgType) {
M
Minghao Li 已提交
267 268
  if (msgType != TDMT_SYNC_NOOP && msgType != TDMT_SYNC_CONFIG_CHANGE && msgType != TDMT_SYNC_CONFIG_CHANGE_FINISH &&
      msgType != TDMT_SYNC_LEADER_TRANSFER) {
M
Minghao Li 已提交
269 270
    return true;
  }
M
Minghao Li 已提交
271

M
Minghao Li 已提交
272 273 274 275
  return false;
}

bool syncUtilUserCommit(tmsg_t msgType) {
M
Minghao Li 已提交
276 277
  if (msgType != TDMT_SYNC_NOOP && msgType != TDMT_SYNC_CONFIG_CHANGE && msgType != TDMT_SYNC_CONFIG_CHANGE_FINISH &&
      msgType != TDMT_SYNC_LEADER_TRANSFER) {
M
Minghao Li 已提交
278 279
    return true;
  }
M
Minghao Li 已提交
280

M
Minghao Li 已提交
281 282 283 284
  return false;
}

bool syncUtilUserRollback(tmsg_t msgType) {
M
Minghao Li 已提交
285 286
  if (msgType != TDMT_SYNC_NOOP && msgType != TDMT_SYNC_CONFIG_CHANGE && msgType != TDMT_SYNC_CONFIG_CHANGE_FINISH &&
      msgType != TDMT_SYNC_LEADER_TRANSFER) {
M
Minghao Li 已提交
287 288
    return true;
  }
M
Minghao Li 已提交
289

M
Minghao Li 已提交
290
  return false;
M
Minghao Li 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
}

void syncUtilJson2Line(char* jsonStr) {
  int p, q, len;
  p = 0;
  q = 1;
  len = strlen(jsonStr);
  while (1) {
    if (jsonStr[q] == '\0') {
      jsonStr[p + 1] = '\0';
      break;
    }

    if (jsonStr[q] == '\n' || jsonStr[q] == ' ' || jsonStr[q] == '\t') {
      q++;
      continue;
    } else {
      jsonStr[p + 1] = jsonStr[q];
      p++;
      q++;
    }
  }
M
Minghao Li 已提交
313
}