google-cloud-spanner 1.16.2 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. checksums.yaml +4 -4
  2. data/AUTHENTICATION.md +1 -1
  3. data/CHANGELOG.md +66 -0
  4. data/CONTRIBUTING.md +1 -1
  5. data/lib/google-cloud-spanner.rb +11 -13
  6. data/lib/google/cloud/spanner.rb +13 -13
  7. data/lib/google/cloud/spanner/admin/database/credentials.rb +2 -2
  8. data/lib/google/cloud/spanner/admin/instance/credentials.rb +2 -2
  9. data/lib/google/cloud/spanner/backup.rb +9 -1
  10. data/lib/google/cloud/spanner/backup/job.rb +2 -2
  11. data/lib/google/cloud/spanner/backup/job/list.rb +2 -2
  12. data/lib/google/cloud/spanner/backup/list.rb +1 -2
  13. data/lib/google/cloud/spanner/backup/restore/job.rb +2 -2
  14. data/lib/google/cloud/spanner/batch_client.rb +2 -2
  15. data/lib/google/cloud/spanner/batch_snapshot.rb +120 -23
  16. data/lib/google/cloud/spanner/batch_update.rb +1 -1
  17. data/lib/google/cloud/spanner/client.rb +388 -47
  18. data/lib/google/cloud/spanner/commit.rb +14 -14
  19. data/lib/google/cloud/spanner/commit_response.rb +87 -0
  20. data/lib/google/cloud/spanner/commit_response/commit_stats.rb +51 -0
  21. data/lib/google/cloud/spanner/convert.rb +16 -7
  22. data/lib/google/cloud/spanner/credentials.rb +2 -2
  23. data/lib/google/cloud/spanner/data.rb +2 -2
  24. data/lib/google/cloud/spanner/database.rb +47 -25
  25. data/lib/google/cloud/spanner/database/backup_info.rb +13 -4
  26. data/lib/google/cloud/spanner/database/job.rb +3 -3
  27. data/lib/google/cloud/spanner/database/job/list.rb +2 -2
  28. data/lib/google/cloud/spanner/database/list.rb +1 -1
  29. data/lib/google/cloud/spanner/database/restore_info.rb +1 -1
  30. data/lib/google/cloud/spanner/fields.rb +8 -8
  31. data/lib/google/cloud/spanner/instance.rb +5 -12
  32. data/lib/google/cloud/spanner/instance/config.rb +1 -1
  33. data/lib/google/cloud/spanner/instance/config/list.rb +1 -1
  34. data/lib/google/cloud/spanner/instance/job.rb +2 -2
  35. data/lib/google/cloud/spanner/instance/list.rb +1 -1
  36. data/lib/google/cloud/spanner/partition.rb +4 -4
  37. data/lib/google/cloud/spanner/policy.rb +2 -2
  38. data/lib/google/cloud/spanner/results.rb +103 -24
  39. data/lib/google/cloud/spanner/service.rb +369 -350
  40. data/lib/google/cloud/spanner/session.rb +370 -39
  41. data/lib/google/cloud/spanner/snapshot.rb +60 -6
  42. data/lib/google/cloud/spanner/transaction.rb +116 -10
  43. data/lib/google/cloud/spanner/version.rb +1 -1
  44. metadata +20 -107
  45. data/lib/google/cloud/spanner/admin/database.rb +0 -149
  46. data/lib/google/cloud/spanner/admin/database/v1.rb +0 -147
  47. data/lib/google/cloud/spanner/admin/database/v1/credentials.rb +0 -46
  48. data/lib/google/cloud/spanner/admin/database/v1/database_admin_client.rb +0 -1513
  49. data/lib/google/cloud/spanner/admin/database/v1/database_admin_client_config.json +0 -111
  50. data/lib/google/cloud/spanner/admin/database/v1/doc/google/iam/v1/iam_policy.rb +0 -64
  51. data/lib/google/cloud/spanner/admin/database/v1/doc/google/iam/v1/options.rb +0 -33
  52. data/lib/google/cloud/spanner/admin/database/v1/doc/google/iam/v1/policy.rb +0 -151
  53. data/lib/google/cloud/spanner/admin/database/v1/doc/google/longrunning/operations.rb +0 -51
  54. data/lib/google/cloud/spanner/admin/database/v1/doc/google/protobuf/any.rb +0 -131
  55. data/lib/google/cloud/spanner/admin/database/v1/doc/google/protobuf/empty.rb +0 -29
  56. data/lib/google/cloud/spanner/admin/database/v1/doc/google/protobuf/field_mask.rb +0 -222
  57. data/lib/google/cloud/spanner/admin/database/v1/doc/google/protobuf/timestamp.rb +0 -113
  58. data/lib/google/cloud/spanner/admin/database/v1/doc/google/rpc/status.rb +0 -39
  59. data/lib/google/cloud/spanner/admin/database/v1/doc/google/spanner/admin/database/v1/backup.rb +0 -325
  60. data/lib/google/cloud/spanner/admin/database/v1/doc/google/spanner/admin/database/v1/spanner_database_admin.rb +0 -368
  61. data/lib/google/cloud/spanner/admin/database/v1/doc/google/type/expr.rb +0 -45
  62. data/lib/google/cloud/spanner/admin/instance.rb +0 -164
  63. data/lib/google/cloud/spanner/admin/instance/v1.rb +0 -162
  64. data/lib/google/cloud/spanner/admin/instance/v1/credentials.rb +0 -46
  65. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/iam/v1/iam_policy.rb +0 -64
  66. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/iam/v1/options.rb +0 -33
  67. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/iam/v1/policy.rb +0 -151
  68. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/longrunning/operations.rb +0 -51
  69. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/protobuf/any.rb +0 -131
  70. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/protobuf/empty.rb +0 -29
  71. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/protobuf/field_mask.rb +0 -222
  72. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/rpc/status.rb +0 -39
  73. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/spanner/admin/instance/v1/spanner_instance_admin.rb +0 -334
  74. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/type/expr.rb +0 -45
  75. data/lib/google/cloud/spanner/admin/instance/v1/instance_admin_client.rb +0 -975
  76. data/lib/google/cloud/spanner/admin/instance/v1/instance_admin_client_config.json +0 -76
  77. data/lib/google/cloud/spanner/v1.rb +0 -16
  78. data/lib/google/cloud/spanner/v1/credentials.rb +0 -42
  79. data/lib/google/cloud/spanner/v1/doc/google/protobuf/any.rb +0 -131
  80. data/lib/google/cloud/spanner/v1/doc/google/protobuf/duration.rb +0 -91
  81. data/lib/google/cloud/spanner/v1/doc/google/protobuf/empty.rb +0 -29
  82. data/lib/google/cloud/spanner/v1/doc/google/protobuf/struct.rb +0 -74
  83. data/lib/google/cloud/spanner/v1/doc/google/protobuf/timestamp.rb +0 -113
  84. data/lib/google/cloud/spanner/v1/doc/google/rpc/status.rb +0 -39
  85. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/keys.rb +0 -150
  86. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/mutation.rb +0 -95
  87. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/query_plan.rb +0 -121
  88. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/result_set.rb +0 -190
  89. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/spanner.rb +0 -570
  90. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/transaction.rb +0 -432
  91. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/type.rb +0 -112
  92. data/lib/google/cloud/spanner/v1/spanner_client.rb +0 -1485
  93. data/lib/google/cloud/spanner/v1/spanner_client_config.json +0 -121
  94. data/lib/google/spanner/admin/database/v1/backup_pb.rb +0 -98
  95. data/lib/google/spanner/admin/database/v1/common_pb.rb +0 -28
  96. data/lib/google/spanner/admin/database/v1/spanner_database_admin_pb.rb +0 -141
  97. data/lib/google/spanner/admin/database/v1/spanner_database_admin_services_pb.rb +0 -169
  98. data/lib/google/spanner/admin/instance/v1/spanner_instance_admin_pb.rb +0 -125
  99. data/lib/google/spanner/admin/instance/v1/spanner_instance_admin_services_pb.rb +0 -181
  100. data/lib/google/spanner/v1/keys_pb.rb +0 -34
  101. data/lib/google/spanner/v1/mutation_pb.rb +0 -39
  102. data/lib/google/spanner/v1/query_plan_pb.rb +0 -48
  103. data/lib/google/spanner/v1/result_set_pb.rb +0 -48
  104. data/lib/google/spanner/v1/spanner_pb.rb +0 -179
  105. data/lib/google/spanner/v1/spanner_services_pb.rb +0 -179
  106. data/lib/google/spanner/v1/transaction_pb.rb +0 -56
  107. data/lib/google/spanner/v1/type_pb.rb +0 -44
@@ -84,17 +84,26 @@ module Google
84
84
  end
85
85
 
86
86
  ##
87
- # The backup contains an externally consistent copy of
88
- # `source_database` at the timestamp specified by `create_time`.
89
- # received.
87
+ # The timestamp indicating the creation of the backup.
90
88
  # @return [Time]
91
89
  def create_time
92
90
  Convert.timestamp_to_time @grpc.create_time
93
91
  end
94
92
 
93
+ ##
94
+ # The backup contains an externally consistent copy of
95
+ # `source_database` at the timestamp specified by
96
+ # the `version_time` received. If no `version_time` was
97
+ # given during the creation of the backup, the `version_time`
98
+ # will be the same as the `create_time`.
99
+ # @return [Time]
100
+ def version_time
101
+ Convert.timestamp_to_time @grpc.version_time
102
+ end
103
+
95
104
  ##
96
105
  # @private Creates a new Database::BackupInfo instance from a
97
- # Google::Spanner::Admin::Database::V1::BackupInfo.
106
+ # `Google::Cloud::Spanner::Admin::Database::V1::BackupInfo`.
98
107
  def self.from_grpc grpc
99
108
  new grpc
100
109
  end
@@ -52,7 +52,7 @@ module Google
52
52
  #
53
53
  class Job
54
54
  ##
55
- # @private The Google::Gax::Operation gRPC object.
55
+ # @private The `Gapic::Operation` gRPC object.
56
56
  attr_accessor :grpc
57
57
 
58
58
  ##
@@ -89,7 +89,7 @@ module Google
89
89
  return nil unless done?
90
90
  return nil unless @grpc.grpc_op.result == :response
91
91
  return nil unless @grpc.results.instance_of? \
92
- Google::Spanner::Admin::Database::V1::Database
92
+ Admin::Database::V1::Database
93
93
  Database.from_grpc @grpc.results, service
94
94
  end
95
95
 
@@ -201,7 +201,7 @@ module Google
201
201
  end
202
202
 
203
203
  ##
204
- # @private New Database::Job from a Google::Gax::Operation object.
204
+ # @private New Database::Job from a `Gapic::Operation` object.
205
205
  def self.from_grpc grpc, service
206
206
  new.tap do |job|
207
207
  job.instance_variable_set :@grpc, grpc
@@ -146,7 +146,7 @@ module Google
146
146
  # @private
147
147
  #
148
148
  # New Database::Job::List from a
149
- # Google::Gax::PagedEnumerable<Google::Longrunning::Operation>
149
+ # `Gapic::PagedEnumerable<Google::Longrunning::Operation>`
150
150
  # object. Operation object is a database operation.
151
151
  #
152
152
  def self.from_grpc grpc, service
@@ -154,7 +154,7 @@ module Google
154
154
  service.databases.instance_variable_get "@operations_client"
155
155
  jobs = new(Array(grpc.response.operations).map do |job_grpc|
156
156
  Job.from_grpc \
157
- Google::Gax::Operation.new(job_grpc, operations_client),
157
+ Gapic::Operation.new(job_grpc, operations_client),
158
158
  service
159
159
  end)
160
160
  jobs.grpc = grpc
@@ -142,7 +142,7 @@ module Google
142
142
 
143
143
  ##
144
144
  # @private New Database::List from a
145
- # Google::Spanner::Admin::Database::V1::ListDatabasesResponse
145
+ # `Google::Cloud::Spanner::Admin::Database::V1::ListDatabasesResponse`
146
146
  # object.
147
147
  def self.from_grpc grpc, service, instance_id, max = nil
148
148
  databases = List.new(Array(grpc.databases).map do |database|
@@ -52,7 +52,7 @@ module Google
52
52
 
53
53
  ##
54
54
  # @private Creates a new Database::RestoreInfo instance from a
55
- # Google::Spanner::Admin::Database::V1::RestoreInfo.
55
+ # `Google::Cloud::Spanner::Admin::Database::V1::RestoreInfo`
56
56
  def self.from_grpc grpc
57
57
  new grpc
58
58
  end
@@ -372,9 +372,9 @@ module Google
372
372
  ##
373
373
  # @private
374
374
  def to_grpc_type
375
- Google::Spanner::V1::Type.new(
375
+ V1::Type.new(
376
376
  code: :STRUCT,
377
- struct_type: Google::Spanner::V1::StructType.new(
377
+ struct_type: V1::StructType.new(
378
378
  fields: @grpc_fields
379
379
  )
380
380
  )
@@ -382,7 +382,7 @@ module Google
382
382
 
383
383
  ##
384
384
  # @private Creates a new Fields instance from a
385
- # Google::Spanner::V1::Metadata::Row_type::Fields.
385
+ # V1::Metadata::Row_type::Fields.
386
386
  def self.from_grpc fields
387
387
  new([]).tap do |f|
388
388
  f.instance_variable_set :@grpc_fields, Array(fields)
@@ -412,18 +412,18 @@ module Google
412
412
  if pair.is_a?(Array)
413
413
  if pair.count == 2
414
414
  if pair.first.is_a?(Integer)
415
- Google::Spanner::V1::StructType::Field.new(
415
+ V1::StructType::Field.new(
416
416
  type: Convert.grpc_type_for_field(pair.last)
417
417
  )
418
418
  else
419
- Google::Spanner::V1::StructType::Field.new(
419
+ V1::StructType::Field.new(
420
420
  name: String(pair.first),
421
421
  type: Convert.grpc_type_for_field(pair.last)
422
422
  )
423
423
  end
424
424
  else
425
- Google::Spanner::V1::StructType::Field.new(
426
- type: Google::Spanner::V1::Type.new(
425
+ V1::StructType::Field.new(
426
+ type: V1::Type.new(
427
427
  code: :ARRAY,
428
428
  array_element_type: Convert.grpc_type_for_field(pair.last)
429
429
  )
@@ -435,7 +435,7 @@ module Google
435
435
  unless pair.is_a?(Symbol)
436
436
  raise ArgumentError, "type must be a symbol"
437
437
  end
438
- Google::Spanner::V1::StructType::Field.new(
438
+ V1::StructType::Field.new(
439
439
  type: Convert.grpc_type_for_field(pair)
440
440
  )
441
441
  end
@@ -342,10 +342,10 @@ module Google
342
342
  # response.
343
343
  #
344
344
  # The response returns a list of
345
- # {Google::Longrunning::Operation long-running operations} whose names
345
+ # `Google::Longrunning::Operation` long-running operations whose names
346
346
  # are prefixed by a database name within the specified instance.
347
347
  # The long-running operation
348
- # {Google::Longrunning::Operation#metadata metadata} field type
348
+ # `Google::Longrunning::Operation#metadata` metadata field type
349
349
  # `metadata.type_url` describes the type of the metadata.
350
350
  #
351
351
  # The filter expression must specify the field name,
@@ -603,10 +603,10 @@ module Google
603
603
  # response.
604
604
  #
605
605
  # The response returns a list of
606
- # {Google::Longrunning::Operation long-running operations} whose names
606
+ # `Google::Longrunning::Operation` long-running operations whose names
607
607
  # are prefixed by a backup name within the specified instance.
608
608
  # The long-running operation
609
- # {Google::Longrunning::Operation#metadata metadata} field type
609
+ # `Google::Longrunning::Operation#metadata` metadata field type
610
610
  # `metadata.type_url` describes the type of the metadata.
611
611
  #
612
612
  # The filter expression must specify the field name of an operation, a
@@ -856,7 +856,7 @@ module Google
856
856
 
857
857
  ##
858
858
  # @private Creates a new Instance instance from a
859
- # Google::Spanner::Admin::Instance::V1::Instance.
859
+ # `Google::Cloud::Spanner::Admin::Instance::V1::Instance`.
860
860
  def self.from_grpc grpc, service
861
861
  new grpc, service
862
862
  end
@@ -869,13 +869,6 @@ module Google
869
869
  def ensure_service!
870
870
  raise "Must have active connection to service" unless service
871
871
  end
872
-
873
- def instance_config_path name
874
- return name if name.to_s.include? "/"
875
- Admin::Instance::V1::InstanceAdminClient.instance_config_path(
876
- project, name.to_s
877
- )
878
- end
879
872
  end
880
873
  end
881
874
  end
@@ -77,7 +77,7 @@ module Google
77
77
 
78
78
  ##
79
79
  # @private Creates a new Instance::Config instance from a
80
- # Google::Spanner::Admin::Instance::V1::InstanceConfig.
80
+ # `Google::Cloud::Spanner::Admin::Instance::V1::InstanceConfig`.
81
81
  def self.from_grpc grpc
82
82
  new grpc
83
83
  end
@@ -142,7 +142,7 @@ module Google
142
142
 
143
143
  ##
144
144
  # @private New Instance::Config::List from a
145
- # Google::Spanner::Admin::Instance::V1::ListInstanceConfigsResponse
145
+ # `Google::Cloud::Spanner::Admin::Instance::V1::ListInstanceConfigsResponse`
146
146
  # object.
147
147
  def self.from_grpc grpc, service, max = nil
148
148
  configs = List.new(Array(grpc.instance_configs).map do |config|
@@ -54,7 +54,7 @@ module Google
54
54
  #
55
55
  class Job
56
56
  ##
57
- # @private The Google::Gax::Operation gRPC object.
57
+ # @private The `Gapic::Operation` gRPC object.
58
58
  attr_accessor :grpc
59
59
 
60
60
  ##
@@ -219,7 +219,7 @@ module Google
219
219
  end
220
220
 
221
221
  ##
222
- # @private New Instance::Job from a Google::Gax::Operation object.
222
+ # @private New Instance::Job from a `Gapic::Operation` object.
223
223
  def self.from_grpc grpc, service
224
224
  new.tap do |job|
225
225
  job.instance_variable_set :@grpc, grpc
@@ -139,7 +139,7 @@ module Google
139
139
 
140
140
  ##
141
141
  # @private New Instance::List from a
142
- # Google::Spanner::Admin::Instance::V1::ListInstancesResponse
142
+ # `Google::Cloud::Spanner::Admin::Instance::V1::ListInstancesResponse`
143
143
  # object.
144
144
  def self.from_grpc grpc, service, max = nil
145
145
  instances = List.new(Array(grpc.instances).map do |instance|
@@ -172,13 +172,13 @@ module Google
172
172
  new.tap do |p|
173
173
  if data[:execute]
174
174
  execute_sql_grpc = \
175
- Google::Spanner::V1::ExecuteSqlRequest.decode(
175
+ V1::ExecuteSqlRequest.decode(
176
176
  Base64.decode64(data[:execute])
177
177
  )
178
178
  p.instance_variable_set :@execute, execute_sql_grpc
179
179
  end
180
180
  if data[:read]
181
- read_grpc = Google::Spanner::V1::ReadRequest.decode \
181
+ read_grpc = V1::ReadRequest.decode \
182
182
  Base64.decode64(data[:read])
183
183
  p.instance_variable_set :@read, read_grpc
184
184
  end
@@ -194,7 +194,7 @@ module Google
194
194
  end
195
195
 
196
196
  ##
197
- # @private New Partition from a Google::Spanner::V1::ExecuteSqlRequest
197
+ # @private New Partition from a `Google::Cloud::Spanner::V1::ExecuteSqlRequest`
198
198
  # object.
199
199
  def self.from_execute_sql_grpc grpc
200
200
  new.tap do |p|
@@ -203,7 +203,7 @@ module Google
203
203
  end
204
204
 
205
205
  ##
206
- # @private New Partition from a Google::Spanner::V1::ReadRequest object.
206
+ # @private New Partition from a `Google::Cloud::Spanner::V1::ReadRequest` object.
207
207
  def self.from_read_grpc grpc
208
208
  new.tap do |p|
209
209
  p.instance_variable_set :@read, grpc
@@ -159,7 +159,7 @@ module Google
159
159
  end
160
160
 
161
161
  ##
162
- # @private Convert the Policy to a Google::Iam::V1::Policy object.
162
+ # @private Convert the Policy to a `Google::Iam::V1::Policy` object.
163
163
  def to_grpc
164
164
  Google::Iam::V1::Policy.new(
165
165
  etag: etag,
@@ -174,7 +174,7 @@ module Google
174
174
  end
175
175
 
176
176
  ##
177
- # @private New Policy from a Google::Iam::V1::Policy object.
177
+ # @private New Policy from a `Google::Iam::V1::Policy` object.
178
178
  def self.from_grpc grpc
179
179
  roles = grpc.bindings.each_with_object({}) do |binding, memo|
180
180
  memo[binding.role] = binding.members.to_a
@@ -41,6 +41,8 @@ module Google
41
41
  # end
42
42
  #
43
43
  class Results
44
+ RST_STREAM_INTERNAL_ERROR = "Received RST_STREAM".freeze
45
+ EOS_INTERNAL_ERROR = "Received unexpected EOS on DATA frame from server".freeze
44
46
  ##
45
47
  # The read timestamp chosen for single-use snapshots (read-only
46
48
  # transactions).
@@ -107,12 +109,24 @@ module Google
107
109
  buffer_upper_bound = 10
108
110
  chunked_value = nil
109
111
  resume_token = nil
112
+ should_resume_request = false
113
+ should_retry_request = false
110
114
 
111
115
  # Cannot call Enumerator#each because it won't return the first
112
116
  # value that was already identified when calling Enumerator#peek.
113
117
  # Iterate only using Enumerator#next and break on StopIteration.
114
118
  loop do
115
119
  begin
120
+ if should_resume_request
121
+ @enum = resume_request(resume_token)
122
+ buffered_responses = []
123
+ should_resume_request = false
124
+ elsif should_retry_request
125
+ @enum = retry_request()
126
+ buffered_responses = []
127
+ should_retry_request = false
128
+ end
129
+
116
130
  grpc = @enum.next
117
131
  # metadata should be set before the first iteration...
118
132
  @metadata ||= grpc.metadata
@@ -143,28 +157,36 @@ module Google
143
157
  # Flush the buffered responses now that they are all handled
144
158
  buffered_responses = []
145
159
  end
146
- rescue GRPC::Cancelled, GRPC::DeadlineExceeded, GRPC::Internal,
147
- GRPC::ResourceExhausted, GRPC::Unauthenticated,
148
- GRPC::Unavailable, GRPC::Core::CallError => err
149
- if resume_token.nil? || resume_token.empty?
150
- # Re-raise if the resume_token is not a valid value.
151
- # This can happen if the buffer was flushed.
152
- raise Google::Cloud::Error.from_error(err)
153
- end
160
+ # TODO: once the generated client throws only Google Cloud errors, remove
161
+ # the GRPC errors from the rescue block
162
+ rescue GRPC::Aborted,
163
+ GRPC::Cancelled,
164
+ GRPC::DeadlineExceeded,
165
+ GRPC::Internal,
166
+ GRPC::ResourceExhausted,
167
+ GRPC::Unauthenticated,
168
+ GRPC::Unavailable,
169
+ GRPC::Core::CallError,
170
+ Google::Cloud::AbortedError,
171
+ Google::Cloud::CanceledError,
172
+ Google::Cloud::DeadlineExceededError,
173
+ Google::Cloud::InternalError,
174
+ Google::Cloud::ResourceExhaustedError,
175
+ Google::Cloud::UnauthenticatedError,
176
+ Google::Cloud::UnavailableError => err
154
177
 
155
- # Resume the stream from the last known resume_token
156
- if @execute_query_options
157
- @enum = @service.execute_streaming_sql \
158
- @session_path, @sql,
159
- @execute_query_options.merge(resume_token: resume_token)
178
+ if resumable?(resume_token)
179
+ should_resume_request = true
180
+ elsif retryable?(err)
181
+ should_retry_request = true
182
+ elsif err.is_a?(Google::Cloud::Error)
183
+ raise err
160
184
  else
161
- @enum = @service.streaming_read_table \
162
- @session_path, @table, @columns,
163
- @read_options.merge(resume_token: resume_token)
185
+ raise Google::Cloud::Error.from_error(err)
164
186
  end
165
187
 
166
- # Flush the buffered responses to reset to the resume_token
167
- buffered_responses = []
188
+ # TODO: once the generated client throws only Google Cloud errors, remove
189
+ # this rescue block (for GRPC::BadStatus)
168
190
  rescue GRPC::BadStatus => err
169
191
  raise Google::Cloud::Error.from_error(err)
170
192
  rescue StopIteration
@@ -198,6 +220,60 @@ module Google
198
220
 
199
221
  # rubocop:enable all
200
222
 
223
+ ##
224
+ # @private
225
+ # Checks if a request can be resumed by inspecting the resume token
226
+ def resumable? resume_token
227
+ resume_token && !resume_token.empty?
228
+ end
229
+
230
+ ##
231
+ # @private
232
+ # Checks if a request can be retried. This is based on the error returned.
233
+ # Retryable errors are:
234
+ # - Unavailable error
235
+ # - Internal EOS error
236
+ # - Internal RST_STREAM error
237
+ def retryable? err
238
+ err.instance_of?(Google::Cloud::UnavailableError) ||
239
+ err.instance_of?(GRPC::Unavailable) ||
240
+ (err.instance_of?(Google::Cloud::InternalError) && err.message.include?(EOS_INTERNAL_ERROR)) ||
241
+ (err.instance_of?(GRPC::Internal) && err.details.include?(EOS_INTERNAL_ERROR)) ||
242
+ (err.instance_of?(Google::Cloud::InternalError) && err.message.include?(RST_STREAM_INTERNAL_ERROR)) ||
243
+ (err.instance_of?(GRPC::Internal) && err.details.include?(RST_STREAM_INTERNAL_ERROR))
244
+ end
245
+
246
+ ##
247
+ # @private
248
+ # Resumes a request, by re-executing it with a resume token.
249
+ def resume_request resume_token
250
+ if @execute_query_options
251
+ @service.execute_streaming_sql(
252
+ @session_path,
253
+ @sql,
254
+ @execute_query_options.merge(resume_token: resume_token)
255
+ )
256
+ else
257
+ @service.streaming_read_table(
258
+ @session_path,
259
+ @table,
260
+ @columns,
261
+ @read_options.merge(resume_token: resume_token)
262
+ )
263
+ end
264
+ end
265
+
266
+ ##
267
+ # @private
268
+ # Retries a request, by re-executing it from scratch.
269
+ def retry_request
270
+ if @execute_query_options
271
+ @service.execute_streaming_sql @session_path, @sql, @execute_query_options
272
+ else
273
+ @service.streaming_read_table @session_path, @table, @columns, @read_options
274
+ end
275
+ end
276
+
201
277
  ##
202
278
  # @private
203
279
  # Get row count from stats. This will be the exact row count for DML
@@ -243,11 +319,11 @@ module Google
243
319
  def self.execute_query service, session_path, sql, params: nil,
244
320
  types: nil, transaction: nil,
245
321
  partition_token: nil, seqno: nil,
246
- query_options: nil
322
+ query_options: nil, call_options: nil
247
323
  execute_query_options = {
248
324
  transaction: transaction, params: params, types: types,
249
325
  partition_token: partition_token, seqno: seqno,
250
- query_options: query_options
326
+ query_options: query_options, call_options: call_options
251
327
  }
252
328
  enum = service.execute_streaming_sql session_path, sql,
253
329
  execute_query_options
@@ -262,10 +338,13 @@ module Google
262
338
  # @private
263
339
  def self.read service, session_path, table, columns, keys: nil,
264
340
  index: nil, limit: nil, transaction: nil,
265
- partition_token: nil
266
- read_options = { keys: keys, index: index, limit: limit,
267
- transaction: transaction,
268
- partition_token: partition_token }
341
+ partition_token: nil, call_options: nil
342
+ read_options = {
343
+ keys: keys, index: index, limit: limit,
344
+ transaction: transaction,
345
+ partition_token: partition_token,
346
+ call_options: call_options
347
+ }
269
348
  enum = service.streaming_read_table \
270
349
  session_path, table, columns, read_options
271
350
  from_enum(enum, service).tap do |results|