Class: Corosync::Quorum

Inherits:
Object
  • Object
show all
Defined in:
lib/corosync/quorum.rb

Overview

Quorum is used for tracking the health of the cluster. This simply reads the quorum state as defined by corosync. Whenever the node gains or loses quorum, a notification callback is called. You can also poll the quorum state instead of using a callback.


Examples:

require 'corosync/quorum'
quorum = Corosync::Quorum.new
quorum.on_notify do |quorate,member_list|
  puts "Cluster is#{quorate ? '' ' not'} quorate"
  puts "  Members: #{member_list.join(' ')}"
end
quorum.connect
loop do
  quorum.dispatch
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connect = false) ⇒ void

Creates a new Quorum instance

Parameters:

  • connect (Boolean) (defaults to: false)

    Whether to join the cluster immediately. If not provided, you must call #connect and/or #connect later.


32
33
34
35
36
37
38
39
40
# File 'lib/corosync/quorum.rb', line 32

def initialize(connect = false)
	@handle = nil
	@fd = nil

	@callbacks = Corosync::QuorumCallbacksT.new
	@callbacks[:quorum_notify_fn] = self.method(:callback_notify)

	self.connect if connect
end

Instance Attribute Details

#fdIO (readonly)

The IO object containing the file descriptor notifications come across. You can use this to check for activity, but do not read anything from it.

Returns:

  • (IO)

25
26
27
# File 'lib/corosync/quorum.rb', line 25

def fd
  @fd
end

Instance Method Details

#connect(start = false) ⇒ void

This method returns an undefined value.

Connect to the Quorum service

Parameters:

  • start (Boolean) (defaults to: false)

    Whether to start listening for notifications (will not make initial call to callback).


45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/corosync/quorum.rb', line 45

def connect(start = false)
	handle_ptr = FFI::MemoryPointer.new(Corosync.find_type(:quorum_handle_t))
	quorum_type_ptr = FFI::MemoryPointer.new(:uint32)

	Corosync.cs_send(:quorum_initialize, handle_ptr, @callbacks, quorum_type_ptr)

	@handle = handle_ptr.read_uint64

	fd_ptr = FFI::MemoryPointer.new(:int)
	Corosync.cs_send(:quorum_fd_get, @handle, fd_ptr)
	@fd = IO.new(fd_ptr.read_int)

	self.start if start
end

#dispatch(timeout = -1)) ⇒ Boolean

Checks for a single pending event and triggers the appropriate callback if found.

Parameters:

  • timeout (Integer) (defaults to: -1))

    How long to wait for an event.

    • -1: Indefinite. Wait forever

    • 0: Non-blocking. If there isn’t a pending event, return immediately

    • >0: Wait the specified number of seconds.

Returns:

  • (Boolean)

    Returns True if an event was triggered. Otherwise False.


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/corosync/quorum.rb', line 97

def dispatch(timeout = -1)
	if !timeout != 0 then
		timeout = nil if timeout == -1
		select([@fd], [], [], timeout)
	end

	begin
		Corosync.cs_send!(:quorum_dispatch, @handle, Corosync::CS_DISPATCH_ONE_NONBLOCKING)
	rescue Corosync::TryAgainError => e
		raise e if e.depth > 1 # this exception is from a nested corosync function, not our quorum_dispatch we just called
		return false
	end

	return true
end

#finalizevoid

This method returns an undefined value.

Shuts down the connection to the Quorum service


62
63
64
65
66
67
68
69
# File 'lib/corosync/quorum.rb', line 62

def finalize
	return if @handle.nil?

	Corosync.cs_send(:quorum_finalize, @handle)

	@handle = nil
	@fd = nil
end

#getquorateBoolean Also known as: quorate?

Get node quorum status

Returns:

  • (Boolean)

    Whether node is quorate.


136
137
138
139
140
141
# File 'lib/corosync/quorum.rb', line 136

def getquorate
	quorate_ptr = FFI::MemoryPointer.new(:int)
	Corosync.cs_send(:quorum_getquorate, @handle, quorate_ptr)

	quorate_ptr.read_int > 0
end

#on_notify(&block) {|quorate, members| ... } ⇒ void

This method returns an undefined value.

Proc to call when quorum state changes.

Parameters:

  • block (Proc)

    Proc to call when quorm state changes. Pass Nil to disable the callback.

Yield Parameters:

  • quorate (Boolean)

    Whether cluster is quorate.

  • members (Array<Fixnum>)

    Node ID of cluster members.


118
119
120
# File 'lib/corosync/quorum.rb', line 118

def on_notify(&block)
	@callback_notify = block
end

#start(initial_callback = false) ⇒ Boolean

Start monitoring for changes to quorum status. This basically just enables triggering the callback. If not called you can still call #quorate? to get quorum state.

Parameters:

  • initial_callback (Boolean) (defaults to: false)

    Whether to call the callback after start.

Returns:

  • (Boolean)

75
76
77
78
79
80
81
82
83
# File 'lib/corosync/quorum.rb', line 75

def start(initial_callback = false)
	connect if @handle.nil?

	Corosync.cs_send(:quorum_trackstart, @handle, Corosync::CS_TRACK_CHANGES)

	if initial_callback and @callback_notify then
		@callback_notify.call(quorate?, [])
	end
end

#stopvoid

This method returns an undefined value.

Stop monitoring for changes to quorum status.


87
88
89
# File 'lib/corosync/quorum.rb', line 87

def stop
	Corosync.cs_send(:quorum_trackstop, @handle)
end