Class: RedisQueuedLocks::Swarm

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_queued_locks/swarm.rb

Overview

rubocop:disable Metrics/ClassLength

Since:

  • 1.9.0

Defined Under Namespace

Modules: Acquirers, RedisClientBuilder, SwarmElement, ZombieInfo Classes: FlushZombies, ProbeHosts, Supervisor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rql_client) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

Since:

  • 1.9.0



50
51
52
53
54
55
56
# File 'lib/redis_queued_locks/swarm.rb', line 50

def initialize(rql_client)
  @rql_client = rql_client
  @sync = RedisQueuedLocks::Utilities::Lock.new
  @supervisor = RedisQueuedLocks::Swarm::Supervisor.new(rql_client)
  @probe_hosts_element = RedisQueuedLocks::Swarm::ProbeHosts.new(rql_client)
  @flush_zombies_element = RedisQueuedLocks::Swarm::FlushZombies.new(rql_client)
end

Instance Attribute Details

#flush_zombies_element ⇒ RedisQueuedLocks::Swarm::FlushZombies (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

Since:

  • 1.9.0



37
38
39
# File 'lib/redis_queued_locks/swarm.rb', line 37

def flush_zombies_element
  @flush_zombies_element
end

#probe_hosts_element ⇒ RedisQueuedLocks::Swarm::ProbeHosts (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

Since:

  • 1.9.0



31
32
33
# File 'lib/redis_queued_locks/swarm.rb', line 31

def probe_hosts_element
  @probe_hosts_element
end

#rql_client ⇒ RedisQueuedLocks::Client (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

Since:

  • 1.9.0



19
20
21
# File 'lib/redis_queued_locks/swarm.rb', line 19

def rql_client
  @rql_client
end

#supervisor ⇒ RedisQueuedLocks::Swarm::Supervisor (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

Since:

  • 1.9.0



25
26
27
# File 'lib/redis_queued_locks/swarm.rb', line 25

def supervisor
  @supervisor
end

#sync ⇒ RedisQueuedLocks::Utilities::Lock (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

Since:

  • 1.9.0



43
44
45
# File 'lib/redis_queued_locks/swarm.rb', line 43

def sync
  @sync
end

Instance Method Details

#deswarm! ⇒ Hash<Symbol,Symbol|Boolean>

Returns Example: { ok: true, result: :terminating }.

Returns:

  • (Hash<Symbol,Symbol|Boolean>) —

    Example: { ok: true, result: :terminating }

Since:

  • 1.9.0



212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/redis_queued_locks/swarm.rb', line 212

def deswarm!
  sync.synchronize do
    supervisor.stop!
    probe_hosts_element.try_kill!
    flush_zombies_element.try_kill!

    # NOTE: need to give a little timespot to stop ractor objects and their main loops;
    sleep(0.1)

    { ok: true, result: :terminating }
  end
end

#flush_zombies(zombie_ttl: rql_client.config['swarm.flush_zombies.zombie_ttl'], lock_scan_size: rql_client.config['swarm.flush_zombies.zombie_lock_scan_size'], queue_scan_size: rql_client.config['swarm.flush_zombies.zombie_queue_scan_size']) ⇒ Hash<Bool,Set<String>]

Returns Hash<Bool,Set].

Parameters:

  • zombie_ttl (Hash) (defaults to: rql_client.config['swarm.flush_zombies.zombie_ttl']) —

    a customizable set of options

  • lock_scan_size (Hash) (defaults to: rql_client.config['swarm.flush_zombies.zombie_lock_scan_size']) —

    a customizable set of options

  • queue_scan_size (Hash) (defaults to: rql_client.config['swarm.flush_zombies.zombie_queue_scan_size']) —

    a customizable set of options

Options Hash (zombie_ttl:):

  • (Integer)

Options Hash (lock_scan_size:):

  • (Integer)

Options Hash (queue_scan_size:):

  • (Integer)

Returns:

  • (Hash<Bool,Set<String>]) —

    Hash<Bool,Set]

Since:

  • 1.9.0



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/redis_queued_locks/swarm.rb', line 104

def flush_zombies(
  zombie_ttl: rql_client.config['swarm.flush_zombies.zombie_ttl'],
  lock_scan_size: rql_client.config['swarm.flush_zombies.zombie_lock_scan_size'],
  queue_scan_size: rql_client.config['swarm.flush_zombies.zombie_queue_scan_size']
)
  RedisQueuedLocks::Swarm::FlushZombies.flush_zombies(
    rql_client.redis_client,
    zombie_ttl,
    lock_scan_size,
    queue_scan_size
  )
end

#probe_hosts ⇒ Hash<Symbol,Hash[String,Float]]

Returns Hash<Symbol,Hash].

Returns:

  • (Hash<Symbol,Hash[String,Float]]) —

    Hash<Symbol,Hash]

Since:

  • 1.9.0



90
91
92
93
94
95
# File 'lib/redis_queued_locks/swarm.rb', line 90

def probe_hosts
  RedisQueuedLocks::Swarm::ProbeHosts.probe_hosts(
    rql_client.redis_client,
    rql_client.uniq_identity
  )
end

#swarm! ⇒ Hash<Symbol,Symbol|Boolean>

Returns Example: { ok: true, result: :swarming }.

Returns:

  • (Hash<Symbol,Symbol|Boolean>) —

    Example: { ok: true, result: :swarming }

Since:

  • 1.9.0



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/redis_queued_locks/swarm.rb', line 181

def swarm!
  sync.synchronize do
    # Step 0:
    #   - stop the supervisor (kill internal observer objects if supervisor is alredy running);
    supervisor.stop!

    # Step 1:
    #   - initialize swarm elements and start their main loops;
    probe_hosts_element.try_swarm!
    flush_zombies_element.try_swarm!

    # Step 2:
    #   - run supervisor that should keep running created swarm elements and their main loops;
    unless supervisor.running?
      supervisor.observe! do
        probe_hosts_element.reswarm_if_dead!
        flush_zombies_element.reswarm_if_dead!
      end
    end

    # NOTE: need to give a little timespot to initialize ractor objects and their main loops;
    sleep(0.1)

    { ok: true, result: :swarming }
  end
end

#swarm_info(zombie_ttl: rql_client.config['swarm.flush_zombies.zombie_ttl']) ⇒ Hash<String,Hash<Symbol,Float|Time>>

Parameters:

  • zombie_ttl (Hash) (defaults to: rql_client.config['swarm.flush_zombies.zombie_ttl']) —

    a customizable set of options

Options Hash (zombie_ttl:):

  • (Integer)

Returns:

  • (Hash<String,Hash<Symbol,Float|Time>>)

Since:

  • 1.9.0



79
80
81
82
83
84
# File 'lib/redis_queued_locks/swarm.rb', line 79

def swarm_info(zombie_ttl: rql_client.config['swarm.flush_zombies.zombie_ttl'])
  RedisQueuedLocks::Swarm::Acquirers.acquirers(
    rql_client.redis_client,
    zombie_ttl
  )
end

#swarm_status ⇒ Hash<Symbol,Boolean|<Hash<Symbol,Boolean>>] Also known as: swarm_state

Returns Hash<Symbol,Boolean|<Hash<Symbol,Boolean>>].

Returns:

  • (Hash<Symbol,Boolean|<Hash<Symbol,Boolean>>]) —

    Hash<Symbol,Boolean|<Hash<Symbol,Boolean>>]

Since:

  • 1.9.0



62
63
64
65
66
67
68
69
70
71
# File 'lib/redis_queued_locks/swarm.rb', line 62

def swarm_status
  sync.synchronize do
    {
      auto_swarm: rql_client.config['swarm.auto_swarm'],
      supervisor: supervisor.status,
      probe_hosts: probe_hosts_element.status,
      flush_zombies: flush_zombies_element.status
    }
  end
end

#zombie_acquirers(zombie_ttl: rql_client.config['swarm.flush_zombies.zombie_ttl'], lock_scan_size: rql_client.config['swarm.flush_zombies.zombie_lock_scan_size']) ⇒ Set<String>

Parameters:

  • zombie_ttl (Hash) (defaults to: rql_client.config['swarm.flush_zombies.zombie_ttl']) —

    a customizable set of options

  • lock_scan_size (Hash) (defaults to: rql_client.config['swarm.flush_zombies.zombie_lock_scan_size']) —

    a customizable set of options

Options Hash (zombie_ttl:):

  • (Integer)

Options Hash (lock_scan_size:):

  • (Integer)

Returns:

  • (Set<String>)

Since:

  • 1.9.0



140
141
142
143
144
145
146
147
148
149
# File 'lib/redis_queued_locks/swarm.rb', line 140

def zombie_acquirers(
  zombie_ttl: rql_client.config['swarm.flush_zombies.zombie_ttl'],
  lock_scan_size: rql_client.config['swarm.flush_zombies.zombie_lock_scan_size']
)
  RedisQueuedLocks::Swarm::ZombieInfo.zombie_acquirers(
    rql_client.redis_client,
    zombie_ttl,
    lock_scan_size
  )
end

#zombie_hosts(zombie_ttl: rql_client.config['swarm.flush_zombies.zombie_ttl']) ⇒ Set<String>

Parameters:

  • zombie_ttl (Hash) (defaults to: rql_client.config['swarm.flush_zombies.zombie_ttl']) —

    a customizable set of options

Options Hash (zombie_ttl:):

  • (Integer)

Returns:

  • (Set<String>)

Since:

  • 1.9.0



156
157
158
# File 'lib/redis_queued_locks/swarm.rb', line 156

def zombie_hosts(zombie_ttl: rql_client.config['swarm.flush_zombies.zombie_ttl'])
  RedisQueuedLocks::Swarm::ZombieInfo.zombie_hosts(rql_client.redis_client, zombie_ttl)
end

#zombie_locks(zombie_ttl: rql_client.config['swarm.flush_zombies.zombie_ttl'], lock_scan_size: rql_client.config['swarm.flush_zombies.zombie_lock_scan_size']) ⇒ Set<String>

Parameters:

  • zombie_ttl (Hash) (defaults to: rql_client.config['swarm.flush_zombies.zombie_ttl']) —

    a customizable set of options

  • lock_scan_size (Hash) (defaults to: rql_client.config['swarm.flush_zombies.zombie_lock_scan_size']) —

    a customizable set of options

Options Hash (zombie_ttl:):

  • (Integer)

Options Hash (lock_scan_size:):

  • (Integer)

Returns:

  • (Set<String>)

Since:

  • 1.9.0



123
124
125
126
127
128
129
130
131
132
# File 'lib/redis_queued_locks/swarm.rb', line 123

def zombie_locks(
  zombie_ttl: rql_client.config['swarm.flush_zombies.zombie_ttl'],
  lock_scan_size: rql_client.config['swarm.flush_zombies.zombie_lock_scan_size']
)
  RedisQueuedLocks::Swarm::ZombieInfo.zombie_locks(
    rql_client.redis_client,
    zombie_ttl,
    lock_scan_size
  )
end

#zombies_info(zombie_ttl: rql_client.config['swarm.flush_zombies.zombie_ttl'], lock_scan_size: rql_client.config['swarm.flush_zombies.zombie_lock_scan_size']) ⇒ Hash<Symbol,Set<String>>

Parameters:

  • zombie_ttl (Hash) (defaults to: rql_client.config['swarm.flush_zombies.zombie_ttl']) —

    a customizable set of options

  • lock_scan_size (Hash) (defaults to: rql_client.config['swarm.flush_zombies.zombie_lock_scan_size']) —

    a customizable set of options

Options Hash (zombie_ttl:):

  • (Integer)

Options Hash (lock_scan_size:):

  • (Integer)

Returns:

  • (Hash<Symbol,Set<String>>)

Since:

  • 1.9.0



166
167
168
169
170
171
172
173
174
175
# File 'lib/redis_queued_locks/swarm.rb', line 166

def zombies_info(
  zombie_ttl: rql_client.config['swarm.flush_zombies.zombie_ttl'],
  lock_scan_size: rql_client.config['swarm.flush_zombies.zombie_lock_scan_size']
)
  RedisQueuedLocks::Swarm::ZombieInfo.zombies_info(
    rql_client.redis_client,
    zombie_ttl,
    lock_scan_size
  )
end