standard_flags 0.3.0 → 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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +188 -0
- data/README.md +531 -59
- data/conformance/README.md +152 -3
- data/conformance/legacy_env.json +7 -1
- data/conformance/schema/fixture.schema.json +390 -21
- data/conformance/schema/manifest.schema.json +347 -0
- data/conformance/upgrade/client_status_server.json +297 -0
- data/conformance/wire/client_state.json +2115 -2
- data/conformance/wire/client_status.json +359 -0
- data/conformance/wire/manifest_check.json +5713 -0
- data/lib/generators/standard_flags/install/templates/initializer.rb.tt +4 -1
- data/lib/generators/standard_flags/migrations/migrations_generator.rb +37 -9
- data/lib/generators/standard_flags/migrations/templates/create_standard_flags_compat_stats.rb.tt +17 -0
- data/lib/standard_flags/client_app.rb +229 -0
- data/lib/standard_flags/client_status/controller.rb +59 -0
- data/lib/standard_flags/client_status.rb +110 -0
- data/lib/standard_flags/config.rb +18 -1
- data/lib/standard_flags/controller.rb +110 -5
- data/lib/standard_flags/definition.rb +37 -35
- data/lib/standard_flags/diagnostics.rb +61 -3
- data/lib/standard_flags/env_pin.rb +5 -15
- data/lib/standard_flags/errors.rb +31 -0
- data/lib/standard_flags/evaluator.rb +1 -0
- data/lib/standard_flags/mcp/flags.rb +12 -1
- data/lib/standard_flags/open_feature/provider.rb +2 -1
- data/lib/standard_flags/parser.rb +0 -16
- data/lib/standard_flags/registry.rb +46 -4
- data/lib/standard_flags/runtime/active_record_store.rb +64 -0
- data/lib/standard_flags/runtime/compat_tracker.rb +228 -0
- data/lib/standard_flags/runtime/memory_store.rb +20 -0
- data/lib/standard_flags/runtime/models.rb +6 -0
- data/lib/standard_flags/runtime/store.rb +18 -0
- data/lib/standard_flags/runtime.rb +33 -3
- data/lib/standard_flags/stores/app_store.rb +172 -0
- data/lib/standard_flags/stores.rb +37 -0
- data/lib/standard_flags/tooling/kotlin.rb +319 -0
- data/lib/standard_flags/tooling/manifest.rb +401 -0
- data/lib/standard_flags/tooling/manifest_check.rb +355 -0
- data/lib/standard_flags/tooling.rb +150 -55
- data/lib/standard_flags/version.rb +1 -1
- data/lib/standard_flags.rb +3 -0
- data/lib/tasks/standard_flags.rake +47 -0
- metadata +15 -2
- data/conformance/legacy_exact.json +0 -90
|
@@ -28,6 +28,15 @@ module StandardFlags
|
|
|
28
28
|
"WHERE NOT EXISTS (SELECT 1 FROM standard_flags_state)".freeze
|
|
29
29
|
LOCK_STATE = "LOCK TABLE standard_flags_state IN SHARE ROW EXCLUSIVE MODE".freeze
|
|
30
30
|
MEMBERSHIP_INSERT_COLUMNS = %i[rule_id cohort_id unit unit_key value expires_at created_at updated_at].freeze
|
|
31
|
+
# Hits add up and the later hit wins. A row unused for longer than
|
|
32
|
+
# CompatTracker::IDLE before this hit takes the new oldest version
|
|
33
|
+
# (SET expressions read the row as it was). first_seen_at is never
|
|
34
|
+
# updated.
|
|
35
|
+
COMPAT_UPSERT = "hits = standard_flags_compat_stats.hits + EXCLUDED.hits, " \
|
|
36
|
+
"oldest_version = CASE WHEN standard_flags_compat_stats.last_hit_at < " \
|
|
37
|
+
"EXCLUDED.last_hit_at - make_interval(secs => #{Runtime::CompatTracker::IDLE}) " \
|
|
38
|
+
"THEN EXCLUDED.oldest_version ELSE standard_flags_compat_stats.oldest_version END, " \
|
|
39
|
+
"last_hit_at = GREATEST(standard_flags_compat_stats.last_hit_at, EXCLUDED.last_hit_at)".freeze
|
|
31
40
|
|
|
32
41
|
# nil when standard_flags_state has no row yet: there is no revision,
|
|
33
42
|
# and the cache reloads at every check until a write creates one.
|
|
@@ -173,6 +182,39 @@ module StandardFlags
|
|
|
173
182
|
end
|
|
174
183
|
end
|
|
175
184
|
|
|
185
|
+
# One upsert that adds the hits and keeps the later hit, then, for each
|
|
186
|
+
# row whose oldest version may move down, a compare-and-set update
|
|
187
|
+
# (versions compare as SemVer, which SQL can't). Skipped (nil) inside
|
|
188
|
+
# an open transaction, like touch_evaluated.
|
|
189
|
+
def record_compat(stats)
|
|
190
|
+
return nil if Models::Record.connection_pool.active_connection&.transaction_open?
|
|
191
|
+
return 0 if stats.empty?
|
|
192
|
+
|
|
193
|
+
compat_table!
|
|
194
|
+
guard("compat tracking") do
|
|
195
|
+
Models::Record.with_connection do
|
|
196
|
+
now = Time.now.utc
|
|
197
|
+
rows = stats.map do |stat|
|
|
198
|
+
{ key: stat.key.to_s, platform: stat.platform, hits: stat.hits, oldest_version: stat.oldest_version,
|
|
199
|
+
last_hit_at: stat.last_hit_at, first_seen_at: now }
|
|
200
|
+
end
|
|
201
|
+
Models::CompatStat.upsert_all(rows, unique_by: %i[key platform], on_duplicate: Arel.sql(COMPAT_UPSERT))
|
|
202
|
+
stats.each { |stat| lower_oldest(stat) if stat.oldest_version }
|
|
203
|
+
end
|
|
204
|
+
stats.size
|
|
205
|
+
end
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
def compat_stats(keys = nil)
|
|
209
|
+
read("compat stats") do
|
|
210
|
+
scope = Models::CompatStat.order(:key, :platform)
|
|
211
|
+
scope = scope.where(key: keys) if keys
|
|
212
|
+
scope.pluck(:key, :platform, :hits, :oldest_version, :last_hit_at, :first_seen_at).map do |key, platform, hits, oldest, last, first|
|
|
213
|
+
CompatStat.new(key: key, platform: platform, hits: hits, oldest_version: oldest, last_hit_at: last, first_seen_at: first)
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
|
|
176
218
|
# One write's view of the tables. Rows are locked FOR UPDATE as they
|
|
177
219
|
# are read, and held until the write ends.
|
|
178
220
|
#
|
|
@@ -384,6 +426,28 @@ module StandardFlags
|
|
|
384
426
|
model.where(id: ids.uniq).update_all("member_count = (#{count})")
|
|
385
427
|
end
|
|
386
428
|
|
|
429
|
+
# A clear error for an app that turned tracking on without running the
|
|
430
|
+
# compat stats migration (upsert_all would complain about an index).
|
|
431
|
+
def compat_table!
|
|
432
|
+
exists = guard("compat tracking") { Models::CompatStat.table_exists? }
|
|
433
|
+
return if exists
|
|
434
|
+
|
|
435
|
+
raise StoreError.new("compat tracking failed: standard_flags_compat_stats is missing; run " \
|
|
436
|
+
"bin/rails g standard_flags:migrations and migrate", cause_class: "StandardFlags::MissingCompatTable")
|
|
437
|
+
end
|
|
438
|
+
|
|
439
|
+
# Versions only ever move down, so a lost race is retried a few times
|
|
440
|
+
# and then left to the next flush's version, which is no newer.
|
|
441
|
+
def lower_oldest(stat)
|
|
442
|
+
row = Models::CompatStat.where(key: stat.key.to_s, platform: stat.platform)
|
|
443
|
+
3.times do
|
|
444
|
+
current = row.pick(:oldest_version)
|
|
445
|
+
return if current == stat.oldest_version || CompatStats.older(current, stat.oldest_version) == current
|
|
446
|
+
|
|
447
|
+
return if row.where(oldest_version: current).update_all(oldest_version: stat.oldest_version).positive?
|
|
448
|
+
end
|
|
449
|
+
end
|
|
450
|
+
|
|
387
451
|
def guard(what)
|
|
388
452
|
yield
|
|
389
453
|
rescue StandardError => e
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
module StandardFlags
|
|
2
|
+
module Runtime
|
|
3
|
+
# One row of standard_flags_compat_stats: how often a compat flag was
|
|
4
|
+
# evaluated by clients on one platform, the oldest client version seen
|
|
5
|
+
# there (a String, nil when none was known) and when it was last hit.
|
|
6
|
+
# The platform is the client's, "unknown" for one this app doesn't
|
|
7
|
+
# recognise, or "none" when the evaluation carried no client.
|
|
8
|
+
#
|
|
9
|
+
# The row whose platform is "*" (CompatTracker::TRACKING) is the flag's
|
|
10
|
+
# tracking marker, written with no hits by every successful write: its
|
|
11
|
+
# `first_seen_at` says since when the counts can be trusted, and its
|
|
12
|
+
# `last_hit_at` (read as `last_flushed_at`) when a write last succeeded.
|
|
13
|
+
CompatStat = Data.define(:key, :platform, :hits, :oldest_version, :last_hit_at, :first_seen_at) do
|
|
14
|
+
def initialize(key:, platform:, hits:, oldest_version:, last_hit_at:, first_seen_at: nil)
|
|
15
|
+
super
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def marker?
|
|
19
|
+
platform == CompatTracker::TRACKING
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# When a write last succeeded, for a tracking marker; nil otherwise.
|
|
23
|
+
def last_flushed_at
|
|
24
|
+
last_hit_at if marker?
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def to_h
|
|
28
|
+
{ key: key, platform: platform, hits: hits, oldest_version: oldest_version, last_hit_at: last_hit_at&.utc&.iso8601 }
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# How stores combine a stored CompatStat with new counts.
|
|
33
|
+
#
|
|
34
|
+
# @api private
|
|
35
|
+
module CompatStats
|
|
36
|
+
module_function
|
|
37
|
+
|
|
38
|
+
# `stored` (or nil) plus `added`: hits add up, the later hit wins and
|
|
39
|
+
# `first_seen_at` is the stored one (or `now` for a new row). The
|
|
40
|
+
# older version wins, unless the row had gone unused for
|
|
41
|
+
# CompatTracker::IDLE before this hit, in which case the new oldest
|
|
42
|
+
# version replaces it: an old spoofed or long-gone version doesn't
|
|
43
|
+
# stick forever. The key is a String.
|
|
44
|
+
def merge(stored, added, now: Time.now.utc)
|
|
45
|
+
return added.with(key: added.key.to_s, first_seen_at: added.first_seen_at || now) if stored.nil?
|
|
46
|
+
|
|
47
|
+
oldest =
|
|
48
|
+
if idle_before?(stored.last_hit_at, added.last_hit_at) then added.oldest_version
|
|
49
|
+
else older(stored.oldest_version, added.oldest_version)
|
|
50
|
+
end
|
|
51
|
+
CompatStat.new(key: stored.key.to_s, platform: stored.platform, hits: stored.hits + added.hits,
|
|
52
|
+
oldest_version: oldest, last_hit_at: [ stored.last_hit_at, added.last_hit_at ].compact.max,
|
|
53
|
+
first_seen_at: stored.first_seen_at)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def idle_before?(stored_hit, new_hit)
|
|
57
|
+
!stored_hit.nil? && !new_hit.nil? && stored_hit < new_hit - CompatTracker::IDLE
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# The older of two version Strings; one that doesn't parse loses.
|
|
61
|
+
def older(first, second)
|
|
62
|
+
a = Version.parse(first)
|
|
63
|
+
b = Version.parse(second)
|
|
64
|
+
return (a ? first : second) if a.nil? || b.nil?
|
|
65
|
+
|
|
66
|
+
b < a ? second : first
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# Write-behind counts for compat windows (`compat :k, until_clients:`),
|
|
71
|
+
# like EvaluationTracker: each evaluation of such a flag adds a hit and
|
|
72
|
+
# the client's version to an in-memory table, and nothing is written
|
|
73
|
+
# during the evaluation. When a unit of work ends, at most once per
|
|
74
|
+
# `config.evaluation_tracking_interval` per process (failed writes
|
|
75
|
+
# included), the counts are added to standard_flags_compat_stats with a
|
|
76
|
+
# tracking marker per compat window, refreshed by every write that
|
|
77
|
+
# succeeds (one write per interval even without hits, so a quiet
|
|
78
|
+
# window still shows tracking is healthy).
|
|
79
|
+
# It is best-effort: a failed write is reported once per process, emits
|
|
80
|
+
# `compat_tracking_failed` once per interval, and the counts are kept
|
|
81
|
+
# for the next try (the table holds one entry per flag and platform, so
|
|
82
|
+
# it stays small).
|
|
83
|
+
#
|
|
84
|
+
# Hits count from every evaluation; the oldest version only from
|
|
85
|
+
# clients on a platform in `config.client_platforms`. Versions come from
|
|
86
|
+
# client headers, which anyone can send, so the oldest version is a
|
|
87
|
+
# hint, not proof.
|
|
88
|
+
#
|
|
89
|
+
# Operator reads (MCP tools, diagnostics) don't count, so they can't
|
|
90
|
+
# keep a dead shim looking alive. A forked child starts with no
|
|
91
|
+
# pending counts (its parent writes them). `config.track_compat` (off by
|
|
92
|
+
# default) turns it on; it needs runtime rules and the compat stats
|
|
93
|
+
# migration.
|
|
94
|
+
class CompatTracker
|
|
95
|
+
UNTRACKED = :standard_flags_compat_untracked
|
|
96
|
+
NO_PLATFORM = "none".freeze
|
|
97
|
+
# The platform of a flag's tracking marker row.
|
|
98
|
+
TRACKING = "*".freeze
|
|
99
|
+
# How long a flag must go unused (and tracking must have run) before
|
|
100
|
+
# flags:stale calls its window removable.
|
|
101
|
+
IDLE = 14 * 86_400
|
|
102
|
+
# How recent the last successful write must be for flags:stale to
|
|
103
|
+
# trust that missing hits mean no use.
|
|
104
|
+
HEALTHY = 2 * 86_400
|
|
105
|
+
|
|
106
|
+
Pending = Struct.new(:hits, :oldest, :last_hit_at)
|
|
107
|
+
|
|
108
|
+
def initialize
|
|
109
|
+
@mutex = Mutex.new
|
|
110
|
+
reset
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Whether evaluations on this thread or fiber are left uncounted.
|
|
114
|
+
def self.untracked?
|
|
115
|
+
Thread.current[UNTRACKED] || EvaluationTracker.untracked? ? true : false
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Evaluations inside the block (on this thread or fiber) are not
|
|
119
|
+
# counted.
|
|
120
|
+
def self.untracked
|
|
121
|
+
previous = Thread.current[UNTRACKED]
|
|
122
|
+
Thread.current[UNTRACKED] = true
|
|
123
|
+
yield
|
|
124
|
+
ensure
|
|
125
|
+
Thread.current[UNTRACKED] = previous
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# A platform outside `config.client_platforms` (possible in a client
|
|
129
|
+
# Hash an app builds itself) counts as "unknown", so the table stays
|
|
130
|
+
# one row per flag and known platform.
|
|
131
|
+
def hit(definition, client, now: Time.now.utc)
|
|
132
|
+
platform = client.platform
|
|
133
|
+
known = platform && StandardFlags.config.client_platforms.any? { |name| name.to_s.downcase == platform }
|
|
134
|
+
version = client.version if known
|
|
135
|
+
platform = known ? platform : (platform || client.unknown_platform? ? Client::UNKNOWN_PLATFORM : NO_PLATFORM)
|
|
136
|
+
@mutex.synchronize do
|
|
137
|
+
forked!
|
|
138
|
+
entry = (@counts[[ definition.key, platform ]] ||= Pending.new(0, nil, nil))
|
|
139
|
+
entry.hits += 1
|
|
140
|
+
entry.oldest = version if version && (entry.oldest.nil? || version < entry.oldest)
|
|
141
|
+
entry.last_hit_at = now
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# Called by Runtime.unit_of_work_complete! with its monotonic clock.
|
|
146
|
+
# Never raises. The interval is claimed before writing, and a failed
|
|
147
|
+
# write keeps the claim, so a struggling database sees at most one
|
|
148
|
+
# attempt per interval per process. Only a write the store skipped
|
|
149
|
+
# (nil: an open transaction, or a store that keeps no stats) may be
|
|
150
|
+
# tried again at the next unit of work.
|
|
151
|
+
def flush(store, now, registry: StandardFlags.registry)
|
|
152
|
+
config = StandardFlags.config
|
|
153
|
+
return unless config.track_compat
|
|
154
|
+
|
|
155
|
+
batch = @mutex.synchronize do
|
|
156
|
+
forked!
|
|
157
|
+
next if @flushed_at && now - @flushed_at < config.evaluation_tracking_interval.to_f
|
|
158
|
+
|
|
159
|
+
@flushed_at = now
|
|
160
|
+
@counts.tap { @counts = {} }
|
|
161
|
+
end
|
|
162
|
+
return unless batch
|
|
163
|
+
|
|
164
|
+
windows = registry.definitions.select(&:compat_window?).map(&:key)
|
|
165
|
+
write(store, batch, windows) unless batch.empty? && windows.empty?
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
# Counts not yet written, as CompatStats.
|
|
169
|
+
def pending
|
|
170
|
+
@mutex.synchronize do
|
|
171
|
+
forked!
|
|
172
|
+
stats(@counts)
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
private
|
|
177
|
+
|
|
178
|
+
def reset
|
|
179
|
+
@counts = {}
|
|
180
|
+
@flushed_at = nil
|
|
181
|
+
@pid = Process.pid
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
# Called under the lock.
|
|
185
|
+
def forked!
|
|
186
|
+
reset unless @pid == Process.pid
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
def write(store, batch, windows)
|
|
190
|
+
flushed_at = Time.now.utc
|
|
191
|
+
markers = windows.map { |key| CompatStat.new(key: key, platform: TRACKING, hits: 0, oldest_version: nil, last_hit_at: flushed_at) }
|
|
192
|
+
written = begin
|
|
193
|
+
store.record_compat(stats(batch) + markers)
|
|
194
|
+
rescue StandardError => e
|
|
195
|
+
failed(e)
|
|
196
|
+
return restore(batch, keep_clock: true)
|
|
197
|
+
end
|
|
198
|
+
restore(batch, keep_clock: false) if written.nil?
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
def stats(counts)
|
|
202
|
+
counts.map do |(key, platform), entry|
|
|
203
|
+
CompatStat.new(key: key, platform: platform, hits: entry.hits, oldest_version: entry.oldest&.to_s,
|
|
204
|
+
last_hit_at: entry.last_hit_at)
|
|
205
|
+
end.sort_by { |stat| [ stat.key.to_s, stat.platform ] }
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
# Puts back counts a write didn't take, merged with any since.
|
|
209
|
+
def restore(batch, keep_clock:)
|
|
210
|
+
@mutex.synchronize do
|
|
211
|
+
@flushed_at = nil unless keep_clock
|
|
212
|
+
batch.each do |id, old|
|
|
213
|
+
entry = (@counts[id] ||= Pending.new(0, nil, nil))
|
|
214
|
+
entry.hits += old.hits
|
|
215
|
+
entry.oldest = old.oldest if old.oldest && (entry.oldest.nil? || old.oldest < entry.oldest)
|
|
216
|
+
entry.last_hit_at = [ entry.last_hit_at, old.last_hit_at ].compact.max
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def failed(error)
|
|
222
|
+
name = (error.cause_class if error.is_a?(StoreError)) || error.class.name
|
|
223
|
+
Reporting.once(:compat_tracking, name) { Reporting.report(error, runtime: "compat_tracking") }
|
|
224
|
+
Reporting.instrument(:compat_tracking_failed, error_class: name)
|
|
225
|
+
end
|
|
226
|
+
end
|
|
227
|
+
end
|
|
228
|
+
end
|
|
@@ -44,6 +44,7 @@ module StandardFlags
|
|
|
44
44
|
@removed = []
|
|
45
45
|
@reads = Hash.new(0)
|
|
46
46
|
@pending = nil # after_commit blocks of the write in progress
|
|
47
|
+
@compat = {}
|
|
47
48
|
end
|
|
48
49
|
|
|
49
50
|
def revision
|
|
@@ -171,6 +172,25 @@ module StandardFlags
|
|
|
171
172
|
end
|
|
172
173
|
end
|
|
173
174
|
|
|
175
|
+
def record_compat(stats)
|
|
176
|
+
@mutex.synchronize do
|
|
177
|
+
@reads[:record_compat] += 1
|
|
178
|
+
now = Time.now.utc
|
|
179
|
+
stats.each do |stat|
|
|
180
|
+
id = [ stat.key.to_s, stat.platform ]
|
|
181
|
+
@compat[id] = CompatStats.merge(@compat[id], stat, now: now)
|
|
182
|
+
end
|
|
183
|
+
stats.size
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def compat_stats(keys = nil)
|
|
188
|
+
@mutex.synchronize do
|
|
189
|
+
@reads[:compat_stats] += 1
|
|
190
|
+
@compat.values.select { |stat| keys.nil? || keys.include?(stat.key) }.sort_by { |stat| [ stat.key, stat.platform ] }
|
|
191
|
+
end
|
|
192
|
+
end
|
|
193
|
+
|
|
174
194
|
def members(rule: nil, cohort: nil, offset: 0, limit: nil)
|
|
175
195
|
@mutex.synchronize do
|
|
176
196
|
ids = owner_ids_or_nil(rule, cohort)
|
|
@@ -36,6 +36,12 @@ module StandardFlags
|
|
|
36
36
|
class State < Record
|
|
37
37
|
self.table_name = "standard_flags_state"
|
|
38
38
|
end
|
|
39
|
+
|
|
40
|
+
# Compat window counts (config.track_compat), from the compat stats
|
|
41
|
+
# migration.
|
|
42
|
+
class CompatStat < Record
|
|
43
|
+
self.table_name = "standard_flags_compat_stats"
|
|
44
|
+
end
|
|
39
45
|
end
|
|
40
46
|
|
|
41
47
|
Runtime.filter_unit_keys!(::ActiveRecord::Base)
|
|
@@ -92,6 +92,15 @@ module StandardFlags
|
|
|
92
92
|
# lock and changes nothing else on the
|
|
93
93
|
# rule; nil when it didn't run (and should
|
|
94
94
|
# be tried again).
|
|
95
|
+
# * record_compat(stats) — adds CompatStats (hits since the last
|
|
96
|
+
# write, the oldest version seen, the last
|
|
97
|
+
# hit) to standard_flags_compat_stats:
|
|
98
|
+
# hits add up, the oldest version and the
|
|
99
|
+
# latest hit win; nil when it didn't run.
|
|
100
|
+
# * compat_stats(keys) — the stored CompatStats for those flag
|
|
101
|
+
# keys (Strings; every key when nil),
|
|
102
|
+
# ordered by key and platform; nil when
|
|
103
|
+
# the store doesn't keep them.
|
|
95
104
|
#
|
|
96
105
|
# Operator reads (the MCP tools, the admin page, Restore), outside a
|
|
97
106
|
# write. Each takes `rule:` (a flag key) or `cohort:` (a cohort key):
|
|
@@ -153,6 +162,15 @@ module StandardFlags
|
|
|
153
162
|
nil
|
|
154
163
|
end
|
|
155
164
|
|
|
165
|
+
# nil means "not done": a store that doesn't keep compat stats.
|
|
166
|
+
def record_compat(_stats)
|
|
167
|
+
nil
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def compat_stats(_keys = nil)
|
|
171
|
+
nil
|
|
172
|
+
end
|
|
173
|
+
|
|
156
174
|
def warnings
|
|
157
175
|
[]
|
|
158
176
|
end
|
|
@@ -3,6 +3,7 @@ require "standard_flags/runtime/rule_snapshot"
|
|
|
3
3
|
require "standard_flags/runtime/store"
|
|
4
4
|
require "standard_flags/runtime/memory_store"
|
|
5
5
|
require "standard_flags/runtime/evaluation_tracker"
|
|
6
|
+
require "standard_flags/runtime/compat_tracker"
|
|
6
7
|
require "standard_flags/runtime/cache"
|
|
7
8
|
require "standard_flags/runtime/resolver"
|
|
8
9
|
|
|
@@ -86,11 +87,39 @@ module StandardFlags
|
|
|
86
87
|
@tracker ||= EvaluationTracker.new
|
|
87
88
|
end
|
|
88
89
|
|
|
90
|
+
# Compat window hit counts for this process.
|
|
91
|
+
def compat_tracker
|
|
92
|
+
@compat_tracker ||= CompatTracker.new
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Counts one evaluation of a compat flag with `until_clients:`, when
|
|
96
|
+
# `config.track_compat` and runtime rules are on and the read isn't an
|
|
97
|
+
# operator's. Called by the Evaluator; never raises.
|
|
98
|
+
def track_compat(definition, context)
|
|
99
|
+
return unless StandardFlags.config.track_compat && enabled? && !CompatTracker.untracked?
|
|
100
|
+
|
|
101
|
+
compat_tracker.hit(definition, context.client)
|
|
102
|
+
rescue StandardError
|
|
103
|
+
nil
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# The stored compat stats for `keys` (all when nil), or nil when the
|
|
107
|
+
# store doesn't keep them. Raises when the store can't be read.
|
|
108
|
+
def compat_stats(keys = nil)
|
|
109
|
+
store.compat_stats(keys&.map(&:to_s))
|
|
110
|
+
end
|
|
111
|
+
|
|
89
112
|
# Called by Memo::Hook when a unit of work ends, after its evaluations:
|
|
90
|
-
# flushes the EvaluationTracker
|
|
91
|
-
# `config.evaluation_tracking_interval` per process).
|
|
113
|
+
# flushes the EvaluationTracker and the CompatTracker if they are due
|
|
114
|
+
# (at most once per `config.evaluation_tracking_interval` per process).
|
|
115
|
+
# Never raises.
|
|
92
116
|
def unit_of_work_complete!
|
|
93
|
-
|
|
117
|
+
return unless enabled?
|
|
118
|
+
|
|
119
|
+
@tracker&.flush(store, now)
|
|
120
|
+
# With tracking on, the tracker flushes even before any hit, so each
|
|
121
|
+
# compat flag's tracking marker is written.
|
|
122
|
+
compat_tracker.flush(store, now) if StandardFlags.config.track_compat
|
|
94
123
|
rescue StandardError
|
|
95
124
|
nil
|
|
96
125
|
end
|
|
@@ -194,6 +223,7 @@ module StandardFlags
|
|
|
194
223
|
@cache = nil
|
|
195
224
|
@clock = nil
|
|
196
225
|
@tracker = nil
|
|
226
|
+
@compat_tracker = nil
|
|
197
227
|
end
|
|
198
228
|
|
|
199
229
|
# The stored string for a Ruby value: true/false become "on"/"off".
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
require "net/http"
|
|
3
|
+
require "timeout"
|
|
4
|
+
require "uri"
|
|
5
|
+
|
|
6
|
+
module StandardFlags
|
|
7
|
+
module Stores
|
|
8
|
+
# The version the App Store currently offers for one app, from Apple's
|
|
9
|
+
# public iTunes lookup API (no credentials), as one client's
|
|
10
|
+
# `store_lookup:`:
|
|
11
|
+
#
|
|
12
|
+
# client :ios_app, platform: :ios, ..., store_lookup: StandardFlags::Stores::AppStore.new(bundle_id: "com.example.app")
|
|
13
|
+
#
|
|
14
|
+
# `call` never waits: it answers the last version fetched (nil before
|
|
15
|
+
# the first, so the declared `available:` applies) and, when a refresh
|
|
16
|
+
# is due, starts one in the background (a thread; `runner:` replaces
|
|
17
|
+
# it). One refresh runs at a time per process, and a forked child
|
|
18
|
+
# starts its own.
|
|
19
|
+
#
|
|
20
|
+
# A good answer is kept for `ttl:` seconds (an hour). After a failure
|
|
21
|
+
# the last good answer stands and the next try waits `retry_after:`
|
|
22
|
+
# seconds (5 minutes), doubling with each further failure up to `ttl:`,
|
|
23
|
+
# so an unreachable store is asked rarely; a runner that can't start
|
|
24
|
+
# the refresh backs off the same way. Each fetch has an overall
|
|
25
|
+
# `deadline:` (5 seconds) covering connect, TLS, request and body, with
|
|
26
|
+
# each phase's own timeout a little shorter so net/http closes its
|
|
27
|
+
# socket itself, and a body cap of 256 KiB read in chunks. The
|
|
28
|
+
# connection is closed on every path. Failures emit
|
|
29
|
+
# `store_lookup_failed.standard_flags` (`store`, `client`, `reason`)
|
|
30
|
+
# after the fetch has ended, never inside its deadline; nothing raises.
|
|
31
|
+
#
|
|
32
|
+
# A store version that isn't SemVer or a shorthand (`2.1.0.4`) reads as
|
|
33
|
+
# unknown, so `available:` applies.
|
|
34
|
+
class AppStore
|
|
35
|
+
ENDPOINT = "https://itunes.apple.com/lookup".freeze
|
|
36
|
+
MAX_BYTES = 256 * 1024
|
|
37
|
+
THREAD_RUNNER = ->(&work) { Thread.new(&work) }
|
|
38
|
+
|
|
39
|
+
attr_reader :bundle_id, :country
|
|
40
|
+
|
|
41
|
+
def initialize(bundle_id:, country: "us", ttl: 3600, retry_after: 300, deadline: 5, clock: nil, runner: nil)
|
|
42
|
+
raise ArgumentError, "bundle_id: must be a non-blank String" unless bundle_id.is_a?(String) && !bundle_id.strip.empty?
|
|
43
|
+
raise ArgumentError, "country: must be a two-letter code" unless country.to_s.match?(/\A[A-Za-z]{2}\z/)
|
|
44
|
+
|
|
45
|
+
@bundle_id = bundle_id.strip.freeze
|
|
46
|
+
@country = country.to_s.downcase.freeze
|
|
47
|
+
@ttl = ttl
|
|
48
|
+
@retry_after = retry_after
|
|
49
|
+
@deadline = deadline
|
|
50
|
+
@clock = clock || -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }
|
|
51
|
+
@runner = runner || THREAD_RUNNER
|
|
52
|
+
@mutex = Mutex.new
|
|
53
|
+
@version = nil
|
|
54
|
+
@next_at = nil
|
|
55
|
+
@failures = 0
|
|
56
|
+
@refreshing = nil
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# The newest version in the store as last fetched, or nil. Starts a
|
|
60
|
+
# background refresh when one is due; never waits for it.
|
|
61
|
+
def call(client_app = nil)
|
|
62
|
+
refresh_later(client_app) if due?
|
|
63
|
+
@version
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Fetches now, on this thread (a warm-up at boot, say), and answers
|
|
67
|
+
# the version known afterwards. A background refresh may run beside
|
|
68
|
+
# it; the later answer wins.
|
|
69
|
+
def refresh!(client_app = nil)
|
|
70
|
+
refresh(client_app, claimed: false)
|
|
71
|
+
@version
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def uri
|
|
75
|
+
URI("#{ENDPOINT}?#{URI.encode_www_form(bundleId: bundle_id, country: country)}")
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
private
|
|
79
|
+
|
|
80
|
+
def due?
|
|
81
|
+
@next_at.nil? || @clock.call >= @next_at
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Claims the refresh (unless one is running in this process) and
|
|
85
|
+
# hands it to the runner.
|
|
86
|
+
def refresh_later(client_app)
|
|
87
|
+
claimed = @mutex.synchronize do
|
|
88
|
+
next false if @refreshing == Process.pid || !due?
|
|
89
|
+
|
|
90
|
+
@refreshing = Process.pid
|
|
91
|
+
end
|
|
92
|
+
return unless claimed
|
|
93
|
+
|
|
94
|
+
@runner.call { refresh(client_app) }
|
|
95
|
+
rescue StandardError => e
|
|
96
|
+
@mutex.synchronize do
|
|
97
|
+
@refreshing = nil
|
|
98
|
+
back_off
|
|
99
|
+
end
|
|
100
|
+
failed(client_app, e.class.name)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def refresh(client_app, claimed: true)
|
|
104
|
+
version = fetch(client_app)
|
|
105
|
+
@mutex.synchronize do
|
|
106
|
+
if version
|
|
107
|
+
@version = version
|
|
108
|
+
@failures = 0
|
|
109
|
+
@next_at = @clock.call + @ttl
|
|
110
|
+
else
|
|
111
|
+
back_off
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
ensure
|
|
115
|
+
@mutex.synchronize { @refreshing = nil } if claimed
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Called under the lock.
|
|
119
|
+
def back_off
|
|
120
|
+
@failures += 1
|
|
121
|
+
@next_at = @clock.call + [ @retry_after * (2**(@failures - 1)), @ttl ].min
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# The version, or nil after reporting why. Reporting runs outside the
|
|
125
|
+
# deadline, so a slow subscriber can't be interrupted by it.
|
|
126
|
+
def fetch(client_app)
|
|
127
|
+
outcome = begin
|
|
128
|
+
Timeout.timeout(@deadline) { download }
|
|
129
|
+
rescue Timeout::Error => e
|
|
130
|
+
[ :failed, e.class.name ]
|
|
131
|
+
end
|
|
132
|
+
return failed(client_app, outcome.last) if outcome.first == :failed
|
|
133
|
+
|
|
134
|
+
result = JSON.parse(outcome.last)
|
|
135
|
+
first = result.is_a?(Hash) && result["results"].is_a?(Array) && result["results"].first
|
|
136
|
+
Version.parse(first.is_a?(Hash) ? first["version"] : nil) || failed(client_app, "no usable version")
|
|
137
|
+
rescue StandardError => e
|
|
138
|
+
failed(client_app, e.class.name)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# `[:ok, body]` or `[:failed, reason]`. Reports nothing (it runs inside
|
|
142
|
+
# the deadline). Net::HTTP.start's block form finishes the connection
|
|
143
|
+
# on every way out; the phase timeouts sit below the deadline so a
|
|
144
|
+
# stalled connect fails inside net/http, which closes its socket.
|
|
145
|
+
def download
|
|
146
|
+
target = uri
|
|
147
|
+
phase = @deadline * 0.8
|
|
148
|
+
Net::HTTP.start(target.host, target.port, use_ssl: true, open_timeout: phase, read_timeout: phase,
|
|
149
|
+
write_timeout: phase, ssl_timeout: phase) do |http|
|
|
150
|
+
http.request(Net::HTTP::Get.new(target)) do |response|
|
|
151
|
+
return [ :failed, "HTTP #{response.code}" ] unless response.is_a?(Net::HTTPSuccess)
|
|
152
|
+
|
|
153
|
+
body = +""
|
|
154
|
+
response.read_body do |chunk|
|
|
155
|
+
body << chunk
|
|
156
|
+
return [ :failed, "response too large" ] if body.bytesize > MAX_BYTES
|
|
157
|
+
end
|
|
158
|
+
return [ :ok, body ]
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
[ :failed, "no response" ]
|
|
162
|
+
rescue StandardError => e
|
|
163
|
+
[ :failed, e.class.name ]
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def failed(client_app, reason)
|
|
167
|
+
Reporting.instrument(:store_lookup_failed, store: self.class.name, client: client_app&.name, reason: reason)
|
|
168
|
+
nil
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
module StandardFlags
|
|
2
|
+
# Where the newest version of a client in its store comes from, for the
|
|
3
|
+
# store ceiling (see StandardFlags::ClientApp): never enforce a minimum
|
|
4
|
+
# that the store can't yet deliver.
|
|
5
|
+
#
|
|
6
|
+
# client :ios_app, platform: :ios, min_version: "2.0.0", since: "2026-12-01", available: "1.9.0",
|
|
7
|
+
# store_lookup: StandardFlags::Stores::AppStore.new(bundle_id: "com.example.app", country: "us")
|
|
8
|
+
#
|
|
9
|
+
# A lookup belongs to the one client that declares it, and only ever caps
|
|
10
|
+
# that client. It is any object that responds to `call(client_app)` with
|
|
11
|
+
# its last known version (a String or a StandardFlags::Version), or nil,
|
|
12
|
+
# and returns at once: it is asked on request threads, so it must never
|
|
13
|
+
# wait on the network (AppStore refreshes in the background). When it
|
|
14
|
+
# answers nil, nonsense or raises, the declared `available:` applies; a
|
|
15
|
+
# pinned `_AVAILABLE` beats it. Failures emit
|
|
16
|
+
# `store_lookup_failed.standard_flags` with `store`, `client` and
|
|
17
|
+
# `reason`. Google Play has no public API for this,
|
|
18
|
+
# so Android clients use `available:` alone.
|
|
19
|
+
module Stores
|
|
20
|
+
autoload :AppStore, "standard_flags/stores/app_store"
|
|
21
|
+
|
|
22
|
+
module_function
|
|
23
|
+
|
|
24
|
+
# The version `app`'s store lookup last reported, or nil. Never raises.
|
|
25
|
+
def available(app)
|
|
26
|
+
lookup = app.store_lookup
|
|
27
|
+
return nil unless lookup
|
|
28
|
+
|
|
29
|
+
Version.parse(lookup.call(app))
|
|
30
|
+
rescue StandardError => e
|
|
31
|
+
Reporting.once(:store_lookup, app.name, e.class.name) do
|
|
32
|
+
Reporting.instrument(:store_lookup_failed, store: lookup.class.name, client: app.name, reason: e.class.name)
|
|
33
|
+
end
|
|
34
|
+
nil
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|