Skip to content

Conversation

@m12i
Copy link
Collaborator

@m12i m12i commented Mar 13, 2019

This change implements driver.ConnBeginTx interface so that sqlhooks can be used when a non default isolation level is used.


This change is Reviewable

@coveralls
Copy link

Coverage Status

Coverage decreased (-1.7%) to 72.889% when pulling 297e933 on coordcity:master into 1932c8d on gchaincl:master.

3 similar comments
@coveralls
Copy link

Coverage Status

Coverage decreased (-1.7%) to 72.889% when pulling 297e933 on coordcity:master into 1932c8d on gchaincl:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-1.7%) to 72.889% when pulling 297e933 on coordcity:master into 1932c8d on gchaincl:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-1.7%) to 72.889% when pulling 297e933 on coordcity:master into 1932c8d on gchaincl:master.

@coveralls
Copy link

coveralls commented Mar 13, 2019

Coverage Status

Coverage increased (+0.1%) to 74.667% when pulling 21d7370 on coordcity:master into 1932c8d on gchaincl:master.

@qustavo
Copy link
Owner

qustavo commented Mar 14, 2019

Is there any way to test this?

Copy link
Collaborator Author

@m12i m12i left a comment

Choose a reason for hiding this comment

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

Sure. Added a test

Reviewable status: 0 of 2 files reviewed, all discussions resolved

@m12i
Copy link
Collaborator Author

m12i commented Mar 25, 2019

@gchaincl Just bringing this PR to your attention again

@qustavo
Copy link
Owner

qustavo commented Mar 25, 2019 via email

@qustavo qustavo requested review from keegancsmith and qustavo March 27, 2019 16:13
Copy link
Owner

@qustavo qustavo left a comment

Choose a reason for hiding this comment

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

Did you notice any impact on the benchmarks? Other than that LGTM!

Copy link
Collaborator Author

@m12i m12i left a comment

Choose a reason for hiding this comment

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

The performance seem unchanged (as expected):

go test -bench=. -benchmem on master:

BenchmarkSQLite3/Without_Hooks-4         	  200000	      7815 ns/op	    1072 B/op	      18 allocs/op
BenchmarkSQLite3/With_Hooks-4            	  200000	      8098 ns/op	    1072 B/op	      18 allocs/op

go test -bench=. -benchmem on this PR:

BenchmarkSQLite3/Without_Hooks-4         	  200000	      7972 ns/op	    1072 B/op	      18 allocs/op
BenchmarkSQLite3/With_Hooks-4            	  200000	      8094 ns/op	    1072 B/op	      18 allocs/op

Reviewable status: 0 of 2 files reviewed, all discussions resolved (waiting on @keegancsmith)

Copy link
Collaborator

@keegancsmith keegancsmith left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r1, 1 of 1 files at r2.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @m12i)


sqlhooks.go, line 167 at r2 (raw file):

		return ciCtx.BeginTx(ctx, opts)
	}
	return nil, errors.New("sqlhooks: driver does not support non-default isolation level")

Are there any drivers that don't implement BeginTx? Because I assume this would regress sqlhooks wrapping those drivers.

Copy link
Collaborator Author

@m12i m12i left a comment

Choose a reason for hiding this comment

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

Reviewable status: 1 of 2 files reviewed, 1 unresolved discussion (waiting on @keegancsmith)


sqlhooks.go, line 167 at r2 (raw file):

Previously, keegancsmith (Keegan Carruthers-Smith) wrote…

Are there any drivers that don't implement BeginTx? Because I assume this would regress sqlhooks wrapping those drivers.

Good point, I checked all major sql drivers and they do implement diver.ConnBeginTx.

Even if a driver does not implement diver.ConnBeginTx, this PR wouldn't change any outcome as calling their BeginTx method would fail anyway, with or without being wrapped by sqlhooks.
I changed the error message to be more accurate.

Copy link
Collaborator Author

@m12i m12i left a comment

Choose a reason for hiding this comment

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

Reviewable status: 1 of 2 files reviewed, 1 unresolved discussion (waiting on @keegancsmith)


sqlhooks.go, line 167 at r2 (raw file):

Previously, m12i (Mahmood Rahmani) wrote…

Good point, I checked all major sql drivers and they do implement diver.ConnBeginTx.

Even if a driver does not implement diver.ConnBeginTx, this PR wouldn't change any outcome as calling their BeginTx method would fail anyway, with or without being wrapped by sqlhooks.
I changed the error message to be more accurate.

Done.

@keegancsmith
Copy link
Collaborator

aah I see now from the godoc of BeginTx:

If a non-default isolation level is used that the driver doesn't support, an error will be returned.

Ok so this retains the behaviour. I would actually duplicate what sql/db returns in the case the interface is not implemented: https://sourcegraph.com/github.com/golang/go@go1.12/-/blob/src/database/sql/ctxutil.go#L110-120

Copy link
Collaborator Author

@m12i m12i left a comment

Choose a reason for hiding this comment

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

That's actually what I thought in the beginning hence returning the previous error message. But if you look at the code in func ctxDriverBegin(...) the sqlhooks' BeginTx() is called in the first if statement (line 106). while errors of line 113 or 119 are for drivers who don't support BeginTx() but still are trying to have non-default opts.

So there are two options for sqlhooks:
1- Say sqlhooks supports only drivers that support transaction (i.e. implement BeginTx())
2- duplicate the whole logic of func ctxDriverBegin() in sqlhooks' BeginTx()

option 2 does not look like a good idea, as every time func ctxDriverBegin() changes it may make sqlhooks duplicated code inconsistent.
option 1 seems okey (not ideal). The reason it's okey is as follows:
There are two types of drivers: the ones who implement BeginTx() and the ones who don't. ctxutil.go behaves differently depending on whether or not the driver supports BeginTx(). For sqlhooks as a wrapper I can't think of a way to support both types of drivers. because sqlhooks cannot make itself a supporter and non-supporter of BeginTx() at the same time.

I hope what I said is clear. Please let me know what you think.

Reviewable status: 1 of 2 files reviewed, 1 unresolved discussion (waiting on @keegancsmith)

Copy link
Collaborator Author

@m12i m12i left a comment

Choose a reason for hiding this comment

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

BTW, in my first response to you, I was wrong about drivers' who don't implement BeginTx(). They only fail if they start a transaction with a non-default opt.

Here is one more reason for sqlhooks to implement BeginTx(). Majority of drivers implement BeginTx(). if you wrap them with sqlhooks they no longer support BeginTx() which to me sounds like downgrading those drivers.
A minority (perhaps less popular) drivers don't implement BeginTx(). Since there is no way to support both, I think sqlhooks should stick with the majority (and most popular) drivers. Does that make sense?

Reviewable status: 1 of 2 files reviewed, 1 unresolved discussion (waiting on @keegancsmith)

@keegancsmith
Copy link
Collaborator

Since there is no way to support both, I think sqlhooks should stick with the majority (and most popular) drivers. Does that make sense?

There is a way, you do type checks against the driver to find out, if it does you return a driver which does support BeginCtx. We do that in other parts of sqlhooks. However, given what you said I agree. Rather make sqlhooks just support the majority. Not sure what @gchaincl thinks.

Copy link
Collaborator Author

@m12i m12i left a comment

Choose a reason for hiding this comment

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

Where would you check against the driver? if it's inside theBeginTx it's too late. I don't see how that can be done. Could you please elaborate?

Reviewable status: 1 of 2 files reviewed, 1 unresolved discussion (waiting on @keegancsmith)

@keegancsmith
Copy link
Collaborator

See the Open function.

Copy link
Collaborator Author

@m12i m12i left a comment

Choose a reason for hiding this comment

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

oh I see. I did start checking it in Open() but it would become a combinatorial explosion. Would that be like 5 more ifs to count for the combination of BeginCtxer and all existing ifs ?

Reviewable status: 1 of 2 files reviewed, 1 unresolved discussion (waiting on @keegancsmith)

@qustavo
Copy link
Owner

qustavo commented Apr 1, 2019

I'm not sure about what's the best choice here, although dropping support for the drivers that don't implement BeginTx sounds reasonable, it's not ideal since we (or at least I) don't know for sure what the affected drivers are. I like the idea of duplicating what https://sourcegraph.com/github.com/golang/go@go1.12/-/blob/src/database/sql/ctxutil.go#L110-120 is doing, even if the code might change, the impact shouldn't huge.

@qustavo
Copy link
Owner

qustavo commented May 12, 2020

any progress on this @m12i ?

@asdine asdine closed this in #33 Jan 21, 2021
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