Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/tikv/client-rust into and…
Browse files Browse the repository at this point in the history
…ylokandy/master
  • Loading branch information
andylokandy committed Dec 10, 2023
2 parents 2e81b06 + bbaf317 commit 32060bb
Show file tree
Hide file tree
Showing 68 changed files with 14,263 additions and 431 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/generated/ diff=false
41 changes: 19 additions & 22 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ on:
push:
branches:
- master
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

env:
CARGO_TERM_COLOR: always

name: CI

Expand All @@ -11,42 +17,36 @@ jobs:
name: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
override: true
- run: rustup component add rustfmt clippy
- uses: actions/checkout@v4
- name: Install Protoc
uses: arduino/setup-protoc@v1
with:
version: '3.x'
repo-token: ${{ secrets.GITHUB_TOKEN }}
- name: Rust Cache
uses: Swatinem/rust-cache@v1.4.0
uses: Swatinem/rust-cache@v2
- name: make check
run: make check
- name: Catch unexpected changes in the generated code
run: |
git diff --exit-code
unit-test:
name: unit test
env:
CARGO_INCREMENTAL: 0
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
override: true
- uses: actions/checkout@v4
- name: Install Protoc
uses: arduino/setup-protoc@v1
with:
version: '3.x'
repo-token: ${{ secrets.GITHUB_TOKEN }}
- name: Rust Cache
uses: Swatinem/[email protected]
uses: Swatinem/rust-cache@v2
- name: Install latest nextest release
uses: taiki-e/install-action@nextest
- name: unit test
run: make unit-test

Expand All @@ -56,19 +56,14 @@ jobs:
CARGO_INCREMENTAL: 0
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
override: true
- uses: actions/checkout@v4
- name: Install Protoc
uses: arduino/setup-protoc@v1
with:
version: '3.x'
repo-token: ${{ secrets.GITHUB_TOKEN }}
- name: Rust Cache
uses: Swatinem/rust-cache@v1.4.0
uses: Swatinem/rust-cache@v2
- name: install tiup
run: curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh
- name: start tiup playground
Expand All @@ -81,5 +76,7 @@ jobs:
[[ "$(curl -I http://127.0.0.1:2379/pd/api/v1/regions 2>/dev/null | head -n 1 | cut -d$' ' -f2)" -ne "405" ]] || break
sleep 1
done
- name: Install latest nextest release
uses: taiki-e/install-action@nextest
- name: integration test
run: MULTI_REGION=1 make integration-test
25 changes: 13 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tikv-client"
version = "0.2.0"
version = "0.3.0"
keywords = ["TiKV", "KV", "distributed-systems"]
license = "Apache-2.0"
authors = ["The TiKV Project Authors"]
Expand All @@ -9,14 +9,21 @@ description = "The Rust language implementation of TiKV client."
edition = "2021"

[features]
default = ["prometheus/process"]
default = ["prometheus"]
prometheus = ["prometheus/push", "prometheus/process"]
# Enable integration tests with a running TiKV and PD instance.
# Use $PD_ADDRS, comma separated, to set the addresses the tests use.
integration-tests = []

[lib]
name = "tikv_client"

[workspace]
members = [
".",
"proto-build",
]

[dependencies]
async-recursion = "0.3"
async-trait = "0.1"
Expand All @@ -27,19 +34,18 @@ futures = { version = "0.3" }
lazy_static = "1"
log = "0.4"
pin-project = "1"
prometheus = { version = "0.13", features = ["push"], default-features = false }
prost = "0.11"
prometheus = { version = "0.13", default-features = false }
prost = "0.12"
rand = "0.8"
regex = "1"
semver = "1.0"
serde = "1.0"
serde_derive = "1.0"
thiserror = "1"
tokio = { version = "1", features = ["sync", "rt-multi-thread", "macros"] }
tonic = { version = "0.9", features = ["tls"] }
tonic = { version = "0.10", features = ["tls"] }

[dev-dependencies]
tempfile = "3.6"
clap = "2"
env_logger = "0.10"
fail = { version = "0.4", features = ["failpoints"] }
Expand All @@ -51,14 +57,9 @@ reqwest = { version = "0.11", default-features = false, features = [
serde_json = "1"
serial_test = "0.5.0"
simple_logger = "1"
tempfile = "3.6"
tokio = { version = "1", features = ["sync", "rt-multi-thread", "macros"] }

[build-dependencies]
glob = "0.3"
tonic-build = "0.9"
# Suppress doctest bug (https://stackoverflow.com/questions/66074003/how-to-turn-off-cargo-doc-test-and-compile-for-a-specific-module-in-rust)
tonic-disable-doctest = "0.1.0"

[[test]]
name = "failpoint_tests"
path = "tests/failpoint_tests.rs"
Expand Down
28 changes: 11 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,40 @@ export RUSTFLAGS=-Dwarnings

.PHONY: default check unit-test integration-tests test doc docker-pd docker-kv docker all

PD_ADDRS ?= "127.0.0.1:2379"
MULTI_REGION ?= 1
export PD_ADDRS ?= 127.0.0.1:2379
export MULTI_REGION ?= 1

ALL_FEATURES := integration-tests

INTEGRATION_TEST_ARGS := --features "integration-tests"

default: check

check:
generate:
cargo run -p tikv-client-proto-build

check: generate
cargo check --all --all-targets --features "${ALL_FEATURES}"
cargo fmt -- --check
cargo clippy --all-targets --features "${ALL_FEATURES}" -- -D clippy::all

unit-test:
cargo test --all --no-default-features
unit-test: generate
cargo nextest run --all --no-default-features

integration-test:
integration-test: generate
cargo test txn_ --all ${INTEGRATION_TEST_ARGS} -- --nocapture
cargo test raw_ --all ${INTEGRATION_TEST_ARGS} -- --nocapture
cargo test misc_ --all ${INTEGRATION_TEST_ARGS} -- --nocapture

test: unit-test integration-test

doc:
doc:
cargo doc --workspace --exclude tikv-client-proto --document-private-items --no-deps

# Deprecated
# docker-pd:
# docker run -d -v $(shell pwd)/config:/config --net=host --name pd --rm pingcap/pd:latest --name "pd" --data-dir "pd" --client-urls "http://127.0.0.1:2379" --advertise-client-urls "http://127.0.0.1:2379" --config /config/pd.toml

# docker-kv:
# docker run -d -v $(shell pwd)/config:/config --net=host --name kv --rm --ulimit nofile=90000:90000 pingcap/tikv:latest --pd-endpoints "127.0.0.1:2379" --addr "127.0.0.1:2378" --data-dir "kv" --config /config/tikv.toml

# docker: docker-pd docker-kv

tiup:
tiup playground nightly --mode tikv-slim --kv 3 --without-monitor --kv.config $(shell pwd)/config/tikv.toml --pd.config $(shell pwd)/config/pd.toml &

all: check doc test
all: generate check doc test

clean:
cargo clean
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ The TiKV client is a Rust library (crate). To use this crate in your project, ad

```toml
[dependencies]
tikv-client = "0.2.0"
tikv-client = "0.3"
```

### Prerequisites
Expand Down Expand Up @@ -113,7 +113,11 @@ We welcome your contributions! Contributing code is great, we also appreciate fi

## Building and testing

We use the standard Cargo workflows, e.g., `cargo build` to build and `cargo test` to run unit tests. You will need to use a nightly Rust toolchain to build and run tests.
We use the standard Cargo workflows, e.g., `cargo build` to build and `cargo test/nextest` to run unit tests. You will need to use a nightly Rust toolchain to build and run tests. Could use [nextest](https://nexte.st/index.html) to speed up ut, install nextest first.

```
cargo install cargo-nextest --locked
```

Running integration tests or manually testing the client with a TiKV cluster is a little bit more involved. The easiest way is to use [TiUp](https://github.com/pingcap/tiup) (>= 1.5) to initialise a cluster on your local machine:

Expand All @@ -131,7 +135,7 @@ PD_ADDRS="127.0.0.1:2379" cargo test --package tikv-client --test integration_te

We use a standard GitHub PR workflow. We run CI on every PR and require all PRs to build without warnings (including clippy and Rustfmt warnings), pass tests, have a DCO sign-off (use `-s` when you commit, the DCO bot will guide you through completing the DCO agreement for your first PR), and have at least one review. If any of this is difficult for you, don't worry about it and ask on the PR.

To run CI-like tests locally, we recommend you run `cargo clippy`, `cargo test`, and `cargo fmt` before submitting your PR. See above for running integration tests, but you probably won't need to worry about this for your first few PRs.
To run CI-like tests locally, we recommend you run `cargo clippy`, `cargo test/nextest run`, and `cargo fmt` before submitting your PR. See above for running integration tests, but you probably won't need to worry about this for your first few PRs.

Please follow PingCAP's [Rust style guide](https://pingcap.github.io/style-guide/rust/). All code PRs should include new tests or test cases.

Expand Down
29 changes: 16 additions & 13 deletions examples/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ async fn main() -> Result<()> {
.expect("Could not scan");

let keys: Vec<_> = pairs.into_iter().map(|p| p.key().clone()).collect();
assert_eq!(&keys, &[
Key::from("k1".to_owned()),
Key::from("k2".to_owned()),
]);
assert_eq!(
&keys,
&[Key::from("k1".to_owned()), Key::from("k2".to_owned()),]
);
println!("Scanning from {start:?} to {end:?} gives: {keys:?}");

let k1 = "k1";
Expand All @@ -122,15 +122,18 @@ async fn main() -> Result<()> {
.into_iter()
.map(|p| String::from_utf8(p.1).unwrap())
.collect();
assert_eq!(&vals, &[
"v1".to_owned(),
"v2".to_owned(),
"v2".to_owned(),
"v3".to_owned(),
"v1".to_owned(),
"v2".to_owned(),
"v3".to_owned()
]);
assert_eq!(
&vals,
&[
"v1".to_owned(),
"v2".to_owned(),
"v2".to_owned(),
"v3".to_owned(),
"v1".to_owned(),
"v2".to_owned(),
"v3".to_owned()
]
);
println!("Scanning batch scan from {batch_scan_keys:?} gives: {vals:?}");

// Cleanly exit.
Expand Down
8 changes: 4 additions & 4 deletions examples/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ async fn main() {
let key1_exists = key_exists(&txn, key1.clone()).await;
let key2: Key = b"key_not_exist".to_vec().into();
let key2_exists = key_exists(&txn, key2.clone()).await;
println!("check exists {:?}", vec![
(key1, key1_exists),
(key2, key2_exists)
]);
println!(
"check exists {:?}",
vec![(key1, key1_exists), (key2, key2_exists)]
);

// scan
let key1: Key = b"key1".to_vec().into();
Expand Down
15 changes: 15 additions & 0 deletions proto-build/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "tikv-client-proto-build"
version = "0.0.0"
publish = false

keywords = ["TiKV", "KV", "distributed-systems"]
license = "Apache-2.0"
authors = ["The TiKV Project Authors"]
repository = "https://github.com/tikv/client-rust"
description = "The Rust language implementation of TiKV client."
edition = "2021"

[dependencies]
glob = "0.3"
tonic-build = { version = "0.10", features = ["cleanup-markdown"] }
7 changes: 3 additions & 4 deletions build.rs → proto-build/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// Copyright 2020 TiKV Project Authors. Licensed under Apache-2.0.

use tonic_disable_doctest::BuilderEx;
// Copyright 2023 TiKV Project Authors. Licensed under Apache-2.0.

fn main() {
tonic_build::configure()
.disable_doctests_for_types([".google.api.HttpRule"])
.emit_rerun_if_changed(false)
.build_server(false)
.include_file("mod.rs")
.out_dir("src/generated")
.compile(
&glob::glob("proto/*.proto")
.unwrap()
Expand Down
3 changes: 3 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[toolchain]
channel = "stable"
components = ["rustfmt", "clippy"]
20 changes: 12 additions & 8 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
edition = "2021"
version = "Two"
reorder_imports = true
imports_granularity = "Item"
group_imports = "StdExternalCrate"
where_single_line = true
trailing_comma = "Vertical"
overflow_delimited_expr = true
format_code_in_doc_comments = true
normalize_comments = true

# Those options are disabled for not supported in stable rust.
# We should enable them back once stabilized.
#
# version = "Two"
# imports_granularity = "Item"
# group_imports = "StdExternalCrate"
# where_single_line = true
# trailing_comma = "Vertical"
# overflow_delimited_expr = true
# format_code_in_doc_comments = true
# normalize_comments = true
1 change: 1 addition & 0 deletions src/backoff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use rand::thread_rng;
use rand::Rng;

pub const DEFAULT_REGION_BACKOFF: Backoff = Backoff::no_jitter_backoff(2, 500, 10);
pub const DEFAULT_STORE_BACKOFF: Backoff = Backoff::no_jitter_backoff(2, 1000, 10);
pub const OPTIMISTIC_BACKOFF: Backoff = Backoff::no_jitter_backoff(2, 500, 10);
pub const PESSIMISTIC_BACKOFF: Backoff = Backoff::no_jitter_backoff(2, 500, 10);

Expand Down
Loading

0 comments on commit 32060bb

Please sign in to comment.