Class: Monitors::Elections

Inherits:
BaseMonitor show all
Defined in:
lib/poolparty/monitors/monitors/elections_monitor.rb

Instance Attribute Summary

Attributes inherited from BaseMonitor

#last_cloud_loaded_time, #log_file_path

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseMonitor

#after_close_callbacks, #before_close_callbacks, #env, inherited, #initialize, #log, #my_cloud

Constructor Details

This class inherits a constructor from Monitors::BaseMonitor

Class Method Details

.candidatesObject

these are the rules that define the elected actions



6
7
8
9
10
11
12
13
# File 'lib/poolparty/monitors/monitors/elections_monitor.rb', line 6

def self.candidates
  {
    :expand => :contract,
    :contract => :expand,
    :configure => nil,
    :none => nil
  }
end

Instance Method Details

#count_ballots(ballots = {}, candidates = {:expand => 0, :contract => 0}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/poolparty/monitors/monitors/elections_monitor.rb', line 25

def count_ballots(ballots={}, candidates={:expand => 0, :contract => 0})
  # Ballots look like:
  # {host => ["contract"]}
  # Count the number of nominations for each candidate action
  candidates.each do |action, ballots|
     stats.each do |ip, node_hsh|
       if node_hsh["nominations"] && node_hsh["nominations"].include?(action.to_s)
         candidates[action]+=1
       end
     end
   end
  candidates
end

#get(data = nil) ⇒ Object



15
16
17
# File 'lib/poolparty/monitors/monitors/elections_monitor.rb', line 15

def get(data=nil)
  'hello'
end

#handle_election(ballots = {}) ⇒ Object

Handle the elections ballots: => 1, “expand” => 4



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/poolparty/monitors/monitors/elections_monitor.rb', line 41

def handle_election(ballots={})      
  # Expand the cloud if 50+% of the votes are for expansion
  # Contract the cloud if 51+% of the votes are for contraction
  # Check to make sure an elected action is not already in progress      
  log "handle_election: #{ballots.inspect}"
  ballots.symbolize_keys!
  total_votes = ballots.inject(0) {|total, arr| total += arr[1] }
        
  ballots.each do |ballot, pro_votes|        
    contra = self.class.candidates[ballot]
    con_votes = ballots[contra] || 0
    
    if (pro_votes - con_votes)/total_votes > 0.5
      return run_elected_action(ballot)
    end
  end
end

#put(data = nil) ⇒ Object



19
20
21
22
23
# File 'lib/poolparty/monitors/monitors/elections_monitor.rb', line 19

def put(data=nil)
  elections = JSON.parse(data)
  log "Received #{elections.histogram.inspect} in Elections Monitor"
  handle_election(elections.histogram)
end

#run_elected_action(ballot) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/poolparty/monitors/monitors/elections_monitor.rb', line 59

def run_elected_action(ballot)
  log "Electing #{ballot}"
  case ballot
  when :expand
    my_cloud.running_action = :expanding
    my_cloud.launch_instance!(:cloud_name => my_cloud.name)
    my_cloud.running_action = nil
  when :contract        
    my_cloud.running_action = :contracting
    my_cloud.terminate_youngest_instance!
    my_cloud.running_action = nil
  else
    "none"
  end
end