syncReplication.c 4.2 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/>.
 */

M
Minghao Li 已提交
16
#include "syncReplication.h"
M
Minghao Li 已提交
17
#include "syncIndexMgr.h"
M
Minghao Li 已提交
18
#include "syncMessage.h"
M
Minghao Li 已提交
19
#include "syncRaftEntry.h"
M
Minghao Li 已提交
20
#include "syncRaftLog.h"
M
Minghao Li 已提交
21
#include "syncRaftStore.h"
M
Minghao Li 已提交
22
#include "syncUtil.h"
M
Minghao Li 已提交
23

M
Minghao Li 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
// TLA+ Spec
// AppendEntries(i, j) ==
//    /\ i /= j
//    /\ state[i] = Leader
//    /\ LET prevLogIndex == nextIndex[i][j] - 1
//           prevLogTerm == IF prevLogIndex > 0 THEN
//                              log[i][prevLogIndex].term
//                          ELSE
//                              0
//           \* Send up to 1 entry, constrained by the end of the log.
//           lastEntry == Min({Len(log[i]), nextIndex[i][j]})
//           entries == SubSeq(log[i], nextIndex[i][j], lastEntry)
//       IN Send([mtype          |-> AppendEntriesRequest,
//                mterm          |-> currentTerm[i],
//                mprevLogIndex  |-> prevLogIndex,
//                mprevLogTerm   |-> prevLogTerm,
//                mentries       |-> entries,
//                \* mlog is used as a history variable for the proof.
//                \* It would not exist in a real implementation.
//                mlog           |-> log[i],
//                mcommitIndex   |-> Min({commitIndex[i], lastEntry}),
//                msource        |-> i,
//                mdest          |-> j])
//    /\ UNCHANGED <<serverVars, candidateVars, leaderVars, logVars>>
M
Minghao Li 已提交
48
//
M
Minghao Li 已提交
49
int32_t syncNodeAppendEntriesPeers(SSyncNode* pSyncNode) {
M
Minghao Li 已提交
50 51
  assert(pSyncNode->state == TAOS_SYNC_STATE_LEADER);

M
Minghao Li 已提交
52
  int32_t ret = 0;
M
Minghao Li 已提交
53
  for (int i = 0; i < pSyncNode->peersNum; ++i) {
M
Minghao Li 已提交
54
    SRaftId* pDestId = &(pSyncNode->peersId[i]);
M
Minghao Li 已提交
55

M
Minghao Li 已提交
56 57
    // set prevLogIndex
    SyncIndex nextIndex = syncIndexMgrGetIndex(pSyncNode->pNextIndex, pDestId);
M
Minghao Li 已提交
58
    SyncIndex preLogIndex = nextIndex - 1;
M
Minghao Li 已提交
59

M
Minghao Li 已提交
60
    // set preLogTerm
M
Minghao Li 已提交
61 62
    SyncTerm preLogTerm = 0;
    if (preLogIndex >= SYNC_INDEX_BEGIN) {
M
Minghao Li 已提交
63
      SSyncRaftEntry* pPreEntry = pSyncNode->pLogStore->getEntry(pSyncNode->pLogStore, preLogIndex);
M
Minghao Li 已提交
64 65
      assert(pPreEntry != NULL);

M
Minghao Li 已提交
66
      preLogTerm = pPreEntry->term;
M
Minghao Li 已提交
67
      syncEntryDestory(pPreEntry);
M
Minghao Li 已提交
68
    }
M
Minghao Li 已提交
69

M
Minghao Li 已提交
70 71 72 73 74 75
    // batch optimized
    // SyncIndex lastIndex = syncUtilMinIndex(pSyncNode->pLogStore->getLastIndex(pSyncNode->pLogStore), nextIndex);

    SyncAppendEntries* pMsg = NULL;
    SSyncRaftEntry*    pEntry = logStoreGetEntry(pSyncNode->pLogStore, nextIndex);
    if (pEntry != NULL) {
M
Minghao Li 已提交
76 77
      pMsg = syncAppendEntriesBuild(pEntry->bytes);
      assert(pMsg != NULL);
M
Minghao Li 已提交
78

M
Minghao Li 已提交
79 80 81 82 83 84 85 86 87 88 89
      // add pEntry into msg
      uint32_t len;
      char*    serialized = syncEntrySerialize(pEntry, &len);
      assert(len == pEntry->bytes);
      memcpy(pMsg->data, serialized, len);

      free(serialized);
      syncEntryDestory(pEntry);

    } else {
      // maybe overflow, send empty record
M
Minghao Li 已提交
90 91
      pMsg = syncAppendEntriesBuild(0);
      assert(pMsg != NULL);
M
Minghao Li 已提交
92
    }
M
Minghao Li 已提交
93

M
Minghao Li 已提交
94
    assert(pMsg != NULL);
M
Minghao Li 已提交
95 96
    pMsg->srcId = pSyncNode->myRaftId;
    pMsg->destId = *pDestId;
M
Minghao Li 已提交
97
    pMsg->term = pSyncNode->pRaftStore->currentTerm;
M
Minghao Li 已提交
98 99 100 101
    pMsg->prevLogIndex = preLogIndex;
    pMsg->prevLogTerm = preLogTerm;
    pMsg->commitIndex = pSyncNode->commitIndex;

M
Minghao Li 已提交
102
    // send AppendEntries
M
Minghao Li 已提交
103
    syncNodeAppendEntries(pSyncNode, pDestId, pMsg);
M
Minghao Li 已提交
104
    syncAppendEntriesDestroy(pMsg);
M
Minghao Li 已提交
105 106
  }

M
Minghao Li 已提交
107 108 109 110 111 112 113 114
  return ret;
}

int32_t syncNodeReplicate(SSyncNode* pSyncNode) {
  // start replicate
  int32_t ret = syncNodeAppendEntriesPeers(pSyncNode);
  return ret;
}
M
Minghao Li 已提交
115 116 117 118 119 120 121 122 123 124

int32_t syncNodeAppendEntries(SSyncNode* pSyncNode, const SRaftId* destRaftId, const SyncAppendEntries* pMsg) {
  sTrace("syncNodeAppendEntries pSyncNode:%p ", pSyncNode);
  int32_t ret = 0;

  SRpcMsg rpcMsg;
  syncAppendEntries2RpcMsg(pMsg, &rpcMsg);
  syncNodeSendMsgById(destRaftId, pSyncNode, &rpcMsg);
  return ret;
}