-
Notifications
You must be signed in to change notification settings - Fork 27
Refactor indexer lambda event decoding #1082
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,31 @@ | ||||||
| # Copyright 2024 - 2026 Block, Inc. | ||||||
| # | ||||||
| # Use of this source code is governed by an MIT-style | ||||||
| # license that can be found in the LICENSE file or at | ||||||
| # https://opensource.org/licenses/MIT. | ||||||
| # | ||||||
| # frozen_string_literal: true | ||||||
|
|
||||||
| require "json" | ||||||
|
|
||||||
| module ElasticGraph | ||||||
| module IndexerLambda | ||||||
| # Decodes SQS message payloads encoded as JSON Lines into ElasticGraph events. | ||||||
| # | ||||||
| # `SqsProcessor` accepts alternate decoders that implement the same | ||||||
| # `#decode_events(sqs_record:, body:)` contract and return event hashes. | ||||||
| # | ||||||
| # @private | ||||||
| class JSONLDecoder | ||||||
| # Decodes the given message payload into zero or more ElasticGraph events. | ||||||
| # | ||||||
| # @param sqs_record [Hash] full SQS record carrying the payload | ||||||
| # @param body [String] resolved SQS message body | ||||||
| # @return [Array<Hash>] decoded ElasticGraph events | ||||||
| def decode_events(sqs_record:, body:) | ||||||
| _ = sqs_record | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the purpose of this line? It looks like it doesn't do anything... More generally, how do you expect |
||||||
| body.split("\n").map { |event| JSON.parse(event) } | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| end | ||||||
| end | ||||||
| end | ||||||
| end | ||||||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,10 @@ | ||||||||||||
| module ElasticGraph | ||||||||||||
| module IndexerLambda | ||||||||||||
| class JSONLDecoder | ||||||||||||
| def decode_events: ( | ||||||||||||
| sqs_record: ::Hash[::String, untyped], | ||||||||||||
| body: ::String | ||||||||||||
| ) -> ::Array[::Hash[::String, untyped]] | ||||||||||||
|
Comment on lines
+4
to
+7
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This is the RBS equivalent of "implements the interface at the class level". |
||||||||||||
| end | ||||||||||||
| end | ||||||||||||
| end | ||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,18 @@ | ||
| module ElasticGraph | ||
| module IndexerLambda | ||
| class SqsProcessor | ||
| interface _EventPayloadDecoder | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably move out of |
||
| def decode_events: ( | ||
| sqs_record: ::Hash[::String, untyped], | ||
| body: ::String | ||
| ) -> ::Array[::Hash[::String, untyped]] | ||
| end | ||
|
|
||
| def initialize: ( | ||
| Indexer::Processor, | ||
| logger: ::Logger, | ||
| ignore_sqs_latency_timestamps_from_arns: ::Set[::String], | ||
| ?event_payload_decoder: _EventPayloadDecoder, | ||
| ?s3_client: Aws::S3::Client?, | ||
| ) -> void | ||
|
|
||
|
|
@@ -14,6 +22,7 @@ module ElasticGraph | |
|
|
||
| @indexer_processor: Indexer::Processor | ||
| @logger: ::Logger | ||
| @event_payload_decoder: _EventPayloadDecoder | ||
| @s3_client: Aws::S3::Client? | ||
|
|
||
| attr_reader ignore_sqs_latency_timestamps_from_arns: ::Set[::String] | ||
|
|
@@ -22,7 +31,6 @@ module ElasticGraph | |
| S3_OFFLOADING_INDICATOR: String | ||
| def extract_sqs_metadata: (::Hash[String, untyped]) -> ::Hash[::String, untyped] | ||
| def millis_to_iso8601: (::String) -> ::String? | ||
| def parse_jsonl: (::String) -> ::Array[::Hash[::String, untyped]] | ||
| def get_payload_from_s3: (::String) -> ::String | ||
| def s3_client: () -> Aws::S3::Client | ||
| def format_response: ( | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,42 @@ | ||||||
| # Copyright 2024 - 2026 Block, Inc. | ||||||
| # | ||||||
| # Use of this source code is governed by an MIT-style | ||||||
| # license that can be found in the LICENSE file or at | ||||||
| # https://opensource.org/licenses/MIT. | ||||||
| # | ||||||
| # frozen_string_literal: true | ||||||
|
|
||||||
| require "elastic_graph/indexer_lambda/jsonl_decoder" | ||||||
|
|
||||||
| module ElasticGraph | ||||||
| module IndexerLambda | ||||||
| RSpec.describe JSONLDecoder do | ||||||
| describe "#decode_events" do | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
(If you adopt my suggestion to make it callable directly on |
||||||
| it "parses JSON Lines payloads into ElasticGraph events" do | ||||||
| decoder = described_class.new | ||||||
|
|
||||||
| decoded_events = decoder.decode_events( | ||||||
| sqs_record: {"messageId" => "123"}, | ||||||
| body: %({"id":"1"}\n{"id":"2","record":{"name":"Widget"}}) | ||||||
| ) | ||||||
|
|
||||||
| expect(decoded_events).to eq([ | ||||||
| {"id" => "1"}, | ||||||
| {"id" => "2", "record" => {"name" => "Widget"}} | ||||||
| ]) | ||||||
| end | ||||||
|
|
||||||
| it "returns no events for an empty message body" do | ||||||
| decoder = described_class.new | ||||||
|
|
||||||
| expect( | ||||||
| decoder.decode_events( | ||||||
| sqs_record: {"messageId" => "123"}, | ||||||
| body: "" | ||||||
| ) | ||||||
| ).to eq([]) | ||||||
| end | ||||||
| end | ||||||
| end | ||||||
| end | ||||||
| end | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is stateless, so we could define this as a
moduleand dodef self.decode_eventsbelow so that there's no garbage instance created for no reason.