Repository navigation
Conversation
…ta via network stream (with BufferedStream this was done indirectly by switching from reading to writing)
…dWriteStream: this class buffers all written data but let you read directly from base stream
|
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? |
|
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. |
|
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. |
|
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: What do you think about this idea? 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].
|
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! |
|
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.
|
Hi Francisco, |
|
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! 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? |
|
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.
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? |
|
Oi Francisco, Thanks for having a look at this. 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. |
|
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! |
|
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. Please, give it a try and let me know if it works ok for you. |
|
Hi Francisco, Thanks for making this available, I will give it a try as soon as possible and let you know. Cheers, |
|
You are welcome, @rmourato ! |
|
Hi Francisco, So, I gave your version a quick spin under Windows .NET and Linux Mono.
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. Thanks for helping! Cheers, |
|
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. |
0b35ef0 to
c43b6af
Compare
e1d8c57 to
1f140e5
Compare
1236d1f to
e8ae2e0
Compare
2fc8944 to
3eb6fa4
Compare
|
Closing as this is likely no longer relevant - Npgsql 3.0 has a totally revamped notification support. Please reopen if otherwise. |
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