Skip to content

Commit

Permalink
Remove unbounded cache
Browse files Browse the repository at this point in the history
Signed-off-by: Jonny Stoten <[email protected]>
  • Loading branch information
jonnystoten committed Oct 11, 2022
1 parent 3d93cca commit 8417915
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 59 deletions.
50 changes: 7 additions & 43 deletions server/storage/tuf_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,15 @@ import (
// by always starting from a current timestamp and then looking up other data by hash
type TUFMetaStorage struct {
MetaStore
// cached metadata by checksum
cachedMeta map[string]*storedMeta
}

// NewTUFMetaStorage instantiates a TUFMetaStorage instance
func NewTUFMetaStorage(m MetaStore) *TUFMetaStorage {
return &TUFMetaStorage{
MetaStore: m,
cachedMeta: make(map[string]*storedMeta),
MetaStore: m,
}
}

type storedMeta struct {
data []byte
createupdate *time.Time
}

// GetCurrent gets a specific TUF record, by walking from the current Timestamp to other metadata by checksum
func (tms TUFMetaStorage) GetCurrent(gun data.GUN, tufRole data.RoleName) (*time.Time, []byte, error) {
timestampTime, timestampJSON, err := tms.MetaStore.GetCurrent(gun, data.CanonicalTimestampRole)
Expand All @@ -58,21 +50,12 @@ func (tms TUFMetaStorage) GetCurrent(gun data.GUN, tufRole data.RoleName) (*time
}
snapshotSHA256Hex := hex.EncodeToString(snapshotSHA256Bytes[:])

// Check the cache if we have our snapshot data
var snapshotTime *time.Time
var snapshotJSON []byte
if cachedSnapshotData, ok := tms.cachedMeta[snapshotSHA256Hex]; ok {
snapshotTime = cachedSnapshotData.createupdate
snapshotJSON = cachedSnapshotData.data
} else {
// Get the snapshot from the underlying store by checksum if it isn't cached yet
snapshotTime, snapshotJSON, err = tms.GetChecksum(gun, data.CanonicalSnapshotRole, snapshotSHA256Hex)
if err != nil {
return nil, nil, err
}
// cache for subsequent lookups
tms.cachedMeta[snapshotSHA256Hex] = &storedMeta{data: snapshotJSON, createupdate: snapshotTime}
// Get the snapshot from the underlying store by checksum
snapshotTime, snapshotJSON, err := tms.GetChecksum(gun, data.CanonicalSnapshotRole, snapshotSHA256Hex)
if err != nil {
return nil, nil, err
}

// If we wanted data for the snapshot role, we're done here
if tufRole == data.CanonicalSnapshotRole {
return snapshotTime, snapshotJSON, nil
Expand All @@ -92,31 +75,12 @@ func (tms TUFMetaStorage) GetCurrent(gun data.GUN, tufRole data.RoleName) (*time
return nil, nil, fmt.Errorf("could not retrieve latest %s sha256", tufRole)
}
roleSHA256Hex := hex.EncodeToString(roleSHA256Bytes[:])
// check if we can retrieve this data from cache
if cachedRoleData, ok := tms.cachedMeta[roleSHA256Hex]; ok {
return cachedRoleData.createupdate, cachedRoleData.data, nil
}

roleTime, roleJSON, err := tms.MetaStore.GetChecksum(gun, tufRole, roleSHA256Hex)
roleTime, roleJSON, err := tms.GetChecksum(gun, tufRole, roleSHA256Hex)
if err != nil {
return nil, nil, err
}
// cache for subsequent lookups
tms.cachedMeta[roleSHA256Hex] = &storedMeta{data: roleJSON, createupdate: roleTime}
return roleTime, roleJSON, nil
}

// GetChecksum gets a specific TUF record by checksum, also checking the internal cache
func (tms TUFMetaStorage) GetChecksum(gun data.GUN, tufRole data.RoleName, checksum string) (*time.Time, []byte, error) {
if cachedRoleData, ok := tms.cachedMeta[checksum]; ok {
return cachedRoleData.createupdate, cachedRoleData.data, nil
}
roleTime, roleJSON, err := tms.MetaStore.GetChecksum(gun, tufRole, checksum)
if err != nil {
return nil, nil, err
}
// cache for subsequent lookups
tms.cachedMeta[checksum] = &storedMeta{data: roleJSON, createupdate: roleTime}
return roleTime, roleJSON, nil
}

Expand Down
16 changes: 0 additions & 16 deletions server/storage/tuf_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,6 @@ func testTUFMetaStoreGetCurrent(t *testing.T, s MetaStore) {
require.NoError(t, s.UpdateMany(gun, updates))
_, _, err = s.GetCurrent(gun, data.CanonicalSnapshotRole)
require.IsType(t, ErrNotFound{}, err)

// GetCurrent on all roles should still succeed - snapshot lookup because of caching,
// and targets and root because the snapshot is cached
for _, tufobj := range tufMetaByRole {
ConsistentGetCurrentFoundTest(t, tufDBStore, tufobj)
}

// add another orphaned root, but ensure that we still get the previous root
// since the new root isn't in a timestamp/snapshot chain
orphanedRootTUF := SampleCustomTUFObj(gun, data.CanonicalRootRole, 3, []byte("orphanedRoot"))
require.NoError(t, s.UpdateCurrent(gun, MakeUpdate(orphanedRootTUF)), "unable to create orphaned root in store")

// a GetCurrent for this gun and root gets us the previous root, which is linked in timestamp and snapshot
ConsistentGetCurrentFoundTest(t, tufDBStore, tufMetaByRole[data.CanonicalRootRole.String()])
// the orphaned root fails on a GetCurrent even though it's in the underlying store
ConsistentTSAndSnapGetDifferentCurrentTest(t, tufDBStore, orphanedRootTUF)
}

func ConsistentGetCurrentFoundTest(t *testing.T, s *TUFMetaStorage, rec StoredTUFMeta) {
Expand Down

0 comments on commit 8417915

Please sign in to comment.