/
model.go
484 lines (429 loc) · 16.3 KB
/
model.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
The model package provides the interface for interacting with a stream of messages internally within Forest Bus. The messages are retained as a slice of bytes to enable cheap marshalling as part of the rpc calls and easy writing to underlying files.
*/
package model
import (
"encoding/binary"
"errors"
"hash/crc32"
"io"
"log"
)
// MESSAGE_OVERHEAD is the total number of bytes used by the header of a message in version 0 of the message format.
const MESSAGE_OVERHEAD = 1 + 4 + 8 + 4
// MESSAGE_VERSION_OFFSET is the location within the header of the byte used to indicate the version of the message format.
const MESSAGE_VERSION_OFFEST = 0
// MESSAGE_LENGTH_OFFSET is the location within the header of the 4 bytes used to record the length of the message.
const MESSAGE_LENGTH_OFFSET = MESSAGE_VERSION_OFFEST + 1
// MESSAGE_TERM_OFFSET is the location within the header of the 8 bytes used to record the election term of the message.
const MESSAGE_TERM_OFFSET = MESSAGE_LENGTH_OFFSET + 4
// MESSAGE_CRC_OFFSET is the location within the header of the 4 bytes used to record the CRC of the message payload.
const MESSAGE_CRC_OFFSET = MESSAGE_TERM_OFFSET + 8
// ERR_UNSUPPORTED_MESSAGE_FORMAT is thrown if a message format is encountered that this code is not familiar with.
var ERR_UNSUPPORTED_MESSAGE_FORMAT = errors.New("Unsupported message format")
// ERR_CRC_MISMATCH is thrown if the message payload's CRC doesn't match that recorded in the header.
var ERR_CRC_MISMATCH = errors.New("CRC check on message payload failed.")
// EMPTY_MESSAGES represts an empty set of messages.
var EMPTY_MESSAGES = Messages{}
// Messages consists of the count of messages and a slice of bytes.
type Messages struct {
Count int
Data []byte
// offsets holds the location of the start of each message within Data
offsets []int
// indexed records whether the message offsets have been calculated
indexed bool
}
type MessageProvider interface {
Messages() [][]byte
}
type MessageProviders []MessageProvider
/*
MessagesFromReader reads messages from the io.Reader and returns the resulting Message objects, along with the number of bytes actually read and any error.
The targetBytes parameter is the initial number of bytes that the function should attempt to retrieve from the Reader while looking for messages. If the given number of bytes doesn't include a full message, additional bytes will be read in an attempt to retrieve at least one full message.
Bytes that have been read that are for part of message will be discarded, but still reflected in the returned readByteCount which always contains the number of bytes actually read from the stream. The Disklog.Segment logic uses this to keep track of where in a file the current file pointer is up to.
*/
func MessagesFromReader(r io.Reader, targetBytes int) (result Messages, readByteCount int, err error) {
result = Messages{}
if targetBytes < MESSAGE_OVERHEAD {
targetBytes = MESSAGE_OVERHEAD + 1024
}
// Keep the original buffer around in case we need it later (0 message scenario)
buffer := make([]byte, targetBytes)
result.Data = buffer
// Read as much data as possible.
readByteCount, err = io.ReadFull(r, result.Data)
if err != nil && err != io.ErrUnexpectedEOF {
return result, readByteCount, err
}
// How much did we get?
//log.Printf("Read: data length: %v (readByteCount: %v)\n", len(result.Data), readByteCount)
result.Data = result.Data[:readByteCount]
//log.Printf("Data len: %v\n", len(result.Data))
countErr := result.countMessages(true)
if countErr != nil {
return result, readByteCount, countErr
}
//log.Printf("Data len: %v\n", len(result.Data))
if result.Count < 1 && readByteCount >= MESSAGE_OVERHEAD {
// We got no message - figure out how much we have to read to get at least one.
log.Printf("INFO: Having to read additional data in order to retrieve at least one message (%v bytes requested, %v read, data len %v)\n", targetBytes, readByteCount, len(result.Data))
length := int32(binary.LittleEndian.Uint32(buffer[MESSAGE_LENGTH_OFFSET:]))
newTarget := int(length) + MESSAGE_OVERHEAD
remainderToRead := newTarget - readByteCount
if remainderToRead > 0 {
newData := make([]byte, newTarget)
copy(newData, buffer[:readByteCount])
newByteCount, err := io.ReadFull(r, newData[readByteCount:])
readByteCount += newByteCount
result.Data = newData
if err != nil {
// Suppress the error - cause by us trying to overread.
return result, readByteCount, nil
}
countErr = result.countMessages(true)
if countErr != nil {
return result, readByteCount, countErr
}
if result.Count > 0 {
log.Printf("Additional read to retrieve message worked - message retrieved.\n")
}
}
}
return result, readByteCount, nil
}
// GetCount returns the number of whole messages represented by the data contained in this set of Messages.
func (msgs *Messages) GetCount() int {
return msgs.Count
}
/*
forEachMessage is a helper function that calls the provided action function once for each message contained in this Messages object.
The action function will recieve the message index (local to this set of messages, starting at 0) and the offset into the slice of bytes that this message header starts at. The action function must return a boolean - true to continue iterating over the rest of the messages, or false to abort the iteration.
*/
func (msgs *Messages) forEachMessage(action func(msgIndex int, msgOffset int) bool) error {
dlen := len(msgs.Data) - MESSAGE_OVERHEAD
localCount := 0
for msgOffset := 0; msgOffset <= dlen; {
if msgs.Data[msgOffset] != 0 {
return ERR_UNSUPPORTED_MESSAGE_FORMAT
}
length := int32(binary.LittleEndian.Uint32(msgs.Data[msgOffset+MESSAGE_LENGTH_OFFSET:]))
if msgOffset+MESSAGE_OVERHEAD+int(length) <= len(msgs.Data) {
if !action(localCount, msgOffset) {
return nil
}
} else {
// We have only a partial message - shrink data to match.
msgs.Data = msgs.Data[:msgOffset]
break
}
localCount++
msgOffset += MESSAGE_OVERHEAD + int(length)
}
return nil
}
/*
countMessages is an internal function that counts how many messages are stored in the slice of bytes contained in this Messages object. If crcCheck is true then the CRC value stored in the header will be compared to the CRC of the payload.
countMessages results in the Messages.Count field containing the number of complete messages in the Messages object. If a CRC check is done and any messages fail CRC validation then the ERR_CRC_MISMATCH error will be returned.
countMessages also populates the offsets slice with message indexes.
*/
func (msgs *Messages) countMessages(crcCheck bool) error {
msgs.Count = 0
crcError := false
msgs.offsets = make([]int, 0)
err := msgs.forEachMessage(func(forIndex int, offset int) bool {
if crcCheck {
length := int32(binary.LittleEndian.Uint32(msgs.Data[offset+MESSAGE_LENGTH_OFFSET:]))
CRC := uint32(binary.LittleEndian.Uint32(msgs.Data[offset+MESSAGE_CRC_OFFSET:]))
actualCRC := crc32.ChecksumIEEE(msgs.Data[offset+MESSAGE_OVERHEAD : offset+MESSAGE_OVERHEAD+int(length)])
if CRC != actualCRC {
crcError = true
}
}
msgs.Count++
msgs.offsets = append(msgs.offsets, offset)
return true
})
if crcError {
return ERR_CRC_MISMATCH
}
msgs.indexed = true
return err
}
/*
GetMessageTerm returns the term of the message at the given local index (starting at zero) within this set of Messages.
*/
func (msgs *Messages) GetMessageTerm(index int) (int64, error) {
var term int64
if !msgs.indexed {
err := msgs.countMessages(false)
if err != nil {
return 0, err
}
}
offset := msgs.offsets[index]
term = int64(binary.LittleEndian.Uint64(msgs.Data[offset+MESSAGE_TERM_OFFSET:]))
return term, nil
}
/*
SetMessageTerm sets the term of all messages in this set of Messages to the given value.
*/
func (msgs *Messages) SetMessageTerm(newTerm int64) error {
if !msgs.indexed {
err := msgs.countMessages(false)
if err != nil {
return err
}
}
for _, offset := range msgs.offsets {
binary.LittleEndian.PutUint64(msgs.Data[offset+MESSAGE_TERM_OFFSET:], uint64(newTerm))
}
return nil
}
/*
Write outputs all valid messages to the given io.Writer. The number of bytes written is returned along with any errors.
*/
func (msgs *Messages) Write(w io.Writer) (int, error) {
written, err := w.Write(msgs.Data)
if err != nil {
return 0, err
}
return written, nil
}
/*
Slice creates a new Messages object containing a subset of the messages in this Messages. The underlying byte slice is shared between the two instances.
*/
func (msgs *Messages) Slice(fromMessageIndex int, toMessageIndex int) (Messages, error) {
var dataSliceStart int
var endOfSlice int
result := Messages{}
if toMessageIndex == fromMessageIndex {
return result, nil
}
if !msgs.indexed {
err := msgs.countMessages(false)
if err != nil {
return result, err
}
}
dataSliceStart = msgs.offsets[fromMessageIndex]
if toMessageIndex >= msgs.Count {
endOfSlice = len(msgs.Data)
} else {
endOfSlice = msgs.offsets[toMessageIndex]
}
result.Data = msgs.Data[dataSliceStart:endOfSlice]
// Create a new index from the old one
result.offsets = make([]int, toMessageIndex-fromMessageIndex)
newIndex := 0
for i := fromMessageIndex; i < toMessageIndex; i++ {
result.offsets[newIndex] = msgs.offsets[i] - dataSliceStart
newIndex++
}
result.indexed = true
result.Count = toMessageIndex - fromMessageIndex
return result, nil
}
/*
Join creates a new instance of Messages that contains both the original Messages content and the given extraMessages as well. This operation requires copying of both the original and the extraMessages underlying byte data.
*/
func (msgs *Messages) Join(extraMessages Messages) (Messages, error) {
newMessages := Messages{}
if !msgs.indexed {
err := msgs.countMessages(false)
if err != nil {
return newMessages, err
}
}
if !extraMessages.indexed {
err := extraMessages.countMessages(false)
if err != nil {
return newMessages, err
}
}
newLength := len(msgs.Data) + len(extraMessages.Data)
newMessages.Data = make([]byte, newLength)
//log.Printf("Copying %v into new array", msgs.Data)
copy(newMessages.Data, msgs.Data)
copy(newMessages.Data[len(msgs.Data):], extraMessages.Data)
// Copy the indexes
newMessages.offsets = make([]int, msgs.Count, msgs.Count+extraMessages.Count)
copy(newMessages.offsets, msgs.offsets)
origLen := len(msgs.Data)
for _, extraOffset := range extraMessages.offsets {
newMessages.offsets = append(newMessages.offsets, extraOffset+origLen)
}
newMessages.Count = msgs.Count + extraMessages.Count
newMessages.indexed = true
return newMessages, nil
}
/*
Payloads returns a slice of byte slices that only contains the payloads of the messages.
The byte data itself is shared between the Messages instance and the resulting Payloads slices.
*/
func (msgs *Messages) Payloads() ([][]byte, error) {
results := make([][]byte, msgs.Count)
if !msgs.indexed {
err := msgs.countMessages(false)
if err != nil {
return results, err
}
}
for i, offset := range msgs.offsets {
var endOfMsg int
if (i + 1) == msgs.Count {
endOfMsg = len(msgs.Data)
} else {
endOfMsg = msgs.offsets[i+1]
}
results[i] = msgs.Data[offset+MESSAGE_OVERHEAD : endOfMsg]
}
return results, nil
}
// RawData provides the underlying raw data of the Messages.
func (msgs *Messages) RawData() []byte {
return msgs.Data
}
// Offsets returns the offset data for these messages.
func (msgs *Messages) Offsets() ([]int, error) {
if !msgs.indexed {
err := msgs.countMessages(false)
if err != nil {
return nil, err
}
}
return msgs.offsets, nil
}
/*
MessagesFromClientData takes the provided slice of payload data and creates the Forest Bus Messages representation of this data.
*/
func MessagesFromClientData(data [][]byte) Messages {
messagesSize := len(data) * MESSAGE_OVERHEAD
for _, msgData := range data {
messagesSize += len(msgData)
}
result := Messages{}
result.Data = make([]byte, messagesSize)
result.offsets = make([]int, len(data))
// Turn each payload into a valid stream of messages
var offset int
for i, msgData := range data {
// Record the version of the format
result.Data[offset] = 0
binary.LittleEndian.PutUint32(result.Data[offset+MESSAGE_LENGTH_OFFSET:], uint32(len(msgData)))
// Placeholder for the Term
binary.LittleEndian.PutUint64(result.Data[offset+MESSAGE_TERM_OFFSET:], 0)
// The CRC
binary.LittleEndian.PutUint32(result.Data[offset+MESSAGE_CRC_OFFSET:], crc32.ChecksumIEEE(msgData))
//log.Printf("Copying msgData %v to %v\n", msgData, offset+MESSAGE_OVERHEAD)
copy(result.Data[offset+MESSAGE_OVERHEAD:], msgData)
result.offsets[i] = offset
offset += MESSAGE_OVERHEAD + len(msgData)
}
result.indexed = true
result.Count = len(data)
return result
}
/*
MessagesFromMessageProvider takes a slices of MessageProviders and returns the Forest Bus Messages representation of this data.
*/
func MessagesFromMessageProvider(dataSources MessageProviders) Messages {
// First iteration calculates the total size of the data.
totalMessageCount := 0
totalMessageLength := 0
for _, dataSource := range dataSources {
data := dataSource.Messages()
totalMessageCount += len(data)
for _, msg := range data {
totalMessageLength += len(msg)
}
}
// Now create the data structures
result := Messages{}
result.Data = make([]byte, totalMessageLength+(totalMessageCount*MESSAGE_OVERHEAD))
result.offsets = make([]int, totalMessageCount)
// Now copy the data and calculate the offsets
offset := 0
msgCount := 0
for _, dataSource := range dataSources {
data := dataSource.Messages()
for _, msgData := range data {
// Record the version of the format
result.Data[offset] = 0
binary.LittleEndian.PutUint32(result.Data[offset+MESSAGE_LENGTH_OFFSET:], uint32(len(msgData)))
// Placeholder for the Term
binary.LittleEndian.PutUint64(result.Data[offset+MESSAGE_TERM_OFFSET:], 0)
// The CRC
binary.LittleEndian.PutUint32(result.Data[offset+MESSAGE_CRC_OFFSET:], crc32.ChecksumIEEE(msgData))
//log.Printf("Copying msgData %v to %v\n", msgData, offset+MESSAGE_OVERHEAD)
copy(result.Data[offset+MESSAGE_OVERHEAD:], msgData)
result.offsets[msgCount] = offset
offset += MESSAGE_OVERHEAD + len(msgData)
msgCount++
}
}
result.indexed = true
result.Count = msgCount
return result
}
/*
ReadSingleMessage is a utility function that reads one message from the given io.Reader.
*/
func ReadSingleMessage(r io.Reader) (length int32, term int64, crc uint32, payload []byte, err error) {
// Header
headerBytes := make([]byte, MESSAGE_OVERHEAD)
_, err = io.ReadFull(r, headerBytes)
if err != nil {
return 0, 0, 0, nil, err
}
version := headerBytes[0]
if version != 0 {
return 0, 0, 0, nil, ERR_UNSUPPORTED_MESSAGE_FORMAT
}
Length := int32(binary.LittleEndian.Uint32(headerBytes[MESSAGE_LENGTH_OFFSET:]))
Term := int64(binary.LittleEndian.Uint64(headerBytes[MESSAGE_TERM_OFFSET:]))
CRC := uint32(binary.LittleEndian.Uint32(headerBytes[MESSAGE_CRC_OFFSET:]))
Payload := make([]byte, Length)
read, err := io.ReadFull(r, Payload)
if err != nil {
return 0, 0, 0, nil, err
}
if read != int(Length) {
return 0, 0, 0, nil, errors.New("Not enough data found for message length.")
}
return Length, Term, CRC, Payload, nil
}
/*
ReadHeaderGetLength returns the length of the message from the header starting at the current position of the io.Reader. The full MESSAGE_OVERHEAD bytes will be read from the io.Reader.
*/
func ReadHeaderGetLength(r io.Reader) (int, error) {
// Header
headerBytes := make([]byte, MESSAGE_OVERHEAD)
_, err := io.ReadFull(r, headerBytes)
if err != nil {
return 0, err
}
version := headerBytes[0]
if version != 0 {
return 0, ERR_UNSUPPORTED_MESSAGE_FORMAT
}
return int(binary.LittleEndian.Uint32(headerBytes[1:])), nil
}
/*
ParseHeader takes the given slice of bytes and returns the individual fields that form the header.
*/
func ParseHeader(headerBytes []byte) (version byte, length int32, term int64, crc uint32) {
version = headerBytes[0]
length = int32(binary.LittleEndian.Uint32(headerBytes[MESSAGE_LENGTH_OFFSET:]))
term = int64(binary.LittleEndian.Uint64(headerBytes[MESSAGE_TERM_OFFSET:]))
crc = uint32(binary.LittleEndian.Uint32(headerBytes[MESSAGE_CRC_OFFSET:]))
return version, length, term, crc
}
// GetMessageContent parses the bytes of a Message and returns just the Payload
func GetMessageContent(r io.Reader) ([]byte, error) {
_, _, _, Payload, err := ReadSingleMessage(r)
if err != nil {
return nil, err
}
return Payload, nil
}