Skip to content

Conversation

a-hilaly
Copy link
Member

@a-hilaly a-hilaly commented Jul 6, 2021

Issue N/A

ack-generate uses go-git to clone and fetch repository tags, these
actions are time consuming (more than a minute for cloning) and do
not tolerate interruption (ctrl+C). Forcing a command to exit when it's
running a git.Clone or a git.FetchTags cause the process to write
incomplete data on disk, therefore re-running ack-generate will
always return an error. The only solution for this problem is to delete
the local sdk cache and let ack-generate reclone (without interruption)

This patch adds a graceful cancellation system for ack-generate
subcommads. Which will allow users to safely ctrl+C and re-run their
commands without any errors.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

`ack-generate` uses go-git to clone and fetch repository tags, these
actions can take time (more than a minute for cloning) and do not
tollerate interruption (ctrl+C). Forcing a command to exit when it's
running a `git.Clone` or a `git.FetchTags` cause the process to write
uncomplete data on disk, and therefore it is impossible to re-run
`ack-generate` without cleaning up the local cache.

This patch adds a gracefull cancellation system for `ack-generate`
subcommads. Which will allow users to safely ctrl+C and re-run their
commands without any errors.
@RedbackThomson
Copy link
Contributor

I see that it sets up a cancel context and prescribes timeouts to each git operation, but how does this prevent it from creating a faulty local version of the repositories?

@a-hilaly
Copy link
Member Author

a-hilaly commented Jul 7, 2021

I see that it sets up a cancel context and prescribes timeouts to each git operation, but how does this prevent it from creating a faulty local version of the repositories?

Yes on one hand this PR adds default timeouts for each git operation, on the other hand we also propagate a cancellation mechanism to all the functions using the main context (context.Background())

Short answer: It is up the go-git to properly handle context cancellation (which they are "trying" to do). We just have to properly ask go-git functions to cancel what they're doing before killing the process.

The cancellation mechanism doesn't guaranty that go-git will not write incomplete data and/or missing git blobs - but only ensure that we are correctly using go-git functions and any function that takes a context.Context as first parameter.

long answer:

Calling a cancel (function return by context.WIthCancel) sends a signal using the channel that is accessible via ctx.Done(). Every function that needs to implement graceful cancellation needs to listen for a ctx.Done() signal using a select statement.

How does go-git handle graceful cancellation for network calls and disk i/o operations ?
For disk IO operations, go-git uses a specific io.Writer implementations that listens to ctx.Done messages - note line 69 that is waiting for a ctx.Done() signal - which is part of the context.Context interface functions. (and still this implementation is not perfect since there no way to cancel a single write op using go stdlib)

For network calls - the cancellation mechanism is implemented in the standard libraries net, net/http, net/http/httputil ... some examples of handle cancel signals: https://github.com/golang/go/blob/2ebe77a2fda1ee9ff6fd9a3e08933ad1ebaea039/src/net/dial.go#L527-L558https://github.com/golang/go/blob/2ebe77a2fda1ee9ff6fd9a3e08933ad1ebaea039/src/net/dial.go#L384-L395

@RedbackThomson
Copy link
Contributor

Glad go-git has support for cancellation contexts. Looks good!

/lgtm

@ack-bot ack-bot added the lgtm Indicates that a PR is ready to be merged. label Jul 7, 2021
@ack-bot
Copy link
Collaborator

ack-bot commented Jul 7, 2021

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: A-Hilaly, RedbackThomson, vijtrip2

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [A-Hilaly,RedbackThomson,vijtrip2]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ack-bot ack-bot merged commit 25e4809 into aws-controllers-k8s:main Jul 7, 2021
// recreate the context.CancelFunc
cancelFunc := func() {
signal.Stop(signalCh)
cancel()
Copy link
Collaborator

Choose a reason for hiding this comment

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

wouldn't cancel have been called already on line 53?

@a-hilaly
Copy link
Member Author

a-hilaly commented Jul 8, 2021

@jaypipes If signalCh receives a message in L52 yes. However it's safe to call a context.CancelFunc more than once: https://github.com/golang/go/blob/master/src/context/context.go#L404

RedbackThomson pushed a commit to RedbackThomson/ack-code-generator that referenced this pull request Jul 8, 2021
…-k8s#123)

Issue N/A

`ack-generate` uses go-git to clone and fetch repository tags, these
actions are time consuming (more than a minute for cloning) and do
not tolerate interruption (ctrl+C). Forcing a command to exit when it's
running a `git.Clone` or a `git.FetchTags` cause the process to write
incomplete data on disk,  therefore re-running `ack-generate` will
always return an error. The only solution for this problem is to delete
the local sdk cache and let `ack-generate` reclone (without interruption)

This patch adds a graceful cancellation system for `ack-generate`
subcommads. Which will allow users to safely ctrl+C and re-run their
commands without any errors.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved lgtm Indicates that a PR is ready to be merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants