janus-ar 0.15.2 → 0.15.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +59 -59
- data/Gemfile.lock +122 -128
- data/README.md +110 -110
- data/Rakefile +3 -1
- data/janus-ar.gemspec +36 -36
- data/lib/active_record/connection_adapters/janus_mysql2_adapter.rb +144 -144
- data/lib/active_record/connection_adapters/janus_trilogy_adapter.rb +144 -144
- data/lib/janus/client.rb +6 -6
- data/lib/janus/context.rb +80 -80
- data/lib/janus/db_console_config.rb +17 -17
- data/lib/janus/logging/logger.rb +15 -15
- data/lib/janus/logging/subscriber.rb +27 -27
- data/lib/janus/query_director.rb +54 -54
- data/lib/janus/version.rb +17 -17
- data/lib/janus.rb +22 -22
- data/spec/lib/active_record/connection_adapters/janus_mysql_adapter_spec.rb +82 -76
- data/spec/lib/active_record/connection_adapters/janus_trilogy_adapter_spec.rb +80 -80
- data/spec/lib/janus/client_spec.rb +7 -7
- data/spec/lib/janus/context_spec.rb +46 -46
- data/spec/lib/janus/logging/logger_spec.rb +31 -31
- data/spec/lib/janus/query_director_spec.rb +59 -59
- data/spec/spec_helper.rb +43 -43
- metadata +20 -8
data/janus-ar.gemspec
CHANGED
@@ -1,36 +1,36 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require File.expand_path('lib/janus/version.rb', __dir__)
|
4
|
-
|
5
|
-
Gem::Specification.new do |gem|
|
6
|
-
gem.authors = ['Lloyd Watkin']
|
7
|
-
gem.email = ['lloyd@olioex.com']
|
8
|
-
gem.description = 'Read/Write proxy for ActiveRecord using primary/replica databases'
|
9
|
-
gem.summary = 'Read/Write proxy for ActiveRecord using primary/replica databases'
|
10
|
-
gem.homepage = 'https://github.com/olioex/janus-ar'
|
11
|
-
gem.licenses = %w(MIT)
|
12
|
-
gem.metadata = {
|
13
|
-
'source_code_uri' => 'https://github.com/olioex/janus-ar',
|
14
|
-
}
|
15
|
-
|
16
|
-
gem.files = `git ls-files`.split($OUTPUT_RECORD_SEPARATOR)
|
17
|
-
gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
|
18
|
-
gem.name = 'janus-ar'
|
19
|
-
gem.require_paths = %w(lib)
|
20
|
-
gem.version = Janus::VERSION
|
21
|
-
|
22
|
-
gem.required_ruby_version = '>= 3.2.0'
|
23
|
-
|
24
|
-
gem.
|
25
|
-
gem.add_development_dependency 'activesupport', '>= 7.1.0'
|
26
|
-
gem.add_development_dependency 'mysql2'
|
27
|
-
gem.add_development_dependency 'trilogy'
|
28
|
-
gem.add_development_dependency 'pry'
|
29
|
-
gem.add_development_dependency 'rake'
|
30
|
-
gem.add_development_dependency 'rspec', '~> 3'
|
31
|
-
gem.add_development_dependency 'rubocop', '~> 1.
|
32
|
-
gem.add_development_dependency 'rubocop-rails', '~> 2.
|
33
|
-
gem.add_development_dependency 'rubocop-rspec'
|
34
|
-
gem.add_development_dependency 'rubocop-thread_safety'
|
35
|
-
gem.add_development_dependency 'rubocop-performance'
|
36
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require File.expand_path('lib/janus/version.rb', __dir__)
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.authors = ['Lloyd Watkin']
|
7
|
+
gem.email = ['lloyd@olioex.com']
|
8
|
+
gem.description = 'Read/Write proxy for ActiveRecord using primary/replica databases'
|
9
|
+
gem.summary = 'Read/Write proxy for ActiveRecord using primary/replica databases'
|
10
|
+
gem.homepage = 'https://github.com/olioex/janus-ar'
|
11
|
+
gem.licenses = %w(MIT)
|
12
|
+
gem.metadata = {
|
13
|
+
'source_code_uri' => 'https://github.com/olioex/janus-ar',
|
14
|
+
}
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($OUTPUT_RECORD_SEPARATOR)
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
|
18
|
+
gem.name = 'janus-ar'
|
19
|
+
gem.require_paths = %w(lib)
|
20
|
+
gem.version = Janus::VERSION
|
21
|
+
|
22
|
+
gem.required_ruby_version = '>= 3.2.0'
|
23
|
+
|
24
|
+
gem.add_dependency 'activerecord', '>= 7.1.0', '< 7.2'
|
25
|
+
gem.add_development_dependency 'activesupport', '>= 7.1.0', '< 7.2'
|
26
|
+
gem.add_development_dependency 'mysql2'
|
27
|
+
gem.add_development_dependency 'trilogy'
|
28
|
+
gem.add_development_dependency 'pry'
|
29
|
+
gem.add_development_dependency 'rake'
|
30
|
+
gem.add_development_dependency 'rspec', '~> 3'
|
31
|
+
gem.add_development_dependency 'rubocop', '~> 1.65.0'
|
32
|
+
gem.add_development_dependency 'rubocop-rails', '~> 2.25.0'
|
33
|
+
gem.add_development_dependency 'rubocop-rspec'
|
34
|
+
gem.add_development_dependency 'rubocop-thread_safety'
|
35
|
+
gem.add_development_dependency 'rubocop-performance'
|
36
|
+
end
|
@@ -1,144 +1,144 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'active_record/connection_adapters/abstract_adapter'
|
4
|
-
require 'active_record/connection_adapters/mysql2_adapter'
|
5
|
-
require_relative '../../janus'
|
6
|
-
|
7
|
-
module ActiveRecord
|
8
|
-
module ConnectionHandling
|
9
|
-
def janus_mysql2_connection(config)
|
10
|
-
ActiveRecord::ConnectionAdapters::JanusMysql2Adapter.new(config)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
module ActiveRecord
|
16
|
-
class Base
|
17
|
-
def self.janus_mysql2_adapter_class
|
18
|
-
ActiveRecord::ConnectionAdapters::JanusMysql2Adapter
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
module ActiveRecord
|
24
|
-
module ConnectionAdapters
|
25
|
-
class JanusMysql2Adapter < ActiveRecord::ConnectionAdapters::Mysql2Adapter
|
26
|
-
FOUND_ROWS = 'FOUND_ROWS'
|
27
|
-
|
28
|
-
attr_reader :config
|
29
|
-
|
30
|
-
class << self
|
31
|
-
def dbconsole(config, options = {})
|
32
|
-
connection_config = Janus::DbConsoleConfig.new(config)
|
33
|
-
|
34
|
-
super(connection_config, options)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def initialize(*args)
|
39
|
-
args[0][:janus]['replica']['database'] = args[0][:database]
|
40
|
-
args[0][:janus]['primary']['database'] = args[0][:database]
|
41
|
-
|
42
|
-
@replica_config = args[0][:janus]['replica']
|
43
|
-
args[0] = args[0][:janus]['primary']
|
44
|
-
|
45
|
-
super(*args)
|
46
|
-
@connection_parameters ||= args[0]
|
47
|
-
update_config
|
48
|
-
end
|
49
|
-
|
50
|
-
def raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: true)
|
51
|
-
case where_to_send?(sql)
|
52
|
-
when :all
|
53
|
-
send_to_replica(sql, connection: :all, method: :raw_execute)
|
54
|
-
super
|
55
|
-
when :replica
|
56
|
-
send_to_replica(sql, connection: :replica, method: :raw_execute)
|
57
|
-
else
|
58
|
-
Janus::Context.stick_to_primary if write_query?(sql)
|
59
|
-
Janus::Context.used_connection(:primary)
|
60
|
-
super
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
def execute(sql)
|
65
|
-
case where_to_send?(sql)
|
66
|
-
when :all
|
67
|
-
send_to_replica(sql, connection: :all, method: :execute)
|
68
|
-
super(sql)
|
69
|
-
when :replica
|
70
|
-
send_to_replica(sql, connection: :replica, method: :execute)
|
71
|
-
else
|
72
|
-
Janus::Context.stick_to_primary if write_query?(sql)
|
73
|
-
Janus::Context.used_connection(:primary)
|
74
|
-
super(sql)
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
def execute_and_free(sql, name = nil, async: false)
|
79
|
-
case where_to_send?(sql)
|
80
|
-
when :all
|
81
|
-
send_to_replica(sql, connection: :all, method: :execute)
|
82
|
-
super(sql, name, async:)
|
83
|
-
when :replica
|
84
|
-
send_to_replica(sql, connection: :replica, method: :execute)
|
85
|
-
else
|
86
|
-
Janus::Context.stick_to_primary if write_query?(sql)
|
87
|
-
Janus::Context.used_connection(:primary)
|
88
|
-
super(sql, name, async:)
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
def connect!(...)
|
93
|
-
replica_connection.connect!(...)
|
94
|
-
super
|
95
|
-
end
|
96
|
-
|
97
|
-
def reconnect!(...)
|
98
|
-
replica_connection.reconnect!(...)
|
99
|
-
super
|
100
|
-
end
|
101
|
-
|
102
|
-
def disconnect!(...)
|
103
|
-
replica_connection.disconnect!(...)
|
104
|
-
super
|
105
|
-
end
|
106
|
-
|
107
|
-
def clear_cache!(...)
|
108
|
-
replica_connection.clear_cache!(...)
|
109
|
-
super
|
110
|
-
end
|
111
|
-
|
112
|
-
def replica_connection
|
113
|
-
@replica_connection ||= ActiveRecord::ConnectionAdapters::Mysql2Adapter.new(@replica_config)
|
114
|
-
end
|
115
|
-
|
116
|
-
private
|
117
|
-
|
118
|
-
def where_to_send?(sql)
|
119
|
-
Janus::QueryDirector.new(sql, open_transactions).where_to_send?
|
120
|
-
end
|
121
|
-
|
122
|
-
def send_to_replica(sql, connection: nil, method: :exec_query)
|
123
|
-
Janus::Context.used_connection(connection) if connection
|
124
|
-
if method == :execute
|
125
|
-
replica_connection.execute(sql)
|
126
|
-
elsif method == :raw_execute
|
127
|
-
replica_connection.execute(sql)
|
128
|
-
else
|
129
|
-
replica_connection.exec_query(sql)
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
def update_config
|
134
|
-
@config[:flags] ||= 0
|
135
|
-
|
136
|
-
if @config[:flags].is_a? Array
|
137
|
-
@config[:flags].push FOUND_ROWS
|
138
|
-
else
|
139
|
-
@config[:flags] |= ::Janus::Client::FOUND_ROWS
|
140
|
-
end
|
141
|
-
end
|
142
|
-
end
|
143
|
-
end
|
144
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_record/connection_adapters/abstract_adapter'
|
4
|
+
require 'active_record/connection_adapters/mysql2_adapter'
|
5
|
+
require_relative '../../janus'
|
6
|
+
|
7
|
+
module ActiveRecord
|
8
|
+
module ConnectionHandling
|
9
|
+
def janus_mysql2_connection(config)
|
10
|
+
ActiveRecord::ConnectionAdapters::JanusMysql2Adapter.new(config)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module ActiveRecord
|
16
|
+
class Base
|
17
|
+
def self.janus_mysql2_adapter_class
|
18
|
+
ActiveRecord::ConnectionAdapters::JanusMysql2Adapter
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
module ActiveRecord
|
24
|
+
module ConnectionAdapters
|
25
|
+
class JanusMysql2Adapter < ActiveRecord::ConnectionAdapters::Mysql2Adapter
|
26
|
+
FOUND_ROWS = 'FOUND_ROWS'
|
27
|
+
|
28
|
+
attr_reader :config
|
29
|
+
|
30
|
+
class << self
|
31
|
+
def dbconsole(config, options = {})
|
32
|
+
connection_config = Janus::DbConsoleConfig.new(config)
|
33
|
+
|
34
|
+
super(connection_config, options)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def initialize(*args)
|
39
|
+
args[0][:janus]['replica']['database'] = args[0][:database]
|
40
|
+
args[0][:janus]['primary']['database'] = args[0][:database]
|
41
|
+
|
42
|
+
@replica_config = args[0][:janus]['replica']
|
43
|
+
args[0] = args[0][:janus]['primary']
|
44
|
+
|
45
|
+
super(*args)
|
46
|
+
@connection_parameters ||= args[0]
|
47
|
+
update_config
|
48
|
+
end
|
49
|
+
|
50
|
+
def raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: true)
|
51
|
+
case where_to_send?(sql)
|
52
|
+
when :all
|
53
|
+
send_to_replica(sql, connection: :all, method: :raw_execute)
|
54
|
+
super
|
55
|
+
when :replica
|
56
|
+
send_to_replica(sql, connection: :replica, method: :raw_execute)
|
57
|
+
else
|
58
|
+
Janus::Context.stick_to_primary if write_query?(sql)
|
59
|
+
Janus::Context.used_connection(:primary)
|
60
|
+
super
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def execute(sql)
|
65
|
+
case where_to_send?(sql)
|
66
|
+
when :all
|
67
|
+
send_to_replica(sql, connection: :all, method: :execute)
|
68
|
+
super(sql)
|
69
|
+
when :replica
|
70
|
+
send_to_replica(sql, connection: :replica, method: :execute)
|
71
|
+
else
|
72
|
+
Janus::Context.stick_to_primary if write_query?(sql)
|
73
|
+
Janus::Context.used_connection(:primary)
|
74
|
+
super(sql)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def execute_and_free(sql, name = nil, async: false)
|
79
|
+
case where_to_send?(sql)
|
80
|
+
when :all
|
81
|
+
send_to_replica(sql, connection: :all, method: :execute)
|
82
|
+
super(sql, name, async:)
|
83
|
+
when :replica
|
84
|
+
send_to_replica(sql, connection: :replica, method: :execute)
|
85
|
+
else
|
86
|
+
Janus::Context.stick_to_primary if write_query?(sql)
|
87
|
+
Janus::Context.used_connection(:primary)
|
88
|
+
super(sql, name, async:)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def connect!(...)
|
93
|
+
replica_connection.connect!(...)
|
94
|
+
super
|
95
|
+
end
|
96
|
+
|
97
|
+
def reconnect!(...)
|
98
|
+
replica_connection.reconnect!(...)
|
99
|
+
super
|
100
|
+
end
|
101
|
+
|
102
|
+
def disconnect!(...)
|
103
|
+
replica_connection.disconnect!(...)
|
104
|
+
super
|
105
|
+
end
|
106
|
+
|
107
|
+
def clear_cache!(...)
|
108
|
+
replica_connection.clear_cache!(...)
|
109
|
+
super
|
110
|
+
end
|
111
|
+
|
112
|
+
def replica_connection
|
113
|
+
@replica_connection ||= ActiveRecord::ConnectionAdapters::Mysql2Adapter.new(@replica_config)
|
114
|
+
end
|
115
|
+
|
116
|
+
private
|
117
|
+
|
118
|
+
def where_to_send?(sql)
|
119
|
+
Janus::QueryDirector.new(sql, open_transactions).where_to_send?
|
120
|
+
end
|
121
|
+
|
122
|
+
def send_to_replica(sql, connection: nil, method: :exec_query)
|
123
|
+
Janus::Context.used_connection(connection) if connection
|
124
|
+
if method == :execute
|
125
|
+
replica_connection.execute(sql)
|
126
|
+
elsif method == :raw_execute
|
127
|
+
replica_connection.execute(sql)
|
128
|
+
else
|
129
|
+
replica_connection.exec_query(sql)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def update_config
|
134
|
+
@config[:flags] ||= 0
|
135
|
+
|
136
|
+
if @config[:flags].is_a? Array
|
137
|
+
@config[:flags].push FOUND_ROWS
|
138
|
+
else
|
139
|
+
@config[:flags] |= ::Janus::Client::FOUND_ROWS
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
@@ -1,144 +1,144 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'active_record/connection_adapters/abstract_adapter'
|
4
|
-
require 'active_record/connection_adapters/trilogy_adapter'
|
5
|
-
require_relative '../../janus'
|
6
|
-
|
7
|
-
module ActiveRecord
|
8
|
-
module ConnectionHandling
|
9
|
-
def janus_trilogy_connection(config)
|
10
|
-
ActiveRecord::ConnectionAdapters::JanusTrilogyAdapter.new(config)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
module ActiveRecord
|
16
|
-
class Base
|
17
|
-
def self.janus_trilogy_adapter_class
|
18
|
-
ActiveRecord::ConnectionAdapters::JanusTrilogyAdapter
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
module ActiveRecord
|
24
|
-
module ConnectionAdapters
|
25
|
-
class JanusTrilogyAdapter < ActiveRecord::ConnectionAdapters::TrilogyAdapter
|
26
|
-
FOUND_ROWS = 'FOUND_ROWS'
|
27
|
-
|
28
|
-
attr_reader :config
|
29
|
-
|
30
|
-
class << self
|
31
|
-
def dbconsole(config, options = {})
|
32
|
-
connection_config = Janus::DbConsoleConfig.new(config)
|
33
|
-
|
34
|
-
super(connection_config, options)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def initialize(*args)
|
39
|
-
args[0][:janus]['replica']['database'] = args[0][:database]
|
40
|
-
args[0][:janus]['primary']['database'] = args[0][:database]
|
41
|
-
|
42
|
-
@replica_config = args[0][:janus]['replica'].symbolize_keys
|
43
|
-
args[0] = args[0][:janus]['primary'].symbolize_keys
|
44
|
-
|
45
|
-
super(*args)
|
46
|
-
@connection_parameters ||= args[0]
|
47
|
-
update_config
|
48
|
-
end
|
49
|
-
|
50
|
-
def raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: true)
|
51
|
-
case where_to_send?(sql)
|
52
|
-
when :all
|
53
|
-
send_to_replica(sql, connection: :all, method: :execute)
|
54
|
-
super
|
55
|
-
when :replica
|
56
|
-
send_to_replica(sql, connection: :replica, method: :execute)
|
57
|
-
else
|
58
|
-
Janus::Context.stick_to_primary if write_query?(sql)
|
59
|
-
Janus::Context.used_connection(:primary)
|
60
|
-
super
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
def execute(sql)
|
65
|
-
case where_to_send?(sql)
|
66
|
-
when :all
|
67
|
-
send_to_replica(sql, connection: :all, method: :execute)
|
68
|
-
super(sql)
|
69
|
-
when :replica
|
70
|
-
send_to_replica(sql, connection: :replica, method: :execute)
|
71
|
-
else
|
72
|
-
Janus::Context.stick_to_primary if write_query?(sql)
|
73
|
-
Janus::Context.used_connection(:primary)
|
74
|
-
super(sql)
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
def execute_and_free(sql, name = nil, async: false)
|
79
|
-
case where_to_send?(sql)
|
80
|
-
when :all
|
81
|
-
send_to_replica(sql, connection: :all, method: :execute)
|
82
|
-
super(sql, name, async:)
|
83
|
-
when :replica
|
84
|
-
send_to_replica(sql, connection: :replica, method: :execute)
|
85
|
-
else
|
86
|
-
Janus::Context.stick_to_primary if write_query?(sql)
|
87
|
-
Janus::Context.used_connection(:primary)
|
88
|
-
super(sql, name, async:)
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
def connect!(...)
|
93
|
-
replica_connection.connect!(...)
|
94
|
-
super
|
95
|
-
end
|
96
|
-
|
97
|
-
def reconnect!(...)
|
98
|
-
replica_connection.reconnect!(...)
|
99
|
-
super
|
100
|
-
end
|
101
|
-
|
102
|
-
def disconnect!(...)
|
103
|
-
replica_connection.disconnect!(...)
|
104
|
-
super
|
105
|
-
end
|
106
|
-
|
107
|
-
def clear_cache!(...)
|
108
|
-
replica_connection.clear_cache!(...)
|
109
|
-
super
|
110
|
-
end
|
111
|
-
|
112
|
-
def replica_connection
|
113
|
-
@replica_connection ||= ActiveRecord::ConnectionAdapters::TrilogyAdapter.new(@replica_config)
|
114
|
-
end
|
115
|
-
|
116
|
-
private
|
117
|
-
|
118
|
-
def where_to_send?(sql)
|
119
|
-
Janus::QueryDirector.new(sql, open_transactions).where_to_send?
|
120
|
-
end
|
121
|
-
|
122
|
-
def send_to_replica(sql, connection: nil, method: :exec_query)
|
123
|
-
Janus::Context.used_connection(connection) if connection
|
124
|
-
if method == :execute
|
125
|
-
replica_connection.execute(sql)
|
126
|
-
elsif method == :raw_execute
|
127
|
-
replica_connection.execute(sql)
|
128
|
-
else
|
129
|
-
replica_connection.exec_query(sql)
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
def update_config
|
134
|
-
@config[:flags] ||= 0
|
135
|
-
|
136
|
-
if @config[:flags].is_a? Array
|
137
|
-
@config[:flags].push FOUND_ROWS
|
138
|
-
else
|
139
|
-
@config[:flags] |= ::Janus::Client::FOUND_ROWS
|
140
|
-
end
|
141
|
-
end
|
142
|
-
end
|
143
|
-
end
|
144
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_record/connection_adapters/abstract_adapter'
|
4
|
+
require 'active_record/connection_adapters/trilogy_adapter'
|
5
|
+
require_relative '../../janus'
|
6
|
+
|
7
|
+
module ActiveRecord
|
8
|
+
module ConnectionHandling
|
9
|
+
def janus_trilogy_connection(config)
|
10
|
+
ActiveRecord::ConnectionAdapters::JanusTrilogyAdapter.new(config)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module ActiveRecord
|
16
|
+
class Base
|
17
|
+
def self.janus_trilogy_adapter_class
|
18
|
+
ActiveRecord::ConnectionAdapters::JanusTrilogyAdapter
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
module ActiveRecord
|
24
|
+
module ConnectionAdapters
|
25
|
+
class JanusTrilogyAdapter < ActiveRecord::ConnectionAdapters::TrilogyAdapter
|
26
|
+
FOUND_ROWS = 'FOUND_ROWS'
|
27
|
+
|
28
|
+
attr_reader :config
|
29
|
+
|
30
|
+
class << self
|
31
|
+
def dbconsole(config, options = {})
|
32
|
+
connection_config = Janus::DbConsoleConfig.new(config)
|
33
|
+
|
34
|
+
super(connection_config, options)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def initialize(*args)
|
39
|
+
args[0][:janus]['replica']['database'] = args[0][:database]
|
40
|
+
args[0][:janus]['primary']['database'] = args[0][:database]
|
41
|
+
|
42
|
+
@replica_config = args[0][:janus]['replica'].symbolize_keys
|
43
|
+
args[0] = args[0][:janus]['primary'].symbolize_keys
|
44
|
+
|
45
|
+
super(*args)
|
46
|
+
@connection_parameters ||= args[0]
|
47
|
+
update_config
|
48
|
+
end
|
49
|
+
|
50
|
+
def raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: true)
|
51
|
+
case where_to_send?(sql)
|
52
|
+
when :all
|
53
|
+
send_to_replica(sql, connection: :all, method: :execute)
|
54
|
+
super
|
55
|
+
when :replica
|
56
|
+
send_to_replica(sql, connection: :replica, method: :execute)
|
57
|
+
else
|
58
|
+
Janus::Context.stick_to_primary if write_query?(sql)
|
59
|
+
Janus::Context.used_connection(:primary)
|
60
|
+
super
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def execute(sql)
|
65
|
+
case where_to_send?(sql)
|
66
|
+
when :all
|
67
|
+
send_to_replica(sql, connection: :all, method: :execute)
|
68
|
+
super(sql)
|
69
|
+
when :replica
|
70
|
+
send_to_replica(sql, connection: :replica, method: :execute)
|
71
|
+
else
|
72
|
+
Janus::Context.stick_to_primary if write_query?(sql)
|
73
|
+
Janus::Context.used_connection(:primary)
|
74
|
+
super(sql)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def execute_and_free(sql, name = nil, async: false)
|
79
|
+
case where_to_send?(sql)
|
80
|
+
when :all
|
81
|
+
send_to_replica(sql, connection: :all, method: :execute)
|
82
|
+
super(sql, name, async:)
|
83
|
+
when :replica
|
84
|
+
send_to_replica(sql, connection: :replica, method: :execute)
|
85
|
+
else
|
86
|
+
Janus::Context.stick_to_primary if write_query?(sql)
|
87
|
+
Janus::Context.used_connection(:primary)
|
88
|
+
super(sql, name, async:)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def connect!(...)
|
93
|
+
replica_connection.connect!(...)
|
94
|
+
super
|
95
|
+
end
|
96
|
+
|
97
|
+
def reconnect!(...)
|
98
|
+
replica_connection.reconnect!(...)
|
99
|
+
super
|
100
|
+
end
|
101
|
+
|
102
|
+
def disconnect!(...)
|
103
|
+
replica_connection.disconnect!(...)
|
104
|
+
super
|
105
|
+
end
|
106
|
+
|
107
|
+
def clear_cache!(...)
|
108
|
+
replica_connection.clear_cache!(...)
|
109
|
+
super
|
110
|
+
end
|
111
|
+
|
112
|
+
def replica_connection
|
113
|
+
@replica_connection ||= ActiveRecord::ConnectionAdapters::TrilogyAdapter.new(@replica_config)
|
114
|
+
end
|
115
|
+
|
116
|
+
private
|
117
|
+
|
118
|
+
def where_to_send?(sql)
|
119
|
+
Janus::QueryDirector.new(sql, open_transactions).where_to_send?
|
120
|
+
end
|
121
|
+
|
122
|
+
def send_to_replica(sql, connection: nil, method: :exec_query)
|
123
|
+
Janus::Context.used_connection(connection) if connection
|
124
|
+
if method == :execute
|
125
|
+
replica_connection.execute(sql)
|
126
|
+
elsif method == :raw_execute
|
127
|
+
replica_connection.execute(sql)
|
128
|
+
else
|
129
|
+
replica_connection.exec_query(sql)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def update_config
|
134
|
+
@config[:flags] ||= 0
|
135
|
+
|
136
|
+
if @config[:flags].is_a? Array
|
137
|
+
@config[:flags].push FOUND_ROWS
|
138
|
+
else
|
139
|
+
@config[:flags] |= ::Janus::Client::FOUND_ROWS
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
data/lib/janus/client.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
module Janus
|
3
|
-
class Client
|
4
|
-
FOUND_ROWS = 2
|
5
|
-
end
|
6
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Janus
|
3
|
+
class Client
|
4
|
+
FOUND_ROWS = 2
|
5
|
+
end
|
6
|
+
end
|