Class: Agent::Lock::Manager::Switch

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

Overview

switch-backend: binding a tree to the other store.

Every lock the tree's marker governs has to go first. Once the marker moves nobody reads the old store, so a lock left there would block nobody and still tell its holder it held something. Those locks are refused, unless the caller discards them. It all happens under the old store's mutex, so nobody claims there while it is being emptied.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tree, store, sweep:, drop:) ⇒ Switch

Returns a new instance of Switch.

Parameters:



57
58
59
60
61
62
# File 'lib/agent/lock/manager/switch.rb', line 57

def initialize(tree, store, sweep:, drop:)
  @tree = tree
  @store = store
  @sweep = sweep
  @drop = drop
end

Class Method Details

.current(tree) ⇒ Store::FileSystemStore, ...

The store a tree is bound to now, whatever the caller asked for: the old store is the one that has to be emptied.

A marker that names no backend is bound to nothing either. An older alock wrote AGENT_LOCK_BACKEND into it as typed, every command since has refused the tree, so no lock was ever taken under that name, and building it here left the tree with no way out.

Parameters:

Returns:

Raises:



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

def self.current(tree)
  established = Store.recorded(tree)
  return unless Store::BACKENDS.include?(established)

  Store.build(established, tree, redis_url: Store.named_redis_url)
end

.holding(tree, store) { ... } ⇒ Object

The store's mutex for every other verb, which refuses to run in a store the tree has left. #call moves the marker under this mutex, so the check and the block see one answer.

Parameters:

Yields:

  • the critical section

Returns:

  • (Object) —

    whatever the block returns

Raises:



43
44
45
46
47
48
49
50
51
# File 'lib/agent/lock/manager/switch.rb', line 43

def self.holding(tree, store)
  store.synchronize do
    moved = Store.recorded(tree)
    next yield if moved.nil? || moved == store.backend

    raise Store::Mismatch, "this tree's locks moved from #{store.backend} to #{moved} " \
                           "while this command waited; run it again"
  end
end

Instance Method Details

#call(target, discard:) ⇒ Array(Symbol, Array<Record>)

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

Parameters:

  • target (Store::FileSystemStore, Store::RedisStore) —

    the store to move to, already connected, so a Redis that does not answer is found out before anything is deleted

  • discard (Boolean) —

    delete the old store's locks instead of refusing

Returns:

  • (Array(Symbol, Array<Record>)) —

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/agent/lock/manager/switch.rb', line 72

def call(target, discard:)
  return [:unchanged, []] if target.backend == @store.backend

  @store.synchronize do
    # Another switch got here first, while this one waited.
    next [:unchanged, []] if Store.recorded(@tree) == target.backend

    @sweep.call
    stranded = @store.bound
    next [:occupied, stranded] if stranded.any? && !discard

    stranded.each { |record| @drop.call(record) }
    Store.rebind(@tree, target.backend)
    [:switched, stranded]
  end
end