syncUtil.c 6.5 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"
M
Minghao Li 已提交
17
#include "syncEnv.h"
M
Minghao Li 已提交
18

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

M
Minghao Li 已提交
21 22 23
// ---- encode / decode
uint64_t syncUtilAddr2U64(const char* host, uint16_t port) {
  uint64_t u64;
24
  uint32_t hostU32 = (uint32_t)taosInetAddr(host);
M
Minghao Li 已提交
25
  // assert(hostU32 != (uint32_t)-1);
M
Minghao Li 已提交
26 27 28
  u64 = (((uint64_t)hostU32) << 32) | (((uint32_t)port) << 16);
  return u64;
}
M
Minghao Li 已提交
29

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

M
Minghao Li 已提交
33 34
  struct in_addr addr;
  addr.s_addr = hostU32;
35
  snprintf(host, len, "%s", taosInetNtoa(addr));
M
Minghao Li 已提交
36 37 38 39 40
  *port = (uint16_t)((u64 & 0x00000000FFFF0000) >> 16);
}

void syncUtilnodeInfo2EpSet(const SNodeInfo* pNodeInfo, SEpSet* pEpSet) {
  pEpSet->inUse = 0;
M
Minghao Li 已提交
41
  pEpSet->numOfEps = 0;
M
Minghao Li 已提交
42 43 44 45
  addEpIntoEpSet(pEpSet, pNodeInfo->nodeFqdn, pNodeInfo->nodePort);
}

void syncUtilraftId2EpSet(const SRaftId* raftId, SEpSet* pEpSet) {
46
  char     host[TSDB_FQDN_LEN] = {0};
M
Minghao Li 已提交
47 48 49
  uint16_t port;

  syncUtilU642Addr(raftId->addr, host, sizeof(host), &port);
M
sync io  
Minghao Li 已提交
50 51 52 53 54

  /*
    pEpSet->numOfEps = 1;
    pEpSet->inUse = 0;
    pEpSet->eps[0].port = port;
M
Minghao Li 已提交
55
    snprintf(pEpSet->eps[0].fqdn, sizeof(pEpSet->eps[0].fqdn), "%s", host);
M
sync io  
Minghao Li 已提交
56
  */
M
Minghao Li 已提交
57
  pEpSet->inUse = 0;
M
Minghao Li 已提交
58
  pEpSet->numOfEps = 0;
M
Minghao Li 已提交
59 60 61 62
  addEpIntoEpSet(pEpSet, host, port);
}

void syncUtilnodeInfo2raftId(const SNodeInfo* pNodeInfo, SyncGroupId vgId, SRaftId* raftId) {
M
sync io  
Minghao Li 已提交
63 64
  uint32_t ipv4 = taosGetIpv4FromFqdn(pNodeInfo->nodeFqdn);
  assert(ipv4 != 0xFFFFFFFF);
65
  char ipbuf[128] = {0};
M
sync io  
Minghao Li 已提交
66 67
  tinet_ntoa(ipbuf, ipv4);
  raftId->addr = syncUtilAddr2U64(ipbuf, pNodeInfo->nodePort);
M
Minghao Li 已提交
68 69 70
  raftId->vgId = vgId;
}

M
Minghao Li 已提交
71 72 73 74
bool syncUtilSameId(const SRaftId* pId1, const SRaftId* pId2) {
  bool ret = pId1->addr == pId2->addr && pId1->vgId == pId2->vgId;
  return ret;
}
M
Minghao Li 已提交
75 76

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

M
Minghao Li 已提交
78 79 80
// ---- SSyncBuffer -----
void syncUtilbufBuild(SSyncBuffer* syncBuf, size_t len) {
  syncBuf->len = len;
wafwerar's avatar
wafwerar 已提交
81
  syncBuf->data = taosMemoryMalloc(syncBuf->len);
M
Minghao Li 已提交
82 83
}

wafwerar's avatar
wafwerar 已提交
84
void syncUtilbufDestroy(SSyncBuffer* syncBuf) { taosMemoryFree(syncBuf->data); }
M
Minghao Li 已提交
85 86 87 88 89 90 91 92

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 已提交
93
  dest->data = taosMemoryMalloc(dest->len);
M
Minghao Li 已提交
94 95
  memcpy(dest->data, src->data, dest->len);
}
M
Minghao Li 已提交
96 97 98

// ---- misc ----

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

M
Minghao Li 已提交
101 102 103 104
int32_t syncUtilElectRandomMS(int32_t min, int32_t max) {
  assert(min > 0 && max > 0 && max >= min);
  return min + syncUtilRand(max - min);
}
M
Minghao Li 已提交
105

M
Minghao Li 已提交
106 107 108
int32_t syncUtilQuorum(int32_t replicaNum) { return replicaNum / 2 + 1; }

cJSON* syncUtilNodeInfo2Json(const SNodeInfo* p) {
109
  char   u64buf[128] = {0};
M
Minghao Li 已提交
110 111 112 113 114 115 116 117 118 119 120
  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) {
121
  char   u64buf[128] = {0};
M
Minghao Li 已提交
122 123
  cJSON* pRoot = cJSON_CreateObject();

124
  snprintf(u64buf, sizeof(u64buf), "%" PRIu64 "", p->addr);
M
Minghao Li 已提交
125
  cJSON_AddStringToObject(pRoot, "addr", u64buf);
126
  char     host[128] = {0};
M
Minghao Li 已提交
127 128 129 130 131 132 133
  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 已提交
134
  cJSON_AddItemToObject(pJson, "SRaftId", pRoot);
M
Minghao Li 已提交
135 136 137
  return pJson;
}

M
Minghao Li 已提交
138 139 140 141 142 143 144
char* syncUtilRaftId2Str(const SRaftId* p) {
  cJSON* pJson = syncUtilRaftId2Json(p);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

M
Minghao Li 已提交
145 146 147 148 149 150 151 152 153 154
const char* syncUtilState2String(ESyncState state) {
  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 已提交
155 156 157 158 159 160 161 162 163 164 165
}

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 已提交
166
  char* s = taosMemoryMalloc(len + 1);
M
Minghao Li 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180
  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 已提交
181
  char*    s = taosMemoryMalloc(len2);
M
Minghao Li 已提交
182 183 184 185 186 187 188 189 190
  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 已提交
191 192 193 194 195 196 197 198 199 200
}

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;
201
}
M
Minghao Li 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214

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

bool syncUtilIsData(tmsg_t msgType) {
  if (msgType == TDMT_VND_SYNC_NOOP || msgType == TDMT_VND_SYNC_CONFIG_CHANGE) {
    return false;
  }
  return true;
}

bool syncUtilUserPreCommit(tmsg_t msgType) {
  if (msgType != TDMT_VND_SYNC_NOOP && msgType != TDMT_VND_SYNC_CONFIG_CHANGE) {
    return true;
  }
  return false;
}

bool syncUtilUserCommit(tmsg_t msgType) {
  if (msgType != TDMT_VND_SYNC_NOOP && msgType != TDMT_VND_SYNC_CONFIG_CHANGE) {
    return true;
  }
  return false;
}

bool syncUtilUserRollback(tmsg_t msgType) {
  if (msgType != TDMT_VND_SYNC_NOOP && msgType != TDMT_VND_SYNC_CONFIG_CHANGE) {
    return true;
  }
  return false;
M
Minghao Li 已提交
243
}