moleculerjs / moleculer-channels

Reliable messages for Moleculer services via external queue/channel/topic.
MIT License
72 stars 15 forks source link

Redis adapter #74

Closed putty-kang closed 3 months ago

putty-kang commented 9 months ago

Publish offline messages, simulate subscription exceptions, set to retry 100 times, and disconnect the subscription end when sending 10 times, When the subscriber reconnects, the subsequent retry message cannot be received and the message is lost

icebob commented 9 months ago

Please create a reproduction code.

putty-kang commented 8 months ago
// static-service.js
const { ServiceBroker } = require("moleculer");
const ChannelsMiddleware = require("@moleculer/channels").Middleware;
const broker = new ServiceBroker({
  namespace: "test",
  nodeID: "test1",
  transporter: "TCP",
  middlewares:[ChannelsMiddleware({
    adapter:"redis://127.0.0.1:6379"
  })]
});

// Start the Moleculer broker
broker.start();

broker.repl();

setInterval(() => {
  broker.sendToChannel("order.created", {
    id: 1234,
    items: "test"
  });
}, 100);

// Start the service to serve static resources

const serviceSchema = {
  name: "subscriber",
  channels: {
    "order.created": {
      group: "mygroup",
      redis: {
        minIdleTime: 1000,
        claimInterval: 1,
        startID: "0"
      },
      maxRetries: 100,
      handler(payload) {
          console.log(payload);
          throw new Error;
      }
    }
  },
}
broker.createService(serviceSchema).then();

// send 10 times

broker.destroyService("subscriber").then(()=>{
  setTimeout(()=>{
    // then create new Service, Remaining transmission times data loss

broker.createService(serviceSchema);
  }, 10000);
});
putty-kang commented 7 months ago

How to ensure that data in pending is not lost after destroying the service

valeeum commented 7 months ago

@putty-kang The default value for moleculer channels configuration for redis.startID is "$" which means pick up only new messages. You will need to store and manage this value if you want to pick up messages that may have come in while service was down.

From README documentation (https://github.com/moleculerjs/moleculer-channels) Starting point when consumers fetch data from the consumer group. By default equals to $, i.e., consumers will only see new elements arriving in the stream. More info [here](https://redis.io/commands/XGROUP)

putty-kang commented 7 months ago

@valeeum I set startID is 0

putty-kang commented 7 months ago

@icebob

const pubClient = this.clients.get(this.pubName);
// 1. Delete consumer from the consumer group
// 2. Do NOT destroy the consumer group
// https://redis.io/commands/XGROUP
return pubClient.xgroup(
    "DELCONSUMER",
    chan.name, // Stream Name
    chan.group, // Consumer Group
    chan.id // Consumer ID
);

================================================================================

This code results in the loss of unconsumed messages in the reconnecting extension after the service is disconnected

putty-kang commented 7 months ago

@icebob

You need to first check if there is a message in the ending, and then delete the corresponding consumer

putty-kang commented 7 months ago
const pubClient = this.clients.get(this.pubName);
let pending = await pubClient.xpending(
    chan.name,
    chan.group,
    "-",
    "+",
    10 // Max reported entries
);
if(!pending) {
    // 1. Delete consumer from the consumer group
    // 2. Do NOT destroy the consumer group
    // https://redis.io/commands/XGROUP
    return pubClient.xgroup(
        "DELCONSUMER",
        chan.name, // Stream Name
        chan.group, // Consumer Group
        chan.id // Consumer ID
    );
}
AndreMaz commented 3 months ago

Repro example and the proposed fix are here: https://github.com/moleculerjs/moleculer-channels/commit/8c3eb6e5c64cad27d75e5479f38c7a97b4e18cd4