relayApi

Source processor that serves the relay API, through which a poller fetches the records of a Kafka topic from a Connect instance, in batches, with per-record acknowledgement.

The record relay

A record relay lets a poller read the records of a Kafka topic over HTTP, and the poller opens every connection. A typical use is to move records from a lower to a higher security site, such as a utility’s OT network, when the higher security site accepts no inbound connections. It has two flows:

  • The relay flow, on the site that holds the topic, reads the topic and serves the relay API. It has this source, followed by pollFromKafka in relay mode and any processors that change the records.

  • The poller flow, on the site that reads the records, starts with the receiveFromRelay source. It fetches the next batch of records, delivers each record into its flow, and sends the cursor of the last completed record with its next fetch. The cursor acknowledges every record before it.

The relay API is served at /{ownerId}/rs/{flowId}/relay/v1 with two operations:

  • POST /fetch returns the next batch of records. A fetch with no records available is held until some arrive or waitMillis passes.

  • POST /release commits a final acknowledgement and releases the poller lease at once. It is answered with status 204 and no body.

A malformed request is rejected with status 400. The generated OpenAPI document is served at /{ownerId}/rs/{flowId}/openapi.json without authentication.

RelayRequest

The poller posts a RelayRequest to the relay API, and the source passes it on as the payload of the message that it sends into the pipeline, as a JSON-compliant object. It has the fields of the posted JSON body, and the field operation, which the source adds from the path. The source validates the request before the pipeline sees it. pollFromKafka reads the payload, so processors placed before it must leave it intact.

The fields of the RelayRequest are:

  • operation: String, required. FETCH for a request posted to /fetch, and RELEASE for a request posted to /release. The source sets it from the path.

  • pollerId: String, required, not blank. The identity of the poller source instance, for the poller lease.

  • ack: String, optional. The cursor up to which the poller has fully processed the records. The relay commits it before it reads. Default: none, and the start position of pollFromKafka applies.

  • cursor: String, optional. The read position. Default: the value of ack. A release does not carry it.

  • maxRecords: Number (integer), optional, at least 0. The most records to return, and larger values are capped to maxBatchRecords of pollFromKafka. The value 0 renews the lease only. Default: maxBatchRecords, which is 100 unless the flow sets it. A release does not carry it.

  • waitMillis: Number (long), optional, at least 0. How long to hold the request when no records are available, and larger values are capped to maxWaitMillis of pollFromKafka. Default: maxWaitMillis, which is 0 unless the flow sets it, so a fetch is answered at once and does not long poll. A release does not carry it.

RelayRequest of a fetch:

{
  "operation": "FETCH",
  "pollerId": "site-a-poller-1",
  "ack": "AQAB",
  "cursor": "AQAC",
  "maxRecords": 100,
  "waitMillis": 20000
}

A release does not carry cursor, maxRecords and waitMillis, and ignores them if it does.

RelayRequest of a release:

{
  "operation": "RELEASE",
  "pollerId": "site-a-poller-1",
  "ack": "AQAB"
}

Response

The response to a fetch is built from the output of the flow’s pipeline. The payload is the list of relayed records, and the exchange properties carry the fields of the batch. Every item of the list is a JSON object with the fields:

  • key: String, nullable. The key of the record.

  • payload: String, nullable. The value of the record.

  • attributes: list of objects with name (String, required) and value (String, nullable), the headers of the record. Empty when there are none, and a null value is read as empty.

  • timestamp: Number (long), required. The timestamp of the record, in milliseconds since the epoch.

  • orderingKey: String, required, not blank. Shared by the records that must be processed in order.

  • cursor: String, required, not blank. The position just after the record. Acknowledging it acknowledges the record and every record before it.

A record outside this shape gives status 500.

The source builds the rest of the response from the following exchange properties. A missing or malformed required property also gives status 500:

  • relayNextCursor: required. The cursor for the position after the batch.

  • relayLease: required, a JSON object with pollerId, epoch and ttlMillis.

  • relayPayloadEncoding: required, TEXT or BASE64.

  • relayHasMore: true or false. Defaults to false.

  • relayLostRecords: a JSON list of numbers, such as [3, 12], one for each retention gap: the offset span lost. The source turns each number n into an object {"lostRecords": n} in the gaps of the response. Defaults to none.

Payload of the pipeline’s output:

[
  {
    "key": "crew-4711",
    "payload": "{\"status\": \"EnRoute\"}",
    "attributes": [ { "name": "messageType", "value": "CrewStatus" } ],
    "timestamp": 1762934400000,
    "orderingKey": "0",
    "cursor": "AQAD"
  }
]

Exchange properties of the pipeline’s output:

relayNextCursor = AQAD
relayLease = {"pollerId": "site-a-poller-1", "epoch": 8, "ttlMillis": 60000}
relayPayloadEncoding = TEXT
relayHasMore = false
relayLostRecords = [3]

The poller receives this response body:

{
  "records": [
    {
      "key": "crew-4711",
      "payload": "{\"status\": \"EnRoute\"}",
      "attributes": [ { "name": "messageType", "value": "CrewStatus" } ],
      "timestamp": 1762934400000,
      "orderingKey": "0",
      "cursor": "AQAD"
    }
  ],
  "payloadEncoding": "TEXT",
  "nextCursor": "AQAD",
  "hasMore": false,
  "gaps": [ { "lostRecords": 3 } ],
  "lease": { "pollerId": "site-a-poller-1", "epoch": 8, "ttlMillis": 60000 }
}

Changing the records

Processors between pollFromKafka and the end of the flow can change the records, for example with map. The result must keep these rules:

  • The payload stays a list of records in the shape above.

  • cursor must not be changed. The poller acknowledges it, so a changed cursor corrupts the acknowledgements. The source checks only that it is not blank.

  • key, payload and the attribute values must be in the encoding that relayPayloadEncoding names: plain strings for TEXT, and Base64 text for BASE64. A processor that writes new values for them must use that same encoding.

  • Records can be removed. The next cursor still moves past them, so they do not hold back the acknowledgement.

  • One record cannot be turned into several.

  • The processors must be free of side effects, because a batch is transformed again each time it is read again, for example after a lost response.

  • The relay* exchange properties of the response are set by pollFromKafka and must stay.

Deployment

The relay API endpoints run on every Connect cluster member, so the poller can call any member. The flow’s pipeline, with pollFromKafka, is a singleton: one instance runs in the cluster, on a member that the cluster picks, and each member routes the requests that it receives to that instance. pollFromKafka requires flowPipelineDeploymentStrategy { scope = SINGLETON }, and its validation enforces it. If the member that holds the pipeline stops, the cluster starts the pipeline on another member.

Restrictions

A flow with this source:

  • cannot contain distribute or split, because both forward messages to other flows, and a relay must not be served by a Headless flow.

  • uses the RequestResponse exchange pattern.

  • with pollFromKafka uses flowPipelineDeploymentStrategy { scope = SINGLETON }, see Deployment.

Examples

The relay flow, on the site that holds the topic:

flowConfig {
    id = "crew-updates-relay"
    ownerId = "my-owner"
    exchangePattern = RequestResponse
    flowPipelineDeploymentStrategy { scope = SINGLETON }
    redeliveryStrategy {
        redeliveryMaxNo = 0
        redeliveryMillis = 27_000
    }

    relayApi { id = "relay-api" }

    pollFromKafka {
        id = "read-crew-updates"
        mode = RELAY
        brokerList = "kafka-0.kafka:9093"
        topic = "crew-updates"
        groupId = "crew-updates.relay"
        authenticationConfigKey = "cloudKafka"
        maxBatchRecords = 500
        maxWaitMillis = 20_000
    }
}

The poller flow, on the site that reads the records:

flowConfig {
    id = "crew-updates-poller"
    ownerId = "my-owner"
    exchangePattern = RequestResponse
    deliveryGuarantee = BySourceSystem

    receiveFromRelay {
        id = "crew-updates-relay-source"
        address = "https://relay.example.com/cloud-owner/rs/crew-updates-relay"
        authenticationConfigKey = "cloudConnect"
        nackStrategy = RelayNackStrategy.PAUSE
    }

    restRequest {
        id = "update-crew"
        defaultMethod = POST
        address = "https://adms-crew.example.com/api/crew/UpdateCrew"
    }
}

Properties

Name Summary

name

Optional, descriptive name for the processor.

id

Required identifier of the processor, unique across all processors within the flow. Must be between 3 and 30 characters long; contain only lower and uppercase alphabetical characters (a-z and A-Z), numbers, dashes ("-"), and underscores ("_"); and start with an alphabetical character. In other words, it adheres to the regex pattern [a-zA-Z][a-zA-Z0-9_-]{2,29}.

exchangeProperties

Optional set of custom properties in a simple jdk-format, that are added to the message exchange properties before processing the incoming payload. Any existing properties with the same name will be replaced by properties defined here.

Sub-builders

Name Summary

messageLoggingStrategy

Strategy for describing how a processor’s message is logged on the server.

payloadArchivingStrategy

Strategy for archiving payloads.