You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gitea/models
Kyle Evans e461f0854f
[RFC] Make archival asynchronous (#11296)
* Make archival asynchronous

The prime benefit being sought here is for large archives to not
clog up the rendering process and cause unsightly proxy timeouts.
As a secondary benefit, archive-in-progress is moved out of the
way into a /tmp file so that new archival requests for the same
commit will not get fulfilled based on an archive that isn't yet
finished.

This asynchronous system is fairly primitive; request comes in, we'll
spawn off a new goroutine to handle it, then we'll mark it as done.
Status requests will see if the file exists in the final location,
and report the archival as done when it exists.

Fixes #11265

* Archive links: drop initial delay to three-quarters of a second

Some, or perhaps even most, archives will not take all that long to archive.
The archive process starts as soon as the download button is initially
clicked, so in theory they could be done quite quickly.  Drop the initial
delay down to three-quarters of a second to make it more responsive in the
common case of the archive being quickly created.

* archiver: restructure a little bit to facilitate testing

This introduces two sync.Cond pointers to the archiver package. If they're
non-nil when we go to process a request, we'll wait until signalled (at all)
to proceed. The tests will then create the sync.Cond so that it can signal
at-will and sanity-check the state of the queue at different phases.

The author believes that nil-checking these two sync.Cond pointers on every
archive processing will introduce minimal overhead with no impact on
maintainability.

* gofmt nit: no space around binary + operator

* services: archiver: appease golangci-lint, lock queueMutex

Locking/unlocking the queueMutex is allowed, but not required, for
Cond.Signal() and Cond.Broadcast().  The magic at play here is just a little
too much for golangci-lint, as we take the address of queueMutex and this is
mostly used in archiver.go; the variable still gets flagged as unused.

* archiver: tests: fix several timing nits

Once we've signaled a cond var, it may take some small amount of time for
the goroutines released to hit the spot we're wanting them to be at. Give
them an appropriate amount of time.

* archiver: tests: no underscore in var name, ungh

* archiver: tests: Test* is run in a separate context than TestMain

We must setup the mutex/cond variables at the beginning of any test that's
going to use it, or else these will be nil when the test is actually ran.

* archiver: tests: hopefully final tweak

Things got shuffled around such that we carefully build up and release
requests from the queue, so we can validate the state of the queue at each
step. Fix some assertions that no longer hold true as fallout.

* repo: Download: restore some semblance of previous behavior

When archival was made async, the GET endpoint was only useful if a previous
POST had initiated the download. This commit restores the previous behavior,
to an extent; we'll now submit the archive request there and return a
"202 Accepted" to indicate that it's processing if we didn't manage to
complete the request within ~2 seconds of submission.

This lets a client directly GET the archive, and gives them some indication
that they may attempt to GET it again at a later time.

* archiver: tests: simplify a bit further

We don't need to risk failure and use time.ParseDuration to get 2 *
time.Second.

else if isn't really necessary if the conditions are simple enough and lead
to the same result.

* archiver: tests: resolve potential source of flakiness

Increase all timeouts to 10 seconds; these aren't hard-coded sleeps, so
there's no guarantee we'll actually take that long. If we need longer to
not have a false-positive, then so be it.

While here, various assert.{Not,}Equal arguments are flipped around so that
the wording in error output reflects reality, where the expected argument is
second and actual third.

* archiver: setup infrastructure for notifying consumers of completion

This API will *not* allow consumers to subscribe to specific requests being
completed, just *any* request being completed. The caller is responsible for
determining if their request is satisfied and waiting again if needed.

* repo: archive: make GET endpoint synchronous again

If the request isn't complete, this endpoint will now submit the request and
wait for completion using the new API. This may still be susceptible to
timeouts for larger repos, but other endpoints now exist that the web
interface will use to negotiate its way through larger archive processes.

* archiver: tests: amend test to include WaitForCompletion()

This is a trivial one, so go ahead and include it.

* archiver: tests: fix test by calling NewContext()

The mutex is otherwise uninitialized, so we need to ensure that we're
actually initializing it if we plan to test it.

* archiver: tests: integrate new WaitForCompletion a little better

We can use this to wait for archives to come in, rather than spinning and
hoping with a timeout.

* archiver: tests: combine numQueued declaration with next-instruction assignment

* routers: repo: reap unused archiving flag from DownloadStatus()

This had some planned usage before, indicating whether this request
initiated the archival process or not. After several rounds of refactoring,
this use was deemed not necessary for much of anything and got boiled down
to !complete in all cases.

* services: archiver: restructure to use a channel

We now offer two forms of waiting for a request:
- WaitForCompletion: wait for completion with no timeout
- TimedWaitForCompletion: wait for completion with timeout

In both cases, we wait for the given request's cchan to close; in the latter
case, we do so with the caller-provided timeout. This completely removes the
need for busy-wait loops in Download/InitiateDownload, as it's fairly clean
to wait on a channel with timeout.

* services: archiver: use defer to unlock now that we can

This previously carried the lock into the goroutine, but an intermediate
step just added the request to archiveInProgress outside of the new
goroutine and removed the need for the goroutine to start out with it.

* Revert "archiver: tests: combine numQueued declaration with next-instruction assignment"

This reverts commit bcc5214023.

Revert "archiver: tests: integrate new WaitForCompletion a little better"

This reverts commit 9fc8bedb56.

Revert "archiver: tests: fix test by calling NewContext()"

This reverts commit 709c35685e.

Revert "archiver: tests: amend test to include WaitForCompletion()"

This reverts commit 75261f56bc.

* archiver: tests: first attempt at WaitForCompletion() tests

* archiver: tests: slight improvement, less busy-loop

Just wait for the requests to complete in order, instead of busy-waiting
with a timeout.  This is slightly less fragile.

While here, reverse the arguments of a nearby assert.Equal() so that
expected/actual are correct in any test output.

* archiver: address lint nits

* services: archiver: only close the channel once

* services: archiver: use a struct{} for the wait channel

This makes it obvious that the channel is only being used as a signal,
rather than anything useful being piped through it.

* archiver: tests: fix expectations

Move the close of the channel into doArchive() itself; notably, before these
goroutines move on to waiting on the Release cond.

The tests are adjusted to reflect that we can't WaitForCompletion() after
they've already completed, as WaitForCompletion() doesn't indicate that
they've been released from the queue yet.

* archiver: tests: set cchan to nil for comparison

* archiver: move ctx.Error's back into the route handlers

We shouldn't be setting this in a service, we should just be validating the
request that we were handed.

* services: archiver: use regex to match a hash

This makes sure we don't try and use refName as a hash when it's clearly not
one, e.g. heads/pull/foo.

* routers: repo: remove the weird /archive/status endpoint

We don't need to do this anymore, we can just continue POSTing to the
archive/* endpoint until we're told the download's complete. This avoids a
potential naming conflict, where a ref could start with "status/"

* archiver: tests: bump reasonable timeout to 15s

* archiver: tests: actually release timedReq

* archiver: tests: run through inFlight instead of manually checking

While we're here, add a test for manually re-processing an archive that's
already been complete. Re-open the channel and mark it incomplete, so that
doArchive can just mark it complete again.

* initArchiveLinks: prevent default behavior from clicking

* archiver: alias gitea's context, golang context import pending

* archiver: simplify logic, just reconstruct slices

While the previous logic was perhaps slightly more efficient, the
new variant's readability is much improved.

* archiver: don't block shutdown on waiting for archive

The technique established launches a goroutine to do the wait,
which will close a wait channel upon termination. For the timeout
case, we also send back a value indicating whether the timeout was
hit or not.

The timeouts are expected to be relatively small, but still a multi-
second delay to shutdown due to this could be unfortunate.

* archiver: simplify shutdown logic

We can just grab the shutdown channel from the graceful manager instead of
constructing a channel to halt the caller and/or pass a result back.

* Style issues

* Fix mis-merge

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: Lauris BH <lauris@nix.lv>
4 years ago
..
fixtures [RFC] Make archival asynchronous (#11296) 4 years ago
migrations Revert "Replies to outdated code comments should also be outdated (#13217)" (#13439) 4 years ago
access.go Upgrade xorm to v1.0.0 (#10646) 5 years ago
access_test.go Fix "access" fixtures and tests (#10247) 5 years ago
action.go Make dashboard newsfeed list length a configurable item (#12469) 5 years ago
action_list.go
action_test.go Add gitea-vet (#10948) 5 years ago
admin.go Add default storage configurations (#12813) 4 years ago
admin_test.go
attachment.go [Refactor] Move APIFormat functions into convert package (#12856) 4 years ago
attachment_test.go Reading pull attachments should depend on read UnitTypePullRequests (#10346) 5 years ago
avatar.go Open transaction when adding Avatar email-hash pairs to the DB (#12577) 5 years ago
branches.go [Enhancement] Allow admin to merge pr with protected file changes (#12078) 4 years ago
branches_test.go
commit_status.go [Refactor] Move APIFormat functions into convert package (#12856) 4 years ago
commit_status_test.go Fix wrong hint when status checking is running on pull request view (#9886) 5 years ago
consistency.go Add migration to set IsArchived false if it is null (#11853) 5 years ago
context.go Upgrade xorm to v1.0.2 (#11900) 5 years ago
convert.go Whilst changing the character set to utf8mb4 we should set ROW_FORMAT=dynamic too (#12804) 4 years ago
error.go Add review request api (#11355) 4 years ago
error_oauth2.go
external_login_user.go Migrate reviews when migrating repository from github (#9463) 5 years ago
fixture_generation.go Move fixture generation to contrib and add test (#10277) 5 years ago
fixture_test.go Move fixture generation to contrib and add test (#10277) 5 years ago
gpg_key.go Add configurable Trust Models (#11712) 4 years ago
gpg_key_test.go Allow addition of gpg keyring with multiple keys (#12487) 5 years ago
helper.go
helper_directory.go Re-attempt to delete temporary upload if the file is locked by another process (#12447) 5 years ago
helper_environment.go Set setting.AppURL as GITEA_ROOT_URL environment variable during pushes (#12752) 4 years ago
issue.go (Re)Load issue labels when changing them (#13007) 4 years ago
issue_assignees.go Remove Unused Functions (#10516) 5 years ago
issue_assignees_test.go
issue_comment.go Revert "Replies to outdated code comments should also be outdated (#13217)" (#13439) 4 years ago
issue_comment_list.go Fixed repo link in generated comment for cross repository dependency (#9863) 5 years ago
issue_comment_test.go
issue_dependency.go
issue_dependency_test.go
issue_label.go Save TimeStamps for Star, Label, Follow, Watch and Collaboration to Database (#13124) 4 years ago
issue_label_test.go Save TimeStamps for Star, Label, Follow, Watch and Collaboration to Database (#13124) 4 years ago
issue_list.go [BugFix] ReviewCount: GetApprovalCounts func sorted wrong (#11086) 5 years ago
issue_list_test.go Add Organization Wide Labels (#10814) 5 years ago
issue_lock.go
issue_milestone.go Add Created & Updated to Milestone (#12662) 4 years ago
issue_milestone_test.go Add name filter to API for GetMilestoneList (#12336) 5 years ago
issue_reaction.go API add/generalize pagination (#9452) 5 years ago
issue_reaction_test.go Migrate reactions when migrating repository from github (#9599) 5 years ago
issue_stopwatch.go Refactor: move Commit To APIFormat Code & Lot of StopWatch related things (#12729) 4 years ago
issue_stopwatch_test.go Add gitea-vet (#10948) 5 years ago
issue_test.go issue search on my related repositories (#9758) 5 years ago
issue_tracked_time.go Fix tracked time issues (#11349) 5 years ago
issue_tracked_time_test.go
issue_user.go
issue_user_test.go Add "Update Branch" button to Pull Requests (#9784) 5 years ago
issue_watch.go Add issue subscription check to API (#10967) 5 years ago
issue_watch_test.go API add/generalize pagination (#9452) 5 years ago
issue_xref.go
issue_xref_test.go
lfs.go LFS support to be stored on minio (#12518) 4 years ago
lfs_lock.go
list_options.go Add cron running API (#12421) 5 years ago
log.go Refactor Logger (#13294) 4 years ago
login_source.go Use ID or Where to instead directly use Get when load object from database (#11925) 5 years ago
main_test.go Add gitea-vet (#10948) 5 years ago
migrate.go [BugFix] remove nil inserts in models (#11096) 5 years ago
models.go Add postgres schema to the search_path on database connection (#12634) 4 years ago
models_test.go gitea dump: include version & Check InstallLock (#12760) 4 years ago
notification.go API: NotificationSubject show Issue/Pull State (#12901) 4 years ago
notification_test.go add request review from specific reviewers feature in pull request (#10756) 5 years ago
oauth2.go #12897 - add mastodon provider (#13293) 4 years ago
oauth2_application.go Use google/uuid to instead satori/go.uuid (#11943) 5 years ago
oauth2_application_test.go
org.go Change order of possible-owner organizations to alphabetical (#13160) 4 years ago
org_team.go Automatically remove Watches, Assignments, etc if user loses access due to being removed as collaborator or from a team (#10997) 5 years ago
org_team_test.go API add/generalize pagination (#9452) 5 years ago
org_test.go Ensure DeleteUser is not allowed to Delete Orgs and visa versa (#10134) 5 years ago
project.go Kanban board (#8346) 5 years ago
project_board.go Kanban board (#8346) 5 years ago
project_issue.go Kanban board (#8346) 5 years ago
project_test.go Kanban board (#8346) 5 years ago
pull.go [Enhancement] Allow admin to merge pr with protected file changes (#12078) 4 years ago
pull_list.go API add/generalize pagination (#9452) 5 years ago
pull_sign.go Add configurable Trust Models (#11712) 4 years ago
pull_test.go Display pull request head branch even the branch deleted or repository deleted (#10413) 5 years ago
release.go Add the tag list page to the release page (#12096) 4 years ago
repo.go updateSize when create a repo with init commit (#13441) 4 years ago
repo_activity.go Add top author stats to activity page (#9615) 5 years ago
repo_avatar.go Avatars and Repo avatars support storing in minio (#12516) 4 years ago
repo_branch.go Move newbranch to standalone package (#9627) 5 years ago
repo_collaboration.go Save TimeStamps for Star, Label, Follow, Watch and Collaboration to Database (#13124) 4 years ago
repo_collaboration_test.go API add/generalize pagination (#9452) 5 years ago
repo_generate.go Avatars and Repo avatars support storing in minio (#12516) 4 years ago
repo_generate_test.go
repo_indexer.go Index code and stats only for non-empty repositories (#10251) 5 years ago
repo_issue.go
repo_language_stats.go Increase size of the language column in language_stat (#12396) 5 years ago
repo_list.go Adopt repositories (#12920) 4 years ago
repo_list_test.go [RFC] Make archival asynchronous (#11296) 4 years ago
repo_mirror.go
repo_permission.go Fix deadlock when deleting team user (#13092) 4 years ago
repo_permission_test.go
repo_redirect.go
repo_redirect_test.go
repo_sign.go Add configurable Trust Models (#11712) 4 years ago
repo_test.go Add team support for review request (#12039) 4 years ago
repo_unit.go Kanban board (#8346) 5 years ago
repo_watch.go Save TimeStamps for Star, Label, Follow, Watch and Collaboration to Database (#13124) 4 years ago
repo_watch_test.go API add/generalize pagination (#9452) 5 years ago
review.go Add review request api (#11355) 4 years ago
review_test.go Add team support for review request (#12039) 4 years ago
ssh_key.go don't append key file if asked not to (#13368) 4 years ago
ssh_key_test.go Disable DSA ssh keys by default (#13056) 4 years ago
star.go Save TimeStamps for Star, Label, Follow, Watch and Collaboration to Database (#13124) 4 years ago
star_test.go API add/generalize pagination (#9452) 5 years ago
task.go Store task errors following migrations and display them (#13246) 4 years ago
test_fixtures.go upgrade to use testfixtures v3 (#11904) 5 years ago
token.go [API] Delete Token accept names too (#12366) 4 years ago
token_test.go [API] Delete Token accept names too (#12366) 4 years ago
topic.go Ensure topics added using the API are added to the repository (#13285) 4 years ago
topic_test.go API add/generalize pagination (#9452) 5 years ago
twofactor.go Attachments: Add extension support, allow all types for releases (#12465) 4 years ago
u2f.go
u2f_test.go Add gitea-vet (#10948) 5 years ago
unit.go Kanban board (#8346) 5 years ago
unit_tests.go Avatars and Repo avatars support storing in minio (#12516) 4 years ago
update.go Remove Unused Functions (#10516) 5 years ago
upload.go Re-attempt to delete temporary upload if the file is locked by another process (#12447) 5 years ago
user.go Avatar autogeneration fixed (#13233) 4 years ago
user_avatar.go Avatar autogeneration fixed (#13233) 4 years ago
user_follow.go Save TimeStamps for Star, Label, Follow, Watch and Collaboration to Database (#13124) 4 years ago
user_follow_test.go Add gitea-vet (#10948) 5 years ago
user_heatmap.go Add hide activity option (#11353) 5 years ago
user_heatmap_test.go Update heatmap fixtures to restore tests (#13224) 4 years ago
user_mail.go Use ID or Where to instead directly use Get when load object from database (#11925) 5 years ago
user_mail_test.go Admin page for managing user e-mail activation (#10557) 5 years ago
user_openid.go Use ID or Where to instead directly use Get when load object from database (#11925) 5 years ago
user_openid_test.go
user_test.go [Refactor] Move APIFormat functions into convert package (#12856) 4 years ago
userlist.go [Refactor] Move APIFormat functions into convert package (#12856) 4 years ago
userlist_test.go
webhook.go Use google/uuid to instead satori/go.uuid (#11943) 5 years ago
webhook_test.go Granular webhook events (#9626) 5 years ago
wiki.go Add owner_name column for table repository for maintaince reason (#9717) 5 years ago
wiki_test.go Move wiki related funtions from models to services/wiki (#9355) 5 years ago