makara 0.4.1 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +5 -5
  2. data/.github/dependabot.yml +11 -0
  3. data/.github/workflows/CI.yml +88 -0
  4. data/.github/workflows/gem-publish-public.yml +36 -0
  5. data/.rspec +1 -1
  6. data/.rubocop.yml +15 -0
  7. data/.rubocop_todo.yml +670 -0
  8. data/CHANGELOG.md +69 -48
  9. data/Gemfile +1 -16
  10. data/README.md +8 -9
  11. data/Rakefile +1 -1
  12. data/gemfiles/activerecord_5.2.gemfile +8 -0
  13. data/gemfiles/activerecord_6.0.gemfile +8 -0
  14. data/gemfiles/activerecord_6.1.gemfile +8 -0
  15. data/gemfiles/activerecord_head.gemfile +6 -0
  16. data/lib/active_record/connection_adapters/jdbcmysql_makara_adapter.rb +4 -18
  17. data/lib/active_record/connection_adapters/jdbcpostgresql_makara_adapter.rb +4 -18
  18. data/lib/active_record/connection_adapters/makara_abstract_adapter.rb +107 -30
  19. data/lib/active_record/connection_adapters/makara_jdbcmysql_adapter.rb +4 -18
  20. data/lib/active_record/connection_adapters/makara_jdbcpostgresql_adapter.rb +4 -18
  21. data/lib/active_record/connection_adapters/makara_mysql2_adapter.rb +4 -20
  22. data/lib/active_record/connection_adapters/makara_postgis_adapter.rb +4 -19
  23. data/lib/active_record/connection_adapters/makara_postgresql_adapter.rb +4 -20
  24. data/lib/active_record/connection_adapters/mysql2_makara_adapter.rb +4 -20
  25. data/lib/active_record/connection_adapters/postgresql_makara_adapter.rb +4 -20
  26. data/lib/makara/cache.rb +0 -2
  27. data/lib/makara/config_parser.rb +7 -15
  28. data/lib/makara/connection_wrapper.rb +43 -22
  29. data/lib/makara/context.rb +1 -0
  30. data/lib/makara/cookie.rb +1 -0
  31. data/lib/makara/error_handler.rb +0 -9
  32. data/lib/makara/errors/all_connections_blacklisted.rb +0 -2
  33. data/lib/makara/errors/blacklist_connection.rb +0 -2
  34. data/lib/makara/errors/blacklisted_while_in_transaction.rb +12 -0
  35. data/lib/makara/errors/invalid_shard.rb +14 -0
  36. data/lib/makara/errors/makara_error.rb +0 -1
  37. data/lib/makara/errors/no_connections_available.rb +0 -2
  38. data/lib/makara/logging/logger.rb +0 -4
  39. data/lib/makara/logging/subscriber.rb +0 -2
  40. data/lib/makara/middleware.rb +1 -2
  41. data/lib/makara/pool.rb +49 -31
  42. data/lib/makara/proxy.rb +56 -30
  43. data/lib/makara/railtie.rb +0 -2
  44. data/lib/makara/strategies/abstract.rb +1 -0
  45. data/lib/makara/strategies/priority_failover.rb +2 -0
  46. data/lib/makara/strategies/round_robin.rb +1 -3
  47. data/lib/makara/strategies/shard_aware.rb +45 -0
  48. data/lib/makara/version.rb +1 -3
  49. data/lib/makara.rb +7 -6
  50. data/makara.gemspec +26 -3
  51. data/spec/active_record/connection_adapters/makara_abstract_adapter_error_handling_spec.rb +1 -6
  52. data/spec/active_record/connection_adapters/makara_abstract_adapter_spec.rb +0 -9
  53. data/spec/active_record/connection_adapters/makara_mysql2_adapter_spec.rb +9 -22
  54. data/spec/active_record/connection_adapters/makara_postgis_adapter_spec.rb +2 -10
  55. data/spec/active_record/connection_adapters/makara_postgresql_adapter_spec.rb +62 -18
  56. data/spec/cache_spec.rb +0 -1
  57. data/spec/config_parser_spec.rb +54 -56
  58. data/spec/connection_wrapper_spec.rb +1 -2
  59. data/spec/cookie_spec.rb +4 -4
  60. data/spec/middleware_spec.rb +2 -2
  61. data/spec/pool_spec.rb +25 -14
  62. data/spec/proxy_spec.rb +0 -4
  63. data/spec/spec_helper.rb +6 -1
  64. data/spec/strategies/priority_failover_spec.rb +3 -4
  65. data/spec/strategies/round_robin_spec.rb +4 -8
  66. data/spec/strategies/shard_aware_spec.rb +218 -0
  67. data/spec/support/deep_dup.rb +1 -1
  68. data/spec/support/helpers.rb +5 -5
  69. data/spec/support/mock_objects.rb +5 -4
  70. data/spec/support/mysql2_database.yml +2 -2
  71. data/spec/support/mysql2_database_with_custom_errors.yml +2 -2
  72. data/spec/support/pool_extensions.rb +0 -3
  73. data/spec/support/postgis_schema.rb +1 -1
  74. data/spec/support/postgresql_database.yml +0 -2
  75. data/spec/support/proxy_extensions.rb +1 -3
  76. data/spec/support/schema.rb +1 -1
  77. data/spec/support/user.rb +1 -2
  78. metadata +165 -20
  79. data/.travis.yml +0 -105
  80. data/gemfiles/ar-head.gemfile +0 -24
  81. data/gemfiles/ar30.gemfile +0 -36
  82. data/gemfiles/ar31.gemfile +0 -36
  83. data/gemfiles/ar32.gemfile +0 -36
  84. data/gemfiles/ar40.gemfile +0 -24
  85. data/gemfiles/ar41.gemfile +0 -24
  86. data/gemfiles/ar42.gemfile +0 -24
  87. data/gemfiles/ar50.gemfile +0 -24
  88. data/gemfiles/ar51.gemfile +0 -24
@@ -2,24 +2,10 @@ require 'active_record/connection_adapters/makara_abstract_adapter'
2
2
  require 'active_record/connection_adapters/jdbcmysql_adapter'
3
3
  require 'active_record/connection_adapters/makara_mysql2_adapter'
4
4
 
5
- if ActiveRecord::VERSION::MAJOR >= 4
6
-
7
- module ActiveRecord
8
- module ConnectionHandling
9
- def makara_jdbcmysql_connection(config)
10
- makara_mysql2_connection(config)
11
- end
12
- end
13
- end
14
-
15
- else
16
-
17
- module ActiveRecord
18
- class Base
19
- def self.makara_jdbcmysql_connection(config)
20
- self.makara_mysql2_connection(config)
21
- end
5
+ module ActiveRecord
6
+ module ConnectionHandling
7
+ def makara_jdbcmysql_connection(config)
8
+ makara_mysql2_connection(config)
22
9
  end
23
10
  end
24
-
25
11
  end
@@ -2,24 +2,10 @@ require 'active_record/connection_adapters/makara_abstract_adapter'
2
2
  require 'active_record/connection_adapters/jdbcpostgresql_adapter'
3
3
  require 'active_record/connection_adapters/makara_postgresql_adapter'
4
4
 
5
- if ActiveRecord::VERSION::MAJOR >= 4
6
-
7
- module ActiveRecord
8
- module ConnectionHandling
9
- def makara_jdbcpostgresql_connection(config)
10
- makara_postgresql_connection(config)
11
- end
12
- end
13
- end
14
-
15
- else
16
-
17
- module ActiveRecord
18
- class Base
19
- def self.makara_jdbcpostgresql_connection(config)
20
- self.makara_postgresql_connection(config)
21
- end
5
+ module ActiveRecord
6
+ module ConnectionHandling
7
+ def makara_jdbcpostgresql_connection(config)
8
+ makara_postgresql_connection(config)
22
9
  end
23
10
  end
24
-
25
11
  end
@@ -1,32 +1,17 @@
1
1
  require 'active_record/connection_adapters/makara_abstract_adapter'
2
2
  require 'active_record/connection_adapters/mysql2_adapter'
3
3
 
4
- if ActiveRecord::VERSION::MAJOR >= 4
5
-
6
- module ActiveRecord
7
- module ConnectionHandling
8
- def makara_mysql2_connection(config)
9
- ActiveRecord::ConnectionAdapters::MakaraMysql2Adapter.new(config)
10
- end
11
- end
12
- end
13
-
14
- else
15
-
16
- module ActiveRecord
17
- class Base
18
- def self.makara_mysql2_connection(config)
19
- ActiveRecord::ConnectionAdapters::MakaraMysql2Adapter.new(config)
20
- end
4
+ module ActiveRecord
5
+ module ConnectionHandling
6
+ def makara_mysql2_connection(config)
7
+ ActiveRecord::ConnectionAdapters::MakaraMysql2Adapter.new(config)
21
8
  end
22
9
  end
23
-
24
10
  end
25
11
 
26
12
  module ActiveRecord
27
13
  module ConnectionAdapters
28
14
  class MakaraMysql2Adapter < ActiveRecord::ConnectionAdapters::MakaraAbstractAdapter
29
-
30
15
  class << self
31
16
  def visitor_for(*args)
32
17
  ActiveRecord::ConnectionAdapters::Mysql2Adapter.visitor_for(*args)
@@ -38,7 +23,6 @@ module ActiveRecord
38
23
  def active_record_connection_for(config)
39
24
  ::ActiveRecord::Base.mysql2_connection(config)
40
25
  end
41
-
42
26
  end
43
27
  end
44
28
  end
@@ -1,29 +1,14 @@
1
1
  require 'active_record/connection_adapters/makara_abstract_adapter'
2
2
  require 'active_record/connection_adapters/postgis_adapter'
3
3
 
4
- if ActiveRecord::VERSION::MAJOR >= 4
5
-
6
- module ActiveRecord
7
- module ConnectionHandling
8
- def makara_postgis_connection(config)
9
- ActiveRecord::ConnectionAdapters::MakaraPostgisAdapter.new(config)
10
- end
11
- end
12
- end
13
-
14
- else
15
-
16
- module ActiveRecord
17
- class Base
18
- def self.makara_postgis_connection(config)
19
- ActiveRecord::ConnectionAdapters::MakaraPostgisAdapter.new(config)
20
- end
4
+ module ActiveRecord
5
+ module ConnectionHandling
6
+ def makara_postgis_connection(config)
7
+ ActiveRecord::ConnectionAdapters::MakaraPostgisAdapter.new(config)
21
8
  end
22
9
  end
23
-
24
10
  end
25
11
 
26
-
27
12
  module ActiveRecord
28
13
  module ConnectionAdapters
29
14
  class MakaraPostgisAdapter < ActiveRecord::ConnectionAdapters::MakaraAbstractAdapter
@@ -1,32 +1,17 @@
1
1
  require 'active_record/connection_adapters/makara_abstract_adapter'
2
2
  require 'active_record/connection_adapters/postgresql_adapter'
3
3
 
4
- if ActiveRecord::VERSION::MAJOR >= 4
5
-
6
- module ActiveRecord
7
- module ConnectionHandling
8
- def makara_postgresql_connection(config)
9
- ActiveRecord::ConnectionAdapters::MakaraPostgreSQLAdapter.new(config)
10
- end
11
- end
12
- end
13
-
14
- else
15
-
16
- module ActiveRecord
17
- class Base
18
- def self.makara_postgresql_connection(config)
19
- ActiveRecord::ConnectionAdapters::MakaraPostgreSQLAdapter.new(config)
20
- end
4
+ module ActiveRecord
5
+ module ConnectionHandling
6
+ def makara_postgresql_connection(config)
7
+ ActiveRecord::ConnectionAdapters::MakaraPostgreSQLAdapter.new(config)
21
8
  end
22
9
  end
23
-
24
10
  end
25
11
 
26
12
  module ActiveRecord
27
13
  module ConnectionAdapters
28
14
  class MakaraPostgreSQLAdapter < ActiveRecord::ConnectionAdapters::MakaraAbstractAdapter
29
-
30
15
  class << self
31
16
  def visitor_for(*args)
32
17
  ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.visitor_for(*args)
@@ -38,7 +23,6 @@ module ActiveRecord
38
23
  def active_record_connection_for(config)
39
24
  ::ActiveRecord::Base.postgresql_connection(config)
40
25
  end
41
-
42
26
  end
43
27
  end
44
28
  end
@@ -1,32 +1,17 @@
1
1
  require 'active_record/connection_adapters/makara_abstract_adapter'
2
2
  require 'active_record/connection_adapters/mysql2_adapter'
3
3
 
4
- if ActiveRecord::VERSION::MAJOR >= 4
5
-
6
- module ActiveRecord
7
- module ConnectionHandling
8
- def mysql2_makara_connection(config)
9
- ActiveRecord::ConnectionAdapters::MakaraMysql2Adapter.new(config)
10
- end
11
- end
12
- end
13
-
14
- else
15
-
16
- module ActiveRecord
17
- class Base
18
- def self.mysql2_makara_connection(config)
19
- ActiveRecord::ConnectionAdapters::MakaraMysql2Adapter.new(config)
20
- end
4
+ module ActiveRecord
5
+ module ConnectionHandling
6
+ def mysql2_makara_connection(config)
7
+ ActiveRecord::ConnectionAdapters::MakaraMysql2Adapter.new(config)
21
8
  end
22
9
  end
23
-
24
10
  end
25
11
 
26
12
  module ActiveRecord
27
13
  module ConnectionAdapters
28
14
  class MakaraMysql2Adapter < ActiveRecord::ConnectionAdapters::MakaraAbstractAdapter
29
-
30
15
  class << self
31
16
  def visitor_for(*args)
32
17
  ActiveRecord::ConnectionAdapters::Mysql2Adapter.visitor_for(*args)
@@ -38,7 +23,6 @@ module ActiveRecord
38
23
  def active_record_connection_for(config)
39
24
  ::ActiveRecord::Base.mysql2_connection(config)
40
25
  end
41
-
42
26
  end
43
27
  end
44
28
  end
@@ -1,32 +1,17 @@
1
1
  require 'active_record/connection_adapters/makara_abstract_adapter'
2
2
  require 'active_record/connection_adapters/postgresql_adapter'
3
3
 
4
- if ActiveRecord::VERSION::MAJOR >= 4
5
-
6
- module ActiveRecord
7
- module ConnectionHandling
8
- def postgresql_makara_connection(config)
9
- ActiveRecord::ConnectionAdapters::MakaraPostgreSQLAdapter.new(config)
10
- end
11
- end
12
- end
13
-
14
- else
15
-
16
- module ActiveRecord
17
- class Base
18
- def self.postgresql_makara_connection(config)
19
- ActiveRecord::ConnectionAdapters::MakaraPostgreSQLAdapter.new(config)
20
- end
4
+ module ActiveRecord
5
+ module ConnectionHandling
6
+ def postgresql_makara_connection(config)
7
+ ActiveRecord::ConnectionAdapters::MakaraPostgreSQLAdapter.new(config)
21
8
  end
22
9
  end
23
-
24
10
  end
25
11
 
26
12
  module ActiveRecord
27
13
  module ConnectionAdapters
28
14
  class MakaraPostgreSQLAdapter < ActiveRecord::ConnectionAdapters::MakaraAbstractAdapter
29
-
30
15
  class << self
31
16
  def visitor_for(*args)
32
17
  ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.visitor_for(*args)
@@ -38,7 +23,6 @@ module ActiveRecord
38
23
  def active_record_connection_for(config)
39
24
  ::ActiveRecord::Base.postgresql_connection(config)
40
25
  end
41
-
42
26
  end
43
27
  end
44
28
  end
data/lib/makara/cache.rb CHANGED
@@ -1,8 +1,6 @@
1
1
  module Makara
2
2
  module Cache
3
-
4
3
  class << self
5
-
6
4
  def store=(store)
7
5
  Makara::Logging::Logger.log deprecation_warning, :warn
8
6
  end
@@ -1,6 +1,7 @@
1
1
  require 'digest/md5'
2
2
  require 'active_support/core_ext/hash/keys'
3
3
  require 'active_support/core_ext/hash/except'
4
+ require 'cgi'
4
5
 
5
6
  # Convenience methods to grab subconfigs out of the primary configuration.
6
7
  # Provides a way to generate a consistent ID based on a unique config.
@@ -19,11 +20,10 @@ require 'active_support/core_ext/hash/except'
19
20
 
20
21
  module Makara
21
22
  class ConfigParser
22
-
23
23
  DEFAULTS = {
24
- :master_ttl => 5,
25
- :blacklist_duration => 30,
26
- :sticky => true
24
+ master_ttl: 5,
25
+ blacklist_duration: 30,
26
+ sticky: true
27
27
  }
28
28
 
29
29
  # ConnectionUrlResolver is borrowed from Rails 4-2 since its location and implementation
@@ -32,7 +32,6 @@ module Makara
32
32
  #
33
33
  # Expands a connection string into a hash.
34
34
  class ConnectionUrlResolver # :nodoc:
35
-
36
35
  # == Example
37
36
  #
38
37
  # url = "postgresql://foo:bar@localhost:9000/foo_test?pool=5&timeout=3000"
@@ -49,6 +48,7 @@ module Makara
49
48
  # }
50
49
  def initialize(url)
51
50
  raise "Database URL cannot be empty" if url.blank?
51
+
52
52
  @uri = URI.parse(url)
53
53
  @adapter = @uri.scheme.tr('-', '_')
54
54
  @adapter = "postgresql" if @adapter == "postgres"
@@ -63,7 +63,7 @@ module Makara
63
63
  # Converts the given URL to a full connection hash.
64
64
  def to_hash
65
65
  config = raw_config.reject { |_,value| value.blank? }
66
- config.map { |key,value| config[key] = URI.unescape(value) if value.is_a? String }
66
+ config.map { |key,value| config[key] = CGI.unescape(value) if value.is_a? String }
67
67
  config
68
68
  end
69
69
 
@@ -130,6 +130,7 @@ module Makara
130
130
  Makara::Logging::Logger.log "Please rename DATABASE_URL to use in the database.yml", :warn
131
131
  end
132
132
  return config unless config.key?(:url)
133
+
133
134
  url = config[:url]
134
135
  url_config = ConnectionUrlResolver.new(url).to_hash
135
136
  url_config = url_config.symbolize_keys
@@ -147,7 +148,6 @@ module Makara
147
148
  @id = sanitize_id(@makara_config[:id])
148
149
  end
149
150
 
150
-
151
151
  def id
152
152
  @id ||= begin
153
153
  sorted = recursive_sort(@config)
@@ -155,24 +155,20 @@ module Makara
155
155
  end
156
156
  end
157
157
 
158
-
159
158
  def master_configs
160
159
  all_configs
161
160
  .select { |config| config[:role] == 'master' }
162
161
  .map { |config| config.except(:role) }
163
162
  end
164
163
 
165
-
166
164
  def slave_configs
167
165
  all_configs
168
166
  .reject { |config| config[:role] == 'master' }
169
167
  .map { |config| config.except(:role) }
170
168
  end
171
169
 
172
-
173
170
  protected
174
171
 
175
-
176
172
  def all_configs
177
173
  @makara_config[:connections].map do |connection|
178
174
  base_config.merge(makara_config.except(:connections))
@@ -180,12 +176,10 @@ module Makara
180
176
  end
181
177
  end
182
178
 
183
-
184
179
  def base_config
185
180
  @base_config ||= DEFAULTS.merge(@config).except(:makara)
186
181
  end
187
182
 
188
-
189
183
  def recursive_sort(thing)
190
184
  return thing.to_s unless thing.include?(Enumerable)
191
185
 
@@ -194,10 +188,8 @@ module Makara
194
188
  end
195
189
 
196
190
  thing.sort_by(&:to_s)
197
-
198
191
  end
199
192
 
200
-
201
193
  def sanitize_id(id)
202
194
  return if id.nil? || id.empty?
203
195
 
@@ -7,7 +7,6 @@ require 'active_support/core_ext/hash/keys'
7
7
 
8
8
  module Makara
9
9
  class ConnectionWrapper
10
-
11
10
  attr_accessor :initial_error, :config
12
11
 
13
12
  # invalid queries caused by connections switching that needs to be replaced
@@ -16,7 +15,7 @@ module Makara
16
15
  def initialize(proxy, connection, config)
17
16
  @config = config.symbolize_keys
18
17
  @connection = connection
19
- @proxy = proxy
18
+ @proxy = proxy
20
19
 
21
20
  if connection.nil?
22
21
  _makara_blacklist!
@@ -35,11 +34,19 @@ module Makara
35
34
  @config[:name]
36
35
  end
37
36
 
37
+ def _makara_shard_id
38
+ @config[:shard_id]
39
+ end
40
+
38
41
  # has this node been blacklisted?
39
42
  def _makara_blacklisted?
40
43
  @blacklisted_until.present? && @blacklisted_until.to_i > Time.now.to_i
41
44
  end
42
45
 
46
+ def _makara_in_transaction?
47
+ @connection && @connection.open_transactions > 0
48
+ end
49
+
43
50
  # blacklist this node for @config[:blacklist_duration] seconds
44
51
  def _makara_blacklist!
45
52
  @connection.disconnect! if @connection
@@ -95,28 +102,21 @@ module Makara
95
102
 
96
103
  # we want to forward all private methods, since we could have kicked out from a private scenario
97
104
  def method_missing(m, *args, &block)
98
- if _makara_connection.respond_to?(m)
99
- _makara_connection.public_send(m, *args, &block)
100
- else # probably private method
101
- _makara_connection.__send__(m, *args, &block)
102
- end
105
+ _makara_connection.send(m, *args, &block)
103
106
  end
104
107
 
108
+ ruby2_keywords :method_missing if Module.private_method_defined?(:ruby2_keywords)
105
109
 
106
- class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
107
- def respond_to#{RUBY_VERSION.to_s =~ /^1.8/ ? nil : '_missing'}?(m, include_private = false)
108
- _makara_connection.respond_to?(m, true)
109
- end
110
- RUBY_EVAL
111
-
110
+ def respond_to_missing?(m, include_private = false)
111
+ _makara_connection.respond_to?(m, true)
112
+ end
112
113
 
113
114
  protected
114
115
 
115
116
  # once the underlying connection is present we must evaluate extra functionality into it.
116
117
  # all extra functionality is in the format of _makara*
117
118
  def _makara_decorate_connection(con)
118
-
119
- extension = %Q{
119
+ extension = <<~RUBY
120
120
  # the proxy object controlling this connection
121
121
  def _makara
122
122
  @_makara
@@ -140,31 +140,52 @@ module Makara
140
140
  def _makara_name
141
141
  #{@config[:name].inspect}
142
142
  end
143
- }
143
+ RUBY
144
+
145
+ args = RUBY_VERSION >= "3.0.0" ? "..." : "*args, &block"
144
146
 
145
147
  # Each method the Makara::Proxy needs to hijack should be redefined in the underlying connection.
146
148
  # The new definition should allow for the proxy to intercept the invocation if required.
147
149
  @proxy.class.hijack_methods.each do |meth|
148
- extension << %Q{
149
- def #{meth}(*args, &block)
150
+ method_call = RUBY_VERSION >= "3.0.0" ? "public_send(#{meth.inspect}, ...)" : "#{meth}(*args, &block)"
151
+
152
+ extension << <<~RUBY
153
+ def #{meth}(#{args})
150
154
  _makara_hijack do |proxy|
151
155
  if proxy
152
- proxy.#{meth}(*args, &block)
156
+ proxy.#{method_call}
153
157
  else
154
158
  super
155
159
  end
156
160
  end
157
161
  end
158
- }
162
+ RUBY
163
+ end
164
+
165
+ # Control methods must always be passed to the
166
+ # Makara::Proxy control object for handling (typically
167
+ # related to ActiveRecord connection pool management)
168
+ @proxy.class.control_methods.each do |meth|
169
+ method_call = RUBY_VERSION >= "3.0.0" ? "public_send(#{meth.inspect}, ...)" : "#{meth}(*args=args, block)"
170
+
171
+ extension << <<~RUBY
172
+ def #{meth}(#{args})
173
+ proxy = _makara
174
+ if proxy
175
+ proxy.control.#{method_call}
176
+ else
177
+ super # Only if we are not wrapped any longer
178
+ end
179
+ end
180
+ RUBY
159
181
  end
160
182
 
161
183
  # extend the instance
162
- con.instance_eval(extension)
184
+ con.instance_eval(extension, __FILE__, __LINE__ + 1)
163
185
  # set the makara context
164
186
  con._makara = @proxy
165
187
 
166
188
  con._makara
167
189
  end
168
-
169
190
  end
170
191
  end
@@ -123,6 +123,7 @@ module Makara
123
123
  end
124
124
 
125
125
  protected
126
+
126
127
  def current
127
128
  fetch(:makara_current_context) { new({}) }
128
129
  end
data/lib/makara/cookie.rb CHANGED
@@ -22,6 +22,7 @@ module Makara
22
22
  end
23
23
 
24
24
  private
25
+
25
26
  # Pairs of {proxy_id}:{timestamp}, separated by "|"
26
27
  # proxy_id1:1518270031.3132212|proxy_id2:1518270030.313232 ..
27
28
  def parse(cookie_string)
@@ -4,36 +4,27 @@
4
4
 
5
5
  module Makara
6
6
  class ErrorHandler
7
-
8
-
9
7
  def handle(connection)
10
8
  yield
11
-
12
9
  rescue Exception => e
13
-
14
10
  if e.is_a?(Makara::Errors::MakaraError)
15
11
  harshly(e)
16
12
  else
17
13
  gracefully(connection, e)
18
14
  end
19
-
20
15
  end
21
16
 
22
-
23
17
  protected
24
18
 
25
-
26
19
  def gracefully(connection, e)
27
20
  err = Makara::Errors::BlacklistConnection.new(connection, e)
28
21
  ::Makara::Logging::Logger.log("Gracefully handling: #{err}")
29
22
  raise err
30
23
  end
31
24
 
32
-
33
25
  def harshly(e)
34
26
  ::Makara::Logging::Logger.log("Harshly handling: #{e}\n#{e.backtrace.join("\n\t")}")
35
27
  raise e
36
28
  end
37
-
38
29
  end
39
30
  end
@@ -1,13 +1,11 @@
1
1
  module Makara
2
2
  module Errors
3
3
  class AllConnectionsBlacklisted < MakaraError
4
-
5
4
  def initialize(pool, errors)
6
5
  errors = [*errors]
7
6
  messages = errors.empty? ? 'No error details' : errors.map(&:message).join(' -> ')
8
7
  super "[Makara/#{pool.role}] All connections are blacklisted -> " + messages
9
8
  end
10
-
11
9
  end
12
10
  end
13
11
  end
@@ -1,14 +1,12 @@
1
1
  module Makara
2
2
  module Errors
3
3
  class BlacklistConnection < MakaraError
4
-
5
4
  attr_reader :original_error
6
5
 
7
6
  def initialize(connection, error)
8
7
  @original_error = error
9
8
  super "[Makara/#{connection._makara_name}] #{error.message}"
10
9
  end
11
-
12
10
  end
13
11
  end
14
12
  end
@@ -0,0 +1,12 @@
1
+ module Makara
2
+ module Errors
3
+ class BlacklistedWhileInTransaction < MakaraError
4
+ attr_reader :role
5
+
6
+ def initialize(role)
7
+ @role = role
8
+ super "[Makara] Blacklisted while in transaction in the #{role} pool"
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ module Makara
2
+ module Errors
3
+ class InvalidShard < MakaraError
4
+ attr_reader :role
5
+ attr_reader :shard_id
6
+
7
+ def initialize(role, shard_id)
8
+ @role = role
9
+ @shard_id = shard_id
10
+ super "[Makara] Invalid shard_id #{shard_id} for the #{role} pool"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,7 +1,6 @@
1
1
  module Makara
2
2
  module Errors
3
3
  class MakaraError < ::ActiveRecord::ActiveRecordError
4
-
5
4
  end
6
5
  end
7
6
  end
@@ -1,14 +1,12 @@
1
1
  module Makara
2
2
  module Errors
3
3
  class NoConnectionsAvailable < MakaraError
4
-
5
4
  attr_reader :role
6
5
 
7
6
  def initialize(role)
8
7
  @role = role
9
8
  super "[Makara] No connections are available in the #{role} pool"
10
9
  end
11
-
12
10
  end
13
11
  end
14
12
  end
@@ -1,9 +1,7 @@
1
1
  module Makara
2
2
  module Logging
3
3
  class Logger
4
-
5
4
  class << self
6
-
7
5
  def log(msg, format = :error)
8
6
  logger.send(format, "[Makara] #{msg}") if logger
9
7
  end
@@ -15,9 +13,7 @@ module Makara
15
13
  def logger=(l)
16
14
  @logger = l
17
15
  end
18
-
19
16
  end
20
-
21
17
  end
22
18
  end
23
19
  end