forked from uber/cadence
/
ringpop.go
183 lines (162 loc) · 5.44 KB
/
ringpop.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package config
import (
"errors"
"fmt"
"reflect"
"strings"
"time"
"github.com/uber/ringpop-go"
"github.com/uber/ringpop-go/discovery"
"github.com/uber/ringpop-go/discovery/jsonfile"
"github.com/uber/ringpop-go/discovery/statichosts"
"github.com/uber/ringpop-go/swim"
tcg "github.com/uber/tchannel-go"
"go.uber.org/yarpc"
"go.uber.org/yarpc/transport/tchannel"
)
const (
// BootstrapModeNone represents a bootstrap mode set to nothing or invalid
BootstrapModeNone BootstrapMode = iota
// BootstrapModeFile represents a file-based bootstrap mode
BootstrapModeFile
// BootstrapModeHosts represents a list of hosts passed in the configuration
BootstrapModeHosts
// BootstrapModeCustom represents a custom bootstrap mode
BootstrapModeCustom
)
const (
defaultMaxJoinDuration = 10 * time.Second
)
// RingpopFactory implements the RingpopFactory interface
type RingpopFactory struct {
config *Ringpop
}
// NewFactory builds a ringpop factory conforming
// to the underlying configuration
func (rpConfig *Ringpop) NewFactory() (*RingpopFactory, error) {
return newRingpopFactory(rpConfig)
}
func (rpConfig *Ringpop) validate() error {
if len(rpConfig.Name) == 0 {
return fmt.Errorf("ringpop config missing `name` param")
}
return validateBootstrapMode(rpConfig)
}
// UnmarshalYAML is called by the yaml package to convert
// the config YAML into a BootstrapMode.
func (m *BootstrapMode) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string
if err := unmarshal(&s); err != nil {
return err
}
var err error
*m, err = parseBootstrapMode(s)
return err
}
// parseBootstrapMode reads a string value and returns a bootstrap mode.
func parseBootstrapMode(s string) (BootstrapMode, error) {
switch strings.ToLower(s) {
case "hosts":
return BootstrapModeHosts, nil
case "file":
return BootstrapModeFile, nil
case "custom":
return BootstrapModeCustom, nil
}
return BootstrapModeNone, errors.New("invalid or no ringpop bootstrap mode")
}
func validateBootstrapMode(rpConfig *Ringpop) error {
switch rpConfig.BootstrapMode {
case BootstrapModeFile:
if len(rpConfig.BootstrapFile) == 0 {
return fmt.Errorf("ringpop config missing bootstrap file param")
}
case BootstrapModeHosts:
if len(rpConfig.BootstrapHosts) == 0 {
return fmt.Errorf("ringpop config missing boostrap hosts param")
}
case BootstrapModeCustom:
if rpConfig.DiscoveryProvider == nil {
return fmt.Errorf("ringpop bootstrapMode is set to custom but discoveryProvider is nil")
}
default:
return fmt.Errorf("ringpop config with unknown boostrap mode")
}
return nil
}
func newRingpopFactory(rpConfig *Ringpop) (*RingpopFactory, error) {
if err := rpConfig.validate(); err != nil {
return nil, err
}
if rpConfig.MaxJoinDuration == 0 {
rpConfig.MaxJoinDuration = defaultMaxJoinDuration
}
return &RingpopFactory{config: rpConfig}, nil
}
// CreateRingpop is the implementation for RingpopFactory.CreateRingpop
func (factory *RingpopFactory) CreateRingpop(dispatcher *yarpc.Dispatcher) (*ringpop.Ringpop, error) {
var ch *tcg.Channel
var err error
if ch, err = factory.getChannel(dispatcher); err != nil {
return nil, err
}
rp, err := ringpop.New(factory.config.Name, ringpop.Channel(ch))
if err != nil {
return nil, err
}
discoveryProvider, err := newDiscoveryProvider(factory.config)
if err != nil {
return nil, err
}
bootstrapOpts := &swim.BootstrapOptions{
MaxJoinDuration: factory.config.MaxJoinDuration,
DiscoverProvider: discoveryProvider,
}
_, err = rp.Bootstrap(bootstrapOpts)
if err != nil {
return nil, err
}
return rp, nil
}
func (factory *RingpopFactory) getChannel(dispatcher *yarpc.Dispatcher) (*tcg.Channel, error) {
t := dispatcher.Inbounds()[0].Transports()[0].(*tchannel.ChannelTransport)
ty := reflect.ValueOf(t.Channel())
var ch *tcg.Channel
var ok bool
if ch, ok = ty.Interface().(*tcg.Channel); !ok {
return nil, errors.New("Unable to get tchannel out of the dispatcher")
}
return ch, nil
}
func newDiscoveryProvider(cfg *Ringpop) (discovery.DiscoverProvider, error) {
if cfg.DiscoveryProvider != nil {
// custom discovery provider takes first precedence
return cfg.DiscoveryProvider, nil
}
switch cfg.BootstrapMode {
case BootstrapModeHosts:
return statichosts.New(cfg.BootstrapHosts...), nil
case BootstrapModeFile:
return jsonfile.New(cfg.BootstrapFile), nil
}
return nil, fmt.Errorf("unknown bootstrap mode")
}