Skip to content

Change interaction with server to only send one SYNC message (Pgbouncer support) #493

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

Merged
merged 1 commit into from
Nov 7, 2019

Conversation

fvannee
Copy link
Contributor

@fvannee fvannee commented Nov 4, 2019

This is a simple implementation of PgBouncer support for asyncpg. It doesn't have any detection features, but at least changes asyncpg behavior in such a way that using pgbouncer is possible.
I implemented part of your suggestions as described in #348 (comment) , but in a simpler way. I just got rid of the explicit SYNC after a parse/describe and changed it to a FLUSH. This should work regardless of the setting of statement_cache_size and whether or not it's pgbouncer or direct postgres connection.
With this, if the user wants to use PgBouncer, he'll still have to set statement_cache_size to 0, however once he does that, everything will work as expected.

Please let me know if this solution is acceptable to merge.

@elprans
Copy link
Member

elprans commented Nov 4, 2019

Hi @fvannee. Thanks for taking a stab at this! This looks like a working fix, albeit I don't have the setup to test properly. Do you mind outlining how you tested this? Please include the description above in the commit message. A comment in _process__prepare() about why FLUSH would also be great.

@fvannee
Copy link
Contributor Author

fvannee commented Nov 4, 2019

@elprans Ok, thanks for your feedback. I added a comment to _process__prepare() that explains it in a bit more detail and changed the commit message.
For testing, I had a simple python file that just did a few queries on the database via PgBouncer, that was running in a loop. I then spawned several of these processes in parallel. On master branch this would immediately crash for me, but with patch it runs fine. I tested it with some queries that'd require an initial codec fetch and some that did not require this. After the codec fetch, there is a SYNC, but that's not a problem in this case, because we're forcing a new parse/describe sequence after that with statement_cache_size==0 anyway.

@elprans
Copy link
Member

elprans commented Nov 4, 2019

I added a comment to _process__prepare()

You probably didn't push that yet, as I don't see it in the diff.

For testing, I had a simple python file that just did a few queries on the database via PgBouncer

Cool.

After the codec fetch, there is a SYNC, but that's not a problem in this case, because we're forcing a new parse/describe sequence after that with statement_cache_size==0 anyway.

That's right.

@fvannee
Copy link
Contributor Author

fvannee commented Nov 4, 2019

@elprans Oops, indeed. Now it's there.

# this effectively clears the error and we then wait until we get a
# ready for new query message
self._write(SYNC_MESSAGE)
self._set_state(PROTOCOL_CANCELLED)
Copy link
Member

Choose a reason for hiding this comment

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

It seems that PROTOCOL_ERROR_CONSUME would be more appropriate here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I can change it to that if preferred.

@@ -588,6 +588,8 @@ cdef class BaseProtocol(CoreProtocol):
})
self.abort()

if self.state == PROTOCOL_PREPARE:
self._write(SYNC_MESSAGE)
Copy link
Member

Choose a reason for hiding this comment

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

It's probably safe to send Sync unconditionally here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not - some of the testcases fail when done unconditionally (I first had it like that). We get into a situation where we may end up in IDLE state due to the first sync received, and then receive another ReadyForQuery message - the current state machine goes into error state when that happens

Copy link
Contributor Author

@fvannee fvannee Nov 4, 2019

Choose a reason for hiding this comment

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

that may actually have to do with existing code line 120 - on receiving a Z message it doesn't parse the state. Isn't there supposed to be a:
self._parse_msg_ready_for_query()
on line 120?

edit: ah no, i'm mixing some things up now. We have to send it conditionally, because in IDLE state we cannot receive a Z message (unless we change _read_server_messages as well to allow receiving Z message in IDLE state).
Still, I think line 120 needs this parse_msg_ready_for_query line, no?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, that is suspicious. Server messages should always be read in full. I'd say it's OK to also receive any number of ReadyForQuery in the IDLE state. This should make it safe to send any number of Sync messages.

Copy link
Contributor Author

@fvannee fvannee Nov 5, 2019

Choose a reason for hiding this comment

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

I've experimented a bit with removing the if-statement and putting the ReadyForQuery handling in the state machine in IDLE state. However, it's giving me some warnings I'm not sure are good.
All test cases pass, but I see some extra lines like this after some test cases and I can't figure out exactly why it's not waiting for these.

Future exception was never retrieved
future: <Future finished exception=BrokenPipeError(32, 'Broken pipe')>
Traceback (most recent call last):
  File "/usr/lib64/python3.6/asyncio/selector_events.py", line 417, in _sock_sendall
    n = sock.send(data)
BrokenPipeError: [Errno 32] Broken pipe

I propose to keep this line as it is for now.

Copy link
Member

Choose a reason for hiding this comment

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

OK. Let's keep it, but please add a comment on why it's needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, done

This is a simple implementation of PgBouncer support for asyncpg. It
doesn't have any detection features, but at least changes asyncpg
behavior in such a way that using PgBouncer is possible.
This commit gets rid of the explicit SYNC after a
parse/describe sequence and changes is to a FLUSH. This should work regardless of
the setting of statement_cache_size and whether or not it's pgbouncer or
a direct postgres connection.
With this, PgBouncer is supported when setting statement_cache_size
explicitly to 0.
@fvannee
Copy link
Contributor Author

fvannee commented Nov 7, 2019

Is this good to merge?

Copy link
Member

@elprans elprans left a comment

Choose a reason for hiding this comment

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

Looks good. Thanks!

@elprans elprans merged commit b043fbd into MagicStack:master Nov 7, 2019
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