-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented synchronization between peers
- Loading branch information
Showing
19 changed files
with
634 additions
and
308 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,25 @@ | ||
run-peer: | ||
go run cmd/* --api-port 3001 | ||
|
||
BINARY_NAME=peer | ||
LEADER_RPC_PORT=51500 | ||
|
||
run-leader: | ||
go run cmd/* --api-port 3001 --grpc-port ${LEADER_RPC_PORT} --name peer-0 | ||
|
||
run-follower-1: | ||
go run cmd/* --api-port 3001 --grpc-port 51501 --leader localhost:${LEADER_RPC_PORT} --name peer-1 | ||
|
||
run-follower-2: | ||
go run cmd/* --api-port 3001 --grpc-port 51502 --leader localhost:${LEADER_RPC_PORT} --name peer-2 | ||
|
||
run-leader-and-follower: | ||
make run-leader &1 | ||
make run-follower | ||
|
||
gen-proto: | ||
protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative protocol/cluster.proto | ||
|
||
build: | ||
go build -o bin/peer cmd/* | ||
make gen-proto | ||
GOARCH=amd64 GOOS=darwin go build -pgo=auto -o bin/${BINARY_NAME}-darwin cmd/* | ||
GOARCH=amd64 GOOS=linux go build -pgo=auto -o bin/${BINARY_NAME}-linux cmd/* | ||
GOARCH=amd64 GOOS=windows go build -pgo=auto -o bin/${BINARY_NAME}-windows cmd/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,32 @@ | ||
package config | ||
|
||
import ( | ||
"io" | ||
"log" | ||
"os" | ||
) | ||
|
||
func setupLogDir() { | ||
dest := GetEnv().TmpDir | ||
dest := GetEnv().LogDir | ||
_, err := os.Stat(dest) | ||
if err != nil { | ||
if err := os.MkdirAll(dest, os.ModePerm); err != nil { | ||
panic(err) | ||
} | ||
} | ||
} | ||
|
||
func EnableLogging(filename string) { | ||
logFile, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
// multi writer for writing logs both to console & file | ||
mw := io.MultiWriter(logFile, os.Stdout) | ||
log.SetOutput(mw) | ||
} | ||
|
||
func DisableLoggin() { | ||
log.SetOutput(os.Stdout) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package internal | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"sync" | ||
|
||
"github.com/gokul656/raft-consensus/common" | ||
"github.com/gokul656/raft-consensus/config" | ||
"github.com/gokul656/raft-consensus/peer" | ||
"github.com/gokul656/raft-consensus/protocol" | ||
) | ||
|
||
var Cluster *peer.RaftHub | ||
var RPCServer protocol.ClusterServer | ||
var once sync.Once | ||
|
||
func StartupRaft() { | ||
log.Println("Setting up cluster...") | ||
once.Do( | ||
func() { | ||
defer common.HandlePanic("raft_state") | ||
|
||
env := config.GetEnv() | ||
Cluster = peer.NewRaft(&peer.Peer{ | ||
Address: fmt.Sprintf("localhost:%s", env.RPCPort), | ||
Name: env.InstanceID, | ||
State: protocol.PeerState_FOLLOWER.Enum(), | ||
}) | ||
|
||
Cluster.AddPeer(env.InstanceID, fmt.Sprintf("localhost:%s", env.RPCPort)) | ||
Cluster.Self = Cluster.GetPeer(env.InstanceID) | ||
|
||
// if there are no leaders, the current peer elects itself as leader & initiates election | ||
if env.Leader == "" { | ||
runAsLeader() | ||
} else { | ||
runAsFollower() | ||
} | ||
}, | ||
) | ||
} | ||
|
||
func runAsLeader() { | ||
env := config.GetEnv() | ||
Cluster.ChangeLeader(env.InstanceID) | ||
|
||
go Cluster.CheckFollowersHealth() | ||
} | ||
|
||
func runAsFollower() { | ||
env := config.GetEnv() | ||
|
||
Cluster.AddPeer(env.LeaderID, env.Leader) | ||
Cluster.ChangeLeader(env.LeaderID) | ||
|
||
err := Cluster.Register(context.Background(), &protocol.AddPeerRequest{ | ||
Peer: &protocol.Peer{ | ||
Name: Cluster.Self.Name, | ||
Address: Cluster.Self.Address, | ||
}, | ||
}) | ||
|
||
if err != nil { | ||
log.Fatalln("[CRITICAL] Unable to register as Follower", err) | ||
} | ||
|
||
Cluster.Synchronize() | ||
go Cluster.CheckLeaderHealth() | ||
} |
Oops, something went wrong.