Class: Agent::Lock::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/agent/lock/manager.rb,
lib/agent/lock/manager/thaw.rb,
lib/agent/lock/manager/switch.rb,
lib/agent/lock/manager/enforcement.rb

Overview

Every decision this gem makes, with nothing printed.

Each verb returns a Result: a status the caller switches on, the records involved, and the exit code the CLI should hand back. Keeping the decisions here and the wording in the commands is what lets the whole lifecycle be tested without capturing output.

Defined Under Namespace

Modules: Thaw Classes: Enforcement, Result, Switch

Constant Summary collapse

DEFAULT_STALE_MINUTES =

Minutes untouched before a lock is reported STALE, and before one whose holder cannot be checked expires. AGENT_LOCK_STALE_MINUTES overrides it.

120

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tree: Tree.for, identity: Identity.current, stale_minutes: nil, store: nil) ⇒ Manager

Returns a new instance of Manager.

Parameters:

  • tree (Tree) (defaults to: Tree.for) —

    the checkout to work on

  • identity (Identity) (defaults to: Identity.current) —

    who the locks are signed by

  • stale_minutes (Integer, nil) (defaults to: nil) —

    nil reads AGENT_LOCK_STALE_MINUTES

  • store (FileSystemStore, RedisStore, nil) (defaults to: nil) —

    nil picks the tree's backend



55
56
57
58
59
60
# File 'lib/agent/lock/manager.rb', line 55

def initialize(tree: Tree.for, identity: Identity.current, stale_minutes: nil, store: nil)
  @tree = tree
  @store = store || Store.for(tree)
  @identity = identity
  @stale_minutes = stale_minutes
end

Instance Attribute Details

#identity ⇒ Object (readonly)

Returns the value of attribute identity.



49
# File 'lib/agent/lock/manager.rb', line 49

attr_reader :tree, :store, :identity

#store ⇒ Store::FileSystemStore, Store::RedisStore (readonly)

Returns where its locks are kept.

Returns:



49
# File 'lib/agent/lock/manager.rb', line 49

attr_reader :tree, :store, :identity

#tree ⇒ Tree (readonly)

Returns the checkout this manager works on.

Returns:

  • (Tree) —

    the checkout this manager works on



49
50
51
# File 'lib/agent/lock/manager.rb', line 49

def tree
  @tree
end

Instance Method Details

#acquire(path, intent: "unspecified", enforce: false, wide: false) ⇒ Result

Claim a scope, unless something overlapping stands in the way.

The scan for conflicts and the write it justifies happen inside the store's mutex. create refuses only an identical scope, so without it ten sessions claiming lib/** and lib/a.rb at once each saw a clear field and each wrote a lock, leaving overlapping claims that all reported success. Freezing stays outside: it walks the tree and runs chflags, and every other session would be waiting on it. What it writes afterwards goes back under the mutex: see Enforcement.

Parameters:

  • path (String) —

    a path or a glob

  • intent (String) (defaults to: "unspecified")
  • enforce (Boolean) (defaults to: false) —

    also make the matched files unwritable

  • wide (Boolean) (defaults to: false) —

    freeze even a scope wider than Freeze::LIMIT files

Returns:

  • (Result) —

    :acquired, or :already_mine, :held, :parent_scope or :interrupted with the records that explain why not. With enforce, :held also answers a lock that was broken while its files were being frozen, with whoever holds the scope now



87
88
89
90
91
92
93
# File 'lib/agent/lock/manager.rb', line 87

def acquire(path, intent: "unspecified", enforce: false, wide: false)
  scope = Scope.parse(path, tree: tree)
  outcome = exclusively { take(scope, intent) }
  return outcome unless enforce && outcome.status == :acquired

  enforce_on(outcome.record, scope, wide: wide)
end

#break_lock(path) ⇒ Result

Parameters:

  • path (String)

Returns:



150
151
152
153
154
155
156
157
158
159
# File 'lib/agent/lock/manager.rb', line 150

def break_lock(path)
  scope = Scope.parse(path, tree: tree)
  exclusively do
    record = locate(scope)
    next result(:not_found, []) if record.nil?

    drop(record)
    result(:broken, [record])
  end
end

#check(path) ⇒ Result

Whether this session may write in a scope. A parent's lock alone does not make it :mine, since the child still has to claim its own corner before its siblings can see it there; to the child, that scope is :free to claim.

Nor does a narrower lock of this session's own, which is the line acquire draws: holding lib/a.rb and asking about lib/** answered :mine while acquire 'lib/**' wrote a new lock, so the asker wrote to files nobody had claimed. Only a lock that contains the whole scope is :mine; anything less is :free, still to be claimed.

Parameters:

  • path (String)

Returns:

  • (Result) —

    :held, :mine with this session's locks that contain the scope, or :free



123
124
125
126
127
128
129
130
# File 'lib/agent/lock/manager.rb', line 123

def check(path)
  scope = Scope.parse(path, tree: tree)
  blocking, family = after_reap { conflicts(scope).partition { |record| record.blocks?(identity) } }
  return result(:held, blocking) if blocking.any?

  own = covering(scope, family.select { |record| record.held_by?(identity) })
  own.any? ? result(:mine, own) : result(:free, [])
end

#list ⇒ Result

Returns every lock in the store, this tree's siblings included.

Returns:

  • (Result) —

    every lock in the store, this tree's siblings included



133
134
135
# File 'lib/agent/lock/manager.rb', line 133

def list
  result(:listed, after_reap { store.all })
end

#mine ⇒ Result

Returns:



138
139
140
# File 'lib/agent/lock/manager.rb', line 138

def mine
  result(:listed, after_reap { store.all.select { |record| record.held_by?(identity) } })
end

#note(path, text) ⇒ Result

Write a line into a lock this session holds, so that a machine coming back up has something better than the diff to work out where it was.

Parameters:

  • path (String)
  • text (String)

Returns:



185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/agent/lock/manager.rb', line 185

def note(path, text)
  scope = Scope.parse(path, tree: tree)
  exclusively do
    record = locate(scope, prefer: ->(found) { found.held_by?(identity) })
    next result(:not_found, []) if record.nil?
    next result(:refused, [record]) unless record.held_by?(identity)

    updated = record.note(text)
    store.update(updated)
    result(:noted, [updated])
  end
end

#reap ⇒ Array<Record>

Locks whose holder is provably gone, or, for a holder on another host that cannot be asked, that nobody has touched in so long that nobody can say. A live holder's lock is never among them, however old; see Record#expired?. Reaped before any decision that depends on them, never on a schedule.

A lock with nothing written in it is deleted. One whose holder wrote down what it was doing is orphaned instead: the process is gone, the claim is void, but the notes are the only record of work interrupted halfway, and a reboot is the most likely reason there are any. Whoever comes next sees them, and resume takes the lock and the notes back.

Takes the store's mutex, so it is not for use inside another synchronize: the verbs here call #sweep there instead.

Returns:

  • (Array<Record>) —

    what it cleared out of the way



177
# File 'lib/agent/lock/manager.rb', line 177

def reap = exclusively { sweep }

#release(path) ⇒ Result

Parameters:

  • path (String)

Returns:



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/agent/lock/manager.rb', line 97

def release(path)
  scope = Scope.parse(path, tree: tree)
  exclusively do
    record = locate(scope, prefer: ->(found) { found.held_by?(identity) })
    next result(:not_found, []) if record.nil?
    next result(:refused, [record]) unless record.held_by?(identity)

    drop(record)
    result(:released, [record])
  end
end

#release_all ⇒ Result

Returns:



143
144
145
146
# File 'lib/agent/lock/manager.rb', line 143

def release_all
  mine = ->(record) { record.held_by?(identity) }
  result(:released, exclusively { store.all.select(&mine).each { |record| drop(record) } })
end

#resume(path) ⇒ Result

Take an interrupted lock back, notes and all.

Inside the store's mutex, because the check and the write are two steps: two sessions resuming one orphan would otherwise both find it interrupted, both write themselves in, and both report :resumed.

An orphan blocks nobody, so while it sat there somebody may have claimed an overlapping scope; that claim is checked for exactly as acquire would check it. And the resumed claim covers the branch this checkout is on now, not the one the orphan was taken on: it is rebound to the current tree, so that other checkouts of this branch see it.

Parameters:

  • path (String)

Returns:

  • (Result) —

    :resumed, :not_found, or :held with what overlaps



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/agent/lock/manager.rb', line 212

def resume(path)
  scope = Scope.parse(path, tree: tree)
  exclusively do
    sweep
    record = locate(scope, prefer: :orphaned?.to_proc)
    next result(:not_found, []) if record.nil? || record.active?

    blocking = conflicts(scope).select { |other| other.blocks?(identity) }
    next result(:held, blocking) if blocking.any?

    # Under the orphan's own spelling, which may differ in case from the
    # one typed here: the id is made from the scope the record states.
    rebind(record, record.scope_object)
  end
end

#stale_minutes ⇒ Integer

Returns how long a lock whose holder cannot be checked is trusted for, and how long a live one goes untouched before it is reported stale.

Returns:

  • (Integer) —

    how long a lock whose holder cannot be checked is trusted for, and how long a live one goes untouched before it is reported stale



65
66
67
# File 'lib/agent/lock/manager.rb', line 65

def stale_minutes
  @stale_minutes ||= Integer(ENV.fetch("AGENT_LOCK_STALE_MINUTES", DEFAULT_STALE_MINUTES))
end

#switch_backend(target, discard: false) ⇒ Result

Bind this tree to another backend: see Switch. Reaped first, so a holder that is gone does not stand in the way.

Parameters:

Returns:

  • (Result) —

    :switched with the locks it discarded, :unchanged, or :occupied with the locks standing in the way



235
# File 'lib/agent/lock/manager.rb', line 235

def switch_backend(target, discard: false) = result(*switcher.call(target, discard:))