syncIndexMgr.c 5.7 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/>.
 */

16
#define _DEFAULT_SOURCE
M
Minghao Li 已提交
17 18 19
#include "syncIndexMgr.h"
#include "syncUtil.h"

S
Shengliang Guan 已提交
20 21 22
SSyncIndexMgr *syncIndexMgrCreate(SSyncNode *pNode) {
  SSyncIndexMgr *pIndexMgr = taosMemoryCalloc(1, sizeof(SSyncIndexMgr));
  if (pIndexMgr == NULL) {
23 24 25
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
M
Minghao Li 已提交
26

S
Shengliang Guan 已提交
27
  pIndexMgr->replicas = &pNode->replicasId;
S
Shengliang Guan 已提交
28
  pIndexMgr->replicaNum = pNode->replicaNum;
C
cadem 已提交
29
  pIndexMgr->totalReplicaNum = pNode->totalReplicaNum;
S
Shengliang Guan 已提交
30 31
  pIndexMgr->pNode = pNode;
  syncIndexMgrClear(pIndexMgr);
M
Minghao Li 已提交
32

S
Shengliang Guan 已提交
33
  return pIndexMgr;
M
Minghao Li 已提交
34 35
}

S
Shengliang Guan 已提交
36
void syncIndexMgrUpdate(SSyncIndexMgr *pIndexMgr, SSyncNode *pNode) {
S
Shengliang Guan 已提交
37
  pIndexMgr->replicas = &pNode->replicasId;
S
Shengliang Guan 已提交
38
  pIndexMgr->replicaNum = pNode->replicaNum;
C
cadem 已提交
39
  pIndexMgr->totalReplicaNum = pNode->totalReplicaNum;
S
Shengliang Guan 已提交
40 41
  pIndexMgr->pNode = pNode;
  syncIndexMgrClear(pIndexMgr);
M
Minghao Li 已提交
42 43
}

S
Shengliang Guan 已提交
44 45 46
void syncIndexMgrDestroy(SSyncIndexMgr *pIndexMgr) {
  if (pIndexMgr != NULL) {
    taosMemoryFree(pIndexMgr);
M
Minghao Li 已提交
47 48 49
  }
}

S
Shengliang Guan 已提交
50 51 52
void syncIndexMgrClear(SSyncIndexMgr *pIndexMgr) {
  memset(pIndexMgr->index, 0, sizeof(pIndexMgr->index));
  memset(pIndexMgr->privateTerm, 0, sizeof(pIndexMgr->privateTerm));
53

54
  int64_t timeNow = taosGetTimestampMs();
C
cadem 已提交
55
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
S
Shengliang Guan 已提交
56 57
    pIndexMgr->startTimeArr[i] = 0;
    pIndexMgr->recvTimeArr[i] = timeNow;
58
  }
M
Minghao Li 已提交
59 60
}

S
Shengliang Guan 已提交
61
void syncIndexMgrSetIndex(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId, SyncIndex index) {
C
cadem 已提交
62
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
S
Shengliang Guan 已提交
63 64
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
      (pIndexMgr->index)[i] = index;
M
Minghao Li 已提交
65 66 67
      return;
    }
  }
68

69 70
  sError("vgId:%d, indexmgr set index:%" PRId64 " for dnode:%d cluster:%d failed", pIndexMgr->pNode->vgId, index,
         DID(pRaftId), CID(pRaftId));
M
Minghao Li 已提交
71 72
}

S
Shengliang Guan 已提交
73
SSyncLogReplMgr *syncNodeGetLogReplMgr(SSyncNode *pNode, SRaftId *pRaftId) {
C
cadem 已提交
74
  for (int i = 0; i < pNode->totalReplicaNum; i++) {
S
Shengliang Guan 已提交
75
    if (syncUtilSameId(&pNode->replicasId[i], pRaftId)) {
B
Benguang Zhao 已提交
76 77 78
      return pNode->logReplMgrs[i];
    }
  }
S
Shengliang Guan 已提交
79

80
  sError("vgId:%d, indexmgr get replmgr from dnode:%d cluster:%d failed", pNode->vgId, DID(pRaftId), CID(pRaftId));
B
Benguang Zhao 已提交
81 82 83
  return NULL;
}

S
Shengliang Guan 已提交
84
SyncIndex syncIndexMgrGetIndex(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId) {
C
cadem 已提交
85
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
S
Shengliang Guan 已提交
86 87
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
      SyncIndex idx = (pIndexMgr->index)[i];
M
Minghao Li 已提交
88 89 90
      return idx;
    }
  }
M
Minghao Li 已提交
91

92 93
  sError("vgId:%d, indexmgr get index from dnode:%d cluster:%d failed", pIndexMgr->pNode->vgId, DID(pRaftId),
         CID(pRaftId));
M
Minghao Li 已提交
94
  return SYNC_INDEX_INVALID;
M
Minghao Li 已提交
95 96
}

S
Shengliang Guan 已提交
97
void syncIndexMgrSetStartTime(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId, int64_t startTime) {
C
cadem 已提交
98
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
S
Shengliang Guan 已提交
99 100
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
      (pIndexMgr->startTimeArr)[i] = startTime;
101 102 103 104
      return;
    }
  }

105 106
  sError("vgId:%d, indexmgr set start-time:%" PRId64 " for dnode:%d cluster:%d failed", pIndexMgr->pNode->vgId,
         startTime, DID(pRaftId), CID(pRaftId));
107 108
}

S
Shengliang Guan 已提交
109
int64_t syncIndexMgrGetStartTime(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId) {
C
cadem 已提交
110
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
S
Shengliang Guan 已提交
111 112
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
      int64_t startTime = (pIndexMgr->startTimeArr)[i];
113 114 115
      return startTime;
    }
  }
S
Shengliang Guan 已提交
116

117 118
  sError("vgId:%d, indexmgr get start-time from dnode:%d cluster:%d failed", pIndexMgr->pNode->vgId, DID(pRaftId),
         CID(pRaftId));
119
  return -1;
120 121
}

S
Shengliang Guan 已提交
122
void syncIndexMgrSetRecvTime(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId, int64_t recvTime) {
C
cadem 已提交
123
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
S
Shengliang Guan 已提交
124 125
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
      (pIndexMgr->recvTimeArr)[i] = recvTime;
126 127 128 129
      return;
    }
  }

130 131
  sError("vgId:%d, indexmgr set recv-time:%" PRId64 " for dnode:%d cluster:%d failed", pIndexMgr->pNode->vgId, recvTime,
         DID(pRaftId), CID(pRaftId));
132 133
}

S
Shengliang Guan 已提交
134
int64_t syncIndexMgrGetRecvTime(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId) {
C
cadem 已提交
135
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
S
Shengliang Guan 已提交
136 137
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
      int64_t recvTime = (pIndexMgr->recvTimeArr)[i];
138 139 140
      return recvTime;
    }
  }
141

142 143
  sError("vgId:%d, indexmgr get recv-time from dnode:%d cluster:%d failed", pIndexMgr->pNode->vgId, DID(pRaftId),
         CID(pRaftId));
144
  return -1;
145 146
}

S
Shengliang Guan 已提交
147
void syncIndexMgrSetTerm(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId, SyncTerm term) {
C
cadem 已提交
148
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
S
Shengliang Guan 已提交
149 150
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
      (pIndexMgr->privateTerm)[i] = term;
M
Minghao Li 已提交
151 152 153 154
      return;
    }
  }

155 156
  sError("vgId:%d, indexmgr set term:%" PRId64 " for dnode:%d cluster:%d failed", pIndexMgr->pNode->vgId, term,
         DID(pRaftId), CID(pRaftId));
M
Minghao Li 已提交
157 158
}

S
Shengliang Guan 已提交
159
SyncTerm syncIndexMgrGetTerm(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId) {
C
cadem 已提交
160
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
S
Shengliang Guan 已提交
161 162
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
      SyncTerm term = (pIndexMgr->privateTerm)[i];
M
Minghao Li 已提交
163 164 165
      return term;
    }
  }
S
Shengliang Guan 已提交
166

167 168
  sError("vgId:%d, indexmgr get term from dnode:%d cluster:%d failed", pIndexMgr->pNode->vgId, DID(pRaftId),
         CID(pRaftId));
169
  return -1;
170
}