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 all 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
28 changes: 28 additions & 0 deletions pkg/repository/v1_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func TestCheckTimestamp(t *testing.T) {
expiredTimestamp := timestampManifest()
expiredTimestamp.Expires = "2000-05-12T04:51:08Z"
mirror.Resources[v1manifest.ManifestURLTimestamp] = serialize(t, expiredTimestamp)
repo.PurgeTimestamp()
_, _, err = repo.fetchTimestamp()
assert.NotNil(t, err)

Expand All @@ -154,6 +155,30 @@ func TestCheckTimestamp(t *testing.T) {
// TODO test that a bad signature causes an error
}

func TestCacheTimestamp(t *testing.T) {
mirror := MockMirror{
Resources: map[string]string{},
}
local := v1manifest.NewMockManifests()
privk := setNewRoot(t, local)
repo := NewV1Repo(&mirror, Options{}, local)

repoTimestamp := timestampManifest()
mirror.Resources[v1manifest.ManifestURLTimestamp] = serialize(t, repoTimestamp, privk)
changed, _, err := repo.fetchTimestamp()
assert.Nil(t, err)
assert.True(t, changed)

delete(mirror.Resources, v1manifest.ManifestURLTimestamp)
changed, _, err = repo.fetchTimestamp()
assert.Nil(t, err)
assert.False(t, changed)

repo.PurgeTimestamp()
_, _, err = repo.fetchTimestamp()
assert.NotNil(t, err)
}

func TestUpdateLocalSnapshot(t *testing.T) {
mirror := MockMirror{
Resources: map[string]string{},
Expand Down Expand Up @@ -390,6 +415,7 @@ func TestEnsureManifests(t *testing.T) {
mirror.Resources[v1manifest.ManifestURLTimestamp] = serialize(t, ts, priv2)
local.Saved = []string{}

repo.PurgeTimestamp()
err = repo.ensureManifests()
assert.Nil(t, err)
assert.Contains(t, local.Saved, v1manifest.ManifestFilenameRoot)
Expand All @@ -403,6 +429,7 @@ func TestEnsureManifests(t *testing.T) {
mirror.Resources[v1manifest.ManifestURLSnapshot] = snapStr
mirror.Resources[v1manifest.ManifestURLTimestamp] = serialize(t, ts, priv)

repo.PurgeTimestamp()
err = repo.ensureManifests()
assert.NotNil(t, err)
}
Expand Down Expand Up @@ -522,6 +549,7 @@ func TestUpdateComponents(t *testing.T) {
ts.Version++
mirror.Resources[v1manifest.ManifestURLSnapshot] = snapStr
mirror.Resources[v1manifest.ManifestURLTimestamp] = serialize(t, ts, priv)
repo.PurgeTimestamp()
err = repo.UpdateComponents([]ComponentSpec{{
ID: "foo",
TargetDir: "/tmp/mock-mock",
Expand Down