Skip to content

Commit

Permalink
Refine docs (#52)
Browse files Browse the repository at this point in the history
* refine docs

* add licenses

* add monolake architecture design

* refine the docs

* refine the docs

* update third-party license

* update README

* address comments and fix the license

* address comments

---------

Co-authored-by: Rain Jiang <[email protected]>
  • Loading branch information
rainj-me and Rain Jiang authored Jan 2, 2024
1 parent fec5488 commit 5a770d9
Show file tree
Hide file tree
Showing 28 changed files with 2,910 additions and 48 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
*.pem

/examples/certs
/docs/cloudwego.github.io
File renamed without changes.
25 changes: 25 additions & 0 deletions LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Copyright (c) 2023 Monolake Contributors

Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
356 changes: 356 additions & 0 deletions LICENSE-THIRD-PARTY

Large diffs are not rendered by default.

78 changes: 61 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,71 @@
# Monolake

Introducing Monolake: a Rust-based proxy with a focus on performance and scale. Leveraging the Monoio runtime and harnessing io-uring, Monolake capitalizes on Rust's efficiency and memory safety.
Monolake is a Rust-based high performance Layer 4/7 proxy framework which is built on the [Monoio](https://github.com/bytedance/monoio) runtime.

# Generating Wiki
## Quick Start

The docs folder will eventually move to [cloudwego.io](https://www.cloudwego.io/). You'll have to generate them locally for now.
The following guide is trying to use monolake with the basic proxy features.

## 1. Install Hugo
### Preparation

- Install a recent release of the Hugo "extended" version. If you install from the [Hugo release page](https://github.com/gohugoio/hugo/releases), make sure you download the `_extended` version which supports SCSS.
```bash
# install rust toolchain
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

- If you have installed the latest version of Go, you can install directly by running the following command:
```
go install -tags extended github.com/gohugoio/hugo@latest
```
# clone repo
git clone https://github.com/cloudwego/monolake.git
cd monolake

- Alternatively, you can use the package manager to install Hugo:
- For Linux: `sudo apt install hugo`
- For macOS: `brew install hugo`
# generate certs
mkdir examples/certs && openssl req -x509 -newkey rsa:2048 -keyout examples/certs/key.pem -out examples/certs/cert.pem -sha256 -days 365 -nodes -subj "/CN=monolake.cloudwego.io"
```

## 2. Run `wiki.sh`
### Build

- Execute the `wiki.sh` script, which performs the following tasks:
- Checks out the [cloudwego/cloudwego.github.io](https://github.com/cloudwego/cloudwego.github.io) repository.
- Copies the local `docs` folder to `content/en/docs/monolake/` within the cloned repository.
- Starts the Hugo server for preview.
```bash
# build dev binary
cargo build

# build release binary
cargo build --release

# build lto release binary
cargo build --profile=release-lto
```

### Run examples

```bash
# run example with debug version
target/debug/monolake -c examples/config.toml

# enable debug logging level
RUST_LOG=debug target/debug/monolake -c examples/config.toml

# send https request
curl -kvvv https://localhost:8082/
```

## Limitations

1. On Linux 5.6+, both uring and epoll are supported
2. On Linux 2.6+, only epoll is supported
3. On macOS, kqueue is used
4. Other platforms are currently not supported

## Call for help

Monoio is a subproject of [CloudWeGo](https://www.cloudwego.io).

Due to the limited resources, any help to make the monolake more mature, reporting issues or requesting features are welcome. Refer the [Contributing](./CONTRIBUTING.md) documents for the guidelines.

## Dependencies

- [monoio](https://github.com/bytedance/monoio), Rust runtime
- [monoio-codec](https://github.com/monoio-rs/monoio-codec), framed reader or writer
- [monoio-tls](https://github.com/monoio-rs/monoio-tls), tls wrapper for monoio
- [monoio-http](https://github.com/monoio-rs/monoio-http), http protocols implementation base monoio

## License

Monoio is licensed under the MIT license or Apache license.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file added docs/http_conn_reuse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/https_conn_reuse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
109 changes: 109 additions & 0 deletions docs/monolake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Monolake, a proxy framework base on Rust and io_uring

Earlier 2023, Cloudflare released a blog to introduce their Oxy, a Rust-based modern proxy framework. We have the similar requirements at Volcano Engine (a public cloud from Bytedance Inc.), so we start Monolake project, a layer 4/7 proxy framework base on Rust and io_uring.

## Architecture of Monolake

There are 3 major categories in monolake, the runtime & transport, the tls and the http. Monolake currently supoprt io_uring and epoll runtime which are benefit from monoio(a thread-per-core rust runtime). The layer 4 proxy is implemented in monolake. The tls category currently support both rustls and native-tls, user can switch between these two solutions in case there is critical security defect in one of them. For the http category, monolake support http/1.1 and h2, we are currently working on the thrift protocol support, the grpc and h3 protocol support is planned.

```plain
+-----------+ +-----------+ +-----------+ +-----------+ +------------+ +-----------+
| HTTP | | HTTP/1.1 | | H2 | |monoio-http| |monoio-codec| | monolake |
+-----------+ +-----------+ +-----------+ +-----------+ +------------+ +-----------+
+-----------+ +-----------+ +-----------+ +-----------+
| TLS | | rustls | |native-tls | |monoio-tls |
+-----------+ +-----------+ +-----------+ +-----------+
+-----------+ +-----------+ +-----------+ +-----------+ +-----------+
|Runtime/ | | io_uring | | epoll | | monoio | | monolake |
|Transport | | | | | | | | |
+-----------+ +-----------+ +-----------+ +-----------+ +-----------+
```

Figure 1, the layers and monolake architecture

Besides multi-protocols and proxy features, monolake provides the ability to update the handler chains at runtime. Combine with the linux SO_REUSEPORT socket option, users can upgrade monolake binary at runtime.

## Why monoio runtime with Thread-per-Core and io_uring support

As we known, performance is one of the main factor for layer 4 and layer 7 proxy framework. To achieve this goal during the design phases, we decide to choose thread-per-core with CPU binding and io_uring as our major features. With thread-per-core feature, monolake can avoid the the context switch and inter process communication. With io_uring feature, monolake can avoid copy the memory between kernel space and the userspace. With these in mind, we carefully compare the Rust runtime between tokio, glommio and monoio. Tokio is the most popular asynchronous Rust runtime which has the most mature communities, but its thread model is not thread-per-core, and the async task can schedule on any thread which may requires context switch and lots of cross threads communication. Glommio and monoio are similar, both support io_uring and thread-per-core, we finally choose monoio due to the supportive from monoio community.

After finalize the monoio runtime, we manage to implement a simple http(s) proxy POC(proof-of-concept) base on it. We run some comparison test between the POC and nginx, and find we can achieve almost 10% performance gain in following diagrams. The X-axis is the workers count, they are 4, 8, 16 and 32, the Y-axis is http requests per second reported by wrk.

![http connection reuse](./http_conn_reuse.png)
Figure 2. http connection reuse between POC and nginx

![https connection reuse](./https_conn_reuse.png)
Figure 3. https connection reuse between POC and nginx

## Runtime handler chain update

Monolake's handler chain is organized as onion model, same concept as tower.rs' layer. The service (handler) trait is defined with the async fn in trait feature (will be stable at rust toolchain 1.75), as below

```rust
pub trait Service<Request> {
/// Responses given by the service.
type Response;
/// Errors produced by the service.
type Error;

/// Process the request and return the response asynchronously.
fn call(&self, req: Request) -> impl Future<Output = Result<Self::Response, Self::Error>>;
}
```

Code Block 1. the service (handler) trait.

```plain
+---------------------------------+
| Service C |
| +----------------------+ |<---- Req
| | Service B | <----- |
| | +-----------+ <----- | |
| | | Service A | -----> | |
| | +-----------+ | -----> |
| +----------------------+ |----> Resp
+---------------------------------+
```

Figure 4. monolake handler chain diagram

Since we build monolake as static compiling, it is hard to change the state of the handler at runtime. To tackle the problem, we proposal to rebuild the handlers chain at runtime.

```plain
+-------------------------------------+ +--------------------------------------------------------------+
| Main Thread | | Worker Thread |
| | | |
| | | +---------------------------------+ |
| | | | Old Svc C | |
| +---------------------------------+ | | | +----------------------+ | |
| | Factory C | | | | | Old Svc B | | |
| | +----------------------+ | | | | | +-----------+ | | |
| | | Factory B | | | | Recreate | | | Old Svc A | | | |
| | | +-----------+ | +-------------------+ | | +-----------+ | | |
| | | | Factory A | | | | | | | +----------------------+ | |
| | | +-----------+ | | | | | +---------------------------------+ |
| | +----------------------+ | | | | |
| +---------------------------------+ | | v |
| | | +------+--------------------------+ |
| | | | New Svc C | |
| | | | +----------------------+ +<---+ Req |
+-------------------------------------+ | | | New Svc B | <----+ | |
| | | +-----------+ <----+ | | |
| | | | New Svc A | +----> | | |
| | | +-----------+ | +----> | |
| | +----------------------+ +----> Resp |
| +---------------------------------+ |
| |
+--------------------------------------------------------------+
```

Figure 5. monolake handler chain runtime upgrade

Refer the [sample code](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=021cf8353df756eb39b578fe9e29e36b) for the runtime handler chain update proposal. This proposal is also working under tokio runtime, if we implement the handlers chain with tokio runtime.

## Next steps

For the next steps, we plan to support Quic and H3 as our highest priority. We also would like to re-implement the H2 instead of directly modifying the code from h2 crate. We also plan to support additional userspace event library like dpdk.
20 changes: 20 additions & 0 deletions docs/wiki_on_cloudwego.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Overview

The cloudwego folder contains all the wiki content host on [CloudWeGo](https://www.cloudwego.io).

## Preview Wiki

``` bash
# install hugo
brew install hugo

# clone the cloudwego repo
git clone https://github.com/cloudwego/cloudwego.github.io

# sync the content to cloudwego folder
rsync -a cloudwego cloudwego.github.io/content/en/docs/monolake

# run hugo server
cd cloudwego.github.io
hugo server --bind ::1 -D
```
Loading

0 comments on commit 5a770d9

Please sign in to comment.