Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple events in 1 transaction #171

Closed
fireya opened this issue Jul 26, 2018 · 5 comments

Comments

@fireya
Copy link

commented Jul 26, 2018


Bug

  • Which version of CAP, OS and .NET Core?
    CAP: 2.2.5, OS: windows 10, >net core: 2.1.2

  • What happen?

Hello! I am experimenting with your great library. and using slightly modified sample:

[Route("~/publishWithTransactionUsingEF")]
        public async Task<IActionResult> PublishMessageWithTransactionUsingEF([FromServices]MyDbContext dbContext,[FromServices]ICapPublisher publisher, int n)
        {
            using (var transaction=dbContext.Database.BeginTransaction())
            {
                for (int i = 0; i < n; i++)
                {
                    await publisher.PublishAsync("xxx.services.account.check", new Person { Name = "Foo"+ i });
                }
                transaction.Commit();
            }
            return Ok();
        }

I use kafka, postgres and entity framework.
When sending n=10, all 10 messages are almost immediatly inserted into kafka and are received, but in database i have this state:
image
4 messages with succeeded state and 6 with scheduled. After a couple of minutes, they are changed to Succeeded state and were resent. So i am getting duplicates.

I suggest, that it is a bug, or am i using library wrong way?

@kexxxfeng

This comment has been minimized.

Copy link
Contributor

commented Jul 26, 2018

Have you try Publish with transaction

await publisher.PublishAsync("xxx.services.account.check", new Person { Name = "Foo"+ i }, 

transaction);
@yang-xiaodong

This comment has been minimized.

Copy link
Member

commented Jul 26, 2018

You should not use this way to send a message for testing because the transaction is not committed, which may result in failure to get the message when changing the state of the message.

The correct way is to use a concurrent test tool, such as wrk or apache benchmark, and then invoke api url.

E.g:
wrk -t 4 -d 20 http://localhost:5000/publish

@fireya

This comment has been minimized.

Copy link
Author

commented Jul 26, 2018

i am simulating some business case: for example, master-details, when i need to produce events for all detail lines when updating master line.

I need to produce strictly one event in 1 transaction?

@yang-xiaodong

This comment has been minimized.

Copy link
Member

commented Jul 26, 2018

There is no such restriction, strictly speaking, it depends on the execution time of your transaction.

We recommend using broadcast mode when you need to send multiple events. In our project, we have almost no need to send multiple events in a transaction operation.

@yang-xiaodong

This comment has been minimized.

Copy link
Member

commented Jul 26, 2018

In the next big release, we should deal with this situation

@yang-xiaodong yang-xiaodong added this to the 2.3.0 milestone Aug 4, 2018

@yang-xiaodong yang-xiaodong added the fixed label Aug 29, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.