/
sitenode_parteners.go
63 lines (52 loc) · 1.67 KB
/
sitenode_parteners.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
package federation
import "github.com/tg123/phabrik/common"
type PartnerNodeInfo struct {
Instance NodeInstance // The node instance of the node which this information is of.
Phase NodePhase // The phase of the node.
Address string // The address of the node.
Token RoutingToken // The token owned by this node.
LeaseAgentAddress string // The address of the lease agent for this node.
LeaseAgentInstanceId int64 // Instance of the lease agent.
EndToEnd bool
NodeFaultDomainId common.Uri // The fault domain setting of this node.
RingName string
}
func (s *SiteNode) updatePartnerNode(h *FederationPartnerNodeHeader) {
s.partenersRWLock.RLock()
old, found := s.parteners[h.Instance.Id]
s.partenersRWLock.RUnlock()
if !found || old.Instance.InstanceId < h.Instance.InstanceId {
p := h.PartnerNodeInfo
s.partenersRWLock.Lock()
s.parteners[h.Instance.Id] = &p
s.partenersRWLock.Unlock()
s.onPartnerChanged(p, !found)
}
}
func (s *SiteNode) KnownPartnerNodes(filter func(PartnerNodeInfo) bool) []PartnerNodeInfo {
s.partenersRWLock.RLock()
defer s.partenersRWLock.RUnlock()
var lst []PartnerNodeInfo
for _, p := range s.parteners {
if filter(*p) {
lst = append(lst, *p)
}
}
return lst
}
func (s *SiteNode) closestPartnerNode(id NodeID, filter func(PartnerNodeInfo) bool) *PartnerNodeInfo {
// TODO optimize
var cur *PartnerNodeInfo
max := u128max
for _, p := range s.KnownPartnerNodes(filter) {
diff := uint128(p.Instance.Id).sub(uint128(id))
if diff == u128zero {
return &p
}
if diff.cmp(max) < 0 {
max = diff
cur = &p
}
}
return cur
}