-
Notifications
You must be signed in to change notification settings - Fork 90
/
util.go
182 lines (164 loc) · 4.54 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package etcdraft
import (
"encoding/binary"
"fmt"
"sort"
"time"
"github.com/coreos/etcd/raft"
"github.com/coreos/etcd/raft/raftpb"
"github.com/meshplus/bitxhub-core/order"
"github.com/meshplus/bitxhub-model/pb"
raftproto "github.com/meshplus/bitxhub/pkg/order/etcdraft/proto"
"github.com/sirupsen/logrus"
)
const (
DefaultBatchTick = 500 * time.Millisecond
DefaultSnapshotCount = 1000
DefaultCheckInterval = 3 * time.Minute
DefaultCheckAlive = 13 * time.Minute
)
func generateRaftPeers(config *order.Config) ([]raft.Peer, error) {
peers := make([]raft.Peer, 0)
for id, vpInfo := range config.Nodes {
vpIngoBytes, err := vpInfo.Marshal()
if err != nil {
return nil, fmt.Errorf("mashal vp info error: %w", err)
}
peers = append(peers, raft.Peer{ID: id, Context: vpIngoBytes})
}
sort.Slice(peers, func(i, j int) bool {
return peers[i].ID < peers[j].ID
})
return peers, nil
}
//Get the raft apply index of the highest block
func (n *Node) getBlockAppliedIndex() uint64 {
height := uint64(0)
n.blockAppliedIndex.Range(
func(key, value interface{}) bool {
k := key.(uint64)
if k > height {
height = k
}
return true
})
appliedIndex, ok := n.blockAppliedIndex.Load(height)
if !ok {
return 0
}
return appliedIndex.(uint64)
}
//Load the lastAppliedIndex of block height
func (n *Node) loadAppliedIndex() uint64 {
dat := n.storage.Get(appliedDbKey)
var lastAppliedIndex uint64
if dat == nil {
lastAppliedIndex = 0
} else {
lastAppliedIndex = binary.LittleEndian.Uint64(dat)
}
return lastAppliedIndex
}
//Write the lastAppliedIndex
func (n *Node) writeAppliedIndex(index uint64) {
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, index)
n.storage.Put(appliedDbKey, buf)
}
func (n *Node) isLeader() bool {
return n.leader == n.id
}
//Determine whether the current apply index is normal
func (n *Node) entriesToApply(allEntries []raftpb.Entry) (entriesToApply []raftpb.Entry) {
if len(allEntries) == 0 {
return
}
firstIdx := allEntries[0].Index
if firstIdx > n.appliedIndex+1 {
n.logger.Fatalf("first index of committed entry[%d] should <= progress.appliedIndex[%d]+1", firstIdx, n.appliedIndex)
}
if n.appliedIndex-firstIdx+1 < uint64(len(allEntries)) {
entriesToApply = allEntries[n.appliedIndex-firstIdx+1:]
}
if len(entriesToApply) > 0 {
n.logger.Infof("start index:%d, end index:%d", entriesToApply[0].Index, entriesToApply[len(entriesToApply)-1].Index)
}
return entriesToApply
}
func (n *Node) ramLastIndex() uint64 {
i, _ := n.raftStorage.ram.LastIndex()
n.logger.Infof("New Leader's last index is %d, appliedIndex is %d", i, n.appliedIndex)
return i
}
func msgToConsensusPbMsg(data []byte, tyr raftproto.RaftMessage_Type, replicaID uint64) *pb.Message {
rm := &raftproto.RaftMessage{
Type: tyr,
FromId: replicaID,
Data: data,
}
cmData, err := rm.Marshal()
if err != nil {
return nil
}
msg := &pb.Message{
Type: pb.Message_CONSENSUS,
Data: cmData,
}
return msg
}
func (n *Node) getSnapshot() ([]byte, error) {
cm := pb.ChainMeta{
Height: n.lastExec,
}
return cm.Marshal()
}
func (n *Node) recoverFromSnapshot() {
snapshot, err := n.raftStorage.snap.Load()
if err != nil {
n.logger.Errorf("load snapshot failed: %s", err.Error())
return
}
targetChainMeta := &pb.ChainMeta{}
err = targetChainMeta.Unmarshal(snapshot.Data)
if err != nil {
n.logger.Errorf("unmarshal target chain meta error: %s", err.Error())
return
}
syncBlocks := func() {
chainMeta := n.getChainMetaFunc()
n.logger.WithFields(logrus.Fields{
"target": targetChainMeta.Height,
"current": chainMeta.Height,
"current_hash": chainMeta.BlockHash.String(),
}).Info("State Update")
blockCh := make(chan *pb.Block, 1024)
go n.syncer.SyncCFTBlocks(chainMeta.Height+1, targetChainMeta.Height, blockCh)
for block := range blockCh {
// indicates that the synchronization blocks function has been completed
if block == nil {
break
}
if block.Height() == n.lastExec+1 {
localList := make([]bool, len(block.Transactions.Transactions))
for i := 0; i < len(block.Transactions.Transactions); i++ {
localList[i] = false
}
executeEvent := &pb.CommitEvent{
Block: block,
LocalList: localList,
}
n.commitC <- executeEvent
n.lastExec = block.Height()
}
}
}
for {
syncBlocks()
if n.lastExec == targetChainMeta.Height {
break
}
n.logger.Warnf("The lastExec is %d, but not equal the target block height %d", n.lastExec, targetChainMeta.Height)
}
n.appliedIndex = snapshot.Metadata.Index
n.snapshotIndex = snapshot.Metadata.Index
}