Expand description
§xmachines-codec-avro
Avro for xmachines-rs machines, an xmachines-codec codec named avro: input events, emitted events and snapshots in the Schema Registry wire format, a zero byte (MAGIC, which is also the format byte of its snapshot records), the schema id (four bytes, big-endian), then the Avro binary datum.
§When to use it
Use it when the records of a machine’s topics are governed by a Schema Registry (Confluent’s, or Redpanda’s built-in one), so that producers and consumers in other languages read and write them with the registered schemas, and evolve them compatibly.
AvroSchemas generates the schemas from a bundle’s JSON Schemas with the rules of the protobuf codec’s event messages, and registers them under subjects of a registry::Registry (the Confluent REST API with the feature registry-http, or registry::MemoryRegistry):
- Input events (
schemas.events) and emitted events (schemas.emitted) are each a union of records, one per event type in schema order, namedxmachines.events.<machine>.<EventType>/xmachines.emitted.<machine>.<EventType>; the record name is the event type (typeis not a field). Producers using a single record schema per type (record-name subjects) work too. - Objects with properties are records (required properties are plain fields, optional ones
["null", T]defaulting to null), string enums are enums (when every value is a valid Avro name), integerslong, numbersdouble, arraysarray, records (additionalProperties)map, discriminated unions (oneOfobjects sharing aconstkey) a union of records without the key, and anything else (free-form JSON) a string holding the JSON text (marked"xstate.json": true). - Snapshots (XState’s
SystemSnapshot) use a fixed schema (xmachines.snapshot.v1), the root machine’s context typed byschemas.contextwhen there is one. Every field is optional, and keys or values a record does not describe go to itsextralist (key and JSON text), so every snapshot round-trips; ordered maps (actors, children, registry) are arrays of{ key, value }.
Records are read with schema resolution: a datum written with any registered schema (found by its id) is resolved against the generated one, so producers can evolve their schemas compatibly. Records with the generated schemas’ own ids are written and read by a direct binary codec driven by the schemas’ shapes, byte-identical to apache-avro.
§Example
use std::sync::Arc;
use serde_json::json;
use xmachines_codec_avro::{registry::MemoryRegistry, AvroSchemas};
let bundle = json!({
"machine": { "id": "meter" },
"schemas": { "events": { "reading": {
"type": "object", "properties": { "value": { "type": "number" } }, "required": ["value"]
} } }
});
let mut schemas = AvroSchemas::from_bundle(&bundle, Arc::new(MemoryRegistry::default())).unwrap();
// Subjects of the input events, emitted events and snapshots; the bundle has no emitted events.
schemas.register([Some("meter-value"), None, Some("meter-changelog-value")], true).unwrap();
let codec = xmachines_codec_avro::codec(Arc::new(schemas));
let events = codec.events().unwrap();
let mut record = Vec::new();
events.encode(&json!({ "type": "reading", "value": 21.5 }), &mut record).unwrap();
assert_eq!(record[0], xmachines_codec_avro::MAGIC);
assert_eq!(events.decode(&record).unwrap(), json!({ "type": "reading", "value": 21.5 }));§Features
registry-http:registry::HttpRegistry, a blocking client of the Schema Registry’s REST API (ureqwith rustls), with basic authentication and a timeout. Off by default;registry::MemoryRegistryis always there, for tests and tools.
§Pins
apache-avro 0.21 resolves other writers’ schemas. It must be 0.20 or later: 0.19 turned on serde_json/arbitrary_precision for the whole build, which changes how every number serializes.
§Related crates
xmachines-codec: the codec traits and their registry.xmachines-codec-protobuf: the same mapping rules for protobuf.xmachines-kafka: itsavrofeature registers this codec with the configuredschemaRegistry;xmachines-kafka avro events|emitted|snapshot <bundle>prints a schema.
§License
MIT
Modules§
- registry
- Schema Registry access: the Confluent REST API (which Redpanda’s
built-in registry implements; feature
registry-http), or an in-memory registry for tests and tools.
Structs§
- Avro
Schemas - Avro support for one machine: generated schemas, their registry ids, and the writer schemas of records read (by id).
- Root
- A generated schema: its shape, parsed Avro schema, JSON text, and id once registered.
Enums§
- Error
- Why a record could not be read or written, a bundle’s schemas have no Avro form, or the schema registry failed.
Constants§
- MAGIC
- The Schema Registry wire format’s first byte.
- MEDIA_
TYPES - The media types of Avro event records.
- NAME
- The name Avro is registered as.
Functions§
- codec
- The Avro codec of a bundle’s schemas: snapshots, input events when the
bundle has
schemas.events, and emitted events when it hasschemas.emitted. A schema writes records once it is registered (AvroSchemas::register); records of any registered schema are read.