Skip to content
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

Fix bugs when concurrent pushing packages #30335

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 46 additions & 8 deletions models/packages/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import (
"context"
"fmt"
"strconv"
"strings"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"

"xorm.io/builder"
Expand Down Expand Up @@ -189,23 +191,59 @@

// TryInsertPackage inserts a package. If a package exists already, ErrDuplicatePackage is returned
func TryInsertPackage(ctx context.Context, p *Package) (*Package, error) {
e := db.GetEngine(ctx)
switch {
case setting.Database.Type.IsMySQL():
if _, err := db.GetEngine(ctx).Exec("INSERT INTO package (owner_id,`type`,lower_name,name,semver_compatible) VALUES (?,?,?,?,?) ON DUPLICATE KEY UPDATE 1=1",
p.OwnerID, p.Type, p.LowerName, p.Name, p.SemverCompatible); err != nil {
return nil, err
}
case setting.Database.Type.IsPostgreSQL(), setting.Database.Type.IsSQLite3():
if _, err := db.GetEngine(ctx).Exec("INSERT INTO package (owner_id,`type`,lower_name,name,semver_compatible) VALUES (?,?,?,?,?) ON CONFLICT (owner_id,`type`,lower_name) DO UPDATE SET lower_name=lower_name",
Copy link

Choose a reason for hiding this comment

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

I think this line should look like:
"INSERT INTO package (owner_id,type,lower_name,name,semver_compatible) VALUES (?,?,?,?,?) ON CONFLICT (owner_id,type,lower_name) DO UPDATE SET lower_name=EXCLUDED.lower_name"

p.OwnerID, p.Type, p.LowerName, p.Name, p.SemverCompatible); err != nil {
return nil, err
}
case setting.Database.Type.IsMSSQL():
r := func(s string) string {
return strings.Replace(s, "'", "''", -1)

Check failure on line 207 in models/packages/package.go

View workflow job for this annotation

GitHub Actions / lint-backend

wrapperFunc: use strings.ReplaceAll method in `strings.Replace(s, "'", "''", -1)` (gocritic)

Check failure on line 207 in models/packages/package.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

wrapperFunc: use strings.ReplaceAll method in `strings.Replace(s, "'", "''", -1)` (gocritic)

Check failure on line 207 in models/packages/package.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

wrapperFunc: use strings.ReplaceAll method in `strings.Replace(s, "'", "''", -1)` (gocritic)
}
sql := fmt.Sprintf(`MERGE INTO package WITH (HOLDLOCK) AS target
USING (SELECT
%d AS owner_id
,'%s' AS [type]
,'%s' AS lower_name
,'%s' AS name
, %s AS semver_compatible) AS source
(owner_id, [type], lower_name, name, semver_compatible)
ON (target.owner_id = source.owner_id
AND target.[type] = source.[type]
AND target.lower_name = source.lower_name)
WHEN MATCHED
THEN UPDATE
SET 1 = 1
WHEN NOT MATCHED
THEN INSERT (owner_id, [type], lower_name, name, semver_compatible)
VALUES (%d, '%s', '%s', '%s', %s)`,
lunny marked this conversation as resolved.
Show resolved Hide resolved
p.OwnerID, r(string(p.Type)), r(p.LowerName), r(p.Name), strconv.FormatBool(p.SemverCompatible),
p.OwnerID, r(string(p.Type)), r(p.LowerName), r(p.Name), strconv.FormatBool(p.SemverCompatible),
)

existing := &Package{}
if _, err := db.GetEngine(ctx).Exec(sql); err != nil {
return nil, err
}

has, err := e.Where(builder.Eq{
}

var existing Package
has, err := db.GetEngine(ctx).Where(builder.Eq{
"owner_id": p.OwnerID,
"type": p.Type,
"lower_name": p.LowerName,
}).Get(existing)
}).Get(&existing)
if err != nil {
return nil, err
}
if has {
return existing, ErrDuplicatePackage
}
if _, err = e.Insert(p); err != nil {
return nil, err
return &existing, ErrDuplicatePackage
}
return p, nil
}
Expand Down
33 changes: 33 additions & 0 deletions tests/integration/api_packages_maven_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"strconv"
"strings"
"sync"
"testing"

"code.gitea.io/gitea/models/db"
Expand Down Expand Up @@ -242,3 +243,35 @@ func TestPackageMaven(t *testing.T) {
putFile(t, fmt.Sprintf("/%s/maven-metadata.xml", snapshotVersion), "test-overwrite", http.StatusCreated)
})
}

func TestPackageMavenConcurrent(t *testing.T) {
defer tests.PrepareTestEnv(t)()

user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})

groupID := "com.gitea"
artifactID := "test-project"
packageVersion := "1.0.1"

root := fmt.Sprintf("/api/packages/%s/maven/%s/%s", user.Name, strings.ReplaceAll(groupID, ".", "/"), artifactID)

putFile := func(t *testing.T, path, content string, expectedStatus int) {
req := NewRequestWithBody(t, "PUT", root+path, strings.NewReader(content)).
AddBasicAuth(user.Name)
MakeRequest(t, req, expectedStatus)
}

t.Run("Concurrent Upload", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()

var wg sync.WaitGroup
for i := 0; i < 10; i++ {
Copy link

@tlusser tlusser Apr 16, 2024

Choose a reason for hiding this comment

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

I think this test is not correct - it should look like:
`

	for i := 0; i < 10; i++ {
		wg.Add(1)
		go func(i int) {
			putFile(t, fmt.Sprintf("/%s/%s.jar", packageVersion, strconv.Itoa(i)), "test", http.StatusCreated)
			wg.Done()
		}(i)
	}

`

wg.Add(1)
go func() {
putFile(t, fmt.Sprintf("/%s/%s.jar", packageVersion, strconv.Itoa(i)), "test", http.StatusCreated)
wg.Done()
}()
}
wg.Wait()
})
}
Loading