From 77649bcec2d5a36f72d3192a9e9c515abe4a3d56 Mon Sep 17 00:00:00 2001 From: happyZYM Date: Sat, 22 Jun 2024 03:25:36 +0000 Subject: [PATCH] ensure data safety --- src/engine.cpp | 37 +++++++++++++++++++++---------------- src/include/engine.h | 14 +++++++++++++- src/main.cpp | 2 ++ src/train_system.cpp | 4 ++++ src/transaction_system.cpp | 5 +++++ src/user_system.cpp | 5 +++++ 6 files changed, 50 insertions(+), 17 deletions(-) diff --git a/src/engine.cpp b/src/engine.cpp index 0baf001..c4c1569 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -24,22 +24,27 @@ const hash_t refund_ticket_hash = 14136625827132030759ull; // SplitMix64Hash(s const hash_t clean_hash = 13563833734274431010ull; // SplitMix64Hash(std::string_view("clean")); const hash_t exit_hash = 5825494509148032335ull; // SplitMix64Hash(std::string_view("exit")); std::string TicketSystemEngine::Execute(const std::string &command) { - // LOG->debug("add_user_hash: {}", add_user_hash); - // LOG->debug("login_hash: {}", login_hash); - // LOG->debug("logout_hash: {}", logout_hash); - // LOG->debug("query_profile_hash: {}", query_profile_hash); - // LOG->debug("modify_profile_hash: {}", modify_profile_hash); - // LOG->debug("add_train_hash: {}", add_train_hash); - // LOG->debug("delete_train_hash: {}", delete_train_hash); - // LOG->debug("release_train_hash: {}", release_train_hash); - // LOG->debug("query_train_hash: {}", query_train_hash); - // LOG->debug("query_ticket_hash: {}", query_ticket_hash); - // LOG->debug("query_transfer_hash: {}", query_transfer_hash); - // LOG->debug("buy_ticket_hash: {}", buy_ticket_hash); - // LOG->debug("query_order_hash: {}", query_order_hash); - // LOG->debug("refund_ticket_hash: {}", refund_ticket_hash); - // LOG->debug("clean_hash: {}", clean_hash); - // LOG->debug("exit_hash: {}", exit_hash); +// LOG->debug("add_user_hash: {}", add_user_hash); +// LOG->debug("login_hash: {}", login_hash); +// LOG->debug("logout_hash: {}", logout_hash); +// LOG->debug("query_profile_hash: {}", query_profile_hash); +// LOG->debug("modify_profile_hash: {}", modify_profile_hash); +// LOG->debug("add_train_hash: {}", add_train_hash); +// LOG->debug("delete_train_hash: {}", delete_train_hash); +// LOG->debug("release_train_hash: {}", release_train_hash); +// LOG->debug("query_train_hash: {}", query_train_hash); +// LOG->debug("query_ticket_hash: {}", query_ticket_hash); +// LOG->debug("query_transfer_hash: {}", query_transfer_hash); +// LOG->debug("buy_ticket_hash: {}", buy_ticket_hash); +// LOG->debug("query_order_hash: {}", query_order_hash); +// LOG->debug("refund_ticket_hash: {}", refund_ticket_hash); +// LOG->debug("clean_hash: {}", clean_hash); +// LOG->debug("exit_hash: {}", exit_hash); +#ifdef ENABLE_ADVANCED_FEATURE + if (server_exit_flag) { + return "server is exiting, this request is rejected."; + } +#endif char command_name[20]; sscanf(command.c_str(), "%*s %s", command_name); hash_t command_name_hash = SplitMix64Hash(std::string_view(command_name)); diff --git a/src/include/engine.h b/src/include/engine.h index 4d12fa4..dec569a 100644 --- a/src/include/engine.h +++ b/src/include/engine.h @@ -3,8 +3,16 @@ #include #include #ifdef ENABLE_ADVANCED_FEATURE +#include +#include #include "dataguard/dataguard.h" #include "dataguard/snapshot.h" +#define AcquireReadAccess std::shared_lock read_lock(command_mutex) +#define AcquireWriteAccess std::unique_lock write_lock(command_mutex) +#include +#else +#define AcquireReadAccess +#define AcquireWriteAccess #endif #include #include "data.h" @@ -15,6 +23,9 @@ class TicketSystemEngine { #ifdef ENABLE_ADVANCED_FEATURE SnapShotManager snapshot_manager; + std::atomic server_exit_flag = false; + std::shared_mutex command_mutex; + friend void handle_client(sockpp::tcp_socket client, TicketSystemEngine &engine); #endif bool its_time_to_exit = false; std::string data_directory; @@ -56,7 +67,8 @@ class TicketSystemEngine { std::string &res_train2_id, int &res_train1_leaving_time_stamp, int &res_train1_arriving_time_stamp, int &res_train2_leaving_time_stamp, int &res_train2_arriving_time_stamp, int &res_train1_price, int &res_train1_seat, - int &res_train2_price, int &res_train2_seat, std::string &res_transfer_station_name, bool sort_by_time); + int &res_train2_price, int &res_train2_seat, std::string &res_transfer_station_name, + bool sort_by_time); public: const bool *its_time_to_exit_ptr = &its_time_to_exit; diff --git a/src/main.cpp b/src/main.cpp index 677d570..db0822b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -54,6 +54,8 @@ void handle_client(sockpp::tcp_socket client, TicketSystemEngine &engine) { // 检查是否需要退出 if (*engine.its_time_to_exit_ptr) { + engine.server_exit_flag = true; + std::unique_lock write_lock(engine.command_mutex); (&engine)->~TicketSystemEngine(); exit(0); return; diff --git a/src/train_system.cpp b/src/train_system.cpp index cb03b1a..6871a98 100644 --- a/src/train_system.cpp +++ b/src/train_system.cpp @@ -10,6 +10,7 @@ #include "utils.h" std::string TicketSystemEngine::AddTrain(const std::string &command) { + AcquireWriteAccess; command_id_t command_id; sscanf(command.c_str(), "[%llu]", &command_id); LOG->debug("command id: {}", command_id); @@ -181,6 +182,7 @@ std::string TicketSystemEngine::AddTrain(const std::string &command) { } std::string TicketSystemEngine::DeleteTrain(const std::string &command) { + AcquireWriteAccess; command_id_t command_id; sscanf(command.c_str(), "[%llu]", &command_id); LOG->debug("command id: {}", command_id); @@ -220,6 +222,7 @@ std::string TicketSystemEngine::DeleteTrain(const std::string &command) { } std::string TicketSystemEngine::ReleaseTrain(const std::string &command) { + AcquireWriteAccess; command_id_t command_id; sscanf(command.c_str(), "[%llu]", &command_id); LOG->debug("command id: {}", command_id); @@ -280,6 +283,7 @@ std::string TicketSystemEngine::ReleaseTrain(const std::string &command) { } std::string TicketSystemEngine::QueryTrain(const std::string &command) { + AcquireReadAccess; command_id_t command_id; sscanf(command.c_str(), "[%llu]", &command_id); LOG->debug("command id: {}", command_id); diff --git a/src/transaction_system.cpp b/src/transaction_system.cpp index 587fa46..cd30f16 100644 --- a/src/transaction_system.cpp +++ b/src/transaction_system.cpp @@ -11,6 +11,7 @@ #include "utils.h" std::string TicketSystemEngine::QueryTicket(const std::string &command) { + AcquireReadAccess; command_id_t command_id; sscanf(command.c_str(), "[%llu]", &command_id); LOG->debug("command id: {}", command_id); @@ -124,6 +125,7 @@ std::string TicketSystemEngine::QueryTicket(const std::string &command) { } std::string TicketSystemEngine::QueryTransfer(const std::string &command) { + AcquireReadAccess; command_id_t command_id; sscanf(command.c_str(), "[%llu]", &command_id); LOG->debug("command id: {}", command_id); @@ -386,6 +388,7 @@ void TicketSystemEngine::CheckTransfer(hash_t train1_ID_hash, hash_t train2_ID_h } std::string TicketSystemEngine::BuyTicket(const std::string &command) { + AcquireWriteAccess; command_id_t command_id; sscanf(command.c_str(), "[%llu]", &command_id); LOG->debug("command id: {}", command_id); @@ -493,6 +496,7 @@ std::string TicketSystemEngine::BuyTicket(const std::string &command) { } std::string TicketSystemEngine::QueryOrder(const std::string &command) { + AcquireReadAccess; command_id_t command_id; sscanf(command.c_str(), "[%llu]", &command_id); LOG->debug("command id: {}", command_id); @@ -546,6 +550,7 @@ std::string TicketSystemEngine::QueryOrder(const std::string &command) { } std::string TicketSystemEngine::RefundTicket(const std::string &command) { + AcquireWriteAccess; command_id_t command_id; sscanf(command.c_str(), "[%llu]", &command_id); LOG->debug("command id: {}", command_id); diff --git a/src/user_system.cpp b/src/user_system.cpp index 5a33b91..1fbf413 100644 --- a/src/user_system.cpp +++ b/src/user_system.cpp @@ -9,6 +9,7 @@ #include "utils.h" std::string TicketSystemEngine::AddUser(const std::string &command) { + AcquireWriteAccess; command_id_t command_id; sscanf(command.c_str(), "[%llu]", &command_id); LOG->debug("command id: {}", command_id); @@ -101,6 +102,7 @@ std::string TicketSystemEngine::AddUser(const std::string &command) { } std::string TicketSystemEngine::LoginUser(const std::string &command) { + AcquireReadAccess; command_id_t command_id; sscanf(command.c_str(), "[%llu]", &command_id); LOG->debug("command id: {}", command_id); @@ -144,6 +146,7 @@ std::string TicketSystemEngine::LoginUser(const std::string &command) { } std::string TicketSystemEngine::LogoutUser(const std::string &command) { + AcquireReadAccess; command_id_t command_id; sscanf(command.c_str(), "[%llu]", &command_id); LOG->debug("command id: {}", command_id); @@ -171,6 +174,7 @@ std::string TicketSystemEngine::LogoutUser(const std::string &command) { } std::string TicketSystemEngine::QueryProfile(const std::string &command) { + AcquireReadAccess; command_id_t command_id; sscanf(command.c_str(), "[%llu]", &command_id); LOG->debug("command id: {}", command_id); @@ -221,6 +225,7 @@ std::string TicketSystemEngine::QueryProfile(const std::string &command) { } std::string TicketSystemEngine::ModifyProfile(const std::string &command) { + AcquireWriteAccess; command_id_t command_id; sscanf(command.c_str(), "[%llu]", &command_id); LOG->debug("command id: {}", command_id);