Skip to content

Commit

Permalink
Change CheckSyncedGitCommits to IsGitCommitSynced (#2561)
Browse files Browse the repository at this point in the history
We never passed more than one hash in.
  • Loading branch information
saquibmian authored Nov 10, 2023
1 parent de2169f commit d6484f2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 36 deletions.
11 changes: 5 additions & 6 deletions private/buf/bufsync/bufsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,13 @@ type Handler interface {
branch string,
) (git.Hash, error)

// 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(
// IsGitCommitSynced is invoked when syncing branches to know if a Git commit is already synced.
// If an error is returned, sync will abort.
IsGitCommitSynced(
ctx context.Context,
module bufmoduleref.ModuleIdentity,
commitHashes map[string]struct{},
) (map[string]struct{}, error)
hash git.Hash,
) (bool, 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: 5 additions & 10 deletions private/buf/bufsync/bufsync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,18 +221,13 @@ func (c *mockSyncHandler) SyncModuleCommit(
return err
}

func (c *mockSyncHandler) CheckSyncedGitCommits(
func (c *mockSyncHandler) IsGitCommitSynced(
ctx context.Context,
module bufmoduleref.ModuleIdentity,
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
hash git.Hash,
) (bool, error) {
_, isSynced := c.syncedCommitsSHAs[hash.Hex()]
return isSynced, nil
}

var _ bufsync.Handler = (*mockSyncHandler)(nil)
11 changes: 5 additions & 6 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, commitHash)
isSynced, err := s.isGitCommitSynced(ctx, moduleIdentity, commit.Hash())
if err != nil {
return fmt.Errorf(
"checking if module %s already synced git commit %s: %w",
Expand Down Expand Up @@ -516,26 +516,25 @@ 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 string) (bool, error) {
func (s *syncer) isGitCommitSynced(ctx context.Context, moduleIdentity bufmoduleref.ModuleIdentity, commitHash git.Hash) (bool, error) {
modIdentity := moduleIdentity.IdentityString()
// check local cache first
if syncedModuleCommits, ok := s.modulesIdentitiesToCommitsSyncedCache[modIdentity]; ok {
if _, commitSynced := syncedModuleCommits[commitHash]; commitSynced {
if _, commitSynced := syncedModuleCommits[commitHash.Hex()]; commitSynced {
return true, nil
}
}
// not in the cache, request BSR check
syncedModuleCommits, err := s.handler.CheckSyncedGitCommits(ctx, moduleIdentity, map[string]struct{}{commitHash: {}})
commitSynced, err := s.handler.IsGitCommitSynced(ctx, moduleIdentity, 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] = struct{}{}
s.modulesIdentitiesToCommitsSyncedCache[modIdentity][commitHash.Hex()] = struct{}{}
}
return commitSynced, nil
}
Expand Down
19 changes: 5 additions & 14 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,7 +31,6 @@ 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 @@ -88,30 +87,22 @@ func (h *syncHandler) ResolveSyncPoint(ctx context.Context, module bufmoduleref.
return hash, nil
}

func (h *syncHandler) CheckSyncedGitCommits(ctx context.Context, module bufmoduleref.ModuleIdentity, commitHashes map[string]struct{}) (map[string]struct{}, error) {
func (h *syncHandler) IsGitCommitSynced(ctx context.Context, module bufmoduleref.ModuleIdentity, hash git.Hash) (bool, 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: stringutil.MapToSlice(commitHashes),
LabelNames: []string{hash.Hex()},
}))
if err != nil {
if connect.CodeOf(err) == connect.CodeNotFound {
// Repo is not created
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, nil
}
syncedHashes[syncedHash] = struct{}{}
return false, fmt.Errorf("get labels in namespace: %w", err)
}
return syncedHashes, nil
return len(res.Msg.Labels) == 1, nil
}

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

0 comments on commit d6484f2

Please sign in to comment.