mndSync.c 8.7 KB
Newer Older
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/>.
 */

#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
17
#include "mndSync.h"
18
#include "mndTrans.h"
19

20
static int32_t mndSyncEqMsg(const SMsgCb *msgcb, SRpcMsg *pMsg) {
M
Minghao Li 已提交
21 22 23 24
  SMsgHead *pHead = pMsg->pCont;
  pHead->contLen = htonl(pHead->contLen);
  pHead->vgId = htonl(pHead->vgId);

25 26 27 28 29 30
  int32_t code = tmsgPutToQueue(msgcb, SYNC_QUEUE, pMsg);
  if (code != 0) {
    rpcFreeCont(pMsg->pCont);
    pMsg->pCont = NULL;
  }
  return code;
M
Minghao Li 已提交
31
}
M
Minghao Li 已提交
32

33 34 35 36 37 38 39 40
static int32_t mndSyncSendMsg(const SEpSet *pEpSet, SRpcMsg *pMsg) {
  int32_t code = tmsgSendReq(pEpSet, pMsg);
  if (code != 0) {
    rpcFreeCont(pMsg->pCont);
    pMsg->pCont = NULL;
  }
  return code;
}
M
Minghao Li 已提交
41

42
void mndSyncCommitMsg(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) {
S
Shengliang Guan 已提交
43 44 45 46
  SMnode    *pMnode = pFsm->data;
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
  SSdbRaw   *pRaw = pMsg->pCont;

S
Shengliang Guan 已提交
47
  int32_t transId = sdbGetIdFromRaw(pMnode->pSdb, pRaw);
S
Shengliang Guan 已提交
48
  pMgmt->errCode = cbMeta.code;
49 50 51 52
  mDebug("trans:%d, is proposed, saved:%d code:0x%x, apply index:%" PRId64 " term:%" PRIu64 " config:%" PRId64
         " role:%s raw:%p",
         transId, pMgmt->transId, cbMeta.code, cbMeta.index, cbMeta.term, cbMeta.lastConfigIndex, syncStr(cbMeta.state),
         pRaw);
S
Shengliang Guan 已提交
53 54 55

  if (pMgmt->errCode == 0) {
    sdbWriteWithoutFree(pMnode->pSdb, pRaw);
56
    sdbSetApplyInfo(pMnode->pSdb, cbMeta.index, cbMeta.term, cbMeta.lastConfigIndex);
S
Shengliang Guan 已提交
57 58 59 60 61 62
  }

  if (pMgmt->transId == transId) {
    if (pMgmt->errCode != 0) {
      mError("trans:%d, failed to propose since %s", transId, tstrerror(pMgmt->errCode));
    }
S
Shengliang Guan 已提交
63
    pMgmt->transId = 0;
S
Shengliang Guan 已提交
64
    tsem_post(&pMgmt->syncSem);
65
  } else {
66 67 68 69 70
    STrans *pTrans = mndAcquireTrans(pMnode, transId);
    if (pTrans != NULL) {
      mndTransExecute(pMnode, pTrans);
      mndReleaseTrans(pMnode, pTrans);
    }
71
#if 0
72
    sdbWriteFile(pMnode->pSdb, SDB_WRITE_DELTA);
73
#endif
M
Minghao Li 已提交
74 75 76
  }
}

77
int32_t mndSyncGetSnapshot(struct SSyncFSM *pFsm, SSnapshot *pSnapshot, void *pReaderParam, void **ppReader) {
78 79 80 81
  mInfo("start to read snapshot from sdb in atomic way");
  SMnode *pMnode = pFsm->data;
  return sdbStartRead(pMnode->pSdb, (SSdbIter **)ppReader, &pSnapshot->lastApplyIndex, &pSnapshot->lastApplyTerm,
                      &pSnapshot->lastConfigIndex);
82 83 84 85
  return 0;
}

int32_t mndSyncGetSnapshotInfo(struct SSyncFSM *pFsm, SSnapshot *pSnapshot) {
86
  SMnode *pMnode = pFsm->data;
87
  sdbGetCommitInfo(pMnode->pSdb, &pSnapshot->lastApplyIndex, &pSnapshot->lastApplyTerm, &pSnapshot->lastConfigIndex);
M
Minghao Li 已提交
88 89 90
  return 0;
}

91
void mndRestoreFinish(struct SSyncFSM *pFsm) {
92
  SMnode *pMnode = pFsm->data;
S
Shengliang Guan 已提交
93

S
Shengliang Guan 已提交
94
  if (!pMnode->deploy) {
95
    mInfo("mnode sync restore finished, and will handle outstanding transactions");
S
Shengliang Guan 已提交
96
    mndTransPullup(pMnode);
S
Shengliang Guan 已提交
97
    mndSetRestore(pMnode, true);
S
Shengliang Guan 已提交
98 99
  } else {
    mInfo("mnode sync restore finished, and will set ready after first deploy");
S
Shengliang Guan 已提交
100
  }
101 102
}

103
void mndReConfig(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SReConfigCbMeta cbMeta) {
S
Shengliang Guan 已提交
104 105 106 107
  SMnode    *pMnode = pFsm->data;
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;

  pMgmt->errCode = cbMeta.code;
108 109
  mInfo("trans:-1, sync reconfig is proposed, saved:%d code:0x%x, index:%" PRId64 " term:%" PRId64, pMgmt->transId,
        cbMeta.code, cbMeta.index, cbMeta.term);
S
Shengliang Guan 已提交
110 111 112 113 114

  if (pMgmt->transId == -1) {
    if (pMgmt->errCode != 0) {
      mError("trans:-1, failed to propose sync reconfig since %s", tstrerror(pMgmt->errCode));
    }
S
Shengliang Guan 已提交
115
    pMgmt->transId = 0;
S
Shengliang Guan 已提交
116
    tsem_post(&pMgmt->syncSem);
S
Shengliang Guan 已提交
117
  }
118 119
}

S
Shengliang Guan 已提交
120 121 122
int32_t mndSnapshotStartRead(struct SSyncFSM *pFsm, void **ppReader) {
  mInfo("start to read snapshot from sdb");
  SMnode *pMnode = pFsm->data;
123
  return sdbStartRead(pMnode->pSdb, (SSdbIter **)ppReader, NULL, NULL, NULL);
S
Shengliang Guan 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
}

int32_t mndSnapshotStopRead(struct SSyncFSM *pFsm, void *pReader) {
  mInfo("stop to read snapshot from sdb");
  SMnode *pMnode = pFsm->data;
  return sdbStopRead(pMnode->pSdb, pReader);
}

int32_t mndSnapshotDoRead(struct SSyncFSM *pFsm, void *pReader, void **ppBuf, int32_t *len) {
  SMnode *pMnode = pFsm->data;
  return sdbDoRead(pMnode->pSdb, pReader, ppBuf, len);
}

int32_t mndSnapshotStartWrite(struct SSyncFSM *pFsm, void **ppWriter) {
  mInfo("start to apply snapshot to sdb");
  SMnode *pMnode = pFsm->data;
  return sdbStartWrite(pMnode->pSdb, (SSdbIter **)ppWriter);
}

int32_t mndSnapshotStopWrite(struct SSyncFSM *pFsm, void *pWriter, bool isApply) {
  mInfo("stop to apply snapshot to sdb, apply:%d", isApply);
  SMnode *pMnode = pFsm->data;
  return sdbStopWrite(pMnode->pSdb, pWriter, isApply);
}

int32_t mndSnapshotDoWrite(struct SSyncFSM *pFsm, void *pWriter, void *pBuf, int32_t len) {
  SMnode *pMnode = pFsm->data;
  return sdbDoWrite(pMnode->pSdb, pWriter, pBuf, len);
}

154 155
SSyncFSM *mndSyncMakeFsm(SMnode *pMnode) {
  SSyncFSM *pFsm = taosMemoryCalloc(1, sizeof(SSyncFSM));
M
Minghao Li 已提交
156
  pFsm->data = pMnode;
157
  pFsm->FpCommitCb = mndSyncCommitMsg;
158 159
  pFsm->FpPreCommitCb = NULL;
  pFsm->FpRollBackCb = NULL;
160 161
  pFsm->FpRestoreFinishCb = mndRestoreFinish;
  pFsm->FpReConfigCb = mndReConfig;
S
Shengliang Guan 已提交
162
  pFsm->FpGetSnapshot = mndSyncGetSnapshot;
163
  pFsm->FpGetSnapshotInfo = mndSyncGetSnapshotInfo;
S
Shengliang Guan 已提交
164 165 166 167 168 169
  pFsm->FpSnapshotStartRead = mndSnapshotStartRead;
  pFsm->FpSnapshotStopRead = mndSnapshotStopRead;
  pFsm->FpSnapshotDoRead = mndSnapshotDoRead;
  pFsm->FpSnapshotStartWrite = mndSnapshotStartWrite;
  pFsm->FpSnapshotStopWrite = mndSnapshotStopWrite;
  pFsm->FpSnapshotDoWrite = mndSnapshotDoWrite;
M
Minghao Li 已提交
170
  return pFsm;
171 172 173 174 175
}

int32_t mndInitSync(SMnode *pMnode) {
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;

176 177
  SSyncInfo syncInfo = {.vgId = 1, .FpSendMsg = mndSyncSendMsg, .FpEqMsg = mndSyncEqMsg};
  snprintf(syncInfo.path, sizeof(syncInfo.path), "%s%ssync", pMnode->path, TD_DIRSEP);
178
  syncInfo.pWal = pMnode->pWal;
179
  syncInfo.pFsm = mndSyncMakeFsm(pMnode);
M
Minghao Li 已提交
180
  syncInfo.isStandBy = pMgmt->standby;
M
Minghao Li 已提交
181
  syncInfo.snapshotEnable = true;
182

S
Shengliang Guan 已提交
183 184 185 186 187 188 189 190 191
  mInfo("start to open mnode sync, standby:%d", pMgmt->standby);
  if (pMgmt->standby || pMgmt->replica.id > 0) {
    SSyncCfg *pCfg = &syncInfo.syncCfg;
    pCfg->replicaNum = 1;
    pCfg->myIndex = 0;
    SNodeInfo *pNode = &pCfg->nodeInfo[0];
    tstrncpy(pNode->nodeFqdn, pMgmt->replica.fqdn, sizeof(pNode->nodeFqdn));
    pNode->nodePort = pMgmt->replica.port;
    mInfo("fqdn:%s port:%u", pNode->nodeFqdn, pNode->nodePort);
M
Minghao Li 已提交
192 193
  }

194
  tsem_init(&pMgmt->syncSem, 0, 0);
195 196 197 198 199
  pMgmt->sync = syncOpen(&syncInfo);
  if (pMgmt->sync <= 0) {
    mError("failed to open sync since %s", terrstr());
    return -1;
  }
M
Minghao Li 已提交
200

S
Shengliang Guan 已提交
201
  mDebug("mnode-sync is opened, id:%" PRId64, pMgmt->sync);
S
Shengliang Guan 已提交
202 203 204 205 206
  return 0;
}

void mndCleanupSync(SMnode *pMnode) {
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
207
  syncStop(pMgmt->sync);
S
Shengliang Guan 已提交
208
  mDebug("mnode-sync is stopped, id:%" PRId64, pMgmt->sync);
209

210
  tsem_destroy(&pMgmt->syncSem);
211 212
  memset(pMgmt, 0, sizeof(SSyncMgmt));
}
M
Minghao Li 已提交
213

S
Shengliang Guan 已提交
214
int32_t mndSyncPropose(SMnode *pMnode, SSdbRaw *pRaw, int32_t transId) {
S
Shengliang Guan 已提交
215
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
216 217 218 219
  SRpcMsg    req = {.msgType = TDMT_MND_APPLY_MSG, .contLen = sdbGetRawTotalSize(pRaw)};
  req.pCont = rpcMallocCont(req.contLen);
  if (req.pCont == NULL) return -1;
  memcpy(req.pCont, pRaw, req.contLen);
S
Shengliang Guan 已提交
220

S
Shengliang Guan 已提交
221 222 223 224
  pMgmt->errCode = 0;
  pMgmt->transId = transId;
  mTrace("trans:%d, will be proposed", pMgmt->transId);

225
  const bool isWeak = false;
226
  int32_t    code = syncPropose(pMgmt->sync, &req, isWeak);
227 228
  if (code == 0) {
    tsem_wait(&pMgmt->syncSem);
M
Minghao Li 已提交
229
  } else if (code == -1 && terrno == TSDB_CODE_SYN_NOT_LEADER) {
230
    terrno = TSDB_CODE_APP_NOT_READY;
M
Minghao Li 已提交
231
  } else if (code == -1 && terrno == TSDB_CODE_SYN_INTERNAL_ERROR) {
232 233
    terrno = TSDB_CODE_SYN_INTERNAL_ERROR;
  } else {
234
    terrno = TSDB_CODE_APP_ERROR;
235
  }
236

237
  rpcFreeCont(req.pCont);
S
Shengliang Guan 已提交
238 239 240 241 242
  if (code != 0) {
    mError("trans:%d, failed to propose, code:0x%x", pMgmt->transId, code);
    return code;
  }

S
Shengliang Guan 已提交
243
  return pMgmt->errCode;
244 245
}

246
void mndSyncStart(SMnode *pMnode) {
247 248
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
  syncSetMsgCb(pMgmt->sync, &pMnode->msgCb);
M
Minghao Li 已提交
249 250
  syncStart(pMgmt->sync);
  mDebug("mnode sync started, id:%" PRId64 " standby:%d", pMgmt->sync, pMgmt->standby);
251 252
}

S
Shengliang Guan 已提交
253 254
void mndSyncStop(SMnode *pMnode) {
  if (pMnode->syncMgmt.transId != 0) {
S
Shengliang Guan 已提交
255
    pMnode->syncMgmt.transId = 0;
S
Shengliang Guan 已提交
256 257 258
    tsem_post(&pMnode->syncMgmt.syncSem);
  }
}
259

S
Shengliang Guan 已提交
260
bool mndIsMaster(SMnode *pMnode) {
S
Shengliang Guan 已提交
261
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
262

S
Shengliang Guan 已提交
263
  if (!syncIsReady(pMgmt->sync)) {
264 265 266 267
    terrno = TSDB_CODE_SYN_NOT_LEADER;
    return false;
  }

S
Shengliang Guan 已提交
268
  if (!pMnode->restored) {
269 270 271 272 273
    terrno = TSDB_CODE_APP_NOT_READY;
    return false;
  }

  return true;
L
Liu Jicong 已提交
274
}