Class: ActiveRecordSpannerAdapter::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord_spanner_adapter/connection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Connection

Returns a new instance of Connection.



19
20
21
22
23
24
# File 'lib/activerecord_spanner_adapter/connection.rb', line 19

def initialize config
  @instance_id = config[:instance]
  @database_id = config[:database]
  @isolation_level = config[:isolation_level]
  @spanner = self.class.spanners config
end

Instance Attribute Details

#current_transactionObject

Returns the value of attribute current_transaction.



16
17
18
# File 'lib/activerecord_spanner_adapter/connection.rb', line 16

def current_transaction
  @current_transaction
end

#database_idObject (readonly)

Returns the value of attribute database_id.



14
15
16
# File 'lib/activerecord_spanner_adapter/connection.rb', line 14

def database_id
  @database_id
end

#instance_idObject (readonly)

Returns the value of attribute instance_id.



13
14
15
# File 'lib/activerecord_spanner_adapter/connection.rb', line 13

def instance_id
  @instance_id
end

#isolation_levelObject

Returns the value of attribute isolation_level.



17
18
19
# File 'lib/activerecord_spanner_adapter/connection.rb', line 17

def isolation_level
  @isolation_level
end

#spannerObject (readonly)

Returns the value of attribute spanner.



15
16
17
# File 'lib/activerecord_spanner_adapter/connection.rb', line 15

def spanner
  @spanner
end

Class Method Details

.database_path(config) ⇒ Object



302
303
304
# File 'lib/activerecord_spanner_adapter/connection.rb', line 302

def self.database_path config
  "#{config[:emulator_host]}/#{config[:project]}/#{config[:instance]}/#{config[:database]}"
end

.information_schema(config) ⇒ Object



53
54
55
56
57
# File 'lib/activerecord_spanner_adapter/connection.rb', line 53

def self.information_schema config
  @information_schemas ||= {}
  @information_schemas[database_path(config)] ||=
    ActiveRecordSpannerAdapter::InformationSchema.new new(config)
end

.reset_information_schemas!Object

Clears the cached information about the underlying information schemas. Call this method if you drop and recreate a database with the same name to prevent the cached information to be used for the new database.



46
47
48
49
50
51
# File 'lib/activerecord_spanner_adapter/connection.rb', line 46

def self.reset_information_schemas!
  @information_schemas.each_value do |info_schema|
    info_schema.connection.disconnect!
  end
  @information_schemas = {}
end

.spanners(config) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/activerecord_spanner_adapter/connection.rb', line 26

def self.spanners config
  config = config.symbolize_keys
  @spanners ||= {}
  @mutex ||= Mutex.new
  @mutex.synchronize do
    @spanners[database_path(config)] ||= Google::Cloud::Spanner.new(
      project_id: config[:project],
      credentials: config[:credentials],
      emulator_host: config[:emulator_host],
      scope: config[:scope],
      timeout: config[:timeout],
      lib_name: "spanner-activerecord-adapter",
      lib_version: ActiveRecordSpannerAdapter::VERSION
    )
  end
end

Instance Method Details

#abort_batchObject

Aborts the current batch on this connection. This is a no-op if there is no batch on this connection.

See Also:



186
187
188
# File 'lib/activerecord_spanner_adapter/connection.rb', line 186

def abort_batch
  @ddl_batch = nil
end

#active?Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/activerecord_spanner_adapter/connection.rb', line 65

def active?
  # This method should not initialize a session.
  unless @session
    return false
  end
  # Assume that it is still active if it has been used in the past 50 minutes.
  if ((Time.current - @last_used) / 60).round < 50
    return true
  end
  session.execute_query "SELECT 1"
  true
rescue StandardError
  false
end

#begin_transaction(isolation = nil) ⇒ Object

Transactions



277
278
279
280
281
282
# File 'lib/activerecord_spanner_adapter/connection.rb', line 277

def begin_transaction isolation = nil
  raise "Nested transactions are not allowed" if current_transaction&.active?
  self.current_transaction = Transaction.new self, isolation || @isolation_level
  current_transaction.begin
  current_transaction
end

#commit_transactionObject



284
285
286
287
# File 'lib/activerecord_spanner_adapter/connection.rb', line 284

def commit_transaction
  raise "This connection does not have a transaction" unless current_transaction
  current_transaction.commit
end

#create_databaseObject

Database Operations



95
96
97
98
99
100
# File 'lib/activerecord_spanner_adapter/connection.rb', line 95

def create_database
  job = spanner.create_database instance_id, database_id
  job.wait_until_done!
  raise Google::Cloud::Error.from_error job.error if job.error?
  job.database
end

#create_transaction_after_failed_first_statement(original_error) ⇒ Object

Creates a transaction using a BeginTransaction RPC. This is used if the first statement of a transaction fails, as that also means that no transaction id was returned.



267
268
269
270
271
272
273
# File 'lib/activerecord_spanner_adapter/connection.rb', line 267

def create_transaction_after_failed_first_statement original_error
  transaction = current_transaction.force_begin_read_write
  Google::Cloud::Spanner::V1::TransactionSelector.new id: transaction.transaction_id
rescue Google::Cloud::Error
  # Raise the original error if the BeginTransaction RPC also fails.
  raise original_error
end

#databaseObject



102
103
104
105
106
107
108
109
110
# File 'lib/activerecord_spanner_adapter/connection.rb', line 102

def database
  @database ||= begin
    database = spanner.database instance_id, database_id
    unless database
      raise ActiveRecord::NoDatabaseError, "#{spanner.project}/#{instance_id}/#{database_id}"
    end
    database
  end
end

#ddl_batchObject

Executes a set of DDL statements as one batch. This method raises an error if no block is given.

Examples:

connection.ddl_batch do
  connection.execute_ddl "CREATE TABLE `Users` (Id INT64, Name STRING(MAX)) PRIMARY KEY (Id)"
  connection.execute_ddl "CREATE INDEX Idx_Users_Name ON `Users` (Name)"
end

Raises:

  • (Google::Cloud::FailedPreconditionError)


141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/activerecord_spanner_adapter/connection.rb', line 141

def ddl_batch
  raise Google::Cloud::FailedPreconditionError, "No block given for the DDL batch" unless block_given?
  begin
    start_batch_ddl
    yield
    run_batch
  rescue StandardError
    abort_batch
    raise
  ensure
    @ddl_batch = nil
  end
end

#ddl_batch?Boolean

Returns true if this connection is currently executing a DDL batch, and otherwise false.

Returns:

  • (Boolean)


157
158
159
160
# File 'lib/activerecord_spanner_adapter/connection.rb', line 157

def ddl_batch?
  return true if @ddl_batch
  false
end

#disconnect!Object



80
81
82
83
84
85
# File 'lib/activerecord_spanner_adapter/connection.rb', line 80

def disconnect!
  session.release!
  true
ensure
  @session = nil
end

#execute_ddl(statements, operation_id: nil, wait_until_done: true) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/activerecord_spanner_adapter/connection.rb', line 115

def execute_ddl statements, operation_id: nil, wait_until_done: true
  raise "DDL cannot be executed during a transaction" if current_transaction&.active?
  self.current_transaction = nil

  statements = Array statements
  return unless statements.any?

  # If a DDL batch is active we only buffer the statements on the connection until the batch is run.
  if @ddl_batch
    @ddl_batch.push(*statements)
    return true
  end

  execute_ddl_statements statements, operation_id, wait_until_done
end

#execute_query(sql, params: nil, types: nil, single_use_selector: nil, request_options: nil) ⇒ Object

DQL, DML Statements



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/activerecord_spanner_adapter/connection.rb', line 210

def execute_query sql, params: nil, types: nil, single_use_selector: nil, request_options: nil
  if params
    converted_params, types =
      Google::Cloud::Spanner::Convert.to_input_params_and_types(
        params, types
      )
  end

  # Clear the transaction from the previous statement.
  unless current_transaction&.active?
    self.current_transaction = nil
  end

  selector = transaction_selector || single_use_selector
  execute_sql_request sql, converted_params, types, selector, request_options
end

#execute_sql_request(sql, converted_params, types, selector, request_options = nil) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/activerecord_spanner_adapter/connection.rb', line 228

def execute_sql_request sql, converted_params, types, selector, request_options = nil
  res = session.execute_query \
    sql,
    params: converted_params,
    types: types,
    transaction: selector,
    request_options: request_options,
    seqno: current_transaction&.next_sequence_number
  current_transaction.grpc_transaction = res..transaction \
      if current_transaction && res&.&.transaction
  res
rescue Google::Cloud::AbortedError
  # Mark the current transaction as aborted to prevent any unnecessary further requests on the transaction.
  current_transaction&.mark_aborted
  raise
rescue Google::Cloud::NotFoundError => e
  if session_not_found?(e) || transaction_not_found?(e)
    reset!
    # Force a retry of the entire transaction if this statement was executed as part of a transaction.
    # Otherwise, just retry the statement itself.
    raise_aborted_err if current_transaction&.active?
    retry
  end
  raise
rescue Google::Cloud::Error => e
  # Check if it was the first statement in a transaction that included a BeginTransaction
  # option in the request. If so, execute an explicit BeginTransaction and then retry the
  # request without the BeginTransaction option.
  if current_transaction && selector&.begin&.read_write
    selector = create_transaction_after_failed_first_statement e
    retry
  end
  # It was not the first statement, so propagate the error.
  raise
end

#raise_aborted_errObject



324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/activerecord_spanner_adapter/connection.rb', line 324

def raise_aborted_err
  retry_info = Google::Rpc::RetryInfo.new retry_delay: Google::Protobuf::Duration.new(seconds: 0, nanos: 1)
  begin
    raise GRPC::BadStatus.new(
      GRPC::Core::StatusCodes::ABORTED,
      "Transaction aborted",
      "google.rpc.retryinfo-bin": Google::Rpc::RetryInfo.encode(retry_info)
    )
  rescue GRPC::BadStatus
    raise Google::Cloud::AbortedError
  end
end

#reset!Object



87
88
89
90
91
# File 'lib/activerecord_spanner_adapter/connection.rb', line 87

def reset!
  disconnect!
  session
  true
end

#rollback_transactionObject



289
290
291
292
# File 'lib/activerecord_spanner_adapter/connection.rb', line 289

def rollback_transaction
  raise "This connection does not have a transaction" unless current_transaction
  current_transaction.rollback
end

#run_batchObject

Runs the current batch on this connection. This will raise a FailedPreconditionError if there is no active batch on this connection.

See Also:



195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/activerecord_spanner_adapter/connection.rb', line 195

def run_batch
  unless @ddl_batch
    raise Google::Cloud::FailedPreconditionError, "There is no batch active on this connection"
  end
  # Just return if the batch is empty.
  return true if @ddl_batch.empty?
  begin
    execute_ddl_statements @ddl_batch, nil, true
  ensure
    @ddl_batch = nil
  end
end

#sessionObject Also known as: connect!



59
60
61
62
# File 'lib/activerecord_spanner_adapter/connection.rb', line 59

def session
  @last_used = Time.current
  @session ||= spanner.create_session instance_id, database_id
end

#session_not_found?(err) ⇒ Boolean

Returns:

  • (Boolean)


306
307
308
309
310
311
312
313
# File 'lib/activerecord_spanner_adapter/connection.rb', line 306

def session_not_found? err
  if err.respond_to?(:metadata) && err.["google.rpc.resourceinfo-bin"]
    resource_info = Google::Rpc::ResourceInfo.decode err.["google.rpc.resourceinfo-bin"]
    type = resource_info["resource_type"]
    return "type.googleapis.com/google.spanner.v1.Session".eql? type
  end
  false
end

#start_batch_ddlObject

Starts a manual DDL batch. The batch must be ended by calling either run_batch or abort_batch.

Examples:

begin
  connection.start_batch_ddl
  connection.execute_ddl "CREATE TABLE `Users` (Id INT64, Name STRING(MAX)) PRIMARY KEY (Id)"
  connection.execute_ddl "CREATE INDEX Idx_Users_Name ON `Users` (Name)"
  connection.run_batch
rescue StandardError
  connection.abort_batch
  raise
end


175
176
177
178
179
180
# File 'lib/activerecord_spanner_adapter/connection.rb', line 175

def start_batch_ddl
  if @ddl_batch
    raise Google::Cloud::FailedPreconditionError, "A DDL batch is already active on this connection"
  end
  @ddl_batch = []
end

#transaction_not_found?(err) ⇒ Boolean

Returns:

  • (Boolean)


315
316
317
318
319
320
321
322
# File 'lib/activerecord_spanner_adapter/connection.rb', line 315

def transaction_not_found? err
  if err.respond_to?(:metadata) && err.["google.rpc.resourceinfo-bin"]
    resource_info = Google::Rpc::ResourceInfo.decode err.["google.rpc.resourceinfo-bin"]
    type = resource_info["resource_type"]
    return "type.googleapis.com/google.spanner.v1.Transaction".eql? type
  end
  false
end

#transaction_selectorObject



294
295
296
# File 'lib/activerecord_spanner_adapter/connection.rb', line 294

def transaction_selector
  current_transaction&.transaction_selector if current_transaction&.active?
end

#truncate(table_name) ⇒ Object



298
299
300
# File 'lib/activerecord_spanner_adapter/connection.rb', line 298

def truncate table_name
  session.delete table_name
end