Repository navigation
Conversation
7ec74bf to
7fb2796
Compare
2af56fe to
11abf6f
Compare
11abf6f to
bda3128
Compare
## Why The shared indexer specs need real protobuf publishers before they can exercise protobuf ingestion. Their protobuf round trip is still a placeholder. ## What Add a test publisher that compiles `schema.proto`, encodes event batches, and decodes native protobuf records. Extract the shared `protoc` locator from the existing schema test helper. ## How The publisher walks protobuf descriptors and uses the generated runtime metadata for scalar and enum overrides. Each instance owns its descriptor pool, so tests can encode with an older publisher and decode with the current schema. Focused specs cover proto2 and proto3, field presence, nested lists and abstract types, exact 64-bit integers, timestamps, and disk-backed artifacts. The publisher stays independent of the production record preparer. ## Risk Adds test support and declares runtime dependencies on `elasticgraph-indexer` and `google-protobuf`. Protobuf ingestion remains inactive; existing indexing examples stay pending. ## Testing No manual testing. ## Bigger picture This prepares the shared indexer parity suite for real protobuf publishing. Record preparation (#1442) and adapter integration plus parity-test activation (#1450) follow in this stack.
9d326d9 to
bcddf29
Compare
Keep scalar metadata when built-in names are customized. Accept numeric enum values from JRuby and avoid native message singleton mocks. Include a reproducible preparation benchmark.
Only format google.protobuf.Timestamp using DateTime directly; prepare other external message scalars (such as google.protobuf.Duration) via JSON encoding and the scalar's indexing preparer. Co-authored-by: Josh Wilson <joshuaw@squareup.com> Signed-off-by: Josh Wilson <joshuaw@squareup.com>
Test one observable behavior per example and assert on the whole prepared record. Drop the mocks and class patches that pinned internal caching and hash conversion, and freeze native records so every example verifies preparation does not mutate them. Define each syntax's schema once per context. Co-authored-by: Josh Wilson <joshuaw@squareup.com> Signed-off-by: Josh Wilson <joshuaw@squareup.com>
Protobuf metadata is appended after any user-registered indexer extension, so a lookup that picked the first extension would pass every other spec. Co-authored-by: Josh Wilson <joshuaw@squareup.com> Signed-off-by: Josh Wilson <joshuaw@squareup.com>
bbd2bb8 to
b145edc
Compare
Preparation output does not depend on proto2 versus proto3, so the specs use the default syntax once. Records are never nil and unknown enum numbers are rejected before preparation, so drop those fallbacks and cover the reachable nested empty abstract wrapper instead. Move `Schema.from_schema_artifacts` coverage into its own spec. Co-authored-by: Josh Wilson <joshuaw@squareup.com> Signed-off-by: Josh Wilson <joshuaw@squareup.com>
Drop the `google.protobuf.Timestamp` special case so every message scalar is prepared from its canonical protobuf JSON form by its scalar's indexing preparer. Co-authored-by: Josh Wilson <joshuaw@squareup.com> Signed-off-by: Josh Wilson <joshuaw@squareup.com>
| private_constant :Type, :Field | ||
|
|
||
| def prepare_message(message, mapping_properties) | ||
| type = @mutex.synchronize { @types[message.class] ||= build_type(message.class.descriptor) } |
There was a problem hiding this comment.
This takes the lock for every nested message and every list element, so one record can take it many times. What do you think about building the full plan once per root message class? build_type could resolve each nested message field's Type up front and store it on the Field, and an abstract type could store a map from each oneof alternative to its concrete Type. Then prepare_message takes the lock once per root record, and prepare_value calls the child Type directly.
| def prepare_message(message, mapping_properties) | ||
| type = @mutex.synchronize { @types[message.class] ||= build_type(message.class.descriptor) } | ||
| fields = type.fields | ||
| unless fields |
There was a problem hiding this comment.
build_type represents an abstract type as Type.new(name, nil), and this unless fields branch checks for that nil. So Type#fields is nilable, and Type#name is set for abstract types but never read.
What do you think of giving each kind of type its own prepare method? An abstract message needs no state of its own, because the concrete subtype message in its value oneof resolves its own Type. So the abstract case can be a singleton module:
module SubtypeUnwrapper
def self.prepare(message, mapping_properties)
alternative = message.value
yield message.public_send(alternative), mapping_properties if alternative
end
endprepare_message then becomes:
type = @mutex.synchronize { @types[message.class] ||= build_type(message.class.descriptor) }
type.prepare(message, mapping_properties) { |nested, nested_mapping| prepare_message(nested, nested_mapping) }With this diff, the elasticgraph-proto_ingestion specs pass and Steep reports no errors:
Diff
diff --git a/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/record_preparer.rb b/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/record_preparer.rb
index e9098d45..71366f98 100644
--- a/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/record_preparer.rb
+++ b/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/record_preparer.rb
@@ -36,57 +36,65 @@ module ElasticGraph
private
# Resolved once per protobuf message class so subsequent records do not inspect descriptors.
+ # Each `prepare` method yields every nested message, with its mapping, to the given block.
Type = ::Data.define(:name, :fields) do
# @implements Type
- end
- Field = ::Data.define(:name, :name_in_index, :presence_method, :list_depth, :leaf_preparer) do
- # @implements Field
- end
- private_constant :Type, :Field
- def prepare_message(message, mapping_properties)
- type = @mutex.synchronize { @types[message.class] ||= build_type(message.class.descriptor) }
- fields = type.fields
- unless fields
- alternative = message.value
- return nil unless alternative
+ def prepare(message, mapping_properties, &prepare_message)
+ prepared = {} # : ::Hash[::String, untyped]
+ fields.each do |field|
+ next if field.presence_method && !message.public_send(field.presence_method)
- return prepare_message(message.public_send(alternative), mapping_properties)
- end
+ nested_mapping = mapping_properties&.dig(field.name_in_index, "properties")
+ prepared[field.name_in_index] = field.prepare(message[field.name], nested_mapping, &prepare_message)
+ end
- prepared = {} # : ::Hash[::String, untyped]
- fields.each do |field|
- next if field.presence_method && !message.public_send(field.presence_method)
+ typename_type = mapping_properties&.dig("__typename", "type")
+ if typename_type && typename_type != "constant_keyword"
+ prepared["__typename"] = name
+ end
- nested_mapping = mapping_properties&.dig(field.name_in_index, "properties")
- prepared[field.name_in_index] = prepare_value(field, message[field.name], field.list_depth, nested_mapping)
+ prepared
end
+ end
- typename_type = mapping_properties&.dig("__typename", "type")
- if typename_type && typename_type != "constant_keyword"
- prepared["__typename"] = type.name
+ # Prepares a message of a union or interface type. Such a message holds the message of its concrete
+ # subtype in its `value` oneof, and that message has its own `Type`, so no per-type state is needed.
+ module SubtypeUnwrapper
+ def self.prepare(message, mapping_properties)
+ alternative = message.value
+ yield message.public_send(alternative), mapping_properties if alternative
end
-
- prepared
end
- def prepare_value(field, value, depth, nested_mapping)
- if depth > 0
- return value.map do |item|
- prepare_value(field, (depth > 1) ? item.values : item, depth - 1, nested_mapping)
+ Field = ::Data.define(:name, :name_in_index, :presence_method, :list_depth, :leaf_preparer) do
+ # @implements Field
+
+ def prepare(value, nested_mapping, depth = list_depth, &prepare_message)
+ if depth > 0
+ return value.map do |item|
+ prepare((depth > 1) ? item.values : item, nested_mapping, depth - 1, &prepare_message)
+ end
end
- end
- if (leaf_preparer = field.leaf_preparer)
- leaf_preparer.call(value)
- else
- prepare_message(value, nested_mapping)
+ if (preparer = leaf_preparer)
+ preparer.call(value)
+ else
+ yield value, nested_mapping
+ end
end
end
+ private_constant :Type, :SubtypeUnwrapper, :Field
+
+ def prepare_message(message, mapping_properties)
+ type = @mutex.synchronize { @types[message.class] ||= build_type(message.class.descriptor) }
+ type.prepare(message, mapping_properties) { |nested, nested_mapping| prepare_message(nested, nested_mapping) }
+ end
def build_type(descriptor)
+ return SubtypeUnwrapper if descriptor.lookup_oneof("value")
+
name = type_name_for(descriptor)
- return Type.new(name, nil) if descriptor.lookup_oneof("value")
overrides = @metadata.types_by_name[name]&.fields_by_name || {}
fields = descriptor.map do |entry|
diff --git a/elasticgraph-proto_ingestion/sig/elastic_graph/proto_ingestion/record_preparer.rbs b/elasticgraph-proto_ingestion/sig/elastic_graph/proto_ingestion/record_preparer.rbs
index 34ee0519..f57e4f64 100644
--- a/elasticgraph-proto_ingestion/sig/elastic_graph/proto_ingestion/record_preparer.rbs
+++ b/elasticgraph-proto_ingestion/sig/elastic_graph/proto_ingestion/record_preparer.rbs
@@ -3,7 +3,7 @@ module ElasticGraph
class RecordPreparer
@metadata: RuntimeMetadata::Schema
@scalar_types_by_name: ::Hash[::String, SchemaArtifacts::RuntimeMetadata::ScalarType]
- @types: ::Hash[::Class, Type]
+ @types: ::Hash[::Class, Type | singleton(SubtypeUnwrapper)]
@enums: ::Hash[::Google::Protobuf::EnumDescriptor, ::Hash[::Symbol | ::Integer, ::String?]]
@mutex: ::Mutex
@@ -12,8 +12,13 @@ module ElasticGraph
class Type < ::Data
attr_reader name: ::String
- attr_reader fields: ::Array[Field]?
- def self.new: (::String, ::Array[Field]?) -> Type
+ attr_reader fields: ::Array[Field]
+ def self.new: (::String, ::Array[Field]) -> Type
+ def prepare: (untyped, ::Hash[::String, untyped]?) { (untyped, ::Hash[::String, untyped]?) -> ::Hash[::String, untyped]? } -> ::Hash[::String, untyped]
+ end
+
+ module SubtypeUnwrapper
+ def self.prepare: (untyped, ::Hash[::String, untyped]?) { (untyped, ::Hash[::String, untyped]?) -> ::Hash[::String, untyped]? } -> ::Hash[::String, untyped]?
end
class Field < ::Data
@@ -23,13 +28,13 @@ module ElasticGraph
attr_reader list_depth: ::Integer
attr_reader leaf_preparer: (^(untyped) -> untyped)?
def self.new: (::String, ::String, ::Symbol?, ::Integer, (^(untyped) -> untyped)?) -> Field
+ def prepare: (untyped, ::Hash[::String, untyped]?, ?::Integer) { (untyped, ::Hash[::String, untyped]?) -> ::Hash[::String, untyped]? } -> untyped
end
private
def prepare_message: (untyped, ::Hash[::String, untyped]?) -> ::Hash[::String, untyped]?
- def prepare_value: (Field, untyped, ::Integer, ::Hash[::String, untyped]?) -> untyped
- def build_type: (::Google::Protobuf::Descriptor) -> Type
+ def build_type: (::Google::Protobuf::Descriptor) -> (Type | singleton(SubtypeUnwrapper))
def build_field: (::Google::Protobuf::Descriptor, ::Google::Protobuf::FieldDescriptor, RuntimeMetadata::Field?) -> Field
def build_enum: (::Google::Protobuf::EnumDescriptor) -> ::Hash[::Symbol | ::Integer, ::String?]
def build_scalar_preparer: (|
|
||
| nested_mapping = mapping_properties&.dig(field.name_in_index, "properties") | ||
| prepared[field.name_in_index] = prepare_value(field, message[field.name], field.list_depth, nested_mapping) | ||
| end |
There was a problem hiding this comment.
Building on my SubtypeUnwrapper suggestion above, we can move this loop into Type#prepare and prepare_value into Field#prepare.
This loop body reads only Field attributes: the presence check, the nested mapping lookup, and the write to name_in_index. What do you think of moving it into Field#prepare_into? Then the loop becomes:
fields.each { |field| field.prepare_into(prepared, message, mapping_properties, &prepare_message) }Here is the diff on top of the SubtypeUnwrapper change. With both changes, the elasticgraph-proto_ingestion specs pass, and Steep and Standard report no issues:
Diff
diff --git a/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/record_preparer.rb b/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/record_preparer.rb
index 71366f98..e960ccc8 100644
--- a/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/record_preparer.rb
+++ b/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/record_preparer.rb
@@ -42,12 +42,7 @@ module ElasticGraph
def prepare(message, mapping_properties, &prepare_message)
prepared = {} # : ::Hash[::String, untyped]
- fields.each do |field|
- next if field.presence_method && !message.public_send(field.presence_method)
-
- nested_mapping = mapping_properties&.dig(field.name_in_index, "properties")
- prepared[field.name_in_index] = field.prepare(message[field.name], nested_mapping, &prepare_message)
- end
+ fields.each { |field| field.prepare_into(prepared, message, mapping_properties, &prepare_message) }
typename_type = mapping_properties&.dig("__typename", "type")
if typename_type && typename_type != "constant_keyword"
@@ -70,10 +65,20 @@ module ElasticGraph
Field = ::Data.define(:name, :name_in_index, :presence_method, :list_depth, :leaf_preparer) do
# @implements Field
- def prepare(value, nested_mapping, depth = list_depth, &prepare_message)
+ # Adds this field's prepared value to `prepared`, unless `message` leaves this singular field unset.
+ def prepare_into(prepared, message, mapping_properties, &prepare_message)
+ return if presence_method && !message.public_send(presence_method)
+
+ nested_mapping = mapping_properties&.dig(name_in_index, "properties")
+ prepared[name_in_index] = prepare_value(message[name], nested_mapping, list_depth, &prepare_message)
+ end
+
+ private
+
+ def prepare_value(value, nested_mapping, depth, &prepare_message)
if depth > 0
return value.map do |item|
- prepare((depth > 1) ? item.values : item, nested_mapping, depth - 1, &prepare_message)
+ prepare_value((depth > 1) ? item.values : item, nested_mapping, depth - 1, &prepare_message)
end
end
diff --git a/elasticgraph-proto_ingestion/sig/elastic_graph/proto_ingestion/record_preparer.rbs b/elasticgraph-proto_ingestion/sig/elastic_graph/proto_ingestion/record_preparer.rbs
index f57e4f64..3a0ac415 100644
--- a/elasticgraph-proto_ingestion/sig/elastic_graph/proto_ingestion/record_preparer.rbs
+++ b/elasticgraph-proto_ingestion/sig/elastic_graph/proto_ingestion/record_preparer.rbs
@@ -28,7 +28,11 @@ module ElasticGraph
attr_reader list_depth: ::Integer
attr_reader leaf_preparer: (^(untyped) -> untyped)?
def self.new: (::String, ::String, ::Symbol?, ::Integer, (^(untyped) -> untyped)?) -> Field
- def prepare: (untyped, ::Hash[::String, untyped]?, ?::Integer) { (untyped, ::Hash[::String, untyped]?) -> ::Hash[::String, untyped]? } -> untyped
+ def prepare_into: (::Hash[::String, untyped], untyped, ::Hash[::String, untyped]?) { (untyped, ::Hash[::String, untyped]?) -> ::Hash[::String, untyped]? } -> void
+
+ private
+
+ def prepare_value: (untyped, ::Hash[::String, untyped]?, ::Integer) { (untyped, ::Hash[::String, untyped]?) -> ::Hash[::String, untyped]? } -> untyped
end
private- Drop empty `Data.define` blocks on `RecordPreparer::Type` and `Field` - Reference `IndexerExtension.name` instead of duplicating the class name string - Move the `google_protobuf.rbs` comment directly above `def subtype` - Add specs covering renaming a non-default scalar to its wire default name and `__typename` on lists of abstract objects under `name_in_index` Co-authored-by: Neon <f833053fa3445a7878359a1bd17c4f6b4ed1ceb3e7414f306f4929b1f9032d02@buzz> Signed-off-by: Josh Wilson <joshuaw@squareup.com>
…o Type, AbstractType, and Field Resolve nested message types and abstract `oneof` alternatives up front while holding `@mutex` once per root record, memoizing shared nested types in `@types`. Move per-type preparation onto `Type#prepare` and `AbstractType#prepare` (removing nilable `Type#fields`) and per-field preparation onto `Field#prepare_into` so nested messages and list elements prepare directly without re-acquiring `@mutex`. Co-authored-by: Neon <f833053fa3445a7878359a1bd17c4f6b4ed1ceb3e7414f306f4929b1f9032d02@buzz> Signed-off-by: Josh Wilson <joshuaw@squareup.com>
Why
The indexer needs datastore hashes, but protobuf publishers produce native messages. Converting them into public record hashes first adds a full traversal and extra allocations.
What
Prepare native protobuf messages directly into datastore records in one pass using the generated protobuf runtime metadata and
google-protobufdescriptors.How
ProtoIngestion::RecordPreparercaches field plans built from protobuf descriptors. Sparse runtime metadata supplies field-name overrides, enum names, and scalar types. Renamed built-in scalars retain their indexing preparers, and enum symbols and numbers map consistently across Ruby runtimes.The preparer applies shared scalar rules, unwraps abstract
oneofrecords, prepares message scalars such asgoogle.protobuf.Timestampfrom their canonical protobuf JSON form through the scalar's indexing preparer, and parses and canonicalizesUntypedleaves. The shared indexing pipeline adds__countsafterward.Preserves field presence, explicit defaults, exact 64-bit integers, nested lists, and concrete types in a single pass.
Risk
Adds
ProtoIngestion::RecordPreparerandRuntimeMetadata::Schema.from_schema_artifacts, which become active when #1450 registers the protobuf ingestion adapter. Runtime dependencies areelasticgraph-indexerandgoogle-protobuf.Testing
No manual testing. Covered by integration specs in
record_preparer_spec.rband unit specs inruntime_metadata/schema_spec.rb.Bigger picture
Builds on #1441's protobuf publishing test support. Native record validation, adapter registration, and activation of the shared indexing parity suite follow in #1450.