Skip to content

Release resources with "Discard All" when return connection into pool - #54

Merged
franciscojunior merged 4 commits into
npgsql:masterfrom
ChenHuajun:discardall
Oct 1, 2013
Merged

franciscojunior merged 4 commits into
npgsql:masterfrom
ChenHuajun:discardall

Conversation

@ChenHuajun

Copy link
Copy Markdown
Contributor

Hi Francisco,

I had made a new patch to release resources clearly with "Discard ALL" when return a connection into the pool,which is based on the following discussion.
http://pgfoundry.org/tracker/index.php?func=detail&aid=1011241&group_id=1000140&atid=592

The major modificaions is as the following
1)Release resources with "Discard ALL" if the backend is 8.3 or later and the protocol is V3.
"Discard ALL" is introduced since 8.3 and it is not able to set session configulations via starup package while use protocol V2. So keep their behaviors as before.

2)Refactor the code to send starup package.
When use protocol V3,set session configulations via starup package,other than execute "set xxxx=yyy" later.

3)Add a test case

Regards,
Chen Huajun

@franciscojunior

Copy link
Copy Markdown
Member

Excellent work, @skykiker .
I'll merge your work after I merge #52 . I'm afraid that changes to #52 will impact your patch and you may need to re adapt it.
Let me know if you have any problems after I merge #52.

@ChenHuajun

Copy link
Copy Markdown
Contributor Author

Your are right.There are some common files modified by #52 and this patch. Could you merge #52 first and let me know,I will re adapt this patch later.

@franciscojunior

Copy link
Copy Markdown
Member

Hi, @skykiker . I just merged #52. You can rebase your code and fix possible conflicts now.

If you have any problem, please let me know.

@ChenHuajun

Copy link
Copy Markdown
Contributor Author

Hi, franciscojunior. I have merged #52. Please check it again.

@franciscojunior

Copy link
Copy Markdown
Member

Hi, @ChenHuajun . I'll check it. Thanks!

@ChenHuajun

Copy link
Copy Markdown
Contributor Author

I have rebased #54 and #53 to the new source. If you have any problem, please let me know.

@franciscojunior

Copy link
Copy Markdown
Member

Sorry for late reply.

Excellent work, @ChenHuajun !

I'm finishing some tests here and will merge your pr soon.

@glenebob

Copy link
Copy Markdown
Contributor

Hi @skykiker / @ChenHuajun

Francisco wanted me to review and comment on your changes to NpgsqlStartupPacket.cs, because it seems to revert some optimizations that were recently added.

First of all, we both like the way you've encapsulated the v2 and v3 functionality in subclasses.

I notice that your PR reverts back to storing strings (database_name, user_name, etc.) rather than byte[]. I switched to byte[] as part of an optimization package. If there is a compelling reason to go back to strings, I'm not seeing it.

Your approach to writing the v3 packet to the backend stream is inefficient, because the use of an intermediate buffer results in extra memory consumption and two new copy operations (once to byte[], then again to the backend stream). Recent changes aimed at making those operations as efficient as possible. What you could do instead of the intermediate buffer, is to scan parameters once to determine total packet length, and then again to write its values directly to the backend stream. If you change parameters from (Dictionary<string, string>) to (Dictionary<byte[], byte[]>), moving the UTF8 encoding operations to BuildStartupPacket(), scanning to determine lengths will be very efficient, and memory footprint will be reduced further.

I also notice that your scan of parameters could be a little more efficient by doing (foreach (KeyValuePair<> param in parameters)).

One last comment... It looks to me like protocol_version, database_name, and user_name could be moved to NpgsqlStartupPacket{}, eliminating duplication.

What do you think?

@ChenHuajun

Copy link
Copy Markdown
Contributor Author

Hi @glenebob

Thanks for your comment.
The reason i reverted back to storing string is that is i think string is more readable,and since WriteToStream() would only be called once,the efficiency should be same as storing byte array.
But,just as your said, the intermediate buffer is a problem.Thanks for your advice.It's a good idle.I will go back to storing byte array and remove the intermediate buffer later.

@franciscojunior

Copy link
Copy Markdown
Member

Excellent work, @ChenHuajun / @skykiker .

I tested your PR here and everything is ok. I'm just waiting @glenebob say if he thinks there is any other change needed. If not, I'll merge it.

@glenebob

glenebob commented Oct 1, 2013

Copy link
Copy Markdown
Contributor

@glenebob says merge away :)

@franciscojunior

Copy link
Copy Markdown
Member

Merge it is, sir!

franciscojunior added a commit that referenced this pull request Oct 1, 2013
Release resources with "Discard All" when return connection into pool 

I had made a new patch to release resources clearly with "Discard ALL" when return a connection into the pool,which is based on the following discussion. 
http://pgfoundry.org/tracker/index.php?func=detail&aid=1011241&group_id=1000140&atid=592

The major modificaions is as the following
1)Release resources with "Discard ALL" if the backend is 8.3 or later and the protocol is V3.
"Discard ALL" is introduced since 8.3 and it is not able to set session configulations via starup package while use protocol V2. So keep their behaviors as before.

2)Refactor the code to send starup package.
When use protocol V3,set session configulations via starup package,other than execute "set xxxx=yyy" later.

3)Add a test case

Regards, 
Chen Huajun
@franciscojunior
franciscojunior merged commit eb2835b into npgsql:master Oct 1, 2013
@franciscojunior

Copy link
Copy Markdown
Member

Hi, @ChenHuajun / @skykiker .

After merging your work, I noticed a little problem regarding extra_floating_digits parameter.

This error only appears in postgresql below 9.0 where the max extra_floating_digits support was changed from 2 to 3.

This is the error I'm getting on postgreql 8.4:

NpgsqlTests.ConnectionTests (TestFixtureSetUp):
SetUp : Npgsql.NpgsqlException : FATAL: 22023: 3 is outside the valid range for parameter "extra_float_digits" (-15 .. 2)

Before this patch, this situation was handled by asking the server to set the extra_float_digits to 2 and then to 3. If the 3 wasn't supported, an exception was generated by the value of 2 was already set.

Today, with your patch, I noticed that asking the server to use a value of 2 was removed. Only the value of 3 is asked.

It was my fault that I didn't test on postgresql 8.4.

Please, give it a try on postgresql 8.4.

Thanks in advance.

franciscojunior added a commit that referenced this pull request Oct 1, 2013
This reverts commit eb2835b, reversing
changes made to 5e64504.

There was a problem with this pull request and postgresql below 9.0 regarding extra_float_digits
franciscojunior added a commit that referenced this pull request Oct 1, 2013
Revert "Merge pull request #54 from ChenHuajun/discardall"

This reverts commit eb2835b, reversing
changes made to 5e64504.

There was a problem with pull request #54 and postgresql below 9.0 regarding extra_float_digits
@ChenHuajun

Copy link
Copy Markdown
Contributor Author

Hi Francisco,
I am sorry for this problem.I did not understand way setting extra_floating_digits twice in the code.
It seem to be two solution,
a)set extra_floating_digits to 2 when using protocol V3 and behave as before when using protocol V2
b)set extra_floating_digits via "SET" command other than startup package regardless which protocol was used.
I am not sure the real influence if choosing method a(set extra_floating_digits to 2 other than 3), but i noticed pgjdbc just do as method a.
As to method b,if choosing it , we should have to SET extra_floating_digits again after execute "discard all",which seem to be a tiny performence issue.
Could your(or others) give any advice?

@franciscojunior

Copy link
Copy Markdown
Member

Hi ChenHuajun!

No problem. It was also my fault because I didn't test it with Postgresql 8.4.

But when using method a, the maximum extra_floating_digits will be 2?
There is a bug report about that and even a test case:

http://pgfoundry.org/tracker/index.php?func=detail&aid=1010992&group_id=1000140&atid=590

and the test case:

https://github.com/franciscojunior/Npgsql2/blob/master/tests/CommandTests.cs#L1185

I remember we received some complaints about the number of floating digits in Npgsql. That's why we need to keep the number of floating points to 3 where available.

I'm more inclined towards method b) unless we can check for sure if 2 digits would be enough as the guys from pgjdbc are doing it. Unless they "forgot" to handle the case of 3 digits which I think is unlikely.

@franciscojunior

Copy link
Copy Markdown
Member

Hi, @ChenHuajun !

I fixed a problem with extra_float_digits when running Npgsql under postgresql below 9.0. This fix is in the pull request #69 .

After working on this fix, I noticed that using extra_float_digits value of 2 makes the test pass too, so I'm thinking that your method a) of using a value of 2 for all connections is probably the best idea.

I would just need to check if I can find the user report where the value of 3 was needed. If I can't find this report, I think you can create a new pull request with your changes like in the method a)

What do you think?

@ChenHuajun

Copy link
Copy Markdown
Contributor Author

Hi, @francisco!
I checked and tested pgjdbc again,i found i had gave a wrong infomation.Indead pgjdbc use the method b) for postgresql 9.0+.

And since it's not sure whether using extra_float_digits value of 2 is enough,just behave as before seems to be a better choose.So, i'm inclined towards that set extra_float_digits to 2 via startup package first,then set extra_float_digits to 3 via SET command while the backend is PostgreSQL 9.0+,do your agree?

Moreover i found there are some conflicts between #69 and this patch,so i want to make a new patch after #69 has been merged.

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