forked from jaegertracing/jaeger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
168 lines (152 loc) · 5.41 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
// Copyright (c) 2019 The Jaeger Authors.
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"crypto/tls"
"fmt"
"time"
"github.com/gocql/gocql"
"github.com/jaegertracing/jaeger/pkg/cassandra"
gocqlw "github.com/jaegertracing/jaeger/pkg/cassandra/gocql"
"github.com/jaegertracing/jaeger/pkg/config/tlscfg"
)
// Configuration describes the configuration properties needed to connect to a Cassandra cluster
type Configuration struct {
Servers []string `validate:"nonzero"`
Keyspace string `validate:"nonzero"`
LocalDC string `yaml:"local_dc"`
ConnectionsPerHost int `validate:"min=1" yaml:"connections_per_host"`
Timeout time.Duration `validate:"min=500"`
ConnectTimeout time.Duration `yaml:"connect_timeout"`
ReconnectInterval time.Duration `validate:"min=500" yaml:"reconnect_interval"`
SocketKeepAlive time.Duration `validate:"min=0" yaml:"socket_keep_alive"`
MaxRetryAttempts int `validate:"min=0" yaml:"max_retry_attempt"`
ProtoVersion int `yaml:"proto_version"`
Consistency string `yaml:"consistency"`
DisableCompression bool `yaml:"disable-compression"`
Port int `yaml:"port"`
Authenticator Authenticator `yaml:"authenticator"`
DisableAutoDiscovery bool `yaml:"disable_auto_discovery"`
EnableDependenciesV2 bool `yaml:"enable_dependencies_v2"`
TLS tlscfg.Options
}
// Authenticator holds the authentication properties needed to connect to a Cassandra cluster
type Authenticator struct {
Basic BasicAuthenticator `yaml:"basic"`
// TODO: add more auth types
}
// BasicAuthenticator holds the username and password for a password authenticator for a Cassandra cluster
type BasicAuthenticator struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
}
// ApplyDefaults copies settings from source unless its own value is non-zero.
func (c *Configuration) ApplyDefaults(source *Configuration) {
if c.ConnectionsPerHost == 0 {
c.ConnectionsPerHost = source.ConnectionsPerHost
}
if c.MaxRetryAttempts == 0 {
c.MaxRetryAttempts = source.MaxRetryAttempts
}
if c.Timeout == 0 {
c.Timeout = source.Timeout
}
if c.ReconnectInterval == 0 {
c.ReconnectInterval = source.ReconnectInterval
}
if c.Port == 0 {
c.Port = source.Port
}
if c.Keyspace == "" {
c.Keyspace = source.Keyspace
}
if c.ProtoVersion == 0 {
c.ProtoVersion = source.ProtoVersion
}
if c.SocketKeepAlive == 0 {
c.SocketKeepAlive = source.SocketKeepAlive
}
}
// SessionBuilder creates new cassandra.Session
type SessionBuilder interface {
NewSession() (cassandra.Session, error)
}
// NewSession creates a new Cassandra session
func (c *Configuration) NewSession() (cassandra.Session, error) {
cluster := c.NewCluster()
session, err := cluster.CreateSession()
if err != nil {
return nil, err
}
return gocqlw.WrapCQLSession(session), nil
}
// NewCluster creates a new gocql cluster from the configuration
func (c *Configuration) NewCluster() *gocql.ClusterConfig {
cluster := gocql.NewCluster(c.Servers...)
cluster.Keyspace = c.Keyspace
cluster.NumConns = c.ConnectionsPerHost
cluster.Timeout = c.Timeout
cluster.ConnectTimeout = c.ConnectTimeout
cluster.ReconnectInterval = c.ReconnectInterval
cluster.SocketKeepalive = c.SocketKeepAlive
if c.ProtoVersion > 0 {
cluster.ProtoVersion = c.ProtoVersion
}
if c.MaxRetryAttempts > 1 {
cluster.RetryPolicy = &gocql.SimpleRetryPolicy{NumRetries: c.MaxRetryAttempts - 1}
}
if c.Port != 0 {
cluster.Port = c.Port
}
if !c.DisableCompression {
cluster.Compressor = gocql.SnappyCompressor{}
}
if c.Consistency == "" {
cluster.Consistency = gocql.LocalOne
} else {
cluster.Consistency = gocql.ParseConsistency(c.Consistency)
}
fallbackHostSelectionPolicy := gocql.RoundRobinHostPolicy()
if c.LocalDC != "" {
fallbackHostSelectionPolicy = gocql.DCAwareRoundRobinPolicy(c.LocalDC)
}
cluster.PoolConfig.HostSelectionPolicy = gocql.TokenAwareHostPolicy(fallbackHostSelectionPolicy, gocql.ShuffleReplicas())
if c.Authenticator.Basic.Username != "" && c.Authenticator.Basic.Password != "" {
cluster.Authenticator = gocql.PasswordAuthenticator{
Username: c.Authenticator.Basic.Username,
Password: c.Authenticator.Basic.Password,
}
}
if c.TLS.Enabled {
cluster.SslOpts = &gocql.SslOptions{
Config: &tls.Config{
ServerName: c.TLS.ServerName,
},
CertPath: c.TLS.CertPath,
KeyPath: c.TLS.KeyPath,
CaPath: c.TLS.CAPath,
EnableHostVerification: !c.TLS.SkipHostVerify,
}
}
// If tunneling connection to C*, disable cluster autodiscovery features.
if c.DisableAutoDiscovery {
cluster.DisableInitialHostLookup = true
cluster.IgnorePeerAddr = true
}
return cluster
}
func (c *Configuration) String() string {
return fmt.Sprintf("%+v", *c)
}