Class: Alephant::Sequencer::SequenceTable

Inherits:
Alephant::Support::DynamoDB::Table
  • Object
show all
Includes:
Logger
Defined in:
lib/alephant/sequencer/sequence_table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name) ⇒ SequenceTable

Returns a new instance of SequenceTable.



14
15
16
17
18
# File 'lib/alephant/sequencer/sequence_table.rb', line 14

def initialize(table_name)
  @mutex      = Mutex.new
  @client     = AWS::DynamoDB::Client::V20120810.new
  @table_name = table_name
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



12
13
14
# File 'lib/alephant/sequencer/sequence_table.rb', line 12

def client
  @client
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



12
13
14
# File 'lib/alephant/sequencer/sequence_table.rb', line 12

def table_name
  @table_name
end

Instance Method Details

#delete_item!(ident) ⇒ Object



93
94
95
96
97
# File 'lib/alephant/sequencer/sequence_table.rb', line 93

def delete_item!(ident)
  client.delete_item(
    item_payload(ident)
  )
end

#sequence_exists(ident) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/alephant/sequencer/sequence_table.rb', line 20

def sequence_exists(ident)
  if ident.nil?
    return false
  end

  !(client.get_item(
    item_payload(ident)
  ).length == 0)
end

#sequence_for(ident) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/alephant/sequencer/sequence_table.rb', line 30

def sequence_for(ident)
  data = client.get_item(
    item_payload(ident)
  )

  data.length > 0 ? data[:item]["value"][:n].to_i : 0
end

#update_sequence_id(ident, value, last_seen_check = nil) ⇒ Object



38
39
40
41
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/alephant/sequencer/sequence_table.rb', line 38

def update_sequence_id(ident, value, last_seen_check = nil)
  begin
    current_sequence = last_seen_check.nil? ? sequence_for(ident) : last_seen_check

    dynamo_response = @mutex.synchronize do
      client.put_item({
        :table_name => table_name,
        :item => {
          'key' => {
            'S' => ident
          },
          'value' => {
            'N' => value.to_s
          }
        },
        :expected => {
          'key' => {
            :comparison_operator => 'NULL'
          },
          'value' => {
            :comparison_operator => 'GE',
            :attribute_value_list => [
              { 'N' => current_sequence.to_s }
            ]
          }
        },
        :conditional_operator => 'OR'
      })
    end

    logger.metric("SequencerFailedConditionalChecks", :value => 0)
    logger.info(
      "event"  => "SequenceIdUpdated",
      "id"     => ident,
      "value"  => value,
      "method" => "#{self.class}#update_sequence_id"
    )

    dynamo_response

  rescue AWS::DynamoDB::Errors::ConditionalCheckFailedException
    logger.metric "SequencerFailedConditionalChecks"
    logger.error(
      "event"                => "DynamoDBConditionalCheckFailed",
      "newSequenceValue"     => value,
      "currentSequenceValue" => current_sequence,
      "id"                   => ident,
      "class"                => e.class,
      "message"              => e.message,
      "backtrace"            => e.backtrace.join("\n"),
      "method"               => "#{self.class}#update_sequence_id"
    )
  end
end