Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add connection string option to route RW/PDML RPC to leader #146

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
"google.golang.org/grpc/status"
)

const userAgent = "go-sql-spanner/1.0.1"
const userAgent = "go-sql-spanner/1.0.2"

// dsnRegExpString describes the valid values for a dsn (connection name) for
// Google Cloud Spanner. The string consists of the following parts:
Expand All @@ -51,8 +51,11 @@ const userAgent = "go-sql-spanner/1.0.1"
// to true to connect to local mock servers that do not use SSL.
// - retryAbortsInternally: Boolean that indicates whether the connection should automatically retry aborted errors.
// The default is true.
// - disableRouteToLeader: Boolean that indicates if all the requests of type read-write and PDML
// need to be routed to the leader region.
// The default is true.
//
// Example: `localhost:9010/projects/test-project/instances/test-instance/databases/test-database;usePlainText=true`
// Example: `localhost:9010/projects/test-project/instances/test-instance/databases/test-database;usePlainText=true;disableRouteToLeader=false`
var dsnRegExp = regexp.MustCompile("((?P<HOSTGROUP>[\\w.-]+(?:\\.[\\w\\.-]+)*[\\w\\-\\._~:/?#\\[\\]@!\\$&'\\(\\)\\*\\+,;=.]+)/)?projects/(?P<PROJECTGROUP>(([a-z]|[-.:]|[0-9])+|(DEFAULT_PROJECT_ID)))(/instances/(?P<INSTANCEGROUP>([a-z]|[-]|[0-9])+)(/databases/(?P<DATABASEGROUP>([a-z]|[-]|[_]|[0-9])+))?)?(([\\?|;])(?P<PARAMSGROUP>.*))?")

var _ driver.DriverContext = &Driver{}
Expand Down Expand Up @@ -211,6 +214,12 @@ func newConnector(d *Driver, dsn string) (*connector, error) {
config.WriteSessions = val
}
}
config.DisableRouteToLeader = true
if strval, ok := connectorConfig.params["disableroutetoleader"]; ok {
if val, err := strconv.ParseBool(strval); err == nil {
config.DisableRouteToLeader = val
}
}
config.UserAgent = userAgent
c := &connector{
driver: d,
Expand Down
72 changes: 55 additions & 17 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ func TestExtractDnsParts(t *testing.T) {
params: map[string]string{},
},
wantSpannerConfig: spanner.ClientConfig{
SessionPoolConfig: spanner.DefaultSessionPoolConfig,
UserAgent: userAgent,
SessionPoolConfig: spanner.DefaultSessionPoolConfig,
UserAgent: userAgent,
DisableRouteToLeader: true,
},
},
{
Expand All @@ -55,8 +56,9 @@ func TestExtractDnsParts(t *testing.T) {
params: map[string]string{},
},
wantSpannerConfig: spanner.ClientConfig{
SessionPoolConfig: spanner.DefaultSessionPoolConfig,
UserAgent: userAgent,
SessionPoolConfig: spanner.DefaultSessionPoolConfig,
UserAgent: userAgent,
DisableRouteToLeader: true,
},
},
{
Expand All @@ -69,8 +71,9 @@ func TestExtractDnsParts(t *testing.T) {
params: map[string]string{},
},
wantSpannerConfig: spanner.ClientConfig{
SessionPoolConfig: spanner.DefaultSessionPoolConfig,
UserAgent: userAgent,
SessionPoolConfig: spanner.DefaultSessionPoolConfig,
UserAgent: userAgent,
DisableRouteToLeader: true,
},
},
{
Expand All @@ -83,8 +86,9 @@ func TestExtractDnsParts(t *testing.T) {
params: map[string]string{},
},
wantSpannerConfig: spanner.ClientConfig{
SessionPoolConfig: spanner.DefaultSessionPoolConfig,
UserAgent: userAgent,
SessionPoolConfig: spanner.DefaultSessionPoolConfig,
UserAgent: userAgent,
DisableRouteToLeader: true,
},
},
{
Expand All @@ -99,8 +103,9 @@ func TestExtractDnsParts(t *testing.T) {
},
},
wantSpannerConfig: spanner.ClientConfig{
SessionPoolConfig: spanner.DefaultSessionPoolConfig,
UserAgent: userAgent,
SessionPoolConfig: spanner.DefaultSessionPoolConfig,
UserAgent: userAgent,
DisableRouteToLeader: true,
},
},
{
Expand All @@ -115,8 +120,9 @@ func TestExtractDnsParts(t *testing.T) {
},
},
wantSpannerConfig: spanner.ClientConfig{
SessionPoolConfig: spanner.DefaultSessionPoolConfig,
UserAgent: userAgent,
SessionPoolConfig: spanner.DefaultSessionPoolConfig,
UserAgent: userAgent,
DisableRouteToLeader: true,
},
},
{
Expand All @@ -132,8 +138,9 @@ func TestExtractDnsParts(t *testing.T) {
},
},
wantSpannerConfig: spanner.ClientConfig{
SessionPoolConfig: spanner.DefaultSessionPoolConfig,
UserAgent: userAgent,
SessionPoolConfig: spanner.DefaultSessionPoolConfig,
UserAgent: userAgent,
DisableRouteToLeader: true,
},
},
{
Expand All @@ -148,8 +155,9 @@ func TestExtractDnsParts(t *testing.T) {
},
},
wantSpannerConfig: spanner.ClientConfig{
SessionPoolConfig: spanner.DefaultSessionPoolConfig,
UserAgent: userAgent,
SessionPoolConfig: spanner.DefaultSessionPoolConfig,
UserAgent: userAgent,
DisableRouteToLeader: true,
},
},
{
Expand All @@ -176,7 +184,37 @@ func TestExtractDnsParts(t *testing.T) {
MaxIdle: spanner.DefaultSessionPoolConfig.MaxIdle,
TrackSessionHandles: spanner.DefaultSessionPoolConfig.TrackSessionHandles,
},
UserAgent: userAgent,
UserAgent: userAgent,
DisableRouteToLeader: true,
},
},
{
input: "spanner.googleapis.com/projects/p/instances/i/databases/d?minSessions=200;maxSessions=1000;writeSessions=0.5;disableroutetoleader=false",
wantConnectorConfig: connectorConfig{
host: "spanner.googleapis.com",
project: "p",
instance: "i",
database: "d",
params: map[string]string{
"minsessions": "200",
"maxsessions": "1000",
"writesessions": "0.5",
"disableroutetoleader": "false",
},
},
wantSpannerConfig: spanner.ClientConfig{
SessionPoolConfig: spanner.SessionPoolConfig{
MinOpened: 200,
MaxOpened: 1000,
WriteSessions: 0.5,
HealthCheckInterval: spanner.DefaultSessionPoolConfig.HealthCheckInterval,
HealthCheckWorkers: spanner.DefaultSessionPoolConfig.HealthCheckWorkers,
MaxBurst: spanner.DefaultSessionPoolConfig.MaxBurst,
MaxIdle: spanner.DefaultSessionPoolConfig.MaxIdle,
TrackSessionHandles: spanner.DefaultSessionPoolConfig.TrackSessionHandles,
},
UserAgent: userAgent,
DisableRouteToLeader: false,
},
},
{
Expand Down
24 changes: 12 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ go 1.19
require (
cloud.google.com/go v0.110.0
cloud.google.com/go/longrunning v0.4.1
cloud.google.com/go/spanner v1.44.0
github.com/golang/protobuf v1.5.2
cloud.google.com/go/spanner v1.44.1-0.20230313050719-c5329ccb31c5
github.com/golang/protobuf v1.5.3
github.com/google/go-cmp v0.5.9
google.golang.org/api v0.111.0
google.golang.org/genproto v0.0.0-20230303212802-e74f57abe488
google.golang.org/api v0.112.0
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4
google.golang.org/grpc v1.53.0
google.golang.org/protobuf v1.28.1
google.golang.org/protobuf v1.29.0
)

require (
Expand All @@ -21,17 +21,17 @@ require (
github.com/census-instrumentation/opencensus-proto v0.4.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cncf/udpa/go v0.0.0-20220112060539-c52dc94e7fbe // indirect
github.com/cncf/xds/go v0.0.0-20230105202645-06c439db220b // indirect
github.com/envoyproxy/go-control-plane v0.10.3 // indirect
github.com/cncf/xds/go v0.0.0-20230310173818-32f1caf87195 // indirect
github.com/envoyproxy/go-control-plane v0.11.0 // indirect
github.com/envoyproxy/protoc-gen-validate v0.9.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/googleapis/gax-go/v2 v2.7.0 // indirect
github.com/googleapis/gax-go/v2 v2.7.1 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/oauth2 v0.5.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/oauth2 v0.6.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/appengine v1.6.7 // indirect
)
Loading