diff options
author | Sami Hiltunen <shiltunen@gitlab.com> | 2022-05-03 15:57:20 +0300 |
---|---|---|
committer | Sami Hiltunen <shiltunen@gitlab.com> | 2022-05-03 16:03:11 +0300 |
commit | 7c78dcb8514bba6195993f83706bacebe3b71dd4 (patch) | |
tree | a37eac88988dcdec667088e06b68a08fe8412cc2 | |
parent | a374f9bf00471e249d8c5d2331ae084481bee450 (diff) |
Return correct error when repository has no up to date replicassmh-no-up-to-date-replicas-error
GetConsistentStorages retrieves the up to date replicas of a repository.
If the repository has no up to date replicas, it returns a repository
not found error. This is incorrect as the repository may still exist
from Praefect's point of view, it's just that the replicas with the
latest changes have been lost. This commit changes GetConsistentStorages
to return a more descriptive error for this scenario instead of a
repository not found error.
Changelog: fixed
-rw-r--r-- | internal/praefect/datastore/repository_store.go | 8 | ||||
-rw-r--r-- | internal/praefect/datastore/repository_store_test.go | 14 |
2 files changed, 20 insertions, 2 deletions
diff --git a/internal/praefect/datastore/repository_store.go b/internal/praefect/datastore/repository_store.go index 780073155..b2c0378f2 100644 --- a/internal/praefect/datastore/repository_store.go +++ b/internal/praefect/datastore/repository_store.go @@ -576,7 +576,7 @@ func (rs *PostgresRepositoryStore) GetConsistentStoragesByRepositoryID(ctx conte return rs.getConsistentStorages(ctx, ` SELECT replica_path, ARRAY_AGG(storage) FROM repositories -JOIN storage_repositories USING (repository_id, relative_path, generation) +LEFT JOIN storage_repositories USING (repository_id, relative_path, generation) WHERE repository_id = $1 GROUP BY replica_path `, repositoryID) @@ -587,7 +587,7 @@ func (rs *PostgresRepositoryStore) GetConsistentStorages(ctx context.Context, vi replicaPath, storages, err := rs.getConsistentStorages(ctx, ` SELECT replica_path, ARRAY_AGG(storage) FROM repositories -JOIN storage_repositories USING (repository_id, relative_path, generation) +LEFT JOIN storage_repositories USING (repository_id, relative_path, generation) WHERE repositories.virtual_storage = $1 AND repositories.relative_path = $2 GROUP BY replica_path @@ -613,6 +613,10 @@ func (rs *PostgresRepositoryStore) getConsistentStorages(ctx context.Context, qu } result := storages.Slice() + if len(result) == 0 { + return "", nil, fmt.Errorf("repository has no up to date replicas") + } + consistentStorages := make(map[string]struct{}, len(result)) for _, storage := range result { consistentStorages[storage] = struct{}{} diff --git a/internal/praefect/datastore/repository_store_test.go b/internal/praefect/datastore/repository_store_test.go index 9c980376c..a4c50d54b 100644 --- a/internal/praefect/datastore/repository_store_test.go +++ b/internal/praefect/datastore/repository_store_test.go @@ -972,6 +972,20 @@ func TestRepositoryStore_Postgres(t *testing.T) { require.Equal(t, "new-path", replicaPath) require.Equal(t, map[string]struct{}{"storage-1": {}, "storage-2": {}}, storages) }) + + t.Run("returns error when no up to date replicas", func(t *testing.T) { + rs := newRepositoryStore(t, nil) + + require.NoError(t, rs.CreateRepository(ctx, 1, vs, repo, "replica-path", "primary", []string{"secondary"}, nil, false, false)) + require.NoError(t, rs.IncrementGeneration(ctx, 1, "primary", nil)) + require.NoError(t, rs.DeleteReplica(ctx, 1, "primary")) + + _, _, err := rs.GetConsistentStorages(ctx, vs, repo) + require.EqualError(t, err, "repository has no up to date replicas") + + _, _, err = rs.GetConsistentStoragesByRepositoryID(ctx, 1) + require.EqualError(t, err, "repository has no up to date replicas") + }) }) t.Run("DeleteInvalidRepository", func(t *testing.T) { |