-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.go
170 lines (152 loc) · 3.72 KB
/
configuration.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
// Copyright (c) 2019 Meng Huang ([email protected])
// This package is licensed under a MIT license that can be found in the LICENSE file.
package raft
import (
"encoding/json"
"fmt"
"reflect"
"sort"
"sync"
)
type configuration struct {
node *node
mu sync.Mutex
members map[string]*Member
}
func newConfiguration(n *node) *configuration {
c := &configuration{
node: n,
members: make(map[string]*Member),
}
c.load()
if _, ok := c.members[c.node.address]; !ok {
c.AddMember(&Member{Address: c.node.address})
}
return c
}
func (c *configuration) SetMembers(members []*Member) {
c.mu.Lock()
for _, v := range members {
c.members[v.Address] = v
}
c.mu.Unlock()
c.save()
}
func (c *configuration) AddMember(member *Member) {
c.mu.Lock()
c.members[member.Address] = member
c.mu.Unlock()
c.save()
}
func (c *configuration) RemoveMember(addr string) {
c.mu.Lock()
if _, ok := c.members[addr]; ok {
delete(c.members, addr)
}
c.mu.Unlock()
c.save()
}
func (c *configuration) LookupMember(addr string) (member *Member) {
c.mu.Lock()
if v, ok := c.members[addr]; ok {
member = v
}
c.mu.Unlock()
return
}
func (c *configuration) Peers() []string {
c.mu.Lock()
peers := make([]string, 0, len(c.members))
for _, v := range c.members {
if v.Address != c.node.address {
peers = append(peers, v.Address)
}
}
c.mu.Unlock()
return peers
}
func (c *configuration) Members() []*Member {
c.mu.Lock()
members := make([]*Member, 0, len(c.members))
for _, v := range c.members {
member := &Member{}
*member = *v
members = append(members, member)
}
c.mu.Unlock()
return members
}
func (c *configuration) membership() []string {
c.mu.Lock()
ms := make([]string, 0, len(c.members))
for _, v := range c.members {
ms = append(ms, fmt.Sprintf("%s;%t", v.Address, v.NonVoting))
}
c.mu.Unlock()
return ms
}
func (c *configuration) save() {
m := members{}
c.mu.Lock()
for _, v := range c.members {
m = append(m, v)
}
sort.Sort(m)
storage := &ConfigurationStorage{Members: m}
c.mu.Unlock()
b, _ := json.Marshal(storage)
c.node.storage.OverWrite(defaultConfig, b)
}
func (c *configuration) load() error {
b, err := c.node.storage.Load(defaultConfig)
if err != nil {
return err
}
storage := &ConfigurationStorage{}
json.Unmarshal(b, storage)
c.mu.Lock()
for _, v := range storage.Members {
c.members[v.Address] = v
}
c.mu.Unlock()
if !c.membershipChanges() {
//logger.Tracef("configuration.load !membershipChanges")
return nil
}
lastNodesCount := c.node.NodesCount()
c.node.deleteNotPeers(c.Peers())
for _, v := range storage.Members {
c.node.addNode(v)
}
nodesCount := c.node.NodesCount()
if lastNodesCount != nodesCount {
c.node.logger.Tracef("configuration.load %s NodesCount %d==>%d", c.node.address, lastNodesCount, nodesCount)
}
return nil
}
func (c *configuration) reset() {
c.mu.Lock()
c.node.storage.Truncate(defaultConfig, 0)
c.members = make(map[string]*Member)
c.mu.Unlock()
}
func (c *configuration) reconfiguration() error {
lastVotingsCount := c.node.votingsCount()
c.node.consideredForMajorities()
votingsCount := c.node.votingsCount()
if lastVotingsCount != votingsCount {
c.node.logger.Tracef("configuration.reconfiguration %s VotingsCount %d==>%d", c.node.address, lastVotingsCount, votingsCount)
}
return nil
}
func (c *configuration) membershipChanges() bool {
oldMembership := c.node.membership()
newMembership := c.membership()
sort.Strings(oldMembership)
sort.Strings(newMembership)
return !reflect.DeepEqual(oldMembership, newMembership)
}
type members []*Member
func (m members) Len() int { return len(m) }
func (m members) Less(i, j int) bool { return m[i].Address < m[j].Address }
func (m members) Swap(i, j int) { m[i], m[j] = m[j], m[i] }