-
Notifications
You must be signed in to change notification settings - Fork 2
/
election.go
75 lines (63 loc) · 1.93 KB
/
election.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
package multi_torrent
import (
. "github.com/danalex97/Speer/interfaces"
"github.com/danalex97/nfsTorrent/cache_torrent"
"strconv"
)
// A MultiElection component is made out of multiple Election components. When
// an election takes place, the nodes that were elected get banned for the
// purpose of them not getting reelected. The banning process is done by
// eliminating all the banned candidates from all the elections that did not
// take place yet. For this algorithm to be correct, we require the number of
// sub-Torrents to be smaller than 100/leaderPercent.
type MultiElection struct {
limit int
nodes int
elections map[string]*cache_torrent.Election
}
func NewMultiElection(elections int, limit int, transport Transport) *MultiElection {
e := &MultiElection{
limit : limit,
nodes : 0,
elections : make(map[string]*cache_torrent.Election),
}
proxy := NewStripProxy(transport)
for i := 0; i < elections; i++ {
e.elections[strconv.Itoa(i)] = cache_torrent.NewElection(limit, proxy)
}
return e
}
func (e *MultiElection) Run() {
}
func (e *MultiElection) NewJoin(id string) {
for i, election := range e.elections {
election.NewJoin(FullId(id, i))
}
}
func (e *MultiElection) Recv(m interface {}) {
switch candidate := m.(type) {
case cache_torrent.Candidate:
e.RegisterCandidate(candidate)
}
}
func (e *MultiElection) RegisterCandidate(candidate cache_torrent.Candidate) {
e.nodes++
for i, election := range e.elections {
election.RegisterCandidate(cache_torrent.Candidate{
Id : FullId(candidate.Id, i),
Up : candidate.Up,
Down : candidate.Down,
})
if e.nodes == e.limit {
// Eliminate the already elected candidates
toRemove := election.GetElected()
for j, curr := range e.elections {
if i != j {
for _, id := range toRemove {
curr.RemoveCandidate(FullId(ExternId(id), j))
}
}
}
}
}
}