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

Revert "Change CheckSyncedGitCommits to IsGitCommitSynced (#2561)" #2562

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
11 changes: 6 additions & 5 deletions private/buf/bufsync/bufsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,14 @@ type Handler interface {
branch string,
) (git.Hash, error)

// IsGitCommitSynced is invoked when syncing branches to know if a Git commit is already synced.
// If an error is returned, sync will abort.
IsGitCommitSynced(
// CheckSyncedGitCommits is invoked when syncing branches to know which commits hashes from a set
// are already synced inthe BSR. It expects to receive the commit hashes that are synced already. If
// an error is returned, sync will abort.
CheckSyncedGitCommits(
ctx context.Context,
module bufmoduleref.ModuleIdentity,
hash git.Hash,
) (bool, error)
commitHashes map[string]struct{},
) (map[string]struct{}, error)

// GetModuleReleaseBranch is invoked before syncing, to gather release branch names for all the
// modules that are about to be synced. If the BSR module does not exist, the implementation should
Expand Down
15 changes: 10 additions & 5 deletions private/buf/bufsync/bufsync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,18 @@ func (c *mockSyncHandler) SyncModuleCommit(
return err
}

func (c *mockSyncHandler) IsGitCommitSynced(
func (c *mockSyncHandler) CheckSyncedGitCommits(
ctx context.Context,
module bufmoduleref.ModuleIdentity,
hash git.Hash,
) (bool, error) {
_, isSynced := c.syncedCommitsSHAs[hash.Hex()]
return isSynced, nil
commitHashes map[string]struct{},
) (map[string]struct{}, error) {
syncedHashes := make(map[string]struct{})
for hash := range commitHashes {
if _, isSynced := c.syncedCommitsSHAs[hash]; isSynced {
syncedHashes[hash] = struct{}{}
}
}
return syncedHashes, nil
}

var _ bufsync.Handler = (*mockSyncHandler)(nil)
11 changes: 6 additions & 5 deletions private/buf/bufsync/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ func (s *syncer) branchSyncableCommits(
commitHash := commit.Hash().Hex()
logger := logger.With(zap.String("commit", commitHash))
// check if this commit is already synced
isSynced, err := s.isGitCommitSynced(ctx, moduleIdentity, commit.Hash())
isSynced, err := s.isGitCommitSynced(ctx, moduleIdentity, commitHash)
if err != nil {
return fmt.Errorf(
"checking if module %s already synced git commit %s: %w",
Expand Down Expand Up @@ -516,25 +516,26 @@ func (s *syncer) branchSyncableCommits(
}

// isGitCommitSynced checks if a commit hash is already synced to a BSR module.
func (s *syncer) isGitCommitSynced(ctx context.Context, moduleIdentity bufmoduleref.ModuleIdentity, commitHash git.Hash) (bool, error) {
func (s *syncer) isGitCommitSynced(ctx context.Context, moduleIdentity bufmoduleref.ModuleIdentity, commitHash string) (bool, error) {
modIdentity := moduleIdentity.IdentityString()
// check local cache first
if syncedModuleCommits, ok := s.modulesIdentitiesToCommitsSyncedCache[modIdentity]; ok {
if _, commitSynced := syncedModuleCommits[commitHash.Hex()]; commitSynced {
if _, commitSynced := syncedModuleCommits[commitHash]; commitSynced {
return true, nil
}
}
// not in the cache, request BSR check
commitSynced, err := s.handler.IsGitCommitSynced(ctx, moduleIdentity, commitHash)
syncedModuleCommits, err := s.handler.CheckSyncedGitCommits(ctx, moduleIdentity, map[string]struct{}{commitHash: {}})
if err != nil {
return false, err
}
_, commitSynced := syncedModuleCommits[commitHash]
if commitSynced {
// populate local cache
if s.modulesIdentitiesToCommitsSyncedCache[modIdentity] == nil {
s.modulesIdentitiesToCommitsSyncedCache[modIdentity] = make(map[string]struct{})
}
s.modulesIdentitiesToCommitsSyncedCache[modIdentity][commitHash.Hex()] = struct{}{}
s.modulesIdentitiesToCommitsSyncedCache[modIdentity][commitHash] = struct{}{}
}
return commitSynced, nil
}
Expand Down
19 changes: 14 additions & 5 deletions private/buf/cmd/buf/command/alpha/repo/reposync/sync_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/bufbuild/buf/private/pkg/connectclient"
"github.com/bufbuild/buf/private/pkg/git"
"github.com/bufbuild/buf/private/pkg/storage"
"github.com/bufbuild/buf/private/pkg/stringutil"
"go.uber.org/zap"
"google.golang.org/protobuf/types/known/timestamppb"
)
Expand Down Expand Up @@ -87,22 +88,30 @@ func (h *syncHandler) ResolveSyncPoint(ctx context.Context, module bufmoduleref.
return hash, nil
}

func (h *syncHandler) IsGitCommitSynced(ctx context.Context, module bufmoduleref.ModuleIdentity, hash git.Hash) (bool, error) {
func (h *syncHandler) CheckSyncedGitCommits(ctx context.Context, module bufmoduleref.ModuleIdentity, commitHashes map[string]struct{}) (map[string]struct{}, error) {
service := connectclient.Make(h.clientConfig, module.Remote(), registryv1alpha1connect.NewLabelServiceClient)
res, err := service.GetLabelsInNamespace(ctx, connect.NewRequest(&registryv1alpha1.GetLabelsInNamespaceRequest{
RepositoryOwner: module.Owner(),
RepositoryName: module.Repository(),
LabelNamespace: registryv1alpha1.LabelNamespace_LABEL_NAMESPACE_GIT_COMMIT,
LabelNames: []string{hash.Hex()},
LabelNames: stringutil.MapToSlice(commitHashes),
}))
if err != nil {
if connect.CodeOf(err) == connect.CodeNotFound {
// Repo is not created
return false, nil
return nil, nil
}
return nil, fmt.Errorf("get labels in namespace: %w", err)
}
syncedHashes := make(map[string]struct{})
for _, label := range res.Msg.Labels {
syncedHash := label.LabelName.Name
if _, expected := commitHashes[syncedHash]; !expected {
return nil, fmt.Errorf("received unexpected synced hash %q, expected %v", syncedHash, commitHashes)
}
return false, fmt.Errorf("get labels in namespace: %w", err)
syncedHashes[syncedHash] = struct{}{}
}
return len(res.Msg.Labels) == 1, nil
return syncedHashes, nil
}

func (h *syncHandler) GetModuleReleaseBranch(ctx context.Context, module bufmoduleref.ModuleIdentity) (string, error) {
Expand Down