Skip to content

Commit

Permalink
feat:check repl state to response immediately if full sync not finish…
Browse files Browse the repository at this point in the history
…ed (#2197)

* check repl state to response immediately if full sync not finished

Signed-off-by: lizhen <lizhen@outlook.jp>
  • Loading branch information
tedli authored Jan 31, 2024
1 parent 574c9f9 commit ea1a54b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
3 changes: 3 additions & 0 deletions include/pika_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,11 @@ struct UnblockTaskArgs {
: key(std::move(key_)), db(db_), dispatchThread(dispatchThread_) {}
};

class PikaClientConn;

class Cmd : public std::enable_shared_from_this<Cmd> {
public:
friend class PikaClientConn;
enum CmdStage { kNone, kBinlogStage, kExecuteStage };
struct HintKeys {
HintKeys() = default;
Expand Down
2 changes: 1 addition & 1 deletion src/cache/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ project (cache)
aux_source_directory(./src DIR_SRCS)
include_directories(include)
add_library(cache STATIC ${DIR_SRCS})
add_dependencies(cache net protobuf glog gflags ${LIBUNWIND_NAME})
add_dependencies(cache net protobuf glog gflags rediscache ${LIBUNWIND_NAME})

target_link_libraries(cache
PUBLIC ${GTEST_LIBRARY}
Expand Down
18 changes: 18 additions & 0 deletions src/pika_client_conn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "include/pika_cmd_table_manager.h"
#include "include/pika_command.h"
#include "include/pika_conf.h"
#include "include/pika_define.h"
#include "include/pika_rm.h"
#include "include/pika_server.h"
#include "net/src/dispatch_thread.h"
Expand Down Expand Up @@ -164,6 +165,23 @@ std::shared_ptr<Cmd> PikaClientConn::DoCmd(const PikaCmdArgsType& argv, const st
c_ptr->res().SetRes(CmdRes::kErrOther, "Server in read-only");
return c_ptr;
}
} else if (c_ptr->is_read() && c_ptr->flag_ == 0) {
const auto& server_guard = std::lock_guard(g_pika_server->GetDBLock());
int role = 0;
auto status = g_pika_rm->CheckDBRole(current_db_, &role);
if (!status.ok()) {
c_ptr->res().SetRes(CmdRes::kErrOther, "Internal ERROR");
return c_ptr;
} else if ((role & PIKA_ROLE_SLAVE) == PIKA_ROLE_SLAVE) {
const auto& slave_db = g_pika_rm->GetSyncSlaveDBByName(DBInfo(current_db_));
if (!slave_db) {
c_ptr->res().SetRes(CmdRes::kErrOther, "Internal ERROR");
return c_ptr;
} else if (slave_db->State() != ReplState::kConnected) {
c_ptr->res().SetRes(CmdRes::kErrOther, "Full sync not completed");
return c_ptr;
}
}
}

// Process Command
Expand Down

0 comments on commit ea1a54b

Please sign in to comment.