Skip to content

Commit

Permalink
refactor: move cache to pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
avtakkar committed Apr 12, 2024
1 parent dd3559f commit 9fe0d76
Show file tree
Hide file tree
Showing 16 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion build/package/peerd-helm/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ version: 0.0.3-alpha
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "0.0.6-alpha"
appVersion: "0.0.7-alpha"
2 changes: 1 addition & 1 deletion internal/files/store/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"testing"

"github.com/azure/peerd/internal/files"
"github.com/azure/peerd/internal/files/cache"
remotetests "github.com/azure/peerd/internal/remote/tests"
"github.com/azure/peerd/pkg/cache"
"github.com/azure/peerd/pkg/discovery/routing/tests"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/files/store/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"os"
"testing"

"github.com/azure/peerd/internal/files/cache"
"github.com/azure/peerd/pkg/cache"
"github.com/azure/peerd/pkg/metrics"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/files/store/mockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package store
import (
"context"

"github.com/azure/peerd/internal/files/cache"
"github.com/azure/peerd/pkg/cache"
"github.com/azure/peerd/pkg/discovery/routing"
)

Expand Down
4 changes: 2 additions & 2 deletions internal/files/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (

p2pcontext "github.com/azure/peerd/internal/context"
"github.com/azure/peerd/internal/files"
"github.com/azure/peerd/internal/files/cache"
"github.com/azure/peerd/internal/remote"
"github.com/azure/peerd/pkg/cache"
"github.com/azure/peerd/pkg/discovery/routing"
"github.com/azure/peerd/pkg/metrics"
"github.com/azure/peerd/pkg/urlparser"
Expand All @@ -25,7 +25,7 @@ import (
func NewFilesStore(ctx context.Context, r routing.Router) (FilesStore, error) {
fs := &store{
metricsRecorder: metrics.FromContext(ctx),
cache: cache.New(ctx),
cache: cache.New(ctx, int64(files.CacheBlockSize)),
prefetchChan: make(chan prefetchableSegment, PrefetchWorkers),
prefetchable: PrefetchWorkers > 0,
router: r,
Expand Down
2 changes: 1 addition & 1 deletion internal/handlers/files/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"os"
"testing"

"github.com/azure/peerd/internal/files/cache"
"github.com/azure/peerd/pkg/cache"
)

func TestMain(m *testing.M) {
Expand Down
2 changes: 1 addition & 1 deletion internal/remote/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Reader interface {
Log() *zerolog.Logger
}

// Error describes an error that occured during a remote operation.
// Error describes an error that occurred during a remote operation.
type Error struct {
*http.Response
error
Expand Down
6 changes: 3 additions & 3 deletions internal/files/cache/cache.go → pkg/cache/filecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"sync/atomic"
"time"

"github.com/azure/peerd/internal/files"
"github.com/dgraph-io/ristretto"
"github.com/rs/zerolog"
)
Expand Down Expand Up @@ -149,7 +148,8 @@ func waitForSet() {
}

// New creates a new cache of files.
func New(ctx context.Context) Cache {
// cacheBlockSize is the fixed size of the cache block in bytes, and is used to evaluate the cost of each item in the cache.
func New(ctx context.Context, cacheBlockSize int64) Cache {
log := zerolog.Ctx(ctx).With().Str("component", "cache").Logger()

atomic.StoreInt32(&fdCnt, 0)
Expand All @@ -176,7 +176,7 @@ func New(ctx context.Context) Cache {
},

Cost: func(val interface{}) int64 {
return int64(files.CacheBlockSize)
return cacheBlockSize
},
}); err != nil {
// This will call os.Exit(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ import (
"golang.org/x/sync/errgroup"
)

var (
cacheBlockSize = int64(1 * 1024 * 1024)
)

func TestGetKey(t *testing.T) {
name := newRandomStringN(10)
offset := int64(100)
c := New(context.Background())
c := New(context.Background(), cacheBlockSize)
got := c.(*fileCache).getKey(name, offset)
want := fmt.Sprintf("%v/%v/%v", Path, name, offset)
if got != want {
Expand All @@ -30,7 +34,7 @@ func TestGetKey(t *testing.T) {
}

func TestExists(t *testing.T) {
c := New(context.Background())
c := New(context.Background(), cacheBlockSize)

filesThatExist := []string{}
for i := 0; i < 5; i++ {
Expand Down Expand Up @@ -113,7 +117,7 @@ func TestExists(t *testing.T) {
}

func TestPutAndGetSize(t *testing.T) {
c := New(context.Background())
c := New(context.Background(), cacheBlockSize)
var eg errgroup.Group

for i := 0; i < 1000; i++ {
Expand Down Expand Up @@ -150,7 +154,7 @@ func TestPutAndGetSize(t *testing.T) {
func TestGetOrCreate(t *testing.T) {
zerolog.TimeFieldFormat = time.RFC3339
//c := New(zerolog.New(os.Stdout).With().Timestamp().Logger().WithContext(context.Background()))
c := New(context.Background())
c := New(context.Background(), cacheBlockSize)
var eg errgroup.Group

fileNames := new(sync.Map)
Expand Down
8 changes: 4 additions & 4 deletions internal/files/cache/interface.go → pkg/cache/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License.
package cache

// Cache describes the cache of files.
// Cache describes a cache of files.
type Cache interface {
// Size gets the size of the file.
Size(path string) (int64, bool)
Expand All @@ -18,12 +18,12 @@ type Cache interface {
}

var (
// FilesCacheMaxCost is the capacity of the files cache in any unit.
// FilesCacheMaxCost is the capacity of the files cache.
FilesCacheMaxCost int64 = 4 * 1024 * 1024 * 1024 // 4 Gib

// MemoryCacheMaxCost is the capacity of the memory cache in any unit.
// MemoryCacheMaxCost is the capacity of the memory cache.
MemoryCacheMaxCost int64 = 1 * 1024 * 1024 * 1024 // 1 Gib

// Path is the path to the cache directory.
Path string = "/tmp/distribution/p2p/cache"
Path string = "/tmp/distribution/peerd/cache"
)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion pkg/peernet/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type network struct {
var _ Network = &network{}

// DefaultTLSConfig creates a TLS config to use for this server.
// This config does not require client certificate verification and is resuable.
// This config does not require client certificate verification and is reusable.
func (n *network) DefaultTLSConfig() *tls.Config {
return n.defaultTLSConfig
}
Expand Down

0 comments on commit 9fe0d76

Please sign in to comment.