-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
68 lines (49 loc) · 1.37 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"log"
"math/rand"
"strconv"
"testing"
"time"
"github.com/krithikvaidya/distributed-dns/raft"
)
var n_replica int
func init() {
/*
* Workaround for a Go bug
* The Init() function for the testing package should be called
* before our init() function for parsing the command-line arguments
* of the `go test` command
*/
testing.Init()
// Command line parameters
flag.IntVar(&n_replica, "n", 5, "total number of replicas (default=5)")
flag.Parse()
log.SetFlags(0) // Turn off timestamps in log output.
rand.Seed(time.Now().UnixNano())
}
func main() {
log.Println("Raft-based Replicated Key Value Store")
log.Printf("Enter the replica's id: ")
var rid int
fmt.Scanf("%d", &rid)
master_context, master_cancel := context.WithCancel(context.Background())
node := raft.Setup_raft_node(master_context, rid, n_replica, false)
node.Meta.Master_ctx = master_context
node.Meta.Master_cancel = master_cancel
// Store the gRPC address of other replicas
rep_addrs := make([]string, n_replica)
for i := 0; i < n_replica; i++ {
if i == rid {
continue
}
rep_addrs[i] = ":500" + strconv.Itoa(i)
}
// Perform steps necessary to setup the node as an active replica.
node.Connect_raft_node(master_context, rid, rep_addrs, false)
log.Printf("Node initialization successful")
node.ListenForShutdown(master_cancel)
}