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

Feature: cache timestamp.json to accelerate #1212

Merged
merged 8 commits into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions cmd/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ func doPublish(
flagSet set.StringSet,
) error {
env := environment.GlobalEnv()
env.V1Repository().PurgeTimestamp()
m, err := env.V1Repository().FetchComponentManifest(component, true)
if err != nil {
if perrs.Cause(err) == repository.ErrUnknownComponent {
Expand Down
21 changes: 19 additions & 2 deletions pkg/repository/v1_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ var ErrUnknownVersion = errors.New("unknown version")
// V1Repository represents a remote repository viewed with the v1 manifest design.
type V1Repository struct {
Options
mirror Mirror
local v1manifest.LocalManifests
mirror Mirror
local v1manifest.LocalManifests
timestamp *v1manifest.Manifest
}

// ComponentSpec describes a component a user would like to have or use.
Expand Down Expand Up @@ -297,6 +298,11 @@ func FnameWithVersion(fname string, version uint) string {
}

func (r *V1Repository) updateLocalRoot() error {
// There is no need to update root.json if other manifest not changed
if r.timestamp != nil {
return nil
}

defer func(start time.Time) {
verbose.Log("Update local root finished in %s", time.Since(start))
}(time.Now())
Expand Down Expand Up @@ -493,12 +499,23 @@ func (r *V1Repository) DownloadComponent(item *v1manifest.VersionItem, target st
return nil
}

// PurgeTimestamp remove timestamp cache from repository
func (r *V1Repository) PurgeTimestamp() {
r.timestamp = nil
}

// FetchTimestamp downloads the timestamp file, validates it, and checks if the snapshot hash in it
// has the same value of our local one. (not hashing the snapshot file itself)
// Return weather the manifest is changed compared to the one in local ts and the FileHash of snapshot.
func (r *V1Repository) fetchTimestamp() (changed bool, manifest *v1manifest.Manifest, err error) {
// check cache first
if r.timestamp != nil {
return false, r.timestamp, nil
}

defer func(start time.Time) {
verbose.Log("Fetch timestamp finished in %s", time.Since(start))
r.timestamp = manifest
}(time.Now())

var ts v1manifest.Timestamp
Expand Down