-
Notifications
You must be signed in to change notification settings - Fork 127
/
sync.go
171 lines (153 loc) · 4.95 KB
/
sync.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
package p2p
import (
"fmt"
"time"
"github.com/MixinNetwork/mixin/common"
"github.com/MixinNetwork/mixin/config"
"github.com/MixinNetwork/mixin/crypto"
"github.com/MixinNetwork/mixin/logger"
)
func (me *Peer) cacheReadSnapshotsForNodeRound(nodeId crypto.Hash, number uint64) ([]*common.SnapshotWithTopologicalOrder, error) {
return me.handle.ReadSnapshotsForNodeRound(nodeId, number)
}
func (me *Peer) cacheReadSnapshotsSinceTopology(offset, limit uint64) ([]*common.SnapshotWithTopologicalOrder, error) {
return me.handle.ReadSnapshotsSinceTopology(offset, limit)
}
// FIXME this could result in a very small topology due to already removed node
// and sync to neighbor since this offset will take substantial time
func (me *Peer) compareRoundGraphAndGetTopologicalOffset(p *Peer, local, remote []*SyncPoint) (uint64, error) {
remoteFilter := make(map[crypto.Hash]*SyncPoint)
for _, p := range remote {
remoteFilter[p.NodeId] = p
}
var offset uint64
for _, l := range local {
r := remoteFilter[l.NodeId]
if r == nil || r.Number > l.Number {
continue
}
number := r.Number + 2 // because the node may be stale or removed, and with cache
logger.Verbosef("network.sync compareRoundGraphAndGetTopologicalOffset %s try %s:%d\n", p.IdForNetwork, l.NodeId, number)
ss, err := me.cacheReadSnapshotsForNodeRound(l.NodeId, number)
if err != nil {
return offset, err
}
if len(ss) == 0 {
logger.Verbosef("network.sync compareRoundGraphAndGetTopologicalOffset %s local round empty %s:%d:%d\n", p.IdForNetwork, l.NodeId, number, l.Number)
continue
}
topo := ss[0].TopologicalOrder
if offset == 0 || topo < offset {
offset = topo
}
}
return offset, nil
}
func (me *Peer) syncToNeighborSince(graph map[crypto.Hash]*SyncPoint, p *Peer, offset uint64) (uint64, error) {
logger.Verbosef("network.sync syncToNeighborSince %s %d\n", p.IdForNetwork, offset)
limit := 200
snapshots, err := me.cacheReadSnapshotsSinceTopology(offset, uint64(limit))
if err != nil {
return offset, err
}
for _, s := range snapshots {
var remoteRound uint64
if r := graph[s.NodeId]; r != nil {
remoteRound = r.Number
}
if s.RoundNumber < remoteRound {
offset = s.TopologicalOrder
continue
}
if s.RoundNumber >= remoteRound+config.SnapshotReferenceThreshold*2 {
return offset, fmt.Errorf("FUTURE %s %d %d", s.NodeId, s.RoundNumber, remoteRound)
}
err := me.SendSnapshotFinalizationMessage(p.IdForNetwork, s.Snapshot)
if err != nil {
return offset, err
}
offset = s.TopologicalOrder
}
time.Sleep(100 * time.Millisecond)
if len(snapshots) < limit {
return offset, fmt.Errorf("EOF")
}
return offset, nil
}
func (me *Peer) syncHeadRoundToRemote(local, remote map[crypto.Hash]*SyncPoint, p *Peer, nodeId crypto.Hash) {
var localFinal, remoteFinal uint64
if r := remote[nodeId]; r != nil {
remoteFinal = r.Number
}
if l := local[nodeId]; l != nil {
localFinal = l.Number
}
if remoteFinal > localFinal {
return
}
logger.Verbosef("network.sync syncHeadRoundToRemote %s %s:%d\n", p.IdForNetwork, nodeId, remoteFinal)
for i := remoteFinal; i <= remoteFinal+config.SnapshotReferenceThreshold+2; i++ {
ss, _ := me.cacheReadSnapshotsForNodeRound(nodeId, i)
for _, s := range ss {
me.SendSnapshotFinalizationMessage(p.IdForNetwork, s.Snapshot)
}
}
}
func (me *Peer) syncToNeighborLoop(p *Peer) {
defer close(p.stn)
for !me.closing && !p.closing {
graph, offset := me.getSyncPointOffset(p)
logger.Verbosef("network.sync syncToNeighborLoop getSyncPointOffset %s %d %v\n", p.IdForNetwork, offset, graph != nil)
if graph == nil {
time.Sleep(time.Duration(config.SnapshotRoundGap))
continue
}
points := me.handle.BuildGraph()
nodes := me.handle.ReadAllNodesWithoutState()
local := make(map[crypto.Hash]*SyncPoint)
for _, n := range points {
local[n.NodeId] = n
}
for _, n := range nodes {
me.syncHeadRoundToRemote(local, graph, p, n)
}
for !me.closing && !p.closing && offset > 0 {
off, err := me.syncToNeighborSince(graph, p, offset)
if err != nil {
logger.Verbosef("network.sync syncToNeighborLoop syncToNeighborSince %s %d DONE with %s", p.IdForNetwork, offset, err)
break
}
offset = off
}
}
}
func (me *Peer) getSyncPointOffset(p *Peer) (map[crypto.Hash]*SyncPoint, uint64) {
var offset uint64
var graph map[crypto.Hash]*SyncPoint
startAt := time.Now()
for !me.closing && !p.closing {
item, err := p.syncRing.Poll(false)
if err != nil {
break
} else if item == nil {
time.Sleep(100 * time.Millisecond)
continue
}
g := item.([]*SyncPoint)
graph = make(map[crypto.Hash]*SyncPoint)
for _, r := range g {
graph[r.NodeId] = r
}
off, err := me.compareRoundGraphAndGetTopologicalOffset(p, me.handle.BuildGraph(), g)
if err != nil {
logger.Verbosef("network.sync compareRoundGraphAndGetTopologicalOffset %s error %v\n", p.IdForNetwork, err)
}
if off > 0 {
offset = off
}
if startAt.Add(time.Second).Before(time.Now()) {
return graph, offset
}
}
return nil, 0
}