Skip to content

Conversation

@nakabonne
Copy link
Member

What this PR does / why we need it:
This PR fixes two EventWatcher issues that:

  • can't register multiple events during a single fetch interval
  • we have to restart Piped when the repository was force pushed

Which issue(s) this PR fixes:

Fixes #1934

Does this PR introduce a user-facing change?:

Fix a bug that can't register multiple events during a single fetch interval

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 32.43%. This pull request decreases coverage by -0.02%.

File Function Base Head Diff
pkg/app/piped/eventwatcher/eventwatcher.go watcher.cloneRepo -- 0.00% +0.00%
pkg/app/piped/eventwatcher/eventwatcher.go watcher.Run 0.00% 0.00% +0.00%
pkg/app/piped/eventwatcher/eventwatcher.go watcher.run 0.00% 0.00% +0.00%

w.logger.Info(fmt.Sprintf("event watcher will update %d outdated values", len(commits)))
for _, c := range commits {
if err := tmpRepo.CommitChanges(ctx, tmpRepo.GetClonedBranch(), c.message, false, c.changes); err != nil {
if err := repo.CommitChanges(ctx, repo.GetClonedBranch(), c.message, false, c.changes); err != nil {
Copy link
Member Author

Choose a reason for hiding this comment

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

The root cause is that it tries to edit files before committing even though we've already written to files in L254.

So I settled on using tmpRepo only for local editing.

func (w *watcher) updateValues(ctx context.Context, repo git.Repo, events []config.EventWatcherEvent, commitMsg string) error {
// Copy the repo to another directory to avoid pull failure in the future.
// Copy the repo to another directory to modify local file to avoid reverting previous changes.
tmpDir, err := ioutil.TempDir("", "event-watcher")
Copy link
Member Author

Choose a reason for hiding this comment

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

Previously, to avoid pull failure in the future, it copied the repository.
Ref: #1411 (comment)

However, we should assume that the commit history of the repository will be broken for some reason kind of like the possibility of force pushes and so on.

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 32.43%. This pull request decreases coverage by -0.02%.

File Function Base Head Diff
pkg/app/piped/eventwatcher/eventwatcher.go watcher.cloneRepo -- 0.00% +0.00%
pkg/app/piped/eventwatcher/eventwatcher.go watcher.Run 0.00% 0.00% +0.00%
pkg/app/piped/eventwatcher/eventwatcher.go watcher.run 0.00% 0.00% +0.00%

@khanhtc1202
Copy link
Member

Nice catch
/lgtm

// from the control-plane to compare the value with one in the git repository.
func (w *watcher) Run(ctx context.Context) error {
w.logger.Info("start running event watcher")

Copy link
Member

Choose a reason for hiding this comment

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

How about making a temporary working directory for watcher and storing all cloned source code into that directory?
By that way, we can add a defer to clean the whole directory to ensure that everything will be removed when the watcher stopped.
And don't have to worry about this ignore:

repo, _ = w.cloneRepo(ctx, repoCfg)

@pipecd-bot pipecd-bot removed the lgtm label Jun 29, 2021
@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 32.45%. This pull request decreases coverage by -0.00%.

File Function Base Head Diff
pkg/app/piped/eventwatcher/eventwatcher.go watcher.cloneRepo -- 0.00% +0.00%
pkg/app/piped/eventwatcher/eventwatcher.go watcher.commitFiles -- 0.00% +0.00%
pkg/app/piped/eventwatcher/eventwatcher.go watcher.Run 0.00% 0.00% +0.00%
pkg/app/piped/eventwatcher/eventwatcher.go watcher.run 0.00% 0.00% +0.00%
pkg/app/piped/eventwatcher/eventwatcher.go watcher.updateValues 0.00% 0.00% +0.00%
pkg/app/piped/eventwatcher/eventwatcher.go watcher.modifyFiles 0.00% -- +-0.00%

@pipecd-bot pipecd-bot added size/L and removed size/M labels Jun 29, 2021
@nakabonne
Copy link
Member Author

Applied the working directory solution so that it can remove all repository at last. (@nghialv 's idea!)

I settled on committing right after writing all changes in an event to a file. It is no problem to call writeFile twice (changing CommitChanges has also no problem because only EventWatcher uses though)

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 32.44%. This pull request decreases coverage by -0.02%.

File Function Base Head Diff
pkg/app/piped/eventwatcher/eventwatcher.go watcher.cloneRepo -- 0.00% +0.00%
pkg/app/piped/eventwatcher/eventwatcher.go watcher.commitFiles -- 0.00% +0.00%
pkg/app/piped/eventwatcher/eventwatcher.go watcher.Run 0.00% 0.00% +0.00%
pkg/app/piped/eventwatcher/eventwatcher.go watcher.run 0.00% 0.00% +0.00%
pkg/app/piped/eventwatcher/eventwatcher.go watcher.updateValues 0.00% 0.00% +0.00%
pkg/app/piped/eventwatcher/eventwatcher.go watcher.modifyFiles 0.00% -- +-0.00%

w.logger.Info("Try to re-clone because it's more likely to be unable to pull the next time too",
zap.String("repo-id", repoCfg.RepoID),
)
repo, _ = w.cloneRepo(ctx, repoCfg)
Copy link
Member

Choose a reason for hiding this comment

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

Is it ok to ignore the retuned error?

Copy link
Member Author

Choose a reason for hiding this comment

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

All we have to do here is just emit error log. cloneRepo() does it, that's why it ignores the error.

Copy link
Member Author

Choose a reason for hiding this comment

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

But conventionally, cloneRepo() should just give back an error and caller emits log.

Copy link
Member

Choose a reason for hiding this comment

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

👍


// cloneRepo clones the git repository under the working directory.
func (w *watcher) cloneRepo(ctx context.Context, repoCfg config.PipedRepository) (git.Repo, error) {
repo, err := w.gitClient.Clone(ctx, repoCfg.RepoID, repoCfg.Remote, repoCfg.Branch, filepath.Join(w.workingDir, repoCfg.RepoID))
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should use a random directory instead of fixing it with the repository ID because this could be affected by the error at line 142.

Copy link
Member Author

Choose a reason for hiding this comment

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

Right.

for _, e := range events {
latestEvent, ok := w.eventGetter.GetLatest(ctx, e.Name, e.Labels)
if !ok {
if err := w.commitFiles(ctx, &e, tmpRepo, commitMsg); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

&e is pointing to the same one. So we should passing struct instead of a pointer.

Copy link
Member Author

Choose a reason for hiding this comment

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

It is. Actually it works well at this logic. But we'd prefer copying it.

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 32.43%. This pull request decreases coverage by -0.03%.

File Function Base Head Diff
pkg/app/piped/eventwatcher/eventwatcher.go watcher.cloneRepo -- 0.00% +0.00%
pkg/app/piped/eventwatcher/eventwatcher.go watcher.commitFiles -- 0.00% +0.00%
pkg/app/piped/eventwatcher/eventwatcher.go watcher.updateValues 0.00% 0.00% +0.00%
pkg/app/piped/eventwatcher/eventwatcher.go watcher.Run 0.00% 0.00% +0.00%
pkg/app/piped/eventwatcher/eventwatcher.go watcher.run 0.00% 0.00% +0.00%
pkg/app/piped/eventwatcher/eventwatcher.go watcher.modifyFiles 0.00% -- +-0.00%

@nakabonne
Copy link
Member Author

Fixed

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 32.42%. This pull request decreases coverage by -0.03%.

File Function Base Head Diff
pkg/app/piped/eventwatcher/eventwatcher.go watcher.cloneRepo -- 0.00% +0.00%
pkg/app/piped/eventwatcher/eventwatcher.go watcher.commitFiles -- 0.00% +0.00%
pkg/app/piped/eventwatcher/eventwatcher.go watcher.run 0.00% 0.00% +0.00%
pkg/app/piped/eventwatcher/eventwatcher.go watcher.updateValues 0.00% 0.00% +0.00%
pkg/app/piped/eventwatcher/eventwatcher.go watcher.Run 0.00% 0.00% +0.00%
pkg/app/piped/eventwatcher/eventwatcher.go watcher.modifyFiles 0.00% -- +-0.00%

@nghialv
Copy link
Member

nghialv commented Jun 29, 2021

Nice.
/lgtm

@khanhtc1202
Copy link
Member

🚀
/approve

@pipecd-bot
Copy link
Collaborator

APPROVE

This pull request is APPROVED by khanhtc1202.

Approvers can cancel the approval by writing /approve cancel in a comment. Any additional commits also will change this pull request to be not-approved.

@pipecd-bot pipecd-bot merged commit 91ec48c into master Jun 29, 2021
@pipecd-bot pipecd-bot deleted the multiple-events-registering branch June 29, 2021 09:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Events not committed by piped

5 participants