forked from ailidani/paxi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
129 lines (111 loc) · 3.74 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
125
126
127
128
129
package paxi
import (
"encoding/json"
"flag"
"os"
"github.com/ailidani/paxi/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
Algorithm string `json:"algorithm"` // replication algorithm name
Quorum string `json:"quorum"` // type of the quorums
F int `json:"f"` // number of failure zones in general grid quorums
Transport string `json:"transport"` // not used
ReplyWhenCommit bool `json:"reply_when_commit"` // reply to client when request is committed, instead of executed
Adaptive bool `json:"adaptive"` // adaptive leader change, if true paxos forward request to current leader
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}
BackOff int `json:"backoff"` // random backoff interval
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
}
func Simulation() {
config.Transport = "chan"
}
// MakeDefaultConfig returns Config object with few default values
// only used by init() and master
func MakeDefaultConfig() Config {
return Config{
Transport: "tcp",
ReplyWhenCommit: false,
Adaptive: true,
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
}
func (c Config) N() int {
return c.n
}
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)
}