activerecord-xa 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +24 -0
- data/COPYRIGHT +5 -0
- data/LICENSE +9 -0
- data/README.md +262 -0
- data/docs/ARCHITECTURE.md +89 -0
- data/docs/INSTALL.md +103 -0
- data/docs/USAGE.md +103 -0
- data/lib/active_record/xa/adapter_extension.rb +75 -0
- data/lib/active_record/xa/branch.rb +171 -0
- data/lib/active_record/xa/configuration.rb +58 -0
- data/lib/active_record/xa/coordinator.rb +374 -0
- data/lib/active_record/xa/decision_log.rb +172 -0
- data/lib/active_record/xa/errors.rb +36 -0
- data/lib/active_record/xa/railtie.rb +22 -0
- data/lib/active_record/xa/recovery.rb +178 -0
- data/lib/active_record/xa/transaction_extension.rb +103 -0
- data/lib/active_record/xa/version.rb +9 -0
- data/lib/active_record/xa/xid.rb +104 -0
- data/lib/active_record/xa.rb +92 -0
- data/lib/activerecord-xa.rb +5 -0
- data/lib/generators/xa/install/install_generator.rb +41 -0
- data/lib/generators/xa/install/templates/initializer.rb.tt +21 -0
- data/lib/generators/xa/install/templates/migration.rb.tt +11 -0
- metadata +93 -0
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_record"
|
|
4
|
+
|
|
5
|
+
require_relative "xa/version"
|
|
6
|
+
require_relative "xa/errors"
|
|
7
|
+
require_relative "xa/configuration"
|
|
8
|
+
require_relative "xa/xid"
|
|
9
|
+
require_relative "xa/branch"
|
|
10
|
+
require_relative "xa/adapter_extension"
|
|
11
|
+
require_relative "xa/transaction_extension"
|
|
12
|
+
require_relative "xa/decision_log"
|
|
13
|
+
require_relative "xa/coordinator"
|
|
14
|
+
require_relative "xa/recovery"
|
|
15
|
+
require_relative "xa/railtie" if defined?(Rails::Railtie)
|
|
16
|
+
|
|
17
|
+
# Atomic transactions across multiple MySQL databases for Active Record, using MySQL XA two-phase commit.
|
|
18
|
+
#
|
|
19
|
+
# ActiveRecord::XA.transaction(PrimaryRecord, TrackerRecord) do
|
|
20
|
+
# persist_tracker_data!
|
|
21
|
+
# persist_application_data!
|
|
22
|
+
# end
|
|
23
|
+
#
|
|
24
|
+
# Either every database commits or none does.
|
|
25
|
+
# If the process dies between PREPARE and COMMIT, ActiveRecord::XA.recover! finishes the job from the decision log.
|
|
26
|
+
#
|
|
27
|
+
module ActiveRecord::XA
|
|
28
|
+
|
|
29
|
+
class << self
|
|
30
|
+
|
|
31
|
+
# The current configuration.
|
|
32
|
+
#
|
|
33
|
+
# @return [Configuration] the configuration
|
|
34
|
+
def config
|
|
35
|
+
@config ||= Configuration.new
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Changes the configuration.
|
|
39
|
+
#
|
|
40
|
+
# @yieldparam config [Configuration] the configuration
|
|
41
|
+
# @return [void]
|
|
42
|
+
def configure
|
|
43
|
+
yield config
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Runs the block as one transaction across every database the classes connect to.
|
|
47
|
+
# Inside the block, use models exactly as normal; only databases the block touches join.
|
|
48
|
+
#
|
|
49
|
+
# @param record_classes [Array<Class>] Active Record classes covering at least two databases
|
|
50
|
+
# @yield the work to do
|
|
51
|
+
# @raise [PrepareFailed, DecisionLogFailed, OutcomeUnknown, TransactionAlreadyOpen] see the README
|
|
52
|
+
# @return [Object] the block's value, or nil when it raised ActiveRecord::Rollback
|
|
53
|
+
def transaction(*record_classes, &block)
|
|
54
|
+
raise ArgumentError, "ActiveRecord::XA.transaction requires a block" unless block
|
|
55
|
+
|
|
56
|
+
Coordinator.new(record_classes, log: decision_log).run(&block)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Resolves prepared but unfinished branches left by a crash or a failed commit.
|
|
60
|
+
# Pass every class that takes part in ActiveRecord::XA transactions, or set config.participants.
|
|
61
|
+
#
|
|
62
|
+
# @param record_classes [Array<Class>] classes covering every participating database
|
|
63
|
+
# @raise [ConfigurationError] if no classes are given or configured
|
|
64
|
+
# @return [Recovery::Report] what the run did
|
|
65
|
+
def recover!(*record_classes)
|
|
66
|
+
record_classes = config.resolved_participants if record_classes.empty?
|
|
67
|
+
raise ConfigurationError, "pass the participating classes or set ActiveRecord::XA.config.participants" if record_classes.empty?
|
|
68
|
+
|
|
69
|
+
Recovery.new(record_classes, log: decision_log, grace: config.recovery_grace_period,
|
|
70
|
+
abort_retention: config.abort_retention).run
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# The decision log, on the configured class's database.
|
|
74
|
+
#
|
|
75
|
+
# @raise [ConfigurationError] if no decision log class is set
|
|
76
|
+
# @return [DecisionLog] the log
|
|
77
|
+
def decision_log
|
|
78
|
+
DecisionLog.new(config.resolved_decision_log_class.connection_pool, namespace: config.namespace)
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Hook into Active Record as it loads, so requiring the gem never loads it early.
|
|
84
|
+
#
|
|
85
|
+
ActiveSupport.on_load(:active_record) do
|
|
86
|
+
require "active_record/connection_adapters/abstract/transaction"
|
|
87
|
+
|
|
88
|
+
ActiveRecord::ConnectionAdapters::Transaction.prepend(ActiveRecord::XA::TransactionExtension)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
ActiveSupport.on_load(:active_record_mysql2adapter) { prepend ActiveRecord::XA::AdapterExtension }
|
|
92
|
+
ActiveSupport.on_load(:active_record_trilogyadapter) { prepend ActiveRecord::XA::AdapterExtension }
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rails/generators"
|
|
4
|
+
require "rails/generators/active_record"
|
|
5
|
+
require "active_record/xa"
|
|
6
|
+
|
|
7
|
+
module ActiveRecord::XA
|
|
8
|
+
module Generators
|
|
9
|
+
|
|
10
|
+
# Sets ActiveRecord::XA up in a Rails application: an initializer and the decision log migration.
|
|
11
|
+
#
|
|
12
|
+
# bin/rails generate xa:install --database primary
|
|
13
|
+
#
|
|
14
|
+
class InstallGenerator < Rails::Generators::Base
|
|
15
|
+
include ActiveRecord::Generators::Migration
|
|
16
|
+
|
|
17
|
+
# Rails would derive "active_record:x_a:install" from the class name.
|
|
18
|
+
#
|
|
19
|
+
namespace "xa:install"
|
|
20
|
+
|
|
21
|
+
source_root File.expand_path("templates", __dir__)
|
|
22
|
+
|
|
23
|
+
class_option :database, type: :string, aliases: %i[--db],
|
|
24
|
+
desc: "The database the decision log lives in; its migrations path gets the migration"
|
|
25
|
+
|
|
26
|
+
# Writes config/initializers/xa.rb.
|
|
27
|
+
#
|
|
28
|
+
# @return [void]
|
|
29
|
+
def create_initializer
|
|
30
|
+
template "initializer.rb.tt", "config/initializers/xa.rb"
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Writes the migration that creates the decision log table.
|
|
34
|
+
#
|
|
35
|
+
# @return [void]
|
|
36
|
+
def create_migration_file
|
|
37
|
+
migration_template "migration.rb.tt", File.join(db_migrate_path, "create_xa_decisions.rb")
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# ActiveRecord::XA: atomic transactions across MySQL databases.
|
|
4
|
+
#
|
|
5
|
+
ActiveRecord::XA.configure do |config|
|
|
6
|
+
# The abstract class whose database holds the decision log table.
|
|
7
|
+
config.decision_log_class = "ApplicationRecord"
|
|
8
|
+
|
|
9
|
+
# Every abstract class whose database takes part in ActiveRecord::XA transactions.
|
|
10
|
+
# `bin/rails xa:recover` and `ActiveRecord::XA.recover!` use these.
|
|
11
|
+
config.participants = %w[ApplicationRecord]
|
|
12
|
+
|
|
13
|
+
# Seconds a transaction must have been running before recovery touches it.
|
|
14
|
+
# config.recovery_grace_period = 60
|
|
15
|
+
|
|
16
|
+
# Called when a committed transaction could not apply XA COMMIT everywhere;
|
|
17
|
+
# recovery finishes it. A good place to page someone.
|
|
18
|
+
# config.on_incomplete_commit = ->(gtrid, failures) { Rails.error.report(ActiveRecord::XA::Error.new("XA in doubt: #{ gtrid }")) }
|
|
19
|
+
|
|
20
|
+
config.logger = Rails.logger
|
|
21
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class CreateXaDecisions < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
|
|
4
|
+
def up
|
|
5
|
+
execute ActiveRecord::XA::DecisionLog.create_table_sql
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def down
|
|
9
|
+
drop_table ActiveRecord::XA::DecisionLog::TABLE
|
|
10
|
+
end
|
|
11
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: activerecord-xa
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Vlad Radulescu
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: activerecord
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '8.1'
|
|
19
|
+
- - "<"
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '8.2'
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
requirements:
|
|
26
|
+
- - ">="
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
version: '8.1'
|
|
29
|
+
- - "<"
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: '8.2'
|
|
32
|
+
description: |
|
|
33
|
+
Nested transaction blocks on different databases look atomic but are not:
|
|
34
|
+
the inner one commits when its block exits. ActiveRecord::XA runs them as one
|
|
35
|
+
transaction with MySQL's XA two-phase commit, so every database commits or
|
|
36
|
+
none does. A durable decision log and a recovery task finish whatever a
|
|
37
|
+
crash leaves half done. Models are used exactly as normal inside the block.
|
|
38
|
+
email:
|
|
39
|
+
- vlad@studio51.solutions
|
|
40
|
+
executables: []
|
|
41
|
+
extensions: []
|
|
42
|
+
extra_rdoc_files: []
|
|
43
|
+
files:
|
|
44
|
+
- CHANGELOG.md
|
|
45
|
+
- COPYRIGHT
|
|
46
|
+
- LICENSE
|
|
47
|
+
- README.md
|
|
48
|
+
- docs/ARCHITECTURE.md
|
|
49
|
+
- docs/INSTALL.md
|
|
50
|
+
- docs/USAGE.md
|
|
51
|
+
- lib/active_record/xa.rb
|
|
52
|
+
- lib/active_record/xa/adapter_extension.rb
|
|
53
|
+
- lib/active_record/xa/branch.rb
|
|
54
|
+
- lib/active_record/xa/configuration.rb
|
|
55
|
+
- lib/active_record/xa/coordinator.rb
|
|
56
|
+
- lib/active_record/xa/decision_log.rb
|
|
57
|
+
- lib/active_record/xa/errors.rb
|
|
58
|
+
- lib/active_record/xa/railtie.rb
|
|
59
|
+
- lib/active_record/xa/recovery.rb
|
|
60
|
+
- lib/active_record/xa/transaction_extension.rb
|
|
61
|
+
- lib/active_record/xa/version.rb
|
|
62
|
+
- lib/active_record/xa/xid.rb
|
|
63
|
+
- lib/activerecord-xa.rb
|
|
64
|
+
- lib/generators/xa/install/install_generator.rb
|
|
65
|
+
- lib/generators/xa/install/templates/initializer.rb.tt
|
|
66
|
+
- lib/generators/xa/install/templates/migration.rb.tt
|
|
67
|
+
homepage: https://github.com/studio51/activerecord-xa
|
|
68
|
+
licenses:
|
|
69
|
+
- MIT
|
|
70
|
+
metadata:
|
|
71
|
+
source_code_uri: https://github.com/studio51/activerecord-xa
|
|
72
|
+
changelog_uri: https://github.com/studio51/activerecord-xa/blob/main/CHANGELOG.md
|
|
73
|
+
bug_tracker_uri: https://github.com/studio51/activerecord-xa/issues
|
|
74
|
+
documentation_uri: https://github.com/studio51/activerecord-xa/tree/main/docs
|
|
75
|
+
rubygems_mfa_required: 'true'
|
|
76
|
+
rdoc_options: []
|
|
77
|
+
require_paths:
|
|
78
|
+
- lib
|
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
80
|
+
requirements:
|
|
81
|
+
- - ">="
|
|
82
|
+
- !ruby/object:Gem::Version
|
|
83
|
+
version: '4.0'
|
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
85
|
+
requirements:
|
|
86
|
+
- - ">="
|
|
87
|
+
- !ruby/object:Gem::Version
|
|
88
|
+
version: '0'
|
|
89
|
+
requirements: []
|
|
90
|
+
rubygems_version: 4.0.20
|
|
91
|
+
specification_version: 4
|
|
92
|
+
summary: Atomic transactions across multiple MySQL databases for Active Record.
|
|
93
|
+
test_files: []
|