-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Use an atomic operation to calculate Issue.Index #7944
Conversation
Codecov Report
@@ Coverage Diff @@
## master #7944 +/- ##
==========================================
+ Coverage 41.47% 41.49% +0.01%
==========================================
Files 478 479 +1
Lines 63975 63965 -10
==========================================
+ Hits 26535 26541 +6
+ Misses 33988 33968 -20
- Partials 3452 3456 +4
Continue to review full report at Codecov.
|
I've just noticed that xorm re-reads the calculated value for me, so calling |
aad1cf1
to
741d920
Compare
*Rebased due to changes in |
I haven't tested it but does it select the max index for the specific repo id or across all repo ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the generated SQL request from xorm repo, it should be good.
Something wrong with the issue/comment template? Mmm.... |
On a side note: the concurrency problem exist also for PR since they used GetMaxIndexOfIssue. But first let validate this PR. 😄 |
…into issue-idx-insert-select
@@ -1109,10 +1103,19 @@ func newIssue(e *xorm.Session, doer *User, opts NewIssueOptions) (err error) { | |||
} | |||
|
|||
// Milestone and assignee validation should happen before insert actual object. | |||
if _, err = e.Insert(opts.Issue); err != nil { | |||
if _, err := e.SetExpr("`index`", "coalesce(MAX(`index`),0)+1"). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this really supported in all db types?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes it should
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lafriks Check my comment about DB support here: #7898 (comment)
As far as the syntax and the functionality, they are supported. About the atomicity, in theory it's there, but It's very difficult to test. There some WIP about that here #7950 and here #7959.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To validate there is an integration TestAPICreateIssue
that validate the creation across all db types.
@sapk In fact, that value is being ignored (it always was). I might PR for that later. |
@typeless , @sapk, @lunny See #7959. Sadly, for solving our duplicate index problem, the MSSQL: With MySQL, a lock is in place but somehow creates a deadlock. With MSSQL and PostgreSQL, if there's a lock, it is not kept from the So, we're back to square one regarding this, and the only viable solution at the moment seems to retry the insert, as @lunny proposed. |
Close due to failing tests on #7959 |
Did you try SetExpr with full select max? |
I don't that will only supports sqlite3 because I have added tests on go-xorm/xorm#1401 and it will be executed on sqlite/mysql/postgres/mssql both on circleci and drone. |
@lunny I'm sorry, of course the statement works well in all databases. Your modification generates the right code and it passed all the existing tests on Gitea. However, it does not fix #7887. It does not work if multiple users attempt to insert at the same time. There are all kinds of concurrency problems. Please see my test in #7959 for details. |
Please notice that I've referenced the master version of xorm (xorm.io/builder,github.com/go-xorm/xorm) to build this. I'm not sure if I did it correctly.
Closes #7887
This PR takes advantage of the new feature supported by xorm of building the
INSERT
from aSELECT
.This is the new code built by xorm (SQLite):
Instead of:
This will prevent the possible race condition mentioned in #7848 by making the index number calculated in the same operation as the insert.
A thread with an alternate solution and its discussion is: #7898,