Skip to content

Commit 999ddb1

Browse files
chore: document some private methods and add multiplex plumbing (#184)
1 parent 40264d0 commit 999ddb1

13 files changed

Lines changed: 304 additions & 82 deletions

File tree

google-cloud-spanner/.rubocop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ AllCops:
1111
- "vendor/**/*"
1212
- "acceptance/data/protos/**/*"
1313

14-
Documentation:
14+
Style/Documentation:
1515
Enabled: false
1616

1717
Metrics/BlockLength:

google-cloud-spanner/Gemfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ gem "minitest-rg", "~> 5.3"
1919
gem "rake"
2020
gem "redcarpet", "~> 3.0"
2121
gem "simplecov", "~> 0.22"
22+
gem "solargraph", group: :development, require: false
2223
gem "yard", "~> 0.9"
23-
gem "yard-doctest", "~> 0.1.17"
24+
gem "yard-doctest", "~> 0.1.17", group: :development

google-cloud-spanner/acceptance/spanner/backup_operations_test.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
it "list backup operations" do
2525
skip if emulator_enabled?
2626

27+
# @type [::Google::Cloud::Spanner::Instance]
2728
instance = spanner.instance instance_id
2829
_(instance).wont_be :nil?
2930

@@ -33,14 +34,14 @@
3334
create_job = database.create_backup backup_id, expire_time
3435
create_job.wait_until_done!
3536

36-
# All
37+
# @type [::Array<Google::Cloud::Spanner::Backup::Job>]
3738
jobs = instance.backup_operations.all.to_a
3839
_(jobs).wont_be :empty?
3940

4041
jobs.each do |job|
4142
_(job).must_be_kind_of Google::Cloud::Spanner::Backup::Job
4243

43-
unless job.error?
44+
if job.done? && !job.error?
4445
_(job.backup).must_be_kind_of Google::Cloud::Spanner::Backup
4546
end
4647

google-cloud-spanner/lib/google/cloud/spanner/backup/job.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,14 @@ class Backup
5858
# end
5959
#
6060
class Job
61-
##
62-
# @private The `Gapic::Operation` gRPC object.
61+
# The wrapped `Gapic::Operation` object.
62+
# @private
63+
# @return [::Gapic::Operation]
6364
attr_accessor :grpc
6465

65-
##
66-
# @private The gRPC Service object.
66+
# The `Spanner::Service` reference.
67+
# @private
68+
# @return [::Google::Cloud::Spanner::Service]
6769
attr_accessor :service
6870

6971
##
@@ -265,8 +267,11 @@ def cancel_time
265267
Convert.timestamp_to_time @grpc.metadata.cancel_time
266268
end
267269

268-
##
269-
# @private New Backup::Job from a `Gapic::Operation` object.
270+
# Create a new Backup::Job from a `Gapic::Operation` object.
271+
# @param grpc [::Gapic::Operation`] The wrapped `Gapic::Operation` object.
272+
# @param service [::Google::Cloud::Spanner::Service] A `Spanner::Service` reference.
273+
# @private
274+
# @return [::Google::Cloud::Spanner::Backup::Job]
270275
def self.from_grpc grpc, service
271276
new.tap do |job|
272277
job.instance_variable_set :@grpc, grpc

google-cloud-spanner/lib/google/cloud/spanner/backup/job/list.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ class Job
3333
# {Google::Cloud::Spanner::Admin::Database.database_admin}.list_backup_operations instead.
3434
#
3535
class List < DelegateClass(::Array)
36+
# The `Spanner::Service` reference.
3637
# @private
37-
# The gRPC Service object.
38+
# @return [::Google::Cloud::Spanner::Service]
3839
attr_accessor :service
3940

4041
# @private
@@ -148,13 +149,14 @@ def all &block
148149
end
149150
end
150151

151-
##
152-
# @private
153-
#
154-
# New Backup::Job::List from a
152+
# Creates a new `Spanner::Backup::Job::List` of `Gapic::Operation` operations from a
155153
# `Gapic::PagedEnumerable<Google::Longrunning::Operation>`
156154
# object. Operation object is a backup operation.
157-
#
155+
# @param grpc [::Gapic::PagedEnumerable<::Google::Longrunning::Operation>]
156+
# Wrapped `Gapic::PagedEnumberable` reference.
157+
# @param service [::Google::Cloud::Spanner::Service] A `Spanner::Service` reference.
158+
# @private
159+
# @return [::Google::Cloud::Spanner::Backup::Job::List]
158160
def self.from_grpc grpc, service
159161
operations_client =
160162
service.databases.instance_variable_get "@operations_client"

google-cloud-spanner/lib/google/cloud/spanner/batch_client.rb

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,18 @@ module Spanner
6363
# new_partition
6464
#
6565
class BatchClient
66-
##
67-
# @private Creates a new Spanner BatchClient instance.
66+
# Creates a new Spanner BatchClient instance.
67+
# @param project [::Google::Cloud::Spanner::Project] A `Spanner::Project` ref.
68+
# @param instance_id [::String] Instance id, e.g. `"my-instance"`.
69+
# @param database_id [::String] Database id, e.g. `"my-database"`.
70+
# @param session_labels [::Hash, nil] Optional. The labels to be applied to all sessions
71+
# created by the client. Example: `"team" => "billing-service"`.
72+
# @param query_options [::Hash, nil] Optional. A hash of values to specify the custom
73+
# query options for executing SQL query. Example parameter `:optimizer_version`.
74+
# @param directed_read_options [::Hash, nil] Optional. Client options used to set
75+
# the `directed_read_options` for all ReadRequests and ExecuteSqlRequests.
76+
# Converts to `V1::DirectedReadOptions`. Example option: `:exclude_replicas`.
77+
# @private
6878
def initialize project, instance_id, database_id, session_labels: nil,
6979
query_options: nil, directed_read_options: nil
7080
@project = project
@@ -404,15 +414,18 @@ def inspect
404414

405415
protected
406416

407-
##
408-
# @private Raise an error unless an active connection to the service is
417+
# Raise an error unless an active connection to the service is
409418
# available.
419+
# @private
420+
# @raise [StandardError]
421+
# @return [nil]
410422
def ensure_service!
411423
raise "Must have active connection to service" unless @project.service
412424
end
413425

414-
##
415426
# New session for each use.
427+
# @private
428+
# @return [::Google::Cloud::Spanner::Session]
416429
def session
417430
ensure_service!
418431
grpc = @project.service.create_session \

google-cloud-spanner/lib/google/cloud/spanner/client.rb

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,26 @@ module Spanner
5151
# end
5252
#
5353
class Client
54-
##
54+
# A semi-arbitrary constant for thread-wide global parameter name
5555
# @private
5656
IS_TRANSACTION_RUNNING_KEY = "ruby_spanner_is_transaction_running".freeze
5757

58-
##
59-
# @private Creates a new Spanner Client instance.
58+
# Creates a new Spanner Client instance.
59+
# @param project [::Google::Cloud::Spanner::Project] A `Spanner::Project` ref.
60+
# @param instance_id [::String] Instance id, e.g. `"my-instance"`.
61+
# @param database_id [::String] Database id, e.g. `"my-database"`.
62+
# @param session_labels [::Hash, nil] Optional. The labels to be applied to all sessions
63+
# created by the client. Example: `"team" => "billing-service"`.
64+
# @param pool_opts [::Hash] Optional. `Spanner::Pool` creation options.
65+
# Example parameter: `:keepalive`.
66+
# @param query_options [::Hash, nil] Optional. A hash of values to specify the custom
67+
# query options for executing SQL query. Example parameter `:optimizer_version`.
68+
# @param database_role [::String, nil] Optional. The Spanner session creator role.
69+
# Example: `analyst`
70+
# @param directed_read_options [::Hash, nil] Optional. Client options used to set
71+
# the `directed_read_options` for all ReadRequests and ExecuteSqlRequests.
72+
# Converts to `V1::DirectedReadOptions`. Example option: `:exclude_replicas`.
73+
# @private
6074
def initialize project, instance_id, database_id, session_labels: nil,
6175
pool_opts: {}, query_options: nil, database_role: nil,
6276
directed_read_options: nil
@@ -89,7 +103,7 @@ def database_id
89103
end
90104

91105
# The Spanner project connected to.
92-
# @return [Project]
106+
# @return [::Google::Cloud::Spanner::Project]
93107
def project
94108
@project
95109
end
@@ -2114,13 +2128,15 @@ def transaction deadline: 120, exclude_txn_from_change_streams: false,
21142128
yield tx
21152129
transaction_id = nil
21162130
transaction_id = tx.transaction_id if tx.existing_transaction?
2117-
commit_resp = @project.service.commit \
2118-
tx.session.path, tx.mutations,
2131+
commit_resp = @project.service.commit(
2132+
tx.session.path,
2133+
tx.mutations,
21192134
transaction_id: transaction_id,
21202135
exclude_txn_from_change_streams: exclude_txn_from_change_streams,
21212136
commit_options: commit_options,
21222137
request_options: request_options,
21232138
call_options: call_options
2139+
)
21242140
resp = CommitResponse.from_grpc commit_resp
21252141
commit_options ? resp : resp.timestamp
21262142
rescue GRPC::Aborted,
@@ -2419,17 +2435,21 @@ def reset
24192435
@pool.reset
24202436
end
24212437

2422-
##
2438+
# Creates a new Session objece.
2439+
# @param multiplexed [::Boolean] Optional. Default to `false`.
2440+
# If `true`, specifies a multiplexed session.
24232441
# @private
2424-
# Creates a new session object every time.
2425-
def create_new_session
2442+
# @return [::Google::Cloud::Spanner::Session]
2443+
def create_new_session multiplexed: false
24262444
ensure_service!
24272445
grpc = @project.service.create_session \
24282446
Admin::Database::V1::DatabaseAdmin::Paths.database_path(
24292447
project: project_id, instance: instance_id, database: database_id
24302448
),
24312449
labels: @session_labels,
2432-
database_role: @database_role
2450+
database_role: @database_role,
2451+
multiplexed: multiplexed
2452+
24332453
Session.from_grpc grpc, @project.service, query_options: @query_options
24342454
end
24352455

google-cloud-spanner/lib/google/cloud/spanner/instance.rb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,16 @@ module Spanner
7474
# end
7575
#
7676
class Instance
77-
##
78-
# @private The gRPC Service object.
77+
# The `Spanner::Service` reference.
78+
# @private
79+
# @return [::Google::Cloud::Spanner::Service]
7980
attr_accessor :service
8081

81-
# @private Creates a new Instance instance.
82+
# Creates a new `Spanner::Instance` instance.
83+
# @param grpc [::Google::Cloud::Spanner::Admin::Instance::V1::Instance]
84+
# The protobuf `V1::Instance` underlying object.
85+
# @param service [::Google::Cloud::Spanner::Service] A `Spanner::Service` reference.
86+
# @private
8287
def initialize grpc, service
8388
@grpc = grpc
8489
@service = service
@@ -957,9 +962,13 @@ def test_permissions *permissions
957962
grpc.permissions
958963
end
959964

960-
##
961-
# @private Creates a new Instance instance from a
965+
# Creates a new Instance instance from a
962966
# `Google::Cloud::Spanner::Admin::Instance::V1::Instance`.
967+
# @param grpc [::Google::Cloud::Spanner::Admin::Instance::V1::Instance]
968+
# The protobuf `V1::Instance` underlying object.
969+
# @param service [::Google::Cloud::Spanner::Service] A `Spanner::Service` reference.
970+
# @private
971+
# @return [::Google::Cloud::Spanner::Instance]
963972
def self.from_grpc grpc, service
964973
new grpc, service
965974
end

google-cloud-spanner/lib/google/cloud/spanner/pool.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ class Pool
3636
# and `Session` objects as values.
3737
attr_accessor :sessions_in_use
3838

39+
# Creates a new Session pool that manages non-multiplexed sessions.
40+
# @param client [::Google::Cloud::Spanner::Client] A `Spanner::Client` reference
41+
# @param min [::Integer] Min number of sessions to keep
42+
# @param max [::Integer] Max number of sessions to keep
43+
# @param keepalive [::Numeric] How long after their last usage the sessions can be reclaimed
44+
# @param fail [::Boolean] If `true` the pool will raise `SessionLimitError` if number of new sessions
45+
# needed is more that can be created due to the `max` parameter. If `false` it will wait instead.
46+
# @param threads [::Integer, nil] Number of threads in the thread pool that is used for keepalive and
47+
# release session actions. If `nil` the Pool will choose a reasonable default.
48+
# @private
3949
def initialize client, min: 10, max: 100, keepalive: 1800,
4050
fail: true, threads: nil
4151
@client = client
@@ -52,6 +62,11 @@ def initialize client, min: 10, max: 100, keepalive: 1800,
5262
init
5363
end
5464

65+
# Provides a session for running an operation
66+
# @yield session Session a client can use to run an operation
67+
# @yieldparam [::Google::Cloud::Spanner::Session] `Spanner::Session` to run an operation
68+
# @private
69+
# @return [nil]
5570
def with_session
5671
session = checkout_session
5772
begin

google-cloud-spanner/lib/google/cloud/spanner/project.rb

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,22 @@ module Spanner
6666
# end
6767
#
6868
class Project
69-
##
70-
# @private The Service object.
71-
attr_accessor :service, :query_options
69+
# The `Spanner::Service` reference.
70+
# @private
71+
# @return [::Google::Cloud::Spanner::Service]
72+
attr_accessor :service
7273

73-
##
74-
# @private Creates a new Spanner Project instance.
74+
# A hash of values to specify the custom query options for executing SQL query.
75+
# Example option: `:optimizer_version`.
76+
# @private
77+
# @return [::Hash, nil]
78+
attr_accessor :query_options
79+
80+
# Creates a new Spanner Project instance.
81+
# @param service [::Google::Cloud::Spanner::Service] The `Spanner::Service` ref.
82+
# @param query_options [::Hash, nil] Optional. A hash of values to specify the custom
83+
# query options for executing SQL query. Example option: `:optimizer_version`.
84+
# @private
7585
def initialize service, query_options: nil
7686
@service = service
7787
@query_options = query_options
@@ -80,6 +90,8 @@ def initialize service, query_options: nil
8090
##
8191
# The identifier for the Cloud Spanner project.
8292
#
93+
# @return [::String]
94+
#
8395
# @example
8496
# require "google/cloud"
8597
#

0 commit comments

Comments
 (0)