From 14f049fab3b5065e0381f3ae29cc2d17311624f1 Mon Sep 17 00:00:00 2001 From: liumiuyong Date: Fri, 30 Apr 2021 17:24:41 +0800 Subject: [PATCH] Fix slave can't execute publish command --- src/redis_cmd.cc | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/redis_cmd.cc b/src/redis_cmd.cc index 5d7354d717f..1bb1fc7cf57 100644 --- a/src/redis_cmd.cc +++ b/src/redis_cmd.cc @@ -3258,12 +3258,17 @@ class CommandDBSize : public Commander { class CommandPublish : public Commander { public: - CommandPublish() : Commander("publish", 3, true) {} - Status Execute(Server *svr, Connection *conn, std::string *output) override { - Redis::PubSub pubsub_db(svr->storage_); - auto s = pubsub_db.Publish(args_[1], args_[2]); - if (!s.ok()) { - return Status(Status::RedisExecErr, s.ToString()); + // mark is_write as false here because slave should be able to execute publish command + CommandPublish() : Commander("publish", 3, false) {} + Status Execute(Server *svr, Connection *conn, std::string *output) override { + if (!svr->IsSlave()) { + // Compromise: can't replicate message to sub-replicas in a cascading-like structure. + // Replication is rely on wal seq, increase the seq on slave will break the replication, hence the compromise + Redis::PubSub pubsub_db(svr->storage_); + auto s = pubsub_db.Publish(args_[1], args_[2]); + if (!s.ok()) { + return Status(Status::RedisExecErr, s.ToString()); + } } int receivers = svr->PublishMessage(args_[1], args_[2]);