Skip to content

Commit a04e551

Browse files
authored
Merge pull request #384 from pingyu/support-rust-protobuf
*: support rust-protobuf
2 parents 85c59fb + c3b56d0 commit a04e551

File tree

25 files changed

+213
-210
lines changed

25 files changed

+213
-210
lines changed

.github/workflows/ci.yml

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,69 +10,37 @@ jobs:
1010
check:
1111
name: check
1212
runs-on: ubuntu-latest
13+
strategy:
14+
matrix:
15+
env: [ 'RUST_PROTOBUF=0', 'RUST_PROTOBUF=1' ]
1316
steps:
1417
- uses: actions/checkout@v2
1518
- uses: actions-rs/toolchain@v1
1619
with:
1720
profile: minimal
1821
toolchain: nightly
1922
override: true
23+
- run: rustup component add rustfmt clippy
2024
- name: Install Protoc
2125
uses: arduino/setup-protoc@v1
2226
with:
2327
version: '3.x'
2428
repo-token: ${{ secrets.GITHUB_TOKEN }}
2529
- name: Rust Cache
2630
uses: Swatinem/rust-cache@v1.4.0
27-
- uses: actions-rs/cargo@v1
2831
with:
29-
command: check
30-
args: --all-targets --all-features
31-
32-
fmt:
33-
name: rustfmt
34-
runs-on: ubuntu-latest
35-
steps:
36-
- uses: actions/checkout@v2
37-
- uses: actions-rs/toolchain@v1
38-
with:
39-
profile: minimal
40-
toolchain: nightly
41-
override: true
42-
- run: rustup component add rustfmt
43-
- uses: actions-rs/cargo@v1
44-
with:
45-
command: fmt
46-
args: --all -- --check
47-
48-
clippy:
49-
name: clippy
50-
runs-on: ubuntu-latest
51-
steps:
52-
- uses: actions/checkout@v2
53-
- uses: actions-rs/toolchain@v1
54-
with:
55-
toolchain: nightly
56-
components: clippy
57-
override: true
58-
- name: Install Protoc
59-
uses: arduino/setup-protoc@v1
60-
with:
61-
version: '3.x'
62-
repo-token: ${{ secrets.GITHUB_TOKEN }}
63-
- name: Rust Cache
64-
uses: Swatinem/rust-cache@v1.4.0
65-
- uses: actions-rs/clippy-check@v1
66-
with:
67-
token: ${{ secrets.GITHUB_TOKEN }}
68-
args: --all-targets --all-features -- -D clippy::all
69-
name: Clippy Output
32+
key: ${{ matrix.env }}
33+
- name: make check
34+
run: ${{ matrix.env }} make check
7035

7136
unit-test:
7237
name: unit test
7338
env:
7439
CARGO_INCREMENTAL: 0
7540
runs-on: ubuntu-latest
41+
strategy:
42+
matrix:
43+
env: [ 'RUST_PROTOBUF=0', 'RUST_PROTOBUF=1' ]
7644
steps:
7745
- uses: actions/checkout@v2
7846
- uses: actions-rs/toolchain@v1
@@ -87,14 +55,19 @@ jobs:
8755
repo-token: ${{ secrets.GITHUB_TOKEN }}
8856
- name: Rust Cache
8957
uses: Swatinem/rust-cache@v1.4.0
58+
with:
59+
key: ${{ matrix.env }}
9060
- name: unit test
91-
run: make unit-test
61+
run: ${{ matrix.env }} make unit-test
9262

9363
integration-test:
9464
name: integration test
9565
env:
9666
CARGO_INCREMENTAL: 0
9767
runs-on: ubuntu-latest
68+
strategy:
69+
matrix:
70+
env: [ 'RUST_PROTOBUF=0', 'RUST_PROTOBUF=1' ]
9871
steps:
9972
- uses: actions/checkout@v2
10073
- uses: actions-rs/toolchain@v1
@@ -109,6 +82,8 @@ jobs:
10982
repo-token: ${{ secrets.GITHUB_TOKEN }}
11083
- name: Rust Cache
11184
uses: Swatinem/rust-cache@v1.4.0
85+
with:
86+
key: ${{ matrix.env }}
11287
- name: install tiup
11388
run: curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh
11489
- name: start tiup playground
@@ -121,4 +96,4 @@ jobs:
12196
sleep 1
12297
done
12398
- name: integration test
124-
run: MULTI_REGION=1 make integration-test
99+
run: ${{ matrix.env }} MULTI_REGION=1 make integration-test

Cargo.toml

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,26 @@ description = "The Rust language implementation of TiKV client."
99
edition = "2018"
1010

1111
[features]
12-
default = []
12+
default = [ "prost-codec", "prometheus/process" ]
1313
# Enable integration tests with a running TiKV and PD instance.
1414
# Use $PD_ADDRS, comma separated, to set the addresses the tests use.
1515
integration-tests = []
16+
protobuf-codec = [
17+
"grpcio/protobuf-codec",
18+
"tikv-client-proto/protobuf-codec",
19+
"tikv-client-common/protobuf-codec",
20+
"tikv-client-pd/protobuf-codec",
21+
"tikv-client-store/protobuf-codec",
22+
"mock-tikv/protobuf-codec",
23+
]
24+
prost-codec = [
25+
"grpcio/prost-codec",
26+
"tikv-client-proto/prost-codec",
27+
"tikv-client-common/prost-codec",
28+
"tikv-client-pd/prost-codec",
29+
"tikv-client-store/prost-codec",
30+
"mock-tikv/prost-codec",
31+
]
1632

1733
[lib]
1834
name = "tikv_client"
@@ -24,10 +40,10 @@ either = "1.6"
2440
fail = "0.4"
2541
futures = { version = "0.3", features = ["async-await", "thread-pool"] }
2642
futures-timer = "3.0"
27-
grpcio = { version = "0.10", features = [ "prost-codec", "openssl-vendored" ], default-features = false }
43+
grpcio = { version = "0.10", features = [ "openssl-vendored" ], default-features = false }
2844
lazy_static = "1"
2945
log = "0.4"
30-
prometheus = { version = "0.12", features = [ "push", "process" ], default-features = false }
46+
prometheus = { version = "0.13", features = [ "push" ], default-features = false }
3147
rand = "0.8"
3248
regex = "1"
3349
semver = "1.0"
@@ -70,3 +86,8 @@ members = [
7086
name = "failpoint_tests"
7187
path = "tests/failpoint_tests.rs"
7288
required-features = ["fail/failpoints"]
89+
90+
[patch.crates-io]
91+
raft-proto = { git = "https://github.com/tikv/raft-rs", rev="95c532612ee6a83591fce9a8b51d6afe87b58835"}
92+
protobuf-codegen = { git = "https://github.com/pingcap/rust-protobuf", rev="82b49fea7e696fd647b5aca0a6c6ec944eab3189" }
93+
protobuf = { git = "https://github.com/pingcap/rust-protobuf", rev="82b49fea7e696fd647b5aca0a6c6ec944eab3189" }

Makefile

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,35 @@ export RUSTFLAGS=-Dwarnings
22

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

5+
ENABLE_FEATURES ?=
6+
PD_ADDRS ?= "127.0.0.1:2379"
7+
MULTI_REGION ?= 1
8+
9+
# Use Rust-protobuf instead of Prost to encode and decode protocol buffers.
10+
ifeq ($(RUST_PROTOBUF),1)
11+
ENABLE_FEATURES += protobuf-codec
12+
else
13+
ENABLE_FEATURES += prost-codec
14+
endif
15+
16+
ALL_FEATURES := ${ENABLE_FEATURES} integration-tests
17+
18+
INTEGRATION_TEST_ARGS := --no-default-features --features "${ENABLE_FEATURES} integration-tests"
19+
520
default: check
621

722
check:
8-
cargo check --all --all-targets --all-features
23+
cargo check --all --all-targets --no-default-features --features "${ALL_FEATURES}"
924
cargo fmt -- --check
10-
cargo clippy --all-targets --all-features -- -D clippy::all
25+
cargo clippy --all-targets --no-default-features --features "${ALL_FEATURES}" -- -D clippy::all
1126

1227
unit-test:
13-
cargo test --all
28+
cargo test --all --no-default-features --features "${ENABLE_FEATURES}"
1429

1530
integration-test:
16-
# MULTI_REGION shall be set manually if needed
17-
PD_ADDRS="127.0.0.1:2379" cargo test txn_ --all --features integration-tests -- --nocapture
18-
PD_ADDRS="127.0.0.1:2379" cargo test raw_ --all --features integration-tests -- --nocapture
19-
PD_ADDRS="127.0.0.1:2379" cargo test misc_ --all --features integration-tests -- --nocapture
31+
cargo test txn_ --all ${INTEGRATION_TEST_ARGS} -- --nocapture
32+
cargo test raw_ --all ${INTEGRATION_TEST_ARGS} -- --nocapture
33+
cargo test misc_ --all ${INTEGRATION_TEST_ARGS} -- --nocapture
2034

2135
test: unit-test integration-test
2236

@@ -36,3 +50,7 @@ tiup:
3650
tiup playground nightly --mode tikv-slim --kv 3 --without-monitor --kv.config $(shell pwd)/config/tikv.toml --pd.config $(shell pwd)/config/pd.toml &
3751

3852
all: check doc test
53+
54+
clean:
55+
cargo clean
56+
rm -rf target

mock-tikv/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ name = "mock-tikv"
33
version = "0.0.0"
44
edition = "2018"
55

6+
[features]
7+
protobuf-codec = ["grpcio/protobuf-codec"]
8+
prost-codec = ["grpcio/prost-codec"]
9+
610
[dependencies]
711
derive-new = "0.5"
812
futures = "0.3"
9-
grpcio = { version = "0.10", features = [ "prost-codec" ], default-features = false }
13+
grpcio = { version = "0.10", default-features = false }
1014
log = "0.4"
1115
tikv-client-proto = { path = "../tikv-client-proto"}

mock-tikv/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
// Copyright 2020 TiKV Project Authors. Licensed under Apache-2.0.
2+
3+
// To support both prost & rust-protobuf.
4+
#![cfg_attr(feature = "prost-codec", allow(clippy::useless_conversion))]
5+
26
mod pd;
37
mod server;
48
mod store;

mock-tikv/src/pd.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl MockPd {
2020
tikv_client_proto::metapb::Region {
2121
start_key: vec![],
2222
end_key: vec![],
23-
peers: vec![Self::leader()],
23+
peers: vec![Self::leader()].into(),
2424
..Default::default()
2525
}
2626
}
@@ -58,12 +58,12 @@ impl Pd for MockPd {
5858
) {
5959
let member = Member {
6060
name: "mock tikv".to_owned(),
61-
client_urls: vec![format!("localhost:{MOCK_PD_PORT}")],
61+
client_urls: vec![format!("localhost:{MOCK_PD_PORT}")].into(),
6262
..Default::default()
6363
};
6464
let resp = GetMembersResponse {
65-
members: vec![member.clone()],
66-
leader: Some(member),
65+
members: vec![member.clone()].into(),
66+
leader: Some(member).into(),
6767
..Default::default()
6868
};
6969
spawn_unary_success!(ctx, req, resp, sink);
@@ -120,7 +120,7 @@ impl Pd for MockPd {
120120
sink: ::grpcio::UnarySink<GetStoreResponse>,
121121
) {
122122
let resp = GetStoreResponse {
123-
store: Some(Self::store()),
123+
store: Some(Self::store()).into(),
124124
..Default::default()
125125
};
126126
spawn_unary_success!(ctx, req, resp, sink);
@@ -169,8 +169,8 @@ impl Pd for MockPd {
169169
sink: ::grpcio::UnarySink<GetRegionResponse>,
170170
) {
171171
let resp = GetRegionResponse {
172-
region: Some(Self::region()),
173-
leader: Some(Self::leader()),
172+
region: Some(Self::region()).into(),
173+
leader: Some(Self::leader()).into(),
174174
..Default::default()
175175
};
176176
spawn_unary_success!(ctx, req, resp, sink);
@@ -192,8 +192,8 @@ impl Pd for MockPd {
192192
sink: ::grpcio::UnarySink<GetRegionResponse>,
193193
) {
194194
let resp = GetRegionResponse {
195-
region: Some(Self::region()),
196-
leader: Some(Self::leader()),
195+
region: Some(Self::region()).into(),
196+
leader: Some(Self::leader()).into(),
197197
..Default::default()
198198
};
199199
spawn_unary_success!(ctx, req, resp, sink);

mock-tikv/src/server.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Copyright 2020 TiKV Project Authors. Licensed under Apache-2.0.
22

3+
#![allow(clippy::useless_conversion)] // To support both prost & rust-protobuf.
4+
35
use crate::{spawn_unary_success, KvStore};
46
use derive_new::new;
57
use futures::{FutureExt, TryFutureExt};
@@ -192,7 +194,7 @@ impl Tikv for MockTikv {
192194
sink: grpcio::UnarySink<tikv_client_proto::kvrpcpb::RawBatchGetResponse>,
193195
) {
194196
let mut resp = tikv_client_proto::kvrpcpb::RawBatchGetResponse::default();
195-
resp.set_pairs(self.inner.raw_batch_get(req.take_keys()));
197+
resp.set_pairs(self.inner.raw_batch_get(req.take_keys().into()).into());
196198
spawn_unary_success!(ctx, req, resp, sink);
197199
}
198200

@@ -214,7 +216,7 @@ impl Tikv for MockTikv {
214216
mut req: tikv_client_proto::kvrpcpb::RawBatchPutRequest,
215217
sink: grpcio::UnarySink<tikv_client_proto::kvrpcpb::RawBatchPutResponse>,
216218
) {
217-
let pairs = req.take_pairs();
219+
let pairs = req.take_pairs().into();
218220
self.inner.raw_batch_put(pairs);
219221
let resp = RawBatchPutResponse::default();
220222
spawn_unary_success!(ctx, req, resp, sink);
@@ -238,7 +240,7 @@ impl Tikv for MockTikv {
238240
mut req: tikv_client_proto::kvrpcpb::RawBatchDeleteRequest,
239241
sink: grpcio::UnarySink<tikv_client_proto::kvrpcpb::RawBatchDeleteResponse>,
240242
) {
241-
let keys = req.take_keys();
243+
let keys = req.take_keys().into();
242244
self.inner.raw_batch_delete(keys);
243245
let resp = RawBatchDeleteResponse::default();
244246
spawn_unary_success!(ctx, req, resp, sink);

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@
9090
//! # })}
9191
//! ```
9292
93+
// To support both prost & rust-protobuf.
94+
#![cfg_attr(feature = "prost-codec", allow(clippy::useless_conversion))]
95+
9396
#[macro_use]
9497
pub mod request;
9598
#[macro_use]

0 commit comments

Comments
 (0)