janus-ar 0.15.2 → 7.2.0
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/.rubocop.yml +26 -22
- data/Gemfile.lock +123 -128
- data/README.md +129 -110
- data/Rakefile +2 -0
- data/janus-ar.gemspec +36 -36
- data/lib/{active_record → janus-ar/active_record}/connection_adapters/janus_mysql2_adapter.rb +148 -144
- data/lib/{active_record → janus-ar/active_record}/connection_adapters/janus_trilogy_adapter.rb +148 -144
- data/lib/{janus → janus-ar}/client.rb +6 -6
- data/lib/{janus → janus-ar}/context.rb +80 -80
- data/lib/{janus → janus-ar}/db_console_config.rb +17 -17
- data/lib/{janus → janus-ar}/logging/logger.rb +15 -15
- data/lib/{janus → janus-ar}/logging/subscriber.rb +27 -27
- data/lib/{janus → janus-ar}/query_director.rb +54 -54
- data/lib/{janus → janus-ar}/version.rb +17 -17
- data/lib/janus-ar.rb +22 -0
- data/spec/lib/{active_record → janus-ar/active_record}/connection_adapters/janus_mysql_adapter_spec.rb +82 -76
- data/spec/lib/{active_record → janus-ar/active_record}/connection_adapters/janus_trilogy_adapter_spec.rb +82 -80
- data/spec/lib/{janus → janus-ar}/client_spec.rb +7 -7
- data/spec/lib/{janus → janus-ar}/context_spec.rb +46 -46
- data/spec/lib/{janus → janus-ar}/logging/logger_spec.rb +31 -31
- data/spec/lib/{janus → janus-ar}/query_director_spec.rb +59 -59
- data/spec/spec_helper.rb +46 -43
- metadata +30 -30
- data/lib/janus.rb +0 -22
data/lib/{active_record → janus-ar/active_record}/connection_adapters/janus_trilogy_adapter.rb
RENAMED
@@ -1,144 +1,148 @@
|
|
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 '
|
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
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
end
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
def
|
123
|
-
Janus::
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
replica_connection.
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
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-ar'
|
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 with_connection(_args = {})
|
93
|
+
self
|
94
|
+
end
|
95
|
+
|
96
|
+
def connect!(...)
|
97
|
+
replica_connection.connect!(...)
|
98
|
+
super
|
99
|
+
end
|
100
|
+
|
101
|
+
def reconnect!(...)
|
102
|
+
replica_connection.reconnect!(...)
|
103
|
+
super
|
104
|
+
end
|
105
|
+
|
106
|
+
def disconnect!(...)
|
107
|
+
replica_connection.disconnect!(...)
|
108
|
+
super
|
109
|
+
end
|
110
|
+
|
111
|
+
def clear_cache!(...)
|
112
|
+
replica_connection.clear_cache!(...)
|
113
|
+
super
|
114
|
+
end
|
115
|
+
|
116
|
+
def replica_connection
|
117
|
+
@replica_connection ||= ActiveRecord::ConnectionAdapters::TrilogyAdapter.new(@replica_config)
|
118
|
+
end
|
119
|
+
|
120
|
+
private
|
121
|
+
|
122
|
+
def where_to_send?(sql)
|
123
|
+
Janus::QueryDirector.new(sql, open_transactions).where_to_send?
|
124
|
+
end
|
125
|
+
|
126
|
+
def send_to_replica(sql, connection: nil, method: :exec_query)
|
127
|
+
Janus::Context.used_connection(connection) if connection
|
128
|
+
if method == :execute
|
129
|
+
replica_connection.execute(sql)
|
130
|
+
elsif method == :raw_execute
|
131
|
+
replica_connection.execute(sql)
|
132
|
+
else
|
133
|
+
replica_connection.exec_query(sql)
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
def update_config
|
138
|
+
@config[:flags] ||= 0
|
139
|
+
|
140
|
+
if @config[:flags].is_a? Array
|
141
|
+
@config[:flags].push FOUND_ROWS
|
142
|
+
else
|
143
|
+
@config[:flags] |= ::Janus::Client::FOUND_ROWS
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
@@ -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
|
@@ -1,80 +1,80 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Janus
|
4
|
-
class Context
|
5
|
-
THREAD_KEY = :janus_ar_context
|
6
|
-
|
7
|
-
# Stores the staged data with an expiration time based on the current time,
|
8
|
-
# and clears any expired entries. Returns true if any changes were made to
|
9
|
-
# the current store
|
10
|
-
def initialize(primary: false, expiry: nil)
|
11
|
-
@primary = primary
|
12
|
-
@expiry = expiry
|
13
|
-
@last_used_connection = :primary
|
14
|
-
end
|
15
|
-
|
16
|
-
def stick_to_primary
|
17
|
-
@primary = true
|
18
|
-
end
|
19
|
-
|
20
|
-
def potential_write
|
21
|
-
stick_to_primary
|
22
|
-
end
|
23
|
-
|
24
|
-
def release_all
|
25
|
-
@primary = false
|
26
|
-
@expiry = nil
|
27
|
-
@last_used_connection = nil
|
28
|
-
end
|
29
|
-
|
30
|
-
def use_primary?
|
31
|
-
@primary
|
32
|
-
end
|
33
|
-
|
34
|
-
def used_connection(connection)
|
35
|
-
@last_used_connection = connection
|
36
|
-
end
|
37
|
-
|
38
|
-
attr_reader :last_used_connection
|
39
|
-
|
40
|
-
class << self
|
41
|
-
def stick_to_primary
|
42
|
-
current.stick_to_primary
|
43
|
-
end
|
44
|
-
|
45
|
-
def release_all
|
46
|
-
current.release_all
|
47
|
-
end
|
48
|
-
|
49
|
-
def used_connection(connection)
|
50
|
-
current.used_connection(connection)
|
51
|
-
end
|
52
|
-
|
53
|
-
def use_primary?
|
54
|
-
current.use_primary?
|
55
|
-
end
|
56
|
-
|
57
|
-
def last_used_connection
|
58
|
-
current.last_used_connection
|
59
|
-
end
|
60
|
-
|
61
|
-
protected
|
62
|
-
|
63
|
-
def current
|
64
|
-
fetch(THREAD_KEY) { new }
|
65
|
-
end
|
66
|
-
|
67
|
-
def fetch(key)
|
68
|
-
get(key) || set(key, yield)
|
69
|
-
end
|
70
|
-
|
71
|
-
def get(key)
|
72
|
-
Thread.current.thread_variable_get(key)
|
73
|
-
end
|
74
|
-
|
75
|
-
def set(key, value)
|
76
|
-
Thread.current.thread_variable_set(key, value)
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Janus
|
4
|
+
class Context
|
5
|
+
THREAD_KEY = :janus_ar_context
|
6
|
+
|
7
|
+
# Stores the staged data with an expiration time based on the current time,
|
8
|
+
# and clears any expired entries. Returns true if any changes were made to
|
9
|
+
# the current store
|
10
|
+
def initialize(primary: false, expiry: nil)
|
11
|
+
@primary = primary
|
12
|
+
@expiry = expiry
|
13
|
+
@last_used_connection = :primary
|
14
|
+
end
|
15
|
+
|
16
|
+
def stick_to_primary
|
17
|
+
@primary = true
|
18
|
+
end
|
19
|
+
|
20
|
+
def potential_write
|
21
|
+
stick_to_primary
|
22
|
+
end
|
23
|
+
|
24
|
+
def release_all
|
25
|
+
@primary = false
|
26
|
+
@expiry = nil
|
27
|
+
@last_used_connection = nil
|
28
|
+
end
|
29
|
+
|
30
|
+
def use_primary?
|
31
|
+
@primary
|
32
|
+
end
|
33
|
+
|
34
|
+
def used_connection(connection)
|
35
|
+
@last_used_connection = connection
|
36
|
+
end
|
37
|
+
|
38
|
+
attr_reader :last_used_connection
|
39
|
+
|
40
|
+
class << self
|
41
|
+
def stick_to_primary
|
42
|
+
current.stick_to_primary
|
43
|
+
end
|
44
|
+
|
45
|
+
def release_all
|
46
|
+
current.release_all
|
47
|
+
end
|
48
|
+
|
49
|
+
def used_connection(connection)
|
50
|
+
current.used_connection(connection)
|
51
|
+
end
|
52
|
+
|
53
|
+
def use_primary?
|
54
|
+
current.use_primary?
|
55
|
+
end
|
56
|
+
|
57
|
+
def last_used_connection
|
58
|
+
current.last_used_connection
|
59
|
+
end
|
60
|
+
|
61
|
+
protected
|
62
|
+
|
63
|
+
def current
|
64
|
+
fetch(THREAD_KEY) { new }
|
65
|
+
end
|
66
|
+
|
67
|
+
def fetch(key)
|
68
|
+
get(key) || set(key, yield)
|
69
|
+
end
|
70
|
+
|
71
|
+
def get(key)
|
72
|
+
Thread.current.thread_variable_get(key)
|
73
|
+
end
|
74
|
+
|
75
|
+
def set(key, value)
|
76
|
+
Thread.current.thread_variable_set(key, value)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -1,17 +1,17 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Janus
|
4
|
-
class DbConsoleConfig
|
5
|
-
def initialize(config)
|
6
|
-
@_config = config.configuration_hash
|
7
|
-
end
|
8
|
-
|
9
|
-
def configuration_hash
|
10
|
-
@_config[:janus]['replica'].symbolize_keys
|
11
|
-
end
|
12
|
-
|
13
|
-
def database
|
14
|
-
@_config[:database]
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Janus
|
4
|
+
class DbConsoleConfig
|
5
|
+
def initialize(config)
|
6
|
+
@_config = config.configuration_hash
|
7
|
+
end
|
8
|
+
|
9
|
+
def configuration_hash
|
10
|
+
@_config[:janus]['replica'].symbolize_keys
|
11
|
+
end
|
12
|
+
|
13
|
+
def database
|
14
|
+
@_config[:database]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -1,15 +1,15 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Janus
|
4
|
-
module Logging
|
5
|
-
class Logger
|
6
|
-
class << self
|
7
|
-
def log(message, format = :info)
|
8
|
-
logger&.send(format, "[Janus] #{message}")
|
9
|
-
end
|
10
|
-
|
11
|
-
attr_accessor :logger
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Janus
|
4
|
+
module Logging
|
5
|
+
class Logger
|
6
|
+
class << self
|
7
|
+
def log(message, format = :info)
|
8
|
+
logger&.send(format, "[Janus] #{message}")
|
9
|
+
end
|
10
|
+
|
11
|
+
attr_accessor :logger
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -1,27 +1,27 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Janus
|
4
|
-
module Logging
|
5
|
-
module Subscriber
|
6
|
-
IGNORE_PAYLOAD_NAMES = %w(SCHEMA EXPLAIN).freeze
|
7
|
-
|
8
|
-
def sql(event)
|
9
|
-
name = event.payload[:name]
|
10
|
-
unless IGNORE_PAYLOAD_NAMES.include?(name)
|
11
|
-
name = [current_wrapper_name(event), name].compact.join(' ')
|
12
|
-
event.payload[:name] = name
|
13
|
-
end
|
14
|
-
super(event)
|
15
|
-
end
|
16
|
-
|
17
|
-
protected
|
18
|
-
|
19
|
-
def current_wrapper_name(_event)
|
20
|
-
connection = Janus::Context.last_used_connection
|
21
|
-
return nil unless connection
|
22
|
-
|
23
|
-
"[#{connection}]"
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Janus
|
4
|
+
module Logging
|
5
|
+
module Subscriber
|
6
|
+
IGNORE_PAYLOAD_NAMES = %w(SCHEMA EXPLAIN).freeze
|
7
|
+
|
8
|
+
def sql(event)
|
9
|
+
name = event.payload[:name]
|
10
|
+
unless IGNORE_PAYLOAD_NAMES.include?(name)
|
11
|
+
name = [current_wrapper_name(event), name].compact.join(' ')
|
12
|
+
event.payload[:name] = name
|
13
|
+
end
|
14
|
+
super(event)
|
15
|
+
end
|
16
|
+
|
17
|
+
protected
|
18
|
+
|
19
|
+
def current_wrapper_name(_event)
|
20
|
+
connection = Janus::Context.last_used_connection
|
21
|
+
return nil unless connection
|
22
|
+
|
23
|
+
"[#{connection}]"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|