Class: Agent::Lock::Store::FileSystemStore

Inherits:
Object
  • Object
show all
Defined in:
lib/agent/lock/store/file_system_store.rb

Overview

Locks as files, which is the default and the one that needs nothing installed. See Tree#store_dir for why they live inside .git.

Constant Summary collapse

SUFFIX =

Every lock file ends in this, which is what all globs for.

".lock.md"
MUTEX =

The file flocked around every claim, beside the locks.

".mutex"
MUTEX_TIMEOUT =

Seconds. A claim's critical section is a directory scan and one write, so anything near this is somebody stuck, not somebody busy.

15

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tree) ⇒ FileSystemStore

Returns a new instance of FileSystemStore.

Parameters:

  • tree (Tree) —

    the checkout whose store directory holds the locks



28
29
30
# File 'lib/agent/lock/store/file_system_store.rb', line 28

def initialize(tree)
  @tree = tree
end

Instance Attribute Details

#tree ⇒ Object (readonly)

Returns the value of attribute tree.



25
26
27
# File 'lib/agent/lock/store/file_system_store.rb', line 25

def tree
  @tree
end

Instance Method Details

#all ⇒ Array<Record>

FNM_DOTMATCH is load-bearing. A scope like .plans/** slugs to a filename that starts with a dot, and a plain glob skips it, so the lock was written, listed nowhere, and blocked nobody. Redis matches those keys either way, and a store that enumerates less than it holds is worse than no store at all.

Returns:

  • (Array<Record>) —

    every lock here, other worktrees included



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

def all
  Dir.glob(File.join(dir, "*#{SUFFIX}"), File::FNM_DOTMATCH)
     .sort
     .filter_map { |path| Record.read(path) }
end

#backend ⇒ String

Returns what the tree's backend marker says for this store.

Returns:

  • (String) —

    what the tree's backend marker says for this store



39
# File 'lib/agent/lock/store/file_system_store.rb', line 39

def backend = "file"

#bound ⇒ Array<Record>

Every lock here. The store sits in the git common dir, so each one was taken in a checkout that shares this tree's backend marker, a worktree removed since included.

Returns:

  • (Array<Record>) —

    the locks a switch of this tree's backend strands



46
# File 'lib/agent/lock/store/file_system_store.rb', line 46

def bound = all

#create(record) ⇒ Boolean

Atomic: two agents racing for one scope cannot both win, because only one File::EXCL create succeeds.

Parameters:

Returns:

  • (Boolean) —

    false when somebody else got there first



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

def create(record)
  FileUtils.mkdir_p(dir)
  File.open(path_for(record.id), File::WRONLY | File::CREAT | File::EXCL) do |file|
    file.write(record.to_markdown)
  end
  true
rescue Errno::EEXIST
  false
end

#delete(record) ⇒ void

This method returns an undefined value.

Parameters:



127
# File 'lib/agent/lock/store/file_system_store.rb', line 127

def delete(record) = FileUtils.rm_f(record.path || path_for(record.id))

#describe ⇒ String

Returns how a listing names this store.

Returns:

  • (String) —

    how a listing names this store



36
# File 'lib/agent/lock/store/file_system_store.rb', line 36

def describe = dir

#dir ⇒ String

Returns:

  • (String)


33
# File 'lib/agent/lock/store/file_system_store.rb', line 33

def dir = tree.store_dir

#find(scope) ⇒ Record?

Parameters:

Returns:



91
# File 'lib/agent/lock/store/file_system_store.rb', line 91

def find(scope) = Record.read(path_for(Record.id_for(tree, scope)))

#mutex_path ⇒ String

Named so that all's *.lock.md can never match it, dot or no dot.

Returns:

  • (String)


136
# File 'lib/agent/lock/store/file_system_store.rb', line 136

def mutex_path = File.join(dir, MUTEX)

#path_for(id) ⇒ String

Parameters:

  • id (String)

Returns:

  • (String)


131
# File 'lib/agent/lock/store/file_system_store.rb', line 131

def path_for(id) = File.join(dir, "#{id}#{SUFFIX}")

#synchronize { ... } ⇒ Object

Runs the block with every other process in this store shut out, so a scan for conflicts and the write it justifies cannot be interleaved. O_EXCL alone settles a race for one scope, but lib/** and lib/a1.rb are two files, and two agents that both scanned an empty store before either wrote both won.

An exclusive flock on .mutex beside the locks. The kernel drops it when the holder exits, crash included, so a dead agent cannot leave the store wedged. A live one that stops mid-claim can, which is why the wait is bounded and ends in an error rather than a hung agent.

Not re-entrant. flock belongs to an open file, not to a process, so a nested call opens a second one, waits on the first, and raises once the timeout runs out.

Yields:

  • the critical section

Returns:

  • (Object) —

    whatever the block returns

Raises:

  • (Error) —

    when the mutex stayed held for longer than the timeout



79
80
81
82
83
84
85
86
87
# File 'lib/agent/lock/store/file_system_store.rb', line 79

def synchronize
  FileUtils.mkdir_p(dir)
  # Closing the file releases the flock, and the block form closes it
  # on the way out whether the critical section returned or raised.
  File.open(mutex_path, File::RDWR | File::CREAT, 0o644) do |file|
    wait_for(file)
    yield
  end
end

#update(record) ⇒ void

This method returns an undefined value.

Replace a lock in place. Written beside itself and renamed, so a reader never sees half a document, and so a crash mid-write leaves the previous version rather than nothing.

The temp name is unique per write, not per process: threads in one process (a facade) used to share <pid>.tmp, and the first rename left the others renaming a file that was gone.

Parameters:



118
119
120
121
122
123
# File 'lib/agent/lock/store/file_system_store.rb', line 118

def update(record)
  path = record.path || path_for(record.id)
  temp = "#{path}.#{Process.pid}.#{SecureRandom.hex(6)}.tmp"
  File.write(temp, record.to_markdown)
  File.rename(temp, path)
end