forked from Azure/azure-event-hubs-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch.go
221 lines (186 loc) · 5.7 KB
/
batch.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package eventhub
import (
"errors"
"github.com/Azure/azure-amqp-common-go/v3/uuid"
"github.com/Azure/go-amqp"
)
type (
// BatchOptions are optional information to add to a batch of messages
BatchOptions struct {
MaxSize MaxMessageSizeInBytes
}
// BatchIterator offers a simple mechanism for batching a list of events
BatchIterator interface {
Done() bool
Next(messageID string, opts *BatchOptions) (*EventBatch, error)
}
// EventBatchIterator provides an easy way to iterate over a slice of events to reliably create batches
EventBatchIterator struct {
Cursors map[string]int
PartitionEventsMap map[string][]*Event
}
// EventBatch is a batch of Event Hubs messages to be sent
EventBatch struct {
*Event
marshaledMessages [][]byte
MaxSize MaxMessageSizeInBytes
size int
}
// BatchOption provides a way to configure `BatchOptions`
BatchOption func(opt *BatchOptions) error
// MaxMessageSizeInBytes is the max number of bytes allowed by Azure Service Bus
MaxMessageSizeInBytes uint
)
const (
// DefaultMaxMessageSizeInBytes is the maximum number of bytes in an event (https://docs.microsoft.com/en-us/azure/event-hubs/event-hubs-quotas)
DefaultMaxMessageSizeInBytes MaxMessageSizeInBytes = 1000000
batchMessageWrapperSize = 100
// KeyOfNoPartitionKey is the key value in Events map for Events which do not have PartitionKey
KeyOfNoPartitionKey = "NoPartitionKey"
)
// ErrMessageIsTooBig represents the error when one single event in the batch is bigger than the maximum batch size
var ErrMessageIsTooBig = errors.New("message is too big")
// BatchWithMaxSizeInBytes configures the EventBatchIterator to fill the batch to the specified max size in bytes
func BatchWithMaxSizeInBytes(sizeInBytes int) BatchOption {
return func(batchOption *BatchOptions) error {
batchOption.MaxSize = MaxMessageSizeInBytes(sizeInBytes)
return nil
}
}
// NewEventBatchIterator wraps a slice of `Event` pointers to allow it to be made into a `EventBatchIterator`.
func NewEventBatchIterator(events ...*Event) *EventBatchIterator {
partitionEventMap := make(map[string][]*Event)
cursors := make(map[string]int)
for _, event := range events {
var ok bool
var key string
if event.PartitionKey == nil {
key = KeyOfNoPartitionKey
} else {
key = *event.PartitionKey
}
if _, ok = partitionEventMap[key]; !ok {
cursors[key] = 0
}
partitionEventMap[key] = append(partitionEventMap[key], event)
}
return &EventBatchIterator{
Cursors: cursors,
PartitionEventsMap: partitionEventMap,
}
}
// Done communicates whether there are more messages remaining to be iterated over.
func (ebi *EventBatchIterator) Done() bool {
for key, cursor := range ebi.Cursors {
if cursor != len(ebi.PartitionEventsMap[key]) {
return false
}
}
return true
}
// Next fetches the batch of messages in the message slice at a position one larger than the last one accessed.
func (ebi *EventBatchIterator) Next(eventID string, opts *BatchOptions) (*EventBatch, error) {
var key string
for partitionKey, cursor := range ebi.Cursors {
if cursor != len(ebi.PartitionEventsMap[partitionKey]) {
key = partitionKey
}
}
if key == "" {
return nil, ErrNoMessages{}
}
if opts == nil {
opts = &BatchOptions{
MaxSize: DefaultMaxMessageSizeInBytes,
}
}
events := ebi.PartitionEventsMap[key][ebi.Cursors[key]:]
eb := NewEventBatch(eventID, opts)
if key != KeyOfNoPartitionKey && len(events) > 0 {
eb.PartitionKey = events[0].PartitionKey
}
for _, event := range events {
ok, err := eb.Add(event)
if err != nil {
return nil, err
}
if !ok {
if len(eb.marshaledMessages) == 0 {
ebi.Cursors[key]++
return nil, ErrMessageIsTooBig
}
return eb, nil
}
ebi.Cursors[key]++
}
return eb, nil
}
// NewEventBatch builds a new event batch
func NewEventBatch(eventID string, opts *BatchOptions) *EventBatch {
if opts == nil {
opts = &BatchOptions{
MaxSize: DefaultMaxMessageSizeInBytes,
}
}
mb := &EventBatch{
MaxSize: opts.MaxSize,
Event: &Event{
ID: eventID,
},
}
return mb
}
// Add adds a message to the batch if the message will not exceed the max size of the batch
func (eb *EventBatch) Add(e *Event) (bool, error) {
e.PartitionKey = eb.PartitionKey
msg, err := e.toMsg()
if err != nil {
return false, err
}
if msg.Properties.MessageID == nil || msg.Properties.MessageID == "" {
uid, err := uuid.NewV4()
if err != nil {
return false, err
}
msg.Properties.MessageID = uid.String()
}
bin, err := msg.MarshalBinary()
if err != nil {
return false, err
}
if eb.Size()+len(bin) > int(eb.MaxSize) {
return false, nil
}
eb.size += len(bin)
eb.marshaledMessages = append(eb.marshaledMessages, bin)
return true, nil
}
// Clear will zero out the batch size and clear the buffered messages
func (eb *EventBatch) Clear() {
eb.marshaledMessages = [][]byte{}
eb.size = 0
}
// Size is the number of bytes in the message batch
func (eb *EventBatch) Size() int {
// calculated data size + batch message wrapper + data wrapper portions of the message
return eb.size + batchMessageWrapperSize + (len(eb.marshaledMessages) * 5)
}
func (eb *EventBatch) toMsg() (*amqp.Message, error) {
batchMessage := eb.amqpBatchMessage()
batchMessage.Data = make([][]byte, len(eb.marshaledMessages))
_ = copy(batchMessage.Data, eb.marshaledMessages)
if eb.PartitionKey != nil {
batchMessage.Annotations = make(amqp.Annotations)
batchMessage.Annotations[partitionKeyAnnotationName] = eb.PartitionKey
}
return batchMessage, nil
}
func (eb *EventBatch) amqpBatchMessage() *amqp.Message {
return &amqp.Message{
Data: make([][]byte, len(eb.marshaledMessages)),
Format: batchMessageFormat,
Properties: &amqp.MessageProperties{
MessageID: eb.ID,
},
}
}