Skip to content
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

[Schema Registry Avro] Improve the encoder according to architects feedback #20061

Closed
deyaaeldeen opened this issue Jan 25, 2022 · 0 comments · Fixed by #19842
Closed

[Schema Registry Avro] Improve the encoder according to architects feedback #20061

deyaaeldeen opened this issue Jan 25, 2022 · 0 comments · Fixed by #19842
Assignees

Comments

@deyaaeldeen
Copy link
Member

It has been recommended by the messaging architect that the encoder should produce and consume messages that customers can send and receive directly using their favorite messaging service.

@deyaaeldeen deyaaeldeen added this to the [2022] February milestone Jan 25, 2022
@deyaaeldeen deyaaeldeen self-assigned this Jan 25, 2022
deyaaeldeen added a commit that referenced this issue Jan 26, 2022
Fixes #20061

## Overview
Revamps the schema registry encoder to work on messages instead of buffers based on the recommendation of the Azure messaging architect.

This changes the APIs as follows:
```ts
  const buffer: NodeJS.Buffer = await serializer.serialize(value, schema);
```
becomes
```ts
  const message: MessageWithMetadata = await encoder.encodeMessageData(value, schema);
```
where `MessageWithMetadata` has a `body` field as well as a `contentType` field. The latter's format is `avro/binary+<Schema ID>`

For derserializing, the change is as follows:
```ts
  const deserializedValue = await serializer.deserialize(buffer);
```
becomes:
```ts
  const decodedObject = await encoder.decodeMessageData(message);
```

## Improvement upon  #15959

This design introduces a new `messageAdapter` option in the encoder constructor to support processing of any message type (e.g. [cloud event](https://github.com/cloudevents/spec/blob/v1.0.1/spec.md)):

```ts
  const encoder = new SchemaRegistryAvroEncoder(schemaRegistryClient, {
    groupName,
    messageAdapter: adapter
  });
```

where `adapter` is a message adapter that follows the following contract:

```ts
interface MessageAdapter<MessageT> {
  produceMessage: (messageWithMetadata: MessageWithMetadata) => MessageT;
  consumeMessage: (message: MessageT) => MessageWithMetadata;
}

 interface MessageWithMetadata {
  body: Uint8Array;
  contentType: string;
}
```

For convenience, the PR adds a couple of convenience adapter factories for Event Hubs's `EventData` and Event Grid's `SendCloudEventInput<Uint8Array>`. For example, the `createCloudEventAdapter` factory can be called to construct an adapter for the latter as follows:

```ts
const adapter = createCloudEventAdapter({
      type: "azure.sdk.eventgrid.samples.cloudevent",
      source: "/azure/sdk/schemaregistry/samples/withEventGrid",
    }),
```

Note that these adapter factories are exported by their respective messaging package without explicitly implementing the contract and the PR adds new encoder tests that check whether the produced adapters follow the contract. This organization could change in the future if we create a new core place for the contract to be imported from.

See the newly added samples for how to send such messages with Event Hubs and Event Grid.

Schema Registry commitment tracking: #15959
Tracking issue: #18608
First iteration design: #18365
@github-actions github-actions bot locked and limited conversation to collaborators Apr 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant