Skip to content

Commit

Permalink
supports backend storage in in-memory storage
Browse files Browse the repository at this point in the history
  • Loading branch information
monnand committed Sep 5, 2014
1 parent 9c99d76 commit 60a1b6a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 39 deletions.
15 changes: 14 additions & 1 deletion storage/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ type InMemoryStorage struct {
containerStorageMap map[string]*containerStorage
maxNumSamples int
maxNumStats int
backend storage.StorageDriver
}

func (self *InMemoryStorage) AddStats(ref info.ContainerReference, stats *info.ContainerStats) error {
Expand All @@ -165,6 +166,13 @@ func (self *InMemoryStorage) AddStats(ref info.ContainerReference, stats *info.C
self.containerStorageMap[ref.Name] = cstore
}
}()

if self.backend != nil {
// TODO(monnand): To deal with long delay write operations, we
// may want to start a pool of goroutines to do write
// operations.
self.backend.AddStats(ref, stats)
}
return cstore.AddStats(stats)
}

Expand Down Expand Up @@ -231,11 +239,16 @@ func (self *InMemoryStorage) Close() error {
return nil
}

func New(maxNumSamples, maxNumStats int) storage.StorageDriver {
func New(
maxNumSamples,
maxNumStats int,
backend storage.StorageDriver,
) *InMemoryStorage {
ret := &InMemoryStorage{
containerStorageMap: make(map[string]*containerStorage, 32),
maxNumSamples: maxNumSamples,
maxNumStats: maxNumStats,
backend: backend,
}
return ret
}
26 changes: 3 additions & 23 deletions storage/memory/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,19 @@ import (
)

type memoryTestStorageDriver struct {
base storage.StorageDriver
storage.StorageDriver
}

func (self *memoryTestStorageDriver) StatsEq(a, b *info.ContainerStats) bool {
return test.DefaultStatsEq(a, b)
}

func (self *memoryTestStorageDriver) AddStats(ref info.ContainerReference, stats *info.ContainerStats) error {
return self.base.AddStats(ref, stats)
}

func (self *memoryTestStorageDriver) RecentStats(containerName string, numStats int) ([]*info.ContainerStats, error) {
return self.base.RecentStats(containerName, numStats)
}

func (self *memoryTestStorageDriver) Percentiles(containerName string, cpuUsagePercentiles []int, memUsagePercentiles []int) (*info.ContainerStatsPercentiles, error) {
return self.base.Percentiles(containerName, cpuUsagePercentiles, memUsagePercentiles)
}

func (self *memoryTestStorageDriver) Samples(containerName string, numSamples int) ([]*info.ContainerStatsSample, error) {
return self.base.Samples(containerName, numSamples)
}

func (self *memoryTestStorageDriver) Close() error {
return self.base.Close()
}

func runStorageTest(f func(test.TestStorageDriver, *testing.T), t *testing.T) {
maxSize := 200

for N := 10; N < maxSize; N += 10 {
testDriver := &memoryTestStorageDriver{}
testDriver.base = New(N, N)
testDriver.StorageDriver = New(N, N, nil)
f(testDriver, t)
}
}
Expand All @@ -79,7 +59,7 @@ func TestPercentilesWithoutSample(t *testing.T) {
func TestPercentiles(t *testing.T) {
N := 100
testDriver := &memoryTestStorageDriver{}
testDriver.base = New(N, N)
testDriver.StorageDriver = New(N, N, nil)
test.StorageDriverTestPercentiles(testDriver, t)
}

Expand Down
22 changes: 7 additions & 15 deletions storagedriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/google/cadvisor/manager"
"github.com/google/cadvisor/storage"
"github.com/google/cadvisor/storage/bigquery"
"github.com/google/cadvisor/storage/cache"
"github.com/google/cadvisor/storage/influxdb"
"github.com/google/cadvisor/storage/memory"
)
Expand All @@ -39,8 +38,9 @@ var argDbBufferDuration = flag.Duration("storage_driver_buffer_duration", 60*tim

const statsRequestedByUI = 60

func NewStorageDriver(driverName string) (storage.StorageDriver, error) {
var storageDriver storage.StorageDriver
func NewStorageDriver(driverName string) (*memory.InMemoryStorage, error) {
var storageDriver *memory.InMemoryStorage
var backendStorage storage.StorageDriver
var err error
// TODO(vmarmol): We shouldn't need the housekeeping interval here and it shouldn't be public.
samplesToCache := int(*argDbBufferDuration / *manager.HousekeepingInterval)
Expand All @@ -49,20 +49,14 @@ func NewStorageDriver(driverName string) (storage.StorageDriver, error) {
samplesToCache = statsRequestedByUI
}
switch driverName {
case "":
// empty string by default is the in memory store
fallthrough
case "memory":
storageDriver = memory.New(*argSampleSize, int(*argDbBufferDuration))
return storageDriver, nil
case "influxdb":
var hostname string
hostname, err = os.Hostname()
if err != nil {
return nil, err
}

storageDriver, err = influxdb.New(
backendStorage, err = influxdb.New(
hostname,
"stats",
*argDbName,
Expand All @@ -74,28 +68,26 @@ func NewStorageDriver(driverName string) (storage.StorageDriver, error) {
// TODO(monnand): One hour? Or user-defined?
1*time.Hour,
)
glog.V(2).Infof("Caching %d recent stats in memory\n", samplesToCache)
storageDriver = cache.MemoryCache(samplesToCache, samplesToCache, storageDriver)
case "bigquery":
var hostname string
hostname, err = os.Hostname()
if err != nil {
return nil, err
}
storageDriver, err = bigquery.New(
backendStorage, err = bigquery.New(
hostname,
"cadvisor",
*argDbName,
1*time.Hour,
)
glog.V(2).Infof("Caching %d recent stats in memory\n", samplesToCache)
storageDriver = cache.MemoryCache(samplesToCache, samplesToCache, storageDriver)

default:
err = fmt.Errorf("Unknown database driver: %v", *argDbDriver)
}
if err != nil {
return nil, err
}
glog.V(2).Infof("Caching %d recent stats in memory; using %v storage driver\n", samplesToCache, driverName)
storageDriver = memory.New(samplesToCache, samplesToCache, backendStorage)
return storageDriver, nil
}

0 comments on commit 60a1b6a

Please sign in to comment.