/
config.go
70 lines (54 loc) · 2.47 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
// Package config exposes some static variables loaded through a .yaml file used throughout the Paxos algorithm.
package config
import (
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"math/rand"
"time"
)
// CONF is the Conf object which holds all the variables
var CONF Conf
// Conf is a type describing some of the meta variables used by different parts of the algorithm.
type Conf struct {
DB_PATH string `yaml:"db_path"` // DB_PATH locates the database file.
PID int `yaml:"pid"` // PID is the identifier of the node, PID is supposed to unique.
V_DEFAULT string `yaml:"v_default"` // V_DEFAULT defines what is the default value proposed by the node for the accept request when no prior value has been proposed.
PORT int `yaml:"port"` // PORT defines the TCP port to which the web server will be listening.
MANUAL_MODE bool `yaml:"manual_mode"` // MANUAL_MODE defines whether the nodes proceed automatically through the phases or wait for user interaction.
TIMEOUT time.Duration `yaml:"timeout"` // TIMEOUT defines the time duration (in seconds) waited by the client before assuming a node is not reachable.
SEEK_TIMEOUT time.Duration `yaml:"seek_timeout"` // SEEK_TIMEOUT defines the time duration (in seconds) needed before performing new a seek request (used only when MANUAL_MODE = false).
NODES []string `yaml:"nodes"` // NODES defines the list of the paxos nodes of the system.
QUORUM int `yaml:"quorum"` // QUORUM defines the number of positive responses needed for the algorithm to proceed. It's computed at execution time, but can be provided explicitly.
}
// LoadConfigFile loads the config '.yaml' file onto the callee Conf object.
func (c *Conf) LoadConfigFile(fn string) {
yamlFile, err := ioutil.ReadFile(fn)
if err != nil {
log.Fatalf("yamlFile.Get err %v ", err)
}
err = yaml.Unmarshal(yamlFile, c)
if err != nil {
log.Fatalf("Unmarshal: %v", err)
}
}
// FillEmptyFields fills in those fields that were left empty in the .yaml file or those which need a run-time computation.
// These are the only fields which can be left blank, each field that is not initialized by this function has to be filled by the user in the .yaml file.
func (c *Conf) FillEmptyFields() {
if c.PID == 0 {
c.PID = rand.Intn(10000)
}
if c.V_DEFAULT == "" {
c.V_DEFAULT = fmt.Sprintf("paxos@%d", c.PID)
}
if c.TIMEOUT == 0 {
c.TIMEOUT = 2
}
if c.SEEK_TIMEOUT == 0 {
c.SEEK_TIMEOUT = 5
}
if c.QUORUM == 0 {
c.QUORUM = len(c.NODES)/2 + 1
}
}