syncUtil.c 8.2 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

  uint32_t hostU32 = taosGetIpv4FromFqdn(host);
  if (hostU32 == (uint32_t)-1) {
29 30
    sError("failed to resolve ipv4 addr. host:%s", host);
    terrno = TSDB_CODE_TSC_INVALID_FQDN;
31 32 33 34
    return -1;
  }

  /*
35
  uint32_t hostU32 = (uint32_t)taosInetAddr(host);
36 37 38 39 40 41 42 43 44 45 46 47 48 49
  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);
  }
50 51
  */

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

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

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

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

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

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

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

88
bool syncUtilnodeInfo2raftId(const SNodeInfo* pNodeInfo, SyncGroupId vgId, SRaftId* raftId) {
M
sync io  
Minghao Li 已提交
89
  uint32_t ipv4 = taosGetIpv4FromFqdn(pNodeInfo->nodeFqdn);
90 91 92 93 94
  if (ipv4 == 0xFFFFFFFF || ipv4 == 1) {
    sError("failed to resolve ipv4 addr. fqdn: %s", pNodeInfo->nodeFqdn);
    terrno = TSDB_CODE_TSC_INVALID_FQDN;
    return false;
  }
95
  char ipbuf[128] = {0};
M
sync io  
Minghao Li 已提交
96 97
  tinet_ntoa(ipbuf, ipv4);
  raftId->addr = syncUtilAddr2U64(ipbuf, pNodeInfo->nodePort);
M
Minghao Li 已提交
98
  raftId->vgId = vgId;
99
  return true;
M
Minghao Li 已提交
100 101
}

M
Minghao Li 已提交
102 103 104 105
bool syncUtilSameId(const SRaftId* pId1, const SRaftId* pId2) {
  bool ret = pId1->addr == pId2->addr && pId1->vgId == pId2->vgId;
  return ret;
}
M
Minghao Li 已提交
106 107

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

M
Minghao Li 已提交
109 110 111
// ---- SSyncBuffer -----
void syncUtilbufBuild(SSyncBuffer* syncBuf, size_t len) {
  syncBuf->len = len;
wafwerar's avatar
wafwerar 已提交
112
  syncBuf->data = taosMemoryMalloc(syncBuf->len);
M
Minghao Li 已提交
113 114
}

wafwerar's avatar
wafwerar 已提交
115
void syncUtilbufDestroy(SSyncBuffer* syncBuf) { taosMemoryFree(syncBuf->data); }
M
Minghao Li 已提交
116 117 118 119 120 121 122 123

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 已提交
124
  dest->data = taosMemoryMalloc(dest->len);
M
Minghao Li 已提交
125 126
  memcpy(dest->data, src->data, dest->len);
}
M
Minghao Li 已提交
127 128 129

// ---- misc ----

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

M
Minghao Li 已提交
132
int32_t syncUtilElectRandomMS(int32_t min, int32_t max) {
M
Minghao Li 已提交
133
  ASSERT(min > 0 && max > 0 && max >= min);
134 135 136 137
  int32_t rdm = min + syncUtilRand(max - min);

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

M
Minghao Li 已提交
140 141 142
int32_t syncUtilQuorum(int32_t replicaNum) { return replicaNum / 2 + 1; }

cJSON* syncUtilNodeInfo2Json(const SNodeInfo* p) {
143
  char   u64buf[128] = {0};
M
Minghao Li 已提交
144 145 146 147 148 149 150 151 152 153 154
  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) {
155
  char   u64buf[128] = {0};
M
Minghao Li 已提交
156 157
  cJSON* pRoot = cJSON_CreateObject();

158
  snprintf(u64buf, sizeof(u64buf), "%" PRIu64 "", p->addr);
M
Minghao Li 已提交
159
  cJSON_AddStringToObject(pRoot, "addr", u64buf);
160
  char     host[128] = {0};
M
Minghao Li 已提交
161 162 163 164 165 166 167
  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 已提交
168
  cJSON_AddItemToObject(pJson, "SRaftId", pRoot);
M
Minghao Li 已提交
169 170 171
  return pJson;
}

M
Minghao Li 已提交
172 173 174 175 176 177 178
char* syncUtilRaftId2Str(const SRaftId* p) {
  cJSON* pJson = syncUtilRaftId2Json(p);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

M
Minghao Li 已提交
179
const char* syncUtilState2String(ESyncState state) {
M
Minghao Li 已提交
180 181 182 183 184 185 186 187 188 189 190 191
  /*
    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 已提交
192
  if (state == TAOS_SYNC_STATE_FOLLOWER) {
M
Minghao Li 已提交
193
    return "follower";
M
Minghao Li 已提交
194
  } else if (state == TAOS_SYNC_STATE_CANDIDATE) {
M
Minghao Li 已提交
195
    return "candidate";
M
Minghao Li 已提交
196
  } else if (state == TAOS_SYNC_STATE_LEADER) {
M
Minghao Li 已提交
197
    return "leader";
M
Minghao Li 已提交
198
  } else {
M
Minghao Li 已提交
199
    return "state_error";
M
Minghao Li 已提交
200
  }
M
Minghao Li 已提交
201 202 203 204 205 206 207 208 209 210 211
}

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 已提交
212
  char* s = taosMemoryMalloc(len + 1);
M
Minghao Li 已提交
213
  ASSERT(s != NULL);
M
Minghao Li 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226
  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 已提交
227
  char*    s = taosMemoryMalloc(len2);
M
Minghao Li 已提交
228
  ASSERT(s != NULL);
M
Minghao Li 已提交
229 230 231 232 233 234 235 236
  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 已提交
237 238 239 240 241 242 243 244 245 246
}

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;
247
}
M
Minghao Li 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260

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 已提交
261 262
}

263
#if 0
M
Minghao Li 已提交
264
bool syncUtilIsData(tmsg_t msgType) {
265
  if (msgType == TDMT_SYNC_NOOP || msgType == TDMT_SYNC_CONFIG_CHANGE) {
M
Minghao Li 已提交
266 267 268 269
    return false;
  }
  return true;
}
270
#endif
M
Minghao Li 已提交
271 272

bool syncUtilUserPreCommit(tmsg_t msgType) {
M
Minghao Li 已提交
273 274
  if (msgType != TDMT_SYNC_NOOP && msgType != TDMT_SYNC_CONFIG_CHANGE && msgType != TDMT_SYNC_CONFIG_CHANGE_FINISH &&
      msgType != TDMT_SYNC_LEADER_TRANSFER) {
M
Minghao Li 已提交
275 276
    return true;
  }
M
Minghao Li 已提交
277

M
Minghao Li 已提交
278 279 280 281
  return false;
}

bool syncUtilUserCommit(tmsg_t msgType) {
M
Minghao Li 已提交
282 283
  if (msgType != TDMT_SYNC_NOOP && msgType != TDMT_SYNC_CONFIG_CHANGE && msgType != TDMT_SYNC_CONFIG_CHANGE_FINISH &&
      msgType != TDMT_SYNC_LEADER_TRANSFER) {
M
Minghao Li 已提交
284 285
    return true;
  }
M
Minghao Li 已提交
286

M
Minghao Li 已提交
287 288 289 290
  return false;
}

bool syncUtilUserRollback(tmsg_t msgType) {
M
Minghao Li 已提交
291 292
  if (msgType != TDMT_SYNC_NOOP && msgType != TDMT_SYNC_CONFIG_CHANGE && msgType != TDMT_SYNC_CONFIG_CHANGE_FINISH &&
      msgType != TDMT_SYNC_LEADER_TRANSFER) {
M
Minghao Li 已提交
293 294
    return true;
  }
M
Minghao Li 已提交
295

M
Minghao Li 已提交
296
  return false;
M
Minghao Li 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
}

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++;
    }
  }
319
}