diff --git a/canonical-party/flink/job.sql b/canonical-party/flink/job.sql
index bc7e4e0..0dff5d5 100644
--- a/canonical-party/flink/job.sql
+++ b/canonical-party/flink/job.sql
@@ -2,7 +2,9 @@
-- Submitted by the platform CLI with `bin/sql-client.sh -f job.sql` on the jobmanager.
--
-- Per entity table, four Flink tables and two continuous INSERTs:
---
_inbound kafka Publish Input: records producers publish (JSON, schema in events/schemas)
+-- _inbound kafka Publish Input: records producers publish (JSON, schema in events/schemas),
+-- keyed by _id: only messages sharing a key share a partition,
+-- and only one partition keeps them in order
-- _ods jdbc upserts inbound records into the ODS table by primary key
-- _cdc postgres-cdc every change committed to the ODS table, however it arrived
-- _outbound upsert-kafka Subscribe Output: compacted topic keyed by _id, deletes as tombstones
diff --git a/canonical-party/odps.yaml b/canonical-party/odps.yaml
index fdf82fc..dba86b1 100644
--- a/canonical-party/odps.yaml
+++ b/canonical-party/odps.yaml
@@ -65,7 +65,7 @@ product:
name:
en: Publish Input
description:
- en: "Kafka topics that accept canonical records, one per entity. JSON payloads conform to the schema registered for the topic."
+ en: "Kafka topics that accept canonical records, one per entity. JSON payloads conform to the schema registered for the topic, and each message is keyed by the entity's identifier, which is what keeps changes to one entity in order."
outputPorttype: API
format: JSON
authenticationMethod: none
diff --git a/canonical-party/product.yaml b/canonical-party/product.yaml
index fe2c23a..fddf966 100644
--- a/canonical-party/product.yaml
+++ b/canonical-party/product.yaml
@@ -54,6 +54,7 @@ events:
topics:
- name: "digital-core.canonical-party.party.inbound"
role: publish-input
+ key: "party_id" # producers MUST key by this: order holds per key, per partition
table: "party"
schema: events/schemas/party.schema.json
partitions: 3
@@ -61,12 +62,14 @@ events:
retention_ms: 604800000
- name: "digital-core.canonical-party.party.outbound"
role: subscribe-output
+ key: "party_id" # set by the Flink upsert-kafka sink
table: "party"
schema: events/schemas/party.schema.json
partitions: 3
cleanup_policy: compact
- name: "digital-core.canonical-party.address.inbound"
role: publish-input
+ key: "address_id" # producers MUST key by this: order holds per key, per partition
table: "address"
schema: events/schemas/address.schema.json
partitions: 3
@@ -74,12 +77,14 @@ events:
retention_ms: 604800000
- name: "digital-core.canonical-party.address.outbound"
role: subscribe-output
+ key: "address_id" # set by the Flink upsert-kafka sink
table: "address"
schema: events/schemas/address.schema.json
partitions: 3
cleanup_policy: compact
- name: "digital-core.canonical-party.person.inbound"
role: publish-input
+ key: "person_id" # producers MUST key by this: order holds per key, per partition
table: "person"
schema: events/schemas/person.schema.json
partitions: 3
@@ -87,12 +92,14 @@ events:
retention_ms: 604800000
- name: "digital-core.canonical-party.person.outbound"
role: subscribe-output
+ key: "person_id" # set by the Flink upsert-kafka sink
table: "person"
schema: events/schemas/person.schema.json
partitions: 3
cleanup_policy: compact
- name: "digital-core.canonical-party.company.inbound"
role: publish-input
+ key: "company_id" # producers MUST key by this: order holds per key, per partition
table: "company"
schema: events/schemas/company.schema.json
partitions: 3
@@ -100,12 +107,14 @@ events:
retention_ms: 604800000
- name: "digital-core.canonical-party.company.outbound"
role: subscribe-output
+ key: "company_id" # set by the Flink upsert-kafka sink
table: "company"
schema: events/schemas/company.schema.json
partitions: 3
cleanup_policy: compact
- name: "digital-core.canonical-party.party_role.inbound"
role: publish-input
+ key: "party_role_id" # producers MUST key by this: order holds per key, per partition
table: "party_role"
schema: events/schemas/party_role.schema.json
partitions: 3
@@ -113,12 +122,14 @@ events:
retention_ms: 604800000
- name: "digital-core.canonical-party.party_role.outbound"
role: subscribe-output
+ key: "party_role_id" # set by the Flink upsert-kafka sink
table: "party_role"
schema: events/schemas/party_role.schema.json
partitions: 3
cleanup_policy: compact
- name: "digital-core.canonical-party.customer.inbound"
role: publish-input
+ key: "customer_id" # producers MUST key by this: order holds per key, per partition
table: "customer"
schema: events/schemas/customer.schema.json
partitions: 3
@@ -126,12 +137,14 @@ events:
retention_ms: 604800000
- name: "digital-core.canonical-party.customer.outbound"
role: subscribe-output
+ key: "customer_id" # set by the Flink upsert-kafka sink
table: "customer"
schema: events/schemas/customer.schema.json
partitions: 3
cleanup_policy: compact
- name: "digital-core.canonical-party.contact_address.inbound"
role: publish-input
+ key: "contact_address_id" # producers MUST key by this: order holds per key, per partition
table: "contact_address"
schema: events/schemas/contact_address.schema.json
partitions: 3
@@ -139,12 +152,14 @@ events:
retention_ms: 604800000
- name: "digital-core.canonical-party.contact_address.outbound"
role: subscribe-output
+ key: "contact_address_id" # set by the Flink upsert-kafka sink
table: "contact_address"
schema: events/schemas/contact_address.schema.json
partitions: 3
cleanup_policy: compact
- name: "digital-core.canonical-party.party_role_contact_address.inbound"
role: publish-input
+ key: "party_role_contact_address_id" # producers MUST key by this: order holds per key, per partition
table: "party_role_contact_address"
schema: events/schemas/party_role_contact_address.schema.json
partitions: 3
@@ -152,12 +167,14 @@ events:
retention_ms: 604800000
- name: "digital-core.canonical-party.party_role_contact_address.outbound"
role: subscribe-output
+ key: "party_role_contact_address_id" # set by the Flink upsert-kafka sink
table: "party_role_contact_address"
schema: events/schemas/party_role_contact_address.schema.json
partitions: 3
cleanup_policy: compact
- name: "digital-core.canonical-party.party_association.inbound"
role: publish-input
+ key: "party_association_id" # producers MUST key by this: order holds per key, per partition
table: "party_association"
schema: events/schemas/party_association.schema.json
partitions: 3
@@ -165,6 +182,7 @@ events:
retention_ms: 604800000
- name: "digital-core.canonical-party.party_association.outbound"
role: subscribe-output
+ key: "party_association_id" # set by the Flink upsert-kafka sink
table: "party_association"
schema: events/schemas/party_association.schema.json
partitions: 3
diff --git a/canonical-party/tests/test_streams.py b/canonical-party/tests/test_streams.py
index 190623e..d8ce3f3 100644
--- a/canonical-party/tests/test_streams.py
+++ b/canonical-party/tests/test_streams.py
@@ -43,15 +43,25 @@ def test_record_round_trip(dp):
refused = dp.sql_error(f"INSERT INTO {schema}.{table} ({columns}) VALUES ({values})")
assert refused is None, f"the ODS would refuse the round-trip record, so it is not published: {refused}"
+ inbound = next(t for t in dp.product.topics if t.table == table and t.role == "publish-input")
+ assert inbound.key, f"{inbound.name} declares no key in product.yaml; producers cannot keep changes in order"
+
try:
- # Publish input -> Flink -> ODS
- dp.publish(dp.topic(table, "publish-input"), [record])
+ # Publish input -> Flink -> ODS, keyed as the contract requires
+ dp.publish(inbound.name, [record], key_field=inbound.key)
dp.eventually(lambda: contains(record, (dp.sql(f"SELECT * FROM {schema}.{table} WHERE {where}") or [None])[0]))
# ODS -> Flink CDC -> subscribe output, and ODS -> pg_duckpipe -> lake
dp.eventually(lambda: contains(record, dp.latest(outbound).get(record_key)))
dp.eventually(lambda: dp.trino(f"SELECT count(*) AS n FROM {lake} WHERE CAST({key} AS varchar) = '{record_key}'") == [{"n": 1}])
+ # Two more, published in order under the same key: one partition, so the last one wins
+ column, value = next(iter(update.items()))
+ ordered = [dict(record, **{column: f"{value} first"}), dict(record, **{column: f"{value} second"})]
+ dp.publish(inbound.name, ordered, key_field=inbound.key)
+ dp.eventually(lambda: (dp.sql(f"SELECT {column} FROM {schema}.{table} WHERE {where}") or [{}])[0].get(column)
+ == f"{value} second")
+
# CRUD API update -> subscribe output and lake
assignments = ", ".join(f'{camel(c)}: "{v}"' for c, v in update.items())
data = dp.graphql(
@@ -60,7 +70,6 @@ def test_record_round_trip(dp):
)
assert data[f"update{pascal(table)}Collection"]["affectedCount"] == 1
dp.eventually(lambda: contains(update, dp.latest(outbound).get(record_key)))
- column, value = next(iter(update.items()))
dp.eventually(lambda: dp.trino(f"SELECT CAST({column} AS varchar) AS v FROM {lake} WHERE CAST({key} AS varchar) = '{record_key}'") == [{"v": str(value)}])
# CRUD API delete -> tombstone on subscribe output, row gone from the lake