Skip to content

Commit

Permalink
refactor(repository): refactored v1 encryption overhead to be a funct…
Browse files Browse the repository at this point in the history
…ion that's only invoked when actual V1 index is opened (#2300)
  • Loading branch information
jkowalski committed Aug 10, 2022
1 parent 37830e5 commit ae833bf
Show file tree
Hide file tree
Showing 12 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions repo/content/committed_content_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type committedContentIndex struct {
// +checklocks:mu
merged index.Merged

v1PerContentOverhead uint32
v1PerContentOverhead func() int
formatProvider format.Provider

// fetchOne loads one index blob
Expand Down Expand Up @@ -354,7 +354,7 @@ func (c *committedContentIndex) missingIndexBlobs(ctx context.Context, blobs []b
}

func newCommittedContentIndex(caching *CachingOptions,
v1PerContentOverhead uint32,
v1PerContentOverhead func() int,
formatProvider format.Provider,
fetchOne func(ctx context.Context, blobID blob.ID, output *gather.WriteBuffer) error,
log logging.Logger,
Expand Down
4 changes: 2 additions & 2 deletions repo/content/committed_content_index_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ func TestCommittedContentIndexCache_Disk(t *testing.T) {

ta := faketime.NewClockTimeWithOffset(0)

testCache(t, &diskCommittedContentIndexCache{testutil.TempDirectory(t), ta.NowFunc(), 3, testlogging.Printf(t.Logf, ""), DefaultIndexCacheSweepAge}, ta)
testCache(t, &diskCommittedContentIndexCache{testutil.TempDirectory(t), ta.NowFunc(), func() int { return 3 }, testlogging.Printf(t.Logf, ""), DefaultIndexCacheSweepAge}, ta)
}

func TestCommittedContentIndexCache_Memory(t *testing.T) {
t.Parallel()

testCache(t, &memoryCommittedContentIndexCache{
contents: map[blob.ID]index.Index{},
v1PerContentOverhead: 3,
v1PerContentOverhead: func() int { return 3 },
}, nil)
}

Expand Down
2 changes: 1 addition & 1 deletion repo/content/committed_content_index_disk_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
type diskCommittedContentIndexCache struct {
dirname string
timeNow func() time.Time
v1PerContentOverhead uint32
v1PerContentOverhead func() int
log logging.Logger
minSweepAge time.Duration
}
Expand Down
2 changes: 1 addition & 1 deletion repo/content/committed_content_index_mem_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type memoryCommittedContentIndexCache struct {
// +checklocks:mu
contents map[blob.ID]index.Index

v1PerContentOverhead uint32 // +checklocksignore
v1PerContentOverhead func() int // +checklocksignore
}

func (m *memoryCommittedContentIndexCache) hasIndexBlobID(ctx context.Context, indexBlobID blob.ID) (bool, error) {
Expand Down
2 changes: 1 addition & 1 deletion repo/content/committed_read_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ func (sm *SharedManager) setupReadManagerCaches(ctx context.Context, caching *Ca
sm.metadataCache = metadataCache
sm.indexBlobCache = indexBlobCache
sm.committedContents = newCommittedContentIndex(caching,
uint32(sm.format.Encryptor().Overhead()),
sm.format.Encryptor().Overhead,
sm.format,
sm.enc.getEncryptedBlob,
sm.namedLogger("committed-content-index"),
Expand Down
2 changes: 1 addition & 1 deletion repo/content/content_index_recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (bm *WriteManager) RecoverIndexFromPackBlob(ctx context.Context, packFile b
return nil, err
}

ndx, err := index.Open(localIndexBytes.Bytes().ToByteSlice(), nil, uint32(bm.format.Encryptor().Overhead()))
ndx, err := index.Open(localIndexBytes.Bytes().ToByteSlice(), nil, bm.format.Encryptor().Overhead)
if err != nil {
return nil, errors.Errorf("unable to open index in file %v", packFile)
}
Expand Down
2 changes: 1 addition & 1 deletion repo/content/content_manager_indexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func ParseIndexBlob(ctx context.Context, blobID blob.ID, encrypted gather.Bytes,
return nil, errors.Wrap(err, "unable to decrypt index blob")
}

ndx, err := index.Open(data.Bytes().ToByteSlice(), nil, uint32(crypter.Encryptor().Overhead()))
ndx, err := index.Open(data.Bytes().ToByteSlice(), nil, crypter.Encryptor().Overhead)
if err != nil {
return nil, errors.Wrapf(err, "unable to open index blob")
}
Expand Down
4 changes: 2 additions & 2 deletions repo/content/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ type Index interface {
}

// Open reads an Index from a given reader. The caller must call Close() when the index is no longer used.
func Open(data []byte, closer func() error, v1PerContentOverhead uint32) (Index, error) {
func Open(data []byte, closer func() error, v1PerContentOverhead func() int) (Index, error) {
h, err := v1ReadHeader(data)
if err != nil {
return nil, errors.Wrap(err, "invalid header")
}

switch h.version {
case Version1:
return openV1PackIndex(h, data, closer, v1PerContentOverhead)
return openV1PackIndex(h, data, closer, uint32(v1PerContentOverhead()))

case Version2:
return openV2PackIndex(data, closer)
Expand Down
2 changes: 1 addition & 1 deletion repo/content/index/merged_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,5 +249,5 @@ func indexWithItems(items ...Info) (Index, error) {
return nil, errors.Wrap(err, "build error")
}

return Open(buf.Bytes(), nil, fakeEncryptionOverhead)
return Open(buf.Bytes(), nil, func() int { return fakeEncryptionOverhead })
}
6 changes: 3 additions & 3 deletions repo/content/index/packindex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func testPackIndex(t *testing.T, version int) {
fuzzTestIndexOpen(data1)
})

ndx, err := Open(data1, nil, fakeEncryptionOverhead)
ndx, err := Open(data1, nil, func() int { return fakeEncryptionOverhead })
if err != nil {
t.Fatalf("can't open index: %v", err)
}
Expand Down Expand Up @@ -281,7 +281,7 @@ func TestPackIndexPerContentLimits(t *testing.T) {
if tc.errMsg == "" {
require.NoError(t, b.buildV2(&result))

pi, err := Open(result.Bytes(), nil, fakeEncryptionOverhead)
pi, err := Open(result.Bytes(), nil, func() int { return fakeEncryptionOverhead })
require.NoError(t, err)

got, err := pi.GetInfo(cid)
Expand Down Expand Up @@ -399,7 +399,7 @@ func fuzzTestIndexOpen(originalData []byte) {
rnd := rand.New(rand.NewSource(12345))

fuzzTest(rnd, originalData, 50000, func(d []byte) {
ndx, err := Open(d, nil, 0)
ndx, err := Open(d, nil, func() int { return 0 })
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion repo/content/index_blob_manager_v0.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ func addIndexBlobsToBuilder(ctx context.Context, enc *encryptedBlobMgr, bld inde
return errors.Wrapf(err, "error getting index %q", indexBlobID)
}

ndx, err := index.Open(data.ToByteSlice(), nil, uint32(enc.crypter.Encryptor().Overhead()))
ndx, err := index.Open(data.ToByteSlice(), nil, enc.crypter.Encryptor().Overhead)
if err != nil {
return errors.Wrapf(err, "unable to open index blob %q", indexBlobID)
}
Expand Down
2 changes: 1 addition & 1 deletion repo/ecc/ecc.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,5 @@ func (e encryptorWrapper) Decrypt(cipherText gather.Bytes, contentID []byte, out
}

func (e encryptorWrapper) Overhead() int {
return e.impl.Overhead() + e.next.Overhead()
panic("Overhead() should not be called")
}

0 comments on commit ae833bf

Please sign in to comment.