Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: implement value_shcema_v0 #722

Merged
merged 20 commits into from
May 25, 2021
2 changes: 1 addition & 1 deletion rdsn
Submodule rdsn updated 72 files
+14 −21 include/dsn/cpp/pipeline.h
+21 −0 include/dsn/dist/block_service.h
+14 −21 include/dsn/dist/fmt_logging.h
+14 −21 include/dsn/dist/replication/duplication_common.h
+8 −2 include/dsn/dist/replication/replication_ddl_client.h
+18 −3 include/dsn/http/http_server.h
+19 −0 src/block_service/CMakeLists.txt
+16 −3 src/block_service/block_service_manager.cpp
+16 −3 src/block_service/block_service_manager.h
+17 −0 src/block_service/fds/fds_service.cpp
+17 −0 src/block_service/fds/fds_service.h
+6 −2 src/block_service/hdfs/hdfs_service.cpp
+2 −0 src/block_service/hdfs/hdfs_service.h
+19 −0 src/block_service/test/CMakeLists.txt
+16 −3 src/block_service/test/block_service_manager_test.cpp
+16 −3 src/block_service/test/block_service_mock.h
+18 −0 src/block_service/test/clear.sh
+17 −0 src/block_service/test/fds_service_test.cpp
+16 −3 src/block_service/test/main.cpp
+19 −0 src/block_service/test/run.sh
+31 −1 src/client/replication_ddl_client.cpp
+12 −5 src/common/backup.thrift
+16 −25 src/common/duplication_common.cpp
+1 −0 src/common/replication_common.cpp
+1 −0 src/common/replication_common.h
+19 −0 src/http/CMakeLists.txt
+16 −25 src/meta/app_env_validator.cpp
+16 −25 src/meta/app_env_validator.h
+23 −7 src/meta/backup_engine.cpp
+4 −0 src/meta/backup_engine.h
+16 −25 src/meta/duplication/duplication_info.cpp
+16 −25 src/meta/duplication/duplication_info.h
+16 −25 src/meta/duplication/meta_duplication_service.cpp
+16 −25 src/meta/duplication/meta_duplication_service.h
+11 −0 src/meta/meta_backup_service.cpp
+1 −0 src/meta/meta_backup_service.h
+16 −3 src/meta/meta_bulk_load_service.cpp
+16 −3 src/meta/meta_bulk_load_service.h
+16 −3 src/meta/meta_http_service.cpp
+16 −3 src/meta/meta_http_service.h
+16 −25 src/meta/meta_split_service.cpp
+17 −25 src/meta/meta_split_service.h
+16 −25 src/meta/meta_state_service_utils.cpp
+16 −25 src/meta/meta_state_service_utils.h
+16 −25 src/meta/meta_state_service_utils_impl.h
+0 −1 src/meta/server_state.cpp
+1 −0 src/meta/server_state.h
+10 −2 src/meta/server_state_restore.cpp
+4 −0 src/meta/test/config-test.ini
+48 −1 src/meta/test/meta_backup_test.cpp
+123 −0 src/meta/test/server_state_restore_test.cpp
+16 −3 src/replica/bulk_load/replica_bulk_loader.cpp
+16 −3 src/replica/bulk_load/replica_bulk_loader.h
+16 −3 src/replica/bulk_load/test/main.cpp
+16 −3 src/replica/bulk_load/test/replica_bulk_loader_test.cpp
+16 −3 src/replica/duplication/duplication_sync_timer.cpp
+16 −3 src/replica/duplication/duplication_sync_timer.h
+16 −3 src/replica/duplication/load_from_private_log.cpp
+16 −25 src/replica/duplication/mutation_batch.cpp
+16 −25 src/replica/duplication/mutation_batch.h
+16 −3 src/replica/duplication/replica_duplicator.cpp
+16 −3 src/replica/duplication/replica_duplicator.h
+16 −3 src/replica/duplication/replica_duplicator_manager.cpp
+16 −3 src/replica/duplication/replica_duplicator_manager.h
+1 −1 src/replica/replica.h
+21 −1 src/replica/replica_backup.cpp
+12 −2 src/replica/replica_restore.cpp
+20 −2 src/replica/test/clear.sh
+5 −1 src/replica/test/config-test.ini
+14 −0 src/replica/test/mock_utils.h
+106 −10 src/replica/test/replica_test.cpp
+19 −0 thirdparty/CMakeLists.txt
13 changes: 12 additions & 1 deletion src/base/pegasus_value_schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,24 @@ class pegasus_value_generator

enum data_version
{
VERSION_0 = 0,
VERSION_COUNT,
VERSION_MAX = VERSION_0,
/// TBD(zlw)
};

struct value_params
{
value_params(std::string &buf, std::vector<rocksdb::Slice> &slices)
: write_buf(buf), write_slices(slices)
{
}

std::map<value_field_type, std::unique_ptr<value_field>> fields;
levy5307 marked this conversation as resolved.
Show resolved Hide resolved
/// TBD(zlw)
// write_buf and write_slices are transferred from `pegasus_value_generator`, Which are used to
levy5307 marked this conversation as resolved.
Show resolved Hide resolved
// prevent data copy
std::string &write_buf;
std::vector<rocksdb::Slice> &write_slices;
};

class value_schema
Expand Down
55 changes: 55 additions & 0 deletions src/base/test/value_manager_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#include "value_schema_manager.h"
#include <gtest/gtest.h>

using namespace pegasus;

extern std::string generate_value(value_schema *schema,
uint32_t expire_ts,
uint64_t time_tag,
dsn::string_view user_data);

TEST(value_schema_manager, get_latest_value_schema)
{
auto schema = value_schema_manager::instance().get_latest_value_schema();
ASSERT_EQ(data_version::VERSION_MAX, schema->version());
}

TEST(value_schema_manager, get_value_schema)
{
struct test_case
{
uint32_t version;
bool schema_exist;
} tests[] = {
{pegasus::data_version::VERSION_0, true}, {pegasus::data_version::VERSION_MAX + 1, false},
};

for (const auto &t : tests) {
auto schema = value_schema_manager::instance().get_value_schema(t.version);
if (t.schema_exist) {
ASSERT_NE(schema, nullptr);
ASSERT_EQ(t.version, schema->version());
} else {
ASSERT_EQ(schema, nullptr);
}
}
}
113 changes: 113 additions & 0 deletions src/base/test/value_schema_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#include "base/pegasus_value_schema.h"
#include "base/value_schema_manager.h"

#include <gtest/gtest.h>

using namespace pegasus;

uint32_t extract_expire_ts(value_schema *schema, const std::string &raw_value)
{
auto segment = schema->extract_field(raw_value, pegasus::value_field_type::EXPIRE_TIMESTAMP);
auto expire_ts_segment = static_cast<expire_timestamp_field *>(segment.get());
return expire_ts_segment->expire_ts;
levy5307 marked this conversation as resolved.
Show resolved Hide resolved
}

uint64_t extract_time_tag(value_schema *schema, const std::string &raw_value)
{
auto segment = schema->extract_field(raw_value, pegasus::value_field_type::TIME_TAG);
auto time_tag_segment = static_cast<time_tag_field *>(segment.get());
return time_tag_segment->time_tag;
}

std::string generate_value(value_schema *schema,
uint32_t expire_ts,
uint64_t time_tag,
dsn::string_view user_data)
{
std::string write_buf;
std::vector<rocksdb::Slice> write_slices;
value_params params{write_buf, write_slices};
params.fields[value_field_type::EXPIRE_TIMESTAMP] =
dsn::make_unique<expire_timestamp_field>(expire_ts);
params.fields[value_field_type::TIME_TAG] = dsn::make_unique<time_tag_field>(time_tag);
params.fields[value_field_type::USER_DATA] = dsn::make_unique<user_data_field>(user_data);

rocksdb::SliceParts sparts = schema->generate_value(params);
std::string raw_value;
for (int i = 0; i < sparts.num_parts; i++) {
raw_value += sparts.parts[i].ToString();
}
return raw_value;
}

TEST(value_schema, generate_and_extract)
{
struct test_case
{
uint32_t data_version;
uint32_t expire_ts;
uint64_t time_tag;
std::string user_data;
} tests[] = {
{0, 1000, 0, ""},
{0, std::numeric_limits<uint32_t>::max(), 0, "pegasus"},
{0, std::numeric_limits<uint32_t>::max(), 0, ""},
{0, 0, 0, "a"},
};

for (const auto &t : tests) {
auto schema = value_schema_manager::instance().get_value_schema(t.data_version);
std::string raw_value = generate_value(schema, t.expire_ts, t.time_tag, t.user_data);

ASSERT_EQ(t.expire_ts, extract_expire_ts(schema, raw_value));
if (t.data_version >= 1) {
ASSERT_EQ(t.time_tag, extract_time_tag(schema, raw_value));
}

dsn::blob user_data = schema->extract_user_data(std::move(raw_value));
ASSERT_EQ(t.user_data, user_data.to_string());
}
}

TEST(value_schema, update_expire_ts)
{
struct test_case
{
uint32_t data_version;
uint32_t expire_ts;
uint32_t update_expire_ts;
} tests[] = {
{0, 1000, 10086},
};

for (const auto &t : tests) {
std::string write_buf;
std::vector<rocksdb::Slice> write_slices;
auto schema = value_schema_manager::instance().get_value_schema(t.data_version);
std::string raw_value = generate_value(schema, t.expire_ts, 0, "");

std::unique_ptr<value_field> segment =
dsn::make_unique<expire_timestamp_field>(t.update_expire_ts);
schema->update_field(raw_value, std::move(segment));
ASSERT_EQ(t.update_expire_ts, extract_expire_ts(schema, raw_value));
}
}
28 changes: 27 additions & 1 deletion src/base/value_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,38 @@ namespace pegasus {

enum value_field_type
{
/// TBD(zlw)
EXPIRE_TIMESTAMP = 0,
TIME_TAG,
USER_DATA,
};

struct value_field
{
virtual ~value_field() = default;
virtual value_field_type type() = 0;
};

struct expire_timestamp_field : public value_field
{
explicit expire_timestamp_field(uint32_t timestamp) { expire_ts = timestamp; }
levy5307 marked this conversation as resolved.
Show resolved Hide resolved
value_field_type type() { return value_field_type::EXPIRE_TIMESTAMP; };
levy5307 marked this conversation as resolved.
Show resolved Hide resolved

uint32_t expire_ts;
};

struct time_tag_field : public value_field
{
explicit time_tag_field(uint64_t tag) { time_tag = tag; }
levy5307 marked this conversation as resolved.
Show resolved Hide resolved
value_field_type type() { return value_field_type::TIME_TAG; };
levy5307 marked this conversation as resolved.
Show resolved Hide resolved

uint64_t time_tag;
};

struct user_data_field : public value_field
{
explicit user_data_field(dsn::string_view data) { user_data = data; }
levy5307 marked this conversation as resolved.
Show resolved Hide resolved
value_field_type type() { return value_field_type::USER_DATA; };
levy5307 marked this conversation as resolved.
Show resolved Hide resolved

dsn::string_view user_data;
};
} // namespace pegasus
16 changes: 9 additions & 7 deletions src/base/value_schema_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
*/

#include "value_schema_manager.h"
#include "value_schema_v0.h"

namespace pegasus {

void value_schema_manager::register_schema(value_schema *schema)
void value_schema_manager::register_schema(std::unique_ptr<value_schema> schema)
{
/// TBD(zlw)
_schemas[schema->version()] = std::move(schema);
}

value_schema *value_schema_manager::get_value_schema(uint32_t meta_cf_data_version,
Expand All @@ -35,14 +35,15 @@ value_schema *value_schema_manager::get_value_schema(uint32_t meta_cf_data_versi

value_schema *value_schema_manager::get_value_schema(uint32_t version) const
{
/// TBD(zlw)
return nullptr;
if (version >= _schemas.size()) {
return nullptr;
}
return _schemas[version].get();
}

value_schema *value_schema_manager::get_latest_value_schema() const
{
/// TBD(zlw)
return nullptr;
return _schemas.rbegin()->get();
}

/**
Expand All @@ -51,6 +52,7 @@ value_schema *value_schema_manager::get_latest_value_schema() const
*/
void register_value_schemas()
{
value_schema_manager::instance().register_schema(dsn::make_unique<value_schema_v0>());
levy5307 marked this conversation as resolved.
Show resolved Hide resolved
/// TBD(zlw)
}

Expand Down
4 changes: 2 additions & 2 deletions src/base/value_schema_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace pegasus {
class value_schema_manager : public dsn::utils::singleton<value_schema_manager>
{
public:
void register_schema(value_schema *schema);
void register_schema(std::unique_ptr<value_schema> schema);
/// using the raw value in rocksdb and data version stored in meta column family to get data
/// version
value_schema *get_value_schema(uint32_t meta_cf_data_version, dsn::string_view value) const;
Expand All @@ -38,6 +38,6 @@ class value_schema_manager : public dsn::utils::singleton<value_schema_manager>
value_schema_manager() = default;
friend class dsn::utils::singleton<value_schema_manager>;

std::array<value_schema *, 2> _schemas;
std::array<std::unique_ptr<value_schema>, data_version::VERSION_COUNT> _schemas;
};
} // namespace pegasus
Loading