Skip to content
This repository has been archived by the owner on May 26, 2023. It is now read-only.

Commit

Permalink
add an identifier to caches (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrikKahl authored Feb 11, 2021
1 parent d2de7db commit 8ffbdc9
Show file tree
Hide file tree
Showing 25 changed files with 994 additions and 32 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/go-logr/logr v0.3.0
github.com/go-logr/zapr v0.3.0
github.com/golang/mock v1.4.4
github.com/google/uuid v1.1.1
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/mandelsoft/vfs v0.0.0-20201002134249-3c471f64a4d1
github.com/onsi/ginkgo v1.14.0
Expand Down
13 changes: 5 additions & 8 deletions ociclient/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ type layeredCache struct {

baseFs *FileSystem
overlayFs *FileSystem

basePath string
}

// NewCache creates a new cache with the given options.
Expand Down Expand Up @@ -61,21 +59,20 @@ func NewCache(log logr.Logger, options ...Option) (*layeredCache, error) {

//initialize metrics
baseCFs.WithMetrics(
metrics.CachedItems.WithLabelValues(opts.BasePath),
metrics.CacheDiskUsage.WithLabelValues(opts.BasePath),
metrics.CacheHitsDisk.WithLabelValues(opts.BasePath))
metrics.CachedItems.WithLabelValues(opts.UID),
metrics.CacheDiskUsage.WithLabelValues(opts.UID),
metrics.CacheHitsDisk.WithLabelValues(opts.UID))
if opts.InMemoryOverlay {
overlayCFs.WithMetrics(nil,
metrics.CacheMemoryUsage.WithLabelValues(opts.BasePath),
metrics.CacheHitsMemory.WithLabelValues(opts.BasePath))
metrics.CacheMemoryUsage.WithLabelValues(opts.UID),
metrics.CacheHitsMemory.WithLabelValues(opts.UID))
}

return &layeredCache{
log: log,
mux: sync.RWMutex{},
baseFs: baseCFs,
overlayFs: overlayCFs,
basePath: opts.BasePath,
}, nil
}

Expand Down
39 changes: 20 additions & 19 deletions ociclient/cache/cache_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ var _ = Describe("Cache", func() {

Context("metrics", func() {
It("should read data from the in memory cache", func() {
uid := "unit-test"
dir, err := ioutil.TempDir(os.TempDir(), "test-")
Expect(err).ToNot(HaveOccurred())
defer func() {
Expand All @@ -64,7 +65,7 @@ var _ = Describe("Cache", func() {
metrics.CacheDiskUsage.Reset()
metrics.CacheMemoryUsage.Reset()

c, err := NewCache(testlog.NullLogger{}, WithBasePath(dir), WithInMemoryOverlay(true))
c, err := NewCache(testlog.NullLogger{}, WithBasePath(dir), WithInMemoryOverlay(true), WithUID(uid))
Expect(err).ToNot(HaveOccurred())
defer c.Close()

Expand All @@ -74,27 +75,27 @@ var _ = Describe("Cache", func() {
expected := `
# HELP ociclient_cache_items_total Total number of items currently cached by instance.
# TYPE ociclient_cache_items_total gauge
ociclient_cache_items_total{path="%s"} 1
ociclient_cache_items_total{id="%s"} 1
`
Expect(testutil.CollectAndCompare(metrics.CachedItems, strings.NewReader(fmt.Sprintf(expected, dir)))).To(Succeed())
Expect(testutil.CollectAndCompare(metrics.CachedItems, strings.NewReader(fmt.Sprintf(expected, uid)))).To(Succeed())
expected = `
# HELP ociclient_cache_disk_hits_total Total number of hits for items cached on disk by an instance.
# TYPE ociclient_cache_disk_hits_total counter
ociclient_cache_disk_hits_total{path="%s"} 0
ociclient_cache_disk_hits_total{id="%s"} 0
`
Expect(testutil.CollectAndCompare(metrics.CacheHitsDisk, strings.NewReader(fmt.Sprintf(expected, dir)))).To(Succeed())
Expect(testutil.CollectAndCompare(metrics.CacheHitsDisk, strings.NewReader(fmt.Sprintf(expected, uid)))).To(Succeed())
expected = `
# HELP ociclient_cache_memory_hits_total Total number of hits for items cached in memory by an instance.
# TYPE ociclient_cache_memory_hits_total counter
ociclient_cache_memory_hits_total{path="%s"} 0
ociclient_cache_memory_hits_total{id="%s"} 0
`
Expect(testutil.CollectAndCompare(metrics.CacheHitsMemory, strings.NewReader(fmt.Sprintf(expected, dir)))).To(Succeed())
Expect(testutil.CollectAndCompare(metrics.CacheHitsMemory, strings.NewReader(fmt.Sprintf(expected, uid)))).To(Succeed())
expected = `
# HELP ociclient_cache_memory_usage_bytes Bytes in memory currently used by cache instance.
# TYPE ociclient_cache_memory_usage_bytes gauge
ociclient_cache_memory_usage_bytes{path="%s"} 0
ociclient_cache_memory_usage_bytes{id="%s"} 0
`
Expect(testutil.CollectAndCompare(metrics.CacheMemoryUsage, strings.NewReader(fmt.Sprintf(expected, dir)))).To(Succeed())
Expect(testutil.CollectAndCompare(metrics.CacheMemoryUsage, strings.NewReader(fmt.Sprintf(expected, uid)))).To(Succeed())

r, err := c.Get(desc)
Expect(err).ToNot(HaveOccurred())
Expand All @@ -108,33 +109,33 @@ var _ = Describe("Cache", func() {
expected = `
# HELP ociclient_cache_items_total Total number of items currently cached by instance.
# TYPE ociclient_cache_items_total gauge
ociclient_cache_items_total{path="%s"} 1
ociclient_cache_items_total{id="%s"} 1
`
Expect(testutil.CollectAndCompare(metrics.CachedItems, strings.NewReader(fmt.Sprintf(expected, dir)))).To(Succeed())
Expect(testutil.CollectAndCompare(metrics.CachedItems, strings.NewReader(fmt.Sprintf(expected, uid)))).To(Succeed())
expected = `
# HELP ociclient_cache_disk_usage_bytes Bytes on disk currently used by cache instance.
# TYPE ociclient_cache_disk_usage_bytes gauge
ociclient_cache_disk_usage_bytes{path="%s"} 10
ociclient_cache_disk_usage_bytes{id="%s"} 10
`
Expect(testutil.CollectAndCompare(metrics.CacheDiskUsage, strings.NewReader(fmt.Sprintf(expected, dir)))).To(Succeed())
Expect(testutil.CollectAndCompare(metrics.CacheDiskUsage, strings.NewReader(fmt.Sprintf(expected, uid)))).To(Succeed())
expected = `
# HELP ociclient_cache_disk_hits_total Total number of hits for items cached on disk by an instance.
# TYPE ociclient_cache_disk_hits_total counter
ociclient_cache_disk_hits_total{path="%s"} 1
ociclient_cache_disk_hits_total{id="%s"} 1
`
Expect(testutil.CollectAndCompare(metrics.CacheHitsDisk, strings.NewReader(fmt.Sprintf(expected, dir)))).To(Succeed())
Expect(testutil.CollectAndCompare(metrics.CacheHitsDisk, strings.NewReader(fmt.Sprintf(expected, uid)))).To(Succeed())
expected = `
# HELP ociclient_cache_memory_hits_total Total number of hits for items cached in memory by an instance.
# TYPE ociclient_cache_memory_hits_total counter
ociclient_cache_memory_hits_total{path="%s"} 1
ociclient_cache_memory_hits_total{id="%s"} 1
`
Expect(testutil.CollectAndCompare(metrics.CacheHitsMemory, strings.NewReader(fmt.Sprintf(expected, dir)))).To(Succeed())
Expect(testutil.CollectAndCompare(metrics.CacheHitsMemory, strings.NewReader(fmt.Sprintf(expected, uid)))).To(Succeed())
expected = `
# HELP ociclient_cache_memory_usage_bytes Bytes in memory currently used by cache instance.
# TYPE ociclient_cache_memory_usage_bytes gauge
ociclient_cache_memory_usage_bytes{path="%s"} 10
ociclient_cache_memory_usage_bytes{id="%s"} 10
`
Expect(testutil.CollectAndCompare(metrics.CacheMemoryUsage, strings.NewReader(fmt.Sprintf(expected, dir)))).To(Succeed())
Expect(testutil.CollectAndCompare(metrics.CacheMemoryUsage, strings.NewReader(fmt.Sprintf(expected, uid)))).To(Succeed())
})
})
})
Expand Down
16 changes: 16 additions & 0 deletions ociclient/cache/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"io"

ocispecv1 "github.com/opencontainers/image-spec/specs-go/v1"

"github.com/google/uuid"
)

var (
Expand Down Expand Up @@ -56,6 +58,9 @@ type Options struct {

// BaseGCConfig defines the garbage collection configuration for the in base cache.
BaseGCConfig GarbageCollectionConfiguration

// UID is the identity of a cache, if not specified a UID will be generated
UID string
}

// Option is the interface to specify different cache options
Expand All @@ -77,6 +82,10 @@ func (o *Options) ApplyDefaults() {
if o.InMemoryOverlay && len(o.InMemoryGCConfig.Size) == 0 {
o.InMemoryGCConfig.Size = "200Mi"
}

if len(o.UID) == 0 {
o.UID = uuid.New().String()
}
}

// WithInMemoryOverlay is the options to specify the usage of a in memory overlayFs
Expand Down Expand Up @@ -135,3 +144,10 @@ func (p WithInMemoryGCConfig) ApplyOption(options *Options) {
cfg := GarbageCollectionConfiguration(p)
cfg.Merge(&options.InMemoryGCConfig)
}

// WithUID is the option to give a cache an identity
type WithUID string

func (p WithUID) ApplyOption(options *Options) {
options.UID = string(p)
}
10 changes: 5 additions & 5 deletions ociclient/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
Name: "memory_usage_bytes",
Help: "Bytes in memory currently used by cache instance.",
},
[]string{"path"},
[]string{"id"},
)

// CacheDiskUsage discloses disk used by caches
Expand All @@ -31,7 +31,7 @@ var (
Name: "disk_usage_bytes",
Help: "Bytes on disk currently used by cache instance.",
},
[]string{"path"},
[]string{"id"},
)

// CachedItems discloses the number of items stored by caches
Expand All @@ -42,7 +42,7 @@ var (
Name: "items_total",
Help: "Total number of items currently cached by instance.",
},
[]string{"path"},
[]string{"id"},
)

// CacheHitsDisk discloses the number of hits for items cached on disk
Expand All @@ -53,7 +53,7 @@ var (
Name: "disk_hits_total",
Help: "Total number of hits for items cached on disk by an instance.",
},
[]string{"path"},
[]string{"id"},
)

// CacheHitsMemory discloses the number of hits for items cached in memory
Expand All @@ -64,7 +64,7 @@ var (
Name: "memory_hits_total",
Help: "Total number of hits for items cached in memory by an instance.",
},
[]string{"path"},
[]string{"id"},
)
)

Expand Down
9 changes: 9 additions & 0 deletions vendor/github.com/google/uuid/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions vendor/github.com/google/uuid/CONTRIBUTING.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions vendor/github.com/google/uuid/CONTRIBUTORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/github.com/google/uuid/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions vendor/github.com/google/uuid/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 80 additions & 0 deletions vendor/github.com/google/uuid/dce.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions vendor/github.com/google/uuid/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8ffbdc9

Please sign in to comment.