Class: Agent::Lock::Manager::Enforcement

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

Overview

acquire --enforce: freezing the files of a lock already won, and writing down which ones.

Freezing happens after the claim is won, never before. Flagging files first and then losing the race would leave a tree full of unwritable files that no lock admits to having frozen, which is the one failure this whole feature is supposed to prevent.

The walk and chflags run outside the store's mutex, since every other session would be waiting on them. That leaves a window in which the lock can be noted, broken, or broken and taken by somebody else, and a record's id is only its tree and scope, so the newcomer's lock has the id of the one this call won. Writing the record captured before the walk back over it lost the note, brought a broken lock back, or replaced another agent's lock with this one; deleting it by id on a refused freeze deleted the other agent's. So both writes go back under the mutex and act only on the claim this call won, read again: #standing.

Instance Method Summary collapse

Constructor Details

#initialize(tree, store) ⇒ Enforcement

Returns a new instance of Enforcement.

Parameters:



29
30
31
32
# File 'lib/agent/lock/manager/enforcement.rb', line 29

def initialize(tree, store)
  @tree = tree
  @store = store
end

Instance Method Details

#apply(record, scope, wide:) ⇒ Array(Record, String)

Not for use inside the store's mutex, which it takes itself.

Parameters:

  • record (Record) —

    the lock, already created

  • scope (Scope)
  • wide (Boolean) —

    freeze even a scope wider than Freeze::LIMIT files

Returns:

  • (Array(Record, String)) —

    the lock with its frozen paths, and a warning, or nil, when the filesystem refused some of them. The record is nil when the claim did not outlast the freeze: nothing is left frozen, and whatever stands in the store is untouched

Raises:

  • (Freeze::TooBroad) —

    when the scope is too wide and wide is false. Nothing was flagged, and the claim is dropped with the freeze, not left standing unenforced



46
47
48
49
50
51
52
53
# File 'lib/agent/lock/manager/enforcement.rb', line 46

def apply(record, scope, wide:)
  wanted = Freeze.matches(scope, @tree)
  frozen = Freeze.apply(wanted, tree: @tree, wide: wide)
  [@store.synchronize { settle(record, scope, frozen) }, shortfall(wanted, frozen)]
rescue Freeze::TooBroad
  @store.synchronize { standing(record, @store.find(scope))&.then { |current| @store.delete(current) } }
  raise
end