From 49eae2a29cf6f02d281217a09c0c376fc2757762 Mon Sep 17 00:00:00 2001 From: levy Date: Fri, 28 May 2021 14:54:03 +0800 Subject: [PATCH 1/4] feat: implement value_schema_manager::get_value_schema --- src/base/test/value_manager_test.cpp | 27 +++++++++++++++++++++++++++ src/base/value_schema_manager.cpp | 21 +++++++++++++++++++-- src/base/value_schema_v2.cpp | 3 ++- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/base/test/value_manager_test.cpp b/src/base/test/value_manager_test.cpp index c8cc5ed8ab..2474ffd12d 100644 --- a/src/base/test/value_manager_test.cpp +++ b/src/base/test/value_manager_test.cpp @@ -56,3 +56,30 @@ TEST(value_schema_manager, get_value_schema) } } } + +TEST(pegasus_value_manager, get_value_schema) +{ + struct test_case + { + uint32_t meta_store_data_version; + uint32_t value_schema_version; + data_version expect_version; + } tests[] = { + {0, 0, pegasus::data_version::VERSION_0}, + {1, 0, pegasus::data_version::VERSION_1}, + {0, 1, pegasus::data_version::VERSION_0}, + {1, 1, pegasus::data_version::VERSION_1}, + {0, 2, pegasus::data_version::VERSION_2}, + {1, 2, pegasus::data_version::VERSION_2}, + }; + + for (const auto &t : tests) { + auto generate_schema = + value_schema_manager::instance().get_value_schema(t.value_schema_version); + std::string raw_value = generate_value(generate_schema, 0, 0, ""); + + auto schema = + value_schema_manager::instance().get_value_schema(t.meta_store_data_version, raw_value); + ASSERT_EQ(t.expect_version, schema->version()); + } +} diff --git a/src/base/value_schema_manager.cpp b/src/base/value_schema_manager.cpp index c6f62c5fde..c0c4df6d9a 100644 --- a/src/base/value_schema_manager.cpp +++ b/src/base/value_schema_manager.cpp @@ -42,8 +42,25 @@ void value_schema_manager::register_schema(std::unique_ptr schema) value_schema *value_schema_manager::get_value_schema(uint32_t meta_cf_data_version, dsn::string_view value) const { - /// TBD(zlw) - return nullptr; + dsn::data_input input(value); + uint8_t first_byte = input.read_u8(); + // first bit = 1 means the data version is >= VERSION_2 + if (first_byte & 0x80) { + // To ensure backward compatibility, return latest version if the data version in value is + // not found. In other words, it will works well in the future if it is compatible with + // current latest version + auto schema = get_value_schema(first_byte & 0x7F); + if (nullptr == schema) { + return get_latest_value_schema(); + } + return schema; + } else { + auto schema = get_value_schema(meta_cf_data_version); + if (nullptr == schema) { + dassert_f(false, "data version({}) in meta cf is not supported", meta_cf_data_version); + } + return schema; + } } value_schema *value_schema_manager::get_value_schema(uint32_t version) const diff --git a/src/base/value_schema_v2.cpp b/src/base/value_schema_v2.cpp index 0f94cb4684..5cae4fc4b4 100644 --- a/src/base/value_schema_v2.cpp +++ b/src/base/value_schema_v2.cpp @@ -46,7 +46,8 @@ std::unique_ptr value_schema_v2::extract_field(dsn::string_view val dsn::blob value_schema_v2::extract_user_data(std::string &&value) { auto ret = dsn::blob::create_from_bytes(std::move(value)); - return ret.range(sizeof(uint8_t) + sizeof(uint32_t) + sizeof(uint64_t)); + static const size_t USER_DATA_OFFSET = sizeof(uint8_t) + sizeof(uint32_t) + sizeof(uint64_t); + return ret.range(USER_DATA_OFFSET); } void value_schema_v2::update_field(std::string &value, std::unique_ptr field) From e30cf91b509952385cbea2deb6a9a4fb631452cd Mon Sep 17 00:00:00 2001 From: levy Date: Fri, 28 May 2021 15:22:01 +0800 Subject: [PATCH 2/4] fix --- src/base/value_schema_manager.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/base/value_schema_manager.cpp b/src/base/value_schema_manager.cpp index c0c4df6d9a..f25161016a 100644 --- a/src/base/value_schema_manager.cpp +++ b/src/base/value_schema_manager.cpp @@ -46,9 +46,9 @@ value_schema *value_schema_manager::get_value_schema(uint32_t meta_cf_data_versi uint8_t first_byte = input.read_u8(); // first bit = 1 means the data version is >= VERSION_2 if (first_byte & 0x80) { - // To ensure backward compatibility, return latest version if the data version in value is - // not found. In other words, it will works well in the future if it is compatible with - // current latest version + // In order to keep backward compatibility, we should return latest version if the data + // version in value is not found. In other words, it will works well in future version if it + // is compatible with current latest version auto schema = get_value_schema(first_byte & 0x7F); if (nullptr == schema) { return get_latest_value_schema(); From 488fe75c806be1d804845ecc7c29e946c9ba0978 Mon Sep 17 00:00:00 2001 From: levy Date: Fri, 28 May 2021 16:20:29 +0800 Subject: [PATCH 3/4] fix --- src/base/value_schema_manager.cpp | 2 +- src/base/value_schema_v2.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/base/value_schema_manager.cpp b/src/base/value_schema_manager.cpp index f25161016a..40437858cd 100644 --- a/src/base/value_schema_manager.cpp +++ b/src/base/value_schema_manager.cpp @@ -48,7 +48,7 @@ value_schema *value_schema_manager::get_value_schema(uint32_t meta_cf_data_versi if (first_byte & 0x80) { // In order to keep backward compatibility, we should return latest version if the data // version in value is not found. In other words, it will works well in future version if it - // is compatible with current latest version + // is compatible with latest version in current auto schema = get_value_schema(first_byte & 0x7F); if (nullptr == schema) { return get_latest_value_schema(); diff --git a/src/base/value_schema_v2.cpp b/src/base/value_schema_v2.cpp index 5cae4fc4b4..7b5ea9b9ec 100644 --- a/src/base/value_schema_v2.cpp +++ b/src/base/value_schema_v2.cpp @@ -46,7 +46,7 @@ std::unique_ptr value_schema_v2::extract_field(dsn::string_view val dsn::blob value_schema_v2::extract_user_data(std::string &&value) { auto ret = dsn::blob::create_from_bytes(std::move(value)); - static const size_t USER_DATA_OFFSET = sizeof(uint8_t) + sizeof(uint32_t) + sizeof(uint64_t); + static const auto USER_DATA_OFFSET = sizeof(uint8_t) + sizeof(uint32_t) + sizeof(uint64_t); return ret.range(USER_DATA_OFFSET); } From 3269b25e7fa22e113398600e7c93176a209d47f4 Mon Sep 17 00:00:00 2001 From: Yingchun Lai <405403881@qq.com> Date: Tue, 1 Jun 2021 12:01:19 +0800 Subject: [PATCH 4/4] Update src/base/value_schema_manager.cpp --- src/base/value_schema_manager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/base/value_schema_manager.cpp b/src/base/value_schema_manager.cpp index 40437858cd..23d53cdf32 100644 --- a/src/base/value_schema_manager.cpp +++ b/src/base/value_schema_manager.cpp @@ -47,7 +47,7 @@ value_schema *value_schema_manager::get_value_schema(uint32_t meta_cf_data_versi // first bit = 1 means the data version is >= VERSION_2 if (first_byte & 0x80) { // In order to keep backward compatibility, we should return latest version if the data - // version in value is not found. In other words, it will works well in future version if it + // version in value is not found. In other words, it will work well in future version if it // is compatible with latest version in current auto schema = get_value_schema(first_byte & 0x7F); if (nullptr == schema) {