nats-io / nats.c

A C client for NATS
Apache License 2.0
390 stars 137 forks source link

Subscription Data Disorder Problem #690

Open li1786422 opened 1 year ago

li1786422 commented 1 year ago

Observed behavior

Please ask js_SubscribeSync(&sub, js, subj, &jsOpts, NULL, NULL); Subscribe to the topic and then pass the while(1){ natsSubscription_NextMsg(&msg, sub, 5000); if (s ! = NATS_OK) sleep(1)//sleep 1 seconds this is pseudocode s = natsMsg_Ack(msg, nullptr); The method natsMsg_Destroy(msg); } where the configuration information for the stream is :{ jsStreamConfig cfg;
jsStreamConfig_Init(&cfg); cfg.Name = stream; // Set the subject cfg.Subjects = (const char*[1]){subj}; cfg.SubjectsLen = 1; // Make it a memory stream. cfg.Storage = js_MemoryStorage; // Add the stream, s = js_AddStream(&si, js, &cfg, NULL, NULL); } Publish message by{ js_Publish(nullptr,js,subject,data,len,nullptr,nullptr) } Continuously print the data, the order of the fetched data is messed up what is the reason why the client pulls the When the client pulls data, how to ensure the data order is correct when it stays for a while and then pulls the data again?

I am following the js_sub.c in the provided example where the differences are { example : s = js_SubscribeSync(&sub, js, subj, &jsOpts, &so, &jerr); my : s = js_SubscribeSync(&sub, js, subj, &jsOpts, NULL, NULL); } } where example's so is initialized as follows{ jsSubOptions so; if (s == NATS_OK) { so.Stream = stream; so.Consumer = durable; if (flowctrl) { so.Config.FlowControl = true; so.Config.Heartbeat = (int64_t)1E9; } } } my :{ jsSubOptions so; if (s == NATS_OK) { so.Stream = stream; so.Consumer = durable; } } When I also use , it reports at runtime:NOT FOUND

Expected behavior

The fetched sequence is the same as the published sequence, and there is no duplication of receiving the received data

Server and client version

nats-server is 2.9.21 natsclient-c 3.61

Host environment

windows10 cpu :i9-12900

Steps to reproduce

No response

li1786422 commented 1 year ago

When I remove sleep, after getting data up to 1000 data it pauses for a while before going to start the subscription from the beginning, where the 1000 data are in order

levb commented 1 year ago

@li1786422 Apologies if I am misunderstanding your issue, but it seems to me that you want your stream configured with a js_InterestPolicy if you want the messages to be removed by explicit ACKs, or with a js_WorkQueuePolicy if you want them to be pulled once. See https://docs.nats.io/nats-concepts/jetstream/streams#retentionpolicy

Please let me know if I misunderstood the issue.

li1786422 commented 1 year ago

@li1786422 Apologies if I am misunderstanding your issue, but it seems to me that you want your stream configured with a js_InterestPolicy if you want the messages to be removed by explicit ACKs, or with a js_WorkQueuePolicy if you want them to be pulled once. See https://docs.nats.io/nats-concepts/jetstream/streams#retentionpolicy

Please let me know if I misunderstood the issue. this my code publish code : publishcode.txt

subscribe code : subscribecode.txt

Scenario 1 Parameters to enter when running the subscription program Enter the topic you want to subscribe to: foo1 Enter the subscription method:3

running result: publishMessage.txt subscribemessage.txt

from subscribe.txt . I found that the data I obtained after hellofoo1_1459 becomes hellofoo1_460 and the data after that is disordered Is this due to not creating a consumer? but When I try to put on a piece of consumption this is reported as Error: 13 - Not Found Error: 13 - Not Found The code I'm trying to create the CONSUMER with is changing to b
js_SubscribeSync(&sub, js, subject.c_str(), &jsOpts, nullptr, nullptr); to js_SubscribeSync(&sub, js, subject.c_str(), &jsOpts, &so, nullptr);

Scenario 2 Parameters to enter when running the subscription program Enter the topic you want to subscribe to: foo1 Enter the subscription method:1 running result: I can't get the data.

I did debugging and found that js_PullSubscribe is successful but natsSubscription_Fetch doesn't fetch the data, I don't understand why that is

my nats-server configuration file : config.txt

levb commented 1 year ago

@li1786422 Thank you for providing the code snippets, they should help to understand/fix the issue. Unfortunately they seem to be encoded and I can't see them when downloaded. Do you mind pasting in the ticket or using https://gist.github.com/?

li1786422 commented 1 year ago

@li1786422 Thank you for providing the code snippets, they should help to understand/fix the issue. Unfortunately they seem to be encoded and I can't see them when downloaded. Do you mind pasting in the ticket or using https://gist.github.com/? you can also see : https://gist.github.com/li1786422/ef00570081dca4d414ae9095bacfba65

this is not encode ,you can try : this my code publish code : publishcode.txt

subscribe code : subscribecode.txt

Scenario 1 Parameters to enter when running the subscription program Enter the topic you want to subscribe to: foo1 Enter the subscription method:3

running result: publishMessage.txt subscribemessage.txt

from subscribe.txt . I found that the data I obtained after hellofoo1_1459 becomes hellofoo1_460 and the data after that is disordered Is this due to not creating a consumer? but When I try to put on a piece of consumption this is reported as Error: 13 - Not Found Error: 13 - Not Found The code I'm trying to create the CONSUMER with is changing to b js_SubscribeSync(&sub, js, subject.c_str(), &jsOpts, nullptr, nullptr); to js_SubscribeSync(&sub, js, subject.c_str(), &jsOpts, &so, nullptr);

Scenario 2 Parameters to enter when running the subscription program Enter the topic you want to subscribe to: foo1 Enter the subscription method:1 running result: I can't get the data.

I did debugging and found that js_PullSubscribe is successful but natsSubscription_Fetch doesn't fetch the data, I don't understand why that is

my nats-server configuration file : config.txt

levb commented 1 year ago

@li1786422 maybe unrelated, but in https://gist.github.com/li1786422/ef00570081dca4d414ae9095bacfba65#file-subscribecode-txt-L96, was it supposed to be 3?

li1786422 commented 1 year ago

@li1786422 maybe unrelated, but in https://gist.github.com/li1786422/ef00570081dca4d414ae9095bacfba65#file-subscribecode-txt-L96, was it supposed to be 3?

Hi I've re-uploaded the file and now the comments section can be downloaded normally without the encoding

li1786422 commented 1 year ago

@li1786422 maybe unrelated, but in https://gist.github.com/li1786422/ef00570081dca4d414ae9095bacfba65#file-subscribecode-txt-L96, was it supposed to be 3?

Can you tell me if the downloaded code is still messed up?

levb commented 1 year ago

I got the samples and the data, sorry was distracted with another big and didn't get a chance to review it in detail.

On Fri, Oct 27, 2023, 5:45 PM li1786422 @.***> wrote:

@li1786422 https://github.com/li1786422 maybe unrelated, but in https://gist.github.com/li1786422/ef00570081dca4d414ae9095bacfba65#file-subscribecode-txt-L96, was it supposed to be 3?

Can you tell me if the downloaded code is still messed up?

— Reply to this email directly, view it on GitHub https://github.com/nats-io/nats.c/issues/690#issuecomment-1783641584, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJB46HEKG3BXPDIFMONNH3YBRITXAVCNFSM6AAAAAA6QIDGO6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTOOBTGY2DCNJYGQ . You are receiving this because you were assigned.Message ID: <nats-io/nats .@.***>

li1786422 commented 12 months ago

I got the samples and the data, sorry was distracted with another big and didn't get a chance to review it in detail. On Fri, Oct 27, 2023, 5:45 PM li1786422 @.> wrote: @li1786422 https://github.com/li1786422 maybe unrelated, but in https://gist.github.com/li1786422/ef00570081dca4d414ae9095bacfba65#file-subscribecode-txt-L96, was it supposed to be 3? Can you tell me if the downloaded code is still messed up? — Reply to this email directly, view it on GitHub <#690 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJB46HEKG3BXPDIFMONNH3YBRITXAVCNFSM6AAAAAA6QIDGO6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTOOBTGY2DCNJYGQ . You are receiving this because you were assigned.Message ID: <nats-io/nats .@.>

That's fine, I just wanted to make sure you could get the data correctly, just look at it when you have the time