Skip to content

Multiple notify fix - #107

Closed
udoliess wants to merge 27 commits into
npgsql:masterfrom
udoliess:multiple_notify_fix
Closed

udoliess wants to merge 27 commits into
npgsql:masterfrom
udoliess:multiple_notify_fix

Conversation

@udoliess

Copy link
Copy Markdown
Contributor

Hello Francisco,

this is a fix for bug [#1011374] http://pgfoundry.org/tracker/index.php?func=detail&aid=1011374&group_id=1000140&atid=590. The problem was the BufferedStream on top of network stream: it reads data into buffer out of network stream and afterwards there is no data available for "Socket.Poll(...)" (NpgsqlConnector.cs line 1172, NpgsqlState.cs line 397, NpgsqlState.cs line 401). A network stream is already buffered in memory so the solution is to bypass buffer for read operations and to use buffer only for write operations on the stream. BufferedStream calls Flush() automaticly on switching between reads and writes. Without this automatic flush we need to call Flush() after each completely written command.

Udo

@franciscojunior

Copy link
Copy Markdown
Member

That's great, Udo!

I'll check it.

I remember I and Glen Parker @glenebob tried to fix this bug and got to the same conclusion that the problem was the buffer which was consuming the data. Although we could fix it for normal stream, we got stuck (in fact I did, Glen had another approach which did a change to the way we handle the responses from the server ) when handling the sslstream. We ended up letting this bug to be fixed when we change our server messages approach to use a dedicated thread and thus we would be able to implement async methods too. But I think your idea is also good. We would need to check the performance of not buffering the stream for reading. Did you make any tests about that?

Also, we already have a class called NpgsqlNetworkStream class which I created to fix a problem when disposing NpgsqlConnections without sending the terminate message to server. Maybe you could use it instead of the BufferedWriteStream? After all, the idea of the NpgsqlNetworkStream would be to add specific implementation details to Npgsql stream handling.

What do you think?

@franciscojunior

Copy link
Copy Markdown
Member

Another detail I forgot to mention is that when using SSL, the stream behaves like bufferedStream and buffers data and so we were back to the same situation as when using bufferedstream. :( We couldn't read the data byte by byte.

@udoliess

udoliess commented Dec 3, 2013

Copy link
Copy Markdown
Contributor Author

Thank you Francisco for your comments. A dedicated thread for handling the responses would be the best solution. Until that may be we can fix this bug with a small patch. I programmed a new approach where reading and writing have separate buffered streams. For detecting if there is data available for reading, one byte is already read in background. This works on top of every stream if BeginRead() of that stream does not block. This is the case for NetworkStream and should be for SslStream and SslClientStream. (But currently I have no testing enviroment for SSL and MONO and so I could not check all cases.) This approach does not use the socket to check for new data.
What do you think?

@udoliess

udoliess commented Jan 9, 2014

Copy link
Copy Markdown
Contributor Author

Hello Francisco @franciscojunior and Glen @glenebob,

my last commit 6e4ada0 introduced an event "Arrived" to class BufferedNetworkStream. This event fires from an extra thread pool thread after one byte was received in background and is available for reading. With this class BufferedNetworkStream I have the idea for another, easy to implement but also (hopefully) fast and safe approach for handling server messages:
To process sync notifications we do not use a dedicated thread. Instead we process these meassages inside the event handler of event Arrived. If the main thread needs the stream for query/response, it disables message processing in event handler by setting a blocking flag. After main thread has finished using the stream, it unsets the blocking flag and ensures, that possibly pending messages are processed.

What do you think about this idea?
Is there work in progress for implementing server message handling in dedicated thread?
Should I try to implement this approach and send it as new pull request?

Thanks,

Udo

… send data via network stream".

This reverts commit cdccdc6.
Additionally this commit fixes missing Flush() calls in authentication phase in NpgsqlState.ProcessBackendResponses_Ver_3().
Another missing Flush() in unused method NpgsqlState.TestNotify() is fixed.
…pure class BufferedNetworkStream (without event) because the event "Arrived" is not needed to fix the multiple notify bug [#1011374].
@udoliess

Copy link
Copy Markdown
Contributor Author

The idea of an event, which handles the incoming messages, should be handled in a separate branch and PR. So I cleaned up this branch to solve only the bug mentioned in the original PR.

--> This branch should be clean for merge now.

Comments welcome!

@franciscojunior

Copy link
Copy Markdown
Member

Hi Udo! Sorry for late response.

We would need to test it with SSL streams. The problem with SSL is that they do their own buffering when reading. When Glen and I were playing with this bug, this SSL 'detail' was the show stopper.

I'll try to set up an SSL configuration and test it and will let you know.

Every BeginRead should be ended with EndRead. But this takes a while.
To speed up disposing we push and forget the EndRead call into thread pool - no one is interested in its result.
@udoliess

udoliess commented Feb 3, 2014

Copy link
Copy Markdown
Contributor Author

Hi Francisco,
thank you very much for replying.
I pushed some more commits - these changes improve the performance while closing BufferedNetworkStream.
(EndRead seems to spend a while after the base stream was closed. So I transfered EndRead into thread pool. One could think about not doing EndRead at all. But the Stream.BeginRead documentation says: "EndRead must be called once for every call to BeginRead.")

@rmourato

Copy link
Copy Markdown

Hi guys,

Just wanted to leave some feedback on this. I haven't spent a huge amount of time on this so take it with a pinch of salt!
I, such as other users, found this issue as I was testing how robust the mechanism was to handle several notifications a second.
I have tested udoliess' fixes and with them I can reliably listen to and receive all notifications sent. It does makes things slightly slower, but that might be because now you do get all messages delivered properly, although from the discussion above I suspect having the extra threading going on might contribute to it given the additional context switching.
Under Mono 3.0.6 (Debian) it's noticeably slower than under Windows .NET.
As an illustrative figure, on Windows .NET I can send and handle about 20 notifications a second and on Linux Mono under half of that. I admit I haven't tried figuring out why this occurs.
Note I haven't looked at SSL streams as that is not a requirement for my use case.

Apart from this, the strategy Npgsql uses doesn't look efficient for handling large volumes of notifications. Rather than registering on a "Notification" event maybe we would be better off logging these on a BlockingCollection or similar that consumers can read from and do whatever processing they wish on them, meaning Npgsql would be released to do other work rather than being blocked by user code.

Thoughts?

@franciscojunior

Copy link
Copy Markdown
Member

Hi, @rmourato !

Thanks for your feedback!

I think the slowness comes from the async usage.

Although this patch fixes the problem, it adds this performance hit as you noticed. We would need to work more on it.

Me and @glenebob have worked in the past with other solutions which although keeps the current performance, it has a problem where it doesn't work with ssl connections because ssl connections do their own buffering. That's why we didn't create a pull request for it yet. But maybe it would be worthwhile to create a pull request so others could give it a try. In order to fix this problem for good we would need to change how Npgsql handle communications with the server.

Apart from this, the strategy Npgsql uses doesn't look efficient for handling large volumes of notifications. Rather than registering on a "Notification" event maybe we would be better off logging these on a BlockingCollection or similar that consumers can read from and do whatever processing they wish on them, meaning Npgsql would be released to do other work rather than being blocked by user code.

I think this is an interesting approach.

Another variation I can think of in order to avoid Npgsql being blocked by user code would be to use separated threads to fire the notifications. We already have a separated thread to handle async notifications while the client is idle, but if the user blocks this thread handling a notification, Npgsql gets stuck. By using a separated thread, we could use a "start and forget" strategy for those notifications decoupling Npgsql from user code handling.

What do you think?

@rmourato

rmourato commented May 1, 2014

Copy link
Copy Markdown

Oi Francisco,

Thanks for having a look at this.
Yes, if you could make that pull request available that would be great as it still fits users who may not need SSL. I can give it a go and let you know how different the performance and reliability looks like.

Regarding the notification approach, using a separate thread to fire them would be fine - as long as you're not creating one per notification that is! Still, the separate container seems more attractive as it would make your library lighter and pass more control onto the user, but you certainly know the library best and obviously you don't want to break functionality for the existing user base.

If I can help with anything do let me know.

@franciscojunior

Copy link
Copy Markdown
Member

Olá, @rmourato !

You are welcome!

I'm glad to know you would like to test it. I'll create the pull request then.

And yes, I also think your notification separate container approach is a very nice strategy. I think we can work on it too.

I'll let you know when I get it on my repository.

Thanks for your feedback!

@franciscojunior

Copy link
Copy Markdown
Member

Hi, @rmourato !

I just created a patch which seems to fix this problem: franciscojunior@9a966e3

Note that, as I said before, it only works on non-SSL connections. If you use SSL, it will break.
This code would need more work to fallback to previous behavior when using SSL streams.

Please, give it a try and let me know if it works ok for you.

@rmourato

rmourato commented May 7, 2014

Copy link
Copy Markdown

Hi Francisco,

Thanks for making this available, I will give it a try as soon as possible and let you know.

Cheers,
Rui

@franciscojunior

Copy link
Copy Markdown
Member

You are welcome, @rmourato !

@rmourato

rmourato commented May 8, 2014

Copy link
Copy Markdown

Hi Francisco,

So, I gave your version a quick spin under Windows .NET and Linux Mono.
The test setup was something along the lines of:

  • A producer process: Writes into a table. A trigger on it generates a NOTIFY for other parties to listen to.
  • A consumer process: Listens to the above notification.
    The producer writes 10000 times into the table, and the consumer receives 10000 notifications from it.

Under Windows .NET it took about 60 seconds (~6ms per write+notify+listen) for the whole process, under Linux Mono (in a VM, mind you!) it took a bit longer, about 80 seconds (~8ms per write+notify+listen), but perhaps most importantly all messages were reliably received.
As your patch yields better performance than udoliess' I'll be using yours for the time being.

Thanks for helping!
If you ever get the async method going let me know and I'll give that a try too.

Cheers,
Rui

@franciscojunior

Copy link
Copy Markdown
Member

Great!

Thanks for having a look at it and for doing the performance testing.

I'll create a pull request about it.

I'll let you know when we get async support.

@roji
roji force-pushed the master branch 2 times, most recently from 0b35ef0 to c43b6af Compare September 16, 2014 08:50
@roji
roji force-pushed the master branch 2 times, most recently from e1d8c57 to 1f140e5 Compare January 14, 2015 02:37
@roji
roji force-pushed the master branch 2 times, most recently from 1236d1f to e8ae2e0 Compare April 12, 2015 07:50
@roji
roji force-pushed the master branch 5 times, most recently from 2fc8944 to 3eb6fa4 Compare April 29, 2015 14:47
@roji

roji commented Aug 9, 2015

Copy link
Copy Markdown
Member

Closing as this is likely no longer relevant - Npgsql 3.0 has a totally revamped notification support. Please reopen if otherwise.

@roji roji closed this Aug 9, 2015
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.

4 participants