ensure data safety
This commit is contained in:
@ -40,6 +40,11 @@ std::string TicketSystemEngine::Execute(const std::string &command) {
|
|||||||
// LOG->debug("refund_ticket_hash: {}", refund_ticket_hash);
|
// LOG->debug("refund_ticket_hash: {}", refund_ticket_hash);
|
||||||
// LOG->debug("clean_hash: {}", clean_hash);
|
// LOG->debug("clean_hash: {}", clean_hash);
|
||||||
// LOG->debug("exit_hash: {}", exit_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];
|
char command_name[20];
|
||||||
sscanf(command.c_str(), "%*s %s", command_name);
|
sscanf(command.c_str(), "%*s %s", command_name);
|
||||||
hash_t command_name_hash = SplitMix64Hash(std::string_view(command_name));
|
hash_t command_name_hash = SplitMix64Hash(std::string_view(command_name));
|
||||||
|
@ -3,8 +3,16 @@
|
|||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
#ifdef ENABLE_ADVANCED_FEATURE
|
#ifdef ENABLE_ADVANCED_FEATURE
|
||||||
|
#include <atomic>
|
||||||
|
#include <shared_mutex>
|
||||||
#include "dataguard/dataguard.h"
|
#include "dataguard/dataguard.h"
|
||||||
#include "dataguard/snapshot.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
|
#endif
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "data.h"
|
#include "data.h"
|
||||||
@ -15,6 +23,9 @@
|
|||||||
class TicketSystemEngine {
|
class TicketSystemEngine {
|
||||||
#ifdef ENABLE_ADVANCED_FEATURE
|
#ifdef ENABLE_ADVANCED_FEATURE
|
||||||
SnapShotManager snapshot_manager;
|
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
|
#endif
|
||||||
bool its_time_to_exit = false;
|
bool its_time_to_exit = false;
|
||||||
std::string data_directory;
|
std::string data_directory;
|
||||||
@ -56,7 +67,8 @@ class TicketSystemEngine {
|
|||||||
std::string &res_train2_id, int &res_train1_leaving_time_stamp,
|
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_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_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:
|
public:
|
||||||
const bool *its_time_to_exit_ptr = &its_time_to_exit;
|
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) {
|
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();
|
(&engine)->~TicketSystemEngine();
|
||||||
exit(0);
|
exit(0);
|
||||||
return;
|
return;
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
std::string TicketSystemEngine::AddTrain(const std::string &command) {
|
std::string TicketSystemEngine::AddTrain(const std::string &command) {
|
||||||
|
AcquireWriteAccess;
|
||||||
command_id_t command_id;
|
command_id_t command_id;
|
||||||
sscanf(command.c_str(), "[%llu]", &command_id);
|
sscanf(command.c_str(), "[%llu]", &command_id);
|
||||||
LOG->debug("command id: {}", 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) {
|
std::string TicketSystemEngine::DeleteTrain(const std::string &command) {
|
||||||
|
AcquireWriteAccess;
|
||||||
command_id_t command_id;
|
command_id_t command_id;
|
||||||
sscanf(command.c_str(), "[%llu]", &command_id);
|
sscanf(command.c_str(), "[%llu]", &command_id);
|
||||||
LOG->debug("command id: {}", 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) {
|
std::string TicketSystemEngine::ReleaseTrain(const std::string &command) {
|
||||||
|
AcquireWriteAccess;
|
||||||
command_id_t command_id;
|
command_id_t command_id;
|
||||||
sscanf(command.c_str(), "[%llu]", &command_id);
|
sscanf(command.c_str(), "[%llu]", &command_id);
|
||||||
LOG->debug("command id: {}", 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) {
|
std::string TicketSystemEngine::QueryTrain(const std::string &command) {
|
||||||
|
AcquireReadAccess;
|
||||||
command_id_t command_id;
|
command_id_t command_id;
|
||||||
sscanf(command.c_str(), "[%llu]", &command_id);
|
sscanf(command.c_str(), "[%llu]", &command_id);
|
||||||
LOG->debug("command id: {}", command_id);
|
LOG->debug("command id: {}", command_id);
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
std::string TicketSystemEngine::QueryTicket(const std::string &command) {
|
std::string TicketSystemEngine::QueryTicket(const std::string &command) {
|
||||||
|
AcquireReadAccess;
|
||||||
command_id_t command_id;
|
command_id_t command_id;
|
||||||
sscanf(command.c_str(), "[%llu]", &command_id);
|
sscanf(command.c_str(), "[%llu]", &command_id);
|
||||||
LOG->debug("command id: {}", 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) {
|
std::string TicketSystemEngine::QueryTransfer(const std::string &command) {
|
||||||
|
AcquireReadAccess;
|
||||||
command_id_t command_id;
|
command_id_t command_id;
|
||||||
sscanf(command.c_str(), "[%llu]", &command_id);
|
sscanf(command.c_str(), "[%llu]", &command_id);
|
||||||
LOG->debug("command id: {}", 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) {
|
std::string TicketSystemEngine::BuyTicket(const std::string &command) {
|
||||||
|
AcquireWriteAccess;
|
||||||
command_id_t command_id;
|
command_id_t command_id;
|
||||||
sscanf(command.c_str(), "[%llu]", &command_id);
|
sscanf(command.c_str(), "[%llu]", &command_id);
|
||||||
LOG->debug("command id: {}", 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) {
|
std::string TicketSystemEngine::QueryOrder(const std::string &command) {
|
||||||
|
AcquireReadAccess;
|
||||||
command_id_t command_id;
|
command_id_t command_id;
|
||||||
sscanf(command.c_str(), "[%llu]", &command_id);
|
sscanf(command.c_str(), "[%llu]", &command_id);
|
||||||
LOG->debug("command id: {}", 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) {
|
std::string TicketSystemEngine::RefundTicket(const std::string &command) {
|
||||||
|
AcquireWriteAccess;
|
||||||
command_id_t command_id;
|
command_id_t command_id;
|
||||||
sscanf(command.c_str(), "[%llu]", &command_id);
|
sscanf(command.c_str(), "[%llu]", &command_id);
|
||||||
LOG->debug("command id: {}", command_id);
|
LOG->debug("command id: {}", command_id);
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
std::string TicketSystemEngine::AddUser(const std::string &command) {
|
std::string TicketSystemEngine::AddUser(const std::string &command) {
|
||||||
|
AcquireWriteAccess;
|
||||||
command_id_t command_id;
|
command_id_t command_id;
|
||||||
sscanf(command.c_str(), "[%llu]", &command_id);
|
sscanf(command.c_str(), "[%llu]", &command_id);
|
||||||
LOG->debug("command id: {}", 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) {
|
std::string TicketSystemEngine::LoginUser(const std::string &command) {
|
||||||
|
AcquireReadAccess;
|
||||||
command_id_t command_id;
|
command_id_t command_id;
|
||||||
sscanf(command.c_str(), "[%llu]", &command_id);
|
sscanf(command.c_str(), "[%llu]", &command_id);
|
||||||
LOG->debug("command id: {}", 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) {
|
std::string TicketSystemEngine::LogoutUser(const std::string &command) {
|
||||||
|
AcquireReadAccess;
|
||||||
command_id_t command_id;
|
command_id_t command_id;
|
||||||
sscanf(command.c_str(), "[%llu]", &command_id);
|
sscanf(command.c_str(), "[%llu]", &command_id);
|
||||||
LOG->debug("command id: {}", 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) {
|
std::string TicketSystemEngine::QueryProfile(const std::string &command) {
|
||||||
|
AcquireReadAccess;
|
||||||
command_id_t command_id;
|
command_id_t command_id;
|
||||||
sscanf(command.c_str(), "[%llu]", &command_id);
|
sscanf(command.c_str(), "[%llu]", &command_id);
|
||||||
LOG->debug("command id: {}", 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) {
|
std::string TicketSystemEngine::ModifyProfile(const std::string &command) {
|
||||||
|
AcquireWriteAccess;
|
||||||
command_id_t command_id;
|
command_id_t command_id;
|
||||||
sscanf(command.c_str(), "[%llu]", &command_id);
|
sscanf(command.c_str(), "[%llu]", &command_id);
|
||||||
LOG->debug("command id: {}", command_id);
|
LOG->debug("command id: {}", command_id);
|
||||||
|
Reference in New Issue
Block a user