From 3d124f700012b4f563d3e377f2a93f6bb9780720 Mon Sep 17 00:00:00 2001 From: HeYuchen <377710264@qq.com> Date: Wed, 12 Feb 2020 15:37:43 +0800 Subject: [PATCH 1/6] add partition_version --- include/dsn/dist/replication/replication_app_base.h | 2 ++ src/dist/replication/lib/replica.cpp | 1 + src/dist/replication/lib/replica.h | 3 +++ src/dist/replication/lib/replica_init.cpp | 1 + src/dist/replication/test/replica_test/unit_test/mock_utils.h | 2 ++ 5 files changed, 9 insertions(+) diff --git a/include/dsn/dist/replication/replication_app_base.h b/include/dsn/dist/replication/replication_app_base.h index 244a555315..2ff9c4a13b 100644 --- a/include/dsn/dist/replication/replication_app_base.h +++ b/include/dsn/dist/replication/replication_app_base.h @@ -228,6 +228,8 @@ class replication_app_base : public replica_base // query app envs. virtual void query_app_envs(/*out*/ std::map &envs) = 0; + virtual void set_partition_version(uint32_t partition_version) {} + public: // // utility functions to be used by app diff --git a/src/dist/replication/lib/replica.cpp b/src/dist/replication/lib/replica.cpp index b25bc8fbba..f3d77366cf 100644 --- a/src/dist/replication/lib/replica.cpp +++ b/src/dist/replication/lib/replica.cpp @@ -65,6 +65,7 @@ replica::replica( _options = &stub->options(); init_state(); _config.pid = gpid; + _partition_version = app.partition_count - 1; std::string counter_str = fmt::format("private.log.size(MB)@{}", gpid); _counter_private_log_size.init_app_counter( diff --git a/src/dist/replication/lib/replica.h b/src/dist/replication/lib/replica.h index 59bd33455a..9b36c72bb8 100644 --- a/src/dist/replication/lib/replica.h +++ b/src/dist/replication/lib/replica.h @@ -487,6 +487,9 @@ class replica : public serverlet, public ref_counter, public replica_ba // ballot when starting partition split and split will stop if ballot changed // _child_init_ballot = 0 if partition not in partition split ballot _child_init_ballot{0}; + // in normal case _partition_version = partition_count-1 + // when parent register child, _partition_version = -1, will reject client read write request + std::atomic _partition_version; // perf counters perf_counter_wrapper _counter_private_log_size; diff --git a/src/dist/replication/lib/replica_init.cpp b/src/dist/replication/lib/replica_init.cpp index 739e7e4508..f0563130f3 100644 --- a/src/dist/replication/lib/replica_init.cpp +++ b/src/dist/replication/lib/replica_init.cpp @@ -312,6 +312,7 @@ error_code replica::init_app_and_prepare_list(bool create_new) _app = nullptr; } else { _is_initializing = true; + _app->set_partition_version(_app_info.partition_count - 1); if (nullptr == _private_log) { ddebug("%s: clear private log, dir = %s", name(), log_dir.c_str()); diff --git a/src/dist/replication/test/replica_test/unit_test/mock_utils.h b/src/dist/replication/test/replica_test/unit_test/mock_utils.h index 505bcb0126..a2b18022e9 100644 --- a/src/dist/replication/test/replica_test/unit_test/mock_utils.h +++ b/src/dist/replication/test/replica_test/unit_test/mock_utils.h @@ -68,6 +68,8 @@ class mock_replication_app_base : public replication_app_base void update_app_envs(const std::map &envs) override { _envs = envs; } void query_app_envs(std::map &out) override { out = _envs; } decree last_durable_decree() const override { return 0; } + + void set_partition_version(uint32_t partition_version) override {} private: std::map _envs; From 9662e858e63ffcbe8f0501bacdcd333aa83be997 Mon Sep 17 00:00:00 2001 From: heyuchen Date: Wed, 12 Feb 2020 16:19:13 +0800 Subject: [PATCH 2/6] format code --- src/dist/replication/test/replica_test/unit_test/mock_utils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dist/replication/test/replica_test/unit_test/mock_utils.h b/src/dist/replication/test/replica_test/unit_test/mock_utils.h index a2b18022e9..f7267cf9a3 100644 --- a/src/dist/replication/test/replica_test/unit_test/mock_utils.h +++ b/src/dist/replication/test/replica_test/unit_test/mock_utils.h @@ -68,7 +68,7 @@ class mock_replication_app_base : public replication_app_base void update_app_envs(const std::map &envs) override { _envs = envs; } void query_app_envs(std::map &out) override { out = _envs; } decree last_durable_decree() const override { return 0; } - + void set_partition_version(uint32_t partition_version) override {} private: From 3d09954934135944dd48474b4eb4b86660229187 Mon Sep 17 00:00:00 2001 From: heyuchen Date: Tue, 18 Feb 2020 12:01:51 +0800 Subject: [PATCH 3/6] small fix --- include/dsn/dist/replication/replication_app_base.h | 2 +- src/dist/replication/lib/replica.h | 2 +- src/dist/replication/test/replica_test/unit_test/mock_utils.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/dsn/dist/replication/replication_app_base.h b/include/dsn/dist/replication/replication_app_base.h index dbd22f744a..bd7628ba86 100644 --- a/include/dsn/dist/replication/replication_app_base.h +++ b/include/dsn/dist/replication/replication_app_base.h @@ -228,7 +228,7 @@ class replication_app_base : public replica_base // query app envs. virtual void query_app_envs(/*out*/ std::map &envs) = 0; - virtual void set_partition_version(uint32_t partition_version) {} + virtual void set_partition_version(int32_t partition_version) {} public: // diff --git a/src/dist/replication/lib/replica.h b/src/dist/replication/lib/replica.h index 2b32827e90..5576f73940 100644 --- a/src/dist/replication/lib/replica.h +++ b/src/dist/replication/lib/replica.h @@ -496,7 +496,7 @@ class replica : public serverlet, public ref_counter, public replica_ba ballot _child_init_ballot{0}; // in normal case _partition_version = partition_count-1 // when parent register child, _partition_version = -1, will reject client read write request - std::atomic _partition_version; + std::atomic _partition_version; // perf counters perf_counter_wrapper _counter_private_log_size; diff --git a/src/dist/replication/test/replica_test/unit_test/mock_utils.h b/src/dist/replication/test/replica_test/unit_test/mock_utils.h index db40aef63a..da6fc30cc5 100644 --- a/src/dist/replication/test/replica_test/unit_test/mock_utils.h +++ b/src/dist/replication/test/replica_test/unit_test/mock_utils.h @@ -69,7 +69,7 @@ class mock_replication_app_base : public replication_app_base void query_app_envs(std::map &out) override { out = _envs; } decree last_durable_decree() const override { return 0; } - void set_partition_version(uint32_t partition_version) override {} + void set_partition_version(int32_t partition_version) override {} private: std::map _envs; From b25a6512f8f1f9a21f8916ffd3d4b322f0d52ff8 Mon Sep 17 00:00:00 2001 From: heyuchen Date: Tue, 18 Feb 2020 16:00:37 +0800 Subject: [PATCH 4/6] update to pure virtual function --- include/dsn/dist/replication/replication_app_base.h | 2 +- .../storage_engine/simple_kv/simple_kv.server.impl.h | 2 ++ src/dist/replication/test/simple_kv/simple_kv.server.impl.h | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/include/dsn/dist/replication/replication_app_base.h b/include/dsn/dist/replication/replication_app_base.h index bd7628ba86..2536d0d8e9 100644 --- a/include/dsn/dist/replication/replication_app_base.h +++ b/include/dsn/dist/replication/replication_app_base.h @@ -228,7 +228,7 @@ class replication_app_base : public replica_base // query app envs. virtual void query_app_envs(/*out*/ std::map &envs) = 0; - virtual void set_partition_version(int32_t partition_version) {} + virtual void set_partition_version(int32_t partition_version) = 0; public: // diff --git a/src/dist/replication/storage_engine/simple_kv/simple_kv.server.impl.h b/src/dist/replication/storage_engine/simple_kv/simple_kv.server.impl.h index 077cb5324b..103067b508 100644 --- a/src/dist/replication/storage_engine/simple_kv/simple_kv.server.impl.h +++ b/src/dist/replication/storage_engine/simple_kv/simple_kv.server.impl.h @@ -90,6 +90,8 @@ class simple_kv_service_impl : public simple_kv_service virtual void query_app_envs(/*out*/ std::map &envs) {} + virtual void set_partition_version(int32_t partition_version) {} + private: void recover(); void recover(const std::string &name, int64_t version); diff --git a/src/dist/replication/test/simple_kv/simple_kv.server.impl.h b/src/dist/replication/test/simple_kv/simple_kv.server.impl.h index 096172eb20..e60fb05b69 100644 --- a/src/dist/replication/test/simple_kv/simple_kv.server.impl.h +++ b/src/dist/replication/test/simple_kv/simple_kv.server.impl.h @@ -90,6 +90,8 @@ class simple_kv_service_impl : public application::simple_kv_service virtual void query_app_envs(/*out*/ std::map &envs) {} + virtual void set_partition_version(int32_t partition_version) {} + private: void recover(); void recover(const std::string &name, int64_t version); From 2025b917f5de0dcba6286d19db3276f55643d979 Mon Sep 17 00:00:00 2001 From: heyuchen Date: Wed, 19 Feb 2020 11:12:16 +0800 Subject: [PATCH 5/6] fix by comment --- include/dsn/dist/replication/replication_app_base.h | 5 +++++ src/dist/replication/lib/replica.h | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/include/dsn/dist/replication/replication_app_base.h b/include/dsn/dist/replication/replication_app_base.h index 2536d0d8e9..ecdd4ceb87 100644 --- a/include/dsn/dist/replication/replication_app_base.h +++ b/include/dsn/dist/replication/replication_app_base.h @@ -228,6 +228,11 @@ class replication_app_base : public replica_base // query app envs. virtual void query_app_envs(/*out*/ std::map &envs) = 0; + // `partition_version` is used to guarantee data consistency during partition split. + // In normal cases, partition_version = partition_count-1, when this replica rejects read + // and write request, partition_version = -1. + // + // Thread-safe. virtual void set_partition_version(int32_t partition_version) = 0; public: diff --git a/src/dist/replication/lib/replica.h b/src/dist/replication/lib/replica.h index 5576f73940..1c38df9b86 100644 --- a/src/dist/replication/lib/replica.h +++ b/src/dist/replication/lib/replica.h @@ -494,8 +494,8 @@ class replica : public serverlet, public ref_counter, public replica_ba // ballot when starting partition split and split will stop if ballot changed // _child_init_ballot = 0 if partition not in partition split ballot _child_init_ballot{0}; - // in normal case _partition_version = partition_count-1 - // when parent register child, _partition_version = -1, will reject client read write request + // in normal cases, _partition_version = partition_count-1 + // when replica reject client read write request, partition_version = -1 std::atomic _partition_version; // perf counters From 07f2e659a9330c16421cc6ea6eb34282bf48d7eb Mon Sep 17 00:00:00 2001 From: heyuchen Date: Wed, 19 Feb 2020 17:35:50 +0800 Subject: [PATCH 6/6] update to non-pure virtual function --- include/dsn/dist/replication/replication_app_base.h | 2 +- .../storage_engine/simple_kv/simple_kv.server.impl.h | 2 -- src/dist/replication/test/replica_test/unit_test/mock_utils.h | 1 + src/dist/replication/test/simple_kv/simple_kv.server.impl.h | 2 -- 4 files changed, 2 insertions(+), 5 deletions(-) diff --git a/include/dsn/dist/replication/replication_app_base.h b/include/dsn/dist/replication/replication_app_base.h index ecdd4ceb87..628c31d9e9 100644 --- a/include/dsn/dist/replication/replication_app_base.h +++ b/include/dsn/dist/replication/replication_app_base.h @@ -233,7 +233,7 @@ class replication_app_base : public replica_base // and write request, partition_version = -1. // // Thread-safe. - virtual void set_partition_version(int32_t partition_version) = 0; + virtual void set_partition_version(int32_t partition_version){}; public: // diff --git a/src/dist/replication/storage_engine/simple_kv/simple_kv.server.impl.h b/src/dist/replication/storage_engine/simple_kv/simple_kv.server.impl.h index 103067b508..077cb5324b 100644 --- a/src/dist/replication/storage_engine/simple_kv/simple_kv.server.impl.h +++ b/src/dist/replication/storage_engine/simple_kv/simple_kv.server.impl.h @@ -90,8 +90,6 @@ class simple_kv_service_impl : public simple_kv_service virtual void query_app_envs(/*out*/ std::map &envs) {} - virtual void set_partition_version(int32_t partition_version) {} - private: void recover(); void recover(const std::string &name, int64_t version); diff --git a/src/dist/replication/test/replica_test/unit_test/mock_utils.h b/src/dist/replication/test/replica_test/unit_test/mock_utils.h index da6fc30cc5..2ccd1d2736 100644 --- a/src/dist/replication/test/replica_test/unit_test/mock_utils.h +++ b/src/dist/replication/test/replica_test/unit_test/mock_utils.h @@ -69,6 +69,7 @@ class mock_replication_app_base : public replication_app_base void query_app_envs(std::map &out) override { out = _envs; } decree last_durable_decree() const override { return 0; } + // TODO(heyuchen): implement this function in further pull request void set_partition_version(int32_t partition_version) override {} private: diff --git a/src/dist/replication/test/simple_kv/simple_kv.server.impl.h b/src/dist/replication/test/simple_kv/simple_kv.server.impl.h index e60fb05b69..096172eb20 100644 --- a/src/dist/replication/test/simple_kv/simple_kv.server.impl.h +++ b/src/dist/replication/test/simple_kv/simple_kv.server.impl.h @@ -90,8 +90,6 @@ class simple_kv_service_impl : public application::simple_kv_service virtual void query_app_envs(/*out*/ std::map &envs) {} - virtual void set_partition_version(int32_t partition_version) {} - private: void recover(); void recover(const std::string &name, int64_t version);