Skip to content

Commit

Permalink
feat: add missing spanner config properties (#152)
Browse files Browse the repository at this point in the history
* feat: add missing spanner config properties

* incorporate requested changes

* fix tests

* re-trigger checks
  • Loading branch information
rahul2393 committed Jun 28, 2023
1 parent 0ab6447 commit c6bda23
Show file tree
Hide file tree
Showing 8 changed files with 325 additions and 1,088 deletions.
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

0 comments on commit c6bda23

Please sign in to comment.