Skip to content

Commit 2314389

Browse files
committed
Updating project structure
1 parent 44e478d commit 2314389

10 files changed

+77
-63
lines changed

Diff for: server/bullyElection.go renamed to bullyElection.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
package server
1+
package election
22

33
// BullyElection implements the bully election algorithm
44
type BullyElection struct {
55
}
66

7-
func (b *BullyElection) startElection(s *Server) {
7+
// StartElection runs the bully election algorithm on the provided server
8+
func (b BullyElection) StartElection(s *Server) {
89
s.emitter.Write(s.id, "", "ELECTION_STARTED")
910
if isHighest(s) {
1011
notifyLow(s)
@@ -14,9 +15,15 @@ func (b *BullyElection) startElection(s *Server) {
1415
s.emitter.Write(s.id, "", "ELECTION_ENDED")
1516
}
1617

17-
func (b *BullyElection) connectServers(s map[string]*Server) map[string]*Server {
18-
19-
return nil
18+
// ConnectServers links the input servers in accordance with the bully algorithm
19+
func (b BullyElection) ConnectServers(s map[string]*Server) {
20+
for currKey, currserver := range s {
21+
for key, server := range s {
22+
if currKey != key {
23+
currserver.NeighborServers[key] = server
24+
}
25+
}
26+
}
2027
}
2128

2229
func isHighest(s *Server) bool {

Diff for: cluster.go

+13-18
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,30 @@ package election
33
import (
44
"errors"
55
"time"
6-
7-
"github.com/LostLaser/election/emitter"
8-
"github.com/LostLaser/election/server"
96
)
107

118
// Cluster is a linked collection of servers
129
type Cluster struct {
13-
linkedServers map[string]*server.Server
14-
emitter *emitter.Emitter
15-
electionAlgorithm server.Election
10+
linkedServers map[string]*Server
11+
emitter *Emitter
12+
electionAlgorithm Election
1613
}
1714

1815
// New will create a cluster with the specified number of servers
19-
func New(serverCount int, heartbeatPause time.Duration) *Cluster {
16+
func New(serverCount int, heartbeatPause time.Duration, algorithm Election) *Cluster {
2017
c := new(Cluster)
21-
c.linkedServers = make(map[string]*server.Server)
22-
c.emitter = emitter.New(serverCount * 10)
23-
c.electionAlgorithm = &server.BullyElection{}
18+
c.linkedServers = make(map[string]*Server)
19+
c.emitter = NewEmitter(serverCount * 10)
20+
c.electionAlgorithm = algorithm
2421

2522
for i := 0; i < serverCount; i++ {
26-
s := server.New(c.emitter, heartbeatPause, c.electionAlgorithm)
23+
s := NewServer(c.emitter, heartbeatPause, c.electionAlgorithm)
2724
c.linkedServers[s.GetID()] = s
2825
}
29-
for currKey, currserver := range c.linkedServers {
30-
for key, server := range c.linkedServers {
31-
if currKey != key {
32-
currserver.NeighborServers[key] = server
33-
}
34-
}
26+
27+
c.electionAlgorithm.ConnectServers(c.linkedServers)
28+
29+
for _, currserver := range c.linkedServers {
3530
go currserver.Initialize()
3631
}
3732

@@ -78,7 +73,7 @@ func (c Cluster) ReadEvent() map[string]string {
7873
return c.emitter.Read()
7974
}
8075

81-
func (c Cluster) getServerByID(id string) (*server.Server, error) {
76+
func (c Cluster) getServerByID(id string) (*Server, error) {
8277
for key, s := range c.linkedServers {
8378
if id == key {
8479
return s, nil

Diff for: cluster_test.go

+15-8
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import (
88
func TestNew(t *testing.T) {
99
expectedServerCount := 3
1010
cycleTime := time.Second
11+
algorithm := &BullyElection{}
1112

12-
cluster := New(expectedServerCount, cycleTime)
13+
cluster := New(expectedServerCount, cycleTime, algorithm)
1314

1415
actualServerCount := len(cluster.linkedServers)
1516
if actualServerCount != expectedServerCount {
@@ -18,11 +19,12 @@ func TestNew(t *testing.T) {
1819
}
1920

2021
func TestNeighbors(t *testing.T) {
21-
expectedServerCount := 3
22+
expectedServerCount := 5
2223
expectedNeighborCount := expectedServerCount - 1
2324
cycleTime := time.Second
25+
algorithm := &BullyElection{}
2426

25-
cluster := New(expectedServerCount, cycleTime)
27+
cluster := New(expectedServerCount, cycleTime, algorithm)
2628
for _, server := range cluster.linkedServers {
2729
neighborCount := len(server.NeighborServers)
2830
if neighborCount != expectedNeighborCount {
@@ -34,8 +36,9 @@ func TestNeighbors(t *testing.T) {
3436
func TestServerListingCount(t *testing.T) {
3537
expectedServerCount := 3
3638
cycleTime := time.Second
39+
algorithm := &BullyElection{}
3740

38-
cluster := New(expectedServerCount, cycleTime)
41+
cluster := New(expectedServerCount, cycleTime, algorithm)
3942

4043
actualServerCount := len(cluster.ServerIds())
4144
if actualServerCount != expectedServerCount {
@@ -46,8 +49,9 @@ func TestServerListingCount(t *testing.T) {
4649
func TestServerListingConsistency(t *testing.T) {
4750
serverCount := 3
4851
cycleTime := time.Second
52+
algorithm := &BullyElection{}
4953

50-
cluster := New(serverCount, cycleTime)
54+
cluster := New(serverCount, cycleTime, algorithm)
5155

5256
for _, i := range cluster.ServerIds() {
5357
found := false
@@ -66,8 +70,9 @@ func TestServerListingConsistency(t *testing.T) {
6670
func TestReadEvent(t *testing.T) {
6771
expectedServerCount := 3
6872
cycleTime := time.Second
73+
algorithm := &BullyElection{}
6974

70-
cluster := New(expectedServerCount, cycleTime)
75+
cluster := New(expectedServerCount, cycleTime, algorithm)
7176
c := make(chan (int))
7277

7378
go func() {
@@ -88,8 +93,9 @@ func TestReadEvent(t *testing.T) {
8893
func TestStop(t *testing.T) {
8994
expectedServerCount := 3
9095
cycleTime := time.Second
96+
algorithm := &BullyElection{}
9197

92-
cluster := New(expectedServerCount, cycleTime)
98+
cluster := New(expectedServerCount, cycleTime, algorithm)
9399
serverIds := cluster.ServerIds()
94100

95101
if len(cluster.ServerIds()) == 0 {
@@ -107,9 +113,10 @@ func TestStop(t *testing.T) {
107113
func TestStopInvl(t *testing.T) {
108114
expectedServerCount := 3
109115
cycleTime := time.Second
116+
algorithm := &BullyElection{}
110117
id := "invl"
111118

112-
cluster := New(expectedServerCount, cycleTime)
119+
cluster := New(expectedServerCount, cycleTime, algorithm)
113120
err := cluster.StopServer("invl")
114121
if err == nil {
115122
t.Errorf("No error recieved for invalid id: %s", id)

Diff for: electionType.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package election
2+
3+
// Election is an interface for all supported election types
4+
type Election interface {
5+
StartElection(*Server)
6+
ConnectServers(map[string]*Server)
7+
}

Diff for: emitter/emitter.go renamed to emitter.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package emitter
1+
package election
22

33
//Emitter is a specialized queue for messaging
44
type Emitter struct {
55
messages chan map[string]string
66
}
77

8-
//New creates an instance of an emitter
9-
func New(bufferSize int) *Emitter {
8+
//NewEmitter creates an instance of an emitter
9+
func NewEmitter(bufferSize int) *Emitter {
1010
e := new(Emitter)
1111
e.messages = make(chan map[string]string, bufferSize)
1212

Diff for: server/process.go renamed to process.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package server
1+
package election
22

33
import (
44
"time"
@@ -8,7 +8,7 @@ func (s *Server) run() {
88
for {
99
if s.state == running {
1010
if !s.pingMaster() || s.triggerElection {
11-
s.electionAlgorithm.startElection(s)
11+
s.electionAlgorithm.StartElection(s)
1212
s.triggerElection = false
1313
}
1414
}

Diff for: ringElection.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package election
2+
3+
// RingElection implements the ring election algorithm
4+
type RingElection struct {
5+
}
6+
7+
// StartElection runs the ring election algorithm on the provided server
8+
func (r *RingElection) StartElection(s *Server) {
9+
return
10+
}
11+
12+
// ConnectServers links the input servers in accordance with the ring election algorithm
13+
func (r *RingElection) ConnectServers(s map[string]*Server) {
14+
for currKey, currserver := range s {
15+
for key, server := range s {
16+
if currKey != key {
17+
currserver.NeighborServers[key] = server
18+
}
19+
}
20+
}
21+
}

Diff for: server/server.go renamed to server.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
package server
1+
package election
22

33
import (
44
"crypto/rand"
55
"fmt"
66
"log"
77
"sync"
88
"time"
9-
10-
"github.com/LostLaser/election/emitter"
119
)
1210

1311
// Server is a single entity
@@ -19,7 +17,7 @@ type Server struct {
1917
electionAlgorithm Election
2018
electionLock sync.Mutex
2119
triggerElection bool
22-
emitter *emitter.Emitter
20+
emitter *Emitter
2321
heartbeatPause time.Duration
2422
}
2523

@@ -28,8 +26,8 @@ const (
2826
stopped = "stopped"
2927
)
3028

31-
// New will create a cluster with the specified number of servers
32-
func New(e *emitter.Emitter, heartbeatPause time.Duration, electionAlgorithm Election) *Server {
29+
// NewServer will create a cluster with the specified number of servers
30+
func NewServer(e *Emitter, heartbeatPause time.Duration, electionAlgorithm Election) *Server {
3331
s := new(Server)
3432
s.id = generateUniqueID()
3533
s.state = running

Diff for: server/election.go

-7
This file was deleted.

Diff for: server/ringElection.go

-14
This file was deleted.

0 commit comments

Comments
 (0)