Class: Calvin::Sequencer

Inherits:
Spinoza::Model show all
Defined in:
lib/spinoza/calvin/sequencer.rb

Overview

Accepts transaction requests from clients. The requests accepted in an epoch are grouped as a batch, given a sequential id, and replicated to the transaction schedulers on each node.

Instance Attribute Summary collapse

Attributes inherited from Spinoza::Model

#timeline

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Spinoza::Model

#time_now

Constructor Details

#initialize(node: raise, dt_epoch: 0.010) ⇒ Sequencer

Returns a new instance of Sequencer.



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/spinoza/calvin/sequencer.rb', line 22

def initialize node: raise, dt_epoch: 0.010
  super timeline: node.timeline

  @node = node
  @dt_epoch = dt_epoch
  @batch = []
  @epoch = 0
  @id = self.class.next_id

  step_epoch
end

Instance Attribute Details

#dt_epochObject (readonly)

Length of epoch in seconds.



13
14
15
# File 'lib/spinoza/calvin/sequencer.rb', line 13

def dt_epoch
  @dt_epoch
end

#idObject (readonly)

ID used to construct UUID for batch.



10
11
12
# File 'lib/spinoza/calvin/sequencer.rb', line 10

def id
  @id
end

#nodeObject (readonly)

Returns the value of attribute node.



7
8
9
# File 'lib/spinoza/calvin/sequencer.rb', line 7

def node
  @node
end

Class Method Details

.next_idObject



17
18
19
# File 'lib/spinoza/calvin/sequencer.rb', line 17

def next_id
  @seq_id += 1
end

Instance Method Details

#accept_transaction(txn) ⇒ Object



71
72
73
# File 'lib/spinoza/calvin/sequencer.rb', line 71

def accept_transaction txn
  @batch << txn
end

#append_batch_to_meta_log(batch_id: batch_id) ⇒ Object



59
60
61
# File 'lib/spinoza/calvin/sequencer.rb', line 59

def append_batch_to_meta_log batch_id: batch_id
  meta_log.append batch_id, node: node
end

#inspectObject



34
35
36
# File 'lib/spinoza/calvin/sequencer.rb', line 34

def inspect
  "<#{self.class} on #{node.inspect}>"
end

#logObject



63
64
65
# File 'lib/spinoza/calvin/sequencer.rb', line 63

def log
  node.log
end

#meta_logObject



67
68
69
# File 'lib/spinoza/calvin/sequencer.rb', line 67

def meta_log
  node.meta_log
end

#step_epochObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/spinoza/calvin/sequencer.rb', line 38

def step_epoch
  unless @batch.empty?
    batch_id = [@id, @epoch] # globally unique, but not ordered
    log.write batch_id, @batch, node: node

    log.when_durable batch_id,
      actor: self,
      action: :append_batch_to_meta_log,
      batch_id: batch_id

    @batch = []
  end
  @epoch += 1

  timeline.schedule Spinoza::Event[
    time: time_now + dt_epoch,
    actor: self,
    action: :step_epoch
  ]
end