~ubuntu-branches/ubuntu/utopic/hockeypuck/utopic-proposed

« back to all changes in this revision

Viewing changes to build/src/github.com/cmars/conflux/recon/settings.go

  • Committer: Package Import Robot
  • Author(s): Casey Marshall
  • Date: 2014-04-13 20:06:01 UTC
  • Revision ID: package-import@ubuntu.com-20140413200601-oxdlqn1gy0x8m55u
Tags: 1.0~rel20140413+7a1892a~trusty
Hockeypuck 1.0 release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   conflux - Distributed database synchronization library
 
3
        Based on the algorithm described in
 
4
                "Set Reconciliation with Nearly Optimal Communication Complexity",
 
5
                        Yaron Minsky, Ari Trachtenberg, and Richard Zippel, 2004.
 
6
 
 
7
   Copyright (C) 2012  Casey Marshall <casey.marshall@gmail.com>
 
8
 
 
9
   This program is free software: you can redistribute it and/or modify
 
10
   it under the terms of the GNU Affero General Public License as published by
 
11
   the Free Software Foundation, version 3.
 
12
 
 
13
   This program is distributed in the hope that it will be useful,
 
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
   GNU Affero General Public License for more details.
 
17
 
 
18
   You should have received a copy of the GNU Affero General Public License
 
19
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
*/
 
21
 
 
22
// Package recon provides the SKS reconciliation protocol, prefix tree interface
 
23
// and an in-memory prefix-tree implementation.
 
24
package recon
 
25
 
 
26
import (
 
27
        "bytes"
 
28
        "fmt"
 
29
        "github.com/pelletier/go-toml"
 
30
        "net"
 
31
        "strconv"
 
32
        "strings"
 
33
)
 
34
 
 
35
type Settings struct {
 
36
        *toml.TomlTree
 
37
        splitThreshold int
 
38
        joinThreshold  int
 
39
        numSamples     int
 
40
}
 
41
 
 
42
func (s *Settings) GetString(key string, defaultValue string) string {
 
43
        if s, is := s.GetDefault(key, defaultValue).(string); is {
 
44
                return s
 
45
        }
 
46
        return defaultValue
 
47
}
 
48
 
 
49
func (s *Settings) GetStrings(key string) (value []string) {
 
50
        if strs, is := s.Get(key).([]interface{}); is {
 
51
                for _, v := range strs {
 
52
                        if str, is := v.(string); is {
 
53
                                value = append(value, str)
 
54
                        }
 
55
                }
 
56
        }
 
57
        return
 
58
}
 
59
 
 
60
func (s *Settings) GetInt(key string, defaultValue int) int {
 
61
        switch v := s.GetDefault(key, defaultValue).(type) {
 
62
        case int:
 
63
                return v
 
64
        case int64:
 
65
                return int(v)
 
66
        default:
 
67
                i, err := strconv.Atoi(fmt.Sprintf("%v", v))
 
68
                if err != nil {
 
69
                        panic(err)
 
70
                }
 
71
                s.Set(key, i)
 
72
                return i
 
73
        }
 
74
        return defaultValue
 
75
}
 
76
 
 
77
func (s *Settings) Version() string {
 
78
        return s.GetString("conflux.recon.version", "1.1.3")
 
79
}
 
80
 
 
81
func (s *Settings) LogName() string {
 
82
        return s.GetString("conflux.recon.logname", "conflux.recon")
 
83
}
 
84
 
 
85
func (s *Settings) HttpPort() int {
 
86
        return s.GetInt("conflux.recon.httpPort", 11371)
 
87
}
 
88
 
 
89
func (s *Settings) ReconPort() int {
 
90
        return s.GetInt("conflux.recon.reconPort", 11370)
 
91
}
 
92
 
 
93
func (s *Settings) Partners() []string {
 
94
        return s.GetStrings("conflux.recon.partners")
 
95
}
 
96
 
 
97
func (s *Settings) Filters() []string {
 
98
        return s.GetStrings("conflux.recon.filters")
 
99
}
 
100
 
 
101
func (s *Settings) ThreshMult() int {
 
102
        return s.GetInt("conflux.recon.threshMult", DefaultThreshMult)
 
103
}
 
104
 
 
105
func (s *Settings) BitQuantum() int {
 
106
        return s.GetInt("conflux.recon.bitQuantum", DefaultBitQuantum)
 
107
}
 
108
 
 
109
func (s *Settings) MBar() int {
 
110
        return s.GetInt("conflux.recon.mBar", DefaultMBar)
 
111
}
 
112
 
 
113
func (s *Settings) SplitThreshold() int {
 
114
        return s.splitThreshold
 
115
}
 
116
 
 
117
func (s *Settings) JoinThreshold() int {
 
118
        return s.joinThreshold
 
119
}
 
120
 
 
121
func (s *Settings) NumSamples() int {
 
122
        return s.numSamples
 
123
}
 
124
 
 
125
func (s *Settings) GossipIntervalSecs() int {
 
126
        return s.GetInt("conflux.recon.gossipIntervalSecs", 60)
 
127
}
 
128
 
 
129
func (s *Settings) MaxOutstandingReconRequests() int {
 
130
        return s.GetInt("conflux.recon.maxOutstandingReconRequests", 100)
 
131
}
 
132
 
 
133
func (s *Settings) ConnTimeout() int {
 
134
        return s.GetInt("conflux.recon.connTimeout", 0)
 
135
}
 
136
 
 
137
func (s *Settings) ReadTimeout() int {
 
138
        return s.GetInt("conflux.recon.readTimeout", 0)
 
139
}
 
140
 
 
141
func DefaultSettings() (settings *Settings) {
 
142
        buf := bytes.NewBuffer(nil)
 
143
        var tree *toml.TomlTree
 
144
        var err error
 
145
        if tree, err = toml.Load(buf.String()); err != nil {
 
146
                panic(err) // unlikely
 
147
        }
 
148
        return NewSettings(tree)
 
149
}
 
150
 
 
151
func NewSettings(tree *toml.TomlTree) (settings *Settings) {
 
152
        settings = &Settings{tree, DefaultSplitThreshold, DefaultJoinThreshold, DefaultNumSamples}
 
153
        settings.UpdateDerived()
 
154
        return
 
155
}
 
156
 
 
157
func (s *Settings) Config() *Config {
 
158
        return &Config{
 
159
                Version:    s.Version(),
 
160
                HttpPort:   s.HttpPort(),
 
161
                BitQuantum: s.BitQuantum(),
 
162
                MBar:       s.MBar(),
 
163
                Filters:    strings.Join(s.Filters(), ",")}
 
164
}
 
165
 
 
166
func (s *Settings) UpdateDerived() {
 
167
        s.splitThreshold = s.ThreshMult() * s.MBar()
 
168
        s.joinThreshold = s.splitThreshold / 2
 
169
        s.numSamples = s.MBar() + 1
 
170
}
 
171
 
 
172
func LoadSettings(path string) (*Settings, error) {
 
173
        var tree *toml.TomlTree
 
174
        var err error
 
175
        if tree, err = toml.LoadFile(path); err != nil {
 
176
                return nil, err
 
177
        }
 
178
        return NewSettings(tree), nil
 
179
}
 
180
 
 
181
func (s *Settings) PartnerAddrs() (addrs []net.Addr, err error) {
 
182
        for _, partner := range s.Partners() {
 
183
                if partner == "" {
 
184
                        continue
 
185
                }
 
186
                addr, err := net.ResolveTCPAddr("tcp", partner)
 
187
                if err != nil {
 
188
                        return nil, err
 
189
                }
 
190
                addrs = append(addrs, addr)
 
191
        }
 
192
        return
 
193
}