Skip to content

Commit 40d9483

Browse files
authored
Merge pull request #7526 from drmingdrmer/3-ver
ci(proto): add compat test for CopyOptions::purge
2 parents 74aa09a + 0475739 commit 40d9483

File tree

9 files changed

+517
-154
lines changed

9 files changed

+517
-154
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/meta/proto-conv/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ serde_json = "1.0.81"
2828
[dev-dependencies]
2929
anyhow = "1.0.58"
3030
maplit = "1.0.2"
31+
pretty_assertions = "1.2.1"

src/meta/proto-conv/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// For use of const fn: `Option::<T>::unwrap` at compile time.
16+
#![feature(const_option)]
17+
1518
//! Provides conversion from and to protobuf defined meta data, which is used for transport.
1619
//!
1720
//! Thus protobuf messages has the maximized compatibility.

src/meta/proto-conv/src/util.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,33 @@
1414

1515
use crate::Incompatible;
1616

17-
pub const VER: u64 = 5;
17+
/// Describes metadata changes.
18+
///
19+
/// This is a list of every `VER` and the corresponding change it introduces.
20+
///
21+
/// ## For developers
22+
///
23+
/// Every time fields are added/removed into/from data types in this crate:
24+
/// - Add a new line to this list to describe what changed.
25+
/// - Add a test case to ensure protobuf message serialized by this this version can be loaded,
26+
/// similar to: test_user_stage_fs_v6() in tests/it/user_stage.rs;
27+
///
28+
/// `VER` is the current metadata version and is automatically set to the last version.
29+
/// `MIN_COMPATIBLE_VER` is the oldest compatible version.
30+
const META_CHANGE_LOG: &[(u64, &str)] = &[
31+
//
32+
(1, "----------: Initial"),
33+
(2, "2022-07-13: Add: share.proto"),
34+
(3, "2022-07-29: Add: user.proto/UserOption::default_role"),
35+
(4, "2022-08-22: Add: config.proto/GcsStorageConfig"),
36+
(
37+
5,
38+
"2022-08-25: Add: ShareMeta::share_from_db_ids; DatabaseMeta::from_share",
39+
),
40+
(6, "2022-09-08: Add: users.proto/CopyOptions::purge"),
41+
];
42+
43+
pub const VER: u64 = META_CHANGE_LOG.last().unwrap().0;
1844
pub const MIN_COMPATIBLE_VER: u64 = 1;
1945

2046
pub fn check_ver(msg_ver: u64, msg_min_compatible: u64) -> Result<(), Incompatible> {
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2022 Datafuse Labs.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use std::fmt::Debug;
16+
use std::fmt::Display;
17+
18+
use common_proto_conv::FromToProto;
19+
use common_proto_conv::VER;
20+
use pretty_assertions::assert_eq;
21+
22+
/// Tests converting rust types from/to protobuf defined types.
23+
/// It also print out encoded protobuf message as data for backward compatibility test.
24+
pub(crate) fn test_pb_from_to<MT>(name: impl Display, m: MT) -> anyhow::Result<()>
25+
where
26+
MT: FromToProto + PartialEq + Debug,
27+
MT::PB: common_protos::prost::Message,
28+
{
29+
let p = m.to_pb()?;
30+
31+
let mut buf = vec![];
32+
common_protos::prost::Message::encode(&p, &mut buf)?;
33+
// The encoded data should be saved for compatability test.
34+
println!("// Encoded data of version {} of {}:", VER, name);
35+
println!("// It is generated with common::test_pb_from_to.");
36+
println!("let {}_v{} = vec!{:?};", name, VER, buf);
37+
38+
let got = MT::from_pb(p)?;
39+
assert_eq!(m, got, "convert from/to protobuf: {}", name);
40+
Ok(())
41+
}
42+
43+
/// Tests loading old version data.
44+
pub(crate) fn test_load_old<MT>(name: impl Display, buf: &[u8], want: MT) -> anyhow::Result<()>
45+
where
46+
MT: FromToProto + PartialEq + Debug,
47+
MT::PB: common_protos::prost::Message + Default,
48+
{
49+
let p: MT::PB = common_protos::prost::Message::decode(buf).map_err(print_err)?;
50+
let got = MT::from_pb(p).map_err(print_err)?;
51+
52+
assert_eq!(want, got, "loading {} with version {} program", name, VER);
53+
Ok(())
54+
}
55+
56+
pub(crate) fn print_err<T: Debug>(e: T) -> T {
57+
eprintln!("Error: {:?}", e);
58+
e
59+
}
60+
61+
macro_rules! func_name {
62+
() => {{
63+
fn f() {}
64+
fn type_name_of<T>(_: T) -> &'static str {
65+
std::any::type_name::<T>()
66+
}
67+
let name = type_name_of(f);
68+
let n = &name[..name.len() - 3];
69+
let nn = n.replace("::{{closure}}", "");
70+
nn
71+
}};
72+
}

src/meta/proto-conv/tests/it/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,9 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14+
15+
#[macro_use]
16+
pub(crate) mod common;
1417
mod proto_conv;
1518
mod user_proto_conv;
19+
mod user_stage;

src/meta/proto-conv/tests/it/proto_conv.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
use std::collections::BTreeMap;
1616
use std::collections::BTreeSet;
17-
use std::fmt::Debug;
1817
use std::sync::Arc;
1918

2019
use common_datavalues as dv;
@@ -24,9 +23,12 @@ use common_meta_app::schema as mt;
2423
use common_meta_app::share;
2524
use common_proto_conv::FromToProto;
2625
use common_proto_conv::Incompatible;
26+
use common_proto_conv::VER;
2727
use common_protos::pb;
2828
use maplit::btreemap;
2929

30+
use crate::common::print_err;
31+
3032
fn s(ss: impl ToString) -> String {
3133
ss.to_string()
3234
}
@@ -233,13 +235,17 @@ fn test_pb_from_to() -> anyhow::Result<()> {
233235
fn test_incompatible() -> anyhow::Result<()> {
234236
let db_meta = new_db_meta();
235237
let mut p = db_meta.to_pb()?;
236-
p.ver = 6;
237-
p.min_compatible = 6;
238+
p.ver = VER + 1;
239+
p.min_compatible = VER + 1;
238240

239241
let res = mt::DatabaseMeta::from_pb(p);
240242
assert_eq!(
241243
Incompatible {
242-
reason: s("executable ver=5 is smaller than the message min compatible ver: 6")
244+
reason: format!(
245+
"executable ver={} is smaller than the message min compatible ver: {}",
246+
VER,
247+
VER + 1
248+
)
243249
},
244250
res.unwrap_err()
245251
);
@@ -487,8 +493,3 @@ fn test_load_old() -> anyhow::Result<()> {
487493

488494
Ok(())
489495
}
490-
491-
fn print_err<T: Debug>(e: T) -> T {
492-
eprintln!("Error: {:?}", e);
493-
e
494-
}

0 commit comments

Comments
 (0)