trellis-pg 0.3.1 → 0.4.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.
@@ -109,12 +109,10 @@ module Trellis
109
109
  # or below it; the wait clears on its own when that transaction ends. nil
110
110
  # when the build waits on nothing.
111
111
  #
112
- # version_handover is set while a process of another Trellis version is
113
- # live on the instance (a VersionHandover): this process stands by (it
114
- # stages, drains and builds nothing) and its state-changing calls raise
115
- # VersionHandoverError until the other version is gone. A process killed
116
- # without a clean shutdown keeps its row live, and so delays the takeover,
117
- # for at most 30 seconds. nil when none is.
112
+ # version_handover is set while another Trellis version owns the instance
113
+ # (a VersionHandover): this process stands by (it stages, drains and builds
114
+ # nothing) and its state-changing calls raise VersionHandoverError until
115
+ # this version claims the instance. nil when this version owns it.
118
116
  #
119
117
  # below_catalog_floor is set while this binary's version is below the
120
118
  # instance's catalog floor, the oldest Trellis version that can read its
@@ -123,6 +121,12 @@ module Trellis
123
121
  # state-changing calls raise VersionFloorError. Run a version at or above
124
122
  # the floor. nil when this binary meets it.
125
123
  #
124
+ # maintenance_refusals lists the ring and maintenance steps this process's
125
+ # staging worker is refused (RefusedSteps), such as retiring a drained
126
+ # segment without TRUNCATE. Nothing is paused for them; each clears itself
127
+ # once a pass of its step succeeds. Only the process that runs the staging
128
+ # worker reports them; empty otherwise.
129
+ #
126
130
  # join is set for a JOIN definition, aggregate or 1-1, and nil for any
127
131
  # other (a JoinStatus). It never holds anything up: it changes nothing
128
132
  # about the definition's status. It reports the estimated size of the
@@ -135,11 +139,15 @@ module Trellis
135
139
  # definition is defined, altered or rebuilt by a resume. Before rolling the
136
140
  # application back to an earlier Trellis release, drop each definition
137
141
  # whose floor is above that release, under the newer one.
142
+ #
143
+ # filter is the definition's WHERE, as definition text, when it is other
144
+ # than TRUE: the target holds only the source rows it is TRUE for. nil for
145
+ # an unfiltered definition.
138
146
  Status = Data.define(:status, :backfill_failure, :capture_wait, :capture_failure,
139
147
  :held_keys, :drain_failure, :source_index_gaps,
140
148
  :extreme_index_gaps, :correcting, :paused_upstream_columns,
141
149
  :build_wait_xid, :version_handover, :below_catalog_floor,
142
- :join, :floor) do
150
+ :maintenance_refusals, :join, :floor, :filter) do
143
151
  def self.from_native(hash)
144
152
  failure = hash[:backfill_failure]
145
153
  wait = hash[:capture_wait]
@@ -162,8 +170,10 @@ module Trellis
162
170
  build_wait_xid: hash.fetch(:build_wait_xid),
163
171
  version_handover: version_handover && VersionHandover.from_native(version_handover),
164
172
  below_catalog_floor: below_catalog_floor && BelowCatalogFloor.new(**below_catalog_floor),
173
+ maintenance_refusals: hash.fetch(:maintenance_refusals, []).map { |r| RefusedStep.new(**r) },
165
174
  join: join && JoinStatus.from_native(join),
166
- floor: hash.fetch(:floor))
175
+ floor: hash.fetch(:floor),
176
+ filter: hash[:filter])
167
177
  end
168
178
  end
169
179
 
@@ -238,25 +248,26 @@ module Trellis
238
248
  # carry them.
239
249
  JoinBuildStep = Data.define(:step, :side, :part, :leaf, :remaining, :position)
240
250
 
241
- # Another Trellis version is live on the instance, so this process changes
242
- # nothing until it is gone, as Trellis.status reports it: this binary's
243
- # version (this_version), the live processes holding it back (processes,
244
- # OtherProcesses), whether the staging-worker lock is held by a session no
245
- # registered process names, which is a staging worker of a release before
246
- # 0.2.0 (unregistered_staging_worker), and whether the migrations this
247
- # binary carries are pending (migrations_pending), in which case every
248
- # live process holds it back, whatever its version.
249
- VersionHandover = Data.define(:this_version, :processes, :unregistered_staging_worker,
250
- :migrations_pending) do
251
+ # Another Trellis version owns the instance, so this process changes
252
+ # nothing until this version claims it, as Trellis.status reports it: this
253
+ # binary's version (this_version), the owner's version (owner, nil while no
254
+ # version has claimed the instance), whether an explicit rollback set the
255
+ # owner (by_rollback), the owner's live processes (processes,
256
+ # OtherProcesses), and when a background process of this version claims
257
+ # the instance by itself (claims_at, a Time; nil while only this version's
258
+ # migrate does).
259
+ VersionHandover = Data.define(:this_version, :owner, :by_rollback, :processes, :claims_at) do
251
260
  def self.from_native(hash)
261
+ claims_at = hash.fetch(:claims_at_micros)
252
262
  new(this_version: hash.fetch(:this_version),
263
+ owner: hash.fetch(:owner),
264
+ by_rollback: hash.fetch(:by_rollback),
253
265
  processes: hash.fetch(:processes).map { |p| OtherProcess.new(**p) },
254
- unregistered_staging_worker: hash.fetch(:unregistered_staging_worker),
255
- migrations_pending: hash.fetch(:migrations_pending))
266
+ claims_at: claims_at && EpochMicros.to_time(claims_at))
256
267
  end
257
268
  end
258
269
 
259
- # A live process of another Trellis version, in a VersionHandover: its
270
+ # A live process of the owner's Trellis version, in a VersionHandover: its
260
271
  # worker id (worker_id, trellis-<host>-<pid>-<random>) and its binary's
261
272
  # version (version, nil for a release before 0.2.0, which records none).
262
273
  OtherProcess = Data.define(:worker_id, :version)
@@ -266,6 +277,13 @@ module Trellis
266
277
  # catalog records (floor) and this binary's version (this_version).
267
278
  BelowCatalogFloor = Data.define(:floor, :this_version)
268
279
 
280
+ # A ring or maintenance step the staging worker's role is refused, in a
281
+ # Status's maintenance_refusals: the step (step), the role (role), the table
282
+ # or sequence refused (object), the privileges the role lacks on it
283
+ # (missing) and Postgres's message (error). role and object are nil, and
284
+ # missing is empty, when they couldn't be read. Grant what it names.
285
+ RefusedStep = Data.define(:step, :role, :object, :missing, :error)
286
+
269
287
  # A node of a partitioned source a definition is being corrected for, as
270
288
  # Trellis.status reports it: the schema-qualified node (node), when the
271
289
  # staging worker queued its correction (since, a Time), and the keys still
@@ -538,11 +556,13 @@ module Trellis
538
556
  Divergence = Data.define(:kind, :key, :column, :persisted, :recomputed, :table, :detail)
539
557
 
540
558
  # The configuration this process's handle connected with, as Trellis.config
541
- # returns it: connect's schema and target_schema, and the handle's call pool
542
- # as it was built: max_call_pool_size and call_pool_wait_timeout_ms.
559
+ # returns it: connect's schema, target_schema and claim_grace_period_ms, and
560
+ # the handle's call pool as it was built: max_call_pool_size and
561
+ # call_pool_wait_timeout_ms.
543
562
  #
544
563
  # Not the url: a connection string can carry a password, and a Config is
545
564
  # the kind of value that ends up in a log line whole. The caller already
546
565
  # has the url it connected with.
547
- Config = Data.define(:schema, :target_schema, :max_call_pool_size, :call_pool_wait_timeout_ms)
566
+ Config = Data.define(:schema, :target_schema, :max_call_pool_size, :call_pool_wait_timeout_ms,
567
+ :claim_grace_period_ms)
548
568
  end
@@ -3,5 +3,5 @@
3
3
  module Trellis
4
4
  # The trellis-pg gem's version: the Trellis release's, which a
5
5
  # `v<VERSION>` tag releases (docs/releasing.md).
6
- VERSION = "0.3.1"
6
+ VERSION = "0.4.0"
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trellis-pg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Salesforce
@@ -41,14 +41,15 @@ files:
41
41
  - ext/trellis_ruby/src/lib.rs
42
42
  - lib/trellis/error.rb
43
43
  - lib/trellis/instance.rb
44
+ - lib/trellis/leave_task.rb
44
45
  - lib/trellis/log_bridge.rb
45
46
  - lib/trellis/metrics.rb
46
47
  - lib/trellis/migration.rb
47
48
  - lib/trellis/pg.rb
48
49
  - lib/trellis/railtie.rb
49
- - lib/trellis/release_check.rb
50
50
  - lib/trellis/schema_dump.rb
51
51
  - lib/trellis/target.rb
52
+ - lib/trellis/task_hooks.rb
52
53
  - lib/trellis/test_support.rb
53
54
  - lib/trellis/values.rb
54
55
  - lib/trellis/version.rb
@@ -1,165 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "open3"
4
- require "pathname"
5
-
6
- module Trellis
7
- # A static check for CI: does this release both change the trellis-pg
8
- # gem's version and add a migration that uses Trellis?
9
- #
10
- # Such a release fails where migrations run before the old processes stop
11
- # (a release phase, a pre-deploy job): while a worker of the old version is
12
- # live, a define, ALTER, PAUSE, RESUME or DROP from the new version refuses
13
- # with Trellis::VersionHandoverError (docs/embedding.md, "Rolling
14
- # deploys"). The remedy is to ship the version change by itself and the
15
- # migrations in the next release, and this check says so on the pull
16
- # request instead of in the deploy.
17
- #
18
- # result = Trellis::ReleaseCheck.call(root: Rails.root, base: "origin/main")
19
- # abort result.message unless result.ok?
20
- #
21
- # `rails trellis:release_check BASE=origin/main` runs it in a Rails app.
22
- #
23
- # It reads git and files only: no database, no Trellis handle, and none of
24
- # the gem's extension. It compares the working tree under `root` with its
25
- # merge base against `base`, so a branch that is behind `base` is judged by
26
- # what the branch itself changes, and uncommitted work counts.
27
- #
28
- # It sees migrations only. A state-changing call from a deploy script or a
29
- # console isn't in any file it reads; the engine's refusal covers those.
30
- module ReleaseCheck
31
- GEM_NAME = "trellis-pg"
32
-
33
- # The check couldn't run: no base ref, not a git checkout, or a base
34
- # this clone has no merge base with (a shallow clone, usually).
35
- class Error < StandardError; end
36
-
37
- # What the check found. `base` is the merge-base commit it compared with,
38
- # the versions are trellis-pg's in the lockfile there and in the working
39
- # tree (nil where the lockfile doesn't list the gem), and `migrations` are
40
- # the migrations added since `base` that mention Trellis, relative to
41
- # `root`.
42
- Result = Data.define(:base, :old_version, :new_version, :migrations) do
43
- # Any difference counts, a patch release and a downgrade too: Trellis
44
- # versions match exactly, so every change of release is a handover. A
45
- # release that adds the gem has no old worker to hand over from.
46
- def version_changed?
47
- !old_version.nil? && !new_version.nil? && old_version != new_version
48
- end
49
-
50
- def ok?
51
- !(version_changed? && migrations.any?)
52
- end
53
-
54
- def message
55
- ok? ? ok_message : failure_message
56
- end
57
-
58
- private
59
-
60
- def ok_message
61
- version = if version_changed?
62
- "#{GEM_NAME} changes from #{old_version} to #{new_version}"
63
- else
64
- "#{GEM_NAME} #{new_version || 'is not in the lockfile'}, unchanged"
65
- end
66
- "trellis:release_check: ok (#{version}; #{migrations.size} Trellis migration(s) added since #{base[0, 12]})"
67
- end
68
-
69
- def failure_message
70
- <<~MESSAGE.chomp
71
- trellis:release_check: this release changes #{GEM_NAME} from #{old_version} to #{new_version} and adds Trellis migrations:
72
- #{migrations.map { |path| " #{path}" }.join("\n")}
73
- While a worker of #{old_version} is still running, #{new_version} may not define, alter, pause, resume
74
- or drop a transform (Trellis::VersionHandoverError), so a deploy that migrates before the old
75
- worker stops fails at these migrations. Ship the #{GEM_NAME} change by itself, then these
76
- migrations in the next release.
77
- MESSAGE
78
- end
79
- end
80
-
81
- class << self
82
- # Runs the check for the app at `root` against the git ref `base`: the
83
- # branch a pull request targets, or the last deployed ref for a release
84
- # of several merged pull requests. `migrations_paths` and `lockfile` are
85
- # relative to `root` (absolute paths under it work too).
86
- #
87
- # Returns a Result; raises Error when the check can't run.
88
- def call(root:, base:, migrations_paths: ["db/migrate"], lockfile: "Gemfile.lock")
89
- root = Pathname(root).expand_path
90
- base = base.to_s.strip
91
- if base.empty?
92
- raise Error, "set BASE to the git ref this release is compared with: the branch a pull " \
93
- "request targets (BASE=origin/main), or the last deployed ref"
94
- end
95
-
96
- # Never an option to git, whatever the environment variable held.
97
- raise Error, "#{base.inspect} is not a git ref" if base.start_with?("-")
98
-
99
- merge_base = merge_base(root, base)
100
- lockfile = relative(root, lockfile)
101
- Result.new(
102
- base: merge_base,
103
- old_version: locked_version(git(root, "show", "#{merge_base}:./#{lockfile}", allow_failure: true)),
104
- new_version: locked_version(read(root.join(lockfile))),
105
- migrations: added_trellis_migrations(root, merge_base, migrations_paths.map { |path| relative(root, path) })
106
- )
107
- end
108
-
109
- private
110
-
111
- def merge_base(root, base)
112
- git(root, "rev-parse", "--is-inside-work-tree")
113
- unless git(root, "rev-parse", "--verify", "--quiet", "#{base}^{commit}", allow_failure: true)
114
- raise Error, "#{base} is not a commit this clone knows: fetch it first"
115
- end
116
-
117
- git(root, "merge-base", base, "HEAD", allow_failure: true)&.strip or
118
- raise Error, "this checkout has no merge base with #{base}: fetch more history " \
119
- "(a shallow clone has none; with actions/checkout, set fetch-depth: 0)"
120
- end
121
-
122
- # trellis-pg's version in a lockfile's text: its `specs:` entry, without
123
- # the platform a platform gem's entry carries (`0.2.0-x86_64-linux`).
124
- # nil for no text or no entry.
125
- def locked_version(text)
126
- text&.[](/^ {4}#{Regexp.escape(GEM_NAME)} \(([^)-]+)/, 1)
127
- end
128
-
129
- # The migrations under `paths` that the working tree has and
130
- # `merge_base` hasn't (committed, staged or untracked), and that
131
- # mention Trellis: the Trellis::Migration helpers, or a direct
132
- # Trellis.apply. An edited old migration doesn't run again, so only
133
- # added (and renamed) files count.
134
- def added_trellis_migrations(root, merge_base, paths)
135
- added = git(root, "diff", "--name-only", "-z", "--diff-filter=AR", "--relative", merge_base, "--", *paths)
136
- untracked = git(root, "ls-files", "-z", "--others", "--exclude-standard", "--", *paths)
137
- (added.split("\0") + untracked.split("\0"))
138
- .uniq
139
- .select { |path| path.end_with?(".rb") && read(root.join(path))&.match?(/\bTrellis\b/) }
140
- .sort
141
- end
142
-
143
- def read(path)
144
- path.file? ? path.read : nil
145
- end
146
-
147
- def relative(root, path)
148
- path = Pathname(path)
149
- (path.absolute? ? path.relative_path_from(root) : path).to_s
150
- end
151
-
152
- # git's stdout, run in `root`. A failing command raises Error, or
153
- # returns nil with allow_failure.
154
- def git(root, *args, allow_failure: false)
155
- out, err, status = Open3.capture3("git", *args, chdir: root.to_s)
156
- return out if status.success?
157
- return nil if allow_failure
158
-
159
- raise Error, "git #{args.first} failed in #{root}: #{err.strip}"
160
- rescue Errno::ENOENT => e
161
- raise Error, "can't run git in #{root}: #{e.message}"
162
- end
163
- end
164
- end
165
- end