-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reporting: Serialize batches as they're constructed
Part of #1220. For scaling events, the size of a gzip-compressed batch is *much* less than the uncompressed version, so it's actually worth it to compress the JSON-encoded contents of the batch as we go, instead of buffering the events as a []ScalingEvent in memory. **Implementation** This change primarily adds the reporting.BatchBuilder[E] type (and some implementations), which abstracts over the process of gradually building the serialized bytes from a batch of events. Two implementations of BatchBuilder are provided in pkg/reporting - one for large JSON arrays, and one for the JSON lines format. To allow continuous gzip compression, both of these JSON batch builders are constructed with a new 'IOBuffer' type, which is just an io.Writer with a method to collect the bytes at the end. There's implementations of IOBuffer for normal bytes and for a gzip-compressed buffer. All that together means that batches of billing and scaling events should now be GZIP-compressed (where possible) as events are added to the batches, dramatically reducing the memory footprint.
- Loading branch information
Showing
10 changed files
with
278 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package reporting | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
var _ BatchBuilder[int] = (*JSONArrayBuilder[int])(nil) | ||
|
||
// JSONArrayBuilder is a BatchBuilder where all the events in a batch are serialized as a single | ||
// large JSON array. | ||
type JSONArrayBuilder[E any] struct { | ||
buf IOBuffer | ||
started bool | ||
} | ||
|
||
// NewJSONArrayBatch creates a new JSONArrayBuilder using the underlying IOBuffer to potentially | ||
// process the JSON encoding -- either with ByteBuffer for plaintext or GZIPBuffer for gzip | ||
// compression. | ||
func NewJSONArrayBuilder[E any](buf IOBuffer) *JSONArrayBuilder[E] { | ||
// open the array: | ||
if _, err := buf.Write([]byte{'['}); err != nil { | ||
panic(fmt.Sprintf("failed to write: %s", err)) | ||
} | ||
|
||
return &JSONArrayBuilder[E]{ | ||
buf: buf, | ||
started: false, | ||
} | ||
} | ||
|
||
func (b *JSONArrayBuilder[E]) Add(event E) { | ||
if b.started { | ||
if _, err := b.buf.Write([]byte("\n\t,")); err != nil { | ||
panic(fmt.Sprintf("failed to write: %s", err)) | ||
} | ||
} | ||
|
||
// note: we use a discrete json.Marshal here instead of json.Encoder becaues encoder adds a | ||
// newline at the end, and that'll make the formatting weird for us. | ||
tmpJSON, err := json.Marshal(event) | ||
if err != nil { | ||
panic(fmt.Sprintf("failed to JSON encode: %s", err)) | ||
} | ||
|
||
if _, err := b.buf.Write(tmpJSON); err != nil { | ||
panic(fmt.Sprintf("failed to write: %s", err)) | ||
} | ||
b.started = true | ||
} | ||
|
||
func (b *JSONArrayBuilder[E]) Finish() []byte { | ||
if _, err := b.buf.Write([]byte("\n]")); err != nil { | ||
panic(fmt.Sprintf("failed to write: %s", err)) | ||
} | ||
|
||
return b.buf.Collect() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package reporting | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
var _ BatchBuilder[int] = (*JSONLinesBuilder[int])(nil) | ||
|
||
// JSONLinesBuilder is a BatchBuilder where each event in the batch is serialized as a separate JSON | ||
// object on its own line, adhering to the "JSON lines"/"jsonl" format. | ||
type JSONLinesBuilder[E any] struct { | ||
buf IOBuffer | ||
} | ||
|
||
func NewJSONLinesBuilder[E any](buf IOBuffer) *JSONLinesBuilder[E] { | ||
return &JSONLinesBuilder[E]{ | ||
buf: buf, | ||
} | ||
} | ||
|
||
func (b *JSONLinesBuilder[E]) Add(event E) { | ||
tmpJSON, err := json.Marshal(event) | ||
if err != nil { | ||
panic(fmt.Sprintf("failed to JSON encode: %s", err)) | ||
} | ||
|
||
if _, err := b.buf.Write(tmpJSON); err != nil { | ||
panic(fmt.Sprintf("failed to write: %s", err)) | ||
} | ||
if _, err := b.buf.Write([]byte{'\n'}); err != nil { | ||
panic(fmt.Sprintf("failed to write: %s", err)) | ||
} | ||
} | ||
|
||
func (b *JSONLinesBuilder[E]) Finish() []byte { | ||
return b.buf.Collect() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.