From 8417915c6d1a4bc96a596e22595d0a624584f9ae Mon Sep 17 00:00:00 2001 From: Jonny Stoten Date: Mon, 12 Sep 2022 10:48:55 +0100 Subject: [PATCH] Remove unbounded cache Signed-off-by: Jonny Stoten --- server/storage/tuf_store.go | 50 +++++--------------------------- server/storage/tuf_store_test.go | 16 ---------- 2 files changed, 7 insertions(+), 59 deletions(-) diff --git a/server/storage/tuf_store.go b/server/storage/tuf_store.go index 4f1fa7d2f..e03ee3f47 100644 --- a/server/storage/tuf_store.go +++ b/server/storage/tuf_store.go @@ -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) @@ -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 @@ -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 } diff --git a/server/storage/tuf_store_test.go b/server/storage/tuf_store_test.go index f97ca7bfc..3e7735ff1 100644 --- a/server/storage/tuf_store_test.go +++ b/server/storage/tuf_store_test.go @@ -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) {