Skip to content

Commit 4e50a2a

Browse files
authored
Revert mutex changes in git.go and cr-cache (kptdev#372)
1 parent 830c956 commit 4e50a2a

3 files changed

Lines changed: 30 additions & 24 deletions

File tree

pkg/cache/crcache/cache.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ func (c *Cache) OpenRepository(ctx context.Context, repositorySpec *configapi.Re
5656
lock.Lock()
5757
defer lock.Unlock()
5858

59-
c.mainLock.Lock()
60-
defer c.mainLock.Unlock()
59+
c.mainLock.RLock()
6160

6261
if repo, ok := c.repositories[key]; ok && repo != nil {
62+
c.mainLock.RUnlock()
6363
// Keep the spec updated in the cache.
6464
repo.repoSpec = repositorySpec
6565
// Check external repo connectivity
@@ -74,6 +74,7 @@ func (c *Cache) OpenRepository(ctx context.Context, repositorySpec *configapi.Re
7474
}
7575
return repo, nil
7676
}
77+
c.mainLock.RUnlock()
7778

7879
externalRepo, err := externalrepo.CreateRepositoryImpl(ctx, repositorySpec, c.options.ExternalRepoOptions)
7980
if err != nil {
@@ -82,7 +83,9 @@ func (c *Cache) OpenRepository(ctx context.Context, repositorySpec *configapi.Re
8283

8384
cachedRepo := newRepository(key, repositorySpec, externalRepo, c.metadataStore, c.options)
8485

86+
c.mainLock.Lock()
8587
c.repositories[key] = cachedRepo
88+
c.mainLock.Unlock()
8689

8790
return cachedRepo, nil
8891
}

pkg/externalrepo/git/git.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -300,15 +300,12 @@ func (r *gitRepository) Version(ctx context.Context) (string, error) {
300300
defer span.End()
301301

302302
r.mutex.Lock()
303-
err := r.fetchRemoteRepositoryWithRetry(ctx)
304-
r.mutex.Unlock()
305-
if err != nil {
303+
defer r.mutex.Unlock()
304+
305+
if err := r.fetchRemoteRepositoryWithRetry(ctx); err != nil {
306306
return "", err
307307
}
308308

309-
r.mutex.RLock()
310-
defer r.mutex.RUnlock()
311-
312309
refs, err := r.repo.References()
313310
if err != nil {
314311
return "", err
@@ -452,9 +449,9 @@ func (r *gitRepository) CreatePackageRevisionDraft(ctx context.Context, obj *por
452449
defer span.End()
453450

454451
_, mutexSpan := tracer.Start(ctx, "gitRepository::CreatePackageRevisionDraft::acquire_mutex")
455-
r.mutex.RLock()
452+
r.mutex.Lock()
456453
mutexSpan.End()
457-
defer r.mutex.RUnlock()
454+
defer r.mutex.Unlock()
458455

459456
var base plumbing.Hash
460457
refName := r.branch.RefInLocal()
@@ -703,8 +700,8 @@ func (r *gitRepository) fetchRemoteRepositoryWithRetry(ctx context.Context) erro
703700
func (r *gitRepository) GetPackageRevision(ctx context.Context, version, path string) (repository.PackageRevision, kptfilev1.GitLock, error) {
704701
ctx, span := tracer.Start(ctx, "gitRepository::GetPackageRevision", trace.WithAttributes())
705702
defer span.End()
706-
r.mutex.RLock()
707-
defer r.mutex.RUnlock()
703+
r.mutex.Lock()
704+
defer r.mutex.Unlock()
708705

709706
var hash plumbing.Hash
710707

@@ -1056,6 +1053,8 @@ func (r *gitRepository) getAuthMethod(ctx context.Context, forceRefresh bool) (t
10561053
}
10571054

10581055
func (r *gitRepository) GetRepo() (string, error) {
1056+
r.mutex.Lock()
1057+
defer r.mutex.Unlock()
10591058

10601059
origin, err := r.repo.Remote("origin")
10611060
if err != nil {
@@ -1346,8 +1345,8 @@ func visitCommitsCollectErrors(iterator object.CommitIter, callback commitCallba
13461345
}
13471346

13481347
func (r *gitRepository) GetResources(hash plumbing.Hash) (map[string]string, error) {
1349-
r.mutex.RLock()
1350-
defer r.mutex.RUnlock()
1348+
r.mutex.Lock()
1349+
defer r.mutex.Unlock()
13511350

13521351
resources := map[string]string{}
13531352

@@ -1412,8 +1411,8 @@ type commitCallback func(*object.Commit) error
14121411
func (r *gitRepository) GetLifecycle(ctx context.Context, pkgRev *gitPackageRevision) porchapi.PackageRevisionLifecycle {
14131412
_, span := tracer.Start(ctx, "gitRepository::GetLifecycle", trace.WithAttributes())
14141413
defer span.End()
1415-
r.mutex.RLock()
1416-
defer r.mutex.RUnlock()
1414+
r.mutex.Lock()
1415+
defer r.mutex.Unlock()
14171416

14181417
return r.getLifecycle(pkgRev)
14191418
}

test/e2e/e2e_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,7 +1665,7 @@ func (t *PorchSuite) TestPackageUpgrade() {
16651665
)
16661666

16671667
t.RegisterGitRepositoryF(t.GetTestBlueprintsRepoURL(), TestBlueprintsRepoName, "", GiteaUser, GiteaPassword)
1668-
1668+
16691669
// Register the repository as 'downstream'
16701670
t.RegisterGitRepositoryF(t.GetPorchTestRepoURL(), gitRepository, "", GiteaUser, GiteaPassword)
16711671

@@ -1811,11 +1811,10 @@ func (t *PorchSuite) TestRegisterRepository() {
18111811
const (
18121812
repository = "register"
18131813
)
1814-
t.RegisterGitRepositoryF(t.GetPorchTestRepoURL(), repository, "", GiteaUser,
1815-
GiteaPassword, RepositoryOptions{RepOpts: withType(configapi.RepositoryTypeGit)},
1814+
t.RegisterGitRepositoryF(t.GetPorchTestRepoURL(), repository, "", GiteaUser,
1815+
GiteaPassword, RepositoryOptions{RepOpts: withType(configapi.RepositoryTypeGit)},
18161816
RepositoryOptions{RepOpts: WithDeployment()})
18171817

1818-
18191818
var repo configapi.Repository
18201819
t.GetF(client.ObjectKey{
18211820
Namespace: t.Namespace,
@@ -2309,7 +2308,7 @@ func (t *PorchSuite) TestLargePackageRevision() {
23092308
const testDataSize = 5 * 1024 * 1024
23102309

23112310
setAnnotationsImage := t.GcrPrefix + "/set-annotations:v0.1.5" // set-annotations:v0.1.5 is an older version that porch maps neither to built-in nor exec.
2312-
2311+
23132312
t.RegisterGitRepositoryF(t.GetPorchTestRepoURL(), "git-fn-pod-large", "", GiteaUser, GiteaPassword)
23142313

23152314
// Create Package Revision
@@ -2780,7 +2779,7 @@ func (t *PorchSuite) TestPackageRevisionGCWithOwner() {
27802779
}
27812780

27822781
func (t *PorchSuite) TestPackageRevisionGCAsOwner() {
2783-
// TODO: Garbage collection is not working when a DB cache PackageRevision resource owner is deleted.
2782+
// TODO: Garbage collection is not working when a DB cache PackageRevision resource owner is deleted.
27842783
// We need to get this test running in DB cache
27852784
if _, ok := os.LookupEnv("DB_CACHE"); ok {
27862785
return
@@ -2957,7 +2956,7 @@ func (t *PorchSuite) TestPackageRevisionFinalizers() {
29572956
func (t *PorchSuite) TestPackageRevisionInMultipleNamespaces() {
29582957

29592958
registerRepoAndTestRevisions := func(repoName string, ns string, oldPRs []porchapi.PackageRevision) []porchapi.PackageRevision {
2960-
t.RegisterGitRepositoryF(t.GetTestBlueprintsRepoURL(), repoName, "", GiteaUser, GiteaPassword,
2959+
t.RegisterGitRepositoryF(t.GetTestBlueprintsRepoURL(), repoName, "", GiteaUser, GiteaPassword,
29612960
RepositoryOptions{RepOpts: InNamespace(ns), SecOpts: SecretInNamespace(ns)})
29622961
prList := porchapi.PackageRevisionList{}
29632962
t.ListF(&prList, client.InNamespace(ns))
@@ -3269,6 +3268,11 @@ func (t *PorchSuite) TestRepositoryModify() {
32693268
continue
32703269
}
32713270

3271+
if ready.Status == metav1.ConditionFalse && ready.Reason == configapi.ReasonReconciling {
3272+
t.Logf("Repository sync in progress")
3273+
continue
3274+
}
3275+
32723276
if got, want := ready.Status, metav1.ConditionTrue; got != want {
32733277
t.Errorf("Repository Ready Condition Status; got %q, want %q", got, want)
32743278
}
@@ -3497,7 +3501,7 @@ func (t *PorchSuite) TestPackageRevisionListWithTwoHangingRepositories() {
34973501
Description: "Working Git repository",
34983502
Type: configapi.RepositoryTypeGit,
34993503
Git: &configapi.GitRepository{
3500-
Repo: t.GetPorchTestRepoURL(),
3504+
Repo: t.GetPorchTestRepoURL(),
35013505
SecretRef: configapi.SecretRef{
35023506
Name: t.CreateOrUpdateSecret(workingRepoName, GiteaUser, GiteaPassword),
35033507
},

0 commit comments

Comments
 (0)