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

[CHANGED] refactored nats.c, prep for js_PullSubscribeAsync #778

Merged
merged 4 commits into from
Aug 7, 2024

Conversation

levb
Copy link
Collaborator

@levb levb commented Aug 6, 2024

This is a replacement for #772 that had too many changes/re-bases and had grown too complicated for review.

  • Code organization - broke up nats.c into ./glib/*.c
  • Added nats_OpenWithConfig for thread pool control, and in preparation for JSON-ifying default options, a separate request from @jnmoyne
  • [EXPERIMENTAL, not used/tested] Allow using a thread pool for reply delivery.

Copy link

codecov bot commented Aug 6, 2024

Codecov Report

Attention: Patch coverage is 74.38380% with 291 lines in your changes missing coverage. Please review.

Project coverage is 68.80%. Comparing base (1e675d6) to head (50506e8).

Files Patch % Lines
src/glib/glib.c 69.53% 21 Missing and 57 partials ⚠️
src/glib/glib_last_error.c 64.82% 18 Missing and 33 partials ⚠️
src/nats.c 56.97% 28 Missing and 9 partials ⚠️
src/sub.c 83.48% 12 Missing and 25 partials ⚠️
src/glib/glib_dispatch_pool.c 64.93% 11 Missing and 16 partials ⚠️
src/glib/glib_timer.c 81.50% 3 Missing and 24 partials ⚠️
src/glib/glib_async_cb.c 82.81% 5 Missing and 6 partials ⚠️
src/glib/glib_gc.c 78.57% 2 Missing and 4 partials ⚠️
src/js.c 81.48% 1 Missing and 4 partials ⚠️
src/timer.c 25.00% 1 Missing and 2 partials ⚠️
... and 6 more
Additional details and impacted files
@@              Coverage Diff               @@
##           lev-bench2     #778      +/-   ##
==============================================
- Coverage       68.84%   68.80%   -0.04%     
==============================================
  Files              39       49      +10     
  Lines           15238    15206      -32     
  Branches         3153     3135      -18     
==============================================
- Hits            10491    10463      -28     
- Misses           1680     1701      +21     
+ Partials         3067     3042      -25     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@levb
Copy link
Collaborator Author

levb commented Aug 6, 2024

performance benchmark: https://github.com/nats-io/nats.c/actions/runs/10267478369/job/28408116810?pr=778

Step: compare benchmarks
image

@levb levb marked this pull request as ready for review August 6, 2024 14:24
Copy link
Member

@kozlovic kozlovic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some changes.

Now, I need to look back at some comment I made in an earlier PR regarding the delivery lock situation, that we could modify something and reduce a lot the use of it. It was a moot point since you started refactoring that (and got rid of altogether), but now that we are back at having the two, I wonder if this would still be applicable.

src/dispatch.c Outdated
signal = true;
msg->next = NULL;
q->head = msg;
if (q->tail == NULL)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would remove, since tail must be NULL if head is NULL. Also, q->tail = msg; should be done in all cases so would move out of the if/else.

src/dispatch.c Outdated
else
{
q->tail->next = msg;
q->tail = msg;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move out of the else.


s = natsMutex_Create(&d->mu);
if (s != NATS_OK)
return s;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return NATS_UPDATE_ERR_STACK(s);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(that being said, not sure what happens since this is a static inline function...)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be the same since func will still be defined.

_destroyDispatcher(d);
natsLib_Release();
}
return s;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return NATS_UPDATE_ERR_STACK(s);

pool->cap = cap;
}
}
return s;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return NATS_UPDATE_ERR_STACK(s);


static inline void _cleanupOwnDispatcher(natsSubscription *sub)
{
nats_destroyQueuedMessages(&sub->ownDispatcher.queue);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, cleanup/destroy functions may be called as part of the creation of the object and you have to assume that the failure could happen at any point and therefore some fields may not have been initialized. So here ownDispatcher seem to be a struct, so it won't be NULL, but queue may, yet nats_destroyQueuedMessages does not seem to be checking for NULL there.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, queue is not a pointer; a NULL-ed out struct, so the for loop bails.

src/sub.c Show resolved Hide resolved
@@ -301,7 +310,7 @@ natsSub_deliverMsgs(void *arg)
}

natsSub_Lock(sub);
onCompleteCB = sub->onCompleteCB;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is an example of what I was talking about :-)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure I am following :) I think this one is VERY low overhead since it's done once, so kind of does not matter that we re-lock here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh no, I was just saying that I was aligning the = ... with the line below, that was a comment about "tabulation", that's all.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kozlovic do you mind emailing me a copy of your VSCode settings.json so I can sync the auto-formatting?

src/sub.c Outdated
{
natsStatus s = NATS_OK;
if (sub->ownDispatcher.thread != NULL)
return NATS_ILLEGAL_STATE; // already running
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setDefaultError.

src/sub.c Outdated

sub->dispatcher = &sub->ownDispatcher;
s = natsThread_Create(&sub->ownDispatcher.thread, natsSub_deliverMsgs, (void *) sub);
return s;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update stack

Copy link
Member

@kozlovic kozlovic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@levb
Copy link
Collaborator Author

levb commented Aug 7, 2024

@kozlovic Can I please get an LGTM on #777 (the base) so I can merge cleanly?

Copy link
Member

@kozlovic kozlovic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (I already done so yesterday - see comment above your request)

Base automatically changed from lev-bench2 to main August 7, 2024 19:55
@levb levb merged commit e56a792 into main Aug 7, 2024
24 checks passed
@levb levb deleted the lev-dispatchers-try2 branch August 7, 2024 19:57
github-actions bot pushed a commit that referenced this pull request Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants