-
Notifications
You must be signed in to change notification settings - Fork 4
/
peers.go
128 lines (122 loc) · 3.22 KB
/
peers.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
package server
import (
pb "github.com/PomeloCloud/BFTRaft4go/proto/server"
"github.com/PomeloCloud/BFTRaft4go/utils"
"github.com/dgraph-io/badger"
"github.com/golang/protobuf/proto"
"log"
"strconv"
)
func GetGroupPeersFromKV(txn *badger.Txn, group uint64) map[uint64]*pb.Peer {
peers := map[uint64]*pb.Peer{}
keyPrefix := ComposeKeyPrefix(group, GROUP_PEERS)
iter := txn.NewIterator(badger.IteratorOptions{})
iter.Seek(append(keyPrefix, utils.U64Bytes(0)...)) // seek the head
for iter.ValidForPrefix(keyPrefix) {
item := iter.Item()
item_data := ItemValue(item)
peer := pb.Peer{}
proto.Unmarshal(*item_data, &peer)
peers[peer.Id] = &peer
iter.Next()
}
iter.Close()
return peers
}
func (m *RTGroup) PeerUncommittedLogEntries(peer *pb.Peer) ([]*pb.LogEntry, *pb.LogEntry) {
entries_ := []*pb.LogEntry{}
prevEntry := &pb.LogEntry{
Term: 0,
Index: 0,
}
m.Server.DB.View(func(txn *badger.Txn) error {
entries := []*pb.LogEntry{}
iter := m.ReversedLogIterator(txn)
nextLogIdx := peer.NextIndex
for true {
entry := iter.Current()
if entry == nil {
break
}
prevEntry = entry
if entry.Index < nextLogIdx {
break
}
entries = append(entries, entry)
iter.Next()
}
if peer.NextIndex == 0 && peer.MatchIndex == 0 {
// new peer, should set prevEntry = 0
prevEntry = &pb.LogEntry{
Term: 0,
Index: 0,
}
}
// reverse so the first will be the one with least index
if len(entries) > 1 {
for i := 0; i < len(entries)/2; i++ {
j := len(entries) - i - 1
entries[i], entries[j] = entries[j], entries[i]
}
}
entries_ = entries
return nil
})
log.Println("prev index for", peer.Id, "is", prevEntry.Index, peer.MatchIndex, "/", peer.NextIndex)
return entries_, prevEntry
}
func (s *BFTRaftServer) ScanHostedGroups(serverId uint64) map[uint64]*RTGroup {
scanKey := utils.U32Bytes(GROUP_PEERS)
res := map[uint64]*RTGroup{}
s.DB.View(func(txn *badger.Txn) error {
iter := txn.NewIterator(badger.IteratorOptions{})
iter.Seek(scanKey)
groups := map[uint64]*RTGroup{}
for iter.ValidForPrefix(scanKey) {
item := iter.Item()
val := ItemValue(item)
peer := &pb.Peer{}
proto.Unmarshal(*val, peer)
if peer.Id == serverId {
group := GetGroupFromKV(txn, peer.Group)
if group != nil {
defaultLeader := uint64(0)
if len(s.GetGroupHosts(txn, group.Id)) < 2 {
defaultLeader = s.Id
}
groups[peer.Group] = NewRTGroup(
s, defaultLeader,
GetGroupPeersFromKV(txn, peer.Group),
group, FOLLOWER,
)
}
}
iter.Next()
}
iter.Close()
res = groups
return nil
})
log.Println("found", len(res), "hosted groups")
for groupId, meta := range res {
log.Println("scanned group:", meta.Group.Id)
k := strconv.Itoa(int(groupId))
s.GroupsOnboard.Set(k, meta)
}
return res
}
func (m *RTGroup) OnboardGroupPeersSlice() []*pb.Peer {
peers := []*pb.Peer{}
for _, peer := range m.GroupPeers {
peers = append(peers, peer)
}
return peers
}
func (s *BFTRaftServer) SavePeer(txn *badger.Txn, peer *pb.Peer) error {
if data, err := proto.Marshal(peer); err == nil {
dbKey := append(ComposeKeyPrefix(peer.Group, GROUP_PEERS), utils.U64Bytes(peer.Id)...)
return txn.Set(dbKey, data, 0x00)
} else {
return err
}
}