Skip to content

Commit

Permalink
yaml: DBConfigs WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Sugu Sougoumarane <[email protected]>
  • Loading branch information
sougou committed Apr 21, 2020
1 parent 71521fd commit b57fe7b
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 19 deletions.
12 changes: 6 additions & 6 deletions config/tablet/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,32 @@ db:
user: vt_app # db_app_user
password: # db_app_password
useSsl: true # db_app_use_ssl
preferSocket: true
preferTcp: false
dba:
user: vt_dba # db_dba_user
password: # db_dba_password
useSsl: true # db_dba_use_ssl
preferSocket: true
preferTcp: false
filtered:
user: vt_filtered # db_filtered_user
password: # db_filtered_password
useSsl: true # db_filtered_use_ssl
preferSocket: true
preferTcp: false
repl:
user: vt_repl # db_repl_user
password: # db_repl_password
useSsl: true # db_repl_use_ssl
preferSocket: true
preferTcp: false
appdebug:
user: vt_appdebug # db_appdebug_user
password: # db_appdebug_password
useSsl: true # db_appdebug_use_ssl
preferSocket: true
preferTcp: false
allprivs:
user: vt_allprivs # db_allprivs_user
password: # db_allprivs_password
useSsl: true # db_allprivs_use_ssl
preferSocket: true
preferTcp: false

oltpReadPool:
size: 16 # queryserver-config-pool-size
Expand Down
6 changes: 3 additions & 3 deletions go/cmd/vttablet/vttablet.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"io/ioutil"

"golang.org/x/net/context"
"sigs.k8s.io/yaml"
"vitess.io/vitess/go/vt/dbconfigs"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/mysqlctl"
Expand All @@ -34,6 +33,7 @@ import (
"vitess.io/vitess/go/vt/vttablet/tabletmanager"
"vitess.io/vitess/go/vt/vttablet/tabletserver"
"vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv"
"vitess.io/vitess/go/yaml2"
)

var (
Expand Down Expand Up @@ -67,10 +67,10 @@ func main() {
if err != nil {
log.Exitf("error reading config file %s: %v", *tabletConfig, err)
}
if err := yaml.Unmarshal(bytes, config); err != nil {
if err := yaml2.Unmarshal(bytes, config); err != nil {
log.Exitf("error parsing config file %s: %v", bytes, err)
}
gotBytes, _ := yaml.Marshal(config)
gotBytes, _ := yaml2.Marshal(config)
log.Infof("Loaded config file %s successfully:\n%s", *tabletConfig, gotBytes)
}

Expand Down
44 changes: 38 additions & 6 deletions go/vt/dbconfigs/dbconfigs.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,51 @@ var (
baseConfig = mysql.ConnParams{}
)

// DB contains the connection parameters and user credentials
// that can be initialized through a yaml file.
type DB struct {
Socket string `json:"socket,omitempty"`
Host string `json:"host,omitempty"`
Port int `json:"port,omitempty"`
CharSet string `json:"charSet,omitempty"`
Flags uint64 `json:"flags,omitempty"`
Flavor string `json:"flavor,omitempty"`
SslCa string `json:"sslCa,omitempty"`
SslCaPath string `json:"sslCaPath,omitempty"`
SslCert string `json:"sslCert,omitempty"`
SslKey string `json:"sslKey,omitempty"`
ServerName string `json:"serverName,omitempty"`
ConnectTimeoutMilliseconds int `json:"connectTimeoutMilliseconds,omitempty"`
App UserConfig `json:"app,omitempty"`
Dba UserConfig `json:"dba,omitempty"`
Filtered UserConfig `json:"filtered,omitempty"`
Repl UserConfig `json:"repl,omitempty"`
Appdebug UserConfig `json:"appdebug,omitempty"`
Allprivs UserConfig `json:"allprivs,omitempty"`

DBName sync2.AtomicString `json:"-"`
SidecarDBName sync2.AtomicString `json:"-"`
}

// UserConfig contains user-specific configs.
type UserConfig struct {
User string `json:"user,omitempty"`
Password string `json:"password,omitempty"`
UseSSL bool `json:"useSsl,omitempty"`
PreferTCP bool `json:"preferTcp,omitempty"`
}

// DBConfigs stores all the data needed to build various connection
// parameters for the db. It stores credentials for app, appdebug,
// allprivs, dba, filtered and repl users.
// It contains other connection parameters like socket, charset, etc.
// It also stores the default db name, which it can combine with the
// rest of the data to build db-sepcific connection parameters.
// It also supplies the SidecarDBName. This is currently hardcoded
// to "_vt", but will soon become customizable.
// The life-cycle of this package is as follows:
// It also supplies the SidecarDBName. This is hardcoded to "_vt".
//
// The legacy way of initializing is as follows:
// App must call RegisterFlags to request the types of connections
// it wants support for. This must be done before involing flags.Parse.
// it wants support for. This must be done before invoking flags.Parse.
// After flag parsing, app invokes the Init function, which will return
// a DBConfigs object.
// The app must store the DBConfigs object internally, and use it to
Expand Down Expand Up @@ -139,8 +173,6 @@ func registerPerUserFlags(dbc *userConfig, userKey string) {
// Connector contains Connection Parameters for mysql connection
type Connector struct {
connParams *mysql.ConnParams
dbName string
host string
}

// New initializes a ConnParams from mysql connection parameters
Expand Down
58 changes: 58 additions & 0 deletions go/vt/dbconfigs/dbconfigs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/yaml2"
)

func TestRegisterFlagsWithSomeFlags(t *testing.T) {
Expand Down Expand Up @@ -427,3 +430,58 @@ func saveDBConfigs() (restore func()) {
baseConfig = savedBaseConfig
}
}

func TestYaml(t *testing.T) {
db := DB{
Socket: "a",
Port: 1,
Flags: 20,
App: UserConfig{
User: "vt_app",
UseSSL: true,
},
Dba: UserConfig{
User: "vt_dba",
},
}
gotBytes, err := yaml2.Marshal(&db)
require.NoError(t, err)
wantBytes := `allprivs: {}
app:
useSsl: true
user: vt_app
appdebug: {}
dba:
user: vt_dba
filtered: {}
flags: 20
port: 1
repl: {}
socket: a
`
assert.Equal(t, wantBytes, string(gotBytes))

inBytes := []byte(`socket: a
port: 1
flags: 20
app:
user: vt_app
useSsl: true
preferTCP: false
dba:
user: vt_dba
`)
gotdb := DB{
Port: 1,
Flags: 20,
App: UserConfig{
PreferTCP: true,
},
Dba: UserConfig{
User: "aaa",
},
}
err = yaml2.Unmarshal(inBytes, &gotdb)
require.NoError(t, err)
assert.Equal(t, &db, &gotdb)
}
8 changes: 4 additions & 4 deletions go/vt/vttablet/tabletserver/tabletenv/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"sigs.k8s.io/yaml"
"vitess.io/vitess/go/yaml2"
)

func TestConfigParse(t *testing.T) {
Expand All @@ -34,7 +34,7 @@ func TestConfigParse(t *testing.T) {
MaxWaiters: 40,
},
}
gotBytes, err := yaml.Marshal(&cfg)
gotBytes, err := yaml2.Marshal(&cfg)
require.NoError(t, err)
wantBytes := `hotRowProtection: {}
olapReadPool: {}
Expand All @@ -57,13 +57,13 @@ txPool: {}
maxWaiters: 40
`)
gotCfg := cfg
err = yaml.Unmarshal(inBytes, &gotCfg)
err = yaml2.Unmarshal(inBytes, &gotCfg)
require.NoError(t, err)
assert.Equal(t, cfg, gotCfg)
}

func TestDefaultConfig(t *testing.T) {
gotBytes, err := yaml.Marshal(NewDefaultConfig())
gotBytes, err := yaml2.Marshal(NewDefaultConfig())
require.NoError(t, err)
want := `cacheResultFields: true
consolidator: enable
Expand Down
28 changes: 28 additions & 0 deletions go/yaml2/yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright 2020 The Vitess Authors.
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 yaml2 ensures that the right yaml package gets imported.
// The default package that goimports adds is not the one we want to use.
package yaml2

import "sigs.k8s.io/yaml"

var (
// Marshal marshals to YAML.
Marshal = yaml.Marshal
// Unmarshal unmarshals from YAML.
Unmarshal = yaml.Unmarshal
)

0 comments on commit b57fe7b

Please sign in to comment.