blob: b65a3d195d240c4787630b5879d866e282e0cdfd [file] [view]
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
# Apache Pulsar Rust client
A Rust client for [Apache Pulsar](https://pulsar.apache.org) scalable topics, built from the
[Scalable Topics client specification](https://github.com/apache/pulsar/tree/master/spec/scalable-topics) on the
[compio](https://github.com/compio-rs/compio) runtime.
**Status:** the first release, 0.1.0, is being prepared.
Producers publish to scalable topics, keeping per-key order across segment splits and
merges, and to regular `persistent://` topics through the migration bridge. Queue consumers read every segment of a
topic, following its splits and merges, and acknowledge each message. Stream consumers read the segments the topic's
controller assigns them, each key in order, share segments by entry bucket when they outnumber them, and acknowledge
cumulatively. Checkpoint consumers read every segment, parents before their children, or as members of a group the
segments the controller assigns them, and resume from checkpoints the application keeps, in the Java client's bytes.
All work in the thread-safe, local and blocking APIs, with token and mTLS authentication, directly or through a proxy.
Producers batch messages by entry bucket and compress them, as Java's do. Messages carry the Java client's schemas:
bytes, string, the primitive types, key/value, JSON, Avro and Protobuf, and generic records. The design is in
[DESIGN.md](DESIGN.md), the milestones in [PLAN.md](PLAN.md).
```rust
let client = PulsarClient::builder().service_url("pulsar://localhost:6650").build().await?;
let producer = client.new_producer(Schema::string()).topic("topic://public/default/orders").create().await?;
let id = producer.new_message().key("user-42").value("hello".to_string()).send().await?;
producer.close().await?;
let consumer = client.new_queue_consumer(Schema::string())
.topic("topic://public/default/orders")
.subscription_name("workers")
.subscribe().await?;
let message = consumer.receive().await?;
consumer.acknowledge(message.id()).await?;
client.close().await?;
```
## Conformance
Each release states the version of the
[Scalable Topics client specification](https://github.com/apache/pulsar/tree/master/spec/scalable-topics) it conforms
to, and which of the specification's optional capabilities it implements, in its
[release notes](https://github.com/apache/pulsar-client-rust/releases). The client conforms to version 1.0. It
implements the required core, C1 to C15
([*conformance*](https://github.com/apache/pulsar/blob/master/spec/scalable-topics/conformance.md) §2), and these of
the optional capabilities (§3):
| Capability | Implemented |
|---|---|
| Entry-bucketing, producer side | Yes: each batch holds one entry bucket's messages, and each entry is stamped with its bucket hashes. |
| Migration bridge | Yes: `persistent://` topics, partitioned or not, and short topic names. |
| Chunking, compression, custom batching | Compression: LZ4, ZLIB, ZSTD and Snappy. No chunking, and no custom batching. |
| Transactions | No. |
| Namespace subscription | No. |
| Dead-letter | No. |
| End-to-end encryption | No. |
## Layout
| Path | What it is |
|---|---|
| `crates/apache-pulsar` | The client: producers, Queue, Stream and Checkpoint consumers, layout watches, connections, the pool, lookup, authentication, TLS, the memory limit, and the thread-safe, local and blocking APIs. |
| `crates/apache-pulsar/examples` | Runnable examples: a producer, the Queue, Stream and Checkpoint consumers, the local and blocking APIs, schemas, TLS and token authentication, and pipelined sends. |
| `crates/apache-pulsar-protocol` | The sans-IO protocol core: generated protobuf code, framing, the connection, producer and consumer state machines, entry decoding, layouts and routing, message ids. Internal; not a stable API. |
| `crates/apache-pulsar-testkit` | The simulated broker and cluster, and the deterministic tests of the protocol state machines. Not published. |
| `checks/` | Milestone 0 checks: compio behavior, and the handshake against a real broker. |
| `tools/java-vectors` | Generates test vectors from the Java client. |
| `xtask` | Repository tasks: protobuf code generation. |
## Examples
`crates/apache-pulsar/examples` has one example for each part of the API. They run against a broker on this machine,
or the one `PULSAR_URL` names. Start a standalone broker that advertises `localhost`, and once it is up, which takes
a few seconds, run one:
```bash
docker run -d --rm --name pulsar -p 6650:6650 -p 8080:8080 apachepulsar/pulsar:5.0.1 bash -c "echo advertisedAddress=localhost >> conf/standalone.conf && exec bin/pulsar standalone"
```
```bash
cargo run --example producer
```
| Example | What it shows |
|---|---|
| `producer` | Keyed messages with properties, a flush and a close, on the default API from a Tokio runtime. |
| `queue_consumer` | A Shared subscription on every segment: each message acknowledged, one negatively and delivered again. |
| `stream_consumer` | Segments the controller assigns, each key in order, and cumulative acknowledgments. |
| `checkpoint_consumer` | No subscription: a checkpoint kept in a file, and a second run that reads on from it. |
| `local` | The local API: a client per thread, each on its own compio event loop. |
| `blocking` | The blocking API, without an async runtime. |
| `schemas` | JSON, Avro and key/value schemas for a struct, and generic records. |
| `authentication` | TLS with a trusted CA, and a token, a token file or a client certificate. |
| `pipelining` | Many sends in flight, in order, with `buffered`, `FuturesOrdered` and `join_all`. |
## Building and testing
Building needs Rust 1.95 or later, and a C compiler for the ZSTD codec.
```bash
cargo test --workspace
```
The generated protobuf code is committed, so building needs no `protoc`. After changing the vendored
`crates/apache-pulsar-protocol/proto/PulsarApi.proto`, regenerate it (needs `protoc` 33.1, the version CI checks
with):
```bash
cargo run -p xtask -- codegen
```
The handshake against a real broker:
```bash
docker run -d --rm --name pulsar -p 6650:6650 -p 8080:8080 apachepulsar/pulsar:5.0.1 bin/pulsar standalone
```
```bash
cargo run -p first-contact -- 127.0.0.1:6650
```
The integration tests start a broker and a proxy in Docker, with TLS, token and mTLS authentication, and remove
them when they finish. A Java tool they run in the broker container is compiled in an `eclipse-temurin` JDK
container. `cargo test` skips them; they need Docker running:
```bash
cargo test -p apache-pulsar --test integration
```
## License
[Apache License 2.0](LICENSE).