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 missing spanner config properties #152

Merged
merged 4 commits into from
Jun 28, 2023
Merged
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
42 changes: 23 additions & 19 deletions benchmarks/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,41 @@ go 1.19
replace github.com/googleapis/go-sql-spanner => ../

require (
cloud.google.com/go v0.110.0
cloud.google.com/go/spanner v1.44.0
cloud.google.com/go v0.110.2
cloud.google.com/go/spanner v1.46.0
github.com/google/uuid v1.3.0
github.com/googleapis/go-sql-spanner v1.0.0
google.golang.org/api v0.111.0
google.golang.org/grpc v1.53.0
google.golang.org/protobuf v1.28.1
github.com/googleapis/go-sql-spanner v1.0.1
google.golang.org/api v0.125.0
google.golang.org/grpc v1.55.0
google.golang.org/protobuf v1.30.0
)

require (
cloud.google.com/go/compute v1.18.0 // indirect
cloud.google.com/go/compute v1.19.3 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v0.12.0 // indirect
cloud.google.com/go/longrunning v0.4.1 // indirect
cloud.google.com/go/iam v0.13.0 // indirect
cloud.google.com/go/longrunning v0.5.0 // indirect
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/envoyproxy/protoc-gen-validate v0.9.1 // 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.10.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/s2a-go v0.1.4 // 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.10.0 // 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/crypto v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230303212802-e74f57abe488 // indirect
google.golang.org/genproto v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
)
398 changes: 59 additions & 339 deletions benchmarks/go.sum

Large diffs are not rendered by default.

56 changes: 51 additions & 5 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ import (
"cloud.google.com/go/spanner"
adminapi "cloud.google.com/go/spanner/admin/database/apiv1"
adminpb "cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
"cloud.google.com/go/spanner/apiv1/spannerpb"
"google.golang.org/api/option"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"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 +52,17 @@ 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 false
// - minSessions: The minimum number of sessions in the backing session pool. The default is 100.
// - maxSessions: The maximum number of sessions in the backing session pool. The default is 400.
// - numChannels: The number of gRPC channels to use to communicate with Cloud Spanner. The default is 4.
// - optimizerVersion: Sets the default query optimizer version to use for this connection.
// - optimizerStatisticsPackage: Sets the default query optimizer statistic package to use for this connection.
// - rpcPriority: Sets the priority for all RPC invocations from this connection (HIGH/MEDIUM/LOW). The default is HIGH.
//
// 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=true`
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 @@ -206,9 +216,45 @@ func newConnector(d *Driver, dsn string) (*connector, error) {
config.MaxOpened = val
}
}
if strval, ok := connectorConfig.params["writesessions"]; ok {
if val, err := strconv.ParseFloat(strval, 64); err == nil {
config.WriteSessions = val
if strval, ok := connectorConfig.params["numchannels"]; ok {
if val, err := strconv.Atoi(strval); err == nil && val > 0 {
config.NumChannels = val
}
}
if strval, ok := connectorConfig.params["rpcpriority"]; ok {
var priority spannerpb.RequestOptions_Priority
switch strings.ToUpper(strval) {
case "LOW":
priority = spannerpb.RequestOptions_PRIORITY_LOW
case "MEDIUM":
priority = spannerpb.RequestOptions_PRIORITY_MEDIUM
case "HIGH":
priority = spannerpb.RequestOptions_PRIORITY_HIGH
default:
priority = spannerpb.RequestOptions_PRIORITY_UNSPECIFIED
}
config.ReadOptions.Priority = priority
config.TransactionOptions.CommitPriority = priority
config.QueryOptions.Priority = priority
}
if strval, ok := connectorConfig.params["optimizerversion"]; ok {
if config.QueryOptions.Options == nil {
config.QueryOptions.Options = &spannerpb.ExecuteSqlRequest_QueryOptions{}
}
config.QueryOptions.Options.OptimizerVersion = strval
}
if strval, ok := connectorConfig.params["optimizerstatisticspackage"]; ok {
if config.QueryOptions.Options == nil {
config.QueryOptions.Options = &spannerpb.ExecuteSqlRequest_QueryOptions{}
}
config.QueryOptions.Options.OptimizerStatisticsPackage = strval
}
if strval, ok := connectorConfig.params["databaserole"]; ok {
config.DatabaseRole = strval
}
if strval, ok := connectorConfig.params["disableroutetoleader"]; ok {
if val, err := strconv.ParseBool(strval); err == nil {
config.DisableRouteToLeader = val
}
}
config.UserAgent = userAgent
Expand Down
26 changes: 19 additions & 7 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"cloud.google.com/go/spanner"
"cloud.google.com/go/spanner/apiv1/spannerpb"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -153,30 +154,41 @@ func TestExtractDnsParts(t *testing.T) {
},
},
{
input: "spanner.googleapis.com/projects/p/instances/i/databases/d?minSessions=200;maxSessions=1000;writeSessions=0.5",
input: "spanner.googleapis.com/projects/p/instances/i/databases/d?minSessions=200;maxSessions=1000;numChannels=10;disableRouteToLeader=true;rpcPriority=Medium;optimizerVersion=1;optimizerStatisticsPackage=latest;databaseRole=child",
wantConnectorConfig: connectorConfig{
host: "spanner.googleapis.com",
project: "p",
instance: "i",
database: "d",
params: map[string]string{
"minsessions": "200",
"maxsessions": "1000",
"writesessions": "0.5",
"minsessions": "200",
"maxsessions": "1000",
"numchannels": "10",
"disableroutetoleader": "true",
"rpcpriority": "Medium",
"optimizerversion": "1",
"optimizerstatisticspackage": "latest",
"databaserole": "child",
},
},
wantSpannerConfig: spanner.ClientConfig{
SessionPoolConfig: spanner.SessionPoolConfig{
MinOpened: 200,
MaxOpened: 1000,
WriteSessions: 0.5,
WriteSessions: 0.2,
HealthCheckInterval: spanner.DefaultSessionPoolConfig.HealthCheckInterval,
HealthCheckWorkers: spanner.DefaultSessionPoolConfig.HealthCheckWorkers,
MaxBurst: spanner.DefaultSessionPoolConfig.MaxBurst,
MaxIdle: spanner.DefaultSessionPoolConfig.MaxIdle,
TrackSessionHandles: spanner.DefaultSessionPoolConfig.TrackSessionHandles,
},
UserAgent: userAgent,
NumChannels: 10,
UserAgent: userAgent,
DisableRouteToLeader: true,
QueryOptions: spanner.QueryOptions{Priority: spannerpb.RequestOptions_PRIORITY_MEDIUM, Options: &spannerpb.ExecuteSqlRequest_QueryOptions{OptimizerVersion: "1", OptimizerStatisticsPackage: "latest"}},
ReadOptions: spanner.ReadOptions{Priority: spannerpb.RequestOptions_PRIORITY_MEDIUM},
TransactionOptions: spanner.TransactionOptions{CommitPriority: spannerpb.RequestOptions_PRIORITY_MEDIUM},
DatabaseRole: "child",
},
},
{
Expand Down Expand Up @@ -204,7 +216,7 @@ func TestExtractDnsParts(t *testing.T) {
if err != nil {
t.Errorf("failed to get connector for %q: %v", tc.input, err)
}
if !cmp.Equal(conn.spannerClientConfig, tc.wantSpannerConfig, cmpopts.IgnoreUnexported(spanner.ClientConfig{}, spanner.SessionPoolConfig{})) {
if !cmp.Equal(conn.spannerClientConfig, tc.wantSpannerConfig, cmpopts.IgnoreUnexported(spanner.ClientConfig{}, spanner.SessionPoolConfig{}, spannerpb.ExecuteSqlRequest_QueryOptions{})) {
t.Errorf("connector Spanner client config mismatch for %q\n Got: %v\nWant: %v", tc.input, conn.spannerClientConfig, tc.wantSpannerConfig)
}
}
Expand Down
48 changes: 26 additions & 22 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,53 @@ go 1.19
replace github.com/googleapis/go-sql-spanner => ../

require (
cloud.google.com/go v0.110.0
cloud.google.com/go/spanner v1.44.0
github.com/docker/docker v23.0.1+incompatible
cloud.google.com/go v0.110.2
cloud.google.com/go/spanner v1.46.0
github.com/docker/docker v24.0.2+incompatible
github.com/docker/go-connections v0.4.0
github.com/googleapis/go-sql-spanner v1.0.0
github.com/googleapis/go-sql-spanner v1.0.1
)

require (
cloud.google.com/go/compute v1.18.0 // indirect
cloud.google.com/go/compute v1.19.3 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v0.12.0 // indirect
cloud.google.com/go/longrunning v0.4.1 // indirect
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
cloud.google.com/go/iam v0.13.0 // indirect
cloud.google.com/go/longrunning v0.5.0 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect
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/cncf/xds/go v0.0.0-20230310173818-32f1caf87195 // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/envoyproxy/go-control-plane v0.10.3 // indirect
github.com/envoyproxy/protoc-gen-validate v0.9.1 // indirect
github.com/envoyproxy/go-control-plane v0.11.0 // indirect
github.com/envoyproxy/protoc-gen-validate v0.10.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/s2a-go v0.1.4 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/googleapis/gax-go/v2 v2.7.0 // indirect
github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect
github.com/googleapis/gax-go/v2 v2.10.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/pkg/errors v0.9.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/crypto v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.111.0 // indirect
google.golang.org/api v0.125.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230303212802-e74f57abe488 // indirect
google.golang.org/grpc v1.53.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
google.golang.org/genproto v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/grpc v1.55.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gotest.tools/v3 v3.4.0 // indirect
)
Loading