9. Classic API

Previous: 8. Quality.

Status: proposed. What is decided is the direction: a classic (v4) API for regular topics, in a crate of its own, so that other languages can wrap this client's core. The rest of this document is a proposal until it is settled; §12 lists what is open.

1. Why

apache-pulsar is a scalable-topics client. It reaches a persistent:// topic only through the migration bridge (6 §8), which needs a 5.0 broker, and there it offers the V5 API: Stream, Queue and Checkpoint consumers, not the classic subscription types.

The clients that other languages use today offer the classic API, and every topic created before 5.0 is a regular topic. A core that those languages wrap has to offer that API too, and work with the brokers their users run. The classic crate does both, over the same connections and the same single-topic engine as apache-pulsar, so that the two don't drift apart.

2. Scope

The classic crate is Java‘s v4 client API (org.apache.pulsar.client.api), which the other languages’ clients follow, written the way apache-pulsar writes Java V5's (7). Proposed for the first version:

AreaFirst versionNotes
Producer, on a topic partitioned or notYesRouting modes and hashing schemes, batching, compression, access modes, dedup, delivery time (§6.1).
Consumer: Exclusive, Shared, Failover, Key_SharedYesAcks, negative acks, ack timeout, seek, unsubscribe; partitioned topics (§6.2).
Consumer on several topics, by list or by patternYes§6.3.
ReaderYes§6.4.
Schemas, authentication, TLSYesapache-pulsar's, shared (§3).
non-persistent:// topicsLaterUnless the first languages need them (§12, question 4).
Dead-letter and retry topics, chunking, end-to-end encryption, transactionsLaterBuilt once in the shared engine, for both crates (Plan).
Table views, broker-confirmed acksLater
HTTP lookup, an admin clientNoAs for apache-pulsar (1 §5).

What the first version has to cover depends on which languages wrap it first (§12, question 4).

3. Crates

A classic API could have been a module of apache-pulsar. A crate of its own needs the engine somewhere both crates can reach. Proposed: a crate of its own for the engine.

CrateContentsStability
apache-pulsar-protocolAs today, plus the classic state machines (§7).Internal: 0.x, pinned by an exact version.
apache-pulsar-core (new)Moved out of apache-pulsar: the driver, the connections, the loops and the single-topic engine, and what both APIs share: the client, authentication, schemas and errors.Internal, like the protocol crate.
apache-pulsarThe scalable layer and the V5 API.Semver, as today.
apache-pulsar-classic (new)The classic API.Semver.

A type both APIs expose, a schema or an error, is defined once in core and re-exported by both crates, so changing it changes both crates' APIs. The classic crate carries none of the scalable layer, and a binding links both API crates over one core.

The alternative is for the classic crate to depend on apache-pulsar, which would expose its engine in a hidden module. It is a smaller change, but it puts an unstable surface inside the crate that makes semver promises, and classic users would compile the scalable layer.

4. One client for both

An application or a binding that uses both kinds of topics should run one set of loops and one connection pool: two clients would double the threads, and the connections to each broker. So the loops, the pools and the client‘s configuration live in core, and each API crate’s client is a handle on a core client. How an application gets both handles on one core client is settled with the API (§12, question 6).

5. API

The same shape as apache-pulsar (7):

  • Three APIs, one implementation: thread-safe (the default), local and blocking (7 §1), with builders over plain-data configuration (7 §11).
  • Client: PulsarClient, with new_producer, new_consumer and new_reader, each taking a schema.
  • Names follow Java‘s v4 builders, in Rust’s case, as apache-pulsar does for V5.
  • Errors: apache-pulsar's Error and ErrorKind, plus the kinds the classic API needs.
  • MessageId: Java‘s v4 message id: ledger, entry, partition and batch index. Its bytes are Java’s MessageId.toByteArray(), so that a position stored by one SDK can be restored by another. The protocol crate's EntryId is that id already; the V5 id embeds it (7 §7).

6. Behavior

Java's v4 client is the reference wherever this document says nothing else, as Java V5 is for apache-pulsar.

6.1 Producer

  • Partitioned topics. CommandPartitionedTopicMetadata gives the partition count. The producer runs one single-topic producer per partition, all on its loop, as a scalable producer runs one per segment. It checks the count again every minute (Java's autoUpdatePartitionsInterval) and adds producers for new partitions.

  • Routing as in Java: round robin by default, where a keyed message goes to its key‘s partition and the others rotate; a single partition; or the application’s router.

  • Hashing schemes. Each client picks a keyed message's partition with a default hash of its own, and no two agree:

    • Java: JavaStringHash, String.hashCode() without its sign bit.
    • C++: BoostHash, Boost‘s string hash. Python, which wraps the C++ client, has no setting for it; Node, which wraps it too, uses it unless told otherwise. The C++ client’s JavaStringHash hashes the UTF-8 bytes, so it matches Java's on ASCII keys only.
    • Go: JavaStringHash too, over the UTF-8 bytes and without clearing the sign bit. It differs from Java‘s on keys that aren’t ASCII, and on about half of random keys when the partition count isn't a power of two.
    • The migration bridge, in Java V5 and apache-pulsar: Murmur3_32Hash, as the spec has it (6 §8).

    Murmur3_32Hash is the one scheme that Java, C++ and Go compute alike for every key. The classic producer offers it and JavaStringHash, Java‘s two. Its default, and whether a binding that replaces the C++ or Go client reproduces that client’s scheme, are open (§12, question 7).

  • Batching with Java v4‘s defaults, which are V5’s: on, 1 ms, 1000 messages, 128 KiB. A partition has one open batch: the engine's one-bucket case (5 §2).

  • Compression as in apache-pulsar, with the v4 setting V5 doesn't expose: Java v4 leaves a payload of 4 KiB or less uncompressed (compressMinMsgBodySize).

  • Access modes, sequence ids, send timeout, delivery time: the engine‘s, per partition, as Java’s v4 ProducerImpl has them.

  • What a v4 client would see differently. The engine writes every entry in the batch format, so a lone message is a batch of one, with batch index 0 in its id. Java writes a lone message as a plain entry, with batch index -1, whether batching is on or off (§12, question 8). The migration bridge marks its producers and consumers with __pulsar.v5.managed=true (6 §8); classic ones don't carry it.

6.2 Consumer

  • Subscription types: Exclusive (Java's default), Shared, Failover and Key_Shared.
  • Failover: the broker picks the active consumer and tells each one with CommandActiveConsumerChange. The consumer passes it to an optional listener, as Java's ConsumerEventListener does.
  • Key_Shared: auto-split hash ranges, Java's default, or sticky ranges the application gives. The engine subscribes Key_Shared only with entry-bucket dispatch today (5 §3).
  • Acks: the engine's individual, cumulative and batch-index acks, grouped, and its negative acks (5 §3). Added: the ack timeout, off by default as in Java (5 §6, question 1).
  • Seek by message id or by publish time (CommandSeek). The consumer drops what it holds, as Java's does.
  • Partitioned topics: one single-topic consumer per partition, all on the consumer‘s loop, with one receive queue. Partitions added later are subscribed, on the same one-minute check as the producer’s.

6.3 Several topics

A consumer on a list of topics, or on the topics of one namespace that match a pattern. As in Java, the pattern‘s topics come from CommandGetTopicsOfNamespace, then from the topic-list watch (CommandWatchTopicList) where the broker advertises supports_topic_watchers, which the connection reads already. A check every minute by default reconciles the list, through the watch when it is connected and with CommandGetTopicsOfNamespace otherwise. The V5 API’s namespace subscription, a later capability, needs the same commands.

6.4 Reader

The engine‘s reader (5 §4), on a topic partitioned or not, plus what the classic reader adds: an inclusive start, seek, and has_message_available, which asks for the topic’s last message id with CommandGetLastMessageId, as the Checkpoint consumer does already.

7. The engine, apart from the scalable layer

2 §1 has the segment engine as the classic client for one topic, under the scalable layer. The consumer‘s code is cut that way: SegmentConsumer, in the protocol crate, is one topic’s consumer. The producer‘s isn’t yet:

  • The protocol crate‘s Producer is the whole scalable producer. Its per-segment state is one struct, but that struct also holds the scalable layer’s: whether the segment retires, its parked messages, its bucket boundaries. The code that registers, batches and completes sends is in the scalable Producer.
  • The tasks that run producers and consumers own the layout watch, and feed the layouts to the state machines.

So the first step carves out a single-topic producer, which the scalable producer and the classic producer both drive, and splits each task‘s work on one topic from its following of layouts. It changes no behavior: the existing tests pass unchanged. How big it is shows only once it starts, which makes it the plan’s main risk.

The protocol crate also needs what the scalable layer never used:

  • CommandPartitionedTopicMetadata, whose generated types are there, unused.
  • CommandGetTopicsOfNamespace and the topic-list watch.
  • Failover subscriptions and CommandActiveConsumerChange.
  • CommandSeek.
  • Key_Shared without entry-bucket dispatch.
  • The ack timeout.

8. Brokers

apache-pulsar needs 5.0 (1 §3). The classic crate sends no scalable-topic command, so nothing ties it to 5.0, and its users' brokers are older too. The connection layer records a broker's feature flags and refuses none; only scalable-topic commands need supports_scalable_topics (4 §3). But nothing has run below 5.0.1 yet, and an older broker lacks some features, which the client then does without, as Java does: the topic-list watch, for one.

Proposed: support every Pulsar release line still maintained, and run the integration tests against each one (§12, question 2).

9. Testing

  • The simulated broker serves both crates: a classic topic is its single-topic case, with partitioned metadata, Failover and seek added (8 §2).
  • Real brokers: the integration tests against a broker of each supported line.
  • Java v4 clients alongside, as the migration bridge's tests run them: Java and Rust producing to and consuming from one subscription, keys on the same partitions, message-id bytes that round-trip.
  • Java's v4 tests. Their client scenarios are spread through the broker's test suite. Mapping them as tests/PORTING.md maps the V5 tests (8 §2.1) is a step of its own.

10. Releases

apache-pulsar-core and apache-pulsar-classic join the release (RELEASE.md): its cargo publish commands and their order, the CI dry run, the minimum-Rust-version job, and an exact pin on core, as on the protocol crate. One source release holds every crate, and RAT checks the license header of every new file.

11. Bindings (later)

Not designed here. The classic crate keeps what 1 §6 asks of a core that others wrap: the thread-safe API owns its threads, its futures run on any executor, and configuration is plain data. Whether the bindings share one C ABI or use native bindings such as PyO3 and napi-rs is decided with them.

12. Open questions

  1. Layout. The engine, the connections and the loops in apache-pulsar-core (§3, proposed), or the classic crate on apache-pulsar?
  2. Brokers. Which is the oldest release line the classic crate supports (§8)?
  3. Timing. Proposed: after the first release. Moving the engine needn't change apache-pulsar's public API.
  4. The first languages. Which ones wrap the core first? They decide what the first version covers (§2).
  5. Names. Proposed: apache-pulsar-classic and apache-pulsar-core. The docs already say “classic”; “v4” would tie the name to a version, and “legacy” sounds deprecated.
  6. One client for both. How does an application get a V5 and a classic handle on one core client (§4)?
  7. Hashing. Which scheme does the classic producer use by default: JavaStringHash, which matches Java v4 producers, or Murmur3_32Hash, which matches the migration bridge and which every client that offers it computes alike? And does a binding that replaces the C++ or Go client reproduce that client‘s scheme, BoostHash or Go’s JavaStringHash, so that its users' keys keep their partitions (§6.1)?
  8. Lone messages. Keep one encoding path, the batch format, as the engine decided, or write a lone message as a plain entry on classic topics, as Java does (§6.1)?