Class: Agent::Lock::Store::FileSystemStore
- Inherits:
-
Object
- Object
- Agent::Lock::Store::FileSystemStore
- 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
allglobs 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
-
#tree ⇒ Object
readonly
Returns the value of attribute tree.
Instance Method Summary collapse
-
#all ⇒ Array<Record>
FNM_DOTMATCH is load-bearing.
-
#backend ⇒ String
What the tree's backend marker says for this store.
-
#bound ⇒ Array<Record>
Every lock here.
-
#create(record) ⇒ Boolean
Atomic: two agents racing for one scope cannot both win, because only one File::EXCL create succeeds.
- #delete(record) ⇒ void
-
#describe ⇒ String
How a listing names this store.
- #dir ⇒ String
- #find(scope) ⇒ Record?
-
#initialize(tree) ⇒ FileSystemStore
constructor
A new instance of FileSystemStore.
-
#mutex_path ⇒ String
Named so that
all's*.lock.mdcan never match it, dot or no dot. - #path_for(id) ⇒ String
-
#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.
-
#update(record) ⇒ void
Replace a lock in place.
Constructor Details
#initialize(tree) ⇒ FileSystemStore
Returns a new instance of FileSystemStore.
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.
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.
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.
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.
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.
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.
36 |
# File 'lib/agent/lock/store/file_system_store.rb', line 36 def describe = dir |
#dir ⇒ String
33 |
# File 'lib/agent/lock/store/file_system_store.rb', line 33 def dir = tree.store_dir |
#find(scope) ⇒ Record?
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.
136 |
# File 'lib/agent/lock/store/file_system_store.rb', line 136 def mutex_path = File.join(dir, MUTEX) |
#path_for(id) ⇒ 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.
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.
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 |