Class: Bitfab::ReplayBranch

Inherits:
Object
  • Object
show all
Defined in:
lib/bitfab/replay_branch.rb

Overview

The database branch a single replay item runs against.

Bitfab.current_replay_branch hands you one inside a replayed method when the source trace carried a DB snapshot reference and the Bitfab service resolved a branch from it. Outside a replay item, or when no branch was resolved, that reader returns nil and your code keeps reading ENV["DATABASE_URL"] the normal way.

Immutable and scoped to one item: the reader builds it from the thread-local replay context, so parallel replay items each see their own branch and no lease state lives on a long-lived object.

Internally the resolved per-item state is a DB branch lease (the SDK/server protocol term). Its useful fields are exposed directly here so customer code never sees the word.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lease, trace_id, context) ⇒ ReplayBranch

Built by Bitfab.current_replay_branch; never constructed by callers.



38
39
40
41
42
43
44
45
46
47
# File 'lib/bitfab/replay_branch.rb', line 38

def initialize(lease, trace_id, context)
  @expires_at = lease["expiresAt"]
  @provider_console_url = lease["providerConsoleUrl"]
  @read_only = lease["readOnly"]
  @region = lease["region"]
  @trace_id = trace_id
  @url = lease["databaseUrl"]
  @context = context
  freeze
end

Instance Attribute Details

#expires_atObject (readonly)

When this branch's URL stops being valid. ISO-8601.



21
22
23
# File 'lib/bitfab/replay_branch.rb', line 21

def expires_at
  @expires_at
end

#provider_console_urlObject (readonly)

Deep link to the branch in the provider console, if available.



24
25
26
# File 'lib/bitfab/replay_branch.rb', line 24

def provider_console_url
  @provider_console_url
end

#read_onlyObject (readonly)

True if the branch is read-only. Use it to skip write operations during replay when the provider returned a read-only lease.



28
29
30
# File 'lib/bitfab/replay_branch.rb', line 28

def read_only
  @read_only
end

#regionObject (readonly)

The branch's region, e.g. "aws-us-east-1". A compute runs in its project's region, so a replay runner elsewhere pays that round trip on every query.



32
33
34
# File 'lib/bitfab/replay_branch.rb', line 32

def region
  @region
end

#trace_idObject (readonly)

The historical trace ID that produced the input for this replay item.



35
36
37
# File 'lib/bitfab/replay_branch.rb', line 35

def trace_id
  @trace_id
end

Instance Method Details

#database_urlObject

Connection string for this item's branch. Point your database client at it instead of the live database for the duration of the replayed call.

Reading it records on the trace that the replayed code obtained the branch URL, which is what separates "a branch was provisioned" from "the branch was actually used". The other readers inspect the lease without exposing the connection string, so they deliberately do not record anything.



56
57
58
59
# File 'lib/bitfab/replay_branch.rb', line 56

def database_url
  @context[:db_snapshot_accessed] = true
  @url
end

#inspectObject

Redact the connection string so a logged or inspected branch cannot leak it.



62
63
64
65
66
# File 'lib/bitfab/replay_branch.rb', line 62

def inspect
  "#<Bitfab::ReplayBranch trace_id=#{@trace_id.inspect} " \
    "expires_at=#{@expires_at.inspect} region=#{@region.inspect} " \
    "read_only=#{@read_only.inspect}>"
end