forked from lirm/aeron-go
/
consensus_module_proxy.go
121 lines (108 loc) · 3.29 KB
/
consensus_module_proxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package cluster
import (
"fmt"
"github.com/corymonroe-coinbase/aeron-go/aeron"
"github.com/corymonroe-coinbase/aeron-go/aeron/atomic"
"github.com/corymonroe-coinbase/aeron-go/aeron/idlestrategy"
"github.com/corymonroe-coinbase/aeron-go/cluster/codecs"
)
const (
scheduleTimerBlockLength = 16
cancelTimerBlockLength = 8
)
// Proxy class for encapsulating encoding and sending of control protocol messages to a cluster
type consensusModuleProxy struct {
marshaller *codecs.SbeGoMarshaller // currently shared as we're not reentrant (but could be here)
idleStrategy idlestrategy.Idler
rangeChecking bool
publication *aeron.Publication
buffer *atomic.Buffer
}
func newConsensusModuleProxy(
options *Options,
publication *aeron.Publication,
) *consensusModuleProxy {
return &consensusModuleProxy{
marshaller: codecs.NewSbeGoMarshaller(),
rangeChecking: options.RangeChecking,
publication: publication,
buffer: atomic.MakeBuffer(make([]byte, 500)),
}
}
// From here we have all the functions that create a data packet and send it on the
// publication. Responses will be processed on the control
// ConnectRequest packet and send
func (proxy *consensusModuleProxy) serviceAckRequest(
logPosition int64,
timestamp int64,
ackID int64,
relevantID int64,
serviceID int32,
) {
// Create a packet and send it
bytes, err := codecs.ServiceAckRequestPacket(
proxy.marshaller,
proxy.rangeChecking,
logPosition,
timestamp,
ackID,
relevantID,
serviceID,
)
if err != nil {
panic(err)
}
proxy.send(bytes)
}
func (proxy *consensusModuleProxy) closeSessionRequest(
clusterSessionId int64,
) {
// Create a packet and send it
bytes, err := codecs.CloseSessionRequestPacket(
proxy.marshaller,
proxy.rangeChecking,
clusterSessionId,
)
if err != nil {
panic(err)
}
proxy.send(bytes)
}
func (proxy *consensusModuleProxy) scheduleTimer(correlationId int64, deadline int64) bool {
buf := proxy.initBuffer(scheduleTimerTemplateId, scheduleTimerBlockLength)
buf.PutInt64(SBEHeaderLength, correlationId)
buf.PutInt64(SBEHeaderLength+8, deadline)
return proxy.offer(buf, SBEHeaderLength+scheduleTimerBlockLength) >= 0
}
func (proxy *consensusModuleProxy) cancelTimer(correlationId int64) bool {
buf := proxy.initBuffer(cancelTimerTemplateId, cancelTimerBlockLength)
buf.PutInt64(SBEHeaderLength, correlationId)
return proxy.offer(buf, SBEHeaderLength+cancelTimerBlockLength) >= 0
}
func (proxy *consensusModuleProxy) initBuffer(templateId uint16, blockLength uint16) *atomic.Buffer {
buf := proxy.buffer
buf.PutUInt16(0, blockLength)
buf.PutUInt16(2, templateId)
buf.PutUInt16(4, ClusterSchemaId)
buf.PutUInt16(6, ClusterSchemaVersion)
return buf
}
// send to our request publication
func (proxy *consensusModuleProxy) send(payload []byte) {
buffer := atomic.MakeBuffer(payload)
for proxy.offer(buffer, buffer.Capacity()) < 0 {
proxy.idleStrategy.Idle(0)
}
}
func (proxy *consensusModuleProxy) offer(buffer *atomic.Buffer, length int32) int64 {
var result int64
for i := 0; i < 3; i++ {
result = proxy.publication.Offer(buffer, 0, length, nil)
if result >= 0 {
break
} else if result == aeron.NotConnected || result == aeron.PublicationClosed || result == aeron.MaxPositionExceeded {
panic(fmt.Sprintf("offer failed, result=%d", result))
}
}
return result
}