-
Notifications
You must be signed in to change notification settings - Fork 457
/
Copy pathconfig.go
247 lines (210 loc) · 8.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// 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 environment
import (
"errors"
"fmt"
clusterclient "github.com/m3db/m3/src/cluster/client"
etcdclient "github.com/m3db/m3/src/cluster/client/etcd"
"github.com/m3db/m3/src/cluster/kv"
m3clusterkvmem "github.com/m3db/m3/src/cluster/kv/mem"
"github.com/m3db/m3/src/cluster/services"
"github.com/m3db/m3/src/cluster/shard"
"github.com/m3db/m3/src/dbnode/kvconfig"
"github.com/m3db/m3/src/dbnode/sharding"
"github.com/m3db/m3/src/dbnode/storage/namespace"
"github.com/m3db/m3/src/dbnode/topology"
"github.com/m3db/m3x/instrument"
)
var (
errInvalidConfig = errors.New("must supply either service or static config")
)
// Configuration is a configuration that can be used to create namespaces, a topology, and kv store
type Configuration struct {
// Service is used when a topology initializer is not supplied.
Service *etcdclient.Configuration `yaml:"service"`
// StaticConfiguration is used for running M3DB with a static config
Static *StaticConfiguration `yaml:"static"`
// Presence of a (etcd) server in this config denotes an embedded cluster
SeedNodes *SeedNodesConfig `yaml:"seedNodes"`
}
// SeedNodesConfig defines fields for seed node
type SeedNodesConfig struct {
RootDir string `yaml:"rootDir"`
InitialAdvertisePeerUrls []string `yaml:"initialAdvertisePeerUrls"`
AdvertiseClientUrls []string `yaml:"advertiseClientUrls"`
ListenPeerUrls []string `yaml:"listenPeerUrls"`
ListenClientUrls []string `yaml:"listenClientUrls"`
InitialCluster []SeedNode `yaml:"initialCluster"`
ClientTransportSecurity SeedNodeSecurityConfig `yaml:"clientTransportSecurity"`
PeerTransportSecurity SeedNodeSecurityConfig `yaml:"peerTransportSecurity"`
}
// SeedNode represents a seed node for the cluster
type SeedNode struct {
HostID string `yaml:"hostID"`
Endpoint string `yaml:"endpoint"`
ClusterState string `yaml:"clusterState"`
}
// SeedNodeSecurityConfig contains the data used for security in seed nodes
type SeedNodeSecurityConfig struct {
CAFile string `yaml:"caFile"`
CertFile string `yaml:"certFile"`
KeyFile string `yaml:"keyFile"`
TrustedCAFile string `yaml:"trustedCaFile"`
CertAuth bool `yaml:"clientCertAuth"`
AutoTLS bool `yaml:"autoTls"`
}
// StaticConfiguration is used for running M3DB with a static config
type StaticConfiguration struct {
Namespaces []namespace.MetadataConfiguration `yaml:"namespaces"`
TopologyConfig *topology.StaticConfiguration `yaml:"topology"`
ListenAddress string `yaml:"listenAddress"`
}
// ConfigureResults stores initializers and kv store for dynamic and static configs
type ConfigureResults struct {
NamespaceInitializer namespace.Initializer
TopologyInitializer topology.Initializer
ClusterClient clusterclient.Client
KVStore kv.Store
}
// ConfigurationParameters are options used to create new ConfigureResults
type ConfigurationParameters struct {
InstrumentOpts instrument.Options
HashingSeed uint32
HostID string
}
// Configure creates a new ConfigureResults
func (c Configuration) Configure(cfgParams ConfigurationParameters) (ConfigureResults, error) {
var emptyConfig ConfigureResults
if c.Service != nil && c.Static != nil {
return emptyConfig, errInvalidConfig
}
if c.Service != nil {
return c.configureDynamic(cfgParams)
}
if c.Static != nil {
return c.configureStatic(cfgParams)
}
return emptyConfig, errInvalidConfig
}
func (c Configuration) configureDynamic(cfgParams ConfigurationParameters) (ConfigureResults, error) {
configSvcClientOpts := c.Service.NewOptions().
SetInstrumentOptions(cfgParams.InstrumentOpts).
// Set timeout to zero so it will wait indefinitely for the
// initial value.
SetServicesOptions(services.NewOptions().SetInitTimeout(0))
configSvcClient, err := etcdclient.NewConfigServiceClient(configSvcClientOpts)
if err != nil {
err = fmt.Errorf("could not create m3cluster client: %v", err)
return ConfigureResults{}, err
}
dynamicOpts := namespace.NewDynamicOptions().
SetInstrumentOptions(cfgParams.InstrumentOpts).
SetConfigServiceClient(configSvcClient).
SetNamespaceRegistryKey(kvconfig.NamespacesKey)
nsInit := namespace.NewDynamicInitializer(dynamicOpts)
serviceID := services.NewServiceID().
SetName(c.Service.Service).
SetEnvironment(c.Service.Env).
SetZone(c.Service.Zone)
topoOpts := topology.NewDynamicOptions().
SetConfigServiceClient(configSvcClient).
SetServiceID(serviceID).
SetQueryOptions(services.NewQueryOptions().SetIncludeUnhealthy(true)).
SetInstrumentOptions(cfgParams.InstrumentOpts).
SetHashGen(sharding.NewHashGenWithSeed(cfgParams.HashingSeed))
topoInit := topology.NewDynamicInitializer(topoOpts)
kv, err := configSvcClient.KV()
if err != nil {
err = fmt.Errorf("could not create KV client, %v", err)
return ConfigureResults{}, err
}
return ConfigureResults{
NamespaceInitializer: nsInit,
TopologyInitializer: topoInit,
ClusterClient: configSvcClient,
KVStore: kv,
}, nil
}
func (c Configuration) configureStatic(cfgParams ConfigurationParameters) (ConfigureResults, error) {
var emptyConfig ConfigureResults
nsList := []namespace.Metadata{}
for _, ns := range c.Static.Namespaces {
md, err := ns.Metadata()
if err != nil {
err = fmt.Errorf("unable to create metadata for static config: %v", err)
return emptyConfig, err
}
nsList = append(nsList, md)
}
nsInitStatic := namespace.NewStaticInitializer(nsList)
shardSet, hostShardSets, err := newStaticShardSet(c.Static.TopologyConfig.Shards, c.Static.TopologyConfig.Hosts)
if err != nil {
err = fmt.Errorf("unable to create shard set for static config: %v", err)
return emptyConfig, err
}
staticOptions := topology.NewStaticOptions().
SetHostShardSets(hostShardSets).
SetShardSet(shardSet)
numHosts := len(c.Static.TopologyConfig.Hosts)
numReplicas := c.Static.TopologyConfig.Replicas
switch numReplicas {
case 0:
if numHosts != 1 {
err := fmt.Errorf("number of hosts (%d) must be 1 if replicas is not set", numHosts)
return emptyConfig, err
}
staticOptions = staticOptions.SetReplicas(1)
default:
if numHosts != numReplicas {
err := fmt.Errorf("number of hosts (%d) not equal to number of replicas (%d)", numHosts, numReplicas)
return emptyConfig, err
}
staticOptions = staticOptions.SetReplicas(c.Static.TopologyConfig.Replicas)
}
topoInit := topology.NewStaticInitializer(staticOptions)
return ConfigureResults{
NamespaceInitializer: nsInitStatic,
TopologyInitializer: topoInit,
KVStore: m3clusterkvmem.NewStore(),
}, nil
}
func newStaticShardSet(numShards int, hosts []topology.HostShardConfig) (sharding.ShardSet, []topology.HostShardSet, error) {
var (
shardSet sharding.ShardSet
hostShardSets []topology.HostShardSet
shardIDs []uint32
err error
)
for i := uint32(0); i < uint32(numShards); i++ {
shardIDs = append(shardIDs, i)
}
shards := sharding.NewShards(shardIDs, shard.Available)
shardSet, err = sharding.NewShardSet(shards, sharding.DefaultHashFn(len(shards)))
if err != nil {
return nil, nil, err
}
for _, i := range hosts {
host := topology.NewHost(i.HostID, i.ListenAddress)
hostShardSet := topology.NewHostShardSet(host, shardSet)
hostShardSets = append(hostShardSets, hostShardSet)
}
return shardSet, hostShardSets, nil
}