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

sample(spanner): add set max commit delay sample #1299

Merged
merged 3 commits into from
Feb 9, 2024
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
76 changes: 37 additions & 39 deletions spanner/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ GEM
specs:
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
base64 (0.2.0)
concurrent-ruby (1.2.2)
concurrent-ruby (1.2.3)
declarative (0.0.20)
diff-lcs (1.5.0)
faraday (2.8.1)
base64
faraday-net_http (>= 2.0, < 3.1)
ruby2_keywords (>= 0.0.4)
faraday-net_http (3.0.2)
diff-lcs (1.5.1)
faraday (2.9.0)
faraday-net_http (>= 2.0, < 3.2)
faraday-net_http (3.1.0)
net-http
faraday-retry (2.2.0)
faraday (~> 2.0)
gapic-common (0.21.1)
Expand All @@ -22,65 +20,66 @@ GEM
googleapis-common-protos-types (>= 1.11.0, < 2.a)
googleauth (~> 1.9)
grpc (~> 1.59)
google-apis-core (0.11.2)
google-apis-core (0.13.0)
addressable (~> 2.5, >= 2.5.1)
googleauth (>= 0.16.2, < 2.a)
googleauth (~> 1.9)
httpclient (>= 2.8.1, < 3.a)
mini_mime (~> 1.0)
representable (~> 3.0)
retriable (>= 2.0, < 4.a)
rexml
webrick
google-apis-iam_v1 (0.51.0)
google-apis-core (>= 0.11.0, < 2.a)
google-apis-iam_v1 (0.53.0)
google-apis-core (>= 0.12.0, < 2.a)
google-cloud-core (1.6.1)
google-cloud-env (>= 1.0, < 3.a)
google-cloud-errors (~> 1.0)
google-cloud-env (2.1.0)
google-cloud-env (2.1.1)
faraday (>= 1.0, < 3.a)
google-cloud-errors (1.3.1)
google-cloud-spanner (2.19.0)
google-cloud-spanner (2.21.0)
concurrent-ruby (~> 1.0)
google-cloud-core (~> 1.5)
google-cloud-spanner-admin-database-v1 (~> 0.1)
google-cloud-spanner-admin-instance-v1 (~> 0.1)
google-cloud-spanner-v1 (~> 0.2)
google-cloud-spanner-admin-database-v1 (0.15.0)
gapic-common (>= 0.20.0, < 2.a)
google-cloud-spanner-admin-database-v1 (0.17.1)
gapic-common (>= 0.21.1, < 2.a)
google-cloud-errors (~> 1.0)
grpc-google-iam-v1 (~> 1.1)
google-cloud-spanner-admin-instance-v1 (0.12.0)
gapic-common (>= 0.20.0, < 2.a)
google-cloud-spanner-admin-instance-v1 (0.13.2)
gapic-common (>= 0.21.1, < 2.a)
google-cloud-errors (~> 1.0)
grpc-google-iam-v1 (~> 1.1)
google-cloud-spanner-v1 (0.19.0)
gapic-common (>= 0.20.0, < 2.a)
google-cloud-spanner-v1 (0.22.2)
gapic-common (>= 0.21.1, < 2.a)
google-cloud-errors (~> 1.0)
google-protobuf (3.25.1)
google-protobuf (3.25.2)
googleapis-common-protos (1.4.0)
google-protobuf (~> 3.14)
googleapis-common-protos-types (~> 1.2)
grpc (~> 1.27)
googleapis-common-protos-types (1.11.0)
google-protobuf (~> 3.18)
googleauth (1.9.1)
googleauth (1.10.0)
faraday (>= 1.0, < 3.a)
google-cloud-env (~> 2.1)
jwt (>= 1.4, < 3.0)
multi_json (~> 1.11)
os (>= 0.9, < 2.0)
signet (>= 0.16, < 2.a)
grpc (1.60.0)
grpc (1.61.0)
google-protobuf (~> 3.25)
googleapis-common-protos-types (~> 1.0)
grpc-google-iam-v1 (1.4.0)
grpc-google-iam-v1 (1.5.0)
google-protobuf (~> 3.18)
googleapis-common-protos (~> 1.4)
grpc (~> 1.41)
httpclient (2.8.3)
jwt (2.7.1)
mini_mime (1.1.5)
multi_json (1.15.0)
net-http (0.4.1)
uri
os (1.1.4)
public_suffix (5.0.4)
representable (3.2.0)
Expand All @@ -89,30 +88,29 @@ GEM
uber (< 0.2.0)
retriable (3.1.2)
rexml (3.2.6)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.2)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.3)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.0)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.6)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.1)
rspec-support (~> 3.13.0)
rspec-support (3.13.0)
rspec_junit_formatter (0.6.0)
rspec-core (>= 2, < 4, != 2.12.0)
ruby2_keywords (0.0.5)
signet (0.18.0)
addressable (~> 2.8)
faraday (>= 0.17.5, < 3.a)
jwt (>= 1.5, < 3.0)
multi_json (~> 1.10)
trailblazer-option (0.1.2)
uber (0.1.0)
webrick (1.8.1)
uri (0.13.0)

PLATFORMS
ruby
Expand All @@ -124,4 +122,4 @@ DEPENDENCIES
rspec_junit_formatter

BUNDLED WITH
2.2.32
2.5.5
45 changes: 45 additions & 0 deletions spanner/spanner_set_max_commit_delay.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2024 Google, 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.

# [START spanner_set_max_commit_delay]
require "google/cloud/spanner"

##
# This is a snippet for showcasing how to pass max_commit_delay in commit_options.
#
# @param project_id [String] The ID of the Google Cloud project.
# @param instance_id [String] The ID of the spanner instance.
# @param database_id [String] The ID of the database.
#
def spanner_set_max_commit_delay project_id:, instance_id:, database_id:
# Instantiates a client
spanner = Google::Cloud::Spanner.new project: project_id
client = spanner.client instance_id, database_id

records = [
{ SingerId: 1, AlbumId: 1, MarketingBudget: 200_000 },
{ SingerId: 2, AlbumId: 2, MarketingBudget: 400_000 }
]
# max_commit_delay is the amount of latency in millisecond, this request
# is willing to incur in order to improve throughput.
# The commit delay must be at least 0ms and at most 500ms.
# Default value is nil.
commit_options = {
return_commit_stats: true,
max_commit_delay: 100
}
resp = client.upsert "Albums", records, commit_options: commit_options
puts "Updated data with #{resp.stats.mutation_count} mutations."
end
# [END spanner_set_max_commit_delay]
40 changes: 40 additions & 0 deletions spanner/spec/spanner_set_max_commit_delay_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2024 Google, 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.

require_relative "./spec_helper"
require_relative "../spanner_set_max_commit_delay"

describe "Google Cloud Spanner max_commit_delay examples" do
before :each do
cleanup_database_resources
create_dml_singers_albums_database
insert_dml_data project_id: @project_id,
instance_id: @instance_id,
database_id: @database_id
end

after :each do
cleanup_database_resources
end

example "spanner_directed_read" do
capture do
spanner_set_max_commit_delay project_id: @project_id,
instance_id: @instance_id,
database_id: @database_id
end

expect(captured_output).to include "Updated data with 6 mutations"
end
end
Loading