-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
124 lines (104 loc) · 3.04 KB
/
config.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
package PaxiBFT
import (
"encoding/json"
"flag"
"os"
"github.com/salemmohammed/PaxiBFT/log"
)
var configFile = flag.String("config", "config.json", "Configuration file for paxi replica. Defaults to config.json.")
// Config contains every system configuration
type Config struct {
Addrs map[ID]string `json:"address"` // address for node communication
HTTPAddrs map[ID]string `json:"http_address"` // address for client server communication
Policy string `json:"policy"` // leader change policy {consecutive, majority}
Threshold float64 `json:"threshold"` // threshold for policy in WPaxos {n consecutive or time interval in ms}
Thrifty bool `json:"thrifty"` // only send messages to a quorum
BufferSize int `json:"buffer_size"` // buffer size for maps
ChanBufferSize int `json:"chan_buffer_size"` // buffer size for channels
MultiVersion bool `json:"multiversion"` // create multi-version database
Benchmark Bconfig `json:"benchmark"` // benchmark configuration
// for future implementation
// Batching bool `json:"batching"`
// Consistency string `json:"consistency"`
// Codec string `json:"codec"` // codec for message serialization between nodes
n int // total number of nodes
z int // total number of zones
npz map[int]int // nodes per zone
}
// Config is global configuration singleton generated by init() func below
var config Config
func init() {
config = MakeDefaultConfig()
}
// GetConfig returns paxi package configuration
func GetConfig() Config {
return config
}
// Simulation enable go channel transportation to simulate distributed environment
func Simulation() {
*scheme = "chan"
}
// MakeDefaultConfig returns Config object with few default values
// only used by init() and master
func MakeDefaultConfig() Config {
return Config{
Policy: "consecutive",
Threshold: 3,
BufferSize: 1024,
ChanBufferSize: 1024,
MultiVersion: false,
Benchmark: DefaultBConfig(),
}
}
// IDs returns all node ids
func (c Config) IDs() []ID {
ids := make([]ID, 0)
for id := range c.Addrs {
ids = append(ids, id)
}
return ids
}
// N returns total number of nodes
func (c Config) N() int {
return c.n
}
// Z returns total number of zones
func (c Config) Z() int {
return c.z
}
// String is implemented to print the config
func (c Config) String() string {
config, err := json.Marshal(c)
if err != nil {
log.Error(err)
return ""
}
return string(config)
}
// Load loads configuration from config file in JSON format
func (c *Config) Load() {
file, err := os.Open(*configFile)
if err != nil {
log.Fatal(err)
}
decoder := json.NewDecoder(file)
err = decoder.Decode(c)
if err != nil {
log.Fatal(err)
}
c.npz = make(map[int]int)
for id := range c.Addrs {
c.n++
c.npz[id.Zone()]++
}
c.z = len(c.npz)
}
// Save saves configuration to file in JSON format
func (c Config) Save() error {
file, err := os.Create(*configFile)
if err != nil {
return err
}
encoder := json.NewEncoder(file)
return encoder.Encode(c)
}