RuSerf

A highly customable, adaptable, runtime agnostic and WASM/WASI friendly decentralized solution for service discovery and orchestration that is lightweight, highly available, and fault tolerant. Port and improve [HashiCorp's serf](https://github.com/hashicorp/serf) to Rust. [github][Github-url] LoC [Build][CI-url] [codecov][codecov-url] [docs.rs][doc-url] [crates.io][crates-url] [crates.io][crates-url] license English | [简体中文][zh-cn-url]
## Introduction ruserf is a decentralized solution for service discovery and orchestration that is lightweight, highly available, and fault tolerant. The use cases for such a library are far-reaching: all distributed systems require membership, and ruserf is a re-usable solution to managing cluster membership and node failure detection. ruserf is eventually consistent but converges quickly on average. The speed at which it converges can be heavily tuned via various knobs on the protocol. Node failures are detected and network partitions are partially tolerated by attempting to communicate to potentially dead nodes through multiple routes. ruserf is WASM/WASI friendly, all crates can be compiled to `wasm-wasi` and `wasm-unknown-unknown` (need to configure the crate features). ### Design Unlike the original Go implementation, Rust's ruserf use highly generic and layered architecture, users can easily implement a component by themselves and plug it to the ruserf. Users can even custom their own `Id` and `Address`. Here are the layers: - **Transport Layer** By default, Rust's ruserf provides two kinds of transport -- [`QuicTransport`](https://docs.rs/ruserf-quic/struct.QuicTransport.html) and [`NetTransport`](https://docs.rs/ruserf-net/struct.NetTransport.html). - **Runtime Layer** Async runtime agnostic are provided by [`agnostic`'s Runtime](https://docs.rs/agnostic/trait.Runtime.html) trait, `tokio`, `async-std` and `smol` are supported by default. Users can implement their own [`Runtime`](https://docs.rs/agnostic/trait.Runtime.html) and plug it into the ruserf. - **Address Resolver Layer** The address resolver layer is supported by [`nodecraft`'s AddressResolver](https://docs.rs/nodecraft/latest/nodecraft/resolver/trait.AddressResolver.html) trait. - **Serialize/Deserilize Layer** By default, Rust's ruserf is using [`length-prefix encoding (Lpe)`](https://docs.rs/ruserf-core/transport/struct.Lpe.html) to serialize/deserialize messages to bytes or visa-vise. The implemention of `Lpe` tries the best to avoid reallocating when doing the serialize/deserialize. But, users can use any other serialize/deserialize framework by implementing [`Wire`](https://docs.rs/ruserf-core/transport/trait.Wire.html) trait. - **[`NetTransport`](https://docs.rs/ruserf-net/struct.NetTransport.html)** Three kinds of different builtin stream layers for `NetTransport`: - [`Tcp`](https://docs.rs/ruserf-net/stream_layer/tcp/struct.Tcp.html): based on TCP and UDP - [`Tls`](https://docs.rs/ruserf-net/stream_layer/tls/struct.Tls.html): based on [`rustls`](https://docs.rs/rustls) and UDP - [`NativeTls`](https://docs.rs/ruserf-net/stream_layer/tls/struct.NativeTls.html): based on [`native-tls`](https://docs.rs/native-tls) and UDP - **[`QuicTransport`](https://docs.rs/ruserf-quic/struct.QuicTransport.html)** QUIC transport is an experimental transport implementation, it is well tested but still experimental. Two kinds of different builtin stream layers for `QuicTransport`: - [`Quinn`](https://docs.rs/ruserf-quic/stream_layer/quinn/struct.Quinn.html): based on [`quinn`](https://docs.rs/quinn) - [`S2n`](https://docs.rs/ruserf-quic/stream_layer/s2n/struct.S2n.html): based on [`s2n-quic`](https://docs.rs/s2n-quic) Users can still implement their own stream layer for different kinds of transport implementations. - **Delegate Layer** This layer is used as a reactor for different kinds of messages. - **`Delegate`** Delegate is the trait that clients must implement if they want to hook into the gossip layer of Serf. All the methods must be thread-safe, as they can and generally will be called concurrently. Here are the sub delegate traits: - **`MergeDelegate`** Used to involve a client in a potential cluster merge operation. Namely, when a node does a promised push/pull (as part of a join), the delegate is involved and allowed to cancel the join based on custom logic. The merge delegate is NOT invoked as part of the push-pull anti-entropy. - **`TransformDelegate`** A delegate for encoding and decoding. Used to control how `ruserf` should encode/decode messages. - **`ReconnectDelegate`** Used to custom reconnect behavior, users can implement to allow overriding the reconnect timeout for individual members. - **`CompositeDelegate`** CompositeDelegate is a helpful struct to split the `Delegate` into multiple small delegates, so that users do not need to implement full `Delegate` when they only want to custom some methods in the Delegate. ### Protocol ruserf is based on ["SWIM: Scalable Weakly-consistent Infection-style Process Group Membership Protocol"](http://ieeexplore.ieee.org/document/1028914/). However, Hashicorp developers extends the protocol in a number of ways: Several extensions are made to increase propagation speed and convergence rate. Another set of extensions, that Hashicorp developers call Lifeguard, are made to make ruserf more robust in the presence of slow message processing (due to factors such as CPU starvation, and network delay or loss). For details on all of these extensions, please read Hashicorp's paper ["Lifeguard : SWIM-ing with Situational Awareness"](https://arxiv.org/abs/1707.00788), along with the ruserf source. ## Installation ```toml [dependencies] ruserf = "0.1" ``` ## Q & A - ***Does Rust's ruserf implemenetation compatible to Go's serf?*** No but yes! By default, it is not compatible. But the secret is the serialize/deserilize layer, Go's serf use the msgpack as the serialization/deserialization framework, so in theory, if you can implement a [`TransformDelegate`](https://docs.rs/ruserf-core/transport/trait.TransformDelegate.html) trait which compat to Go's serf, then it becomes compatible. - ***If Go's serf adds more functionalities, will this project also support?*** Yes! And this project may also add more functionalities whereas the Go's serf does not have. e.g. wasmer support, bindings to other languages and etc. ## Related Projects - [`agnostic`](https://github.com/al8n/agnostic): helps you to develop runtime agnostic crates - [`nodecraft`](https://github.com/al8n/nodecraft): crafting seamless node operations for distributed systems, which provides foundational traits for node identification and address resolution. - [`transformable`](https://github.com/al8n/transformable): transform its representation between structured and byte form. - [`peekable`](https://github.com/al8n/peekable): peekable reader and async reader - [`memberlist`](https://github.com/al8n/memberlist): A highly customable, adaptable, runtime agnostic and WASM/WASI friendly Gossip protocol which helps manage cluster membership and member failure detection. #### License `ruserf` is under the terms of the MPL-2.0 license. See [LICENSE](./LICENSE) for details. Copyright (c) 2024 Al Liu. Copyright (c) 2013 HashiCorp, Inc. [Github-url]: https://github.com/al8n/ruserf/ [CI-url]: https://github.com/al8n/ruserf/actions/workflows/ci.yml [doc-url]: https://docs.rs/ruserf [crates-url]: https://crates.io/crates/ruserf [codecov-url]: https://app.codecov.io/gh/al8n/ruserf/ [zh-cn-url]: https://github.com/al8n/ruserf/tree/main/README-zh_CN.md