ensure data safety
This commit is contained in:
@ -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));
|
||||
|
@ -3,8 +3,16 @@
|
||||
#include <map>
|
||||
#include <string>
|
||||
#ifdef ENABLE_ADVANCED_FEATURE
|
||||
#include <atomic>
|
||||
#include <shared_mutex>
|
||||
#include "dataguard/dataguard.h"
|
||||
#include "dataguard/snapshot.h"
|
||||
#define AcquireReadAccess std::shared_lock<std::shared_mutex> read_lock(command_mutex)
|
||||
#define AcquireWriteAccess std::unique_lock<std::shared_mutex> write_lock(command_mutex)
|
||||
#include <sockpp/tcp_acceptor.h>
|
||||
#else
|
||||
#define AcquireReadAccess
|
||||
#define AcquireWriteAccess
|
||||
#endif
|
||||
#include <vector>
|
||||
#include "data.h"
|
||||
@ -15,6 +23,9 @@
|
||||
class TicketSystemEngine {
|
||||
#ifdef ENABLE_ADVANCED_FEATURE
|
||||
SnapShotManager snapshot_manager;
|
||||
std::atomic<bool> 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;
|
||||
|
@ -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<std::shared_mutex> write_lock(engine.command_mutex);
|
||||
(&engine)->~TicketSystemEngine();
|
||||
exit(0);
|
||||
return;
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
Reference in New Issue
Block a user