syncUtil.c 7.8 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;
60
  snprintf(host, len, "%s", taosInetNtoa(addr));
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 89
  uint32_t ipv4 = taosGetIpv4FromFqdn(pNodeInfo->nodeFqdn);
  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 127 128 129
int32_t syncUtilElectRandomMS(int32_t min, int32_t max) {
  assert(min > 0 && max > 0 && max >= min);
  return min + syncUtilRand(max - min);
}
M
Minghao Li 已提交
130

M
Minghao Li 已提交
131 132 133
int32_t syncUtilQuorum(int32_t replicaNum) { return replicaNum / 2 + 1; }

cJSON* syncUtilNodeInfo2Json(const SNodeInfo* p) {
134
  char   u64buf[128] = {0};
M
Minghao Li 已提交
135 136 137 138 139 140 141 142 143 144 145
  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) {
146
  char   u64buf[128] = {0};
M
Minghao Li 已提交
147 148
  cJSON* pRoot = cJSON_CreateObject();

149
  snprintf(u64buf, sizeof(u64buf), "%" PRIu64 "", p->addr);
M
Minghao Li 已提交
150
  cJSON_AddStringToObject(pRoot, "addr", u64buf);
151
  char     host[128] = {0};
M
Minghao Li 已提交
152 153 154 155 156 157 158
  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 已提交
159
  cJSON_AddItemToObject(pJson, "SRaftId", pRoot);
M
Minghao Li 已提交
160 161 162
  return pJson;
}

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

M
Minghao Li 已提交
170
const char* syncUtilState2String(ESyncState state) {
M
Minghao Li 已提交
171 172 173 174 175 176 177 178 179 180 181 182
  /*
    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 已提交
183
  if (state == TAOS_SYNC_STATE_FOLLOWER) {
M
Minghao Li 已提交
184
    return "follower";
M
Minghao Li 已提交
185
  } else if (state == TAOS_SYNC_STATE_CANDIDATE) {
M
Minghao Li 已提交
186
    return "candidate";
M
Minghao Li 已提交
187
  } else if (state == TAOS_SYNC_STATE_LEADER) {
M
Minghao Li 已提交
188
    return "leader";
M
Minghao Li 已提交
189
  } else {
M
Minghao Li 已提交
190
    return "state_error";
M
Minghao Li 已提交
191
  }
M
Minghao Li 已提交
192 193 194 195 196 197 198 199 200 201 202
}

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 已提交
203
  char* s = taosMemoryMalloc(len + 1);
M
Minghao Li 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217
  assert(s != NULL);
  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 已提交
218
  char*    s = taosMemoryMalloc(len2);
M
Minghao Li 已提交
219 220 221 222 223 224 225 226 227
  assert(s != NULL);
  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 已提交
228 229 230 231 232 233 234 235 236 237
}

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;
238
}
M
Minghao Li 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251

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 已提交
252 253
}

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

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

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

bool syncUtilUserRollback(tmsg_t msgType) {
278
  if (msgType != TDMT_SYNC_NOOP && msgType != TDMT_SYNC_CONFIG_CHANGE && msgType != TDMT_SYNC_LEADER_TRANSFER) {
M
Minghao Li 已提交
279 280 281
    return true;
  }
  return false;
M
Minghao Li 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
}

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