switchman 3.2.1 → 4.0.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.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +15 -14
  3. data/db/migrate/20180828183945_add_default_shard_index.rb +1 -1
  4. data/db/migrate/20190114212900_add_unique_name_indexes.rb +10 -4
  5. data/lib/switchman/active_record/abstract_adapter.rb +4 -2
  6. data/lib/switchman/active_record/associations.rb +89 -49
  7. data/lib/switchman/active_record/attribute_methods.rb +72 -34
  8. data/lib/switchman/active_record/base.rb +145 -27
  9. data/lib/switchman/active_record/calculations.rb +96 -49
  10. data/lib/switchman/active_record/connection_handler.rb +18 -0
  11. data/lib/switchman/active_record/connection_pool.rb +24 -3
  12. data/lib/switchman/active_record/database_configurations.rb +37 -15
  13. data/lib/switchman/active_record/finder_methods.rb +44 -14
  14. data/lib/switchman/active_record/log_subscriber.rb +11 -5
  15. data/lib/switchman/active_record/migration.rb +45 -3
  16. data/lib/switchman/active_record/pending_migration_connection.rb +17 -0
  17. data/lib/switchman/active_record/persistence.rb +30 -0
  18. data/lib/switchman/active_record/postgresql_adapter.rb +11 -10
  19. data/lib/switchman/active_record/predicate_builder.rb +2 -2
  20. data/lib/switchman/active_record/query_cache.rb +49 -20
  21. data/lib/switchman/active_record/query_methods.rb +192 -135
  22. data/lib/switchman/active_record/relation.rb +28 -13
  23. data/lib/switchman/active_record/spawn_methods.rb +2 -2
  24. data/lib/switchman/active_record/statement_cache.rb +2 -2
  25. data/lib/switchman/active_record/tasks/database_tasks.rb +6 -1
  26. data/lib/switchman/active_record/test_fixtures.rb +26 -16
  27. data/lib/switchman/active_support/cache.rb +9 -4
  28. data/lib/switchman/arel.rb +34 -18
  29. data/lib/switchman/call_super.rb +2 -2
  30. data/lib/switchman/database_server.rb +69 -31
  31. data/lib/switchman/default_shard.rb +14 -3
  32. data/lib/switchman/engine.rb +29 -22
  33. data/lib/switchman/environment.rb +2 -2
  34. data/lib/switchman/errors.rb +13 -0
  35. data/lib/switchman/guard_rail/relation.rb +1 -1
  36. data/lib/switchman/parallel.rb +6 -6
  37. data/lib/switchman/r_spec_helper.rb +12 -11
  38. data/lib/switchman/shard.rb +180 -68
  39. data/lib/switchman/sharded_instrumenter.rb +3 -3
  40. data/lib/switchman/shared_schema_cache.rb +11 -0
  41. data/lib/switchman/standard_error.rb +4 -0
  42. data/lib/switchman/test_helper.rb +2 -2
  43. data/lib/switchman/version.rb +1 -1
  44. data/lib/switchman.rb +27 -15
  45. data/lib/tasks/switchman.rake +96 -60
  46. metadata +35 -45
@@ -1,40 +1,30 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # In rails 7.0+ if you have only 1 db in the env it doesn't try to do explicit activation
4
- # (and for rails purposes we only have one db per env because each database server is a separate env)
5
- if Rails.version < '7.0'
6
- task_prefix = ::Rake::Task.task_defined?('app:db:migrate') ? 'app:db' : 'db'
7
- ::Rake::Task["#{task_prefix}:migrate"].clear_actions.enhance do
8
- ::ActiveRecord::Tasks::DatabaseTasks.migrate
9
- # Ensure this doesn't blow up when running inside the dummy app
10
- Rake::Task["#{task_prefix}:_dump"].invoke
11
- end
12
- end
13
-
14
3
  module Switchman
15
4
  module Rake
16
- def self.filter_database_servers(&block)
17
- chain = filter_database_servers_chain # use a local variable so that the current chain is closed over in the following lambda
18
- @filter_database_servers_chain = ->(servers) { block.call(servers, chain) }
5
+ def self.filter_database_servers
6
+ # use a local variable so that the current chain is closed over in the following lambda
7
+ chain = filter_database_servers_chain
8
+ @filter_database_servers_chain = ->(servers) { yield(servers, chain) }
19
9
  end
20
10
 
21
11
  def self.scope(base_scope = Shard,
22
- database_server: ENV['DATABASE_SERVER'],
23
- shard: ENV['SHARD'])
12
+ database_server: ENV.fetch("DATABASE_SERVER", nil),
13
+ shard: ENV.fetch("SHARD", nil))
24
14
  servers = DatabaseServer.all
25
15
 
26
16
  if database_server
27
17
  servers = database_server
28
- if servers.first == '-'
18
+ if servers.first == "-"
29
19
  negative = true
30
20
  servers = servers[1..]
31
21
  end
32
- servers = servers.split(',')
33
- open = servers.delete('open')
22
+ servers = servers.split(",")
23
+ open = servers.delete("open")
34
24
 
35
- servers = servers.map { |server| DatabaseServer.find(server) }.compact
25
+ servers = servers.filter_map { |server| DatabaseServer.find(server) }
36
26
  if open
37
- open_servers = DatabaseServer.all.select { |server| server.config[:open] }
27
+ open_servers = DatabaseServer.select { |server| server.config[:open] }
38
28
  servers.concat(open_servers)
39
29
  servers << DatabaseServer.find(nil) if open_servers.empty?
40
30
  servers.uniq!
@@ -42,9 +32,22 @@ module Switchman
42
32
  servers = DatabaseServer.all - servers if negative
43
33
  end
44
34
 
35
+ ENV["REGION"]&.split(",")&.each do |region|
36
+ method = :select!
37
+ if region[0] == "-"
38
+ method = :reject!
39
+ region = region[1..]
40
+ end
41
+ if region == "self"
42
+ servers.send(method, &:in_current_region?)
43
+ else
44
+ servers.send(method) { |server| server.in_region?(region) }
45
+ end
46
+ end
47
+
45
48
  servers = filter_database_servers_chain.call(servers)
46
49
 
47
- scope = base_scope.order(::Arel.sql('database_server_id IS NOT NULL, database_server_id, id'))
50
+ scope = base_scope.order(::Arel.sql("database_server_id IS NOT NULL, database_server_id, id"))
48
51
  if servers != DatabaseServer.all
49
52
  database_server_ids = servers.map(&:id)
50
53
  database_server_ids << nil if servers.include?(Shard.default.database_server)
@@ -57,36 +60,81 @@ module Switchman
57
60
  end
58
61
 
59
62
  def self.options
60
- { parallel: ENV['PARALLEL'].to_i }
63
+ { exception: (ENV["FAIL_FAST"] == "0") ? :defer : :raise, parallel: ENV["PARALLEL"].to_i }
61
64
  end
62
65
 
63
66
  # classes - an array or proc, to activate as the current shard during the
64
67
  # task.
65
68
  def self.shardify_task(task_name, classes: [::ActiveRecord::Base])
69
+ log_format = ENV.fetch("LOG_FORMAT", nil)
66
70
  old_task = ::Rake::Task[task_name]
67
71
  old_actions = old_task.actions.dup
68
72
  old_task.actions.clear
69
73
 
70
74
  old_task.enhance do |*task_args|
71
75
  if ::Rails.env.test?
72
- require 'switchman/test_helper'
76
+ require "switchman/test_helper"
73
77
  TestHelper.recreate_persistent_test_shards(dont_create: true)
74
78
  end
75
79
 
76
80
  ::GuardRail.activate(:deploy) do
77
81
  Shard.default.database_server.unguard do
78
82
  classes = classes.call if classes.respond_to?(:call)
79
- Shard.with_each_shard(scope, classes, **options) do
83
+
84
+ # We don't want the shard status messages to be wrapped using a custom log transfomer
85
+ original_stderr = $stderr
86
+ original_stdout = $stdout
87
+ output = if log_format == "json"
88
+ lambda { |msg|
89
+ JSON.dump(shard: Shard.current.id,
90
+ database_server: Shard.current.database_server.id,
91
+ type: "log",
92
+ message: msg)
93
+ }
94
+ else
95
+ nil
96
+ end
97
+ Shard.with_each_shard(scope, classes, output: output, **options) do
80
98
  shard = Shard.current
81
- puts "#{shard.id}: #{shard.description}"
99
+
100
+ if log_format == "json"
101
+ original_stdout.puts JSON.dump(
102
+ shard: shard.id,
103
+ database_server: shard.database_server.id,
104
+ type: "started"
105
+ )
106
+ else
107
+ original_stdout.puts "#{shard.id}: #{shard.description}"
108
+ end
82
109
 
83
110
  shard.database_server.unguard do
84
111
  old_actions.each { |action| action.call(*task_args) }
85
112
  end
113
+
114
+ if log_format == "json"
115
+ original_stdout.puts JSON.dump(
116
+ shard: shard.id,
117
+ database_server: shard.database_server.id,
118
+ type: "completed"
119
+ )
120
+ end
86
121
  nil
122
+ rescue => e
123
+ if log_format == "json"
124
+ original_stderr.puts JSON.dump(
125
+ shard: shard.id,
126
+ database_server: shard.database_server.id,
127
+ type: "failed",
128
+ message: e.full_message
129
+ )
130
+ end
131
+
132
+ raise
87
133
  end
88
134
  rescue => e
89
- warn "Exception from #{e.current_shard.id}: #{e.current_shard.description}:\n#{e.full_message}" if options[:parallel] != 0
135
+ if options[:parallel] != 0
136
+ warn "Exception from #{e.current_shard.id}: #{e.current_shard.description}:\n#{e.full_message}"
137
+ end
90
138
  raise
91
139
  end
92
140
  end
@@ -98,7 +146,7 @@ module Switchman
98
146
  end
99
147
 
100
148
  def self.shard_scope(scope, raw_shard_ids)
101
- raw_shard_ids = raw_shard_ids.split(',')
149
+ raw_shard_ids = raw_shard_ids.split(",")
102
150
 
103
151
  shard_ids = []
104
152
  negative_shard_ids = []
@@ -108,13 +156,13 @@ module Switchman
108
156
 
109
157
  raw_shard_ids.each do |id|
110
158
  case id
111
- when 'default'
159
+ when "default"
112
160
  shard_ids << Shard.default.id
113
- when '-default'
161
+ when "-default"
114
162
  negative_shard_ids << Shard.default.id
115
- when 'primary'
163
+ when "primary"
116
164
  shard_ids.concat(Shard.primary.pluck(:id))
117
- when '-primary'
165
+ when "-primary"
118
166
  negative_shard_ids.concat(Shard.primary.pluck(:id))
119
167
  when /^(-?)(\d+)?\.\.(\.)?(\d+)?$/
120
168
  negative, start, open, finish = $1.present?, $2, $3.present?, $4
@@ -122,8 +170,8 @@ module Switchman
122
170
 
123
171
  range = []
124
172
  range << "id>=#{start}" if start
125
- range << "id<#{'=' unless open}#{finish}" if finish
126
- (negative ? negative_ranges : ranges) << "(#{range.join(' AND ')})"
173
+ range << "id<#{"=" unless open}#{finish}" if finish
174
+ (negative ? negative_ranges : ranges) << "(#{range.join(" AND ")})"
127
175
  when /^-(\d+)$/
128
176
  negative_shard_ids << $1.to_i
129
177
  when /^\d+$/
@@ -151,21 +199,21 @@ module Switchman
151
199
  select = []
152
200
  if index != 1
153
201
  subscope = subscope.offset(per_chunk * (index - 1))
154
- select << 'MIN(id) AS min_id'
202
+ select << "MIN(id) AS min_id"
155
203
  end
156
204
  if index != denominator
157
205
  subscope = subscope.limit(per_chunk)
158
- select << 'MAX(id) AS max_id'
206
+ select << "MAX(id) AS max_id"
159
207
  end
160
208
 
161
- result = Shard.from(subscope).select(select.join(', ')).to_a.first
209
+ result = Shard.from(subscope).select(select.join(", ")).to_a.first
162
210
  range = case index
163
211
  when 1
164
- "id<=#{result['max_id']}"
212
+ "id<=#{result["max_id"]}"
165
213
  when denominator
166
- "id>=#{result['min_id']}"
214
+ "id>=#{result["min_id"]}"
167
215
  else
168
- "(id>=#{result['min_id']} AND id<=#{result['max_id']})"
216
+ "(id>=#{result["min_id"]} AND id<=#{result["max_id"]})"
169
217
  end
170
218
 
171
219
  (numerator.negative? ? negative_ranges : ranges) << range
@@ -186,16 +234,16 @@ module Switchman
186
234
 
187
235
  conditions = []
188
236
  positive_queries = []
189
- positive_queries << ranges.join(' OR ') unless ranges.empty?
237
+ positive_queries << ranges.join(" OR ") unless ranges.empty?
190
238
  unless shard_ids.empty?
191
- positive_queries << 'id IN (?)'
239
+ positive_queries << "id IN (?)"
192
240
  conditions << shard_ids
193
241
  end
194
- positive_query = positive_queries.join(' OR ')
242
+ positive_query = positive_queries.join(" OR ")
195
243
  scope = scope.where(positive_query, *conditions) unless positive_queries.empty?
196
244
 
197
- scope = scope.where("NOT (#{negative_ranges.join(' OR')})") unless negative_ranges.empty?
198
- scope = scope.where('id NOT IN (?)', negative_shard_ids) unless negative_shard_ids.empty?
245
+ scope = scope.where("NOT (#{negative_ranges.join(" OR")})") unless negative_ranges.empty?
246
+ scope = scope.where("id NOT IN (?)", negative_shard_ids) unless negative_shard_ids.empty?
199
247
  scope
200
248
  end
201
249
 
@@ -206,21 +254,9 @@ module Switchman
206
254
 
207
255
  module ActiveRecord
208
256
  module PostgreSQLDatabaseTasks
209
- def structure_dump(filename, extra_flags = nil)
210
- set_psql_env
211
- args = ['--schema-only', '--no-privileges', '--no-owner', '--file', filename]
212
- args.concat(Array(extra_flags)) if extra_flags
213
- shard = Shard.current.name
214
- serialized_search_path = shard
215
- args << "--schema=#{Shellwords.escape(shard)}"
216
-
217
- ignore_tables = ::ActiveRecord::SchemaDumper.ignore_tables
218
- args += ignore_tables.flat_map { |table| ['-T', table] } if ignore_tables.any?
219
-
220
- args << db_config.database
221
- run_cmd('pg_dump', args, 'dumping')
222
- remove_sql_header_comments(filename)
223
- File.open(filename, 'a') { |f| f << "SET search_path TO #{serialized_search_path};\n\n" }
257
+ def structure_dump(...)
258
+ ::ActiveRecord.dump_schemas = Switchman::Shard.current.name
259
+ super
224
260
  end
225
261
  end
226
262
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: switchman
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.1
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cody Cutrer
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2022-11-22 00:00:00.000000000 Z
13
+ date: 2025-02-12 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
@@ -18,20 +18,20 @@ dependencies:
18
18
  requirements:
19
19
  - - ">="
20
20
  - !ruby/object:Gem::Version
21
- version: 6.1.4
21
+ version: '7.0'
22
22
  - - "<"
23
23
  - !ruby/object:Gem::Version
24
- version: '7.1'
24
+ version: '7.2'
25
25
  type: :runtime
26
26
  prerelease: false
27
27
  version_requirements: !ruby/object:Gem::Requirement
28
28
  requirements:
29
29
  - - ">="
30
30
  - !ruby/object:Gem::Version
31
- version: 6.1.4
31
+ version: '7.0'
32
32
  - - "<"
33
33
  - !ruby/object:Gem::Version
34
- version: '7.1'
34
+ version: '7.2'
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: guardrail
37
37
  requirement: !ruby/object:Gem::Requirement
@@ -66,48 +66,34 @@ dependencies:
66
66
  requirements:
67
67
  - - ">="
68
68
  - !ruby/object:Gem::Version
69
- version: '6.1'
69
+ version: '7.0'
70
70
  - - "<"
71
71
  - !ruby/object:Gem::Version
72
- version: '7.1'
72
+ version: '7.2'
73
73
  type: :runtime
74
74
  prerelease: false
75
75
  version_requirements: !ruby/object:Gem::Requirement
76
76
  requirements:
77
77
  - - ">="
78
78
  - !ruby/object:Gem::Version
79
- version: '6.1'
79
+ version: '7.0'
80
80
  - - "<"
81
81
  - !ruby/object:Gem::Version
82
- version: '7.1'
82
+ version: '7.2'
83
83
  - !ruby/object:Gem::Dependency
84
- name: appraisal
84
+ name: debug
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: 2.3.0
89
+ version: '1.8'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: 2.3.0
97
- - !ruby/object:Gem::Dependency
98
- name: byebug
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- version: '0'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- version: '0'
96
+ version: '1.8'
111
97
  - !ruby/object:Gem::Dependency
112
98
  name: pg
113
99
  requirement: !ruby/object:Gem::Requirement
@@ -122,20 +108,6 @@ dependencies:
122
108
  - - "~>"
123
109
  - !ruby/object:Gem::Version
124
110
  version: '1.2'
125
- - !ruby/object:Gem::Dependency
126
- name: pry
127
- requirement: !ruby/object:Gem::Requirement
128
- requirements:
129
- - - ">="
130
- - !ruby/object:Gem::Version
131
- version: '0'
132
- type: :development
133
- prerelease: false
134
- version_requirements: !ruby/object:Gem::Requirement
135
- requirements:
136
- - - ">="
137
- - !ruby/object:Gem::Version
138
- version: '0'
139
111
  - !ruby/object:Gem::Dependency
140
112
  name: rake
141
113
  requirement: !ruby/object:Gem::Requirement
@@ -170,14 +142,14 @@ dependencies:
170
142
  requirements:
171
143
  - - "~>"
172
144
  - !ruby/object:Gem::Version
173
- version: '4.0'
145
+ version: '6.0'
174
146
  type: :development
175
147
  prerelease: false
176
148
  version_requirements: !ruby/object:Gem::Requirement
177
149
  requirements:
178
150
  - - "~>"
179
151
  - !ruby/object:Gem::Version
180
- version: '4.0'
152
+ version: '6.0'
181
153
  - !ruby/object:Gem::Dependency
182
154
  name: rubocop
183
155
  requirement: !ruby/object:Gem::Requirement
@@ -192,6 +164,20 @@ dependencies:
192
164
  - - "~>"
193
165
  - !ruby/object:Gem::Version
194
166
  version: '1.10'
167
+ - !ruby/object:Gem::Dependency
168
+ name: rubocop-inst
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: '1'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: '1'
195
181
  - !ruby/object:Gem::Dependency
196
182
  name: rubocop-rake
197
183
  requirement: !ruby/object:Gem::Requirement
@@ -255,6 +241,7 @@ files:
255
241
  - lib/switchman/active_record/attribute_methods.rb
256
242
  - lib/switchman/active_record/base.rb
257
243
  - lib/switchman/active_record/calculations.rb
244
+ - lib/switchman/active_record/connection_handler.rb
258
245
  - lib/switchman/active_record/connection_pool.rb
259
246
  - lib/switchman/active_record/database_configurations.rb
260
247
  - lib/switchman/active_record/database_configurations/database_config.rb
@@ -262,6 +249,7 @@ files:
262
249
  - lib/switchman/active_record/log_subscriber.rb
263
250
  - lib/switchman/active_record/migration.rb
264
251
  - lib/switchman/active_record/model_schema.rb
252
+ - lib/switchman/active_record/pending_migration_connection.rb
265
253
  - lib/switchman/active_record/persistence.rb
266
254
  - lib/switchman/active_record/postgresql_adapter.rb
267
255
  - lib/switchman/active_record/predicate_builder.rb
@@ -290,6 +278,7 @@ files:
290
278
  - lib/switchman/rails.rb
291
279
  - lib/switchman/shard.rb
292
280
  - lib/switchman/sharded_instrumenter.rb
281
+ - lib/switchman/shared_schema_cache.rb
293
282
  - lib/switchman/standard_error.rb
294
283
  - lib/switchman/test_helper.rb
295
284
  - lib/switchman/unsharded_record.rb
@@ -300,6 +289,7 @@ licenses:
300
289
  - MIT
301
290
  metadata:
302
291
  rubygems_mfa_required: 'true'
292
+ source_code_uri: https://github.com/instructure/switchman
303
293
  post_install_message:
304
294
  rdoc_options: []
305
295
  require_paths:
@@ -308,14 +298,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
308
298
  requirements:
309
299
  - - ">="
310
300
  - !ruby/object:Gem::Version
311
- version: '2.7'
301
+ version: '3.0'
312
302
  required_rubygems_version: !ruby/object:Gem::Requirement
313
303
  requirements:
314
304
  - - ">="
315
305
  - !ruby/object:Gem::Version
316
306
  version: '0'
317
307
  requirements: []
318
- rubygems_version: 3.1.6
308
+ rubygems_version: 3.2.33
319
309
  signing_key:
320
310
  specification_version: 4
321
311
  summary: Rails sharding magic