Class: DynamoAutoscale::DynamoActioner

Inherits:
Actioner
  • Object
show all
Includes:
Logger
Defined in:
lib/dynamo-autoscale/dynamo_actioner.rb

Instance Attribute Summary

Attributes inherited from Actioner

#downscales, #table, #upscales

Instance Method Summary collapse

Methods included from Logger

included, logger, #logger, logger=

Methods inherited from Actioner

#check_day_reset!, #clear_pending!, #downscale, #flush_operations!, #initialize, maximum_throughput, maximum_throughput=, minimum_throughput, minimum_throughput=, #pending_reads?, #pending_writes?, #provisioned_for, #provisioned_reads, #provisioned_writes, #queue_operation!, #set, #should_flush?, #try_flush!, #upscale

Constructor Details

This class inherits a constructor from DynamoAutoscale::Actioner

Instance Method Details

#can_run?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/dynamo-autoscale/dynamo_actioner.rb', line 38

def can_run?
  dynamo.status == :active
end

#dynamoObject



5
6
7
# File 'lib/dynamo-autoscale/dynamo_actioner.rb', line 5

def dynamo
  @dynamo ||= AWS::DynamoDB.new.tables[table.name]
end

#scale(metric, value) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/dynamo-autoscale/dynamo_actioner.rb', line 9

def scale metric, value
  aws_throughput_key = case metric
  when :reads
    :read_capacity_units
  when :writes
    :write_capacity_units
  end

  if throughput_synced?
    dynamo_scale(aws_throughput_key => value)
  else
    # If the throughputs were not synced, the likelihood is we made the
    # decision to scale based on false data. Clear it.
    clear_pending!
    false
  end
end

#scale_both(reads, writes) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/dynamo-autoscale/dynamo_actioner.rb', line 27

def scale_both reads, writes
  if throughput_synced?
    dynamo_scale(read_capacity_units: reads, write_capacity_units: writes)
  else
    # If the throughputs were not synced, the likelihood is we made the
    # decision to scale based on false data. Clear it.
    clear_pending!
    false
  end
end

#throughput_synced?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/dynamo-autoscale/dynamo_actioner.rb', line 42

def throughput_synced?
  time, datum = table.data.last

  # If we've not gathered any data, we cannot know if our values are synced
  # with Dynamo so we have to assume they are not until we get some data in.
  return false if time.nil? or datum.nil?

  if dynamo.read_capacity_units != datum[:provisioned_reads]
    logger.error "[actioner] DynamoDB disagrees with CloudWatch on what " +
      "the provisioned reads are. To be on the safe side, operations are " +
      "not being applied."

    return false
  end

  if dynamo.write_capacity_units != datum[:provisioned_writes]
    logger.error "[actioner] DynamoDB disagrees with CloudWatch on what " +
      "the provisioned writes are. To be on the safe side, operations are " +
      "not being applied."

    return false
  end

  return true
end