Skip to content

Commit af7962e

Browse files
committed
Attribute datastore failures to adapter-normalized events
`Processor.process` keyed factory results by the input event and looked up each datastore bulk failure with `factory_results_by_event.fetch( result.event)`. An ingestion adapter's `validate_event` may return a different event object (for example with a replaced record or a resolved abstract type). Operations carry that normalized event, and so do the bulk results, so the lookup raised `KeyError` and crashed the batch instead of reporting a per-event `FailedEventError`. Index factory results by each operation's event as well, so failures resolve regardless of whether the adapter normalized the event. Because `Event` equality includes the version, two versions of the same document in one batch remain distinct.
1 parent a8df5dc commit af7962e

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

‎elasticgraph-indexer/lib/elastic_graph/indexer/processor.rb‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ def process_returning_failures(events, refresh_indices: false)
4444
factory_results_by_event = events.to_h { |event| [event, @operation_factory.build(event)] }
4545

4646
factory_results = factory_results_by_event.values
47+
# Adapters may replace a native record or resolve an abstract type during validation.
48+
# Datastore results refer to that normalized operation event, not the original input.
49+
factory_results_by_operation_event = {} # : ::Hash[event, Operation::Factory::BuildResult]
50+
factory_results.each do |factory_result|
51+
factory_result.operations.each { |operation| factory_results_by_operation_event[operation.event] = factory_result }
52+
end
4753

4854
log_skipped_record_validations(factory_results)
4955

@@ -55,7 +61,7 @@ def process_returning_failures(events, refresh_indices: false)
5561
all_failures =
5662
factory_results.filter_map(&:failed_event_error) +
5763
bulk_result.failure_results.map do |result|
58-
all_operations_for_event = factory_results_by_event.fetch(result.event).operations
64+
all_operations_for_event = factory_results_by_operation_event.fetch(result.event).operations
5965
FailedEventError.from_failed_operation_result(result, all_operations_for_event.to_set)
6066
end
6167

‎elasticgraph-indexer/spec/unit/elastic_graph/indexer/processor_spec.rb‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,37 @@ class Indexer
9696
end
9797
end
9898

99+
it "associates datastore failures with all operations for an adapter-normalized event, without conflating versions" do
100+
original = build_upsert_event(:widget, id: "same-id", __version: 1)
101+
newer = original.with(version: 2)
102+
adapter = indexer.ingestion_adapters_by_format.fetch("json")
103+
allow(adapter).to receive(:validate_event).and_wrap_original do |method, event, **options|
104+
result = method.call(event, **options)
105+
IngestionAdapter::ValidationResult.valid(
106+
result.event.with(record: result.event.record.merge("normalized" => true)), result.record_preparer
107+
)
108+
end
109+
operation_groups = nil
110+
allow(datastore_router).to receive(:bulk) do |operations, **|
111+
operation_groups = operations.group_by(&:event)
112+
DatastoreIndexingRouter::BulkResult.new({"main" => operations.map do |operation|
113+
result = if operation.event.version == 1
114+
Operation::Result.failure_of(operation, "failed normalized event")
115+
else
116+
Operation::Result.success_of(operation)
117+
end
118+
[operation, result]
119+
end})
120+
end
121+
122+
failures = process_returning_failures([original, newer])
123+
expect(failures).not_to be_empty
124+
expect(failures.map(&:version).uniq).to eq([1])
125+
expect(failures.map(&:event)).to all have_attributes(record: a_hash_including("normalized" => true))
126+
expect(failures.map(&:operations).uniq).to eq([operation_groups.find { |event, _| event.version == 1 }.last.to_set])
127+
expect(failures.flat_map { |failure| failure.operations.to_a }.map(&:event)).to all have_attributes(version: 1)
128+
end
129+
99130
describe "latency metrics" do
100131
it "extracts latency metrics from events" do
101132
component = upsert_event_with_latency_timestamps(:component, 36, 72)

0 commit comments

Comments
 (0)