Skip to content

Forward sql.active_record notifications back into the calling thread#41590

Merged
eileencodes merged 1 commit intorails:mainfrom
Shopify:log-subscriber-publish
Mar 10, 2021
Merged

Forward sql.active_record notifications back into the calling thread#41590
eileencodes merged 1 commit intorails:mainfrom
Shopify:log-subscriber-publish

Conversation

@casperisfine
Copy link
Copy Markdown
Contributor

It is not uncommon for sql.active_record subscribers to rely on
thread local or fiber local state. For instance the buffered-logger
gem buffer the logs in a thread variable.

With the introduction of async queries, the sql.active_record
events can now be produced from a background thread and that break
some expectations.

This makes it hard for subscriber to map the event to the request
or job that scheduled it.

That is why I believe we should instead store the event and
publish it back on the calling thread when the results are
accessed.

cc @rafaelfranca @etiennebarrie @eileencodes @jhawthorn @tenderlove

NB: this will conflict a bit with #41586, once one is merged the other will have to rebase.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Since the notification is no longer triggered in the background thread, we can no longer use a monitor to do synchronization. The best I could think of is a sleep loop that check the FutureResult state.

@casperisfine casperisfine force-pushed the log-subscriber-publish branch from 33d1a24 to 619d8a6 Compare March 2, 2021 13:33
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This might be a bit excessive, but I'm worried about a killed thread leaving a connection in a broken state.

@casperisfine casperisfine force-pushed the log-subscriber-publish branch 2 times, most recently from 3173ea1 to ecccdca Compare March 2, 2021 14:37
Copy link
Copy Markdown
Member

@jhawthorn jhawthorn left a comment

Choose a reason for hiding this comment

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

Small suggestions. I really like the approach 👍

@casperisfine casperisfine force-pushed the log-subscriber-publish branch 2 times, most recently from 93505ee to fbe5c56 Compare March 3, 2021 09:20
It is not uncommon for `sql.active_record` subscribers to rely on
thread local or fiber local state. For instance the `buffered-logger`
gem buffer the logs in a thread variable.

With the introduction of async queries, the `sql.active_record`
events can now be produced from a background thread and that break
some expectations.

This makes it hard for subscriber to map the event to the request
or job that scheduled it.

That is why I believe we should instead store the event and
publish it back on the calling thread when the results are
accessed.
@casperisfine casperisfine force-pushed the log-subscriber-publish branch from fbe5c56 to 091dc78 Compare March 3, 2021 09:33
elsif canceled?
@event_buffer&.flush

if canceled?
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I changed the order here to avoid a flaky test. The cancelation test would sometimes fail because the query would be executed before we access the result.

But either way I think it makes sense to disallow accessing a canceled result, even if we have it.

@casperisfine
Copy link
Copy Markdown
Contributor Author

CI is failing for an unrelated reason.

begin
if pending?
execute_query(connection, async: true)
@event_buffer = @instrumenter.buffer
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

👍 Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants