torwig commented on code in PR #1657:
URL: https://github.com/apache/kvrocks/pull/1657#discussion_r1290163655


##########
src/commands/cmd_pubsub.cc:
##########
@@ -27,20 +27,48 @@ namespace redis {
 
 class CommandPublish : public Commander {
  public:
-  // mark is_write as false here because slave should be able to execute 
publish command
   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 relies on WAL seq, increase the seq on slave will break 
the replication, hence the compromise
+      // Compromise: can't replicate a message to sub-replicas in a 
cascading-like structure.
+      // Replication relies on WAL seq; increasing the seq on a replica will 
break the replication process,
+      // hence the compromise solution
       redis::PubSub pubsub_db(svr->storage);
+
       auto s = pubsub_db.Publish(args_[1], args_[2]);
       if (!s.ok()) {
         return {Status::RedisExecErr, s.ToString()};
       }
     }
 
     int receivers = svr->PublishMessage(args_[1], args_[2]);
+
     *output = redis::Integer(receivers);
+
+    return Status::OK();
+  }
+};
+
+class CommandMPublish : public Commander {
+ public:
+  Status Execute(Server *svr, Connection *conn, std::string *output) override {
+    int total_receivers = 0;
+
+    for (size_t i = 2; i < args_.size(); i++) {
+      if (!svr->IsSlave()) {
+        redis::PubSub pubsub_db(svr->storage);
+
+        auto s = pubsub_db.Publish(args_[1], args_[i]);
+        if (!s.ok()) {
+          return {Status::RedisExecErr, s.ToString()};
+        }
+      }
+
+      int receivers = svr->PublishMessage(args_[1], args_[i]);
+      total_receivers += receivers;
+    }
+
+    *output = redis::Integer(total_receivers);

Review Comment:
   In general, I think it's not easy to utilize the return value of the 
`PUBLISH` command, except to check whether it is 0 or more. The actual goal of 
Pub/Sub pattern is to decouple publishers from subscribers and if I want to 
check the number of receivers I should know the exact number of them. And for 
example, if I know that there should be 5 subscribers but I received only 4 in 
response to the `PUBLISH` command, what should I do?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to